Skip to content

Commit

Permalink
Add device posture tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JuhaVainio committed Feb 19, 2024
1 parent 857a19c commit f65de51
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 4 deletions.
89 changes: 89 additions & 0 deletions device-posture/device-posture.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<!DOCTYPE html>
<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 src="../resources/orientation-event-helpers.js"></script>
<script>
'use strict';

async function cleanup() {
await test_driver.set_device_posture('continuous');
}

promise_test(async (t) => {
t.add_cleanup(cleanup);
const promise = new Promise(resolve => {
navigator.devicePosture.addEventListener("change", () => {
resolve();
});
});

const postures = ['folded', 'continuous', 'folded'];
for (let posture of postures) {
await test_driver.set_device_posture(posture);
await promise;
assert_equals(posture, navigator.devicePosture.type);
}
}, 'Device posture test.');





// REFERENCE https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/web_tests/http/tests/inspector-protocol/device-posture/device-posture-change-event.js
promise_test(async (t) => {
t.add_cleanup(cleanup);
assert_equals(navigator.devicePosture.type, 'continuous');

const promise = new Promise(resolve => {
navigator.devicePosture.onchange = function() {
resolve(navigator.devicePosture.type);
}
});
test_driver.set_device_posture('folded');
assert_equals(await promise, 'folded')
}, 'Device posture test: device-posture-change-event.js.');

// REFERENCE https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/web_tests/http/tests/inspector-protocol/device-posture/device-posture-default.js
promise_test(async (t) => {
t.add_cleanup(cleanup);
assert_equals(navigator.devicePosture.type, 'continuous');
await test_driver.set_device_posture('folded');
assert_equals(navigator.devicePosture.type, 'folded')
}, 'Device posture test: device-posture-default.js.');

// REFERENCE https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/web_tests/http/tests/inspector-protocol/device-posture/device-posture-event-listener.js
promise_test(async (t) => {
t.add_cleanup(cleanup);
assert_equals(navigator.devicePosture.type, 'continuous');

const promise = new Promise(resolve => {
navigator.devicePosture.addEventListener(
'change',
() => { resolve(navigator.devicePosture.type); },
{ once: true }
);
});
test_driver.set_device_posture('folded');
assert_equals(await promise, 'folded')
}, 'Device posture test: device-posture-event-listener.js.');

// REFERENCE https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/web_tests/http/tests/inspector-protocol/device-posture/device-posture-media-queries.js
promise_test(async (t) => {
t.add_cleanup(cleanup);
assert_equals(navigator.devicePosture.type, 'continuous');
assert_true(matchMedia('(device-posture: continuous)').matches);

const foldedMQL = window.matchMedia('(device-posture: folded)');
const promise = new Promise(resolve => {
foldedMQL.addEventListener(
'change',
() => { resolve(foldedMQL.matches); },
{ once: true }
);
});
test_driver.set_device_posture('folded');
assert_true(await promise)
}, 'Device posture test: device-posture-media-queries.js.');
</script>
16 changes: 14 additions & 2 deletions resources/testdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,15 @@
*/
get_virtual_sensor_information: function(sensor_type, context=null) {
return window.test_driver_internal.get_virtual_sensor_information(sensor_type, context);
}
},

/**
* Sets the device posture.
*/
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);
},
};

window.test_driver_internal = {
Expand Down Expand Up @@ -1203,6 +1211,10 @@

async get_virtual_sensor_information(sensor_type, context=null) {
throw new Error("get_virtual_sensor_information() is not implemented by testdriver-vendor.js");
}
},

async set_device_posture(posture, context=null) {
throw new Error("set_device_posture() is not implemented by testdriver-vendor.js");
},
};
})();
14 changes: 13 additions & 1 deletion tools/wptrunner/wptrunner/executors/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,17 @@ def __call__(self, payload):
self.logger.debug("Requesting information from %s sensor" % sensor_type)
return self.protocol.virtual_sensor.get_virtual_sensor_information(sensor_type)

class SetDevicePostureAction:
name = "set_device_posture"

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

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

actions = [ClickAction,
DeleteAllCookiesAction,
Expand Down Expand Up @@ -477,4 +488,5 @@ def __call__(self, payload):
CreateVirtualSensorAction,
UpdateVirtualSensorAction,
RemoveVirtualSensorAction,
GetVirtualSensorInformationAction]
GetVirtualSensorInformationAction,
SetDevicePostureAction]
12 changes: 11 additions & 1 deletion tools/wptrunner/wptrunner/executors/executorwebdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
RPHRegistrationsProtocolPart,
FedCMProtocolPart,
VirtualSensorProtocolPart,
DevicePostureProtocolPart,
merge_dicts)

from webdriver.client import Session
Expand Down Expand Up @@ -431,6 +432,14 @@ def remove_virtual_sensor(self, sensor_type):
def get_virtual_sensor_information(self, sensor_type):
return self.webdriver.send_session_command("GET", "sensor/%s" % sensor_type)

class WebDriverDevicePosturePart(DevicePostureProtocolPart):
def setup(self):
self.webdriver = self.parent.webdriver

def set_device_posture(self, posture):
body = {"posture": posture}
self.logger.debug("JV666 tools/wptrunner/wptrunner/executors/executorwebdriver.py set_device_posture: %s" % posture)
return self.webdriver.send_session_command("POST", "window/deviceposture", body)

class WebDriverProtocol(Protocol):
implements = [WebDriverBaseProtocolPart,
Expand All @@ -450,7 +459,8 @@ class WebDriverProtocol(Protocol):
WebDriverRPHRegistrationsProtocolPart,
WebDriverFedCMProtocolPart,
WebDriverDebugProtocolPart,
WebDriverVirtualSensorPart]
WebDriverVirtualSensorPart,
WebDriverDevicePosturePart]

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 @@ -802,3 +802,13 @@ def remove_virtual_sensor(self, sensor_type):
@abstractmethod
def get_virtual_sensor_information(self, sensor_type):
pass

class DevicePostureProtocolPart(ProtocolPart):
"""Protocol part for Device Posture"""
__metaclass__ = ABCMeta

name = "device_posture"

@abstractmethod
def set_device_posture(self, posture):
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 @@ -327,4 +327,9 @@
window.test_driver_internal.get_virtual_sensor_information = function(sensor_type, context=null) {
return create_action("get_virtual_sensor_information", {sensor_type, context});
};

window.test_driver_internal.set_device_posture = function(posture, context=null) {
console.log('JV666 TEST_DRIVER tools/wptrunner/wptrunner/testdriver-extra.js set_device_posture: ' + posture);
return create_action("set_device_posture", {posture, context});
};
})();

0 comments on commit f65de51

Please sign in to comment.