1/*
2 * Copyright (C) 2015 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 android.view;
18
19import android.test.AndroidTestCase;
20import android.test.suitebuilder.annotation.SmallTest;
21
22public class HandlerActionQueueTest extends AndroidTestCase {
23
24    @SmallTest
25    public void testPostAndRemove() {
26        HandlerActionQueue runQueue = new HandlerActionQueue();
27        MockRunnable runnable1 = new MockRunnable();
28        MockRunnable runnable2 = new MockRunnable();
29        MockRunnable runnable3 = new MockRunnable();
30
31        runQueue.post(runnable1);
32        runQueue.post(runnable1);
33        runQueue.post(runnable2);
34        runQueue.postDelayed(runnable1, 100);
35        runQueue.postDelayed(null, 500);
36        assertEquals(5, runQueue.size());
37        assertEquals(0, runQueue.getDelay(0));
38        assertEquals(0, runQueue.getDelay(1));
39        assertEquals(0, runQueue.getDelay(2));
40        assertEquals(100, runQueue.getDelay(3));
41        assertEquals(500, runQueue.getDelay(4));
42        assertEquals(500, runQueue.getDelay(4));
43        assertEquals(runnable1, runQueue.getRunnable(0));
44        assertEquals(runnable1, runQueue.getRunnable(1));
45        assertEquals(runnable2, runQueue.getRunnable(2));
46        assertEquals(runnable1, runQueue.getRunnable(3));
47        assertEquals(null, runQueue.getRunnable(4));
48
49        runQueue.removeCallbacks(runnable1);
50        assertEquals(2, runQueue.size());
51        assertEquals(0, runQueue.getDelay(0));
52        assertEquals(500, runQueue.getDelay(1));
53        assertEquals(runnable2, runQueue.getRunnable(0));
54        assertEquals(null, runQueue.getRunnable(1));
55
56        try {
57            assertNull(runQueue.getRunnable(2));
58            assertFalse(true);
59        } catch (IndexOutOfBoundsException e) {
60            // Should throw an exception.
61        }
62
63        runQueue.removeCallbacks(runnable3);
64        assertEquals(2, runQueue.size());
65
66        runQueue.removeCallbacks(runnable2);
67        assertEquals(1, runQueue.size());
68        assertEquals(null, runQueue.getRunnable(0));
69
70        runQueue.removeCallbacks(null);
71        assertEquals(0, runQueue.size());
72    }
73
74    private static class MockRunnable implements Runnable {
75        @Override
76        public void run() {
77
78        }
79    }
80}
81