Adding new test: ContactBrowserActivityTest

Change-Id: Ib30796b5ab4a3a30704385e7b4e0eb47451a15df
diff --git a/tests/src/com/android/contacts/activities/ContactBrowserActivityTest.java b/tests/src/com/android/contacts/activities/ContactBrowserActivityTest.java
new file mode 100644
index 0000000..a642b56
--- /dev/null
+++ b/tests/src/com/android/contacts/activities/ContactBrowserActivityTest.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts.activities;
+
+import com.android.contacts.R;
+import com.android.contacts.tests.mocks.ContactsMockContext;
+import com.android.contacts.tests.mocks.MockContentProvider;
+
+import android.content.Intent;
+import android.provider.ContactsContract.Groups;
+import android.provider.ContactsContract.ProviderStatus;
+import android.test.ActivityUnitTestCase;
+
+/**
+ * Tests for {@link ContactBrowserActivity}.
+ *
+ * Running all tests:
+ *
+ *   runtest contacts
+ * or
+ *   adb shell am instrument \
+ *     -w com.android.contacts.tests/android.test.InstrumentationTestRunner
+ */
+public class ContactBrowserActivityTest
+        extends ActivityUnitTestCase<ContactBrowserActivity>
+{
+    private ContactsMockContext mContext;
+    private MockContentProvider mContactsProvider;
+
+    public ContactBrowserActivityTest() {
+        super(ContactBrowserActivity.class);
+    }
+
+    @Override
+    public void setUp() {
+        mContext = new ContactsMockContext(getInstrumentation().getTargetContext());
+        mContactsProvider = mContext.getContactsProvider();
+        setActivityContext(mContext);
+    }
+
+    public void testSingleAccountNoGroups() {
+
+        // TODO: actually simulate a single account
+
+        expectProviderStatusQueryAndReturnNormal();
+        expectGroupsQueryAndReturnEmpty();
+
+        Intent intent = new Intent(Intent.ACTION_DEFAULT);
+
+        ContactBrowserActivity activity = startActivity(intent, null, null);
+
+        getInstrumentation().callActivityOnResume(activity);
+        getInstrumentation().callActivityOnStart(activity);
+
+        mContext.waitForLoaders(activity, R.id.contact_list_filter_loader);
+
+        getInstrumentation().waitForIdleSync();
+
+        mContext.verify();
+    }
+
+    private void expectProviderStatusQueryAndReturnNormal() {
+        mContactsProvider
+                .expectQuery(ProviderStatus.CONTENT_URI)
+                .withProjection(ProviderStatus.STATUS, ProviderStatus.DATA1)
+                .returnRow(ProviderStatus.STATUS_NORMAL, null);
+    }
+
+    private void expectGroupsQueryAndReturnEmpty() {
+        mContactsProvider
+                .expectQuery(Groups.CONTENT_URI)
+                .withAnyProjection()
+                .withAnySelection()
+                .returnEmptyCursor();
+    }
+}
diff --git a/tests/src/com/android/contacts/tests/mocks/ContactsMockContext.java b/tests/src/com/android/contacts/tests/mocks/ContactsMockContext.java
index 2cce073..a7aaba4 100644
--- a/tests/src/com/android/contacts/tests/mocks/ContactsMockContext.java
+++ b/tests/src/com/android/contacts/tests/mocks/ContactsMockContext.java
@@ -18,6 +18,8 @@
 
 //import com.android.providers.contacts.ContactsMockPackageManager;
 
+import android.app.Activity;
+import android.content.AsyncTaskLoader;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.ContextWrapper;
@@ -78,4 +80,35 @@
         mContactsProvider.verify();
         mSettingsProvider.verify();
     }
+
+    /**
+     * Waits for the specified loaders to complete loading.
+     */
+    public void waitForLoaders(final Activity activity, int... loaderIds) {
+        // We want to wait for each loader using a separate thread, so that we can
+        // simulate race conditions.
+        Thread[] waitThreads = new Thread[loaderIds.length];
+        for (int i = 0; i < loaderIds.length; i++) {
+            final int loaderId = loaderIds[i];
+            waitThreads[i] = new Thread("LoaderWaitingThread" + i) {
+                @Override
+                public void run() {
+                    AsyncTaskLoader<?> loader =
+                            (AsyncTaskLoader<?>) activity.getLoaderManager().getLoader(loaderId);
+                    loader.waitForLoader();
+                }
+            };
+            waitThreads[i].start();
+        }
+
+        // Now we wait for all these threads to finish
+        for (Thread thread : waitThreads) {
+            try {
+                thread.join();
+            } catch (InterruptedException e) {
+                // Ignore
+            }
+        }
+    }
+
 }