Fix PointerTrackerQueue.releaseAllPointerOlderThan

Change-Id: If353869720895ee5355079c15c724132762dd70c
main
Tadashi G. Takaoka 2013-05-14 12:25:21 +09:00
parent e69066d11b
commit 979fb7415b
2 changed files with 171 additions and 160 deletions

View File

@ -48,6 +48,9 @@ public final class PointerTrackerQueue {
public void add(final Element pointer) {
synchronized (mExpandableArrayOfActivePointers) {
if (DEBUG) {
Log.d(TAG, "add: " + pointer + " " + this);
}
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
final int arraySize = mArraySize;
if (arraySize < expandableArray.size()) {
@ -61,24 +64,27 @@ public final class PointerTrackerQueue {
public void remove(final Element pointer) {
synchronized (mExpandableArrayOfActivePointers) {
if (DEBUG) {
Log.d(TAG, "remove: " + pointer + " " + this);
}
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
final int arraySize = mArraySize;
int newSize = 0;
int newIndex = 0;
for (int index = 0; index < arraySize; index++) {
final Element element = expandableArray.get(index);
if (element == pointer) {
if (newSize != index) {
if (newIndex != index) {
Log.w(TAG, "Found duplicated element in remove: " + pointer);
}
continue; // Remove this element from the expandableArray.
}
if (newSize != index) {
if (newIndex != index) {
// Shift this element toward the beginning of the expandableArray.
expandableArray.set(newSize, element);
expandableArray.set(newIndex, element);
}
newSize++;
newIndex++;
}
mArraySize = newSize;
mArraySize = newIndex;
}
}
@ -95,8 +101,8 @@ public final class PointerTrackerQueue {
}
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
final int arraySize = mArraySize;
int newSize, index;
for (newSize = index = 0; index < arraySize; index++) {
int newIndex, index;
for (newIndex = index = 0; index < arraySize; index++) {
final Element element = expandableArray.get(index);
if (element == pointer) {
break; // Stop releasing elements.
@ -105,29 +111,30 @@ public final class PointerTrackerQueue {
element.onPhantomUpEvent(eventTime);
continue; // Remove this element from the expandableArray.
}
if (newSize != index) {
if (newIndex != index) {
// Shift this element toward the beginning of the expandableArray.
expandableArray.set(newSize, element);
expandableArray.set(newIndex, element);
}
newSize++;
newIndex++;
}
// Shift rest of the expandableArray.
int count = 0;
for (; index < arraySize; index++) {
final Element element = expandableArray.get(index);
if (element == pointer) {
if (count > 0) {
count++;
if (count > 1) {
Log.w(TAG, "Found duplicated element in releaseAllPointersOlderThan: "
+ pointer);
}
count++;
}
if (newSize != index) {
expandableArray.set(newSize, expandableArray.get(index));
newSize++;
if (newIndex != index) {
// Shift this element toward the beginning of the expandableArray.
expandableArray.set(newIndex, expandableArray.get(index));
}
newIndex++;
}
mArraySize = newSize;
mArraySize = newIndex;
}
}
@ -146,26 +153,26 @@ public final class PointerTrackerQueue {
}
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
final int arraySize = mArraySize;
int newSize = 0, count = 0;
int newIndex = 0, count = 0;
for (int index = 0; index < arraySize; index++) {
final Element element = expandableArray.get(index);
if (element == pointer) {
if (count > 0) {
count++;
if (count > 1) {
Log.w(TAG, "Found duplicated element in releaseAllPointersExcept: "
+ pointer);
}
count++;
} else {
element.onPhantomUpEvent(eventTime);
continue; // Remove this element from the expandableArray.
}
if (newSize != index) {
if (newIndex != index) {
// Shift this element toward the beginning of the expandableArray.
expandableArray.set(newSize, element);
expandableArray.set(newIndex, element);
}
newSize++;
newIndex++;
}
mArraySize = newSize;
mArraySize = newIndex;
}
}
@ -202,6 +209,9 @@ public final class PointerTrackerQueue {
public void cancelAllPointerTracker() {
synchronized (mExpandableArrayOfActivePointers) {
if (DEBUG) {
Log.d(TAG, "cancelAllPointerTracker: " + this);
}
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
final int arraySize = mArraySize;
for (int index = 0; index < arraySize; index++) {

View File

@ -66,23 +66,23 @@ public class PointerTrackerQueueTests extends AndroidTestCase {
private final PointerTrackerQueue mQueue = new PointerTrackerQueue();
public void testEmpty() {
assertEquals("empty queue", 0, mQueue.size());
assertEquals("empty queue", "[]", mQueue.toString());
assertEquals(0, mQueue.size());
assertEquals("[]", mQueue.toString());
}
public void testAdd() {
mQueue.add(mElement1);
assertEquals("add element1", 1, mQueue.size());
assertEquals("after adding element1", "[1]", mQueue.toString());
assertEquals(1, mQueue.size());
assertEquals("[1]", mQueue.toString());
mQueue.add(mElement2);
assertEquals("add element2", 2, mQueue.size());
assertEquals("after adding element2", "[1 2]", mQueue.toString());
assertEquals(2, mQueue.size());
assertEquals("[1 2]", mQueue.toString());
mQueue.add(mElement3);
assertEquals("add element3", 3, mQueue.size());
assertEquals("after adding element3", "[1 2 3]", mQueue.toString());
assertEquals(3, mQueue.size());
assertEquals("[1 2 3]", mQueue.toString());
mQueue.add(mElement4);
assertEquals("add element4", 4, mQueue.size());
assertEquals("after adding element4", "[1 2 3 4]", mQueue.toString());
assertEquals(4, mQueue.size());
assertEquals("[1 2 3 4]", mQueue.toString());
}
public void testRemove() {
@ -94,33 +94,29 @@ public class PointerTrackerQueueTests extends AndroidTestCase {
mQueue.add(mElement4);
mQueue.remove(mElement2);
assertEquals("remove element2", 3, mQueue.size());
assertEquals("after removing element2", "[1 3 4]", mQueue.toString());
assertEquals(3, mQueue.size());
assertEquals("[1 3 4]", mQueue.toString());
mQueue.remove(mElement4);
assertEquals("remove element4", 2, mQueue.size());
assertEquals("after removing element4", "[1 3]", mQueue.toString());
assertEquals(2, mQueue.size());
assertEquals("[1 3]", mQueue.toString());
mQueue.remove(mElement4);
assertEquals("remove element4 again", 2, mQueue.size());
assertEquals("after removing element4 again", "[1 3]", mQueue.toString());
assertEquals(2, mQueue.size());
assertEquals("[1 3]", mQueue.toString());
mQueue.remove(mElement1);
assertEquals("remove element1", 1, mQueue.size());
assertEquals("after removing element4", "[3]", mQueue.toString());
assertEquals(1, mQueue.size());
assertEquals("[3]", mQueue.toString());
mQueue.remove(mElement3);
assertEquals("remove element3", 0, mQueue.size());
assertEquals("after removing element3", "[]", mQueue.toString());
assertEquals(0, mQueue.size());
assertEquals("[]", mQueue.toString());
mQueue.remove(mElement1);
assertEquals("remove element1 again", 0, mQueue.size());
assertEquals("after removing element1 again", "[]", mQueue.toString());
assertEquals(0, mQueue.size());
assertEquals("[]", mQueue.toString());
assertEquals("after remove elements", 0, Element.sPhantomUpCount);
assertEquals("after remove element1",
Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime);
assertEquals("after remove element2",
Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
assertEquals("after remove element3",
Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
assertEquals("after remove element4",
Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
assertEquals(0, Element.sPhantomUpCount);
assertEquals(Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
}
public void testAddAndRemove() {
@ -132,38 +128,34 @@ public class PointerTrackerQueueTests extends AndroidTestCase {
mQueue.add(mElement4);
mQueue.remove(mElement2);
assertEquals("remove element2", 3, mQueue.size());
assertEquals("after removing element2", "[1 3 4]", mQueue.toString());
assertEquals(3, mQueue.size());
assertEquals("[1 3 4]", mQueue.toString());
mQueue.remove(mElement4);
assertEquals("remove element4", 2, mQueue.size());
assertEquals("after removing element4", "[1 3]", mQueue.toString());
assertEquals(2, mQueue.size());
assertEquals("[1 3]", mQueue.toString());
mQueue.add(mElement2);
assertEquals("add element2", 3, mQueue.size());
assertEquals("after adding element2", "[1 3 2]", mQueue.toString());
assertEquals(3, mQueue.size());
assertEquals("[1 3 2]", mQueue.toString());
mQueue.remove(mElement4);
assertEquals("remove element4 again", 3, mQueue.size());
assertEquals("after removing element4 again", "[1 3 2]", mQueue.toString());
assertEquals(3, mQueue.size());
assertEquals("[1 3 2]", mQueue.toString());
mQueue.remove(mElement1);
assertEquals("remove element1", 2, mQueue.size());
assertEquals("after removing element4", "[3 2]", mQueue.toString());
assertEquals(2, mQueue.size());
assertEquals("[3 2]", mQueue.toString());
mQueue.add(mElement1);
assertEquals("add element1", 3, mQueue.size());
assertEquals("after adding element1", "[3 2 1]", mQueue.toString());
assertEquals(3, mQueue.size());
assertEquals("[3 2 1]", mQueue.toString());
mQueue.remove(mElement3);
assertEquals("remove element3", 2, mQueue.size());
assertEquals("after removing element3", "[2 1]", mQueue.toString());
assertEquals(2, mQueue.size());
assertEquals("[2 1]", mQueue.toString());
mQueue.remove(mElement1);
assertEquals("remove element1 again", 1, mQueue.size());
assertEquals("after removing element1 again", "[2]", mQueue.toString());
assertEquals(1, mQueue.size());
assertEquals("[2]", mQueue.toString());
assertEquals("after remove element1",
Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime);
assertEquals("after remove element2",
Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
assertEquals("after remove element3",
Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
assertEquals("after remove element4",
Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
}
public void testReleaseAllPointers() {
@ -176,20 +168,33 @@ public class PointerTrackerQueueTests extends AndroidTestCase {
final long eventTime = 123;
Element.sPhantomUpCount = 0;
mQueue.releaseAllPointers(eventTime);
assertEquals("after releaseAllPointers", 4, Element.sPhantomUpCount);
assertEquals("after releaseAllPointers", 0, mQueue.size());
assertEquals("after releaseAllPointers", "[]", mQueue.toString());
assertEquals("after releaseAllPointers element1",
eventTime + 1, mElement1.mPhantomUpEventTime);
assertEquals("after releaseAllPointers element2",
eventTime + 2, mElement2.mPhantomUpEventTime);
assertEquals("after releaseAllPointers element3",
eventTime + 3, mElement3.mPhantomUpEventTime);
assertEquals("after releaseAllPointers element4",
eventTime + 4, mElement4.mPhantomUpEventTime);
assertEquals(4, Element.sPhantomUpCount);
assertEquals(0, mQueue.size());
assertEquals("[]", mQueue.toString());
assertEquals(eventTime + 1, mElement1.mPhantomUpEventTime);
assertEquals(eventTime + 2, mElement2.mPhantomUpEventTime);
assertEquals(eventTime + 3, mElement3.mPhantomUpEventTime);
assertEquals(eventTime + 4, mElement4.mPhantomUpEventTime);
}
public void testReleaseAllPointersOlderThan() {
public void testReleaseAllPointersOlderThanFirst() {
mElement2.mIsModifier = true;
mQueue.add(mElement1);
mQueue.add(mElement2);
mQueue.add(mElement3);
final long eventTime = 123;
Element.sPhantomUpCount = 0;
mQueue.releaseAllPointersOlderThan(mElement1, eventTime);
assertEquals(0, Element.sPhantomUpCount);
assertEquals(3, mQueue.size());
assertEquals("[1 2 3]", mQueue.toString());
assertEquals(Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
}
public void testReleaseAllPointersOlderThanLast() {
mElement2.mIsModifier = true;
mQueue.add(mElement1);
mQueue.add(mElement2);
@ -199,20 +204,34 @@ public class PointerTrackerQueueTests extends AndroidTestCase {
final long eventTime = 123;
Element.sPhantomUpCount = 0;
mQueue.releaseAllPointersOlderThan(mElement4, eventTime);
assertEquals("after releaseAllPointersOlderThan", 2, Element.sPhantomUpCount);
assertEquals("after releaseAllPointersOlderThan", 2, mQueue.size());
assertEquals("after releaseAllPointersOlderThan", "[2 4]", mQueue.toString());
assertEquals("after releaseAllPointersOlderThan element1",
eventTime + 1, mElement1.mPhantomUpEventTime);
assertEquals("after releaseAllPointersOlderThan element2",
Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
assertEquals("after releaseAllPointersOlderThan element3",
eventTime + 2, mElement3.mPhantomUpEventTime);
assertEquals("after releaseAllPointersOlderThan element4",
Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
assertEquals(2, Element.sPhantomUpCount);
assertEquals(2, mQueue.size());
assertEquals("[2 4]", mQueue.toString());
assertEquals(eventTime + 1, mElement1.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
assertEquals(eventTime + 2, mElement3.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
}
public void testReleaseAllPointersOlderThanWithoutModifier() {
public void testReleaseAllPointersOlderThanWithoutModifierMiddle() {
mQueue.add(mElement1);
mQueue.add(mElement2);
mQueue.add(mElement3);
mQueue.add(mElement4);
final long eventTime = 123;
Element.sPhantomUpCount = 0;
mQueue.releaseAllPointersOlderThan(mElement3, eventTime);
assertEquals(2, Element.sPhantomUpCount);
assertEquals(2, mQueue.size());
assertEquals("[3 4]", mQueue.toString());
assertEquals(eventTime + 1, mElement1.mPhantomUpEventTime);
assertEquals(eventTime + 2, mElement2.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
}
public void testReleaseAllPointersOlderThanWithoutModifierLast() {
mQueue.add(mElement1);
mQueue.add(mElement2);
mQueue.add(mElement3);
@ -221,19 +240,13 @@ public class PointerTrackerQueueTests extends AndroidTestCase {
final long eventTime = 123;
Element.sPhantomUpCount = 0;
mQueue.releaseAllPointersOlderThan(mElement4, eventTime);
assertEquals("after releaseAllPointersOlderThan without modifier",
3, Element.sPhantomUpCount);
assertEquals("after releaseAllPointersOlderThan without modifier", 1, mQueue.size());
assertEquals("after releaseAllPointersOlderThan without modifier",
"[4]", mQueue.toString());
assertEquals("after releaseAllPointersOlderThan without modifier element1",
eventTime + 1, mElement1.mPhantomUpEventTime);
assertEquals("after releaseAllPointersOlderThan without modifier element2",
eventTime + 2, mElement2.mPhantomUpEventTime);
assertEquals("after releaseAllPointersOlderThan without modifier element3",
eventTime + 3, mElement3.mPhantomUpEventTime);
assertEquals("after releaseAllPointersOlderThan without modifier element4",
Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
assertEquals(3, Element.sPhantomUpCount);
assertEquals(1, mQueue.size());
assertEquals("[4]", mQueue.toString());
assertEquals(eventTime + 1, mElement1.mPhantomUpEventTime);
assertEquals(eventTime + 2, mElement2.mPhantomUpEventTime);
assertEquals(eventTime + 3, mElement3.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
}
public void testReleaseAllPointersExcept() {
@ -246,17 +259,13 @@ public class PointerTrackerQueueTests extends AndroidTestCase {
final long eventTime = 123;
Element.sPhantomUpCount = 0;
mQueue.releaseAllPointersExcept(mElement3, eventTime);
assertEquals("after releaseAllPointersExcept", 3, Element.sPhantomUpCount);
assertEquals("after releaseAllPointersExcept", 1, mQueue.size());
assertEquals("after releaseAllPointersExcept", "[3]", mQueue.toString());
assertEquals("after releaseAllPointersExcept element1",
eventTime + 1, mElement1.mPhantomUpEventTime);
assertEquals("after releaseAllPointersExcept element2",
eventTime + 2, mElement2.mPhantomUpEventTime);
assertEquals("after releaseAllPointersExcept element3",
Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
assertEquals("after releaseAllPointersExcept element4",
eventTime + 3, mElement4.mPhantomUpEventTime);
assertEquals(3, Element.sPhantomUpCount);
assertEquals(1, mQueue.size());
assertEquals("[3]", mQueue.toString());
assertEquals(eventTime + 1, mElement1.mPhantomUpEventTime);
assertEquals(eventTime + 2, mElement2.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
assertEquals(eventTime + 3, mElement4.mPhantomUpEventTime);
}
public void testHasModifierKeyOlderThan() {
@ -268,54 +277,46 @@ public class PointerTrackerQueueTests extends AndroidTestCase {
mQueue.add(mElement3);
mQueue.add(mElement4);
assertFalse("hasModifierKeyOlderThan element1", mQueue.hasModifierKeyOlderThan(mElement1));
assertFalse("hasModifierKeyOlderThan element2", mQueue.hasModifierKeyOlderThan(mElement2));
assertFalse("hasModifierKeyOlderThan element3", mQueue.hasModifierKeyOlderThan(mElement3));
assertFalse("hasModifierKeyOlderThan element4", mQueue.hasModifierKeyOlderThan(mElement4));
assertFalse(mQueue.hasModifierKeyOlderThan(mElement1));
assertFalse(mQueue.hasModifierKeyOlderThan(mElement2));
assertFalse(mQueue.hasModifierKeyOlderThan(mElement3));
assertFalse(mQueue.hasModifierKeyOlderThan(mElement4));
mElement2.mIsModifier = true;
assertFalse("hasModifierKeyOlderThan element1", mQueue.hasModifierKeyOlderThan(mElement1));
assertFalse("hasModifierKeyOlderThan element2", mQueue.hasModifierKeyOlderThan(mElement2));
assertTrue("hasModifierKeyOlderThan element3", mQueue.hasModifierKeyOlderThan(mElement3));
assertTrue("hasModifierKeyOlderThan element4", mQueue.hasModifierKeyOlderThan(mElement4));
assertFalse(mQueue.hasModifierKeyOlderThan(mElement1));
assertFalse(mQueue.hasModifierKeyOlderThan(mElement2));
assertTrue(mQueue.hasModifierKeyOlderThan(mElement3));
assertTrue(mQueue.hasModifierKeyOlderThan(mElement4));
assertEquals("after hasModifierKeyOlderThan", 0, Element.sPhantomUpCount);
assertEquals("after hasModifierKeyOlderThan", 4, mQueue.size());
assertEquals("after hasModifierKeyOlderThan", "[1 2 3 4]", mQueue.toString());
assertEquals("after hasModifierKeyOlderThan element1",
Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime);
assertEquals("after hasModifierKeyOlderThan element2",
Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
assertEquals("after hasModifierKeyOlderThan element3",
Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
assertEquals("after hasModifierKeyOlderThan element4",
Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
assertEquals(0, Element.sPhantomUpCount);
assertEquals(4, mQueue.size());
assertEquals("[1 2 3 4]", mQueue.toString());
assertEquals(Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
}
public void testIsAnyInSlidingKeyInput() {
Element.sPhantomUpCount = 0;
assertFalse("isAnyInSlidingKeyInput empty", mQueue.isAnyInSlidingKeyInput());
assertFalse(mQueue.isAnyInSlidingKeyInput());
mQueue.add(mElement1);
mQueue.add(mElement2);
mQueue.add(mElement3);
mQueue.add(mElement4);
assertFalse("isAnyInSlidingKeyInput element1", mQueue.isAnyInSlidingKeyInput());
assertFalse(mQueue.isAnyInSlidingKeyInput());
mElement3.mIsInSlidingKeyInput = true;
assertTrue("isAnyInSlidingKeyInput element1", mQueue.isAnyInSlidingKeyInput());
assertTrue(mQueue.isAnyInSlidingKeyInput());
assertEquals("after isAnyInSlidingKeyInput", 0, Element.sPhantomUpCount);
assertEquals("after isAnyInSlidingKeyInput", 4, mQueue.size());
assertEquals("after isAnyInSlidingKeyInput", "[1 2 3 4]", mQueue.toString());
assertEquals("after isAnyInSlidingKeyInput element1",
Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime);
assertEquals("after isAnyInSlidingKeyInput element2",
Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
assertEquals("after isAnyInSlidingKeyInput element3",
Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
assertEquals("after isAnyInSlidingKeyInput element4",
Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
assertEquals(0, Element.sPhantomUpCount);
assertEquals(4, mQueue.size());
assertEquals("[1 2 3 4]", mQueue.toString());
assertEquals(Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
assertEquals(Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
}
}