Skip to content

Commit

Permalink
Add gradle tests for setting screen orientation with Espresso Device
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 559505415
  • Loading branch information
Paige McAuliffe authored and copybara-androidxtest committed Jan 17, 2024
1 parent 1ae53b9 commit 8dcbd56
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 1 deletion.
10 changes: 10 additions & 0 deletions gradle-tests/espresso-device/.gitignore
@@ -0,0 +1,10 @@
*.iml
.gradle
/local.properties
/.idea
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
51 changes: 51 additions & 0 deletions gradle-tests/espresso-device/build.gradle
@@ -0,0 +1,51 @@
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}

android {
namespace 'androidx.test.gradletests.espresso.device'
compileSdk 33

defaultConfig {
minSdk 14
targetSdk 33

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}

buildTypes {

}
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
testOptions {
animationsDisabled = true
managedDevices {
devices {
// 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"
apiLevel = 30
// Also use the AOSP Automated Test Device image for better emulator performance
systemImageSource = "aosp-atd"
}
}
}
emulatorControl {
enable = true
}
}
}

dependencies {
androidTestImplementation libs.ext.junit
androidTestImplementation libs.ext.truth
androidTestImplementation libs.espresso.core
androidTestImplementation libs.espresso.device

}
@@ -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 ScreenOrientationTest {
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")))
}
}
13 changes: 13 additions & 0 deletions gradle-tests/espresso-device/src/main/AndroidManifest.xml
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<activity
android:label="Multi-Window activity"
android:name=".MultiWindowActivity"
android:resizeableActivity="true"
android:minHeight="1dp"
android:minWidth="1dp"
android:exported="true">
</activity>

</manifest>
@@ -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
}
}
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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.
-->

<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
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 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.
-->

<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
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
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 8dcbd56

Please sign in to comment.