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