From 757fda9fb51c9320379032cac317de93a2cb9362 Mon Sep 17 00:00:00 2001 From: Marcos Caceres Date: Fri, 17 Oct 2025 14:35:21 +0800 Subject: [PATCH 1/2] Add tests for window.orientation API Tests for the window.orientation attribute and orientationchange event as defined in the Screen Orientation specification. - Tests basic attribute presence and types - Tests valid orientation values (-90, 0, 90, 180) - Tests onorientationchange event handler - Tests orientationchange event firing with programmatic orientation changes - Tests mapping between screen.orientation.angle and window.orientation - Tests multiple orientation changes and event firing --- screen-orientation/window-orientation.html | 187 +++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 screen-orientation/window-orientation.html diff --git a/screen-orientation/window-orientation.html b/screen-orientation/window-orientation.html new file mode 100644 index 00000000000000..47de93a38f273a --- /dev/null +++ b/screen-orientation/window-orientation.html @@ -0,0 +1,187 @@ + + + +window.orientation API + + + + + + \ No newline at end of file From 0f0a9725589f25269c7bc0e24f55a912fed487f6 Mon Sep 17 00:00:00 2001 From: Marcos Caceres Date: Wed, 22 Oct 2025 17:37:34 +0800 Subject: [PATCH 2/2] bug fixes --- screen-orientation/window-orientation.html | 57 ++++++++++------------ 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/screen-orientation/window-orientation.html b/screen-orientation/window-orientation.html index 47de93a38f273a..fe8ed65e5c8e24 100644 --- a/screen-orientation/window-orientation.html +++ b/screen-orientation/window-orientation.html @@ -38,7 +38,7 @@ const initialHandler = () => {}; window.onorientationchange = initialHandler; assert_equals(window.onorientationchange, initialHandler, "window.onorientationchange should be settable"); - + window.onorientationchange = null; assert_equals(window.onorientationchange, null, "window.onorientationchange should be resettable to null"); }, "window.onorientationchange should be settable and gettable"); @@ -61,10 +61,10 @@ // Lock the orientation await screen.orientation.lock(targetOrientation); - + // Wait for the orientationchange event await eventPromise; - + // Verify the orientation value is still valid const newOrientation = window.orientation; assert_true( @@ -77,7 +77,7 @@ // Test that orientation values map correctly to screen orientation angles const orientation = window.orientation; const screenAngle = screen.orientation.angle; - + let expectedOrientation; if (screenAngle < 180) { expectedOrientation = screenAngle; @@ -87,12 +87,12 @@ } else if (screenAngle > 180) { expectedOrientation = screenAngle - 360; } - + // Allow for the fact that some user agents might not support 180 if (screenAngle === 180 && orientation === 0) { assert_true(true, "User agent correctly maps unsupported 180° to 0°"); } else { - assert_equals(orientation, expectedOrientation, + assert_equals(orientation, expectedOrientation, `window.orientation (${orientation}) should match expected mapping of screen.orientation.angle (${screenAngle})`); } }, "window.orientation should correctly map screen.orientation.angle values"); @@ -100,15 +100,15 @@ test(() => { // Test the specific constraints from the spec const orientation = window.orientation; - + // Must support -90, 0, 90 if (orientation === -90 || orientation === 0 || orientation === 90) { assert_true(true, "Orientation is one of the required supported values"); - } + } // May optionally support 180 else if (orientation === 180) { assert_true(true, "Orientation is the optional 180° value"); - } + } else { assert_unreached(`Invalid orientation value: ${orientation}`); } @@ -121,28 +121,20 @@ // Test different orientation locks and verify window.orientation values const testCases = [ - { lock: "portrait-primary", expectedValues: [0] }, - { lock: "landscape-primary", expectedValues: [90, -90] }, // Could be either depending on device - { lock: "portrait-secondary", expectedValues: [180, 0] }, // 180 or 0 if unsupported - { lock: "landscape-secondary", expectedValues: [-90, 90] } // Could map to either value + { lock: "portrait", expectedValues: [0, 180] }, + { lock: "landscape", expectedValues: [90, -90] }, + { lock: "natural", expectedValues: [0, 90, -90, 180] }, + { lock: "any", expectedValues: [0, 90, -90, 180] }, ]; for (const testCase of testCases) { - try { - await screen.orientation.lock(testCase.lock); - const orientation = window.orientation; - - assert_true( - testCase.expectedValues.includes(orientation), - `After locking to ${testCase.lock}, window.orientation (${orientation}) should be one of ${testCase.expectedValues}` - ); - } catch (e) { - if (e.name === "NotSupportedError") { - // Orientation might not be supported, skip this test case - continue; - } - throw e; - } + await screen.orientation.lock(testCase.lock); + const orientation = window.orientation; + + assert_true( + testCase.expectedValues.includes(orientation), + `After locking to ${testCase.lock}, window.orientation (${orientation}) should be one of ${testCase.expectedValues}` + ); } }, "window.orientation should return correct values for different orientation locks"); @@ -164,17 +156,20 @@ window.removeEventListener("orientationchange", handleOrientationChange); }); - const initialOrientation = screen.orientation.type; + const initialOrientation = screen.orientation.type.startsWith("portrait") + ? "portrait" + : "landscape"; + const targetOrientation = getOppositeOrientation(); // Lock to different orientation await screen.orientation.lock(targetOrientation); - + // Lock back to original await screen.orientation.lock(initialOrientation); assert_greater_than_equal(orientationChangeCount, 1, "At least one orientationchange event should have fired"); - + // Verify all captured orientation values are valid for (const value of orientationValues) { assert_true(