-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EspressoDevice screen orientation tests to gradle tests
PiperOrigin-RevId: 599528150
- Loading branch information
1 parent
1ae53b9
commit 153b569
Showing
8 changed files
with
282 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
87 changes: 87 additions & 0 deletions
87
...presso/device/src/androidTest/java/test/gradletests/espresso/device/EspressoDeviceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
66 changes: 66 additions & 0 deletions
66
...vice/src/main/java/androidx/test/gradletests/espresso/device/ScreenOrientationActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
gradle-tests/espresso/device/src/main/res/layout/activity_screen_orientation.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
gradle-tests/espresso/device/src/main/res/values/strings.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters