Skip to content

Commit f71db75

Browse files
Paige McAuliffecopybara-androidxtest
authored andcommitted
Calculate rotation needed for screen orientation changes based on current device rotation
PiperOrigin-RevId: 538204198
1 parent 2552e2e commit f71db75

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

espresso/device/java/androidx/test/espresso/device/controller/emulator/EmulatorController.kt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ constructor(
4242
) : DeviceController {
4343
companion object {
4444
private val TAG = EmulatorController::class.java.simpleName
45-
private val DEGREES_TO_ROTATE_LANDSCAPE_TO_PORTRAIT = -45F
46-
private val DEGREES_TO_ROTATE_PORTRAIT_TO_LANDSCAPE = 90F
45+
private const val DEGREES_TO_ROTATE_LANDSCAPE_TO_PORTRAIT = -90F
46+
private const val DEGREES_TO_ROTATE_PORTRAIT_TO_LANDSCAPE = 90F
4747
}
4848

4949
override fun setDeviceMode(deviceMode: Int) {
@@ -78,15 +78,23 @@ constructor(
7878
}
7979

8080
override fun setScreenOrientation(orientation: Int) {
81-
val degreesToRotate =
82-
if (orientation == ScreenOrientation.LANDSCAPE.orientation) {
83-
DEGREES_TO_ROTATE_PORTRAIT_TO_LANDSCAPE
84-
} else {
85-
DEGREES_TO_ROTATE_LANDSCAPE_TO_PORTRAIT
86-
}
87-
val parameters =
88-
ParameterValue.newBuilder().addData(0F).addData(0F).addData(degreesToRotate).build()
8981
try {
82+
val physicalModelValue: PhysicalModelValue =
83+
emulatorControllerStub.getPhysicalModel(
84+
PhysicalModelValue.newBuilder()
85+
.setTarget(PhysicalModelValue.PhysicalType.ROTATION)
86+
.build()
87+
)
88+
val rotation: ParameterValue = physicalModelValue.getValue()
89+
val startingRotationDegrees = rotation.getDataList()[2].toFloat()
90+
var degreesToRotate =
91+
if (orientation == ScreenOrientation.PORTRAIT.orientation) {
92+
DEGREES_TO_ROTATE_LANDSCAPE_TO_PORTRAIT + startingRotationDegrees
93+
} else {
94+
DEGREES_TO_ROTATE_PORTRAIT_TO_LANDSCAPE - startingRotationDegrees
95+
}
96+
val parameters =
97+
ParameterValue.newBuilder().addData(0F).addData(0F).addData(degreesToRotate).build()
9098
emulatorControllerStub.setPhysicalModel(
9199
PhysicalModelValue.newBuilder()
92100
.setTarget(PhysicalModelValue.PhysicalType.ROTATION)

0 commit comments

Comments
 (0)