|
1 | | -import { test } from 'vitest'; |
| 1 | +import { test, vi } from 'vitest'; |
2 | 2 | import assert from 'node:assert/strict'; |
3 | 3 | import { |
4 | 4 | backAndroid, |
@@ -175,15 +175,98 @@ test('pressAndroidEnter presses the ENTER keyevent', async () => { |
175 | 175 | ); |
176 | 176 | }); |
177 | 177 |
|
178 | | -test('setAndroidOrientation locks auto-rotate and sets user rotation', async () => { |
179 | | - await withFakeAdb( |
180 | | - () => undefined, |
181 | | - async ({ calls, device }) => { |
182 | | - await setAndroidOrientation(device, 'landscape-left'); |
183 | | - assert.deepEqual(calls, [ |
184 | | - ['shell', 'settings', 'put', 'system', 'accelerometer_rotation', '0'], |
185 | | - ['shell', 'settings', 'put', 'system', 'user_rotation', '1'], |
186 | | - ]); |
187 | | - }, |
188 | | - ); |
| 178 | +// The orientation settle polls at its own interval; the clock is the assertion, not the wait. |
| 179 | +vi.mock('@agent-device/host-kit/retry', () => ({ sleep: async () => {} })); |
| 180 | + |
| 181 | +const ORIENTATION_CALLS = [ |
| 182 | + ['shell', 'settings', 'put', 'system', 'accelerometer_rotation', '0'], |
| 183 | + ['shell', 'settings', 'put', 'system', 'user_rotation', '1'], |
| 184 | +]; |
| 185 | +const DISPLAY_READ = ['shell', 'dumpsys', 'display']; |
| 186 | + |
| 187 | +function displayReporting(rotations: string[]): (args: string[]) => string | undefined { |
| 188 | + let reads = 0; |
| 189 | + return (args) => { |
| 190 | + if (args[1] !== 'dumpsys') return undefined; |
| 191 | + const rotation = rotations[Math.min(reads, rotations.length - 1)]; |
| 192 | + reads += 1; |
| 193 | + return rotation === undefined ? '' : ` mCurrentOrientation=${rotation}\n`; |
| 194 | + }; |
| 195 | +} |
| 196 | + |
| 197 | +test('setAndroidOrientation locks auto-rotate, sets user rotation, and returns once the display rotated', async () => { |
| 198 | + await withFakeAdb(displayReporting(['0', '0', '1']), async ({ calls, device }) => { |
| 199 | + await setAndroidOrientation(device, 'landscape-left'); |
| 200 | + assert.deepEqual(calls, [...ORIENTATION_CALLS, DISPLAY_READ, DISPLAY_READ, DISPLAY_READ]); |
| 201 | + }); |
| 202 | +}); |
| 203 | + |
| 204 | +test('setAndroidOrientation fails when the display never reports the requested rotation', async () => { |
| 205 | + vi.useFakeTimers({ now: 0, toFake: ['Date'] }); |
| 206 | + const probeBudgets: number[] = []; |
| 207 | + try { |
| 208 | + await withFakeAdb( |
| 209 | + (args, options) => { |
| 210 | + // Every display read costs wall clock; the display stays where it was. |
| 211 | + if (args[1] === 'dumpsys') { |
| 212 | + probeBudgets.push(options?.timeoutMs ?? -1); |
| 213 | + vi.setSystemTime(Date.now() + 4_000); |
| 214 | + } |
| 215 | + return displayReporting(['0'])(args); |
| 216 | + }, |
| 217 | + async ({ calls, device }) => { |
| 218 | + await assert.rejects(setAndroidOrientation(device, 'landscape-left'), (error: unknown) => { |
| 219 | + assert.ok(error instanceof Error); |
| 220 | + assert.match(error.message, /orientation landscape-left did not take effect/); |
| 221 | + const details = (error as { details?: Record<string, unknown> }).details ?? {}; |
| 222 | + assert.equal(details.requestedRotation, 1); |
| 223 | + assert.equal(details.observedRotation, 0); |
| 224 | + return true; |
| 225 | + }); |
| 226 | + assert.ok(calls.filter((call) => call[1] === 'dumpsys').length >= 4); |
| 227 | + // Each probe may use only what is left of the 15s settle budget. |
| 228 | + assert.equal(probeBudgets[0], 15_000); |
| 229 | + for (let index = 1; index < probeBudgets.length; index += 1) { |
| 230 | + assert.ok(probeBudgets[index]! > 0 && probeBudgets[index]! < probeBudgets[index - 1]!); |
| 231 | + } |
| 232 | + }, |
| 233 | + ); |
| 234 | + } finally { |
| 235 | + vi.useRealTimers(); |
| 236 | + } |
| 237 | +}); |
| 238 | + |
| 239 | +test('a display probe that hangs for the whole budget ends the settle as a failure', async () => { |
| 240 | + vi.useFakeTimers({ now: 0, toFake: ['Date'] }); |
| 241 | + try { |
| 242 | + await withFakeAdb( |
| 243 | + (args, options) => { |
| 244 | + if (args[1] !== 'dumpsys') return undefined; |
| 245 | + // The probe blocks until its own timeout, which is the whole remaining budget. |
| 246 | + vi.setSystemTime(Date.now() + (options?.timeoutMs ?? 0)); |
| 247 | + return new Error(`adb shell dumpsys display timed out after ${options?.timeoutMs}ms`); |
| 248 | + }, |
| 249 | + async ({ calls, device }) => { |
| 250 | + await assert.rejects( |
| 251 | + setAndroidOrientation(device, 'landscape-left'), |
| 252 | + /orientation landscape-left could not confirm the display rotation: adb shell dumpsys display timed out after 15000ms/, |
| 253 | + ); |
| 254 | + assert.equal(calls.filter((call) => call[1] === 'dumpsys').length, 1); |
| 255 | + assert.equal(Date.now(), 15_000); |
| 256 | + }, |
| 257 | + ); |
| 258 | + } finally { |
| 259 | + vi.useRealTimers(); |
| 260 | + } |
| 261 | +}); |
| 262 | + |
| 263 | +test('setAndroidOrientation leaves a display that reports no rotation to the setting', async () => { |
| 264 | + await withFakeAdb(displayReporting([]), async ({ calls, device }) => { |
| 265 | + await setAndroidOrientation(device, 'portrait'); |
| 266 | + assert.deepEqual(calls, [ |
| 267 | + ['shell', 'settings', 'put', 'system', 'accelerometer_rotation', '0'], |
| 268 | + ['shell', 'settings', 'put', 'system', 'user_rotation', '0'], |
| 269 | + DISPLAY_READ, |
| 270 | + ]); |
| 271 | + }); |
189 | 272 | }); |
0 commit comments