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.keyboard.internal;
18
19import android.test.AndroidTestCase;
20import android.test.suitebuilder.annotation.SmallTest;
21
22@SmallTest
23public class PointerTrackerQueueTests extends AndroidTestCase {
24    public static class Element implements PointerTrackerQueue.Element {
25        public static int sPhantomUpCount;
26        public static final long NOT_HAPPENED = -1;
27
28        public final int mId;
29        public boolean mIsModifier;
30        public boolean mIsInDraggingFinger;
31        public long mPhantomUpEventTime = NOT_HAPPENED;
32
33        public Element(int id) {
34            mId = id;
35        }
36
37        @Override
38        public boolean isModifier() {
39            return mIsModifier;
40        }
41
42        @Override
43        public boolean isInDraggingFinger() {
44            return mIsInDraggingFinger;
45        }
46
47        @Override
48        public void onPhantomUpEvent(long eventTime) {
49            sPhantomUpCount++;
50            mPhantomUpEventTime = eventTime + sPhantomUpCount;
51        }
52
53        @Override
54        public void cancelTrackingForAction() {}
55
56        @Override
57        public String toString() {
58            return Integer.toString(mId);
59        }
60    }
61
62    private final Element mElement1 = new Element(1);
63    private final Element mElement2 = new Element(2);
64    private final Element mElement3 = new Element(3);
65    private final Element mElement4 = new Element(4);
66    private final PointerTrackerQueue mQueue = new PointerTrackerQueue();
67
68    public void testEmpty() {
69        assertEquals(0, mQueue.size());
70        assertEquals("[]", mQueue.toString());
71    }
72
73    public void testAdd() {
74        mQueue.add(mElement1);
75        assertEquals(1, mQueue.size());
76        assertEquals("[1]", mQueue.toString());
77        mQueue.add(mElement2);
78        assertEquals(2, mQueue.size());
79        assertEquals("[1 2]", mQueue.toString());
80        mQueue.add(mElement3);
81        assertEquals(3, mQueue.size());
82        assertEquals("[1 2 3]", mQueue.toString());
83        mQueue.add(mElement4);
84        assertEquals(4, mQueue.size());
85        assertEquals("[1 2 3 4]", mQueue.toString());
86    }
87
88    public void testRemove() {
89        Element.sPhantomUpCount = 0;
90
91        mQueue.add(mElement1);
92        mQueue.add(mElement2);
93        mQueue.add(mElement3);
94        mQueue.add(mElement4);
95
96        mQueue.remove(mElement2);
97        assertEquals(3, mQueue.size());
98        assertEquals("[1 3 4]", mQueue.toString());
99        mQueue.remove(mElement4);
100        assertEquals(2, mQueue.size());
101        assertEquals("[1 3]", mQueue.toString());
102        mQueue.remove(mElement4);
103        assertEquals(2, mQueue.size());
104        assertEquals("[1 3]", mQueue.toString());
105        mQueue.remove(mElement1);
106        assertEquals(1, mQueue.size());
107        assertEquals("[3]", mQueue.toString());
108        mQueue.remove(mElement3);
109        assertEquals(0, mQueue.size());
110        assertEquals("[]", mQueue.toString());
111        mQueue.remove(mElement1);
112        assertEquals(0, mQueue.size());
113        assertEquals("[]", mQueue.toString());
114
115        assertEquals(0, Element.sPhantomUpCount);
116        assertEquals(Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime);
117        assertEquals(Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
118        assertEquals(Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
119        assertEquals(Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
120    }
121
122    public void testAddAndRemove() {
123        Element.sPhantomUpCount = 0;
124
125        mQueue.add(mElement1);
126        mQueue.add(mElement2);
127        mQueue.add(mElement3);
128        mQueue.add(mElement4);
129
130        mQueue.remove(mElement2);
131        assertEquals(3, mQueue.size());
132        assertEquals("[1 3 4]", mQueue.toString());
133        mQueue.remove(mElement4);
134        assertEquals(2, mQueue.size());
135        assertEquals("[1 3]", mQueue.toString());
136        mQueue.add(mElement2);
137        assertEquals(3, mQueue.size());
138        assertEquals("[1 3 2]", mQueue.toString());
139        mQueue.remove(mElement4);
140        assertEquals(3, mQueue.size());
141        assertEquals("[1 3 2]", mQueue.toString());
142        mQueue.remove(mElement1);
143        assertEquals(2, mQueue.size());
144        assertEquals("[3 2]", mQueue.toString());
145        mQueue.add(mElement1);
146        assertEquals(3, mQueue.size());
147        assertEquals("[3 2 1]", mQueue.toString());
148        mQueue.remove(mElement3);
149        assertEquals(2, mQueue.size());
150        assertEquals("[2 1]", mQueue.toString());
151        mQueue.remove(mElement1);
152        assertEquals(1, mQueue.size());
153        assertEquals("[2]", mQueue.toString());
154
155        assertEquals(Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime);
156        assertEquals(Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
157        assertEquals(Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
158        assertEquals(Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
159    }
160
161    public void testReleaseAllPointers() {
162        mElement2.mIsModifier = true;
163        mQueue.add(mElement1);
164        mQueue.add(mElement2);
165        mQueue.add(mElement3);
166        mQueue.add(mElement4);
167
168        final long eventTime = 123;
169        Element.sPhantomUpCount = 0;
170        mQueue.releaseAllPointers(eventTime);
171        assertEquals(4, Element.sPhantomUpCount);
172        assertEquals(0, mQueue.size());
173        assertEquals("[]", mQueue.toString());
174        assertEquals(eventTime + 1, mElement1.mPhantomUpEventTime);
175        assertEquals(eventTime + 2, mElement2.mPhantomUpEventTime);
176        assertEquals(eventTime + 3, mElement3.mPhantomUpEventTime);
177        assertEquals(eventTime + 4, mElement4.mPhantomUpEventTime);
178    }
179
180    public void testReleaseAllPointersOlderThanFirst() {
181        mElement2.mIsModifier = true;
182        mQueue.add(mElement1);
183        mQueue.add(mElement2);
184        mQueue.add(mElement3);
185
186        final long eventTime = 123;
187        Element.sPhantomUpCount = 0;
188        mQueue.releaseAllPointersOlderThan(mElement1, eventTime);
189        assertEquals(0, Element.sPhantomUpCount);
190        assertEquals(3, mQueue.size());
191        assertEquals("[1 2 3]", mQueue.toString());
192        assertEquals(Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime);
193        assertEquals(Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
194        assertEquals(Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
195    }
196
197    public void testReleaseAllPointersOlderThanLast() {
198        mElement2.mIsModifier = true;
199        mQueue.add(mElement1);
200        mQueue.add(mElement2);
201        mQueue.add(mElement3);
202        mQueue.add(mElement4);
203
204        final long eventTime = 123;
205        Element.sPhantomUpCount = 0;
206        mQueue.releaseAllPointersOlderThan(mElement4, eventTime);
207        assertEquals(2, Element.sPhantomUpCount);
208        assertEquals(2, mQueue.size());
209        assertEquals("[2 4]", mQueue.toString());
210        assertEquals(eventTime + 1, mElement1.mPhantomUpEventTime);
211        assertEquals(Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
212        assertEquals(eventTime + 2, mElement3.mPhantomUpEventTime);
213        assertEquals(Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
214    }
215
216    public void testReleaseAllPointersOlderThanWithoutModifierMiddle() {
217        mQueue.add(mElement1);
218        mQueue.add(mElement2);
219        mQueue.add(mElement3);
220        mQueue.add(mElement4);
221
222        final long eventTime = 123;
223        Element.sPhantomUpCount = 0;
224        mQueue.releaseAllPointersOlderThan(mElement3, eventTime);
225        assertEquals(2, Element.sPhantomUpCount);
226        assertEquals(2, mQueue.size());
227        assertEquals("[3 4]", mQueue.toString());
228        assertEquals(eventTime + 1, mElement1.mPhantomUpEventTime);
229        assertEquals(eventTime + 2, mElement2.mPhantomUpEventTime);
230        assertEquals(Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
231        assertEquals(Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
232    }
233
234    public void testReleaseAllPointersOlderThanWithoutModifierLast() {
235        mQueue.add(mElement1);
236        mQueue.add(mElement2);
237        mQueue.add(mElement3);
238        mQueue.add(mElement4);
239
240        final long eventTime = 123;
241        Element.sPhantomUpCount = 0;
242        mQueue.releaseAllPointersOlderThan(mElement4, eventTime);
243        assertEquals(3, Element.sPhantomUpCount);
244        assertEquals(1, mQueue.size());
245        assertEquals("[4]", mQueue.toString());
246        assertEquals(eventTime + 1, mElement1.mPhantomUpEventTime);
247        assertEquals(eventTime + 2, mElement2.mPhantomUpEventTime);
248        assertEquals(eventTime + 3, mElement3.mPhantomUpEventTime);
249        assertEquals(Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
250    }
251
252    public void testReleaseAllPointersExcept() {
253        mElement2.mIsModifier = true;
254        mQueue.add(mElement1);
255        mQueue.add(mElement2);
256        mQueue.add(mElement3);
257        mQueue.add(mElement4);
258
259        final long eventTime = 123;
260        Element.sPhantomUpCount = 0;
261        mQueue.releaseAllPointersExcept(mElement3, eventTime);
262        assertEquals(3, Element.sPhantomUpCount);
263        assertEquals(1, mQueue.size());
264        assertEquals("[3]", mQueue.toString());
265        assertEquals(eventTime + 1, mElement1.mPhantomUpEventTime);
266        assertEquals(eventTime + 2, mElement2.mPhantomUpEventTime);
267        assertEquals(Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
268        assertEquals(eventTime + 3, mElement4.mPhantomUpEventTime);
269    }
270
271    public void testHasModifierKeyOlderThan() {
272        Element.sPhantomUpCount = 0;
273        assertFalse("hasModifierKeyOlderThan empty", mQueue.hasModifierKeyOlderThan(mElement1));
274
275        mQueue.add(mElement1);
276        mQueue.add(mElement2);
277        mQueue.add(mElement3);
278        mQueue.add(mElement4);
279
280        assertFalse(mQueue.hasModifierKeyOlderThan(mElement1));
281        assertFalse(mQueue.hasModifierKeyOlderThan(mElement2));
282        assertFalse(mQueue.hasModifierKeyOlderThan(mElement3));
283        assertFalse(mQueue.hasModifierKeyOlderThan(mElement4));
284
285        mElement2.mIsModifier = true;
286        assertFalse(mQueue.hasModifierKeyOlderThan(mElement1));
287        assertFalse(mQueue.hasModifierKeyOlderThan(mElement2));
288        assertTrue(mQueue.hasModifierKeyOlderThan(mElement3));
289        assertTrue(mQueue.hasModifierKeyOlderThan(mElement4));
290
291        assertEquals(0, Element.sPhantomUpCount);
292        assertEquals(4, mQueue.size());
293        assertEquals("[1 2 3 4]", mQueue.toString());
294        assertEquals(Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime);
295        assertEquals(Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
296        assertEquals(Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
297        assertEquals(Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
298    }
299
300    public void testIsAnyInDraggingFinger() {
301        Element.sPhantomUpCount = 0;
302        assertFalse(mQueue.isAnyInDraggingFinger());
303
304        mQueue.add(mElement1);
305        mQueue.add(mElement2);
306        mQueue.add(mElement3);
307        mQueue.add(mElement4);
308
309        assertFalse(mQueue.isAnyInDraggingFinger());
310
311        mElement3.mIsInDraggingFinger = true;
312        assertTrue(mQueue.isAnyInDraggingFinger());
313
314        assertEquals(0, Element.sPhantomUpCount);
315        assertEquals(4, mQueue.size());
316        assertEquals("[1 2 3 4]", mQueue.toString());
317        assertEquals(Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime);
318        assertEquals(Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime);
319        assertEquals(Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime);
320        assertEquals(Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime);
321    }
322}
323