blob: 5ef08e02029ee0be3d48464cc2d5756adb890bbe [file] [log] [blame]
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +09001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package android.text;
18
19import static android.text.Layout.Alignment.ALIGN_NORMAL;
Roozbeh Pournaderde0353d2017-06-06 15:44:03 -070020
Siyamed Sinir0fa89d62017-07-24 20:46:41 -070021import static org.junit.Assert.assertEquals;
Andrei Stingaceanue1a7d0d2017-04-24 16:53:17 +010022import static org.junit.Assert.assertFalse;
23import static org.junit.Assert.assertNotNull;
24import static org.junit.Assert.assertNull;
25import static org.junit.Assert.assertTrue;
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090026
Roozbeh Pournaderde0353d2017-06-06 15:44:03 -070027import android.graphics.Canvas;
28import android.graphics.Paint;
Andrei Stingaceanue1a7d0d2017-04-24 16:53:17 +010029import android.support.test.filters.SmallTest;
30import android.support.test.runner.AndroidJUnit4;
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090031import android.text.style.ReplacementSpan;
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090032
Andrei Stingaceanue1a7d0d2017-04-24 16:53:17 +010033import org.junit.Test;
34import org.junit.runner.RunWith;
35
36@SmallTest
37@RunWith(AndroidJUnit4.class)
38public class DynamicLayoutTest {
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090039 private static final int WIDTH = 10000;
40
Andrei Stingaceanue1a7d0d2017-04-24 16:53:17 +010041 @Test
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090042 public void testGetBlocksAlwaysNeedToBeRedrawn_en() {
43 final SpannableStringBuilder builder = new SpannableStringBuilder();
44 final DynamicLayout layout = new DynamicLayout(builder, new TextPaint(), WIDTH,
45 ALIGN_NORMAL, 0, 0, false);
46
47 assertNull(layout.getBlocksAlwaysNeedToBeRedrawn());
48
49 builder.append("abcd efg\n");
50 builder.append("hijk lmn\n");
51 assertNull(layout.getBlocksAlwaysNeedToBeRedrawn());
52
53 builder.delete(0, builder.length());
54 assertNull(layout.getBlocksAlwaysNeedToBeRedrawn());
55 }
56
Roozbeh Pournaderde0353d2017-06-06 15:44:03 -070057 private class MockReplacementSpan extends ReplacementSpan {
58 public int getSize(Paint paint, CharSequence text, int start, int end,
59 Paint.FontMetricsInt fm) {
60 return 10;
61 }
62
63 public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top,
64 int y, int bottom, Paint paint) { }
65 }
66
Andrei Stingaceanue1a7d0d2017-04-24 16:53:17 +010067 @Test
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090068 public void testGetBlocksAlwaysNeedToBeRedrawn_replacementSpan() {
69 final SpannableStringBuilder builder = new SpannableStringBuilder();
70 final DynamicLayout layout = new DynamicLayout(builder, new TextPaint(), WIDTH,
71 ALIGN_NORMAL, 0, 0, false);
72
73 assertNull(layout.getBlocksAlwaysNeedToBeRedrawn());
74
75 builder.append("abcd efg\n");
76 builder.append("hijk lmn\n");
77 assertNull(layout.getBlocksAlwaysNeedToBeRedrawn());
78
Roozbeh Pournaderde0353d2017-06-06 15:44:03 -070079 builder.setSpan(new MockReplacementSpan(), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090080 assertNotNull(layout.getBlocksAlwaysNeedToBeRedrawn());
81 assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0));
82
Roozbeh Pournaderde0353d2017-06-06 15:44:03 -070083 builder.setSpan(new MockReplacementSpan(), 9, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090084 assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0));
85 assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(1));
86
87 builder.delete(9, 13);
88 assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0));
89 assertFalse(layout.getBlocksAlwaysNeedToBeRedrawn().contains(1));
90
91 builder.delete(0, 4);
92 assertFalse(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0));
93 assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().isEmpty());
94 }
95
Andrei Stingaceanue1a7d0d2017-04-24 16:53:17 +010096 @Test
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090097 public void testGetBlocksAlwaysNeedToBeRedrawn_thai() {
98 final SpannableStringBuilder builder = new SpannableStringBuilder();
99 final DynamicLayout layout = new DynamicLayout(builder, new TextPaint(), WIDTH,
100 ALIGN_NORMAL, 0, 0, false);
101
102 assertNull(layout.getBlocksAlwaysNeedToBeRedrawn());
103
104 builder.append("\u0E22\u0E34\u0E19\u0E14\u0E35\u0E15\u0E49\u0E2D\u0E19\u0E23\u0E31\u0E1A");
105 builder.append("\u0E2A\u0E39\u0E48");
106 assertNull(layout.getBlocksAlwaysNeedToBeRedrawn());
107
108 builder.append("\u0E48\u0E48\u0E48\u0E48\u0E48");
109 assertNotNull(layout.getBlocksAlwaysNeedToBeRedrawn());
110 assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0));
111
112 builder.delete(builder.length() -5, builder.length());
113 assertFalse(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0));
114 assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().isEmpty());
115 }
Siyamed Sinir0fa89d62017-07-24 20:46:41 -0700116
117 @Test
118 public void testGetLineExtra_withoutLinespacing() {
119 final SpannableStringBuilder text = new SpannableStringBuilder("a\nb\nc");
120 final TextPaint textPaint = new TextPaint();
121
122 // create a StaticLayout to check against
123 final StaticLayout staticLayout = StaticLayout.Builder.obtain(text, 0,
124 text.length(), textPaint, WIDTH)
125 .setAlignment(ALIGN_NORMAL)
126 .setIncludePad(false)
127 .build();
128
129 // create the DynamicLayout
130 final DynamicLayout dynamicLayout = new DynamicLayout(text,
131 textPaint,
132 WIDTH,
133 ALIGN_NORMAL,
134 1f /*spacingMultiplier*/,
135 0 /*spacingAdd*/,
136 false /*includepad*/);
137
138 final int lineCount = staticLayout.getLineCount();
139 assertEquals(lineCount, dynamicLayout.getLineCount());
140 for (int i = 0; i < lineCount; i++) {
141 assertEquals(staticLayout.getLineExtra(i), dynamicLayout.getLineExtra(i));
142 }
143 }
144
145 @Test
146 public void testGetLineExtra_withLinespacing() {
147 final SpannableStringBuilder text = new SpannableStringBuilder("a\nb\nc");
148 final TextPaint textPaint = new TextPaint();
149 final float spacingMultiplier = 2f;
150 final float spacingAdd = 4;
151
152 // create a StaticLayout to check against
153 final StaticLayout staticLayout = StaticLayout.Builder.obtain(text, 0,
154 text.length(), textPaint, WIDTH)
155 .setAlignment(ALIGN_NORMAL)
156 .setIncludePad(false)
157 .setLineSpacing(spacingAdd, spacingMultiplier)
158 .build();
159
160 // create the DynamicLayout
161 final DynamicLayout dynamicLayout = new DynamicLayout(text,
162 textPaint,
163 WIDTH,
164 ALIGN_NORMAL,
165 spacingMultiplier,
166 spacingAdd,
167 false /*includepad*/);
168
169 final int lineCount = staticLayout.getLineCount();
170 assertEquals(lineCount, dynamicLayout.getLineCount());
171 for (int i = 0; i < lineCount - 1; i++) {
172 assertEquals(staticLayout.getLineExtra(i), dynamicLayout.getLineExtra(i));
173 }
174 }
175
176 @Test(expected = IndexOutOfBoundsException.class)
177 public void testGetLineExtra_withNegativeValue() {
178 final DynamicLayout layout = new DynamicLayout("", new TextPaint(), 10 /*width*/,
179 ALIGN_NORMAL, 1.0f /*spacingMultiplier*/, 0f /*spacingAdd*/, false /*includepad*/);
180 layout.getLineExtra(-1);
181 }
182
183 @Test(expected = IndexOutOfBoundsException.class)
184 public void testGetLineExtra_withParamGreaterThanLineCount() {
185 final DynamicLayout layout = new DynamicLayout("", new TextPaint(), 10 /*width*/,
186 ALIGN_NORMAL, 1.0f /*spacingMultiplier*/, 0f /*spacingAdd*/, false /*includepad*/);
187 layout.getLineExtra(100);
188 }
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +0900189}