1bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette/* 2bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette * Copyright (C) 2015 The Android Open Source Project 3bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette * 4bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette * Licensed under the Apache License, Version 2.0 (the "License"); 5bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette * you may not use this file except in compliance with the License. 6bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette * You may obtain a copy of the License at 7bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette * 8bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette * http://www.apache.org/licenses/LICENSE-2.0 9bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette * 10bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette * Unless required by applicable law or agreed to in writing, software 11bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette * distributed under the License is distributed on an "AS IS" BASIS, 12bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette * See the License for the specific language governing permissions and 14bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette * limitations under the License. 15bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette */ 16bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette 17bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverettepackage android.view; 18bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette 19bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viveretteimport android.test.AndroidTestCase; 20bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viveretteimport android.test.suitebuilder.annotation.SmallTest; 21bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette 22bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverettepublic class HandlerActionQueueTest extends AndroidTestCase { 23bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette 24bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette @SmallTest 25bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette public void testPostAndRemove() { 26bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette HandlerActionQueue runQueue = new HandlerActionQueue(); 27bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette MockRunnable runnable1 = new MockRunnable(); 28bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette MockRunnable runnable2 = new MockRunnable(); 29bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette MockRunnable runnable3 = new MockRunnable(); 30bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette 31bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette runQueue.post(runnable1); 32bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette runQueue.post(runnable1); 33bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette runQueue.post(runnable2); 34bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette runQueue.postDelayed(runnable1, 100); 35bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette runQueue.postDelayed(null, 500); 36bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(5, runQueue.size()); 37bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(0, runQueue.getDelay(0)); 38bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(0, runQueue.getDelay(1)); 39bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(0, runQueue.getDelay(2)); 40bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(100, runQueue.getDelay(3)); 41bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(500, runQueue.getDelay(4)); 42bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(500, runQueue.getDelay(4)); 43bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(runnable1, runQueue.getRunnable(0)); 44bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(runnable1, runQueue.getRunnable(1)); 45bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(runnable2, runQueue.getRunnable(2)); 46bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(runnable1, runQueue.getRunnable(3)); 47bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(null, runQueue.getRunnable(4)); 48bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette 49bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette runQueue.removeCallbacks(runnable1); 50bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(2, runQueue.size()); 51bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(0, runQueue.getDelay(0)); 52bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(500, runQueue.getDelay(1)); 53bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(runnable2, runQueue.getRunnable(0)); 54bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(null, runQueue.getRunnable(1)); 55bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette 56bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette try { 57bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertNull(runQueue.getRunnable(2)); 58bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertFalse(true); 59bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette } catch (IndexOutOfBoundsException e) { 60bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette // Should throw an exception. 61bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette } 62bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette 63bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette runQueue.removeCallbacks(runnable3); 64bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(2, runQueue.size()); 65bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette 66bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette runQueue.removeCallbacks(runnable2); 67bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(1, runQueue.size()); 68bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(null, runQueue.getRunnable(0)); 69bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette 70bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette runQueue.removeCallbacks(null); 71bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette assertEquals(0, runQueue.size()); 72bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette } 73bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette 74bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette private static class MockRunnable implements Runnable { 75bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette @Override 76bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette public void run() { 77bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette 78bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette } 79bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette } 80bea0c7daa6611d8b96e1271f8854f500a87342fcAlan Viverette} 81