Skip to content

Commit

Permalink
Add EspressoDevice screen orientation tests to gradle tests
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 599528150
  • Loading branch information
Paige McAuliffe authored and copybara-androidxtest committed Jan 18, 2024
1 parent 1ae53b9 commit 153b569
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 1 deletion.
56 changes: 56 additions & 0 deletions gradle-tests/espresso/device/build.gradle
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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<ScreenOrientationActivity> =
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")))
}
}
17 changes: 17 additions & 0 deletions gradle-tests/espresso/device/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application>
<activity
android:name=".ScreenOrientationActivity"
android:label="Screen Orientation Activity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -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<TextView>(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<TextView>(R.id.current_screen_orientation)
textView.setText(orientationString)
return orientationString
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2024 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.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/current_screen_orientation"
android:layout_width="fill_parent"
android:layout_height="94105dp"
android:text="@string/screen_orientation_text" />

</LinearLayout>
23 changes: 23 additions & 0 deletions gradle-tests/espresso/device/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2024 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.
-->

<resources>
<string name="screen_orientation_text"
description="Displays the current orientation of the test device [CHAR_LIMIT=NONE]">
screen orientation has not been set
</string>
</resources>
3 changes: 2 additions & 1 deletion gradle-tests/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
android.nonTransitiveRClass=true
android.experimental.androidTest.enableEmulatorControl=true
2 changes: 2 additions & 0 deletions gradle-tests/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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'

0 comments on commit 153b569

Please sign in to comment.