ActivityTest.java revision f9fae237b421f257e8fcc2316d8fcfde81762537
133873d2b41a5cd2597b20a4e88eb8942944c0f23Tyler Schultzpackage com.xtremelabs.robolectric.shadows;
20add4ea006647573fc11fac463c87842f2dcc403Christian Williams
30add4ea006647573fc11fac463c87842f2dcc403Christian Williamsimport android.app.Activity;
485af7906879a68c99b1878dac22605fa32454f3fRich Humphreyimport android.app.Dialog;
50add4ea006647573fc11fac463c87842f2dcc403Christian Williamsimport android.appwidget.AppWidgetProvider;
64e38d69f092a879f1a8ba5cc8dce8526da47851aTyler Schultzimport android.content.Context;
7b832a2815d03667cbe1415bfcfd596292cb76f85Josh Lauer & Christian Williamsimport android.content.Intent;
80add4ea006647573fc11fac463c87842f2dcc403Christian Williamsimport android.content.IntentFilter;
94e38d69f092a879f1a8ba5cc8dce8526da47851aTyler Schultzimport android.content.SharedPreferences;
10c6526af59ebcc818c95680ac6f8efe83a0b65644adriancowhamimport android.content.pm.ActivityInfo;
11b832a2815d03667cbe1415bfcfd596292cb76f85Josh Lauer & Christian Williamsimport android.net.Uri;
1285af7906879a68c99b1878dac22605fa32454f3fRich Humphreyimport android.os.Bundle;
1305d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richardimport android.view.KeyEvent;
1485af7906879a68c99b1878dac22605fa32454f3fRich Humphreyimport android.view.View;
15297cc102ff285524a4109025ecd2c9c6ef051ce5Jan Berkelimport android.widget.FrameLayout;
16ce8adbf01d520e692a7eb6b72874c0182966127bPhil Goodwin & Tyler Schultzimport com.xtremelabs.robolectric.ApplicationResolver;
17c23ee9ca694000bd03b88d958bb84000e04f51e4Christian Williamsimport com.xtremelabs.robolectric.R;
180add4ea006647573fc11fac463c87842f2dcc403Christian Williamsimport com.xtremelabs.robolectric.Robolectric;
1907257335f88da163910cc0bd039e6163edc38580Gregg Van Hove & Phil Goodwinimport com.xtremelabs.robolectric.WithTestDefaultsRunner;
20a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultzimport com.xtremelabs.robolectric.util.TestRunnable;
21b832a2815d03667cbe1415bfcfd596292cb76f85Josh Lauer & Christian Williamsimport com.xtremelabs.robolectric.util.Transcript;
2285af7906879a68c99b1878dac22605fa32454f3fRich Humphreyimport org.hamcrest.CoreMatchers;
2385af7906879a68c99b1878dac22605fa32454f3fRich Humphreyimport org.junit.Test;
2485af7906879a68c99b1878dac22605fa32454f3fRich Humphreyimport org.junit.runner.RunWith;
2585af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
2685af7906879a68c99b1878dac22605fa32454f3fRich Humphreyimport java.util.concurrent.atomic.AtomicBoolean;
2785af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
2885af7906879a68c99b1878dac22605fa32454f3fRich Humphreyimport static com.xtremelabs.robolectric.Robolectric.shadowOf;
29297cc102ff285524a4109025ecd2c9c6ef051ce5Jan Berkelimport static com.xtremelabs.robolectric.util.TestUtil.assertInstanceOf;
3085af7906879a68c99b1878dac22605fa32454f3fRich Humphreyimport static com.xtremelabs.robolectric.util.TestUtil.newConfig;
314e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwinimport static org.hamcrest.CoreMatchers.*;
3285af7906879a68c99b1878dac22605fa32454f3fRich Humphreyimport static org.hamcrest.core.StringStartsWith.startsWith;
3385af7906879a68c99b1878dac22605fa32454f3fRich Humphreyimport static org.junit.Assert.*;
3485af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
3507257335f88da163910cc0bd039e6163edc38580Gregg Van Hove & Phil Goodwin@RunWith(WithTestDefaultsRunner.class)
360add4ea006647573fc11fac463c87842f2dcc403Christian Williamspublic class ActivityTest {
370add4ea006647573fc11fac463c87842f2dcc403Christian Williams    @Test(expected = IllegalStateException.class)
380add4ea006647573fc11fac463c87842f2dcc403Christian Williams    public void shouldComplainIfActivityIsDestroyedWithRegisteredBroadcastReceivers() throws Exception {
39256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        DialogLifeCycleActivity activity = new DialogLifeCycleActivity();
400add4ea006647573fc11fac463c87842f2dcc403Christian Williams        activity.registerReceiver(new AppWidgetProvider(), new IntentFilter());
410add4ea006647573fc11fac463c87842f2dcc403Christian Williams        activity.onDestroy();
420add4ea006647573fc11fac463c87842f2dcc403Christian Williams    }
430add4ea006647573fc11fac463c87842f2dcc403Christian Williams
440add4ea006647573fc11fac463c87842f2dcc403Christian Williams    @Test
450add4ea006647573fc11fac463c87842f2dcc403Christian Williams    public void shouldNotComplainIfActivityIsDestroyedWhileAnotherActivityHasRegisteredBroadcastReceivers() throws Exception {
46256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        DialogLifeCycleActivity activity = new DialogLifeCycleActivity();
470add4ea006647573fc11fac463c87842f2dcc403Christian Williams
48256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        DialogLifeCycleActivity activity2 = new DialogLifeCycleActivity();
490add4ea006647573fc11fac463c87842f2dcc403Christian Williams        activity2.registerReceiver(new AppWidgetProvider(), new IntentFilter());
500add4ea006647573fc11fac463c87842f2dcc403Christian Williams
510add4ea006647573fc11fac463c87842f2dcc403Christian Williams        activity.onDestroy(); // should not throw exception
520add4ea006647573fc11fac463c87842f2dcc403Christian Williams    }
530add4ea006647573fc11fac463c87842f2dcc403Christian Williams
54ce8adbf01d520e692a7eb6b72874c0182966127bPhil Goodwin & Tyler Schultz    @Test
5533cb58b49af57097ec8450f1e2e8daed530fa823Stefano Dacchille    public void shouldNotRegisterNullBroadcastReceiver() {
5633cb58b49af57097ec8450f1e2e8daed530fa823Stefano Dacchille        DialogLifeCycleActivity activity = new DialogLifeCycleActivity();
5733cb58b49af57097ec8450f1e2e8daed530fa823Stefano Dacchille        activity.registerReceiver(null, new IntentFilter());
5833cb58b49af57097ec8450f1e2e8daed530fa823Stefano Dacchille
5933cb58b49af57097ec8450f1e2e8daed530fa823Stefano Dacchille        activity.onDestroy();
6033cb58b49af57097ec8450f1e2e8daed530fa823Stefano Dacchille    }
6133cb58b49af57097ec8450f1e2e8daed530fa823Stefano Dacchille
6233cb58b49af57097ec8450f1e2e8daed530fa823Stefano Dacchille    @Test
63b832a2815d03667cbe1415bfcfd596292cb76f85Josh Lauer & Christian Williams    public void startActivityForResultAndReceiveResult_shouldSendResponsesBackToActivity() throws Exception {
64b832a2815d03667cbe1415bfcfd596292cb76f85Josh Lauer & Christian Williams        final Transcript transcript = new Transcript();
65b832a2815d03667cbe1415bfcfd596292cb76f85Josh Lauer & Christian Williams        Activity activity = new Activity() {
664e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin            @Override
674e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
68b832a2815d03667cbe1415bfcfd596292cb76f85Josh Lauer & Christian Williams                transcript.add("onActivityResult called with requestCode " + requestCode + ", resultCode " + resultCode + ", intent data " + data.getData());
69b832a2815d03667cbe1415bfcfd596292cb76f85Josh Lauer & Christian Williams            }
70b832a2815d03667cbe1415bfcfd596292cb76f85Josh Lauer & Christian Williams        };
71b832a2815d03667cbe1415bfcfd596292cb76f85Josh Lauer & Christian Williams        activity.startActivityForResult(new Intent().setType("audio/*"), 123);
72b832a2815d03667cbe1415bfcfd596292cb76f85Josh Lauer & Christian Williams        activity.startActivityForResult(new Intent().setType("image/*"), 456);
73b832a2815d03667cbe1415bfcfd596292cb76f85Josh Lauer & Christian Williams
74b832a2815d03667cbe1415bfcfd596292cb76f85Josh Lauer & Christian Williams        shadowOf(activity).receiveResult(new Intent().setType("image/*"), Activity.RESULT_OK,
754e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin                new Intent().setData(Uri.parse("content:foo")));
76b832a2815d03667cbe1415bfcfd596292cb76f85Josh Lauer & Christian Williams        transcript.assertEventsSoFar("onActivityResult called with requestCode 456, resultCode -1, intent data content:foo");
77b832a2815d03667cbe1415bfcfd596292cb76f85Josh Lauer & Christian Williams    }
78b832a2815d03667cbe1415bfcfd596292cb76f85Josh Lauer & Christian Williams
79b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams    @Test
80b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams    public void startActivityForResultAndReceiveResult_whenNoIntentMatches_shouldThrowException() throws Exception {
81b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams        Activity activity = new Activity() {
824e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin            @Override
834e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
84b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams                throw new IllegalStateException("should not be called");
85b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams            }
86b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams        };
87b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams        activity.startActivityForResult(new Intent().setType("audio/*"), 123);
88b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams        activity.startActivityForResult(new Intent().setType("image/*"), 456);
89b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams
90b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams        Intent requestIntent = new Intent().setType("video/*");
91b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams        try {
92b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams            shadowOf(activity).receiveResult(requestIntent, Activity.RESULT_OK,
93b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams                    new Intent().setData(Uri.parse("content:foo")));
94b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams            fail();
95b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams        } catch (Exception e) {
96b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams            assertThat(e.getMessage(), startsWith("No intent matches " + requestIntent));
97b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams        }
98b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams    }
99b76cabccd9f35b57fe486faf7d5763952c07c1a9Josh Lauer & Christian Williams
100ea74b95565e92521affdc7cc1a773e10fd235a96Josh Lauer & Christian Williams    @Test
101cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey    public void shouldSupportStartActivityForResult() throws Exception {
102256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        DialogLifeCycleActivity activity = new DialogLifeCycleActivity();
103cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey        ShadowActivity shadowActivity = Robolectric.shadowOf(activity);
104256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        Intent intent = new Intent().setClass(activity, DialogLifeCycleActivity.class);
105cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey        assertThat(shadowActivity.getNextStartedActivity(), nullValue());
10640c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
107cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey        activity.startActivityForResult(intent, 142);
10840c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
109cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey        Intent startedIntent = shadowActivity.getNextStartedActivity();
110cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey        assertThat(startedIntent, notNullValue());
111cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey        assertThat(startedIntent, sameInstance(intent));
112cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey    }
11340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
114cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey    @Test
115cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey    public void shouldSupportGetStartedActitivitesForResult() throws Exception {
116256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        DialogLifeCycleActivity activity = new DialogLifeCycleActivity();
117cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey        ShadowActivity shadowActivity = Robolectric.shadowOf(activity);
118256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        Intent intent = new Intent().setClass(activity, DialogLifeCycleActivity.class);
11940c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
120cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey        activity.startActivityForResult(intent, 142);
12140c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
122cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey        ShadowActivity.IntentForResult intentForResult = shadowActivity.getNextStartedActivityForResult();
123cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey        assertThat(intentForResult, notNullValue());
12440c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        assertThat(shadowActivity.getNextStartedActivityForResult(), nullValue());
12540c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        assertThat(intentForResult.intent, notNullValue());
12640c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        assertThat(intentForResult.intent, sameInstance(intent));
12740c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        assertThat(intentForResult.requestCode, equalTo(142));
128cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey    }
12940c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
130cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey    @Test
131cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey    public void shouldSupportPeekStartedActitivitesForResult() throws Exception {
132256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        DialogLifeCycleActivity activity = new DialogLifeCycleActivity();
133cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey        ShadowActivity shadowActivity = Robolectric.shadowOf(activity);
134256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        Intent intent = new Intent().setClass(activity, DialogLifeCycleActivity.class);
135cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey
136cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey        activity.startActivityForResult(intent, 142);
13740c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
138cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey        ShadowActivity.IntentForResult intentForResult = shadowActivity.peekNextStartedActivityForResult();
139cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey        assertThat(intentForResult, notNullValue());
14040c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        assertThat(shadowActivity.peekNextStartedActivityForResult(), sameInstance(intentForResult));
14140c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        assertThat(intentForResult.intent, notNullValue());
14240c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        assertThat(intentForResult.intent, sameInstance(intent));
14340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        assertThat(intentForResult.requestCode, equalTo(142));
144cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey    }
14540c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
146cf37f66c9bcbf0b25b879fbb78c9442c9101c7acRich Humphrey    @Test
147c23ee9ca694000bd03b88d958bb84000e04f51e4Christian Williams    public void onContentChangedShouldBeCalledAfterContentViewIsSet() throws RuntimeException {
148c23ee9ca694000bd03b88d958bb84000e04f51e4Christian Williams        final Transcript transcript = new Transcript();
149c23ee9ca694000bd03b88d958bb84000e04f51e4Christian Williams        Activity customActivity = new Activity() {
150c23ee9ca694000bd03b88d958bb84000e04f51e4Christian Williams            @Override
151c23ee9ca694000bd03b88d958bb84000e04f51e4Christian Williams            public void onContentChanged() {
152c23ee9ca694000bd03b88d958bb84000e04f51e4Christian Williams                transcript.add("onContentChanged was called; title is \"" + shadowOf(findViewById(R.id.title)).innerText() + "\"");
153c23ee9ca694000bd03b88d958bb84000e04f51e4Christian Williams            }
154c23ee9ca694000bd03b88d958bb84000e04f51e4Christian Williams        };
155c23ee9ca694000bd03b88d958bb84000e04f51e4Christian Williams        customActivity.setContentView(R.layout.main);
156c23ee9ca694000bd03b88d958bb84000e04f51e4Christian Williams        transcript.assertEventsSoFar("onContentChanged was called; title is \"Main Layout\"");
157c23ee9ca694000bd03b88d958bb84000e04f51e4Christian Williams    }
158c23ee9ca694000bd03b88d958bb84000e04f51e4Christian Williams
159c23ee9ca694000bd03b88d958bb84000e04f51e4Christian Williams    @Test
160ce8adbf01d520e692a7eb6b72874c0182966127bPhil Goodwin & Tyler Schultz    public void shouldRetrievePackageNameFromTheManifest() throws Exception {
161147cdda64f4a372a27599ae1f902bd7be90bee5cPhil Goodwin        Robolectric.application = new ApplicationResolver(newConfig("TestAndroidManifestWithPackageName.xml")).resolveApplication();
16285af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        assertThat("com.wacka.wa", equalTo(new Activity().getPackageName()));
163ce8adbf01d520e692a7eb6b72874c0182966127bPhil Goodwin & Tyler Schultz    }
164ce8adbf01d520e692a7eb6b72874c0182966127bPhil Goodwin & Tyler Schultz
165a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz    @Test
166a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz    public void shouldRunUiTasksImmediatelyByDefault() throws Exception {
167a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz        TestRunnable runnable = new TestRunnable();
168256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        DialogLifeCycleActivity activity = new DialogLifeCycleActivity();
169a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz        activity.runOnUiThread(runnable);
170a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz        assertTrue(runnable.wasRun);
171a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz    }
172a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz
173a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz    @Test
174a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz    public void shouldQueueUiTasksWhenUiThreadIsPaused() throws Exception {
175a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz        Robolectric.pauseMainLooper();
176a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz
177256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        DialogLifeCycleActivity activity = new DialogLifeCycleActivity();
178a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz        TestRunnable runnable = new TestRunnable();
179a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz        activity.runOnUiThread(runnable);
180a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz        assertFalse(runnable.wasRun);
181a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz
182a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz        Robolectric.unPauseMainLooper();
183a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz        assertTrue(runnable.wasRun);
184a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz    }
18585af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
18688a7b3c27c736843e69e6ec12c1d7c7397c14d1cRich Humphrey    @Test
18785af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    public void showDialog_shouldCreatePrepareAndShowDialog() {
188256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        final DialogLifeCycleActivity activity = new DialogLifeCycleActivity();
18985af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        final AtomicBoolean dialogWasShown = new AtomicBoolean(false);
19085af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
19185af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        new Dialog(activity) {
1924e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin            {
1934e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin                activity.dialog = this;
1944e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin            }
19585af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
19685af7906879a68c99b1878dac22605fa32454f3fRich Humphrey            @Override
19785af7906879a68c99b1878dac22605fa32454f3fRich Humphrey            public void show() {
19885af7906879a68c99b1878dac22605fa32454f3fRich Humphrey                dialogWasShown.set(true);
19985af7906879a68c99b1878dac22605fa32454f3fRich Humphrey            }
20085af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        };
20185af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
202256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        activity.showDialog(1);
20385af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
20485af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        assertTrue(activity.createdDialog);
20585af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        assertTrue(activity.preparedDialog);
20685af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        assertTrue(dialogWasShown.get());
2074c5a8c640b626c2b53b0c168d8db6d2930e270fdPhil Goodwin & Rick Kawala    }
2084e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin
20905fe24178737f40b46d4033578ef1ce6b5dfa43bPhil Goodwin & Rick Kawala    @Test
21085af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    public void showDialog_shouldCreatePrepareAndShowDialogWithBundle() {
211256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        final DialogLifeCycleActivity activity = new DialogLifeCycleActivity();
21285af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        final AtomicBoolean dialogWasShown = new AtomicBoolean(false);
21385af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
21485af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        new Dialog(activity) {
2154e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin            {
2164e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin                activity.dialog = this;
2174e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin            }
21885af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
21985af7906879a68c99b1878dac22605fa32454f3fRich Humphrey            @Override
22085af7906879a68c99b1878dac22605fa32454f3fRich Humphrey            public void show() {
22185af7906879a68c99b1878dac22605fa32454f3fRich Humphrey                dialogWasShown.set(true);
22285af7906879a68c99b1878dac22605fa32454f3fRich Humphrey            }
22385af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        };
22485af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
225256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        activity.showDialog(1, new Bundle());
22685af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
22785af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        assertTrue(activity.createdDialog);
22885af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        assertTrue(activity.preparedDialogWithBundle);
22985af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        assertTrue(dialogWasShown.get());
23085af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    }
23185af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
23285af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    @Test
233256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante    public void showDialog_shouldReuseDialogs() {
234256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        final DialogCreatingActivity activity = new DialogCreatingActivity();
235256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante
236256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        activity.showDialog(1);
237256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante
2385117035453920f3d4d0f3969bdab0305dcdb94baJoe Moore & Ryan Richard        Dialog firstDialog = ShadowDialog.getLatestDialog();
239256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante
240256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        activity.showDialog(1);
241256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante
2425117035453920f3d4d0f3969bdab0305dcdb94baJoe Moore & Ryan Richard        final Dialog secondDialog = ShadowDialog.getLatestDialog();
243256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante
244256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        assertSame("dialogs should be the same instance", firstDialog, secondDialog);
245256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante    }
246256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante
2472dd02a943021d7a8c0784854374c2fb3d1e5f1a6Matthias Kiefer    @Test
248943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb    public void showDialog_shouldShowDialog() throws Exception {
249943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb        final DialogCreatingActivity activity = new DialogCreatingActivity();
250943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb        activity.showDialog(1);
251943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb        Dialog dialog = ShadowDialog.getLatestDialog();
252943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb        assertTrue(dialog.isShowing());
253943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb    }
254943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb
255943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb    @Test
256943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb    public void dismissDialog_shouldDismissPreviouslyShownDialog() throws Exception {
257943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb        final DialogCreatingActivity activity = new DialogCreatingActivity();
258943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb        activity.showDialog(1);
259943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb        activity.dismissDialog(1);
260943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb        Dialog dialog = ShadowDialog.getLatestDialog();
261943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb        assertFalse(dialog.isShowing());
262943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb    }
263943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb
264943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb    @Test(expected = IllegalArgumentException.class)
265943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb    public void dismissDialog_shouldThrowExceptionIfDialogWasNotPreviouslyShown() throws Exception {
266943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb        final DialogCreatingActivity activity = new DialogCreatingActivity();
267943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb        activity.dismissDialog(1);
268943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb    }
269943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb
270943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb    @Test
2712dd02a943021d7a8c0784854374c2fb3d1e5f1a6Matthias Kiefer    public void removeDialog_shouldCreateDialogAgain() {
2722dd02a943021d7a8c0784854374c2fb3d1e5f1a6Matthias Kiefer        final DialogCreatingActivity activity = new DialogCreatingActivity();
2734e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin
2742dd02a943021d7a8c0784854374c2fb3d1e5f1a6Matthias Kiefer        activity.showDialog(1);
2752dd02a943021d7a8c0784854374c2fb3d1e5f1a6Matthias Kiefer        Dialog firstDialog = ShadowDialog.getLatestDialog();
2764e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin
2772dd02a943021d7a8c0784854374c2fb3d1e5f1a6Matthias Kiefer        activity.removeDialog(1);
2782dd02a943021d7a8c0784854374c2fb3d1e5f1a6Matthias Kiefer        assertNull(Robolectric.shadowOf(activity).getDialogById(1));
2792dd02a943021d7a8c0784854374c2fb3d1e5f1a6Matthias Kiefer
2802dd02a943021d7a8c0784854374c2fb3d1e5f1a6Matthias Kiefer        activity.showDialog(1);
2812dd02a943021d7a8c0784854374c2fb3d1e5f1a6Matthias Kiefer        Dialog secondDialog = ShadowDialog.getLatestDialog();
2824e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin
2832dd02a943021d7a8c0784854374c2fb3d1e5f1a6Matthias Kiefer        assertNotSame("dialogs should not be the same instance", firstDialog, secondDialog);
284943730659b697ae049fe203b2a51d75113c49cb5Chuck Greb    }
285256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante
286256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante    @Test
28785af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    public void shouldCallOnCreateDialogFromShowDialog() {
28885af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        ActivityWithOnCreateDialog activity = new ActivityWithOnCreateDialog();
28985af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        activity.showDialog(123);
29085af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        assertTrue(activity.onCreateDialogWasCalled);
29185af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        assertThat(ShadowDialog.getLatestDialog(), CoreMatchers.<Object>notNullValue());
29285af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    }
29385af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
29485af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    @Test
29585af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    public void shouldCallFinishInOnBackPressed() {
29685af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        Activity activity = new Activity();
29785af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        activity.onBackPressed();
29885af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
29988a7b3c27c736843e69e6ec12c1d7c7397c14d1cRich Humphrey        ShadowActivity shadowActivity = shadowOf(activity);
30085af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        assertTrue(shadowActivity.isFinishing());
30185af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    }
30285af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
3034e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin    @Test
30485af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    public void shouldSupportCurrentFocus() {
305256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        DialogLifeCycleActivity activity = new DialogLifeCycleActivity();
30685af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        ShadowActivity shadow = shadowOf(activity);
30788a7b3c27c736843e69e6ec12c1d7c7397c14d1cRich Humphrey
30885af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        assertNull(shadow.getCurrentFocus());
30985af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        View view = new View(activity);
31085af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        shadow.setCurrentFocus(view);
31185af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        assertEquals(view, shadow.getCurrentFocus());
31288a7b3c27c736843e69e6ec12c1d7c7397c14d1cRich Humphrey    }
313a4f7d50f8e6f12d541421383544fe16c185e4bbcPhil Goodwin & Tyler Schultz
3144e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin    @Test
315c6526af59ebcc818c95680ac6f8efe83a0b65644adriancowham    public void shouldSetOrientation() {
3167f306c7c98e32f85115ce57780a6752477b8faadadriancowham        DialogLifeCycleActivity activity = new DialogLifeCycleActivity();
3174e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
3184e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin        assertThat(activity.getRequestedOrientation(), equalTo(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT));
319c6526af59ebcc818c95680ac6f8efe83a0b65644adriancowham    }
3204e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin
321f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker    @Test
322f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker    public void retrieveIdOfResource() {
323f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker        Activity activity = new Activity();
324f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker
325f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker        int id1 = R.string.hello;
326f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker        String string = activity.getString(id1);
327f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker        assertEquals("Hello", string);
328f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker
329f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker        int id = activity.getResources().getIdentifier("hello", "string", "com.xtremelabs.robolectric");
330f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker        assertTrue(id > 0);
3314e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin
332f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker        String hello = activity.getResources().getString(id);
333f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker        assertEquals("Hello", hello);
334f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker    }
335f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker
336f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker    @Test
337f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker    public void retrieveIdOfNonExistingResource() {
338f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker        Activity activity = new Activity();
339f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker
340f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker        int id = activity.getResources().getIdentifier("just_alot_of_crap", "string", "com.xtremelabs.robolectric");
341f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker        assertTrue(id == 0);
342f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker    }
343f320a2d3b8075716daf2a80f5a8f7890086fb609Mark Bakker
34405d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard    @Test
345297cc102ff285524a4109025ecd2c9c6ef051ce5Jan Berkel    public void shouldSetContentViewWithFrameLayoutAsParent() throws Exception {
346297cc102ff285524a4109025ecd2c9c6ef051ce5Jan Berkel        Activity activity = new Activity();
347297cc102ff285524a4109025ecd2c9c6ef051ce5Jan Berkel        activity.setContentView(R.layout.toplevel_merge);
348297cc102ff285524a4109025ecd2c9c6ef051ce5Jan Berkel
349297cc102ff285524a4109025ecd2c9c6ef051ce5Jan Berkel        View contentView = shadowOf(activity).getContentView();
350297cc102ff285524a4109025ecd2c9c6ef051ce5Jan Berkel        assertInstanceOf(FrameLayout.class, contentView);
3514e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin        assertThat(((FrameLayout) contentView).getChildCount(), equalTo(2));
352297cc102ff285524a4109025ecd2c9c6ef051ce5Jan Berkel    }
3534e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin
354f14c3931ebac66beb7bf03f5d08fdd8895f02157Phil Goodwin & Ryan Richard    @Test
35505d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard    public void onKeyUp_recordsThatItWasCalled() throws Exception {
35605d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard        Activity activity = new Activity();
35705d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard        boolean consumed = activity.onKeyUp(KeyEvent.KEYCODE_0, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_0));
35805d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard
35905d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard        assertFalse(consumed);
36005d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard        assertTrue(shadowOf(activity).onKeyUpWasCalled());
36105d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard
36205d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard        shadowOf(activity).resetKeyUpWasCalled();
36305d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard        assertFalse(shadowOf(activity).onKeyUpWasCalled());
36405d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard    }
3654e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin
36605d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard    @Test
36705d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard    public void onKeyUp_callsOnBackPressedWhichFinishesTheActivity() throws Exception {
36805d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard        Activity activity = new Activity();
36905d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard        boolean consumed = activity.onKeyUp(KeyEvent.KEYCODE_BACK, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
37005d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard
37105d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard        assertTrue(consumed);
37205d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard        assertTrue(shadowOf(activity).onKeyUpWasCalled());
37305d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard        assertTrue(activity.isFinishing());
37405d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard    }
3754e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin
37696bd436ce2e84a30747574ce2b7e4ef5e61bc3d0Valtteri Virtanen    @Test
37796bd436ce2e84a30747574ce2b7e4ef5e61bc3d0Valtteri Virtanen    public void shouldGiveSharedPreferences() throws Exception {
3784e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin        Activity activity = new Activity();
3794e38d69f092a879f1a8ba5cc8dce8526da47851aTyler Schultz        SharedPreferences preferences = activity.getPreferences(Context.MODE_PRIVATE);
3804e38d69f092a879f1a8ba5cc8dce8526da47851aTyler Schultz        assertNotNull(preferences);
3814e38d69f092a879f1a8ba5cc8dce8526da47851aTyler Schultz        preferences.edit().putString("foo", "bar").commit();
3824e38d69f092a879f1a8ba5cc8dce8526da47851aTyler Schultz        assertThat(activity.getPreferences(Context.MODE_PRIVATE).getString("foo", null), equalTo("bar"));
38396bd436ce2e84a30747574ce2b7e4ef5e61bc3d0Valtteri Virtanen    }
38405d5fa490c39eae46684f666a66acb8edf42661cPhil Goodwin & Ryan Richard
3854e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin    @Test
3864e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin    public void shouldFindContentViewContainer() throws Exception {
3874e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin        Activity activity = new Activity();
3884e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin        View contentView = new View(activity);
3894e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin        activity.setContentView(contentView);
3904e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin
3914e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin        FrameLayout contentViewContainer = (FrameLayout) activity.findViewById(android.R.id.content);
3924e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin        assertThat(contentViewContainer.getChildAt(0), is(contentView));
3934e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin    }
3944e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin
395521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen    @Test
396f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson      public void createGoesThroughFullLifeCycle() throws Exception {
397f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson        TestActivity activity = new TestActivity();
398f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson
399f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson        shadowOf(activity).create();
400f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson
401f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson        activity.transcript.assertEventsSoFar(
402f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson                "onCreate",
403f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson                "onStart",
404f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson                "onPostCreate",
405f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson                "onResume"
406f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson        );
407f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson    }
408f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson
409f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson
410f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson    @Test
411521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen    public void recreateGoesThroughFullLifeCycle() throws Exception {
412521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        TestActivity activity = new TestActivity();
413521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen
414521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        ShadowActivity shadow = shadowOf(activity);
415521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        shadow.recreate();
416521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen
417521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        activity.transcript.assertEventsSoFar(
418521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen                "onSaveInstanceState",
419521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen                "onPause",
420521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen                "onStop",
421521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen                "onRetainNonConfigurationInstance",
422521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen                "onDestroy",
423521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen                "onCreate",
424521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen                "onStart",
425521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen                "onRestoreInstanceState",
426521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen                "onResume"
427521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        );
428521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen
429521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        Integer storedValue = (Integer) activity.getLastNonConfigurationInstance();
430521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        assertEquals(5, storedValue.intValue());
431521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen    }
432521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen
433521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen    private static class TestActivity extends Activity {
434521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        Transcript transcript = new Transcript();
435521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen
436f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson        private boolean isRecreating = false;
437f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson
438521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        @Override
439521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        public void onSaveInstanceState(Bundle outState) {
440f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson            isRecreating = true;
441521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            transcript.add("onSaveInstanceState");
442521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            outState.putString("TestActivityKey", "TestActivityValue");
443521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            super.onSaveInstanceState(outState);
444521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        }
445521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen
446521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        @Override
447521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        public void onRestoreInstanceState(Bundle savedInstanceState) {
448521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            transcript.add("onRestoreInstanceState");
449521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            assertTrue(savedInstanceState.containsKey("TestActivityKey"));
450521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            assertEquals("TestActivityValue", savedInstanceState.getString("TestActivityKey"));
451521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            super.onRestoreInstanceState(savedInstanceState);
452521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        }
453521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen
454521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        @Override
455521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        public Object onRetainNonConfigurationInstance() {
456521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            transcript.add("onRetainNonConfigurationInstance");
457521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            return new Integer(5);
458521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        }
459521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen
460521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        @Override
461521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        public void onPause() {
462521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            transcript.add("onPause");
463521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            super.onPause();
464521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        }
465521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen
466521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        @Override
467521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        public void onDestroy() {
468521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            transcript.add("onDestroy");
469521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            super.onDestroy();
470521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        }
471521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen
472521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        @Override
473521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        public void onCreate(Bundle savedInstanceState) {
474521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            transcript.add("onCreate");
475f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson
476f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson            if( isRecreating ) {
477f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson                assertTrue(savedInstanceState.containsKey("TestActivityKey"));
478f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson                assertEquals("TestActivityValue", savedInstanceState.getString("TestActivityKey"));
479f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson            }
480f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson
481521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            super.onCreate(savedInstanceState);
482521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        }
483521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen
484521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        @Override
485521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        public void onStart() {
486521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            transcript.add("onStart");
487521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            super.onStart();
488521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        }
489521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen
490521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        @Override
491f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson        public void onPostCreate(Bundle savedInstanceState) {
492f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson            transcript.add("onPostCreate");
493f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson            super.onPostCreate(savedInstanceState);
494f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson        }
495f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson
496f9fae237b421f257e8fcc2316d8fcfde81762537Levi Wilson        @Override
497521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        public void onStop() {
498521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            transcript.add("onStop");
499521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            super.onStop();
500521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        }
501521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen
502521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        @Override
503521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        public void onResume() {
504521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            transcript.add("onResume");
505521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen            super.onResume();
506521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen        }
507521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen    }
508521cabb44ef11199544abf17cfc1b7fb84019914Jon Boekenoogen
509256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante    private static class DialogCreatingActivity extends Activity {
510256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        @Override
511256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        protected Dialog onCreateDialog(int id) {
512256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante            return new Dialog(this);
513256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante        }
514256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante    }
515c6526af59ebcc818c95680ac6f8efe83a0b65644adriancowham
516256949ba5562873047aecf1bf47533e5ae1cd933Phil Goodwin & Phil Plante    private static class DialogLifeCycleActivity extends Activity {
51785af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        public boolean createdDialog = false;
51885af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        public boolean preparedDialog = false;
51985af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        public boolean preparedDialogWithBundle = false;
52085af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        public Dialog dialog = null;
52185af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
5224e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin        @Override
5234e3b6432e36c8d7e95cee1066bd71a511f7c7b88Chris Perry & Phil Goodwin        protected void onDestroy() {
5240add4ea006647573fc11fac463c87842f2dcc403Christian Williams            super.onDestroy();
5250add4ea006647573fc11fac463c87842f2dcc403Christian Williams        }
52685af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
52785af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        @Override
52885af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        protected Dialog onCreateDialog(int id) {
52985af7906879a68c99b1878dac22605fa32454f3fRich Humphrey            createdDialog = true;
53085af7906879a68c99b1878dac22605fa32454f3fRich Humphrey            return dialog;
53185af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        }
53285af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
53385af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        @Override
53485af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        protected void onPrepareDialog(int id, Dialog dialog) {
53585af7906879a68c99b1878dac22605fa32454f3fRich Humphrey            preparedDialog = true;
53685af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        }
53785af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
53885af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        @Override
53985af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
54085af7906879a68c99b1878dac22605fa32454f3fRich Humphrey            preparedDialogWithBundle = true;
54185af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        }
54285af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    }
54385af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
54485af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    private static class ActivityWithOnCreateDialog extends Activity {
54585af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        boolean onCreateDialogWasCalled = false;
54685af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
54785af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        @Override
54885af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        protected Dialog onCreateDialog(int id) {
54985af7906879a68c99b1878dac22605fa32454f3fRich Humphrey            onCreateDialogWasCalled = true;
55085af7906879a68c99b1878dac22605fa32454f3fRich Humphrey            return new Dialog(null);
55185af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        }
5520add4ea006647573fc11fac463c87842f2dcc403Christian Williams    }
5530add4ea006647573fc11fac463c87842f2dcc403Christian Williams}
554