blob: ace6611a52c56cdd63fc5ff2db5ef1707944ab48 [file] [log] [blame]
Yohei Yukawac2ddd602014-05-06 21:22:49 +09001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yohei Yukawa35f74372015-08-12 20:14:08 -070017package android.view.inputmethod;
Yohei Yukawac2ddd602014-05-06 21:22:49 +090018
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +090019import static android.view.inputmethod.CursorAnchorInfo.FLAG_HAS_INVISIBLE_REGION;
20import static android.view.inputmethod.CursorAnchorInfo.FLAG_HAS_VISIBLE_REGION;
21import static android.view.inputmethod.CursorAnchorInfo.FLAG_IS_RTL;
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +090022
Yohei Yukawa8306fc42017-12-11 15:09:28 -080023import static org.junit.Assert.assertEquals;
24import static org.junit.Assert.assertFalse;
25import static org.junit.Assert.assertNull;
26import static org.junit.Assert.assertTrue;
27
28import android.graphics.Matrix;
29import android.graphics.RectF;
30import android.os.Parcel;
Yohei Yukawa8306fc42017-12-11 15:09:28 -080031import android.text.TextUtils;
32import android.view.inputmethod.CursorAnchorInfo.Builder;
33
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090034import androidx.test.filters.SmallTest;
35import androidx.test.runner.AndroidJUnit4;
36
Yohei Yukawa8306fc42017-12-11 15:09:28 -080037import org.junit.Test;
38import org.junit.runner.RunWith;
39
40import java.util.Objects;
41
42@SmallTest
43@RunWith(AndroidJUnit4.class)
44public class CursorAnchorInfoTest {
45 private static final float EPSILON = 0.0000001f;
46
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +090047 private static final RectF[] MANY_BOUNDS = new RectF[] {
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +090048 new RectF(101.0f, 201.0f, 301.0f, 401.0f),
Yohei Yukawac2ddd602014-05-06 21:22:49 +090049 new RectF(102.0f, 202.0f, 302.0f, 402.0f),
50 new RectF(103.0f, 203.0f, 303.0f, 403.0f),
51 new RectF(104.0f, 204.0f, 304.0f, 404.0f),
52 new RectF(105.0f, 205.0f, 305.0f, 405.0f),
53 new RectF(106.0f, 206.0f, 306.0f, 406.0f),
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +090054 new RectF(107.0f, 207.0f, 307.0f, 407.0f),
Yohei Yukawac2ddd602014-05-06 21:22:49 +090055 new RectF(108.0f, 208.0f, 308.0f, 408.0f),
56 new RectF(109.0f, 209.0f, 309.0f, 409.0f),
57 new RectF(110.0f, 210.0f, 310.0f, 410.0f),
58 new RectF(111.0f, 211.0f, 311.0f, 411.0f),
59 new RectF(112.0f, 212.0f, 312.0f, 412.0f),
60 new RectF(113.0f, 213.0f, 313.0f, 413.0f),
61 new RectF(114.0f, 214.0f, 314.0f, 414.0f),
62 new RectF(115.0f, 215.0f, 315.0f, 415.0f),
63 new RectF(116.0f, 216.0f, 316.0f, 416.0f),
64 new RectF(117.0f, 217.0f, 317.0f, 417.0f),
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +090065 new RectF(118.0f, 218.0f, 318.0f, 418.0f),
66 new RectF(119.0f, 219.0f, 319.0f, 419.0f),
67 };
68 private static final int[] MANY_FLAGS_ARRAY = new int[] {
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +090069 FLAG_HAS_INVISIBLE_REGION,
70 FLAG_HAS_INVISIBLE_REGION | FLAG_HAS_VISIBLE_REGION,
71 FLAG_HAS_VISIBLE_REGION,
72 FLAG_HAS_VISIBLE_REGION,
73 FLAG_HAS_VISIBLE_REGION,
74 FLAG_HAS_VISIBLE_REGION,
75 FLAG_HAS_VISIBLE_REGION | FLAG_IS_RTL,
76 FLAG_HAS_INVISIBLE_REGION | FLAG_HAS_VISIBLE_REGION | FLAG_IS_RTL,
77 FLAG_HAS_INVISIBLE_REGION | FLAG_IS_RTL,
78 FLAG_HAS_VISIBLE_REGION | FLAG_IS_RTL,
79 FLAG_HAS_VISIBLE_REGION,
80 FLAG_HAS_VISIBLE_REGION | FLAG_IS_RTL,
81 FLAG_HAS_VISIBLE_REGION,
82 FLAG_HAS_VISIBLE_REGION | FLAG_IS_RTL,
83 FLAG_HAS_VISIBLE_REGION,
84 FLAG_HAS_VISIBLE_REGION | FLAG_IS_RTL,
85 FLAG_HAS_VISIBLE_REGION,
86 FLAG_HAS_INVISIBLE_REGION,
87 FLAG_HAS_INVISIBLE_REGION | FLAG_IS_RTL,
Yohei Yukawac2ddd602014-05-06 21:22:49 +090088 };
89
Yohei Yukawa8306fc42017-12-11 15:09:28 -080090 @Test
Yohei Yukawac2ddd602014-05-06 21:22:49 +090091 public void testBuilder() throws Exception {
92 final int SELECTION_START = 30;
93 final int SELECTION_END = 40;
Yohei Yukawa81f4cb32014-05-13 22:20:35 +090094 final int COMPOSING_TEXT_START = 32;
95 final String COMPOSING_TEXT = "test";
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +090096 final int INSERTION_MARKER_FLAGS =
97 FLAG_HAS_VISIBLE_REGION | FLAG_HAS_INVISIBLE_REGION | FLAG_IS_RTL;
Yohei Yukawac2ddd602014-05-06 21:22:49 +090098 final float INSERTION_MARKER_HORIZONTAL = 10.5f;
99 final float INSERTION_MARKER_TOP = 100.1f;
100 final float INSERTION_MARKER_BASELINE = 110.4f;
101 final float INSERTION_MARKER_BOTOM = 111.0f;
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900102
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900103 Matrix TRANSFORM_MATRIX = new Matrix(Matrix.IDENTITY_MATRIX);
104 TRANSFORM_MATRIX.setScale(10.0f, 20.0f);
105
Yohei Yukawac46b5f02014-06-10 12:26:34 +0900106 final Builder builder = new Builder();
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900107 builder.setSelectionRange(SELECTION_START, SELECTION_END)
Yohei Yukawa81f4cb32014-05-13 22:20:35 +0900108 .setComposingText(COMPOSING_TEXT_START, COMPOSING_TEXT)
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900109 .setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL, INSERTION_MARKER_TOP,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900110 INSERTION_MARKER_BASELINE, INSERTION_MARKER_BOTOM, INSERTION_MARKER_FLAGS)
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900111 .setMatrix(TRANSFORM_MATRIX);
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900112 for (int i = 0; i < MANY_BOUNDS.length; i++) {
113 final RectF bounds = MANY_BOUNDS[i];
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900114 final int flags = MANY_FLAGS_ARRAY[i];
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900115 builder.addCharacterBounds(i, bounds.left, bounds.top, bounds.right, bounds.bottom,
116 flags);
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900117 }
118
119 final CursorAnchorInfo info = builder.build();
120 assertEquals(SELECTION_START, info.getSelectionStart());
121 assertEquals(SELECTION_END, info.getSelectionEnd());
Yohei Yukawa81f4cb32014-05-13 22:20:35 +0900122 assertEquals(COMPOSING_TEXT_START, info.getComposingTextStart());
Yohei Yukawa9a9c1122014-06-27 17:13:32 +0900123 assertTrue(TextUtils.equals(COMPOSING_TEXT, info.getComposingText()));
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900124 assertEquals(INSERTION_MARKER_FLAGS, info.getInsertionMarkerFlags());
Yohei Yukawa8306fc42017-12-11 15:09:28 -0800125 assertEquals(INSERTION_MARKER_HORIZONTAL, info.getInsertionMarkerHorizontal(), EPSILON);
126 assertEquals(INSERTION_MARKER_TOP, info.getInsertionMarkerTop(), EPSILON);
127 assertEquals(INSERTION_MARKER_BASELINE, info.getInsertionMarkerBaseline(), EPSILON);
128 assertEquals(INSERTION_MARKER_BOTOM, info.getInsertionMarkerBottom(), EPSILON);
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900129 assertEquals(TRANSFORM_MATRIX, info.getMatrix());
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900130 for (int i = 0; i < MANY_BOUNDS.length; i++) {
131 final RectF expectedBounds = MANY_BOUNDS[i];
Yohei Yukawa699a49b2014-09-09 12:39:49 +0900132 assertEquals(expectedBounds, info.getCharacterBounds(i));
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900133 }
Yohei Yukawa699a49b2014-09-09 12:39:49 +0900134 assertNull(info.getCharacterBounds(-1));
135 assertNull(info.getCharacterBounds(MANY_BOUNDS.length + 1));
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900136 for (int i = 0; i < MANY_FLAGS_ARRAY.length; i++) {
137 final int expectedFlags = MANY_FLAGS_ARRAY[i];
Yohei Yukawa699a49b2014-09-09 12:39:49 +0900138 assertEquals(expectedFlags, info.getCharacterBoundsFlags(i));
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900139 }
Yohei Yukawa699a49b2014-09-09 12:39:49 +0900140 assertEquals(0, info.getCharacterBoundsFlags(-1));
141 assertEquals(0, info.getCharacterBoundsFlags(MANY_BOUNDS.length + 1));
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900142
143 // Make sure that the builder can reproduce the same object.
144 final CursorAnchorInfo info2 = builder.build();
145 assertEquals(SELECTION_START, info2.getSelectionStart());
146 assertEquals(SELECTION_END, info2.getSelectionEnd());
Yohei Yukawa81f4cb32014-05-13 22:20:35 +0900147 assertEquals(COMPOSING_TEXT_START, info2.getComposingTextStart());
Yohei Yukawa9a9c1122014-06-27 17:13:32 +0900148 assertTrue(TextUtils.equals(COMPOSING_TEXT, info2.getComposingText()));
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900149 assertEquals(INSERTION_MARKER_FLAGS, info2.getInsertionMarkerFlags());
Yohei Yukawa8306fc42017-12-11 15:09:28 -0800150 assertEquals(INSERTION_MARKER_HORIZONTAL, info2.getInsertionMarkerHorizontal(), EPSILON);
151 assertEquals(INSERTION_MARKER_TOP, info2.getInsertionMarkerTop(), EPSILON);
152 assertEquals(INSERTION_MARKER_BASELINE, info2.getInsertionMarkerBaseline(), EPSILON);
153 assertEquals(INSERTION_MARKER_BOTOM, info2.getInsertionMarkerBottom(), EPSILON);
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900154 assertEquals(TRANSFORM_MATRIX, info2.getMatrix());
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900155 for (int i = 0; i < MANY_BOUNDS.length; i++) {
156 final RectF expectedBounds = MANY_BOUNDS[i];
Yohei Yukawa699a49b2014-09-09 12:39:49 +0900157 assertEquals(expectedBounds, info2.getCharacterBounds(i));
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900158 }
Yohei Yukawa699a49b2014-09-09 12:39:49 +0900159 assertNull(info2.getCharacterBounds(-1));
160 assertNull(info2.getCharacterBounds(MANY_BOUNDS.length + 1));
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900161 for (int i = 0; i < MANY_FLAGS_ARRAY.length; i++) {
162 final int expectedFlags = MANY_FLAGS_ARRAY[i];
Yohei Yukawa699a49b2014-09-09 12:39:49 +0900163 assertEquals(expectedFlags, info2.getCharacterBoundsFlags(i));
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900164 }
Yohei Yukawa699a49b2014-09-09 12:39:49 +0900165 assertEquals(0, info2.getCharacterBoundsFlags(-1));
166 assertEquals(0, info2.getCharacterBoundsFlags(MANY_BOUNDS.length + 1));
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900167 assertEquals(info, info2);
168 assertEquals(info.hashCode(), info2.hashCode());
169
Yohei Yukawa9a9c1122014-06-27 17:13:32 +0900170 // Make sure that object can be marshaled via {@link Parsel}.
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900171 final CursorAnchorInfo info3 = cloneViaParcel(info2);
172 assertEquals(SELECTION_START, info3.getSelectionStart());
173 assertEquals(SELECTION_END, info3.getSelectionEnd());
Yohei Yukawa81f4cb32014-05-13 22:20:35 +0900174 assertEquals(COMPOSING_TEXT_START, info3.getComposingTextStart());
Yohei Yukawa9a9c1122014-06-27 17:13:32 +0900175 assertTrue(TextUtils.equals(COMPOSING_TEXT, info3.getComposingText()));
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900176 assertEquals(INSERTION_MARKER_FLAGS, info3.getInsertionMarkerFlags());
Yohei Yukawa8306fc42017-12-11 15:09:28 -0800177 assertEquals(INSERTION_MARKER_HORIZONTAL, info3.getInsertionMarkerHorizontal(), EPSILON);
178 assertEquals(INSERTION_MARKER_TOP, info3.getInsertionMarkerTop(), EPSILON);
179 assertEquals(INSERTION_MARKER_BASELINE, info3.getInsertionMarkerBaseline(), EPSILON);
180 assertEquals(INSERTION_MARKER_BOTOM, info3.getInsertionMarkerBottom(), EPSILON);
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900181 assertEquals(TRANSFORM_MATRIX, info3.getMatrix());
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900182 for (int i = 0; i < MANY_BOUNDS.length; i++) {
183 final RectF expectedBounds = MANY_BOUNDS[i];
Yohei Yukawa699a49b2014-09-09 12:39:49 +0900184 assertEquals(expectedBounds, info3.getCharacterBounds(i));
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900185 }
Yohei Yukawa699a49b2014-09-09 12:39:49 +0900186 assertNull(info3.getCharacterBounds(-1));
187 assertNull(info3.getCharacterBounds(MANY_BOUNDS.length + 1));
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900188 for (int i = 0; i < MANY_FLAGS_ARRAY.length; i++) {
189 final int expectedFlags = MANY_FLAGS_ARRAY[i];
Yohei Yukawa699a49b2014-09-09 12:39:49 +0900190 assertEquals(expectedFlags, info3.getCharacterBoundsFlags(i));
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900191 }
Yohei Yukawa699a49b2014-09-09 12:39:49 +0900192 assertEquals(0, info3.getCharacterBoundsFlags(-1));
193 assertEquals(0, info3.getCharacterBoundsFlags(MANY_BOUNDS.length + 1));
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900194 assertEquals(info.hashCode(), info3.hashCode());
195
196 builder.reset();
197 final CursorAnchorInfo uninitializedInfo = builder.build();
198 assertEquals(-1, uninitializedInfo.getSelectionStart());
199 assertEquals(-1, uninitializedInfo.getSelectionEnd());
Yohei Yukawa81f4cb32014-05-13 22:20:35 +0900200 assertEquals(-1, uninitializedInfo.getComposingTextStart());
201 assertNull(uninitializedInfo.getComposingText());
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900202 assertEquals(0, uninitializedInfo.getInsertionMarkerFlags());
Yohei Yukawa8306fc42017-12-11 15:09:28 -0800203 assertEquals(Float.NaN, uninitializedInfo.getInsertionMarkerHorizontal(), EPSILON);
204 assertEquals(Float.NaN, uninitializedInfo.getInsertionMarkerTop(), EPSILON);
205 assertEquals(Float.NaN, uninitializedInfo.getInsertionMarkerBaseline(), EPSILON);
206 assertEquals(Float.NaN, uninitializedInfo.getInsertionMarkerBottom(), EPSILON);
Yohei Yukawab5268dc2014-06-27 16:16:47 +0900207 assertEquals(Matrix.IDENTITY_MATRIX, uninitializedInfo.getMatrix());
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900208 }
209
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900210 private static void assertNotEquals(final CursorAnchorInfo reference,
211 final CursorAnchorInfo actual) {
212 assertFalse(Objects.equals(reference, actual));
213 }
214
Yohei Yukawa8306fc42017-12-11 15:09:28 -0800215 @Test
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900216 public void testEquality() throws Exception {
217 final Matrix MATRIX1 = new Matrix();
218 MATRIX1.setTranslate(10.0f, 20.0f);
219 final Matrix MATRIX2 = new Matrix();
220 MATRIX2.setTranslate(110.0f, 120.0f);
221 final Matrix NAN_MATRIX = new Matrix();
222 NAN_MATRIX.setValues(new float[]{
223 Float.NaN, Float.NaN, Float.NaN,
224 Float.NaN, Float.NaN, Float.NaN,
225 Float.NaN, Float.NaN, Float.NaN});
226 final int SELECTION_START1 = 2;
227 final int SELECTION_END1 = 7;
228 final String COMPOSING_TEXT1 = "0123456789";
229 final int COMPOSING_TEXT_START1 = 0;
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900230 final int INSERTION_MARKER_FLAGS1 = FLAG_HAS_VISIBLE_REGION;
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900231 final float INSERTION_MARKER_HORIZONTAL1 = 10.5f;
232 final float INSERTION_MARKER_TOP1 = 100.1f;
233 final float INSERTION_MARKER_BASELINE1 = 110.4f;
234 final float INSERTION_MARKER_BOTOM1 = 111.0f;
235 final int SELECTION_START2 = 4;
236 final int SELECTION_END2 = 8;
237 final String COMPOSING_TEXT2 = "9876543210";
238 final int COMPOSING_TEXT_START2 = 3;
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900239 final int INSERTION_MARKER_FLAGS2 =
240 FLAG_HAS_VISIBLE_REGION | FLAG_HAS_INVISIBLE_REGION | FLAG_IS_RTL;
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900241 final float INSERTION_MARKER_HORIZONTAL2 = 14.5f;
242 final float INSERTION_MARKER_TOP2 = 200.1f;
243 final float INSERTION_MARKER_BASELINE2 = 210.4f;
244 final float INSERTION_MARKER_BOTOM2 = 211.0f;
245
246 // Default instance should be equal.
247 assertEquals(new Builder().build(), new Builder().build());
248
249 assertEquals(
250 new Builder().setSelectionRange(SELECTION_START1, SELECTION_END1).build(),
251 new Builder().setSelectionRange(SELECTION_START1, SELECTION_END1).build());
252 assertNotEquals(
253 new Builder().setSelectionRange(SELECTION_START1, SELECTION_END1).build(),
254 new Builder().setSelectionRange(SELECTION_START1, SELECTION_END2).build());
255 assertNotEquals(
256 new Builder().setSelectionRange(SELECTION_START1, SELECTION_END1).build(),
257 new Builder().setSelectionRange(SELECTION_START2, SELECTION_END1).build());
258 assertNotEquals(
259 new Builder().setSelectionRange(SELECTION_START1, SELECTION_END1).build(),
260 new Builder().setSelectionRange(SELECTION_START2, SELECTION_END2).build());
261 assertEquals(
262 new Builder().setComposingText(COMPOSING_TEXT_START1, COMPOSING_TEXT1).build(),
263 new Builder().setComposingText(COMPOSING_TEXT_START1, COMPOSING_TEXT1).build());
264 assertNotEquals(
265 new Builder().setComposingText(COMPOSING_TEXT_START1, COMPOSING_TEXT1).build(),
266 new Builder().setComposingText(COMPOSING_TEXT_START2, COMPOSING_TEXT1).build());
267 assertNotEquals(
268 new Builder().setComposingText(COMPOSING_TEXT_START1, COMPOSING_TEXT1).build(),
269 new Builder().setComposingText(COMPOSING_TEXT_START1, COMPOSING_TEXT2).build());
270 assertNotEquals(
271 new Builder().setComposingText(COMPOSING_TEXT_START1, COMPOSING_TEXT1).build(),
272 new Builder().setComposingText(COMPOSING_TEXT_START2, COMPOSING_TEXT2).build());
273
274 // For insertion marker locations, {@link Float#NaN} is treated as if it was a number.
275 assertEquals(
276 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900277 Float.NaN, Float.NaN, Float.NaN, Float.NaN,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900278 INSERTION_MARKER_FLAGS1).build(),
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900279 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900280 Float.NaN, Float.NaN, Float.NaN, Float.NaN,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900281 INSERTION_MARKER_FLAGS1).build());
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900282
283 // Check Matrix.
284 assertEquals(
285 new Builder().setMatrix(MATRIX1).build(),
286 new Builder().setMatrix(MATRIX1).build());
287 assertNotEquals(
288 new Builder().setMatrix(MATRIX1).build(),
289 new Builder().setMatrix(MATRIX2).build());
290 assertNotEquals(
291 new Builder().setMatrix(MATRIX1).build(),
292 new Builder().setMatrix(NAN_MATRIX).build());
293 // Unlike insertion marker locations, {@link Float#NaN} in the matrix is treated as just a
294 // NaN as usual (NaN == NaN -> false).
295 assertNotEquals(
296 new Builder().setMatrix(NAN_MATRIX).build(),
297 new Builder().setMatrix(NAN_MATRIX).build());
298
299 assertEquals(
300 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
301 INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1,
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900302 INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900303 INSERTION_MARKER_FLAGS1).build(),
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900304 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
305 INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1,
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900306 INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900307 INSERTION_MARKER_FLAGS1).build());
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900308 assertNotEquals(
309 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
310 Float.NaN, INSERTION_MARKER_TOP1,
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900311 INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900312 INSERTION_MARKER_FLAGS1).build(),
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900313 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
314 INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1,
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900315 INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900316 INSERTION_MARKER_FLAGS1).build());
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900317 assertNotEquals(
318 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
319 INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1,
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900320 INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900321 INSERTION_MARKER_FLAGS1).build(),
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900322 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
323 INSERTION_MARKER_HORIZONTAL2, INSERTION_MARKER_TOP1,
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900324 INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900325 INSERTION_MARKER_FLAGS1).build());
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900326 assertNotEquals(
327 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
328 INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1,
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900329 INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900330 INSERTION_MARKER_FLAGS1).build(),
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900331 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
332 INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP2,
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900333 INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900334 INSERTION_MARKER_FLAGS1).build());
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900335 assertNotEquals(
336 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
337 INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1,
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900338 INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900339 INSERTION_MARKER_FLAGS1).build(),
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900340 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
341 INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1,
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900342 INSERTION_MARKER_BASELINE2, INSERTION_MARKER_BOTOM1,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900343 INSERTION_MARKER_FLAGS1).build());
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900344 assertNotEquals(
345 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
346 INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1,
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900347 INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900348 INSERTION_MARKER_FLAGS1).build(),
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900349 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
350 INSERTION_MARKER_HORIZONTAL2, INSERTION_MARKER_TOP1,
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900351 INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900352 INSERTION_MARKER_FLAGS1).build());
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900353 assertNotEquals(
354 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
355 INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1,
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900356 INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900357 INSERTION_MARKER_FLAGS1).build(),
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900358 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
359 INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1,
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900360 INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM2,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900361 INSERTION_MARKER_FLAGS1).build());
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900362 assertNotEquals(
363 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
364 INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1,
365 INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900366 INSERTION_MARKER_FLAGS1).build(),
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900367 new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(
368 INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1,
369 INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900370 INSERTION_MARKER_FLAGS2).build());
Yohei Yukawaeea0b8b2014-07-08 19:46:11 +0900371 }
372
Yohei Yukawa8306fc42017-12-11 15:09:28 -0800373 @Test
Yohei Yukawa419b1b042014-05-15 15:36:23 +0900374 public void testMatrixIsCopied() throws Exception {
375 final Matrix MATRIX1 = new Matrix();
376 MATRIX1.setTranslate(10.0f, 20.0f);
377 final Matrix MATRIX2 = new Matrix();
378 MATRIX2.setTranslate(110.0f, 120.0f);
379 final Matrix MATRIX3 = new Matrix();
380 MATRIX3.setTranslate(210.0f, 220.0f);
381 final Matrix matrix = new Matrix();
Yohei Yukawac46b5f02014-06-10 12:26:34 +0900382 final Builder builder = new Builder();
Yohei Yukawa419b1b042014-05-15 15:36:23 +0900383
384 matrix.set(MATRIX1);
385 builder.setMatrix(matrix);
386 matrix.postRotate(90.0f);
387
388 final CursorAnchorInfo firstInstance = builder.build();
389 assertEquals(MATRIX1, firstInstance.getMatrix());
390 matrix.set(MATRIX2);
391 builder.setMatrix(matrix);
392 final CursorAnchorInfo secondInstance = builder.build();
393 assertEquals(MATRIX1, firstInstance.getMatrix());
394 assertEquals(MATRIX2, secondInstance.getMatrix());
395
396 matrix.set(MATRIX3);
397 assertEquals(MATRIX1, firstInstance.getMatrix());
398 assertEquals(MATRIX2, secondInstance.getMatrix());
399 }
400
Yohei Yukawa8306fc42017-12-11 15:09:28 -0800401 @Test
Yohei Yukawab5268dc2014-06-27 16:16:47 +0900402 public void testMatrixIsRequired() throws Exception {
403 final int SELECTION_START = 30;
404 final int SELECTION_END = 40;
405 final int COMPOSING_TEXT_START = 32;
406 final String COMPOSING_TEXT = "test";
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900407 final int INSERTION_MARKER_FLAGS = FLAG_HAS_VISIBLE_REGION;
Yohei Yukawab5268dc2014-06-27 16:16:47 +0900408 final float INSERTION_MARKER_HORIZONTAL = 10.5f;
409 final float INSERTION_MARKER_TOP = 100.1f;
410 final float INSERTION_MARKER_BASELINE = 110.4f;
411 final float INSERTION_MARKER_BOTOM = 111.0f;
412 Matrix TRANSFORM_MATRIX = new Matrix(Matrix.IDENTITY_MATRIX);
413 TRANSFORM_MATRIX.setScale(10.0f, 20.0f);
414
415 final Builder builder = new Builder();
416 // Check twice to make sure if Builder#reset() works as expected.
417 for (int repeatCount = 0; repeatCount < 2; ++repeatCount) {
418 builder.setSelectionRange(SELECTION_START, SELECTION_END)
419 .setComposingText(COMPOSING_TEXT_START, COMPOSING_TEXT);
420 try {
421 // Should succeed as coordinate transformation matrix is not required if no
422 // positional information is specified.
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900423 builder.build();
Yohei Yukawab5268dc2014-06-27 16:16:47 +0900424 } catch (IllegalArgumentException ex) {
425 assertTrue(false);
426 }
427
428 builder.setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL, INSERTION_MARKER_TOP,
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900429 INSERTION_MARKER_BASELINE, INSERTION_MARKER_BOTOM, INSERTION_MARKER_FLAGS);
Yohei Yukawab5268dc2014-06-27 16:16:47 +0900430 try {
431 // Coordinate transformation matrix is required if no positional information is
432 // specified.
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900433 builder.build();
434 assertTrue(false);
Yohei Yukawab5268dc2014-06-27 16:16:47 +0900435 } catch (IllegalArgumentException ex) {
Yohei Yukawab5268dc2014-06-27 16:16:47 +0900436 }
437
438 builder.setMatrix(TRANSFORM_MATRIX);
439 try {
440 // Should succeed as coordinate transformation matrix is required.
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900441 builder.build();
Yohei Yukawab5268dc2014-06-27 16:16:47 +0900442 } catch (IllegalArgumentException ex) {
443 assertTrue(false);
444 }
445
446 builder.reset();
447 }
448 }
449
Yohei Yukawa8306fc42017-12-11 15:09:28 -0800450 @Test
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900451 public void testBuilderAddCharacterBounds() throws Exception {
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900452 // A negative index should be rejected.
453 try {
Yohei Yukawaa41c4bc2014-09-08 15:56:20 +0900454 new Builder().addCharacterBounds(-1, 0.0f, 0.0f, 0.0f, 0.0f, FLAG_HAS_VISIBLE_REGION);
Yohei Yukawa0b01e7f2014-07-08 15:29:51 +0900455 assertTrue(false);
456 } catch (IllegalArgumentException ex) {
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900457 }
458 }
459
460 private static CursorAnchorInfo cloneViaParcel(final CursorAnchorInfo src) {
461 Parcel parcel = null;
462 try {
463 parcel = Parcel.obtain();
464 src.writeToParcel(parcel, 0);
465 parcel.setDataPosition(0);
466 return new CursorAnchorInfo(parcel);
467 } finally {
468 if (parcel != null) {
469 parcel.recycle();
470 }
471 }
472 }
473}