Home | History | Annotate | Download | only in email
      1 /*
      2  * Copyright (C) 2010 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.email;
     18 
     19 import com.android.email.provider.ProviderTestUtils;
     20 import com.android.emailcommon.provider.Account;
     21 import com.android.emailcommon.utility.Utility;
     22 
     23 import android.content.Context;
     24 import android.test.InstrumentationTestCase;
     25 import android.test.suitebuilder.annotation.LargeTest;
     26 
     27 import java.util.ArrayList;
     28 import java.util.concurrent.atomic.AtomicBoolean;
     29 
     30 /**
     31  * Large tests for {@link Utility}.
     32  */
     33 @LargeTest
     34 public class UtilityLargeTest extends InstrumentationTestCase {
     35     private static final int WAIT_UNTIL_TIMEOUT_SECONDS = 10;
     36 
     37     // Isolted Context for providers.
     38     private Context mProviderContext;
     39 
     40 
     41     @Override
     42     protected void setUp() throws Exception {
     43         super.setUp();
     44         mProviderContext = DBTestHelper.ProviderContextSetupHelper.getProviderContext(
     45                 getInstrumentation().getTargetContext());
     46     }
     47 
     48 
     49     public void testForEachAccount() throws Throwable {
     50         // Create some accounts...
     51         Account acct1 = ProviderTestUtils.setupAccount("acct1", true, mProviderContext);
     52         Account acct2 = ProviderTestUtils.setupAccount("acct2", true, mProviderContext);
     53 
     54         final ArrayList<Long> ids = new ArrayList<Long>(); // Account id array.
     55         final AtomicBoolean done = new AtomicBoolean(false);
     56 
     57         // Kick ForEachAccount and collect IDs...
     58         // AsyncTask needs to be created on the UI thread.
     59         runTestOnUiThread(new Runnable() {
     60             @Override
     61             public void run() {
     62                 new Utility.ForEachAccount(mProviderContext) {
     63                     @Override
     64                     protected void performAction(long accountId) {
     65                         synchronized (ids) {
     66                             ids.add(accountId);
     67                         }
     68                     }
     69 
     70                     @Override
     71                     protected void onFinished() {
     72                         done.set(true);
     73                     }
     74                 }.execute();
     75             }
     76         });
     77 
     78         // Wait until it's done...
     79         TestUtils.waitUntil(new TestUtils.Condition() {
     80             @Override
     81             public boolean isMet() {
     82                 return done.get();
     83             }
     84         }, WAIT_UNTIL_TIMEOUT_SECONDS);
     85 
     86         // Check the collected IDs.
     87         synchronized (ids) {
     88             assertEquals(2, ids.size());
     89             // ids may not be sorted, so...
     90             assertTrue(ids.contains(acct1.mId));
     91             assertTrue(ids.contains(acct2.mId));
     92         }
     93     }
     94 }
     95