From b976d8141e1e659c9abd0e914d1df9c8390942c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Alc=C3=A9rreca?= Date: Thu, 18 Jan 2024 15:25:02 +0000 Subject: [PATCH 1/2] Adds more espresso-device samples --- .../EspressoDeviceSample/app/build.gradle | 20 ++++++- .../app/src/androidTest/AndroidManifest.xml | 4 ++ .../EspressoDeviceSample/OnDeviceTest.kt | 52 +++++++++++++++++++ .../RequiresDisplayTest.kt | 5 +- .../app/src/main/AndroidManifest.xml | 2 +- .../app/src/main/AppManifest.xml | 2 +- .../app/src/main/res/layout/activity_main.xml | 5 ++ ui/espresso/EspressoDeviceSample/build.gradle | 4 +- .../EspressoDeviceSample/gradle.properties | 1 + .../gradle/wrapper/gradle-wrapper.properties | 3 +- 10 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 ui/espresso/EspressoDeviceSample/app/src/androidTest/java/com/example/android/testing/espresso/EspressoDeviceSample/OnDeviceTest.kt diff --git a/ui/espresso/EspressoDeviceSample/app/build.gradle b/ui/espresso/EspressoDeviceSample/app/build.gradle index 806ea4543..8990d8442 100644 --- a/ui/espresso/EspressoDeviceSample/app/build.gradle +++ b/ui/espresso/EspressoDeviceSample/app/build.gradle @@ -6,7 +6,7 @@ android { compileSdk 33 defaultConfig { applicationId "com.example.android.testing.espresso.EspressoDeviceSample" - minSdkVersion 14 + minSdkVersion 19 targetSdkVersion 33 versionCode 1 versionName "1.0" @@ -15,13 +15,19 @@ android { } productFlavors { } + testOptions { + + emulatorControl { + enable = true + } unitTests { + includeAndroidResources = true } managedDevices { devices { - // run with ../gradlew nexusOneApi30DebugAndroidTest + // run with ./gradlew nexusOneApi30DebugAndroidTest nexusOneApi30(com.android.build.api.dsl.ManagedVirtualDevice) { // A lower resolution device is used here for better emulator performance device = "Nexus One" @@ -31,6 +37,16 @@ android { // to take a screenshot systemImageSource = "aosp-atd" } + // run with ./gradlew foldApi34DebugAndroidTest + foldApi34(com.android.build.api.dsl.ManagedVirtualDevice) { + // A lower resolution device is used here for better emulator performance + device = "Pixel Fold" + apiLevel = 34 + // Also use the AOSP ATD image for better emulator performance + // The androidx.test screenshot APIs will automatically enable hardware rendering + // to take a screenshot + systemImageSource = "aosp" + } } } } diff --git a/ui/espresso/EspressoDeviceSample/app/src/androidTest/AndroidManifest.xml b/ui/espresso/EspressoDeviceSample/app/src/androidTest/AndroidManifest.xml index 28336a3ff..9df6d0f59 100644 --- a/ui/espresso/EspressoDeviceSample/app/src/androidTest/AndroidManifest.xml +++ b/ui/espresso/EspressoDeviceSample/app/src/androidTest/AndroidManifest.xml @@ -19,8 +19,12 @@ android:versionCode="1" android:versionName="1.0"> + + + + diff --git a/ui/espresso/EspressoDeviceSample/app/src/androidTest/java/com/example/android/testing/espresso/EspressoDeviceSample/OnDeviceTest.kt b/ui/espresso/EspressoDeviceSample/app/src/androidTest/java/com/example/android/testing/espresso/EspressoDeviceSample/OnDeviceTest.kt new file mode 100644 index 000000000..e88951642 --- /dev/null +++ b/ui/espresso/EspressoDeviceSample/app/src/androidTest/java/com/example/android/testing/espresso/EspressoDeviceSample/OnDeviceTest.kt @@ -0,0 +1,52 @@ +package com.example.android.testing.espresso.EspressoDeviceSample + +import androidx.test.espresso.Espresso.closeSoftKeyboard +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.action.ViewActions.click +import androidx.test.espresso.action.ViewActions.typeText +import androidx.test.espresso.assertion.ViewAssertions.matches +import androidx.test.espresso.device.DeviceInteraction.Companion.setBookMode +import androidx.test.espresso.device.DeviceInteraction.Companion.setClosedMode +import androidx.test.espresso.device.DeviceInteraction.Companion.setFlatMode +import androidx.test.espresso.device.DeviceInteraction.Companion.setTabletopMode +import androidx.test.espresso.device.EspressoDevice.Companion.onDevice +import androidx.test.espresso.device.action.ScreenOrientation +import androidx.test.espresso.device.action.setClosedMode +import androidx.test.espresso.device.action.setFlatMode +import androidx.test.espresso.device.action.setTabletopMode +import androidx.test.espresso.device.controller.DeviceMode.TABLETOP +import androidx.test.espresso.device.filter.RequiresDeviceMode +import androidx.test.espresso.device.rules.ScreenOrientationRule +import androidx.test.espresso.matcher.ViewMatchers.isDisplayed +import androidx.test.espresso.matcher.ViewMatchers.withId +import androidx.test.ext.junit.rules.activityScenarioRule +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +/* + * Illustrates usage of [onDevice] API to change the mode of foldable devices. + */ +@RunWith(AndroidJUnit4::class) +class OnDeviceTest { + + @get:Rule var activityScenarioRule = activityScenarioRule() + + @get:Rule val screenOrientationRule: ScreenOrientationRule = + ScreenOrientationRule(ScreenOrientation.PORTRAIT) + + @Test + @RequiresDeviceMode(TABLETOP) + fun changeText_sameActivity() { + + onDevice().setBookMode() + // Type text and then press the button. + onView(withId(R.id.hello_tv)).check(matches(isDisplayed())) + onDevice().setTabletopMode() + onDevice().setClosedMode() + onView(withId(R.id.hello_tv)).check(matches(isDisplayed())) + onDevice().setFlatMode() + onView(withId(R.id.hello_tv)).check(matches(isDisplayed())) + } +} diff --git a/ui/espresso/EspressoDeviceSample/app/src/androidTest/java/com/example/android/testing/espresso/EspressoDeviceSample/RequiresDisplayTest.kt b/ui/espresso/EspressoDeviceSample/app/src/androidTest/java/com/example/android/testing/espresso/EspressoDeviceSample/RequiresDisplayTest.kt index 6ba423979..d14c5af1c 100644 --- a/ui/espresso/EspressoDeviceSample/app/src/androidTest/java/com/example/android/testing/espresso/EspressoDeviceSample/RequiresDisplayTest.kt +++ b/ui/espresso/EspressoDeviceSample/app/src/androidTest/java/com/example/android/testing/espresso/EspressoDeviceSample/RequiresDisplayTest.kt @@ -15,6 +15,8 @@ */ package com.example.android.testing.espresso.EspressoDeviceSample +import androidx.test.espresso.device.EspressoDevice.Companion.onDevice +import androidx.test.espresso.device.action.setTabletopMode import androidx.test.espresso.device.filter.RequiresDisplay import androidx.test.espresso.device.sizeclass.HeightSizeClass import androidx.test.espresso.device.sizeclass.WidthSizeClass @@ -27,6 +29,7 @@ import org.junit.runner.RunWith */ @RunWith(AndroidJUnit4::class) class RequiresDisplayTest { + @RequiresDisplay( widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.COMPACT, heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.COMPACT @@ -90,4 +93,4 @@ class RequiresDisplayTest { ) @Test fun testOnDevicesWithExpandedWidthAndHeight() {} -} \ No newline at end of file +} diff --git a/ui/espresso/EspressoDeviceSample/app/src/main/AndroidManifest.xml b/ui/espresso/EspressoDeviceSample/app/src/main/AndroidManifest.xml index 6b29b606b..6f0729028 100644 --- a/ui/espresso/EspressoDeviceSample/app/src/main/AndroidManifest.xml +++ b/ui/espresso/EspressoDeviceSample/app/src/main/AndroidManifest.xml @@ -16,6 +16,7 @@ --> + - diff --git a/ui/espresso/EspressoDeviceSample/app/src/main/AppManifest.xml b/ui/espresso/EspressoDeviceSample/app/src/main/AppManifest.xml index a314a52af..5f6eab579 100644 --- a/ui/espresso/EspressoDeviceSample/app/src/main/AppManifest.xml +++ b/ui/espresso/EspressoDeviceSample/app/src/main/AppManifest.xml @@ -18,5 +18,5 @@ - + diff --git a/ui/espresso/EspressoDeviceSample/app/src/main/res/layout/activity_main.xml b/ui/espresso/EspressoDeviceSample/app/src/main/res/layout/activity_main.xml index 3e52e22f1..fc8461066 100644 --- a/ui/espresso/EspressoDeviceSample/app/src/main/res/layout/activity_main.xml +++ b/ui/espresso/EspressoDeviceSample/app/src/main/res/layout/activity_main.xml @@ -21,4 +21,9 @@ android:orientation="vertical" android:padding="@dimen/activity_horizontal_margin" tools:context=".MainActivity"> + diff --git a/ui/espresso/EspressoDeviceSample/build.gradle b/ui/espresso/EspressoDeviceSample/build.gradle index 8f441bd84..8ddb5c1e8 100644 --- a/ui/espresso/EspressoDeviceSample/build.gradle +++ b/ui/espresso/EspressoDeviceSample/build.gradle @@ -2,7 +2,7 @@ buildscript { ext.kotlinVersion = "1.7.10" - ext.agpVersion = "8.1.1" + ext.agpVersion = "8.3.0-beta02" repositories { // Insert local test repo here google() @@ -29,5 +29,5 @@ ext { robolectricVersion = "4.10.3" extTruthVersion = "1.6.0-alpha01" extJUnitVersion = "1.2.0-alpha01" - espressoDeviceVersion = "1.0.0-alpha01" + espressoDeviceVersion = "1.0.0-alpha07" } diff --git a/ui/espresso/EspressoDeviceSample/gradle.properties b/ui/espresso/EspressoDeviceSample/gradle.properties index 20d5b5682..6ae4cdced 100644 --- a/ui/espresso/EspressoDeviceSample/gradle.properties +++ b/ui/espresso/EspressoDeviceSample/gradle.properties @@ -20,3 +20,4 @@ android.defaults.buildfeatures.buildconfig=true android.nonFinalResIds=false android.nonTransitiveRClass=false android.useAndroidX=true +android.experimental.androidTest.enableEmulatorControl=true diff --git a/ui/espresso/EspressoDeviceSample/gradle/wrapper/gradle-wrapper.properties b/ui/espresso/EspressoDeviceSample/gradle/wrapper/gradle-wrapper.properties index b93c46a5f..1dc51a370 100644 --- a/ui/espresso/EspressoDeviceSample/gradle/wrapper/gradle-wrapper.properties +++ b/ui/espresso/EspressoDeviceSample/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ +#Thu Jan 18 11:36:24 UTC 2024 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists From f1b37737e258b0be0bc7a4f7823098f920994bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Alc=C3=A9rreca?= Date: Thu, 18 Jan 2024 17:39:20 +0000 Subject: [PATCH 2/2] Adds rule order --- .../EspressoDeviceSample/OnDeviceTest.kt | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/ui/espresso/EspressoDeviceSample/app/src/androidTest/java/com/example/android/testing/espresso/EspressoDeviceSample/OnDeviceTest.kt b/ui/espresso/EspressoDeviceSample/app/src/androidTest/java/com/example/android/testing/espresso/EspressoDeviceSample/OnDeviceTest.kt index e88951642..283063a87 100644 --- a/ui/espresso/EspressoDeviceSample/app/src/androidTest/java/com/example/android/testing/espresso/EspressoDeviceSample/OnDeviceTest.kt +++ b/ui/espresso/EspressoDeviceSample/app/src/androidTest/java/com/example/android/testing/espresso/EspressoDeviceSample/OnDeviceTest.kt @@ -1,9 +1,6 @@ package com.example.android.testing.espresso.EspressoDeviceSample -import androidx.test.espresso.Espresso.closeSoftKeyboard import androidx.test.espresso.Espresso.onView -import androidx.test.espresso.action.ViewActions.click -import androidx.test.espresso.action.ViewActions.typeText import androidx.test.espresso.assertion.ViewAssertions.matches import androidx.test.espresso.device.DeviceInteraction.Companion.setBookMode import androidx.test.espresso.device.DeviceInteraction.Companion.setClosedMode @@ -11,9 +8,6 @@ import androidx.test.espresso.device.DeviceInteraction.Companion.setFlatMode import androidx.test.espresso.device.DeviceInteraction.Companion.setTabletopMode import androidx.test.espresso.device.EspressoDevice.Companion.onDevice import androidx.test.espresso.device.action.ScreenOrientation -import androidx.test.espresso.device.action.setClosedMode -import androidx.test.espresso.device.action.setFlatMode -import androidx.test.espresso.device.action.setTabletopMode import androidx.test.espresso.device.controller.DeviceMode.TABLETOP import androidx.test.espresso.device.filter.RequiresDeviceMode import androidx.test.espresso.device.rules.ScreenOrientationRule @@ -31,22 +25,22 @@ import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class OnDeviceTest { - @get:Rule var activityScenarioRule = activityScenarioRule() + @get:Rule(order = 1) var activityScenarioRule = activityScenarioRule() - @get:Rule val screenOrientationRule: ScreenOrientationRule = - ScreenOrientationRule(ScreenOrientation.PORTRAIT) + @get:Rule(order = 2) var screenOrientationRule: ScreenOrientationRule = + ScreenOrientationRule(ScreenOrientation.LANDSCAPE) @Test @RequiresDeviceMode(TABLETOP) - fun changeText_sameActivity() { + fun tabletopMode_playerIdDisplayed() { onDevice().setBookMode() // Type text and then press the button. onView(withId(R.id.hello_tv)).check(matches(isDisplayed())) - onDevice().setTabletopMode() - onDevice().setClosedMode() - onView(withId(R.id.hello_tv)).check(matches(isDisplayed())) - onDevice().setFlatMode() - onView(withId(R.id.hello_tv)).check(matches(isDisplayed())) +// onDevice().setTabletopMode() +// onDevice().setClosedMode() +// onView(withId(R.id.hello_tv)).check(matches(isDisplayed())) +// onDevice().setFlatMode() +// onView(withId(R.id.hello_tv)).check(matches(isDisplayed())) } }