Fix VoicemailPlaybackTests.

There appears to be two issues which were borking this, although I'm not sure why
there was the change (maybe it's been around a long time).

A single instance of presenter is retained over time, which is no good for our case
where we're creating different FakeAsyncTaskExecutors each time, so added a method
for testing where we can clear the instance.

Secondly, the method checking the text views didn't work anymore. Not sure why, but
I added a hook into the state text so the tests can continue to check for that.

Bug: 23640774
Change-Id: Ie729627f3bb4ee08476d5ad0198e43cfea72ce5c
diff --git a/src/com/android/dialer/voicemail/VoicemailPlaybackLayout.java b/src/com/android/dialer/voicemail/VoicemailPlaybackLayout.java
index 133da36..14c5473 100644
--- a/src/com/android/dialer/voicemail/VoicemailPlaybackLayout.java
+++ b/src/com/android/dialer/voicemail/VoicemailPlaybackLayout.java
@@ -42,6 +42,7 @@
 import com.android.dialer.R;
 import com.android.dialer.calllog.CallLogAsyncTaskUtil;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 
 import java.util.concurrent.TimeUnit;
@@ -411,4 +412,9 @@
         }
         return String.format("%02d:%02d", minutes, seconds);
     }
+
+    @VisibleForTesting
+    public String getStateText() {
+        return mStateText.getText().toString();
+    }
 }
diff --git a/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java b/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java
index 62da942..95622bf 100644
--- a/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java
+++ b/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java
@@ -781,4 +781,9 @@
     public boolean isSpeakerphoneOn() {
         return mIsSpeakerphoneOn;
     }
+
+    @VisibleForTesting
+    public void clearInstance() {
+        sInstance = null;
+    }
 }
diff --git a/tests/src/com/android/dialer/voicemail/VoicemailPlaybackTest.java b/tests/src/com/android/dialer/voicemail/VoicemailPlaybackTest.java
index 6c7cf77..420a17c 100644
--- a/tests/src/com/android/dialer/voicemail/VoicemailPlaybackTest.java
+++ b/tests/src/com/android/dialer/voicemail/VoicemailPlaybackTest.java
@@ -17,6 +17,7 @@
 package com.android.dialer.voicemail;
 
 import static com.android.dialer.voicemail.VoicemailPlaybackPresenter.Tasks.CHECK_FOR_CONTENT;
+import static com.android.dialer.voicemail.VoicemailPlaybackPresenter.Tasks.CHECK_CONTENT_AFTER_CHANGE;
 
 import android.app.Activity;
 import android.content.ContentResolver;
@@ -31,7 +32,6 @@
 import android.view.View;
 import android.widget.TextView;
 
-import com.android.contacts.common.test.IntegrationTestUtils;
 import com.android.dialer.R;
 import com.android.dialer.calllog.CallLogActivity;
 import com.android.dialer.util.AsyncTaskExecutors;
@@ -61,7 +61,6 @@
     private VoicemailPlaybackLayout mLayout;
 
     private Uri mVoicemailUri;
-    private IntegrationTestUtils mTestUtils;
     private LocaleTestUtils mLocaleTestUtils;
     private FakeAsyncTaskExecutor mFakeAsyncTaskExecutor;
 
@@ -75,7 +74,6 @@
 
         mFakeAsyncTaskExecutor = new FakeAsyncTaskExecutor(getInstrumentation());
         AsyncTaskExecutors.setFactoryForTest(mFakeAsyncTaskExecutor.getFactory());
-        mTestUtils = new IntegrationTestUtils(getInstrumentation());
 
         // Some of the tests rely on the text - safest to force a specific locale.
         mLocaleTestUtils = new LocaleTestUtils(getInstrumentation().getTargetContext());
@@ -95,54 +93,72 @@
         mLocaleTestUtils.restoreLocale();
         mLocaleTestUtils = null;
 
-        mLayout = null;
-        mPresenter = null;
-        mTestUtils = null;
+        mPresenter.clearInstance();
         AsyncTaskExecutors.setFactoryForTest(null);
 
+        mActivity = null;
+        mPresenter = null;
+        mLayout = null;
+
         super.tearDown();
     }
 
     public void testFetchingVoicemail() throws Throwable {
-        setUriForRealFileVoicemailEntry();
+        setUriForUnfetchedVoicemailEntry();
         setPlaybackViewForPresenter();
-        assertHasOneTextViewContaining("Loading voicemail");
+
+        getInstrumentation().runOnMainSync(new Runnable() {
+            @Override
+            public void run() {
+                mPresenter.resumePlayback();
+            }
+        });
+        mFakeAsyncTaskExecutor.runTask(CHECK_FOR_CONTENT);
+        getInstrumentation().waitForIdleSync();
+
+        assertStateTextContains("Loading voicemail");
     }
 
     public void testWhenCheckForContentCompletes() throws Throwable {
         setUriForRealFileVoicemailEntry();
         setPlaybackViewForPresenter();
 
-        // There is a background check that is testing to see if we have the content available.
-        // Once that task completes, we shouldn't be showing the fetching message.
+        getInstrumentation().runOnMainSync(new Runnable() {
+            @Override
+            public void run() {
+                mPresenter.resumePlayback();
+            }
+        });
         mFakeAsyncTaskExecutor.runTask(CHECK_FOR_CONTENT);
         getInstrumentation().waitForIdleSync();
 
-        assertHasOneTextViewContaining("Buffering");
-        assertHasZeroTextViewsContaining("Loading voicemail");
+        // Since the content is already fetched, don't show the loading message.
+        assertStateTextNotContains("Loading voicemail");
     }
 
     public void testInvalidVoicemailShowsErrorMessage() throws Throwable {
         setUriForInvalidVoicemailEntry();
         setPlaybackViewForPresenter();
 
+        getInstrumentation().runOnMainSync(new Runnable() {
+            @Override
+            public void run() {
+                mPresenter.resumePlayback();
+            }
+        });
         mFakeAsyncTaskExecutor.runTask(CHECK_FOR_CONTENT);
         getInstrumentation().waitForIdleSync();
 
         // The media player will have thrown an IOException since the file doesn't exist.
         // This should have put a failed to play message on screen, buffering is gone.
-        assertHasOneTextViewContaining("Couldn't play voicemail");
-        assertHasZeroTextViewsContaining("Buffering");
+        assertStateTextContains("Couldn't play voicemail");
+        assertStateTextNotContains("Buffering");
     }
 
     public void testClickingSpeakerphoneButton() throws Throwable {
         setUriForRealFileVoicemailEntry();
         setPlaybackViewForPresenter();
 
-        // Wait for check for content to complete.
-        mFakeAsyncTaskExecutor.runTask(CHECK_FOR_CONTENT);
-        getInstrumentation().waitForIdleSync();
-
         // Check that the speakerphone is false to start.
         assertFalse(mPresenter.isSpeakerphoneOn());
 
@@ -176,6 +192,18 @@
         }
     }
 
+    private void setUriForUnfetchedVoicemailEntry() {
+        assertNull(mVoicemailUri);
+        ContentValues values = new ContentValues();
+        values.put(VoicemailContract.Voicemails.DATE, String.valueOf(System.currentTimeMillis()));
+        values.put(VoicemailContract.Voicemails.NUMBER, CONTACT_NUMBER);
+        values.put(VoicemailContract.Voicemails.MIME_TYPE, MIME_TYPE);
+        values.put(VoicemailContract.Voicemails.HAS_CONTENT, 0);
+        String packageName = getInstrumentation().getTargetContext().getPackageName();
+        mVoicemailUri = getContentResolver().insert(
+                VoicemailContract.Voicemails.buildSourceUri(packageName), values);
+    }
+
     private void setUriForInvalidVoicemailEntry() {
         assertNull(mVoicemailUri);
         ContentResolver contentResolver = getContentResolver();
@@ -204,18 +232,14 @@
         }
     }
 
-    private void assertHasOneTextViewContaining(String text) throws Throwable {
+    private void assertStateTextContains(String text) throws Throwable {
         assertNotNull(mLayout);
-        List<TextView> views = mTestUtils.getTextViewsWithString(mLayout, text);
-        assertEquals("There should have been one TextView with text '" + text + "' but found "
-                + views, 1, views.size());
+        assertTrue(mLayout.getStateText().contains(text));
     }
 
-    private void assertHasZeroTextViewsContaining(String text) throws Throwable {
+    private void assertStateTextNotContains(String text) throws Throwable {
         assertNotNull(mLayout);
-        List<TextView> views = mTestUtils.getTextViewsWithString(mLayout, text);
-        assertEquals("There should have been no TextViews with text '" + text + "' but found "
-                + views, 0,  views.size());
+        assertFalse(mLayout.getStateText().contains(text));
     }
 
     private ContentResolver getContentResolver() {