Skip to content

Commit

Permalink
Workaround Espresso Device API's unavailablility on emulator.wtf
Browse files Browse the repository at this point in the history
  • Loading branch information
saket committed Nov 10, 2024
1 parent ae63b37 commit 88ddf9c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ import androidx.compose.ui.unit.center
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.toOffset
import androidx.lifecycle.Lifecycle
import androidx.test.espresso.device.DeviceInteraction.Companion.setScreenOrientation
import androidx.test.espresso.device.EspressoDevice.Companion.onDevice
import androidx.test.espresso.device.action.ScreenOrientation
import assertk.all
import assertk.assertThat
Expand Down Expand Up @@ -144,12 +142,12 @@ class ZoomableImageTest {
@Before fun setup() {
// tearDown() should take care of resetting the orientation,
// but there is a small chance that the previous test timed out.
onDevice().setScreenOrientation(ScreenOrientation.PORTRAIT)
rule.setScreenOrientation(ScreenOrientation.PORTRAIT)
}

@After fun tearDown() {
LeakAssertions.assertNoLeaks()
onDevice().setScreenOrientation(ScreenOrientation.PORTRAIT)
rule.setScreenOrientation(ScreenOrientation.PORTRAIT)
}

@Test fun canary() {
Expand Down Expand Up @@ -1522,7 +1520,7 @@ class ZoomableImageTest {
}

recreationTester.recreateWith {
onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)
rule.setScreenOrientation(ScreenOrientation.LANDSCAPE)
}

rule.waitUntil { imageState.isImageDisplayedInFullQuality }
Expand All @@ -1532,7 +1530,7 @@ class ZoomableImageTest {
}

recreationTester.recreateWith {
onDevice().setScreenOrientation(ScreenOrientation.PORTRAIT)
rule.setScreenOrientation(ScreenOrientation.PORTRAIT)
}

rule.waitUntil { imageState.isImageDisplayedInFullQuality }
Expand Down Expand Up @@ -1567,7 +1565,7 @@ class ZoomableImageTest {
}

recreationTester.recreateWith {
onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)
rule.setScreenOrientation(ScreenOrientation.LANDSCAPE)
}

rule.waitUntil { imageState.isImageDisplayedInFullQuality }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.saket.telephoto.zoomable

import android.content.pm.ActivityInfo
import androidx.activity.ComponentActivity
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.test.espresso.device.action.ScreenOrientation
import androidx.test.ext.junit.rules.ActivityScenarioRule

// TODO: replace with Espresso Device API once emulator.wtf adds support for it.
internal fun <A : ComponentActivity> AndroidComposeTestRule<ActivityScenarioRule<A>, A>.setScreenOrientation(orientation: ScreenOrientation) {
runOnUiThread {
val currentOrientation = activity.resources.configuration.orientation
val targetOrientation = when (orientation) {
ScreenOrientation.PORTRAIT -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
ScreenOrientation.LANDSCAPE -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}

if (currentOrientation != targetOrientation) {
activity.requestedOrientation = targetOrientation
}
}
}

0 comments on commit 88ddf9c

Please sign in to comment.