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