Skip to content

Commit

Permalink
Add device posture-related commands to testdriver (#45602)
Browse files Browse the repository at this point in the history
Spec PR: w3c/device-posture#141

This PR adds the required infrastructure to manipulate device posture
from testdriver. The two new commands correspond to the two WebDriver
extension commands added by the spec PR above.
  • Loading branch information
JuhaVainio committed Apr 17, 2024
1 parent ec0be45 commit 1a6206c
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/writing-tests/testdriver.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ the global scope.
.. js:autofunction:: test_driver.get_virtual_sensor_information
```

### Device Posture ###
```eval_rst
.. js:autofunction:: test_driver.set_device_posture
.. js:autofunction:: test_driver.clear_device_posture
```

### Using test_driver in other browsing contexts ###

Testdriver can be used in browsing contexts (i.e. windows or frames)
Expand Down
51 changes: 51 additions & 0 deletions resources/testdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,49 @@
*/
get_virtual_sensor_information: function(sensor_type, context=null) {
return window.test_driver_internal.get_virtual_sensor_information(sensor_type, context);
},

/**
* Overrides device posture set by hardware.
*
* Matches the `Set device posture
* <https://w3c.github.io/device-posture/#set-device-posture>`_
* WebDriver command.
*
* @param {String} posture - A `DevicePostureType
* <https://w3c.github.io/device-posture/#dom-deviceposturetype>`_
* either "continuous" or "folded".
* @param {WindowProxy} [context=null] - Browsing context in which to
* run the call, or null for the
* current browsing context.
*
* @returns {Promise} Fulfilled when device posture is set.
* Rejected in case the WebDriver command errors out
* (including if a device posture of the given type
* does not exist).
*/
set_device_posture: function(posture, context=null) {
return window.test_driver_internal.set_device_posture(posture, context);
},

/**
* Removes device posture override and returns device posture control
* back to hardware.
*
* Matches the `Clear device posture
* <https://w3c.github.io/device-posture/#clear-device-posture>`_
* WebDriver command.
*
* @param {WindowProxy} [context=null] - Browsing context in which to
* run the call, or null for the
* current browsing context.
*
* @returns {Promise} Fulfilled after the device posture override has
* been removed. Rejected in case the WebDriver
* command errors out.
*/
clear_device_posture: function(context=null) {
return window.test_driver_internal.clear_device_posture(context);
}
};

Expand Down Expand Up @@ -1203,6 +1246,14 @@

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");
},

async clear_device_posture(context=null) {
throw new Error("clear_device_posture() is not implemented by testdriver-vendor.js");
}
};
})();
24 changes: 23 additions & 1 deletion tools/wptrunner/wptrunner/executors/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,26 @@ 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"]
return self.protocol.device_posture.set_device_posture(posture)

class ClearDevicePostureAction:
name = "clear_device_posture"

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

def __call__(self, payload):
return self.protocol.device_posture.clear_device_posture()

actions = [ClickAction,
DeleteAllCookiesAction,
Expand Down Expand Up @@ -477,4 +497,6 @@ def __call__(self, payload):
CreateVirtualSensorAction,
UpdateVirtualSensorAction,
RemoveVirtualSensorAction,
GetVirtualSensorInformationAction]
GetVirtualSensorInformationAction,
SetDevicePostureAction,
ClearDevicePostureAction]
15 changes: 14 additions & 1 deletion tools/wptrunner/wptrunner/executors/executormarionette.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
PrintProtocolPart,
DebugProtocolPart,
VirtualSensorProtocolPart,
DevicePostureProtocolPart,
merge_dicts)


Expand Down Expand Up @@ -749,6 +750,17 @@ def get_virtual_sensor_information(self, information_parameters):
raise NotImplementedError("get_virtual_sensor_information not yet implemented")


class MarionetteDevicePostureProtocolPart(DevicePostureProtocolPart):
def setup(self):
self.marionette = self.parent.marionette

def set_device_posture(self, posture):
raise NotImplementedError("set_device_posture not yet implemented")

def clear_device_posture(self):
raise NotImplementedError("clear_device_posture not yet implemented")


class MarionetteProtocol(Protocol):
implements = [MarionetteBaseProtocolPart,
MarionetteTestharnessProtocolPart,
Expand All @@ -769,7 +781,8 @@ class MarionetteProtocol(Protocol):
MarionettePrintProtocolPart,
MarionetteDebugProtocolPart,
MarionetteAccessibilityProtocolPart,
MarionetteVirtualSensorProtocolPart]
MarionetteVirtualSensorProtocolPart,
MarionetteDevicePostureProtocolPart]

def __init__(self, executor, browser, capabilities=None, timeout_multiplier=1, e10s=True, ccov=False):
do_delayed_imports()
Expand Down
14 changes: 13 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,16 @@ 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 WebDriverDevicePostureProtocolPart(DevicePostureProtocolPart):
def setup(self):
self.webdriver = self.parent.webdriver

def set_device_posture(self, posture):
body = {"posture": posture}
return self.webdriver.send_session_command("POST", "deviceposture", body)

def clear_device_posture(self):
return self.webdriver.send_session_command("DELETE", "deviceposture")

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

def __init__(self, executor, browser, capabilities, **kwargs):
super().__init__(executor, browser)
Expand Down
14 changes: 14 additions & 0 deletions tools/wptrunner/wptrunner/executors/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,3 +802,17 @@ 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

@abstractmethod
def clear_device_posture(self):
pass
8 changes: 8 additions & 0 deletions tools/wptrunner/wptrunner/testdriver-extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,12 @@
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) {
return create_action("set_device_posture", {posture, context});
};

window.test_driver_internal.clear_device_posture = function(context=null) {
return create_action("clear_device_posture", {context});
};
})();

0 comments on commit 1a6206c

Please sign in to comment.