1 /* 2 * Copyright (C) 2011 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 17 package com.android.emailcommon.utility; 18 19 import android.test.AndroidTestCase; 20 21 import java.util.ArrayList; 22 import java.util.concurrent.atomic.AtomicInteger; 23 24 public class DelayedOperationsTests extends AndroidTestCase { 25 private DelayedOperationsForTest mDelayedOperations; 26 27 @Override 28 protected void setUp() throws Exception { 29 super.setUp(); 30 31 mDelayedOperations = new DelayedOperationsForTest(); 32 } 33 34 public void testEnueue() { 35 // Can pass only final vars, so AtomicInteger. 36 final AtomicInteger i = new AtomicInteger(1); 37 38 mDelayedOperations.post(new Runnable() { 39 @Override public void run() { 40 i.addAndGet(2); 41 } 42 }); 43 44 mDelayedOperations.post(new Runnable() { 45 @Override public void run() { 46 i.addAndGet(4); 47 } 48 }); 49 50 // 2 ops queued. 51 assertEquals(2, mDelayedOperations.mPendingOperations.size()); 52 53 // Value still not changed. 54 assertEquals(1, i.get()); 55 56 // Execute all pending tasks! 57 mDelayedOperations.runQueuedOperations(); 58 59 // 1 + 2 + 4 = 7 60 assertEquals(7, i.get()); 61 62 // No pending tasks. 63 assertEquals(0, mDelayedOperations.mPendingOperations.size()); 64 } 65 66 public void testCancel() { 67 // Can pass only final vars, so AtomicInteger. 68 final AtomicInteger i = new AtomicInteger(1); 69 70 // Post & cancel it immediately 71 Runnable r; 72 mDelayedOperations.post(r = new Runnable() { 73 @Override public void run() { 74 i.addAndGet(2); 75 } 76 }); 77 mDelayedOperations.removeCallbacks(r); 78 79 mDelayedOperations.post(new Runnable() { 80 @Override public void run() { 81 i.addAndGet(4); 82 } 83 }); 84 85 // 1 op queued. 86 assertEquals(1, mDelayedOperations.mPendingOperations.size()); 87 88 // Value still not changed. 89 assertEquals(1, i.get()); 90 91 // Execute all pending tasks! 92 mDelayedOperations.runQueuedOperations(); 93 94 // 1 + 4 = 5 95 assertEquals(5, i.get()); 96 97 // No pending tasks. 98 assertEquals(0, mDelayedOperations.mPendingOperations.size()); 99 } 100 101 public void testCancelAll() { 102 // Can pass only final vars, so AtomicInteger. 103 final AtomicInteger i = new AtomicInteger(1); 104 105 mDelayedOperations.post(new Runnable() { 106 @Override public void run() { 107 i.addAndGet(2); 108 } 109 }); 110 111 mDelayedOperations.post(new Runnable() { 112 @Override public void run() { 113 i.addAndGet(4); 114 } 115 }); 116 117 // 2 op queued. 118 assertEquals(2, mDelayedOperations.mPendingOperations.size()); 119 120 // Value still not changed. 121 assertEquals(1, i.get()); 122 123 // Cancel all!! 124 mDelayedOperations.removeCallbacks(); 125 126 // There should be no pending tasks in handler. 127 assertEquals(0, mDelayedOperations.mPostedToHandler.size()); 128 129 // Nothing should have changed. 130 assertEquals(1, i.get()); 131 132 // No pending tasks. 133 assertEquals(0, mDelayedOperations.mPendingOperations.size()); 134 } 135 136 private static class DelayedOperationsForTest extends DelayedOperations { 137 // Represents all runnables pending in the handler. 138 public final ArrayList<Runnable> mPostedToHandler = new ArrayList<Runnable>(); 139 140 public DelayedOperationsForTest() { 141 super(null); 142 } 143 144 // Emulate Handler.post 145 @Override 146 void postRunnable(Runnable r) { 147 mPostedToHandler.add(r); 148 } 149 150 // Emulate Handler.removeCallbacks 151 @Override 152 void cancelRunnable(Runnable r) { 153 mPostedToHandler.remove(r); 154 } 155 156 public void runQueuedOperations() { 157 for (Runnable r : mPostedToHandler) { 158 r.run(); 159 } 160 mPostedToHandler.clear(); 161 } 162 } 163 } 164