UtilityLargeTest.java revision f52afae9424fe41071cc34a8d6cbcb82b992a411
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
17package com.android.email;
18
19import com.android.email.provider.EmailContent.Account;
20import com.android.email.provider.EmailProvider;
21import com.android.email.provider.ProviderTestUtils;
22
23import android.content.Context;
24import android.test.InstrumentationTestCase;
25import android.test.suitebuilder.annotation.LargeTest;
26
27import java.util.ArrayList;
28import java.util.concurrent.atomic.AtomicBoolean;
29
30/**
31 * Large tests for {@link Utility}.
32 */
33@LargeTest
34public 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(), EmailProvider.class);
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