aboutsummaryrefslogtreecommitdiff
path: root/robolectric/src/test/java/org/robolectric/android/controller/ActivityControllerTest.java
blob: 09aec11c9ab5372777f9478d33950de357d69a55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
package org.robolectric.android.controller;

import static android.os.Build.VERSION_CODES.O_MR1;
import static android.os.Build.VERSION_CODES.P;
import static android.os.Build.VERSION_CODES.Q;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.robolectric.Shadows.shadowOf;
import static org.robolectric.annotation.LooperMode.Mode.LEGACY;
import static org.robolectric.shadows.ShadowLooper.shadowMainLooper;

import android.app.Activity;
import android.app.Fragment;
import android.app.WindowConfiguration;
import android.content.ComponentName;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.ContextThemeWrapper;
import android.view.ViewRootImpl;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.R;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.LooperMode;
import org.robolectric.shadows.ShadowLooper;
import org.robolectric.shadows.ShadowWindowManagerImpl;
import org.robolectric.util.Scheduler;
import org.robolectric.util.TestRunnable;

@RunWith(AndroidJUnit4.class)
public class ActivityControllerTest {
  private static final List<String> transcript = new ArrayList<>();
  private final ComponentName componentName =
      new ComponentName("org.robolectric", MyActivity.class.getName());
  private final ActivityController<MyActivity> controller =
      Robolectric.buildActivity(MyActivity.class);

  @Before
  public void setUp() throws Exception {
    transcript.clear();
  }

  @Test
  public void canCreateActivityNotListedInManifest() {
    Activity activity = Robolectric.setupActivity(Activity.class);
    assertThat(activity).isNotNull();
    assertThat(activity.getThemeResId()).isEqualTo(R.style.Theme_Robolectric);
  }

  public static class TestDelayedPostActivity extends Activity {
    TestRunnable r1 = new TestRunnable();
    TestRunnable r2 = new TestRunnable();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Handler h = new Handler();
      h.post(r1);
      h.postDelayed(r2, 60000);
    }
  }

  @Test
  @LooperMode(LEGACY)
  public void pendingTasks_areRunEagerly_whenActivityIsStarted_andSchedulerUnPaused() {
    final Scheduler s = Robolectric.getForegroundThreadScheduler();
    final long startTime = s.getCurrentTime();
    TestDelayedPostActivity activity = Robolectric.setupActivity(TestDelayedPostActivity.class);
    assertWithMessage("immediate task").that(activity.r1.wasRun).isTrue();
    assertWithMessage("currentTime").that(s.getCurrentTime()).isEqualTo(startTime);
  }

  @Test
  @LooperMode(LEGACY)
  public void delayedTasks_areNotRunEagerly_whenActivityIsStarted_andSchedulerUnPaused() {
    // Regression test for issue #1509
    final Scheduler s = Robolectric.getForegroundThreadScheduler();
    final long startTime = s.getCurrentTime();
    TestDelayedPostActivity activity = Robolectric.setupActivity(TestDelayedPostActivity.class);
    assertWithMessage("before flush").that(activity.r2.wasRun).isFalse();
    assertWithMessage("currentTime before flush").that(s.getCurrentTime()).isEqualTo(startTime);
    s.advanceToLastPostedRunnable();
    assertWithMessage("after flush").that(activity.r2.wasRun).isTrue();
    assertWithMessage("currentTime after flush")
        .that(s.getCurrentTime())
        .isEqualTo(startTime + 60000);
  }

  @Test
  public void shouldSetIntent() throws Exception {
    MyActivity myActivity = controller.create().get();
    assertThat(myActivity.getIntent()).isNotNull();
    assertThat(myActivity.getIntent().getComponent()).isEqualTo(componentName);
  }

  @Test
  public void shouldSetIntentComponentWithCustomIntentWithoutComponentSet() throws Exception {
    MyActivity myActivity =
        Robolectric.buildActivity(MyActivity.class, new Intent(Intent.ACTION_VIEW)).create().get();
    assertThat(myActivity.getIntent().getAction()).isEqualTo(Intent.ACTION_VIEW);
    assertThat(myActivity.getIntent().getComponent()).isEqualTo(componentName);
  }

  @Test
  public void shouldSetIntentForGivenActivityInstance() throws Exception {
    ActivityController<MyActivity> activityController =
        ActivityController.of(new MyActivity()).create();
    assertThat(activityController.get().getIntent()).isNotNull();
  }

  @Test
  @LooperMode(LEGACY)
  public void whenLooperIsNotPaused_shouldCreateWithMainLooperPaused() throws Exception {
    ShadowLooper.unPauseMainLooper();
    controller.create();
    assertThat(shadowOf(Looper.getMainLooper()).isPaused()).isFalse();
    assertThat(transcript).containsAtLeast("finishedOnCreate", "onCreate");
  }

  @Test
  public void whenLooperIsAlreadyPaused_shouldCreateWithMainLooperPaused() throws Exception {
    shadowMainLooper().pause();
    controller.create();
    assertThat(transcript).contains("finishedOnCreate");
    shadowMainLooper().idle();
    assertThat(transcript).contains("onCreate");
  }

  @Test
  public void visible_addsTheDecorViewToTheWindowManager() {
    controller.create().visible();
    assertThat(controller.get().getWindow().getDecorView().getParent().getClass())
        .isEqualTo(ViewRootImpl.class);
  }

  @Test
  public void start_callsPerformStartWhilePaused() {
    controller.create().start();
    assertThat(transcript).containsAtLeast("finishedOnStart", "onStart");
  }

  @Test
  public void stop_callsPerformStopWhilePaused() {
    controller.create().start().stop();
    assertThat(transcript).containsAtLeast("finishedOnStop", "onStop");
  }

  @Test
  public void restart_callsPerformRestartWhilePaused() {
    controller.create().start().stop().restart();
    assertThat(transcript).containsAtLeast("finishedOnRestart", "onRestart");
  }

  @Test
  public void pause_callsPerformPauseWhilePaused() {
    controller.create().pause();
    assertThat(transcript).containsAtLeast("finishedOnPause", "onPause");
  }

  @Test
  public void resume_callsPerformResumeWhilePaused() {
    controller.create().start().resume();
    assertThat(transcript).containsAtLeast("finishedOnResume", "onResume");
  }

  @Test
  public void destroy_callsPerformDestroyWhilePaused() {
    controller.create().destroy();
    assertThat(transcript).containsAtLeast("finishedOnDestroy", "onDestroy");
  }

  @Test
  public void destroy_cleansUpWindowManagerState() {
    WindowManager windowManager = controller.get().getWindowManager();
    ShadowWindowManagerImpl shadowWindowManager =
        ((ShadowWindowManagerImpl) shadowOf(windowManager));
    controller.create().start().resume().visible();
    assertThat(shadowWindowManager.getViews())
        .contains(controller.get().getWindow().getDecorView());
    controller.pause().stop().destroy();
    assertThat(shadowWindowManager.getViews())
        .doesNotContain(controller.get().getWindow().getDecorView());
  }

  @Test
  public void postCreate_callsOnPostCreateWhilePaused() {
    controller.create().postCreate(new Bundle());
    assertThat(transcript).containsAtLeast("finishedOnPostCreate", "onPostCreate");
  }

  @Test
  public void postResume_callsOnPostResumeWhilePaused() {
    controller.create().postResume();
    assertThat(transcript).containsAtLeast("finishedOnPostResume", "onPostResume");
  }

  @Test
  public void restoreInstanceState_callsPerformRestoreInstanceStateWhilePaused() {
    controller.create().restoreInstanceState(new Bundle());
    assertThat(transcript)
        .containsAtLeast("finishedOnRestoreInstanceState", "onRestoreInstanceState");
  }

  @Test
  public void newIntent_callsOnNewIntentWhilePaused() {
    controller.create().newIntent(new Intent(Intent.ACTION_VIEW));
    assertThat(transcript).containsAtLeast("finishedOnNewIntent", "onNewIntent");
  }

  @Test
  public void userLeaving_callsPerformUserLeavingWhilePaused() {
    controller.create().userLeaving();
    assertThat(transcript).containsAtLeast("finishedOnUserLeaveHint", "onUserLeaveHint");
  }

  @Test
  public void setup_callsLifecycleMethodsAndMakesVisible() {
    controller.setup();
    assertThat(transcript)
        .containsAtLeast("onCreate", "onStart", "onPostCreate", "onResume", "onPostResume");
    assertThat(controller.get().getWindow().getDecorView().getParent().getClass())
        .isEqualTo(ViewRootImpl.class);
  }

  @Test
  public void setupWithBundle_callsLifecycleMethodsAndMakesVisible() {
    controller.setup(new Bundle());
    assertThat(transcript)
        .containsAtLeast(
            "onCreate",
            "onStart",
            "onRestoreInstanceState",
            "onPostCreate",
            "onResume",
            "onPostResume");
    assertThat(controller.get().getWindow().getDecorView().getParent().getClass())
        .isEqualTo(ViewRootImpl.class);
  }

  @Test
  @Config(sdk = Build.VERSION_CODES.KITKAT)
  public void attach_shouldWorkWithAPI19() {
    MyActivity activity = Robolectric.buildActivity(MyActivity.class).create().get();
    assertThat(activity).isNotNull();
  }

  @Test
  public void configurationChange_callsLifecycleMethodsAndAppliesConfig() {
    Configuration config =
        new Configuration(
            ApplicationProvider.getApplicationContext().getResources().getConfiguration());
    final float newFontScale = config.fontScale *= 2;

    controller.setup();
    transcript.clear();
    controller.configurationChange(config);
    assertThat(transcript)
        .containsAtLeast(
            "onPause",
            "onStop",
            "onDestroy",
            "onCreate",
            "onStart",
            "onRestoreInstanceState",
            "onPostCreate",
            "onResume",
            "onPostResume");
    assertThat(controller.get().getResources().getConfiguration().fontScale)
        .isEqualTo(newFontScale);
  }

  @Test
  public void configurationChange_callsOnConfigurationChangedAndAppliesConfigWhenAllManaged() {
    Configuration config =
        new Configuration(
            ApplicationProvider.getApplicationContext().getResources().getConfiguration());
    final float newFontScale = config.fontScale *= 2;

    ActivityController<ConfigAwareActivity> configController =
        Robolectric.buildActivity(ConfigAwareActivity.class).setup();
    transcript.clear();
    configController.configurationChange(config);
    assertThat(transcript).containsAtLeast("onConfigurationChanged", "View.onConfigurationChanged");
    assertThat(configController.get().getResources().getConfiguration().fontScale)
        .isEqualTo(newFontScale);
  }

  @Config(minSdk = P)
  @Test
  public void configurationChange_windowConfigurationChanges_doesNotRecreateActivity() {
    Configuration config =
        new Configuration(
            ApplicationProvider.getApplicationContext().getResources().getConfiguration());
    WindowConfiguration windowConfiguration = config.windowConfiguration;
    windowConfiguration.setWindowingMode(WindowConfiguration.WINDOWING_MODE_FULLSCREEN);

    ActivityController<ConfigAwareActivity> controller =
        Robolectric.buildActivity(ConfigAwareActivity.class).setup();
    transcript.clear();
    controller.configurationChange(config);

    assertThat(transcript).containsAtLeast("onConfigurationChanged", "View.onConfigurationChanged");
    assertThat(
            controller
                .get()
                .getResources()
                .getConfiguration()
                .windowConfiguration
                .getWindowingMode())
        .isEqualTo(WindowConfiguration.WINDOWING_MODE_FULLSCREEN);
  }

  @Test
  @Config(maxSdk = O_MR1)
  public void configurationChange_callsLifecycleMethodsAndAppliesConfigWhenAnyNonManaged_beforeP() {
    configurationChange_callsLifecycleMethodsAndAppliesConfigWhenAnyNonManaged(
        "onSaveInstanceState", "onStop");
  }

  @Test
  @Config(minSdk = P)
  public void configurationChange_callsLifecycleMethodsAndAppliesConfigWhenAnyNonManaged_fromP() {
    configurationChange_callsLifecycleMethodsAndAppliesConfigWhenAnyNonManaged(
        "onStop", "onSaveInstanceState");
  }

  @Test
  @Config(qualifiers = "sw600dp")
  public void noArgsConfigurationChange_appliesChangedSystemConfiguration() {
    ActivityController<ConfigAwareActivity> configController =
        Robolectric.buildActivity(ConfigAwareActivity.class).setup();
    RuntimeEnvironment.setQualifiers("sw800dp");
    configController.configurationChange();
    assertThat(configController.get().newConfig.smallestScreenWidthDp).isEqualTo(800);
  }

  @Test
  @Config(qualifiers = "land")
  public void configurationChange_restoresTheme() {
    Configuration config =
        new Configuration(
            ApplicationProvider.getApplicationContext().getResources().getConfiguration());
    config.orientation = Configuration.ORIENTATION_PORTRAIT;

    controller.get().setTheme(android.R.style.Theme_Black);
    controller.setup();
    transcript.clear();
    controller.configurationChange(config);
    int restoredTheme = shadowOf((ContextThemeWrapper) controller.get()).callGetThemeResId();
    assertThat(restoredTheme).isEqualTo(android.R.style.Theme_Black);
  }

  @Test
  @Config(qualifiers = "land")
  public void configurationChange_reattachesRetainedFragments() {
    Configuration config =
        new Configuration(
            ApplicationProvider.getApplicationContext().getResources().getConfiguration());
    config.orientation = Configuration.ORIENTATION_PORTRAIT;

    ActivityController<NonConfigStateActivity> configController =
        Robolectric.buildActivity(NonConfigStateActivity.class).setup();
    NonConfigStateActivity activity = configController.get();
    Fragment retainedFragment = activity.retainedFragment;
    Fragment otherFragment = activity.nonRetainedFragment;
    configController.configurationChange(config);
    activity = configController.get();

    assertThat(activity.retainedFragment).isNotNull();
    assertThat(activity.retainedFragment).isSameInstanceAs(retainedFragment);
    assertThat(activity.nonRetainedFragment).isNotNull();
    assertThat(activity.nonRetainedFragment).isNotSameInstanceAs(otherFragment);
  }

  @Test
  public void isChangingConfiguration() {
    try (ActivityController<ConfigChangeActivity> controller =
        Robolectric.buildActivity(ConfigChangeActivity.class)) {

      controller.recreate();

      assertThat(transcript).containsExactly("onPause true", "onStop true", "onDestroy true");
    }
  }

  private static class ConfigChangeActivity extends Activity {

    @Override
    public void onPause() {
      super.onPause();
      transcript.add("onPause " + isChangingConfigurations());
    }

    @Override
    public void onStop() {
      super.onStop();
      transcript.add("onStop " + isChangingConfigurations());
    }

    @Override
    public void onDestroy() {
      super.onDestroy();
      transcript.add("onDestroy " + isChangingConfigurations());
    }
  }

  @Test
  public void windowFocusChanged() {
    controller.setup();
    assertThat(transcript).doesNotContain("finishedOnWindowFocusChanged");
    assertThat(controller.get().hasWindowFocus()).isFalse();

    transcript.clear();

    controller.windowFocusChanged(true);
    assertThat(transcript).containsExactly("finishedOnWindowFocusChanged");
    assertThat(controller.get().hasWindowFocus()).isTrue();
  }

  @Test
  @Config(minSdk = VERSION_CODES.Q)
  public void onTopActivityResumedCalledWithSetup() {
    controller.setup();
    assertThat(transcript).contains("finishedOnTopResumedActivityChanged");
  }

  @Test
  @Config(maxSdk = VERSION_CODES.P)
  public void onTopActivityResumedNotCalledWithSetupPreQ() {
    controller.setup();
    assertThat(transcript).doesNotContain("finishedOnTopResumedActivityChanged");
  }

  @Test
  public void close_transitionsActivityStateToDestroyed() {
    Robolectric.buildActivity(MyActivity.class).close();
    assertThat(transcript).isEmpty();
    transcript.clear();

    Robolectric.buildActivity(MyActivity.class).create().close();
    assertThat(transcript)
        .containsExactly("onCreate", "finishedOnCreate", "onDestroy", "finishedOnDestroy");
    transcript.clear();

    Robolectric.buildActivity(MyActivity.class).create().start().close();
    assertThat(transcript)
        .containsExactly(
            "onCreate",
            "finishedOnCreate",
            "onStart",
            "finishedOnStart",
            "onStop",
            "finishedOnStop",
            "onDestroy",
            "finishedOnDestroy");
    transcript.clear();

    Robolectric.buildActivity(MyActivity.class).setup().close();
    List<String> expectedStringList = new ArrayList<>();
    expectedStringList.add("onCreate");
    expectedStringList.add("finishedOnCreate");
    expectedStringList.add("onStart");
    expectedStringList.add("finishedOnStart");
    expectedStringList.add("onPostCreate");
    expectedStringList.add("finishedOnPostCreate");
    expectedStringList.add("onResume");
    expectedStringList.add("finishedOnResume");
    expectedStringList.add("onPostResume");
    expectedStringList.add("finishedOnPostResume");
    if (RuntimeEnvironment.getApiLevel() >= Q) {
      expectedStringList.add("finishedOnTopResumedActivityChanged");
    }
    expectedStringList.add("onPause");
    expectedStringList.add("finishedOnPause");
    expectedStringList.add("onStop");
    expectedStringList.add("finishedOnStop");
    expectedStringList.add("onDestroy");
    expectedStringList.add("finishedOnDestroy");
    assertThat(transcript).containsExactly(expectedStringList.toArray());
  }

  @Test
  public void close_tryWithResources_getsDestroyed() {
    try (ActivityController<MyActivity> ignored =
        Robolectric.buildActivity(MyActivity.class).setup()) {
      // no-op
    }
    List<String> expectedStringList = new ArrayList<>();
    expectedStringList.add("onCreate");
    expectedStringList.add("finishedOnCreate");
    expectedStringList.add("onStart");
    expectedStringList.add("finishedOnStart");
    expectedStringList.add("onPostCreate");
    expectedStringList.add("finishedOnPostCreate");
    expectedStringList.add("onResume");
    expectedStringList.add("finishedOnResume");
    expectedStringList.add("onPostResume");
    expectedStringList.add("finishedOnPostResume");
    if (RuntimeEnvironment.getApiLevel() >= Q) {
      expectedStringList.add("finishedOnTopResumedActivityChanged");
    }
    expectedStringList.add("onPause");
    expectedStringList.add("finishedOnPause");
    expectedStringList.add("onStop");
    expectedStringList.add("finishedOnStop");
    expectedStringList.add("onDestroy");
    expectedStringList.add("finishedOnDestroy");
    assertThat(transcript).containsExactly(expectedStringList.toArray());
  }

  private void configurationChange_callsLifecycleMethodsAndAppliesConfigWhenAnyNonManaged(
      String secondExpected, String thirdExpected) {
    Configuration config =
        new Configuration(
            ApplicationProvider.getApplicationContext().getResources().getConfiguration());
    final float newFontScale = config.fontScale *= 2;
    final int newOrientation = config.orientation = (config.orientation + 1) % 3;

    ActivityController<ConfigAwareActivity> configController =
        Robolectric.buildActivity(ConfigAwareActivity.class).setup();
    transcript.clear();
    configController.configurationChange(config);
    assertThat(transcript)
        .containsAtLeast(
            "onPause",
            secondExpected,
            thirdExpected,
            "onDestroy",
            "onCreate",
            "onStart",
            "onResume")
        .inOrder();
    assertThat(configController.get().getResources().getConfiguration().fontScale)
        .isEqualTo(newFontScale);
    assertThat(configController.get().getResources().getConfiguration().orientation)
        .isEqualTo(newOrientation);
  }

  public static class MyActivity extends Activity {
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
      transcribeWhilePaused("onRestoreInstanceState");
      transcript.add("finishedOnRestoreInstanceState");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      transcribeWhilePaused("onSaveInstanceState");
      transcript.add("finishedOnSaveInstanceState");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
      setContentView(
          new LinearLayout(this) {
            @Override
            protected void onConfigurationChanged(Configuration configuration) {
              super.onConfigurationChanged(configuration);
              transcribeWhilePaused("View.onConfigurationChanged");
            }
          });
      transcribeWhilePaused("onCreate");
      transcript.add("finishedOnCreate");
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
      super.onPostCreate(savedInstanceState);
      transcribeWhilePaused("onPostCreate");
      transcript.add("finishedOnPostCreate");
    }

    @Override
    protected void onPostResume() {
      super.onPostResume();
      transcribeWhilePaused("onPostResume");
      transcript.add("finishedOnPostResume");
    }

    @Override
    protected void onDestroy() {
      super.onDestroy();
      transcribeWhilePaused("onDestroy");
      transcript.add("finishedOnDestroy");
    }

    @Override
    protected void onStart() {
      super.onStart();
      transcribeWhilePaused("onStart");
      transcript.add("finishedOnStart");
    }

    @Override
    protected void onStop() {
      super.onStop();
      transcribeWhilePaused("onStop");
      transcript.add("finishedOnStop");
    }

    @Override
    protected void onResume() {
      super.onResume();
      transcribeWhilePaused("onResume");
      transcript.add("finishedOnResume");
    }

    @Override
    protected void onRestart() {
      super.onRestart();
      transcribeWhilePaused("onRestart");
      transcript.add("finishedOnRestart");
    }

    @Override
    protected void onPause() {
      super.onPause();
      transcribeWhilePaused("onPause");
      transcript.add("finishedOnPause");
    }

    @Override
    protected void onNewIntent(Intent intent) {
      super.onNewIntent(intent);
      transcribeWhilePaused("onNewIntent");
      transcript.add("finishedOnNewIntent");
    }

    @Override
    protected void onUserLeaveHint() {
      super.onUserLeaveHint();
      transcribeWhilePaused("onUserLeaveHint");
      transcript.add("finishedOnUserLeaveHint");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      transcribeWhilePaused("onConfigurationChanged");
      transcript.add("finishedOnConfigurationChanged");
    }

    @Override
    public void onWindowFocusChanged(boolean newFocus) {
      super.onWindowFocusChanged(newFocus);
      transcript.add("finishedOnWindowFocusChanged");
    }

    @Override
    public void onTopResumedActivityChanged(boolean isTopResumedActivity) {
      super.onTopResumedActivityChanged(isTopResumedActivity);
      transcript.add("finishedOnTopResumedActivityChanged");
    }

    private void transcribeWhilePaused(final String event) {
      runOnUiThread(() -> transcript.add(event));
    }
  }

  public static class ConfigAwareActivity extends MyActivity {

    Configuration newConfig;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      if (savedInstanceState != null) {
        assertThat(savedInstanceState.getSerializable("test")).isNotNull();
      }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      outState.putSerializable("test", new Exception());
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      this.newConfig = new Configuration(newConfig);
      super.onConfigurationChanged(newConfig);
    }
  }

  public static final class NonConfigStateActivity extends Activity {
    Fragment retainedFragment;
    Fragment nonRetainedFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      if (savedInstanceState == null) {
        retainedFragment = new Fragment();
        retainedFragment.setRetainInstance(true);
        nonRetainedFragment = new Fragment();
        getFragmentManager()
            .beginTransaction()
            .add(android.R.id.content, retainedFragment, "retained")
            .add(android.R.id.content, nonRetainedFragment, "non-retained")
            .commit();
      } else {
        retainedFragment = getFragmentManager().findFragmentByTag("retained");
        nonRetainedFragment = getFragmentManager().findFragmentByTag("non-retained");
      }
    }
  }
}