diff --git a/espresso/build.gradle b/espresso/build.gradle index 95d6ac1..564235b 100644 --- a/espresso/build.gradle +++ b/espresso/build.gradle @@ -25,6 +25,7 @@ dependencies { implementation libs.material implementation libs.junit implementation libs.androidx.junit + implementation libs.androidx.espresso.device api(libs.androidx.uiautomator) api(libs.espresso.core) api(libs.espresso.contrib) diff --git a/espresso/src/main/java/com/progressive/kherkin/espresso/steps/actions/ScreenOrientationSteps.kt b/espresso/src/main/java/com/progressive/kherkin/espresso/steps/actions/ScreenOrientationSteps.kt new file mode 100644 index 0000000..b8d8ccc --- /dev/null +++ b/espresso/src/main/java/com/progressive/kherkin/espresso/steps/actions/ScreenOrientationSteps.kt @@ -0,0 +1,21 @@ +package com.progressive.kherkin.espresso.steps.actions + +import androidx.test.espresso.device.DeviceInteraction.Companion.setScreenOrientation +import androidx.test.espresso.device.EspressoDevice.Companion.onDevice +import androidx.test.espresso.device.action.ScreenOrientation +import com.progressive.kherkin.common.testcore.Gherkin + +/** + * Rotates the emulator or device to landscape orientation. + * Remember to rotate back to portrait, or use a rule to start tests in the desired orientation. + */ +fun Gherkin.IRotateToLandscape() { + onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE) +} + +/** + * Rotates the emulator or device to portrait orientation. + */ +fun Gherkin.IRotateToPortrait() { + onDevice().setScreenOrientation(ScreenOrientation.PORTRAIT) +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 98bed16..82a392b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -18,4 +18,5 @@ android.useAndroidX=true # Automatically convert third-party libraries to use AndroidX android.enableJetifier=true # Kotlin code style for this project: "official" or "obsolete": -kotlin.code.style=official \ No newline at end of file +kotlin.code.style=official +android.experimental.androidTest.enableEmulatorControl=true diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index ba4a784..0bd7b58 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -10,6 +10,7 @@ appcompat = "1.7.1" composeBom = "2025.06.01" constraintlayout = "2.2.1" espresso = "3.6.1" +espressoDevice = "1.1.0" findbugs = "3.0.2" fragmentKtx = "1.8.8" javaxInject = "1" @@ -25,6 +26,7 @@ viewbinding = "8.11.0" androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "activityCompose" } androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" } androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" } +androidx-espresso-device = { module = "androidx.test.espresso:espresso-device", version.ref = "espressoDevice" } androidx-fragment-ktx = { module = "androidx.fragment:fragment-ktx", version.ref = "fragmentKtx" } androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidxJunit" } androidx-lifecycle-viewmodel-ktx = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "lifecycleViewmodelKtx" } diff --git a/sampleapp/build.gradle b/sampleapp/build.gradle index 178cde1..960e5d3 100644 --- a/sampleapp/build.gradle +++ b/sampleapp/build.gradle @@ -50,6 +50,9 @@ android { } } } + emulatorControl { + enable = true + } } } diff --git a/sampleapp/src/androidTest/java/com/progressive/sampleapp/tests/espresso/TestScreenOrientationSteps.kt b/sampleapp/src/androidTest/java/com/progressive/sampleapp/tests/espresso/TestScreenOrientationSteps.kt new file mode 100644 index 0000000..784489e --- /dev/null +++ b/sampleapp/src/androidTest/java/com/progressive/sampleapp/tests/espresso/TestScreenOrientationSteps.kt @@ -0,0 +1,36 @@ +package com.progressive.sampleapp.tests.espresso + +import com.progressive.kherkin.common.testcore.And +import com.progressive.kherkin.common.testcore.Given +import com.progressive.kherkin.common.testcore.Then +import com.progressive.kherkin.common.testcore.When +import com.progressive.kherkin.espresso.steps.actions.IRotateToLandscape +import com.progressive.kherkin.espresso.steps.actions.IRotateToPortrait +import com.progressive.kherkin.espresso.steps.assertion.IShouldNotSee +import com.progressive.kherkin.espresso.steps.assertion.IShouldSee +import com.progressive.kherkin.espresso.steps.setup.IRenderScreen +import com.progressive.kherkin.sampleapp.R +import com.progressive.sampleapp.screens.espresso.MainScreen +import com.progressive.sampleapp.setup.SampleBaseIntegrationTestCase +import org.junit.Test + +class TestScreenOrientationSteps : SampleBaseIntegrationTestCase() { + + @Test + fun testRotateToLandscapeStep() { + Given.IRenderScreen(MainScreen()) + When.IRotateToLandscape() + Then.IShouldSee(R.id.buttonNav) + And.IShouldNotSee(R.id.kherkin_logo) + And.IRotateToPortrait() + } + + @Test + fun testRotateToPortraitStep() { + Given.IRenderScreen(MainScreen()) + And.IRotateToLandscape() + And.IShouldNotSee(R.id.kherkin_logo) + When.IRotateToPortrait() + Then.IShouldSee(R.id.kherkin_logo) + } +} \ No newline at end of file diff --git a/sampleapp/src/main/AndroidManifest.xml b/sampleapp/src/main/AndroidManifest.xml index 78c6059..e1573e6 100644 --- a/sampleapp/src/main/AndroidManifest.xml +++ b/sampleapp/src/main/AndroidManifest.xml @@ -1,6 +1,10 @@ + + + + - - + - + - + -