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