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