diff --git a/gradle-tests/espresso/device/build.gradle b/gradle-tests/espresso/device/build.gradle new file mode 100644 index 000000000..16ecdbe16 --- /dev/null +++ b/gradle-tests/espresso/device/build.gradle @@ -0,0 +1,56 @@ +plugins { + id 'com.android.library' + id 'org.jetbrains.kotlin.android' +} + +tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) { + kotlinOptions { + jvmTarget = "17" + } +} + +android { + namespace 'androidx.test.gradletests.espresso.device' + compileSdk rootProject.ext.compileSdk + + defaultConfig { + minSdk rootProject.ext.minSdk + targetSdk rootProject.ext.targetSdk + multiDexEnabled true + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_17 + targetCompatibility JavaVersion.VERSION_17 + } + testOptions { + animationsDisabled = true + managedDevices { + devices { + // run with ../gradlew nexusOneDebugAndroidTest + nexusOne(com.android.build.api.dsl.ManagedVirtualDevice) { + // A lower resolution device is used here for better emulator performance + device = "Nexus One" + apiLevel = rootProject.ext.emulatorApi + // Also use the AOSP Automated Test Device image for better emulator performance + systemImageSource = "aosp-atd" + } + } + } + emulatorControl { + enable = true + } + } +} + +dependencies { + androidTestImplementation libs.espresso.core + androidTestImplementation libs.espresso.device + androidTestImplementation libs.ext.junit + androidTestImplementation libs.ext.truth +} \ No newline at end of file diff --git a/gradle-tests/espresso/device/src/androidTest/java/test/gradletests/espresso/device/EspressoDeviceTest.kt b/gradle-tests/espresso/device/src/androidTest/java/test/gradletests/espresso/device/EspressoDeviceTest.kt new file mode 100644 index 000000000..75c634580 --- /dev/null +++ b/gradle-tests/espresso/device/src/androidTest/java/test/gradletests/espresso/device/EspressoDeviceTest.kt @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2023 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 androidx.test.gradletests.espresso.device + +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.action.ViewActions.click +import androidx.test.espresso.assertion.ViewAssertions.matches +import androidx.test.espresso.device.EspressoDevice.Companion.onDevice +import androidx.test.espresso.device.action.ScreenOrientation +import androidx.test.espresso.device.action.setScreenOrientation +import androidx.test.espresso.device.rules.ScreenOrientationRule +import androidx.test.espresso.matcher.ViewMatchers.withId +import androidx.test.espresso.matcher.ViewMatchers.withText +import androidx.test.ext.junit.rules.ActivityScenarioRule +import org.junit.Rule +import org.junit.Test +import org.junit.rules.RuleChain +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(JUnit4::class) +class EspressoDeviceTest { + private val activityRule: ActivityScenarioRule = + ActivityScenarioRule(ScreenOrientationActivity::class.java) + + private val screenOrientationRule: ScreenOrientationRule = + ScreenOrientationRule(ScreenOrientation.PORTRAIT) + + @get:Rule + val ruleChain: RuleChain = RuleChain.outerRule(activityRule).around(screenOrientationRule) + + @Test + fun onDevice_setScreenOrientationToLandscape() { + onDevice().perform(setScreenOrientation(ScreenOrientation.LANDSCAPE)) + + onView(withId(R.id.current_screen_orientation)).check(matches(withText("landscape"))) + } + + @Test + fun onDevice_setScreenOrientationToPortrait() { + onDevice().perform(setScreenOrientation(ScreenOrientation.PORTRAIT)) + + onView(withId(R.id.current_screen_orientation)).check(matches(withText("portrait"))) + } + + @Test + fun onDevice_setScreenOrientationToLandscapeAndThenToPortrait() { + onDevice().perform(setScreenOrientation(ScreenOrientation.LANDSCAPE)) + + onView(withId(R.id.current_screen_orientation)).check(matches(withText("landscape"))) + + onDevice().perform(setScreenOrientation(ScreenOrientation.PORTRAIT)) + + onView(withId(R.id.current_screen_orientation)).check(matches(withText("portrait"))) + } + + @Test + fun onDevice_clickAndThenSetScreenOrientationToLandscape() { + onView(withId(R.id.current_screen_orientation)).perform(click()) + + onDevice().perform(setScreenOrientation(ScreenOrientation.LANDSCAPE)) + + onView(withId(R.id.current_screen_orientation)).check(matches(withText("landscape"))) + } + + @Test + fun onDevice_setScreenOrientationToLandscapeThenClick() { + onDevice().perform(setScreenOrientation(ScreenOrientation.LANDSCAPE)) + + onView(withId(R.id.current_screen_orientation)).perform(click()) + onView(withId(R.id.current_screen_orientation)).check(matches(withText("landscape"))) + } +} diff --git a/gradle-tests/espresso/device/src/main/AndroidManifest.xml b/gradle-tests/espresso/device/src/main/AndroidManifest.xml new file mode 100644 index 000000000..57a4c505d --- /dev/null +++ b/gradle-tests/espresso/device/src/main/AndroidManifest.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/gradle-tests/espresso/device/src/main/java/androidx/test/gradletests/espresso/device/ScreenOrientationActivity.kt b/gradle-tests/espresso/device/src/main/java/androidx/test/gradletests/espresso/device/ScreenOrientationActivity.kt new file mode 100644 index 000000000..0f0391aa3 --- /dev/null +++ b/gradle-tests/espresso/device/src/main/java/androidx/test/gradletests/espresso/device/ScreenOrientationActivity.kt @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2023 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 androidx.test.gradletests.espresso.device + +import android.app.Activity +import android.content.res.Configuration +import android.os.Bundle +import android.util.Log +import android.widget.TextView + +/** Activity that updates a TextView when its screen orientation is changed. */ +class ScreenOrientationActivity : Activity() { + companion object { + private val TAG = "ScreenOrientationActivity" + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_screen_orientation) + + // Set orientation in onCreate the first time it's called. + val textView: TextView = findViewById(R.id.current_screen_orientation) + if ( + textView + .getText() + .toString() + .equals(getResources().getString(R.string.screen_orientation_text)) + ) { + val orientation = setOrientationString(getResources().getConfiguration().orientation) + Log.d(TAG, "onCreate. Orientation set to " + orientation) + } + } + + override fun onConfigurationChanged(newConfig: Configuration) { + super.onConfigurationChanged(newConfig) + val newOrientation = setOrientationString(newConfig.orientation) + Log.d(TAG, "onConfigurationChanged. New orientation is " + newOrientation) + } + + private fun setOrientationString(orientation: Int): String { + val orientationString = + if (orientation == Configuration.ORIENTATION_LANDSCAPE) { + "landscape" + } else { + "portrait" + } + + val textView: TextView = findViewById(R.id.current_screen_orientation) + textView.setText(orientationString) + return orientationString + } +} diff --git a/gradle-tests/espresso/device/src/main/res/layout/activity_screen_orientation.xml b/gradle-tests/espresso/device/src/main/res/layout/activity_screen_orientation.xml new file mode 100644 index 000000000..3dbddd996 --- /dev/null +++ b/gradle-tests/espresso/device/src/main/res/layout/activity_screen_orientation.xml @@ -0,0 +1,29 @@ + + + + + + + + \ No newline at end of file diff --git a/gradle-tests/espresso/device/src/main/res/values/strings.xml b/gradle-tests/espresso/device/src/main/res/values/strings.xml new file mode 100644 index 000000000..5ca58d0b6 --- /dev/null +++ b/gradle-tests/espresso/device/src/main/res/values/strings.xml @@ -0,0 +1,23 @@ + + + + + + screen orientation has not been set + + \ No newline at end of file diff --git a/gradle-tests/gradle.properties b/gradle-tests/gradle.properties index 3e927b11e..ad500c0da 100644 --- a/gradle-tests/gradle.properties +++ b/gradle-tests/gradle.properties @@ -18,4 +18,5 @@ android.useAndroidX=true # Enables namespacing of each library's R class so that its R class includes only the # resources declared in the library itself and none from the library's dependencies, # thereby reducing the size of the R class for that library -android.nonTransitiveRClass=true \ No newline at end of file +android.nonTransitiveRClass=true +android.experimental.androidTest.enableEmulatorControl=true \ No newline at end of file diff --git a/gradle-tests/settings.gradle b/gradle-tests/settings.gradle index 0de023046..b925edb9e 100644 --- a/gradle-tests/settings.gradle +++ b/gradle-tests/settings.gradle @@ -22,6 +22,7 @@ dependencyResolutionManagement { library('espresso.accessibility', 'androidx.test.espresso:espresso-accessibility:3.6.0-alpha03') library('espresso.contrib', 'androidx.test.espresso:espresso-contrib:3.6.0-alpha03') library('espresso.core', 'androidx.test.espresso:espresso-core:3.6.0-alpha03') + library('espresso.device', 'androidx.test.espresso:espresso-device:1.0.0-alpha08') library('espresso.idlingresource', 'androidx.test.espresso:espresso-idling-resource:3.6.0-alpha03') library('espresso.intents', 'androidx.test.espresso:espresso-intents:3.6.0-alpha03') library('espresso.web', 'androidx.test.espresso:espresso-web:3.6.0-alpha03') @@ -36,6 +37,7 @@ include ':runner' include ':espresso' include ':espresso:accessibility' include ':espresso:contrib' +include ':espresso:device' include ':espresso:idling_resource' include ':espresso:web' include ':orchestrator'