2012-07-10 01:47:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
* use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations under
|
|
|
|
* the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
|
|
|
import android.test.AndroidTestCase;
|
2013-02-04 23:25:24 +00:00
|
|
|
import android.test.suitebuilder.annotation.SmallTest;
|
2012-07-10 01:47:44 +00:00
|
|
|
|
2012-08-23 08:47:13 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
2013-02-04 23:25:24 +00:00
|
|
|
@SmallTest
|
2012-07-10 01:47:44 +00:00
|
|
|
public class InputPointersTests extends AndroidTestCase {
|
2012-07-18 11:31:09 +00:00
|
|
|
private static final int DEFAULT_CAPACITY = 48;
|
|
|
|
|
2012-07-10 01:47:44 +00:00
|
|
|
public void testNewInstance() {
|
2012-07-18 11:31:09 +00:00
|
|
|
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
|
2012-07-19 10:44:54 +00:00
|
|
|
assertEquals("new instance size", 0, src.getPointerSize());
|
2012-07-10 01:47:44 +00:00
|
|
|
assertNotNull("new instance xCoordinates", src.getXCoordinates());
|
|
|
|
assertNotNull("new instance yCoordinates", src.getYCoordinates());
|
|
|
|
assertNotNull("new instance pointerIds", src.getPointerIds());
|
|
|
|
assertNotNull("new instance times", src.getTimes());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testReset() {
|
2012-07-18 11:31:09 +00:00
|
|
|
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
|
2012-07-10 01:47:44 +00:00
|
|
|
final int[] xCoordinates = src.getXCoordinates();
|
|
|
|
final int[] yCoordinates = src.getXCoordinates();
|
|
|
|
final int[] pointerIds = src.getXCoordinates();
|
|
|
|
final int[] times = src.getXCoordinates();
|
|
|
|
|
|
|
|
src.reset();
|
2012-07-19 10:44:54 +00:00
|
|
|
assertEquals("size after reset", 0, src.getPointerSize());
|
|
|
|
assertNotSame("xCoordinates after reset", xCoordinates, src.getXCoordinates());
|
|
|
|
assertNotSame("yCoordinates after reset", yCoordinates, src.getYCoordinates());
|
|
|
|
assertNotSame("pointerIds after reset", pointerIds, src.getPointerIds());
|
|
|
|
assertNotSame("times after reset", times, src.getTimes());
|
2012-07-10 01:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testAdd() {
|
2012-07-18 11:31:09 +00:00
|
|
|
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
|
2012-07-10 01:47:44 +00:00
|
|
|
final int limit = src.getXCoordinates().length * 2 + 10;
|
|
|
|
for (int i = 0; i < limit; i++) {
|
|
|
|
src.addPointer(i, i * 2, i * 3, i * 4);
|
2012-07-19 10:44:54 +00:00
|
|
|
assertEquals("size after add " + i, i + 1, src.getPointerSize());
|
2012-07-10 01:47:44 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < limit; i++) {
|
|
|
|
assertEquals("xCoordinates at " + i, i, src.getXCoordinates()[i]);
|
|
|
|
assertEquals("yCoordinates at " + i, i * 2, src.getYCoordinates()[i]);
|
|
|
|
assertEquals("pointerIds at " + i, i * 3, src.getPointerIds()[i]);
|
|
|
|
assertEquals("times at " + i, i * 4, src.getTimes()[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testAddAt() {
|
2012-07-18 11:31:09 +00:00
|
|
|
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
|
2012-07-10 01:47:44 +00:00
|
|
|
final int limit = 1000, step = 100;
|
|
|
|
for (int i = 0; i < limit; i += step) {
|
|
|
|
src.addPointer(i, i, i * 2, i * 3, i * 4);
|
2012-07-19 10:44:54 +00:00
|
|
|
assertEquals("size after add at " + i, i + 1, src.getPointerSize());
|
2012-07-10 01:47:44 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < limit; i += step) {
|
|
|
|
assertEquals("xCoordinates at " + i, i, src.getXCoordinates()[i]);
|
|
|
|
assertEquals("yCoordinates at " + i, i * 2, src.getYCoordinates()[i]);
|
|
|
|
assertEquals("pointerIds at " + i, i * 3, src.getPointerIds()[i]);
|
|
|
|
assertEquals("times at " + i, i * 4, src.getTimes()[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testSet() {
|
2012-07-18 11:31:09 +00:00
|
|
|
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
|
2012-07-10 01:47:44 +00:00
|
|
|
final int limit = src.getXCoordinates().length * 2 + 10;
|
|
|
|
for (int i = 0; i < limit; i++) {
|
|
|
|
src.addPointer(i, i * 2, i * 3, i * 4);
|
|
|
|
}
|
2012-07-18 11:31:09 +00:00
|
|
|
final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
|
2012-07-10 01:47:44 +00:00
|
|
|
dst.set(src);
|
2012-07-19 10:44:54 +00:00
|
|
|
assertEquals("size after set", dst.getPointerSize(), src.getPointerSize());
|
|
|
|
assertSame("xCoordinates after set", dst.getXCoordinates(), src.getXCoordinates());
|
|
|
|
assertSame("yCoordinates after set", dst.getYCoordinates(), src.getYCoordinates());
|
|
|
|
assertSame("pointerIds after set", dst.getPointerIds(), src.getPointerIds());
|
|
|
|
assertSame("times after set", dst.getTimes(), src.getTimes());
|
2012-07-10 01:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testCopy() {
|
2012-07-18 11:31:09 +00:00
|
|
|
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
|
2012-07-10 01:47:44 +00:00
|
|
|
final int limit = 100;
|
|
|
|
for (int i = 0; i < limit; i++) {
|
|
|
|
src.addPointer(i, i * 2, i * 3, i * 4);
|
|
|
|
}
|
2012-07-18 11:31:09 +00:00
|
|
|
final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
|
2012-07-10 01:47:44 +00:00
|
|
|
dst.copy(src);
|
2012-07-19 10:44:54 +00:00
|
|
|
assertEquals("size after copy", dst.getPointerSize(), src.getPointerSize());
|
|
|
|
assertNotSame("xCoordinates after copy", dst.getXCoordinates(), src.getXCoordinates());
|
|
|
|
assertNotSame("yCoordinates after copy", dst.getYCoordinates(), src.getYCoordinates());
|
|
|
|
assertNotSame("pointerIds after copy", dst.getPointerIds(), src.getPointerIds());
|
|
|
|
assertNotSame("times after copy", dst.getTimes(), src.getTimes());
|
2012-07-10 01:47:44 +00:00
|
|
|
final int size = dst.getPointerSize();
|
2012-07-19 10:44:54 +00:00
|
|
|
assertArrayEquals("xCoordinates values after copy",
|
2012-07-10 01:47:44 +00:00
|
|
|
dst.getXCoordinates(), 0, src.getXCoordinates(), 0, size);
|
2012-07-19 10:44:54 +00:00
|
|
|
assertArrayEquals("yCoordinates values after copy",
|
2012-07-10 01:47:44 +00:00
|
|
|
dst.getYCoordinates(), 0, src.getYCoordinates(), 0, size);
|
2012-07-19 10:44:54 +00:00
|
|
|
assertArrayEquals("pointerIds values after copy",
|
2012-07-10 01:47:44 +00:00
|
|
|
dst.getPointerIds(), 0, src.getPointerIds(), 0, size);
|
2012-07-19 10:44:54 +00:00
|
|
|
assertArrayEquals("times values after copy",
|
2012-07-10 01:47:44 +00:00
|
|
|
dst.getTimes(), 0, src.getTimes(), 0, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testAppend() {
|
2012-07-18 11:31:09 +00:00
|
|
|
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
|
2012-07-13 02:17:25 +00:00
|
|
|
final int srcLen = 100;
|
|
|
|
for (int i = 0; i < srcLen; i++) {
|
2012-07-10 01:47:44 +00:00
|
|
|
src.addPointer(i, i * 2, i * 3, i * 4);
|
|
|
|
}
|
2012-07-13 02:17:25 +00:00
|
|
|
final int dstLen = 50;
|
2012-07-18 11:31:09 +00:00
|
|
|
final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
|
2012-07-13 02:17:25 +00:00
|
|
|
for (int i = 0; i < dstLen; i++) {
|
|
|
|
final int value = -i - 1;
|
2012-07-10 01:47:44 +00:00
|
|
|
dst.addPointer(value * 4, value * 3, value * 2, value);
|
|
|
|
}
|
2012-07-18 11:31:09 +00:00
|
|
|
final InputPointers dstCopy = new InputPointers(DEFAULT_CAPACITY);
|
2012-07-10 01:47:44 +00:00
|
|
|
dstCopy.copy(dst);
|
|
|
|
|
|
|
|
dst.append(src, 0, 0);
|
2012-07-19 10:44:54 +00:00
|
|
|
assertEquals("size after append zero", dstLen, dst.getPointerSize());
|
|
|
|
assertArrayEquals("xCoordinates after append zero",
|
|
|
|
dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen);
|
|
|
|
assertArrayEquals("yCoordinates after append zero",
|
|
|
|
dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen);
|
|
|
|
assertArrayEquals("pointerIds after append zero",
|
|
|
|
dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen);
|
|
|
|
assertArrayEquals("times after append zero",
|
|
|
|
dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen);
|
2012-07-10 01:47:44 +00:00
|
|
|
|
2012-07-13 02:17:25 +00:00
|
|
|
dst.append(src, 0, srcLen);
|
2012-07-19 10:44:54 +00:00
|
|
|
assertEquals("size after append", dstLen + srcLen, dst.getPointerSize());
|
|
|
|
assertTrue("primitive length after append",
|
2012-07-13 02:17:25 +00:00
|
|
|
dst.getPointerIds().length >= dstLen + srcLen);
|
2012-07-19 10:44:54 +00:00
|
|
|
assertArrayEquals("original xCoordinates values after append",
|
|
|
|
dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen);
|
|
|
|
assertArrayEquals("original yCoordinates values after append",
|
|
|
|
dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen);
|
|
|
|
assertArrayEquals("original pointerIds values after append",
|
|
|
|
dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen);
|
|
|
|
assertArrayEquals("original times values after append",
|
|
|
|
dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen);
|
|
|
|
assertArrayEquals("appended xCoordinates values after append",
|
|
|
|
src.getXCoordinates(), 0, dst.getXCoordinates(), dstLen, srcLen);
|
|
|
|
assertArrayEquals("appended yCoordinates values after append",
|
|
|
|
src.getYCoordinates(), 0, dst.getYCoordinates(), dstLen, srcLen);
|
|
|
|
assertArrayEquals("appended pointerIds values after append",
|
|
|
|
src.getPointerIds(), 0, dst.getPointerIds(), dstLen, srcLen);
|
|
|
|
assertArrayEquals("appended times values after append",
|
|
|
|
src.getTimes(), 0, dst.getTimes(), dstLen, srcLen);
|
2012-07-10 01:47:44 +00:00
|
|
|
}
|
|
|
|
|
2012-08-23 08:47:13 +00:00
|
|
|
public void testAppendResizableIntArray() {
|
|
|
|
final int srcLen = 100;
|
|
|
|
final int srcPointerId = 1;
|
|
|
|
final int[] srcPointerIds = new int[srcLen];
|
|
|
|
Arrays.fill(srcPointerIds, srcPointerId);
|
|
|
|
final ResizableIntArray srcTimes = new ResizableIntArray(DEFAULT_CAPACITY);
|
|
|
|
final ResizableIntArray srcXCoords = new ResizableIntArray(DEFAULT_CAPACITY);
|
|
|
|
final ResizableIntArray srcYCoords= new ResizableIntArray(DEFAULT_CAPACITY);
|
|
|
|
for (int i = 0; i < srcLen; i++) {
|
|
|
|
srcTimes.add(i * 2);
|
|
|
|
srcXCoords.add(i * 3);
|
|
|
|
srcYCoords.add(i * 4);
|
|
|
|
}
|
|
|
|
final int dstLen = 50;
|
|
|
|
final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
|
|
|
|
for (int i = 0; i < dstLen; i++) {
|
|
|
|
final int value = -i - 1;
|
|
|
|
dst.addPointer(value * 4, value * 3, value * 2, value);
|
|
|
|
}
|
|
|
|
final InputPointers dstCopy = new InputPointers(DEFAULT_CAPACITY);
|
|
|
|
dstCopy.copy(dst);
|
|
|
|
|
|
|
|
dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, 0);
|
|
|
|
assertEquals("size after append zero", dstLen, dst.getPointerSize());
|
|
|
|
assertArrayEquals("xCoordinates after append zero",
|
|
|
|
dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen);
|
|
|
|
assertArrayEquals("yCoordinates after append zero",
|
|
|
|
dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen);
|
|
|
|
assertArrayEquals("pointerIds after append zero",
|
|
|
|
dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen);
|
|
|
|
assertArrayEquals("times after append zero",
|
|
|
|
dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen);
|
|
|
|
|
|
|
|
dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, srcLen);
|
|
|
|
assertEquals("size after append", dstLen + srcLen, dst.getPointerSize());
|
|
|
|
assertTrue("primitive length after append",
|
|
|
|
dst.getPointerIds().length >= dstLen + srcLen);
|
|
|
|
assertArrayEquals("original xCoordinates values after append",
|
|
|
|
dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen);
|
|
|
|
assertArrayEquals("original yCoordinates values after append",
|
|
|
|
dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen);
|
|
|
|
assertArrayEquals("original pointerIds values after append",
|
|
|
|
dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen);
|
|
|
|
assertArrayEquals("original times values after append",
|
|
|
|
dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen);
|
|
|
|
assertArrayEquals("appended xCoordinates values after append",
|
|
|
|
srcXCoords.getPrimitiveArray(), 0, dst.getXCoordinates(), dstLen, srcLen);
|
|
|
|
assertArrayEquals("appended yCoordinates values after append",
|
|
|
|
srcYCoords.getPrimitiveArray(), 0, dst.getYCoordinates(), dstLen, srcLen);
|
|
|
|
assertArrayEquals("appended pointerIds values after append",
|
|
|
|
srcPointerIds, 0, dst.getPointerIds(), dstLen, srcLen);
|
|
|
|
assertArrayEquals("appended times values after append",
|
|
|
|
srcTimes.getPrimitiveArray(), 0, dst.getTimes(), dstLen, srcLen);
|
|
|
|
}
|
|
|
|
|
2012-07-10 01:47:44 +00:00
|
|
|
private static void assertArrayEquals(String message, int[] expecteds, int expectedPos,
|
|
|
|
int[] actuals, int actualPos, int length) {
|
|
|
|
if (expecteds == null && actuals == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (expecteds == null || actuals == null) {
|
|
|
|
fail(message + ": expecteds=" + expecteds + " actuals=" + actuals);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
assertEquals(message + ": element at " + i,
|
|
|
|
expecteds[i + expectedPos], actuals[i + actualPos]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|