Skip to content

Commit 96d39da

Browse files
Paige McAuliffecopybara-androidxtest
authored andcommitted
Add EspressoDevice screen orientation tests to gradle tests
PiperOrigin-RevId: 738590802
1 parent 9ace3e9 commit 96d39da

File tree

10 files changed

+271
-1
lines changed

10 files changed

+271
-1
lines changed

gradle-tests/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ load(
66
"ANDROIDX_TRUTH_VERSION",
77
"CORE_VERSION",
88
"ESPRESSO_VERSION",
9+
"ESPRESSO_DEVICE_VERSION",
910
"MONITOR_VERSION",
1011
"ORCHESTRATOR_VERSION",
1112
"RULES_VERSION",
@@ -27,6 +28,7 @@ expand_template(
2728
"{ANDROIDX_TRUTH_VERSION}": ANDROIDX_TRUTH_VERSION,
2829
"{CORE_VERSION}": CORE_VERSION,
2930
"{ESPRESSO_VERSION}": ESPRESSO_VERSION,
31+
"{ESPRESSO_DEVICE_VERSION}": ESPRESSO_DEVICE_VERSION,
3032
"{MONITOR_VERSION}": MONITOR_VERSION,
3133
"{ORCHESTRATOR_VERSION}": ORCHESTRATOR_VERSION,
3234
"{RULES_VERSION}": RULES_VERSION,
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
plugins {
2+
id 'com.android.library'
3+
id 'org.jetbrains.kotlin.android'
4+
}
5+
6+
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
7+
kotlinOptions {
8+
jvmTarget = "17"
9+
}
10+
}
11+
12+
android {
13+
namespace 'androidx.test.gradletests.espresso.device'
14+
compileSdk rootProject.ext.compileSdk
15+
16+
defaultConfig {
17+
minSdk rootProject.ext.minSdk
18+
targetSdk rootProject.ext.targetSdk
19+
multiDexEnabled true
20+
21+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
22+
}
23+
24+
buildTypes {
25+
26+
}
27+
compileOptions {
28+
sourceCompatibility JavaVersion.VERSION_17
29+
targetCompatibility JavaVersion.VERSION_17
30+
}
31+
testOptions {
32+
animationsDisabled = true
33+
managedDevices {
34+
devices {
35+
// run with ../gradlew nexusOneDebugAndroidTest
36+
nexusOne(com.android.build.api.dsl.ManagedVirtualDevice) {
37+
// A lower resolution device is used here for better emulator performance
38+
device = "Nexus One"
39+
apiLevel = rootProject.ext.emulatorApi
40+
// Also use the AOSP Automated Test Device image for better emulator performance
41+
systemImageSource = "aosp-atd"
42+
}
43+
}
44+
}
45+
emulatorControl {
46+
enable = true
47+
}
48+
}
49+
}
50+
51+
dependencies {
52+
androidTestImplementation libs.espresso.core
53+
androidTestImplementation libs.espresso.device
54+
androidTestImplementation libs.ext.junit
55+
androidTestImplementation libs.ext.truth
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (C) 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.test.gradletests.espresso.device
18+
19+
import androidx.test.espresso.Espresso.onView
20+
import androidx.test.espresso.action.ViewActions.click
21+
import androidx.test.espresso.assertion.ViewAssertions.matches
22+
import androidx.test.espresso.device.EspressoDevice.Companion.onDevice
23+
import androidx.test.espresso.device.action.ScreenOrientation
24+
import androidx.test.espresso.device.action.setScreenOrientation
25+
import androidx.test.espresso.device.rules.ScreenOrientationRule
26+
import androidx.test.espresso.matcher.ViewMatchers.withId
27+
import androidx.test.espresso.matcher.ViewMatchers.withText
28+
import androidx.test.ext.junit.rules.ActivityScenarioRule
29+
import org.junit.Rule
30+
import org.junit.Test
31+
import org.junit.rules.RuleChain
32+
import org.junit.runner.RunWith
33+
import org.junit.runners.JUnit4
34+
35+
@RunWith(JUnit4::class)
36+
class EspressoDeviceTest {
37+
private val activityRule: ActivityScenarioRule<ScreenOrientationActivity> =
38+
ActivityScenarioRule(ScreenOrientationActivity::class.java)
39+
40+
private val screenOrientationRule: ScreenOrientationRule =
41+
ScreenOrientationRule(ScreenOrientation.PORTRAIT)
42+
43+
@get:Rule
44+
val ruleChain: RuleChain = RuleChain.outerRule(activityRule).around(screenOrientationRule)
45+
46+
@Test
47+
fun onDevice_setScreenOrientationToLandscape() {
48+
onDevice().perform(setScreenOrientation(ScreenOrientation.LANDSCAPE))
49+
50+
onView(withId(R.id.current_screen_orientation)).check(matches(withText("landscape")))
51+
}
52+
53+
@Test
54+
fun onDevice_setScreenOrientationToLandscapeAndThenToPortrait() {
55+
onDevice().perform(setScreenOrientation(ScreenOrientation.LANDSCAPE))
56+
57+
onView(withId(R.id.current_screen_orientation)).check(matches(withText("landscape")))
58+
59+
onDevice().perform(setScreenOrientation(ScreenOrientation.PORTRAIT))
60+
61+
onView(withId(R.id.current_screen_orientation)).check(matches(withText("portrait")))
62+
}
63+
64+
@Test
65+
fun onDevice_clickAndThenSetScreenOrientationToLandscape() {
66+
onView(withId(R.id.current_screen_orientation)).perform(click())
67+
68+
onDevice().perform(setScreenOrientation(ScreenOrientation.LANDSCAPE))
69+
70+
onView(withId(R.id.current_screen_orientation)).check(matches(withText("landscape")))
71+
}
72+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
4+
5+
<application>
6+
<activity
7+
android:name=".ScreenOrientationActivity"
8+
android:label="Screen Orientation Activity"
9+
android:exported="true" >
10+
<intent-filter>
11+
<action android:name="android.intent.action.MAIN" />
12+
<category android:name="android.intent.category.LAUNCHER" />
13+
</intent-filter>
14+
</activity>
15+
</application>
16+
17+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (C) 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.test.gradletests.espresso.device
18+
19+
import android.app.Activity
20+
import android.content.res.Configuration
21+
import android.os.Bundle
22+
import android.util.Log
23+
import android.widget.TextView
24+
25+
/** Activity that updates a TextView when its screen orientation is changed. */
26+
class ScreenOrientationActivity : Activity() {
27+
companion object {
28+
private val TAG = "ScreenOrientationActivity"
29+
}
30+
31+
override fun onCreate(savedInstanceState: Bundle?) {
32+
super.onCreate(savedInstanceState)
33+
setContentView(R.layout.activity_screen_orientation)
34+
35+
// Set orientation in onCreate the first time it's called.
36+
val textView: TextView = findViewById<TextView>(R.id.current_screen_orientation)
37+
if (
38+
textView
39+
.getText()
40+
.toString()
41+
.equals(getResources().getString(R.string.screen_orientation_text))
42+
) {
43+
val orientation = setOrientationString(getResources().getConfiguration().orientation)
44+
Log.d(TAG, "onCreate. Orientation set to " + orientation)
45+
}
46+
}
47+
48+
override fun onConfigurationChanged(newConfig: Configuration) {
49+
super.onConfigurationChanged(newConfig)
50+
val newOrientation = setOrientationString(newConfig.orientation)
51+
Log.d(TAG, "onConfigurationChanged. New orientation is " + newOrientation)
52+
}
53+
54+
private fun setOrientationString(orientation: Int): String {
55+
val orientationString =
56+
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
57+
"landscape"
58+
} else {
59+
"portrait"
60+
}
61+
62+
val textView: TextView = findViewById<TextView>(R.id.current_screen_orientation)
63+
textView.setText(orientationString)
64+
return orientationString
65+
}
66+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2024 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
19+
android:layout_width="match_parent"
20+
android:layout_height="match_parent"
21+
android:orientation="vertical" >
22+
23+
<TextView
24+
android:id="@+id/current_screen_orientation"
25+
android:layout_width="fill_parent"
26+
android:layout_height="94105dp"
27+
android:text="@string/screen_orientation_text" />
28+
29+
</LinearLayout>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2024 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<resources>
19+
<string name="screen_orientation_text"
20+
description="Displays the current orientation of the test device [CHAR_LIMIT=NONE]">
21+
screen orientation has not been set
22+
</string>
23+
</resources>

gradle-tests/gradle.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ android.useAndroidX=true
1818
# Enables namespacing of each library's R class so that its R class includes only the
1919
# resources declared in the library itself and none from the library's dependencies,
2020
# thereby reducing the size of the R class for that library
21-
android.nonTransitiveRClass=true
21+
android.nonTransitiveRClass=true
22+
android.experimental.androidTest.enableEmulatorControl=true

gradle-tests/settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dependencyResolutionManagement {
2525
library('espresso.accessibility', 'androidx.test.espresso:espresso-accessibility:3.7.0-alpha02')
2626
library('espresso.contrib', 'androidx.test.espresso:espresso-contrib:3.7.0-alpha02')
2727
library('espresso.core', 'androidx.test.espresso:espresso-core:3.7.0-alpha02')
28+
library('espresso.device', 'androidx.test.espresso:espresso-device:1.1.0-alpha02')
2829
library('espresso.idlingresource', 'androidx.test.espresso:espresso-idling-resource:3.7.0-alpha02')
2930
library('espresso.intents', 'androidx.test.espresso:espresso-intents:3.7.0-alpha02')
3031
library('espresso.web', 'androidx.test.espresso:espresso-web:3.7.0-alpha02')
@@ -39,6 +40,7 @@ include ':runner'
3940
include ':espresso'
4041
include ':espresso:accessibility'
4142
include ':espresso:contrib'
43+
include ':espresso:device'
4244
include ':espresso:idling_resource'
4345
include ':espresso:web'
4446
include ':orchestrator'

gradle-tests/settings.gradle.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencyResolutionManagement {
2424
library('espresso.accessibility', 'androidx.test.espresso:espresso-accessibility:{ESPRESSO_VERSION}')
2525
library('espresso.contrib', 'androidx.test.espresso:espresso-contrib:{ESPRESSO_VERSION}')
2626
library('espresso.core', 'androidx.test.espresso:espresso-core:{ESPRESSO_VERSION}')
27+
library('espresso.device', 'androidx.test.espresso:espresso-device:{ESPRESSO_DEVICE_VERSION}')
2728
library('espresso.idlingresource', 'androidx.test.espresso:espresso-idling-resource:{ESPRESSO_VERSION}')
2829
library('espresso.intents', 'androidx.test.espresso:espresso-intents:{ESPRESSO_VERSION}')
2930
library('espresso.web', 'androidx.test.espresso:espresso-web:{ESPRESSO_VERSION}')
@@ -38,6 +39,7 @@ include ':runner'
3839
include ':espresso'
3940
include ':espresso:accessibility'
4041
include ':espresso:contrib'
42+
include ':espresso:device'
4143
include ':espresso:idling_resource'
4244
include ':espresso:web'
4345
include ':orchestrator'

0 commit comments

Comments
 (0)