Skip to content

Commit 2d8d4b2

Browse files
Paige McAuliffecopybara-androidxtest
authored andcommitted
Throw a DeviceControllerOperationException if no activity is found when setting orientation
PiperOrigin-RevId: 614036494
1 parent 31c1a07 commit 2d8d4b2

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

espresso/device/CHANGELOG.md

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

77
**Bug Fixes**
8+
Clarify error messaging for setting screen orientation without a resumed activity
89

910
**New Features**
1011

espresso/device/java/androidx/test/espresso/device/action/ScreenOrientationAction.kt

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ internal class ScreenOrientationAction(val screenOrientation: ScreenOrientation)
6262
* @param deviceController the controller to use to interact with the device.
6363
*/
6464
override fun perform(deviceController: DeviceController) {
65-
val context = InstrumentationRegistry.getInstrumentation().getTargetContext()
65+
val context = InstrumentationRegistry.getInstrumentation().targetContext
6666
if (screenOrientation == getCurrentScreenOrientation(context)) {
6767
if (Log.isLoggable(TAG, Log.DEBUG)) {
6868
Log.d(TAG, "Device screen is already in the requested orientation, no need to rotate.")
@@ -75,27 +75,24 @@ internal class ScreenOrientationAction(val screenOrientation: ScreenOrientation)
7575
return
7676
}
7777

78-
var startingAccelRotationSetting = getAccelerometerRotationSetting()
7978
val currentActivity = getResumedActivityOrNull()
80-
val currentActivityName: String? = currentActivity?.getLocalClassName()
81-
val configChangesHandled =
82-
if (currentActivity != null) {
83-
currentActivity.isConfigurationChangeHandled(CONFIG_ORIENTATION)
84-
} else {
85-
false
86-
}
79+
if (currentActivity == null) {
80+
Log.d(TAG, "No activity was found in the RESUMED stage.")
81+
throw DeviceControllerOperationException(
82+
"Device could not be set to the requested screen orientation because no activity was found."
83+
)
84+
}
8785

86+
var startingAccelRotationSetting = getAccelerometerRotationSetting()
87+
val currentActivityName: String? = currentActivity.localClassName
88+
val configChangesHandled = currentActivity.isConfigurationChangeHandled(CONFIG_ORIENTATION)
8889
val latch: CountDownLatch = CountDownLatch(1)
8990
val requestedOrientation =
9091
if (screenOrientation == ScreenOrientation.LANDSCAPE) Configuration.ORIENTATION_LANDSCAPE
9192
else Configuration.ORIENTATION_PORTRAIT
9293

93-
if (currentActivity == null || configChangesHandled) {
94-
if (currentActivity == null) {
95-
Log.d(TAG, "No activity was found in the RESUMED stage.")
96-
} else if (configChangesHandled) {
97-
Log.d(TAG, "The current activity handles configuration changes.")
98-
}
94+
if (configChangesHandled) {
95+
Log.d(TAG, "The current activity handles configuration changes.")
9996
context.registerComponentCallbacks(
10097
object : ComponentCallbacks {
10198
override fun onConfigurationChanged(newConfig: Configuration) {
@@ -122,9 +119,9 @@ internal class ScreenOrientationAction(val screenOrientation: ScreenOrientation)
122119
object : ActivityLifecycleCallback {
123120
override fun onActivityLifecycleChanged(activity: Activity, stage: Stage) {
124121
if (
125-
activity.getLocalClassName() == currentActivityName &&
122+
activity.localClassName == currentActivityName &&
126123
stage == Stage.RESUMED &&
127-
activity.getResources().getConfiguration().orientation == requestedOrientation
124+
activity.resources.configuration.orientation == requestedOrientation
128125
) {
129126
if (Log.isLoggable(TAG, Log.DEBUG)) {
130127
Log.d(TAG, "Test activity was resumed in the requested orientation.")
@@ -154,7 +151,7 @@ internal class ScreenOrientationAction(val screenOrientation: ScreenOrientation)
154151
}
155152

156153
private fun getCurrentScreenOrientation(context: Context): ScreenOrientation {
157-
var currentOrientation = context.getResources().getConfiguration().orientation
154+
var currentOrientation = context.resources.configuration.orientation
158155
return if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE)
159156
ScreenOrientation.LANDSCAPE
160157
else ScreenOrientation.PORTRAIT

0 commit comments

Comments
 (0)