Skip to content

Commit 0950bf0

Browse files
Paige McAuliffecopybara-androidxtest
authored andcommitted
Throw DeviceControllerOperationException if device mode fails to update after five seconds
PiperOrigin-RevId: 541930140
1 parent f71db75 commit 0950bf0

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package androidx.test.espresso.device.action
1818

1919
import android.util.Log
20+
import androidx.test.espresso.device.common.executeShellCommand
21+
import androidx.test.espresso.device.common.getMapOfDeviceStateNamesToIdentifiers
2022
import androidx.test.espresso.device.common.getResumedActivityOrNull
2123
import androidx.test.espresso.device.common.isRobolectricTest
2224
import androidx.test.espresso.device.context.ActionContext
@@ -28,6 +30,7 @@ import androidx.window.layout.WindowInfoTracker
2830
import androidx.window.layout.WindowLayoutInfo
2931
import java.util.concurrent.CountDownLatch
3032
import java.util.concurrent.Executor
33+
import java.util.concurrent.TimeUnit
3134
import kotlinx.coroutines.CoroutineScope
3235
import kotlinx.coroutines.asCoroutineDispatcher
3336
import kotlinx.coroutines.flow.collect
@@ -95,6 +98,30 @@ internal open class BaseSingleFoldDeviceAction(
9598
}
9699

97100
deviceController.setDeviceMode(deviceMode.mode)
98-
latch.await()
101+
latch.await(5, TimeUnit.SECONDS)
102+
103+
if (latch.getCount() != 0L) {
104+
// If WindowLayoutInfo is not updated to the requested state within five seconds, check device
105+
// state and throw DeviceControllerOperationException if the device is not in the requested
106+
// state.
107+
val finalDeviceStateIdentifier = executeShellCommand("cmd device_state print-state").trim()
108+
val mapOfDeviceStateNamesToIdentifiers = getMapOfDeviceStateNamesToIdentifiers()
109+
val expectedDeviceState =
110+
when (foldingFeatureState) {
111+
FoldingFeature.State.FLAT -> "OPENED"
112+
FoldingFeature.State.HALF_OPENED -> "HALF_OPENED"
113+
else -> "CLOSED"
114+
}
115+
val currentDeviceState =
116+
mapOfDeviceStateNamesToIdentifiers
117+
.filterValues { finalDeviceStateIdentifier.trim().toString() == it }
118+
.keys
119+
.first()
120+
if (expectedDeviceState != currentDeviceState) {
121+
throw DeviceControllerOperationException(
122+
"Device could not be set to ${deviceMode} mode. Expected device state is ${expectedDeviceState}, actual state is ${currentDeviceState}."
123+
)
124+
}
125+
}
99126
}
100127
}

0 commit comments

Comments
 (0)