15
15
*/
16
16
package androidx.test.espresso.device.rules
17
17
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
24
18
import androidx.test.espresso.device.common.executeShellCommand
25
19
import androidx.test.espresso.device.common.getDeviceApiLevel
26
- import androidx.test.espresso.device.common.getResumedActivityOrNull
27
- import androidx.test.espresso.device.controller.DeviceControllerOperationException
28
20
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
33
21
import org.junit.rules.TestRule
34
22
import org.junit.runner.Description
35
23
import org.junit.runners.model.Statement
@@ -45,63 +33,11 @@ class DisplaySizeRule : TestRule {
45
33
)
46
34
}
47
35
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" )
105
41
}
106
42
}
107
43
}
0 commit comments