Merge "Maybe fix flaky testDetachCalledWhenDestroyFragment" into nyc-support-25.2-dev am: c24a66bc90
am: 0c43a1711c

Change-Id: Ic95d4050f4225fdfb36f7f4e7bfad60bc793d591
diff --git a/v17/leanback/tests/java/android/support/v17/leanback/app/PlaybackFragmentTest.java b/v17/leanback/tests/java/android/support/v17/leanback/app/PlaybackFragmentTest.java
index 2fcf3ed..7282eac 100644
--- a/v17/leanback/tests/java/android/support/v17/leanback/app/PlaybackFragmentTest.java
+++ b/v17/leanback/tests/java/android/support/v17/leanback/app/PlaybackFragmentTest.java
@@ -52,7 +52,7 @@
     @Test
     public void testDetachCalledWhenDestroyFragment() throws Throwable {
         launchAndWaitActivity(PlaybackTestFragment.class, 1000);
-        PlaybackTestFragment fragment = (PlaybackTestFragment) mActivity.getTestFragment();
+        final PlaybackTestFragment fragment = (PlaybackTestFragment) mActivity.getTestFragment();
         PlaybackGlue glue = fragment.getGlue();
         activityTestRule.runOnUiThread(new Runnable() {
             public void run() {
@@ -62,7 +62,7 @@
         PollingCheck.waitFor(new PollingCheck.PollingCheckCondition() {
             @Override
             public boolean canProceed() {
-                return mActivity.isDestroyed();
+                return fragment.mDestroyCalled;
             }
         });
         assertNull(glue.getHost());
diff --git a/v17/leanback/tests/java/android/support/v17/leanback/app/PlaybackSupportFragmentTest.java b/v17/leanback/tests/java/android/support/v17/leanback/app/PlaybackSupportFragmentTest.java
index d33e3ef..e32d90b 100644
--- a/v17/leanback/tests/java/android/support/v17/leanback/app/PlaybackSupportFragmentTest.java
+++ b/v17/leanback/tests/java/android/support/v17/leanback/app/PlaybackSupportFragmentTest.java
@@ -55,7 +55,7 @@
     @Test
     public void testDetachCalledWhenDestroyFragment() throws Throwable {
         launchAndWaitActivity(PlaybackTestSupportFragment.class, 1000);
-        PlaybackTestSupportFragment fragment = (PlaybackTestSupportFragment) mActivity.getTestFragment();
+        final PlaybackTestSupportFragment fragment = (PlaybackTestSupportFragment) mActivity.getTestFragment();
         PlaybackGlue glue = fragment.getGlue();
         activityTestRule.runOnUiThread(new Runnable() {
             public void run() {
@@ -65,7 +65,7 @@
         PollingCheck.waitFor(new PollingCheck.PollingCheckCondition() {
             @Override
             public boolean canProceed() {
-                return mActivity.isDestroyed();
+                return fragment.mDestroyCalled;
             }
         });
         assertNull(glue.getHost());
diff --git a/v17/leanback/tests/java/android/support/v17/leanback/app/PlaybackTestFragment.java b/v17/leanback/tests/java/android/support/v17/leanback/app/PlaybackTestFragment.java
index f9fd33f..51c2c10 100644
--- a/v17/leanback/tests/java/android/support/v17/leanback/app/PlaybackTestFragment.java
+++ b/v17/leanback/tests/java/android/support/v17/leanback/app/PlaybackTestFragment.java
@@ -60,6 +60,7 @@
 
     private android.support.v17.leanback.media.PlaybackControlGlue mGlue;
     private ListRowPresenter mListRowPresenter;
+    boolean mDestroyCalled;
 
     public SparseArrayObjectAdapter getAdapter() {
         return (SparseArrayObjectAdapter) super.getAdapter();
@@ -74,6 +75,12 @@
     };
 
     @Override
+    public void onDestroy() {
+        super.onDestroy();
+        mDestroyCalled = true;
+    }
+
+    @Override
     public void onCreate(Bundle savedInstanceState) {
         Log.i(TAG, "onCreate");
         super.onCreate(savedInstanceState);
diff --git a/v17/leanback/tests/java/android/support/v17/leanback/app/PlaybackTestSupportFragment.java b/v17/leanback/tests/java/android/support/v17/leanback/app/PlaybackTestSupportFragment.java
index 0973fe1..1234421 100644
--- a/v17/leanback/tests/java/android/support/v17/leanback/app/PlaybackTestSupportFragment.java
+++ b/v17/leanback/tests/java/android/support/v17/leanback/app/PlaybackTestSupportFragment.java
@@ -63,6 +63,7 @@
 
     private android.support.v17.leanback.media.PlaybackControlGlue mGlue;
     private ListRowPresenter mListRowPresenter;
+    boolean mDestroyCalled;
 
     public SparseArrayObjectAdapter getAdapter() {
         return (SparseArrayObjectAdapter) super.getAdapter();
@@ -77,6 +78,12 @@
     };
 
     @Override
+    public void onDestroy() {
+        super.onDestroy();
+        mDestroyCalled = true;
+    }
+
+    @Override
     public void onCreate(Bundle savedInstanceState) {
         Log.i(TAG, "onCreate");
         super.onCreate(savedInstanceState);
diff --git a/v17/leanback/tests/java/android/support/v17/leanback/app/SingleFragmentTestBase.java b/v17/leanback/tests/java/android/support/v17/leanback/app/SingleFragmentTestBase.java
index fd1f922..6c1f5bc 100644
--- a/v17/leanback/tests/java/android/support/v17/leanback/app/SingleFragmentTestBase.java
+++ b/v17/leanback/tests/java/android/support/v17/leanback/app/SingleFragmentTestBase.java
@@ -19,6 +19,7 @@
 import android.os.SystemClock;
 import android.support.test.InstrumentationRegistry;
 import android.support.test.rule.ActivityTestRule;
+import android.support.v17.leanback.testutils.PollingCheck;
 
 import org.junit.After;
 import org.junit.Rule;
@@ -37,14 +38,16 @@
 
     @After
     public void afterTest() throws Throwable {
-        activityTestRule.runOnUiThread(new Runnable() {
-            public void run() {
-                if (mActivity != null) {
-                    mActivity.finish();
-                    mActivity = null;
+        final SingleFragmentTestActivity activity = mActivity;
+        if (activity != null) {
+            mActivity = null;
+            activityTestRule.runOnUiThread(new Runnable() {
+                public void run() {
+                    activity.finish();
                 }
-            }
-        });
+            });
+            PollingCheck.waitFor(new PollingCheck.ActivityDestroy(activity));
+        }
     }
 
     public void sendKeys(int ...keys) {
diff --git a/v17/leanback/tests/java/android/support/v17/leanback/app/SingleSupportFragmentTestBase.java b/v17/leanback/tests/java/android/support/v17/leanback/app/SingleSupportFragmentTestBase.java
index a7cb793..9c872f8 100644
--- a/v17/leanback/tests/java/android/support/v17/leanback/app/SingleSupportFragmentTestBase.java
+++ b/v17/leanback/tests/java/android/support/v17/leanback/app/SingleSupportFragmentTestBase.java
@@ -22,6 +22,7 @@
 import android.os.SystemClock;
 import android.support.test.InstrumentationRegistry;
 import android.support.test.rule.ActivityTestRule;
+import android.support.v17.leanback.testutils.PollingCheck;
 
 import org.junit.After;
 import org.junit.Rule;
@@ -40,14 +41,16 @@
 
     @After
     public void afterTest() throws Throwable {
-        activityTestRule.runOnUiThread(new Runnable() {
-            public void run() {
-                if (mActivity != null) {
-                    mActivity.finish();
-                    mActivity = null;
+        final SingleSupportFragmentTestActivity activity = mActivity;
+        if (activity != null) {
+            mActivity = null;
+            activityTestRule.runOnUiThread(new Runnable() {
+                public void run() {
+                    activity.finish();
                 }
-            }
-        });
+            });
+            PollingCheck.waitFor(new PollingCheck.ActivityDestroy(activity));
+        }
     }
 
     public void sendKeys(int ...keys) {