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