Home | History | Annotate | Download | only in ui
      1 package com.android.mail.ui;
      2 
      3 import android.app.Activity;
      4 import android.app.LoaderManager;
      5 import android.content.Intent;
      6 import android.content.Loader;
      7 import android.os.Bundle;
      8 import android.test.ActivityUnitTestCase;
      9 import android.test.suitebuilder.annotation.SmallTest;
     10 
     11 import com.google.common.annotations.VisibleForTesting;
     12 
     13 import java.util.concurrent.CountDownLatch;
     14 import java.util.concurrent.TimeUnit;
     15 
     16 public class MailAsyncTaskLoaderTest
     17         extends ActivityUnitTestCase<MailAsyncTaskLoaderTest.LoaderTestActivity> {
     18 
     19     public static class LoaderTestActivity extends Activity {
     20         final CountDownLatch loadFinishedLatch = new CountDownLatch(1);
     21         final CountDownLatch resultDiscardedLatch = new CountDownLatch(1);
     22         volatile Object result;
     23 
     24         public void runLoaderTest() {
     25             result = new Object();
     26 
     27             getLoaderManager().initLoader(0, null, new LoaderManager.LoaderCallbacks<Object>() {
     28                 @Override
     29                 public Loader<Object> onCreateLoader(int id, Bundle args) {
     30                     return new MailAsyncTaskLoader<Object>(LoaderTestActivity.this) {
     31                         @Override
     32                         protected void onDiscardResult(Object result) {
     33                             MailAsyncTaskLoaderTest.assertNotNull(result);
     34                             resultDiscardedLatch.countDown();
     35                         }
     36 
     37                         @Override
     38                         public Object loadInBackground() {
     39                             return result;
     40                         }
     41                     };
     42                 }
     43 
     44                 @Override
     45                 public void onLoadFinished(Loader<Object> loader, Object data) {
     46                     MailAsyncTaskLoaderTest.assertEquals(data, result);
     47                     loadFinishedLatch.countDown();
     48                 }
     49 
     50                 @Override
     51                 public void onLoaderReset(Loader<Object> loader) {}
     52             });
     53             while (true) {
     54                 try {
     55                     MailAsyncTaskLoaderTest.assertTrue(
     56                             loadFinishedLatch.await(30, TimeUnit.SECONDS));
     57                 } catch (final InterruptedException e) {
     58                     continue;
     59                 }
     60                 break;
     61             }
     62             getLoaderManager().destroyLoader(0);
     63             while (true) {
     64                 try {
     65                     MailAsyncTaskLoaderTest.assertTrue(
     66                             resultDiscardedLatch.await(30, TimeUnit.SECONDS));
     67                 } catch (final InterruptedException e) {
     68                     continue;
     69                 }
     70                 break;
     71             }
     72         }
     73     }
     74 
     75     public MailAsyncTaskLoaderTest() {
     76         super(LoaderTestActivity.class);
     77     }
     78 
     79     @SmallTest
     80     public void testLoader() {
     81         startActivity(new Intent(Intent.ACTION_MAIN), null, null);
     82         getInstrumentation().callActivityOnStart(getActivity());
     83         getActivity().runLoaderTest();
     84     }
     85 }
     86