1 /*
2  * Copyright (C) 2012 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 
17 package com.android.inputmethod.latin.common;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNotSame;
22 import static org.junit.Assert.assertSame;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 
26 import androidx.test.filters.SmallTest;
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 import java.util.Arrays;
33 
34 @SmallTest
35 @RunWith(AndroidJUnit4.class)
36 public class InputPointersTests {
37     private static final int DEFAULT_CAPACITY = 48;
38 
39     @Test
testNewInstance()40     public void testNewInstance() {
41         final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
42         assertEquals("new instance size", 0, src.getPointerSize());
43         assertNotNull("new instance xCoordinates", src.getXCoordinates());
44         assertNotNull("new instance yCoordinates", src.getYCoordinates());
45         assertNotNull("new instance pointerIds", src.getPointerIds());
46         assertNotNull("new instance times", src.getTimes());
47     }
48 
49     @Test
testReset()50     public void testReset() {
51         final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
52         final int[] xCoordinates = src.getXCoordinates();
53         final int[] yCoordinates = src.getXCoordinates();
54         final int[] pointerIds = src.getXCoordinates();
55         final int[] times = src.getXCoordinates();
56 
57         src.reset();
58         assertEquals("size after reset", 0, src.getPointerSize());
59         assertNotSame("xCoordinates after reset", xCoordinates, src.getXCoordinates());
60         assertNotSame("yCoordinates after reset", yCoordinates, src.getYCoordinates());
61         assertNotSame("pointerIds after reset", pointerIds, src.getPointerIds());
62         assertNotSame("times after reset", times, src.getTimes());
63     }
64 
65     @Test
testAdd()66     public void testAdd() {
67         final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
68         final int limit = src.getXCoordinates().length * 2 + 10;
69         for (int i = 0; i < limit; i++) {
70             final int x = i;
71             final int y = i * 2;
72             final int pointerId = i * 3;
73             final int time = i * 4;
74             src.addPointer(x, y, pointerId, time);
75             assertEquals("size after add " + i, i + 1, src.getPointerSize());
76         }
77         for (int i = 0; i < limit; i++) {
78             final int x = i;
79             final int y = i * 2;
80             final int pointerId = i * 3;
81             final int time = i * 4;
82             assertEquals("xCoordinates at " + i, x, src.getXCoordinates()[i]);
83             assertEquals("yCoordinates at " + i, y, src.getYCoordinates()[i]);
84             assertEquals("pointerIds at " + i, pointerId, src.getPointerIds()[i]);
85             assertEquals("times at " + i, time, src.getTimes()[i]);
86         }
87     }
88 
89     @Test
testAddAt()90     public void testAddAt() {
91         final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
92         final int limit = 1000, step = 100;
93         for (int i = 0; i < limit; i += step) {
94             final int x = i;
95             final int y = i * 2;
96             final int pointerId = i * 3;
97             final int time = i * 4;
98             src.addPointerAt(i, x, y, pointerId, time);
99             assertEquals("size after add at " + i, i + 1, src.getPointerSize());
100         }
101         for (int i = 0; i < limit; i += step) {
102             final int x = i;
103             final int y = i * 2;
104             final int pointerId = i * 3;
105             final int time = i * 4;
106             assertEquals("xCoordinates at " + i, x, src.getXCoordinates()[i]);
107             assertEquals("yCoordinates at " + i, y, src.getYCoordinates()[i]);
108             assertEquals("pointerIds at " + i, pointerId, src.getPointerIds()[i]);
109             assertEquals("times at " + i, time, src.getTimes()[i]);
110         }
111     }
112 
113     @Test
testSet()114     public void testSet() {
115         final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
116         final int limit = src.getXCoordinates().length * 2 + 10;
117         for (int i = 0; i < limit; i++) {
118             final int x = i;
119             final int y = i * 2;
120             final int pointerId = i * 3;
121             final int time = i * 4;
122             src.addPointer(x, y, pointerId, time);
123         }
124         final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
125         dst.set(src);
126         assertEquals("size after set", dst.getPointerSize(), src.getPointerSize());
127         assertSame("xCoordinates after set", dst.getXCoordinates(), src.getXCoordinates());
128         assertSame("yCoordinates after set", dst.getYCoordinates(), src.getYCoordinates());
129         assertSame("pointerIds after set", dst.getPointerIds(), src.getPointerIds());
130         assertSame("times after set", dst.getTimes(), src.getTimes());
131     }
132 
133     @Test
testCopy()134     public void testCopy() {
135         final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
136         final int limit = 100;
137         for (int i = 0; i < limit; i++) {
138             final int x = i;
139             final int y = i * 2;
140             final int pointerId = i * 3;
141             final int time = i * 4;
142             src.addPointer(x, y, pointerId, time);
143         }
144         final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
145         dst.copy(src);
146         assertEquals("size after copy", dst.getPointerSize(), src.getPointerSize());
147         assertNotSame("xCoordinates after copy", dst.getXCoordinates(), src.getXCoordinates());
148         assertNotSame("yCoordinates after copy", dst.getYCoordinates(), src.getYCoordinates());
149         assertNotSame("pointerIds after copy", dst.getPointerIds(), src.getPointerIds());
150         assertNotSame("times after copy", dst.getTimes(), src.getTimes());
151         final int size = dst.getPointerSize();
152         assertIntArrayEquals("xCoordinates values after copy",
153                 dst.getXCoordinates(), 0, src.getXCoordinates(), 0, size);
154         assertIntArrayEquals("yCoordinates values after copy",
155                 dst.getYCoordinates(), 0, src.getYCoordinates(), 0, size);
156         assertIntArrayEquals("pointerIds values after copy",
157                 dst.getPointerIds(), 0, src.getPointerIds(), 0, size);
158         assertIntArrayEquals("times values after copy",
159                 dst.getTimes(), 0, src.getTimes(), 0, size);
160     }
161 
162     @Test
testAppend()163     public void testAppend() {
164         final int dstLength = 50;
165         final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
166         for (int i = 0; i < dstLength; i++) {
167             final int x = i * 4;
168             final int y = i * 3;
169             final int pointerId = i * 2;
170             final int time = i;
171             dst.addPointer(x, y, pointerId, time);
172         }
173         final InputPointers dstCopy = new InputPointers(DEFAULT_CAPACITY);
174         dstCopy.copy(dst);
175 
176         final ResizableIntArray srcXCoords = new ResizableIntArray(DEFAULT_CAPACITY);
177         final ResizableIntArray srcYCoords = new ResizableIntArray(DEFAULT_CAPACITY);
178         final ResizableIntArray srcPointerIds = new ResizableIntArray(DEFAULT_CAPACITY);
179         final ResizableIntArray srcTimes = new ResizableIntArray(DEFAULT_CAPACITY);
180         final int srcLength = 100;
181         final int srcPointerId = 10;
182         for (int i = 0; i < srcLength; i++) {
183             final int x = i;
184             final int y = i * 2;
185             // The time value must be larger than <code>dst</code>.
186             final int time = i * 4 + dstLength;
187             srcXCoords.add(x);
188             srcYCoords.add(y);
189             srcPointerIds.add(srcPointerId);
190             srcTimes.add(time);
191         }
192 
193         final int startPos = 0;
194         dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords,
195                 startPos, 0 /* length */);
196         assertEquals("size after append zero", dstLength, dst.getPointerSize());
197         assertIntArrayEquals("xCoordinates after append zero",
198                 dstCopy.getXCoordinates(), startPos, dst.getXCoordinates(), startPos, dstLength);
199         assertIntArrayEquals("yCoordinates after append zero",
200                 dstCopy.getYCoordinates(), startPos, dst.getYCoordinates(), startPos, dstLength);
201         assertIntArrayEquals("pointerIds after append zero",
202                 dstCopy.getPointerIds(), startPos, dst.getPointerIds(), startPos, dstLength);
203         assertIntArrayEquals("times after append zero",
204                 dstCopy.getTimes(), startPos, dst.getTimes(), startPos, dstLength);
205 
206         dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords,
207                 startPos, srcLength);
208         assertEquals("size after append", dstLength + srcLength, dst.getPointerSize());
209         assertTrue("primitive length after append",
210                 dst.getPointerIds().length >= dstLength + srcLength);
211         assertIntArrayEquals("original xCoordinates values after append",
212                 dstCopy.getXCoordinates(), startPos, dst.getXCoordinates(), startPos, dstLength);
213         assertIntArrayEquals("original yCoordinates values after append",
214                 dstCopy.getYCoordinates(), startPos, dst.getYCoordinates(), startPos, dstLength);
215         assertIntArrayEquals("original pointerIds values after append",
216                 dstCopy.getPointerIds(), startPos, dst.getPointerIds(), startPos, dstLength);
217         assertIntArrayEquals("original times values after append",
218                 dstCopy.getTimes(), startPos, dst.getTimes(), startPos, dstLength);
219         assertIntArrayEquals("appended xCoordinates values after append",
220                 srcXCoords.getPrimitiveArray(), startPos, dst.getXCoordinates(),
221                 dstLength, srcLength);
222         assertIntArrayEquals("appended yCoordinates values after append",
223                 srcYCoords.getPrimitiveArray(), startPos, dst.getYCoordinates(),
224                 dstLength, srcLength);
225         assertIntArrayEquals("appended pointerIds values after append",
226                 srcPointerIds.getPrimitiveArray(), startPos, dst.getPointerIds(),
227                 dstLength, srcLength);
228         assertIntArrayEquals("appended times values after append",
229                 srcTimes.getPrimitiveArray(), startPos, dst.getTimes(), dstLength, srcLength);
230     }
231 
232     @Test
testAppendResizableIntArray()233     public void testAppendResizableIntArray() {
234         final int dstLength = 50;
235         final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
236         for (int i = 0; i < dstLength; i++) {
237             final int x = i * 4;
238             final int y = i * 3;
239             final int pointerId = i * 2;
240             final int time = i;
241             dst.addPointer(x, y, pointerId, time);
242         }
243         final InputPointers dstCopy = new InputPointers(DEFAULT_CAPACITY);
244         dstCopy.copy(dst);
245 
246         final int srcLength = 100;
247         final int srcPointerId = 1;
248         final int[] srcPointerIds = new int[srcLength];
249         Arrays.fill(srcPointerIds, srcPointerId);
250         final ResizableIntArray srcTimes = new ResizableIntArray(DEFAULT_CAPACITY);
251         final ResizableIntArray srcXCoords = new ResizableIntArray(DEFAULT_CAPACITY);
252         final ResizableIntArray srcYCoords= new ResizableIntArray(DEFAULT_CAPACITY);
253         for (int i = 0; i < srcLength; i++) {
254             // The time value must be larger than <code>dst</code>.
255             final int time = i * 2 + dstLength;
256             final int x = i * 3;
257             final int y = i * 4;
258             srcTimes.add(time);
259             srcXCoords.add(x);
260             srcYCoords.add(y);
261         }
262 
263         dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, 0);
264         assertEquals("size after append zero", dstLength, dst.getPointerSize());
265         assertIntArrayEquals("xCoordinates after append zero",
266                 dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLength);
267         assertIntArrayEquals("yCoordinates after append zero",
268                 dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLength);
269         assertIntArrayEquals("pointerIds after append zero",
270                 dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLength);
271         assertIntArrayEquals("times after append zero",
272                 dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLength);
273 
274         dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, srcLength);
275         assertEquals("size after append", dstLength + srcLength, dst.getPointerSize());
276         assertTrue("primitive length after append",
277                 dst.getPointerIds().length >= dstLength + srcLength);
278         assertIntArrayEquals("original xCoordinates values after append",
279                 dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLength);
280         assertIntArrayEquals("original yCoordinates values after append",
281                 dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLength);
282         assertIntArrayEquals("original pointerIds values after append",
283                 dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLength);
284         assertIntArrayEquals("original times values after append",
285                 dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLength);
286         assertIntArrayEquals("appended xCoordinates values after append",
287                 srcXCoords.getPrimitiveArray(), 0, dst.getXCoordinates(), dstLength, srcLength);
288         assertIntArrayEquals("appended yCoordinates values after append",
289                 srcYCoords.getPrimitiveArray(), 0, dst.getYCoordinates(), dstLength, srcLength);
290         assertIntArrayEquals("appended pointerIds values after append",
291                 srcPointerIds, 0, dst.getPointerIds(), dstLength, srcLength);
292         assertIntArrayEquals("appended times values after append",
293                 srcTimes.getPrimitiveArray(), 0, dst.getTimes(), dstLength, srcLength);
294     }
295 
296     // TODO: Consolidate this method with
297     // {@link ResizableIntArrayTests#assertIntArrayEquals(String,int[],int,int[],int,int)}.
assertIntArrayEquals(final String message, final int[] expecteds, final int expectedPos, final int[] actuals, final int actualPos, final int length)298     private static void assertIntArrayEquals(final String message, final int[] expecteds,
299             final int expectedPos, final int[] actuals, final int actualPos, final int length) {
300         if (expecteds == actuals) {
301             return;
302         }
303         if (expecteds == null || actuals == null) {
304             assertEquals(message, Arrays.toString(expecteds), Arrays.toString(actuals));
305             return;
306         }
307         if (expecteds.length < expectedPos + length || actuals.length < actualPos + length) {
308             fail(message + ": insufficient length: expecteds=" + Arrays.toString(expecteds)
309                     + " actuals=" + Arrays.toString(actuals));
310             return;
311         }
312         for (int i = 0; i < length; i++) {
313             assertEquals(message + " [" + i + "]",
314                     expecteds[i + expectedPos], actuals[i + actualPos]);
315         }
316     }
317 
318     @Test
testShift()319     public void testShift() {
320         final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
321         final int limit = 100;
322         final int shiftAmount = 20;
323         for (int i = 0; i < limit; i++) {
324             final int x = i;
325             final int y = i * 2;
326             final int pointerId = i * 3;
327             final int time = i * 4;
328             src.addPointer(x, y, pointerId, time);
329         }
330         src.shift(shiftAmount);
331         assertEquals("length after shift", src.getPointerSize(), limit - shiftAmount);
332         for (int i = 0; i < limit - shiftAmount; ++i) {
333             final int oldIndex = i + shiftAmount;
334             final int x = oldIndex;
335             final int y = oldIndex * 2;
336             final int pointerId = oldIndex * 3;
337             final int time = oldIndex * 4;
338             assertEquals("xCoordinates at " + i, x, src.getXCoordinates()[i]);
339             assertEquals("yCoordinates at " + i, y, src.getYCoordinates()[i]);
340             assertEquals("pointerIds at " + i, pointerId, src.getPointerIds()[i]);
341             assertEquals("times at " + i, time, src.getTimes()[i]);
342         }
343     }
344 }
345