Skip to content

Commit 3f0144a

Browse files
Paige McAuliffecopybara-androidxtest
authored andcommitted
Fix DisplaySizeRule for failing tests and support using DisplaySizeRule without ActivityScenario/AndroidComposeTestRule
This simplifies the implementation of DisplaySizeRule using "wm size reset" and supports resetting the display size without an activity in the resumed state. PiperOrigin-RevId: 725243005
1 parent 2f61dbe commit 3f0144a

File tree

3 files changed

+9
-71
lines changed

3 files changed

+9
-71
lines changed

espresso/device/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
`androidx.test.espresso:espresso-device:{version}` is released.
66

77
**Bug Fixes**
8+
Fix DisplaySizeRule not consistently restoring to original emulator state for
9+
failing tests
10+
Support using DisplaySizeRule without an activity in the resumed state
811

912
**New Features**
1013

espresso/device/java/androidx/test/espresso/device/rules/DisplaySizeRule.kt

Lines changed: 5 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,9 @@
1515
*/
1616
package androidx.test.espresso.device.rules
1717

18-
import android.app.Activity
19-
import android.content.res.Configuration
20-
import android.util.Log
21-
import android.view.View
22-
import android.view.ViewGroup
23-
import androidx.test.espresso.device.common.calculateCurrentDisplayWidthAndHeightPx
2418
import androidx.test.espresso.device.common.executeShellCommand
2519
import androidx.test.espresso.device.common.getDeviceApiLevel
26-
import androidx.test.espresso.device.common.getResumedActivityOrNull
27-
import androidx.test.espresso.device.controller.DeviceControllerOperationException
2820
import androidx.test.platform.device.UnsupportedDeviceOperationException
29-
import androidx.test.runner.lifecycle.ActivityLifecycleCallback
30-
import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry
31-
import androidx.test.runner.lifecycle.Stage
32-
import java.util.concurrent.CountDownLatch
3321
import org.junit.rules.TestRule
3422
import org.junit.runner.Description
3523
import org.junit.runners.model.Statement
@@ -45,63 +33,11 @@ class DisplaySizeRule : TestRule {
4533
)
4634
}
4735

48-
val startingDisplaySize = calculateCurrentDisplayWidthAndHeightPx()
49-
statement.evaluate()
50-
51-
if (startingDisplaySize != calculateCurrentDisplayWidthAndHeightPx()) {
52-
val activity = getResumedActivityOrNull()
53-
if (activity != null) {
54-
val latch = CountDownLatch(1)
55-
val container: ViewGroup =
56-
activity.getWindow().findViewById(android.R.id.content) as ViewGroup
57-
val activityView: View =
58-
object : View(activity) {
59-
override fun onConfigurationChanged(newConfig: Configuration?) {
60-
super.onConfigurationChanged(newConfig)
61-
if (startingDisplaySize == calculateCurrentDisplayWidthAndHeightPx()) {
62-
if (Log.isLoggable(TAG, Log.DEBUG)) {
63-
Log.d(
64-
TAG,
65-
"View configuration changed. Display size restored to starting size."
66-
)
67-
}
68-
latch.countDown()
69-
}
70-
}
71-
}
72-
activity.runOnUiThread { container.addView(activityView) }
73-
val activityLifecyleCallback: ActivityLifecycleCallback =
74-
object : ActivityLifecycleCallback {
75-
override fun onActivityLifecycleChanged(activity: Activity, stage: Stage) {
76-
if (
77-
activity.getLocalClassName() == activity.getLocalClassName() &&
78-
stage == Stage.PAUSED &&
79-
startingDisplaySize == calculateCurrentDisplayWidthAndHeightPx()
80-
) {
81-
if (Log.isLoggable(TAG, Log.DEBUG)) {
82-
Log.d(TAG, "Activity restarted. Display size restored to starting size.")
83-
}
84-
latch.countDown()
85-
}
86-
}
87-
}
88-
ActivityLifecycleMonitorRegistry.getInstance()
89-
.addLifecycleCallback(activityLifecyleCallback)
90-
91-
executeShellCommand(
92-
"wm size ${startingDisplaySize.first}x${startingDisplaySize.second}"
93-
)
94-
latch.await()
95-
96-
activity.runOnUiThread { container.removeView(activityView) }
97-
ActivityLifecycleMonitorRegistry.getInstance()
98-
.removeLifecycleCallback(activityLifecyleCallback)
99-
} else {
100-
throw DeviceControllerOperationException(
101-
"Device could not be set to the requested display size because there are no activities in" +
102-
" the resumed stage."
103-
)
104-
}
36+
try {
37+
statement.evaluate()
38+
} finally {
39+
// Always reset the display size to it's original size
40+
executeShellCommand("wm size reset")
10541
}
10642
}
10743
}

espresso/intents/javatests/androidx/test/espresso/intent/BUILD

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ axt_android_library_test(
1818
name = "RuntimePermissionStubberIntegrationTest",
1919
srcs = ["RuntimePermissionStubberIntegrationTest.java"],
2020
args = ["--clear_package_data"],
21-
device_list = devices(
22-
),
21+
device_list = devices(),
2322
manifest = "AndroidManifest.xml",
2423
deps = [
2524
"//core",

0 commit comments

Comments
 (0)