Add tests for ActivityScenarioRule in Hilt.
RELNOTES=N/A
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=311830950
diff --git a/java/dagger/hilt/android/example/gradle/simple/app/build.gradle b/java/dagger/hilt/android/example/gradle/simple/app/build.gradle
index c95ea85..e6184bf 100644
--- a/java/dagger/hilt/android/example/gradle/simple/app/build.gradle
+++ b/java/dagger/hilt/android/example/gradle/simple/app/build.gradle
@@ -57,6 +57,7 @@
testAnnotationProcessor 'com.google.dagger:hilt-android-compiler:LOCAL-SNAPSHOT'
androidTestImplementation 'com.google.truth:truth:1.0.1'
+ androidTestImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'com.google.dagger:hilt-android-testing:LOCAL-SNAPSHOT'
diff --git a/java/dagger/hilt/android/example/gradle/simple/app/src/androidTest/java/dagger/hilt/android/example/gradle/simple/ActivityScenarioRuleTest.java b/java/dagger/hilt/android/example/gradle/simple/app/src/androidTest/java/dagger/hilt/android/example/gradle/simple/ActivityScenarioRuleTest.java
new file mode 100644
index 0000000..7f1fcfc
--- /dev/null
+++ b/java/dagger/hilt/android/example/gradle/simple/app/src/androidTest/java/dagger/hilt/android/example/gradle/simple/ActivityScenarioRuleTest.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2020 The Dagger Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package dagger.hilt.android.example.gradle.simple;
+
+import static androidx.lifecycle.Lifecycle.State.RESUMED;
+import static com.google.common.truth.Truth.assertThat;
+
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.test.ext.junit.rules.ActivityScenarioRule;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import dagger.hilt.android.AndroidEntryPoint;
+import dagger.hilt.android.testing.BindValue;
+import dagger.hilt.android.testing.HiltAndroidRule;
+import dagger.hilt.android.testing.HiltAndroidTest;
+import javax.inject.Inject;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/** Tests that {@link ActivityScenarioRule} works with Hilt tests. */
+@HiltAndroidTest
+@RunWith(AndroidJUnit4.class)
+public final class ActivityScenarioRuleTest {
+ private static final String STR_VALUE = "STR_VALUE";
+
+ /** An activity to test injection. */
+ @AndroidEntryPoint
+ public static final class TestActivity extends AppCompatActivity {
+ @Inject String str;
+ }
+
+ @Rule(order = 0) public HiltAndroidRule hiltRule = new HiltAndroidRule(this);
+
+ @Rule(order = 1)
+ public ActivityScenarioRule<TestActivity> scenarioRule =
+ new ActivityScenarioRule<>(TestActivity.class);
+
+ @BindValue String str = STR_VALUE;
+
+ @Test
+ public void testState() {
+ assertThat(scenarioRule.getScenario().getState()).isEqualTo(RESUMED);
+ }
+
+ @Test
+ public void testInjection() {
+ scenarioRule
+ .getScenario()
+ .onActivity(activity -> assertThat(activity.str).isEqualTo(STR_VALUE));
+ }
+}
diff --git a/java/dagger/hilt/android/example/gradle/simple/app/src/debug/AndroidManifest.xml b/java/dagger/hilt/android/example/gradle/simple/app/src/debug/AndroidManifest.xml
index 27274ce..86460d8 100644
--- a/java/dagger/hilt/android/example/gradle/simple/app/src/debug/AndroidManifest.xml
+++ b/java/dagger/hilt/android/example/gradle/simple/app/src/debug/AndroidManifest.xml
@@ -25,5 +25,9 @@
android:name=".Injection2Test$TestActivity"
android:theme="@style/Theme.AppCompat.Light"
android:exported="false"/>
+ <activity
+ android:name=".ActivityScenarioRuleTest$TestActivity"
+ android:theme="@style/Theme.AppCompat.Light"
+ android:exported="false"/>
</application>
</manifest>
diff --git a/java/dagger/hilt/android/example/gradle/simple/app/src/test/java/dagger/hilt/android/example/gradle/simple/ActivityScenarioRuleTest.java b/java/dagger/hilt/android/example/gradle/simple/app/src/test/java/dagger/hilt/android/example/gradle/simple/ActivityScenarioRuleTest.java
new file mode 100644
index 0000000..81cfc81
--- /dev/null
+++ b/java/dagger/hilt/android/example/gradle/simple/app/src/test/java/dagger/hilt/android/example/gradle/simple/ActivityScenarioRuleTest.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2020 The Dagger Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package dagger.hilt.android.example.gradle.simple;
+
+import static androidx.lifecycle.Lifecycle.State.RESUMED;
+import static com.google.common.truth.Truth.assertThat;
+
+import android.os.Build;
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.test.ext.junit.rules.ActivityScenarioRule;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import dagger.hilt.android.AndroidEntryPoint;
+import dagger.hilt.android.testing.BindValue;
+import dagger.hilt.android.testing.HiltAndroidRule;
+import dagger.hilt.android.testing.HiltAndroidTest;
+import dagger.hilt.android.testing.HiltTestApplication;
+import javax.inject.Inject;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.annotation.Config;
+
+/** Tests that {@link ActivityScenarioRule} works with Hilt tests. */
+@HiltAndroidTest
+@RunWith(AndroidJUnit4.class)
+// Robolectric requires Java9 to run API 29 and above, so use API 28 instead
+@Config(sdk = Build.VERSION_CODES.P, application = HiltTestApplication.class)
+public final class ActivityScenarioRuleTest {
+ private static final String STR_VALUE = "STR_VALUE";
+
+ /** An activity to test injection. */
+ @AndroidEntryPoint
+ public static final class TestActivity extends AppCompatActivity {
+ @Inject String str;
+ }
+
+ @Rule(order = 0) public HiltAndroidRule hiltRule = new HiltAndroidRule(this);
+
+ @Rule(order = 1)
+ public ActivityScenarioRule<TestActivity> scenarioRule =
+ new ActivityScenarioRule<>(TestActivity.class);
+
+ @BindValue String str = STR_VALUE;
+
+ @Test
+ public void testState() {
+ assertThat(scenarioRule.getScenario().getState()).isEqualTo(RESUMED);
+ }
+
+ @Test
+ public void testInjection() {
+ scenarioRule
+ .getScenario()
+ .onActivity(activity -> assertThat(activity.str).isEqualTo(STR_VALUE));
+ }
+}