1180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell/*
2180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell * Copyright (C) 2016 The Android Open Source Project
3180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell *
4180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell * Licensed under the Apache License, Version 2.0 (the "License");
5180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell * you may not use this file except in compliance with the License.
6180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell * You may obtain a copy of the License at
7180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell *
8180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell *      http://www.apache.org/licenses/LICENSE-2.0
9180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell *
10180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell * Unless required by applicable law or agreed to in writing, software
11180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell * distributed under the License is distributed on an "AS IS" BASIS,
12180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell * See the License for the specific language governing permissions and
14180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell * limitations under the License.
15180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell */
16180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
17180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
18180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powellpackage android.app;
19180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
204034eaecff7e71f3de95a192c144e9c21e6f5a29George Mountimport static junit.framework.TestCase.assertNotNull;
214034eaecff7e71f3de95a192c144e9c21e6f5a29George Mountimport static junit.framework.TestCase.assertNotSame;
224034eaecff7e71f3de95a192c144e9c21e6f5a29George Mountimport static junit.framework.TestCase.assertSame;
234034eaecff7e71f3de95a192c144e9c21e6f5a29George Mount
244034eaecff7e71f3de95a192c144e9c21e6f5a29George Mountimport static org.junit.Assert.assertTrue;
254034eaecff7e71f3de95a192c144e9c21e6f5a29George Mountimport static org.junit.Assert.fail;
264034eaecff7e71f3de95a192c144e9c21e6f5a29George Mount
27180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powellimport android.content.Context;
28180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powellimport android.os.Handler;
29180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powellimport android.os.Parcelable;
30180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powellimport android.support.test.filters.MediumTest;
31180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powellimport android.support.test.rule.ActivityTestRule;
32180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powellimport android.support.test.runner.AndroidJUnit4;
33180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powellimport android.util.ArrayMap;
344034eaecff7e71f3de95a192c144e9c21e6f5a29George Mount
35180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powellimport org.junit.Rule;
36180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powellimport org.junit.Test;
37180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powellimport org.junit.runner.RunWith;
38180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
39180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell@RunWith(AndroidJUnit4.class)
40180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powellpublic class LoaderLifecycleTest {
41180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell    @Rule
42180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell    public ActivityTestRule<EmptyActivity> mActivityRule =
43180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            new ActivityTestRule<>(EmptyActivity.class);
44180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell    @Test
45180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell    @MediumTest
46180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell    public void loaderIdentityTest() throws Throwable{
47180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell        mActivityRule.runOnUiThread(() -> {
48180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final Handler h = new Handler();
49180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final FragmentController fc1 = FragmentController.createController(
50180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell                    new TestFragmentHostCallback(mActivityRule.getActivity(), h, 0));
51180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
52180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.attachHost(null);
53180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.dispatchCreate();
54180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
55180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final FragmentManager fm1 = fc1.getFragmentManager();
56180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
57180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final Fragment f1 = new Fragment();
58180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fm1.beginTransaction().add(f1, "one").commitNow();
59180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
60180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            // Removing and re-adding a fragment completely will destroy its LoaderManager.
61180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            // Keep the first one here to confirm this later.
62180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final LoaderManager lm1 = f1.getLoaderManager();
63180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
64180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            // Remove the fragment, add a second one, and re-add the first to
65180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            // force its internal index to change. The tests below should still remain consistent.
66180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final Fragment f2 = new Fragment();
67180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fm1.beginTransaction().remove(f1).commitNow();
68180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fm1.beginTransaction().add(f2, "two").commitNow();
69180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fm1.beginTransaction().add(f1, "one").commitNow();
70180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
71180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            // We'll check this to see if we get the same instance back later
72180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            // as passed through NonConfigurationInstance. If the keys stay consistent
73180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            // across fragment remove/re-add, this will be consistent.
74180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final LoaderManager lm12 = f1.getLoaderManager();
75180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
76180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            assertNotSame("fully removed and re-added fragment got same LoaderManager", lm1, lm12);
77180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
78180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.dispatchActivityCreated();
79180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.noteStateNotSaved();
80180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.execPendingActions();
81180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.doLoaderStart();
82180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.dispatchStart();
83180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.reportLoaderStart();
84180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.dispatchResume();
85180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.execPendingActions();
86180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
87180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            // Bring the state back down to destroyed, simulating an activity restart
88180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.dispatchPause();
89180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final Parcelable savedState = fc1.saveAllState();
90180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.doLoaderStop(true);
91180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.dispatchStop();
92180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final FragmentManagerNonConfig nonconf = fc1.retainNestedNonConfig();
93180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
94180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final ArrayMap<String, LoaderManager> loaderNonConfig = fc1.retainLoaderNonConfig();
95180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            assertNotNull("loaderNonConfig was null", loaderNonConfig);
96180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
97180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.dispatchDestroy();
98180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
99180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            // Create the new controller and restore state
100180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final FragmentController fc2 = FragmentController.createController(
101180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell                    new TestFragmentHostCallback(mActivityRule.getActivity(), h, 0));
102180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
103180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final FragmentManager fm2 = fc2.getFragmentManager();
104180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
105180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.attachHost(null);
106100ba76d7a45847ffd3ecb1d468c91c0d0652032Adam Powell            // Make sure nothing blows up on a null here
107100ba76d7a45847ffd3ecb1d468c91c0d0652032Adam Powell            fc2.restoreLoaderNonConfig(null);
108100ba76d7a45847ffd3ecb1d468c91c0d0652032Adam Powell            // for real this time
109180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.restoreLoaderNonConfig(loaderNonConfig);
110180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.restoreAllState(savedState, nonconf);
111180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.dispatchCreate();
112180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
113180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
114180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.dispatchActivityCreated();
115180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.noteStateNotSaved();
116180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.execPendingActions();
117180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.doLoaderStart();
118180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.dispatchStart();
119180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.reportLoaderStart();
120180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.dispatchResume();
121180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.execPendingActions();
122180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
123180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            // Test that the fragments are in the configuration we expect
124180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final Fragment restoredOne = fm2.findFragmentByTag("one");
125180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final LoaderManager lm2 = restoredOne.getLoaderManager();
126180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
127180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            assertSame("didn't get same LoaderManager instance back", lm2, lm12);
128180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
129180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            // Bring the state back down to destroyed before we finish the test
130180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.dispatchPause();
131180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.saveAllState();
132180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.dispatchStop();
133180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.dispatchDestroy();
134180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell        });
135180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell    }
136180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
137180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell    @Test
138180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell    @MediumTest
139180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell    public void backStackLoaderIdentityTest() throws Throwable{
140180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell        mActivityRule.runOnUiThread(() -> {
141180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final Handler h = new Handler();
142180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final FragmentHostCallback host1 =
143180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell                    new TestFragmentHostCallback(mActivityRule.getActivity(), h, 0);
144180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final FragmentController fc1 = FragmentController.createController(host1);
145180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
146180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.attachHost(null);
147180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.dispatchCreate();
148180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
149180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final FragmentManager fm1 = fc1.getFragmentManager();
150180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
151180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final Fragment f1 = new Fragment();
152180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fm1.beginTransaction().add(f1, "one").commitNow();
153180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
154180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final LoaderManager lm1 = f1.getLoaderManager();
155180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
156180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            // Put the fragment on the back stack.
157180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fm1.beginTransaction().remove(f1).addToBackStack("backentry").commit();
158180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fm1.executePendingTransactions();
159180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
160180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.dispatchActivityCreated();
161180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.noteStateNotSaved();
162180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.execPendingActions();
163180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.doLoaderStart();
164180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.dispatchStart();
165180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.reportLoaderStart();
166180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.dispatchResume();
167180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.execPendingActions();
168180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
169180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            // Bring the state back down to destroyed, simulating an activity restart
170180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.dispatchPause();
171180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final Parcelable savedState = fc1.saveAllState();
172180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.doLoaderStop(true);
173180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.dispatchStop();
174180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final FragmentManagerNonConfig nonconf = fc1.retainNestedNonConfig();
175180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
176180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final ArrayMap<String, LoaderManager> loaderNonConfig = fc1.retainLoaderNonConfig();
177180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            assertNotNull("loaderNonConfig was null", loaderNonConfig);
178180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
179180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc1.dispatchDestroy();
180180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
181180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            // Create the new controller and restore state
182180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final FragmentHostCallback host2 =
183180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell                    new TestFragmentHostCallback(mActivityRule.getActivity(), h, 0);
184180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final FragmentController fc2 = FragmentController.createController(host2);
185180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
186180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final FragmentManager fm2 = fc2.getFragmentManager();
187180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
188180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.attachHost(null);
189180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.restoreLoaderNonConfig(loaderNonConfig);
190180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.restoreAllState(savedState, nonconf);
191180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.dispatchCreate();
192180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
193180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
194180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.dispatchActivityCreated();
195180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.noteStateNotSaved();
196180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.execPendingActions();
197180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.doLoaderStart();
198180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.dispatchStart();
199180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.reportLoaderStart();
200180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.dispatchResume();
201180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.execPendingActions();
202180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
203180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            assertNotSame("LoaderManager kept reference to old FragmentHostCallback",
204180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell                    host1, lm1.getFragmentHostCallback());
205180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            assertSame("LoaderManager did not refrence new FragmentHostCallback",
206180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell                    host2, lm1.getFragmentHostCallback());
207180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
208180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            // Test that the fragments are in the configuration we expect
209180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final Fragment restoredOne = fm2.findFragmentByTag("one");
2104034eaecff7e71f3de95a192c144e9c21e6f5a29George Mount            try {
2114034eaecff7e71f3de95a192c144e9c21e6f5a29George Mount                restoredOne.getLoaderManager();
2124034eaecff7e71f3de95a192c144e9c21e6f5a29George Mount                fail("A restored fragment on the back stack doesn't have a host, so it should "
2134034eaecff7e71f3de95a192c144e9c21e6f5a29George Mount                        + "throw an exception");
2144034eaecff7e71f3de95a192c144e9c21e6f5a29George Mount            } catch (IllegalStateException e) {
2154034eaecff7e71f3de95a192c144e9c21e6f5a29George Mount                // expected
2164034eaecff7e71f3de95a192c144e9c21e6f5a29George Mount            }
2174034eaecff7e71f3de95a192c144e9c21e6f5a29George Mount            fm2.popBackStackImmediate();
2184034eaecff7e71f3de95a192c144e9c21e6f5a29George Mount            // Now restoredOne should be added and should be in a good state.
2194034eaecff7e71f3de95a192c144e9c21e6f5a29George Mount            assertTrue(restoredOne.isAdded());
220180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            final LoaderManager lm2 = restoredOne.getLoaderManager();
221180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
222180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            assertSame("didn't get same LoaderManager instance back", lm2, lm1);
223180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
224180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            // Bring the state back down to destroyed before we finish the test
225180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.dispatchPause();
226180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.saveAllState();
227180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.dispatchStop();
228180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            fc2.dispatchDestroy();
229180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell        });
230180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell    }
231180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
232180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell    public class TestFragmentHostCallback extends FragmentHostCallback<LoaderLifecycleTest> {
233180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell        public TestFragmentHostCallback(Context context, Handler handler, int windowAnimations) {
234180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            super(context, handler, windowAnimations);
235180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell        }
236180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell
237180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell        @Override
238180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell        public LoaderLifecycleTest onGetHost() {
239180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell            return LoaderLifecycleTest.this;
240180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell        }
241180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell    }
242180202f2211d8c6bbb7a7057e61dafc90fe31930Adam Powell}
243