From 4034eaecff7e71f3de95a192c144e9c21e6f5a29 Mon Sep 17 00:00:00 2001 From: George Mount Date: Tue, 16 May 2017 13:07:51 -0700 Subject: [PATCH] Fix broken LoaderLifecycleTest#loaderIdentityTest Bug 37994415 We have a preemptive check for getLoaderManager() to ensure that the host is set before returning a value. In this test, the host was not set because the fragment was restored and on the back stack. The solution is to pop the back stack to ensure that it is added so that it comes up to the proper state before getting the loader manager. Test: ran the broken test Change-Id: Ic91cc5ca9468da22bdfef28cda1d4488b2f737f0 --- .../src/android/app/LoaderLifecycleTest.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/core/tests/coretests/src/android/app/LoaderLifecycleTest.java b/core/tests/coretests/src/android/app/LoaderLifecycleTest.java index 1850d575dfa58..c83e7988dcc64 100644 --- a/core/tests/coretests/src/android/app/LoaderLifecycleTest.java +++ b/core/tests/coretests/src/android/app/LoaderLifecycleTest.java @@ -17,6 +17,13 @@ package android.app; +import static junit.framework.TestCase.assertNotNull; +import static junit.framework.TestCase.assertNotSame; +import static junit.framework.TestCase.assertSame; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import android.content.Context; import android.os.Handler; import android.os.Parcelable; @@ -24,14 +31,11 @@ import android.support.test.filters.MediumTest; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; import android.util.ArrayMap; + import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; -import static junit.framework.TestCase.assertNotNull; -import static junit.framework.TestCase.assertNotSame; -import static junit.framework.TestCase.assertSame; - @RunWith(AndroidJUnit4.class) public class LoaderLifecycleTest { @Rule @@ -203,6 +207,16 @@ public class LoaderLifecycleTest { // Test that the fragments are in the configuration we expect final Fragment restoredOne = fm2.findFragmentByTag("one"); + try { + restoredOne.getLoaderManager(); + fail("A restored fragment on the back stack doesn't have a host, so it should " + + "throw an exception"); + } catch (IllegalStateException e) { + // expected + } + fm2.popBackStackImmediate(); + // Now restoredOne should be added and should be in a good state. + assertTrue(restoredOne.isAdded()); final LoaderManager lm2 = restoredOne.getLoaderManager(); assertSame("didn't get same LoaderManager instance back", lm2, lm1);