package com.example.dexopener;

import android.app.Activity;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.lifecycle.ActivityLifecycleCallback;
import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
import android.support.test.runner.lifecycle.Stage;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mockito.quality.Strictness;

import static org.mockito.Mockito.verify;

public class MainActivityTest implements ActivityLifecycleCallback {

    @Rule
    public final ActivityTestRule<MainActivity> activityRule =
            new ActivityTestRule<>(MainActivity.class, true, false);

    @Rule
    public final MockitoRule mockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);

    @Mock
    private MainActivityDelegate delegate;

    @Before
    public void setUp() throws Exception {
        ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback(this);
        activityRule.launchActivity(null);
    }

    @After
    public void tearDown() throws Exception {
        ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(this);
    }

    @Override
    public void onActivityLifecycleChanged(Activity activity, Stage stage) {
        if (activity instanceof MainActivity && stage == Stage.PRE_ON_CREATE) {
            ((MainActivity) activity).delegate = delegate;
        }
    }

    @Test
    public void onCreate_should_simply_call_MainActivityDelegate_onCreate() throws Exception {
        verify(delegate).onCreate(null);
    }

}

@@ -44,20 +44,22 @@
 oid.
-app.Activity
+content.Intent
 ;%0Aim
@@ -146,98 +146,35 @@
 ner.
-lifecycle.ActivityLifecycleCallback;%0Aimport android.support.test.runner.lifecyc
+intercepting.Sing
 le
-.
 Activity
 Life
@@ -173,135 +173,17 @@
 vity
-LifecycleMonitorRegistry;%0Aimport android.support.test.runner.lifecycle.Stage;%0A%0Aimport org.junit.After;%0Aimport org.junit.Before;
+Factory;%0A
 %0Aimp
@@ -229,33 +229,8 @@
 st;%0A
-import org.mockito.Mock;%0A
 impo
@@ -342,16 +342,56 @@
 tness;%0A%0A
+import static org.mockito.Mockito.mock;%0A
 import s
@@ -459,45 +459,8 @@
 est 
-implements ActivityLifecycleCallback 
 %7B%0A%0A 
@@ -570,271 +570,179 @@
 e%3C%3E(
-MainActivity.class, true, false);%0A%0A    @Rule%0A    public final MockitoRule mockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);%0A%0A    @Mock%0A    private MainActivityDelegate delegate;%0A%0A    @Before%0A    public void setUp() throws Exception %7B%0A
+new SingleActivityFactory%3CMainActivity%3E(MainActivity.class) %7B%0A                @Override%0A                protected MainActivity create(Intent intent) %7B%0A            
         
 Acti
@@ -733,32 +733,36 @@
             
+Main
 Activity
 LifecycleMon
@@ -753,520 +753,359 @@
 vity
-LifecycleMonitorRegistry.getInstance().addLifecycleCallback(this);%0A        activityRule.launchActivity(null
+ testTarget = new MainActivity();%0A                    testTarget.delegate = mock(MainActivityDelegate.class
 );%0A    
-%7D%0A%0A
     
-@After%0A    public void tearDown() throws Exception %7B%0A        ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(this)
+            return testTarget
 ;%0A    
-%7D%0A%0A
     
-@Override%0A    public void onActivityLifecycleChanged(Activity activity, Stage stage) %7B%0A        if (activity instanceof MainActivity && stage == Stage.PRE_ON_CREATE) %7B%0A            ((MainActivity) activity).delegate = delegate;%0A        %7D%0A    %7D
+        %7D%0A            %7D, /* initialTouchMode */ false, /* launchActivity */ false);%0A%0A    @Rule%0A    public final MockitoRule mockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
 %0A%0A  
@@ -1219,15 +1219,85 @@
     
-verify(
+activityRule.launchActivity(null);%0A        verify(activityRule.getActivity().
 dele

Refactor MainActivityTest to be more modern