diff --git a/runner/AndroidJunitRunnerSample/app/build.gradle b/runner/AndroidJunitRunnerSample/app/build.gradle index 5d7057c1b..930ef8da2 100644 --- a/runner/AndroidJunitRunnerSample/app/build.gradle +++ b/runner/AndroidJunitRunnerSample/app/build.gradle @@ -22,11 +22,19 @@ android { productFlavors { } - useLibrary 'android.test.runner' - - useLibrary 'android.test.base' - useLibrary 'android.test.mock' - + testOptions { + devices { + // run with ../gradlew -P android.sdk.channel=3 nexusOneApi30DebugAndroidTest + nexusOneApi30 (com.android.build.api.dsl.ManagedVirtualDevice) { + // A lower resolution device is used here for better emulator performance + device = "Nexus One" + apiLevel = 30 + // Also use the AOSP ATD image for better emulator performance + systemImageSource = "aosp-atd" + abi = "x86" + } + } + } } dependencies { diff --git a/runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/CalculatorInstrumentationTest.java b/runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/CalculatorInstrumentationTest.java index 8cdde1d39..d42fdf065 100644 --- a/runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/CalculatorInstrumentationTest.java +++ b/runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/CalculatorInstrumentationTest.java @@ -19,11 +19,13 @@ import junit.framework.TestSuite; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.internal.builders.AllDefaultPossibilitiesBuilder; import org.junit.runner.RunWith; import androidx.test.core.app.ActivityScenario; +import androidx.test.ext.junit.rules.ActivityScenarioRule; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.LargeTest; import androidx.test.runner.AndroidJUnitRunner; @@ -36,27 +38,23 @@ import static androidx.test.espresso.assertion.ViewAssertions.matches; import static androidx.test.espresso.matcher.ViewMatchers.withId; import static androidx.test.espresso.matcher.ViewMatchers.withText; +import static com.example.android.testing.androidjunitrunnersample.HintMatcher.withHint; /** - * JUnit4 Ui Tests for {@link CalculatorActivity} using the {@link AndroidJUnitRunner}. - * This class uses the JUnit4 syntax for tests. - *

- * With the new AndroidJUnit runner you can run both JUnit3 and JUnit4 tests in a single test - * suite. The {@link AndroidRunnerBuilder} which extends JUnit's - * {@link AllDefaultPossibilitiesBuilder} will create a single {@link - * TestSuite} from all tests and run them. + * Ui Tests for {@link CalculatorActivity} using the {@link AndroidJUnitRunner}. */ @RunWith(AndroidJUnit4.class) @LargeTest public class CalculatorInstrumentationTest { + /** - * Use {@link ActivityScenario} to create and launch of the activity. + * Use {@link ActivityScenarioRule} to create and launch of the activity before each test, and close + * it after each test. */ - @Before - public void launchActivity() { - ActivityScenario.launch(CalculatorActivity.class); - } + @Rule + public ActivityScenarioRule mActivityScenarioRule + = new ActivityScenarioRule<>(CalculatorActivity.class); @Test public void noOperandShowsComputationError() { @@ -105,5 +103,4 @@ private void performOperation(int btnOperationResId, String operandOne, // Check the expected test is displayed in the Ui onView(withId(R.id.operation_result_text_view)).check(matches(withText(expectedResult))); } - } diff --git a/runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/OperationHintInstrumentationTest.java b/runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/OperationHintInstrumentationTest.java index 54898a02c..7d4d2016d 100644 --- a/runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/OperationHintInstrumentationTest.java +++ b/runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/OperationHintInstrumentationTest.java @@ -16,60 +16,45 @@ package com.example.android.testing.androidjunitrunnersample; -import junit.framework.TestSuite; - -import org.junit.internal.builders.AllDefaultPossibilitiesBuilder; +import static androidx.test.core.app.ApplicationProvider.getApplicationContext; +import static androidx.test.espresso.Espresso.onView; +import static androidx.test.espresso.assertion.ViewAssertions.matches; +import static androidx.test.espresso.matcher.ViewMatchers.withId; +import static com.example.android.testing.androidjunitrunnersample.HintMatcher.withHint; +import androidx.test.ext.junit.rules.ActivityScenarioRule; +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.LargeTest; import androidx.test.runner.AndroidJUnitRunner; -import android.test.ActivityInstrumentationTestCase2; -import static com.example.android.testing.androidjunitrunnersample.HintMatcher.withHint; -import static androidx.test.espresso.Espresso.onView; -import static androidx.test.espresso.assertion.ViewAssertions.matches; -import static androidx.test.espresso.matcher.ViewMatchers.withId; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.junit.Assert.assertThat; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; /** - * JUnit3 Ui Tests for {@link CalculatorActivity} using the {@link AndroidJUnitRunner}. This class - * uses the Junit3 syntax for tests. - * - *

With the new AndroidJUnit runner you can run both JUnit3 and JUnit4 tests in a single test - * test suite. The {@link AndroidRunnerBuilder} which extends JUnit's {@link - * AllDefaultPossibilitiesBuilder} will create a single {@link TestSuite} from all tests and run - * them.

+ * Ui Tests for {@link CalculatorActivity} operation hints using the {@link AndroidJUnitRunner}. */ @LargeTest -public class OperationHintInstrumentationTest - extends ActivityInstrumentationTestCase2 { - - private CalculatorActivity mActivity; - - public OperationHintInstrumentationTest() { - super(CalculatorActivity.class); - } - - @Override - protected void setUp() throws Exception { - super.setUp(); - - // Espresso does not start the Activity for you we need to do this manually here. - mActivity = getActivity(); - } - - public void testPreconditions() { - assertThat(mActivity, notNullValue()); - } - +@RunWith(AndroidJUnit4.class) +public class OperationHintInstrumentationTest { + + /** + * Use {@link ActivityScenarioRule} to create and launch of the activity before each test, and close + * it after each test. + */ + @Rule + public ActivityScenarioRule mActivityScenarioRule + = new ActivityScenarioRule<>(CalculatorActivity.class); + + @Test public void testEditText_OperandOneHint() { - String operandOneHint = mActivity.getString(R.string.type_operand_one_hint); + String operandOneHint = getApplicationContext().getString(R.string.type_operand_one_hint); onView(withId(R.id.operand_one_edit_text)).check(matches(withHint(operandOneHint))); } + @Test public void testEditText_OperandTwoHint() { - String operandTwoHint = mActivity.getString(R.string.type_operant_two_hint); + String operandTwoHint = getApplicationContext().getString(R.string.type_operant_two_hint); onView(withId(R.id.operand_two_edit_text)).check(matches(withHint(operandTwoHint))); } - } diff --git a/runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/OperationHintLegacyInstrumentationTest.java b/runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/OperationHintLegacyInstrumentationTest.java deleted file mode 100644 index a6226abdb..000000000 --- a/runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/OperationHintLegacyInstrumentationTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 2015, The Android Open Source Project - * - * 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 com.example.android.testing.androidjunitrunnersample; - -import junit.framework.TestSuite; - -import org.junit.internal.builders.AllDefaultPossibilitiesBuilder; - -import androidx.test.filters.LargeTest; -import androidx.test.runner.AndroidJUnitRunner; -import android.test.ActivityInstrumentationTestCase2; - -import static androidx.test.espresso.Espresso.onView; -import static androidx.test.espresso.assertion.ViewAssertions.matches; -import static androidx.test.espresso.matcher.ViewMatchers.withId; -import static com.example.android.testing.androidjunitrunnersample.HintMatcher.withHint; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.junit.Assert.assertThat; - -/** - * JUnit3 Ui Tests for {@link CalculatorActivity} using the {@link AndroidJUnitRunner}. This class - * uses the Junit3 syntax for tests and extends {@link ActivityInstrumentationTestCase2}. - *

- * With the new AndroidJUnit runner you can run both JUnit3 and JUnit4 tests in a single test - * test suite. The {@link AndroidRunnerBuilder} which extends JUnit's {@link - * AllDefaultPossibilitiesBuilder} will create a single {@link TestSuite} from all tests and run - * them. - *

- * ActivityInstrumentationTestCase2 will be deprecated soon. Please use {@link ActivityTestRule} - * when writing new tests. For an example on how to use {@link ActivityTestRule} please see - * {@link CalculatorInstrumentationTest}. - */ -@LargeTest -public class OperationHintLegacyInstrumentationTest - extends ActivityInstrumentationTestCase2 { - - private CalculatorActivity mActivity; - - public OperationHintLegacyInstrumentationTest() { - super(CalculatorActivity.class); - } - - @Override - protected void setUp() throws Exception { - super.setUp(); - - // Espresso does not start the Activity for you we need to do this manually here. - mActivity = getActivity(); - } - - public void testPreconditions() { - assertThat(mActivity, notNullValue()); - } - - public void testEditText_OperandOneHint() { - String operandOneHint = mActivity.getString(R.string.type_operand_one_hint); - onView(withId(R.id.operand_one_edit_text)).check(matches(withHint(operandOneHint))); - } - - public void testEditText_OperandTwoHint() { - String operandTwoHint = mActivity.getString(R.string.type_operant_two_hint); - onView(withId(R.id.operand_two_edit_text)).check(matches(withHint(operandTwoHint))); - } - -} diff --git a/runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/suite/InstrumentationTestSuite.java b/runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/suite/InstrumentationTestSuite.java index e585edae0..19d1c63f7 100644 --- a/runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/suite/InstrumentationTestSuite.java +++ b/runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/suite/InstrumentationTestSuite.java @@ -17,7 +17,7 @@ package com.example.android.testing.androidjunitrunnersample.suite; import com.example.android.testing.androidjunitrunnersample.CalculatorInstrumentationTest; -import com.example.android.testing.androidjunitrunnersample.OperationHintLegacyInstrumentationTest; +import com.example.android.testing.androidjunitrunnersample.OperationHintInstrumentationTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; @@ -26,5 +26,5 @@ * Runs all Junit3 and Junit4 Instrumentation tests. */ @RunWith(Suite.class) -@Suite.SuiteClasses({CalculatorInstrumentationTest.class, OperationHintLegacyInstrumentationTest.class}) +@Suite.SuiteClasses({CalculatorInstrumentationTest.class, OperationHintInstrumentationTest.class}) public class InstrumentationTestSuite {} diff --git a/runner/AndroidJunitRunnerSample/build.gradle b/runner/AndroidJunitRunnerSample/build.gradle index 8006df0c8..dd1fabeb5 100644 --- a/runner/AndroidJunitRunnerSample/build.gradle +++ b/runner/AndroidJunitRunnerSample/build.gradle @@ -1,7 +1,7 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.agpVersion = "7.0.2" + ext.agpVersion = "7.1.0-alpha13" repositories { // Insert local test repo here google() diff --git a/runner/AndroidJunitRunnerSample/gradle.properties b/runner/AndroidJunitRunnerSample/gradle.properties index cbbc44ba0..426b57cd5 100644 --- a/runner/AndroidJunitRunnerSample/gradle.properties +++ b/runner/AndroidJunitRunnerSample/gradle.properties @@ -17,4 +17,4 @@ # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true android.useAndroidX=true -android.useAndroidX=true + diff --git a/runner/AndroidJunitRunnerSample/gradle/wrapper/gradle-wrapper.properties b/runner/AndroidJunitRunnerSample/gradle/wrapper/gradle-wrapper.properties index c31c2fddf..bf8781776 100644 --- a/runner/AndroidJunitRunnerSample/gradle/wrapper/gradle-wrapper.properties +++ b/runner/AndroidJunitRunnerSample/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip