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 com.android.emailcommon.utility.EmailAsyncTask.Tracker;
     20 
     21 import android.test.AndroidTestCase;
     22 import android.test.MoreAsserts;
     23 
     24 public class EmailAsyncTaskTests extends AndroidTestCase {
     25     public void testAll() throws Exception {
     26         // Because AsyncTask relies on the UI thread and how we use threads in test, we can't
     27         // execute() these tasks.
     28         // Instead, we directly call onPostExecute/onCancel.
     29 
     30         final EmailAsyncTask.Tracker tracker = new EmailAsyncTask.Tracker();
     31 
     32         // Initially empty
     33         assertEquals(0, tracker.getTaskCountForTest());
     34 
     35         // Start 4 tasks
     36         final MyTask task1 = new MyTask(tracker);
     37         assertEquals(1, tracker.getTaskCountForTest());
     38 
     39         final MyTask task2 = new MyTask(tracker);
     40         assertEquals(2, tracker.getTaskCountForTest());
     41 
     42         final MyTask task3 = new MyTask(tracker);
     43         assertEquals(3, tracker.getTaskCountForTest());
     44 
     45         final MyTask task4 = new MyTask(tracker);
     46         assertEquals(4, tracker.getTaskCountForTest());
     47 
     48         // Check the piping for doInBackground
     49         task1.mDoInBackgroundResult = "R";
     50         assertEquals("R", task1.callDoInBackgroundForTest("1", "2"));
     51         MoreAsserts.assertEquals(new String[] {"1", "2"}, task1.mDoInBackgroundArg);
     52 
     53         // Finish task1
     54         task1.callOnPostExecuteForTest("a");
     55 
     56         // onPostExecute should unregister the instance
     57         assertEquals(3, tracker.getTaskCountForTest());
     58         // and call onPostExecuteInternal
     59         assertEquals("a", task1.mOnPostExecuteArg);
     60         assertNull(task1.mOnCancelledArg);
     61 
     62         // Cancel task 3
     63         task3.callOnCancelledForTest("b");
     64         // onCancelled should unregister the instance too
     65         assertEquals(2, tracker.getTaskCountForTest());
     66         // and call onCancelledInternal
     67         assertNull(task3.mOnPostExecuteArg);
     68         assertEquals("b", task3.mOnCancelledArg);
     69 
     70         // Task 2 and 4 are still registered.
     71 
     72         // Cancel all left
     73         tracker.cancellAllInterrupt();
     74 
     75         // Check if they're canceled
     76         assertEquals(0, tracker.getTaskCountForTest());
     77     }
     78 
     79     // Make sure null tracker will be accepted
     80     public void testNullTracker() {
     81         final MyTask task1 = new MyTask(null);
     82         task1.unregisterSelf();
     83     }
     84 
     85     /**
     86      * Test for {@link EmailAsyncTask.Tracker#cancelOthers}
     87      */
     88     public void testCancellOthers() {
     89         final EmailAsyncTask.Tracker tracker = new EmailAsyncTask.Tracker();
     90 
     91         final MyTask task1 = new MyTask(tracker);
     92         final MyTask task2 = new MyTask(tracker);
     93         final MyTask task3 = new MyTask(tracker);
     94 
     95         final MyTask sub1 = new MyTaskSubClass(tracker);
     96         final MyTask sub2 = new MyTaskSubClass(tracker);
     97         final MyTask sub3 = new MyTaskSubClass(tracker);
     98 
     99         // All should be in the tracker.
    100         assertEquals(6, tracker.getTaskCountForTest());
    101 
    102         // This should remove task1, task2, but not task3 itself.
    103         tracker.cancelOthers(task3);
    104 
    105         assertEquals(4, tracker.getTaskCountForTest());
    106         assertTrue(tracker.containsTaskForTest(task3));
    107 
    108         // Same for sub1.
    109         tracker.cancelOthers(sub1);
    110 
    111         assertEquals(2, tracker.getTaskCountForTest());
    112         assertTrue(tracker.containsTaskForTest(task3));
    113         assertTrue(tracker.containsTaskForTest(sub1));
    114     }
    115 
    116     private static class MyTask extends EmailAsyncTask<String, String, String> {
    117         public String[] mDoInBackgroundArg;
    118         public String mDoInBackgroundResult;
    119         public String mOnCancelledArg;
    120         public String mOnPostExecuteArg;
    121 
    122         public MyTask(Tracker tracker) {
    123             super(tracker);
    124         }
    125 
    126         @Override
    127         protected String doInBackground(String... params) {
    128             mDoInBackgroundArg = params;
    129             return mDoInBackgroundResult;
    130         }
    131 
    132         @Override
    133         protected void onCancelled(String result) {
    134             mOnCancelledArg = result;
    135         }
    136 
    137         @Override
    138         protected void onSuccess(String result) {
    139             mOnPostExecuteArg = result;
    140         }
    141     }
    142 
    143     private static class MyTaskSubClass extends MyTask {
    144         public MyTaskSubClass(Tracker tracker) {
    145             super(tracker);
    146         }
    147     }
    148 }
    149