1 package org.robolectric.integrationtests.axt; 2 3 import static com.google.common.truth.Truth.assertThat; 4 import static org.junit.Assert.fail; 5 6 import android.app.Activity; 7 import android.content.Intent; 8 import android.os.Bundle; 9 import androidx.test.ext.junit.runners.AndroidJUnit4; 10 import androidx.test.rule.ActivityTestRule; 11 import java.util.ArrayList; 12 import java.util.List; 13 import org.junit.Before; 14 import org.junit.Ignore; 15 import org.junit.Rule; 16 import org.junit.Test; 17 import org.junit.runner.RunWith; 18 19 /** Integration tests for {@link ActivityTestRule} that verify it behaves consistently on device and 20 * Robolectric. */ 21 @RunWith(AndroidJUnit4.class) 22 public class ActivityTestRuleTest { 23 24 private static final List<String> callbacks = new ArrayList<>(); 25 26 @Rule 27 public ActivityTestRule<TranscriptActivity> rule = 28 new ActivityTestRule<TranscriptActivity>(TranscriptActivity.class, false, false) { 29 @Override 30 protected void beforeActivityLaunched() { 31 super.beforeActivityLaunched(); 32 callbacks.add("beforeActivityLaunched"); 33 } 34 35 @Override 36 protected void afterActivityLaunched() { 37 callbacks.add("afterActivityLaunched"); 38 super.afterActivityLaunched(); 39 } 40 41 @Override 42 protected void afterActivityFinished() { 43 callbacks.add("afterActivityFinished"); 44 super.afterActivityFinished(); 45 } 46 }; 47 48 public static class TranscriptActivity extends Activity { 49 Bundle receivedBundle; 50 51 @Override onCreate(Bundle savedInstanceState)52 public void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 receivedBundle = savedInstanceState; 55 callbacks.add("onCreate"); 56 } 57 58 @Override onStart()59 public void onStart() { 60 super.onStart(); 61 callbacks.add("onStart"); 62 } 63 64 @Override onResume()65 public void onResume() { 66 super.onResume(); 67 callbacks.add("onResume"); 68 } 69 70 @Override onPause()71 public void onPause() { 72 super.onPause(); 73 callbacks.add("onPause"); 74 } 75 76 @Override onStop()77 public void onStop() { 78 super.onStop(); 79 callbacks.add("onStop"); 80 } 81 82 @Override onRestart()83 public void onRestart() { 84 super.onRestart(); 85 callbacks.add("onRestart"); 86 } 87 88 @Override onDestroy()89 public void onDestroy() { 90 super.onDestroy(); 91 callbacks.add("onDestroy"); 92 } 93 } 94 95 @Before setUp()96 public void setUp() { 97 callbacks.clear(); 98 } 99 100 @Test launchActivity_callbackSequence()101 public void launchActivity_callbackSequence() { 102 TranscriptActivity activity = rule.launchActivity(null); 103 assertThat(activity).isNotNull(); 104 assertThat(callbacks) 105 .containsExactly( 106 "beforeActivityLaunched", "onCreate", "onStart", "onResume", "afterActivityLaunched"); 107 } 108 109 /** 110 * Starting an activity with options is currently not supported, so check that received bundle is 111 * always null in both modes. 112 */ 113 @Test launchActivity_bundle()114 public void launchActivity_bundle() { 115 TranscriptActivity activity = rule.launchActivity(null); 116 assertThat(activity.receivedBundle).isNull(); 117 } 118 launchActivity_intentExtras()119 @Test public void launchActivity_intentExtras() { 120 Intent intent = new Intent(); 121 intent.putExtra("Key", "Value"); 122 123 TranscriptActivity activity = rule.launchActivity(intent); 124 125 Intent activityIntent = activity.getIntent(); 126 assertThat(activityIntent.getExtras()).isNotNull(); 127 assertThat(activityIntent.getStringExtra("Key")).isEqualTo("Value"); 128 } 129 130 @Test finishActivity()131 public void finishActivity() { 132 rule.launchActivity(null); 133 callbacks.clear(); 134 rule.finishActivity(); 135 136 assertThat(callbacks).contains("afterActivityFinished"); 137 // TODO: On-device this will also invoke onPause windowFocusChanged false 138 // need to track activity state and respond accordingly in robolectric 139 } 140 141 @Test 142 @Ignore // javadoc for ActivityTestRule#finishActivity is incorrect finishActivity_notLaunched()143 public void finishActivity_notLaunched() { 144 try { 145 rule.finishActivity(); 146 fail("exception not thrown"); 147 } catch (IllegalStateException e) { 148 // expected 149 } 150 } 151 } 152