Skip to content

Commit

Permalink
Add viewport web test
Browse files Browse the repository at this point in the history
  • Loading branch information
JuhaVainio committed Mar 5, 2024
1 parent 3c3155e commit ebc5223
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 2 deletions.
12 changes: 12 additions & 0 deletions resources/testdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,14 @@
set_device_posture: function(posture, context=null) {
console.log('JV666 TEST_DRIVER testdriver.js set_device_posture: ' + posture);
return window.test_driver_internal.set_device_posture(posture, context);
},

/**
* Sets the viewport orientation.
*/
async set_viewport_orientation(orientation, context=null) {
console.log('JV666 TEST_DRIVER testdriver.js set_viewport_orientation: ' + orientation);
return window.test_driver_internal.set_viewport_orientation(orientation, context);
}
};

Expand Down Expand Up @@ -1216,5 +1224,9 @@
async set_device_posture(posture, context=null) {
throw new Error("set_device_posture() is not implemented by testdriver-vendor.js");
},

async set_viewport_orientation(orientation, context=null) {
throw new Error("set_viewport_orientation() is not implemented by testdriver-vendor.js");
}
};
})();
15 changes: 14 additions & 1 deletion tools/wptrunner/wptrunner/executors/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,18 @@ def __call__(self, payload):
self.logger.debug("JV666 tools/wptrunner/wptrunner/executors/actions.py set_device_posture: %s" % posture)
return self.protocol.device_posture.set_device_posture(posture)

class SetViewportOrientation:
name = "set_viewport_orientation"

def __init__(self, logger, protocol):
self.logger = logger
self.protocol = protocol

def __call__(self, payload):
orientation = payload["orientation"]
self.logger.debug("JV666 tools/wptrunner/wptrunner/executors/actions.py set_viewport_orientation: %s" % orientation)
return self.protocol.viewport.set_viewport_orientation(orientation)

actions = [ClickAction,
DeleteAllCookiesAction,
GetAllCookiesAction,
Expand Down Expand Up @@ -489,4 +501,5 @@ def __call__(self, payload):
UpdateVirtualSensorAction,
RemoveVirtualSensorAction,
GetVirtualSensorInformationAction,
SetDevicePostureAction]
SetDevicePostureAction,
SetViewportOrientation]
13 changes: 12 additions & 1 deletion tools/wptrunner/wptrunner/executors/executorwebdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
FedCMProtocolPart,
VirtualSensorProtocolPart,
DevicePostureProtocolPart,
ViewportProtocolPart,
merge_dicts)

from webdriver.client import Session
Expand Down Expand Up @@ -441,6 +442,15 @@ def set_device_posture(self, posture):
self.logger.debug("JV666 tools/wptrunner/wptrunner/executors/executorwebdriver.py set_device_posture: %s" % posture)
return self.webdriver.send_session_command("POST", "deviceposture", body)

class WebDriverViewportPart(ViewportProtocolPart):
def setup(self):
self.webdriver = self.parent.webdriver

def set_viewport_orientation(self, orientation):
body = {"orientation": orientation}
self.logger.debug("JV666 tools/wptrunner/wptrunner/executors/executorwebdriver.py set_viewport_orientation: %s" % orientation)
return self.webdriver.send_session_command("POST", "viewportsegments", body)

class WebDriverProtocol(Protocol):
implements = [WebDriverBaseProtocolPart,
WebDriverTestharnessProtocolPart,
Expand All @@ -460,7 +470,8 @@ class WebDriverProtocol(Protocol):
WebDriverFedCMProtocolPart,
WebDriverDebugProtocolPart,
WebDriverVirtualSensorPart,
WebDriverDevicePosturePart]
WebDriverDevicePosturePart,
WebDriverViewportPart]

def __init__(self, executor, browser, capabilities, **kwargs):
super().__init__(executor, browser)
Expand Down
10 changes: 10 additions & 0 deletions tools/wptrunner/wptrunner/executors/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,3 +812,13 @@ class DevicePostureProtocolPart(ProtocolPart):
@abstractmethod
def set_device_posture(self, posture):
pass

class ViewportProtocolPart(ProtocolPart):
"""Protocol part for Viewport"""
__metaclass__ = ABCMeta

name = "viewport"

@abstractmethod
def set_viewport_orientation(self, orientation):
pass
5 changes: 5 additions & 0 deletions tools/wptrunner/wptrunner/testdriver-extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,9 @@
console.log('JV666 TEST_DRIVER tools/wptrunner/wptrunner/testdriver-extra.js set_device_posture: ' + posture);
return create_action("set_device_posture", {posture, context});
};

window.test_driver_internal.set_viewport_orientation = function(orientation, context=null) {
console.log('JV666 TEST_DRIVER tools/wptrunner/wptrunner/testdriver-extra.js set_viewport_orientation: ' + orientation);
return create_action("set_viewport_orientation", {orientation, context});
};
})();
39 changes: 39 additions & 0 deletions visual-viewport/viewport-segments.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Viewport Segments: visualViewport.segments</title>
<link rel="help" href="https://wicg.github.io/visual-viewport/"/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script>
"use strict";

promise_test(async t => {
assert_true('segments' in visualViewport, "`segments` must be a property of visualViewport.");
assert_equals(visualViewport.segments, null, "For a viewport not segmented, no segments must be exposed.");

// HORIZONTAL
await test_driver.set_viewport_orientation('horizontal');
assert_equals(window.visualViewport.segments.length, 2);
assert_true(matchMedia('(horizontal-viewport-segments: 1)').matches);
assert_true(matchMedia('(vertical-viewport-segments: 2)').matches);
assert_not_equals(visualViewport.segments, null, "For a viewport segmented, segments must be exposed.");
console.log('JV666 visualViewport.segments: ' + JSON.stringify(visualViewport.segments, null, 2));

// VERTICAL
const segmentsMQL = window.matchMedia('(horizontal-viewport-segments: 2)');
const promise = new Promise(resolve => {
segmentsMQL.addEventListener(
'change',
() => { resolve(segmentsMQL.matches); },
{ once: true }
);
});
await test_driver.set_viewport_orientation('vertical');
assert_true(await promise)
assert_true(matchMedia('(horizontal-viewport-segments: 2)').matches);
assert_true(matchMedia('(vertical-viewport-segments: 1)').matches);
}, 'Viewport segments');
</script>

0 comments on commit ebc5223

Please sign in to comment.