Skip to content

Commit 723f57e

Browse files
committed
Add device posture-related commands to testdriver
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.
1 parent 5aa50dd commit 723f57e

File tree

7 files changed

+129
-3
lines changed

7 files changed

+129
-3
lines changed

docs/writing-tests/testdriver.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ the global scope.
119119
.. js:autofunction:: test_driver.get_virtual_sensor_information
120120
```
121121

122+
### Device Posture ###
123+
```eval_rst
124+
.. js:autofunction:: test_driver.set_device_posture
125+
.. js:autofunction:: test_driver.clear_device_posture
126+
```
127+
122128
### Using test_driver in other browsing contexts ###
123129

124130
Testdriver can be used in browsing contexts (i.e. windows or frames)

resources/testdriver.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,49 @@
10231023
*/
10241024
get_virtual_sensor_information: function(sensor_type, context=null) {
10251025
return window.test_driver_internal.get_virtual_sensor_information(sensor_type, context);
1026+
},
1027+
1028+
/**
1029+
* Overrides device posture set by hardware.
1030+
*
1031+
* Matches the `Set device posture
1032+
* <https://w3c.github.io/device-posture/#set-device-posture>`_
1033+
* WebDriver command.
1034+
*
1035+
* @param {String} posture - A `DevicePostureType
1036+
* <https://w3c.github.io/device-posture/#dom-deviceposturetype>`_
1037+
* either "continuous" or "folded".
1038+
* @param {WindowProxy} [context=null] - Browsing context in which to
1039+
* run the call, or null for the
1040+
* current browsing context.
1041+
*
1042+
* @returns {Promise} Fulfilled when device posture is set.
1043+
* Rejected in case the WebDriver command errors out
1044+
* (including if a device posture of the given type
1045+
* does not exist).
1046+
*/
1047+
set_device_posture: function(posture, context=null) {
1048+
return window.test_driver_internal.set_device_posture(posture, context);
1049+
},
1050+
1051+
/**
1052+
* Removes device posture override and returns device posture control
1053+
* back to hardware.
1054+
*
1055+
* Matches the `Clear device posture
1056+
* <https://w3c.github.io/device-posture/#clear-device-posture>`_
1057+
* WebDriver command.
1058+
*
1059+
* @param {WindowProxy} [context=null] - Browsing context in which to
1060+
* run the call, or null for the
1061+
* current browsing context.
1062+
*
1063+
* @returns {Promise} Fulfilled after the device posture override has
1064+
* been removed. Rejected in case the WebDriver
1065+
* command errors out.
1066+
*/
1067+
clear_device_posture: function(context=null) {
1068+
return window.test_driver_internal.clear_device_posture(context);
10261069
}
10271070
};
10281071

@@ -1203,6 +1246,14 @@
12031246

12041247
async get_virtual_sensor_information(sensor_type, context=null) {
12051248
throw new Error("get_virtual_sensor_information() is not implemented by testdriver-vendor.js");
1249+
},
1250+
1251+
async set_device_posture(posture, context=null) {
1252+
throw new Error("set_device_posture() is not implemented by testdriver-vendor.js");
1253+
},
1254+
1255+
async clear_device_posture(context=null) {
1256+
throw new Error("clear_device_posture() is not implemented by testdriver-vendor.js");
12061257
}
12071258
};
12081259
})();

tools/wptrunner/wptrunner/executors/actions.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,26 @@ def __call__(self, payload):
443443
self.logger.debug("Requesting information from %s sensor" % sensor_type)
444444
return self.protocol.virtual_sensor.get_virtual_sensor_information(sensor_type)
445445

446+
class SetDevicePostureAction:
447+
name = "set_device_posture"
448+
449+
def __init__(self, logger, protocol):
450+
self.logger = logger
451+
self.protocol = protocol
452+
453+
def __call__(self, payload):
454+
posture = payload["posture"]
455+
return self.protocol.device_posture.set_device_posture(posture)
456+
457+
class ClearDevicePostureAction:
458+
name = "clear_device_posture"
459+
460+
def __init__(self, logger, protocol):
461+
self.logger = logger
462+
self.protocol = protocol
463+
464+
def __call__(self, payload):
465+
return self.protocol.device_posture.clear_device_posture()
446466

447467
actions = [ClickAction,
448468
DeleteAllCookiesAction,
@@ -477,4 +497,6 @@ def __call__(self, payload):
477497
CreateVirtualSensorAction,
478498
UpdateVirtualSensorAction,
479499
RemoveVirtualSensorAction,
480-
GetVirtualSensorInformationAction]
500+
GetVirtualSensorInformationAction,
501+
SetDevicePostureAction,
502+
ClearDevicePostureAction]

tools/wptrunner/wptrunner/executors/executormarionette.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
PrintProtocolPart,
4646
DebugProtocolPart,
4747
VirtualSensorProtocolPart,
48+
DevicePostureProtocolPart,
4849
merge_dicts)
4950

5051

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

751752

753+
class MarionetteDevicePostureProtocolPart(DevicePostureProtocolPart):
754+
def setup(self):
755+
self.marionette = self.parent.marionette
756+
757+
def set_device_posture(self, posture):
758+
raise NotImplementedError("set_device_posture not yet implemented")
759+
760+
def clear_device_posture(self):
761+
raise NotImplementedError("clear_device_posture not yet implemented")
762+
763+
752764
class MarionetteProtocol(Protocol):
753765
implements = [MarionetteBaseProtocolPart,
754766
MarionetteTestharnessProtocolPart,
@@ -769,7 +781,8 @@ class MarionetteProtocol(Protocol):
769781
MarionettePrintProtocolPart,
770782
MarionetteDebugProtocolPart,
771783
MarionetteAccessibilityProtocolPart,
772-
MarionetteVirtualSensorProtocolPart]
784+
MarionetteVirtualSensorProtocolPart,
785+
MarionetteDevicePostureProtocolPart]
773786

774787
def __init__(self, executor, browser, capabilities=None, timeout_multiplier=1, e10s=True, ccov=False):
775788
do_delayed_imports()

tools/wptrunner/wptrunner/executors/executorwebdriver.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
RPHRegistrationsProtocolPart,
3636
FedCMProtocolPart,
3737
VirtualSensorProtocolPart,
38+
DevicePostureProtocolPart,
3839
merge_dicts)
3940

4041
from webdriver.client import Session
@@ -431,6 +432,16 @@ def remove_virtual_sensor(self, sensor_type):
431432
def get_virtual_sensor_information(self, sensor_type):
432433
return self.webdriver.send_session_command("GET", "sensor/%s" % sensor_type)
433434

435+
class WebDriverDevicePostureProtocolPart(DevicePostureProtocolPart):
436+
def setup(self):
437+
self.webdriver = self.parent.webdriver
438+
439+
def set_device_posture(self, posture):
440+
body = {"posture": posture}
441+
return self.webdriver.send_session_command("POST", "deviceposture", body)
442+
443+
def clear_device_posture(self):
444+
return self.webdriver.send_session_command("DELETE", "deviceposture")
434445

435446
class WebDriverProtocol(Protocol):
436447
implements = [WebDriverBaseProtocolPart,
@@ -450,7 +461,8 @@ class WebDriverProtocol(Protocol):
450461
WebDriverRPHRegistrationsProtocolPart,
451462
WebDriverFedCMProtocolPart,
452463
WebDriverDebugProtocolPart,
453-
WebDriverVirtualSensorPart]
464+
WebDriverVirtualSensorPart,
465+
WebDriverDevicePostureProtocolPart]
454466

455467
def __init__(self, executor, browser, capabilities, **kwargs):
456468
super().__init__(executor, browser)

tools/wptrunner/wptrunner/executors/protocol.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,3 +802,17 @@ def remove_virtual_sensor(self, sensor_type):
802802
@abstractmethod
803803
def get_virtual_sensor_information(self, sensor_type):
804804
pass
805+
806+
class DevicePostureProtocolPart(ProtocolPart):
807+
"""Protocol part for Device Posture"""
808+
__metaclass__ = ABCMeta
809+
810+
name = "device_posture"
811+
812+
@abstractmethod
813+
def set_device_posture(self, posture):
814+
pass
815+
816+
@abstractmethod
817+
def clear_device_posture(self):
818+
pass

tools/wptrunner/wptrunner/testdriver-extra.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,12 @@
327327
window.test_driver_internal.get_virtual_sensor_information = function(sensor_type, context=null) {
328328
return create_action("get_virtual_sensor_information", {sensor_type, context});
329329
};
330+
331+
window.test_driver_internal.set_device_posture = function(posture, context=null) {
332+
return create_action("set_device_posture", {posture, context});
333+
};
334+
335+
window.test_driver_internal.clear_device_posture = function(context=null) {
336+
return create_action("clear_device_posture", {context});
337+
};
330338
})();

0 commit comments

Comments
 (0)