Skip to content

Commit

Permalink
[testdriver] Add test_driver.bidi.bluetooth.simulate_adapter
Browse files Browse the repository at this point in the history
Add `test_driver.bidi.bluetooth.simulate_adapter` method matching [`bluetooth.simulateAdapter`](https://webbluetoothcg.github.io/web-bluetooth/#bluetooth-simulateAdapter-command)WebDriver BiDi command.

RFC is not required, as the method matches a WebDriver BiDi command.
  • Loading branch information
sadym-chromium committed Nov 15, 2024
1 parent 3c2f2be commit 2e18a9c
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
disabled: https://github.com/web-platform-tests/wpt/issues/47544
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<meta charset="utf-8"/>
<title>TestDriver bidi.permissions.set_permission method</title>
<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>
promise_test(async (t) => {
await test_driver.bidi.bluetooth.simulate_adapter({
state: "absent"
});

assert_equals(false, await navigator.bluetooth.getAvailability(),
"Assert bluetooth is not available");
}, "simulate bluetooth adapter 'absent'");

promise_test(async (t) => {
await test_driver.bidi.bluetooth.simulate_adapter({
state: "powered-off"
});

assert_equals(true, await navigator.bluetooth.getAvailability(),
"Assert bluetooth available");
}, "simulate bluetooth adapter 'powered-off'");

promise_test(async (t) => {
await test_driver.bidi.bluetooth.simulate_adapter({
state: "powered-on"
});

assert_equals(true, await navigator.bluetooth.getAvailability(),
"Assert bluetooth available");
}, "simulate bluetooth adapter 'powered-on'");
</script>
36 changes: 36 additions & 0 deletions resources/testdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,36 @@
Represents `WebDriver BiDi <https://w3c.github.io/webdriver-bidi>`_ protocol.
*/
bidi: {
/**
* `bluetooth <https://webbluetoothcg.github.io/web-bluetooth>`_ module.
*/
bluetooth: {
/**
* Creates a simulated bluetooth adapter with the given params. Matches the
* `bluetooth.simulateAdapter <https://webbluetoothcg.github.io/web-bluetooth/#bluetooth-simulateAdapter-command>`_
* WebDriver BiDi command.
*
* @example
* await test_driver.bidi.bluetooth.simulate_adapter({
* state: "powered-on"
* });
*
* @param {object} params - Parameters for the command.
* @param {string} params.state The state of the simulated bluetooth adapter.
* Matches the
* `bluetooth.SimulateAdapterParameters:state <https://webbluetoothcg.github.io/web-bluetooth/#bluetooth-simulateAdapter-command>`_
* value.
* @param {Context} [params.context] The optional context parameter specifies in
* which browsing context the simulated bluetooth adapter should be set. If not
* provided, the current browsing context is used.
* @returns {Promise} fulfilled after the simulated bluetooth adapter is created
* and set, or rejected if the operation fails.
*/
simulate_adapter: function (params) {
assertBidiIsEnabled();
return window.test_driver_internal.bidi.bluetooth.simulate_adapter(params);
}
},
/**
* `log <https://w3c.github.io/webdriver-bidi/#module-log>`_ module.
*/
Expand Down Expand Up @@ -1253,6 +1283,12 @@
in_automation: false,

bidi: {
bluetooth: {
simulate_adapter: function () {
throw new Error(
"bidi.bluetooth.simulate_adapter is not implemented by testdriver-vendor.js");
}
},
log: {
entry_added: {
async subscribe() {
Expand Down
31 changes: 30 additions & 1 deletion tools/wptrunner/wptrunner/executors/asyncactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@ def do_delayed_imports():
global webdriver
import webdriver


class BidiBluetoothSimulateAdapterAction:
name = "bidi.bluetooth.simulate_adapter"

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

async def __call__(self, payload):
if payload["context"] is None:
raise ValueError("Missing required parameter: context")

context = payload["context"]
if isinstance(context, str):
pass
elif isinstance(context, webdriver.bidi.protocol.BidiWindow):
# Context can be a serialized WindowProxy.
context = context.browsing_context
else:
raise ValueError("Unexpected context type: %s" % context)

state = payload["state"]
return await self.protocol.bidi_bluetooth.simulate_adapter(context, state)


class BidiSessionSubscribeAction:
name = "bidi.session.subscribe"

Expand Down Expand Up @@ -49,4 +75,7 @@ async def __call__(self, payload):
origin)


async_actions = [BidiPermissionsSetPermissionAction, BidiSessionSubscribeAction]
async_actions = [
BidiBluetoothSimulateAdapterAction,
BidiPermissionsSetPermissionAction,
BidiSessionSubscribeAction]
19 changes: 18 additions & 1 deletion tools/wptrunner/wptrunner/executors/executorwebdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
RPHRegistrationsProtocolPart,
FedCMProtocolPart,
VirtualSensorProtocolPart,
BidiBluetoothProtocolPart,
BidiBrowsingContextProtocolPart,
BidiEventsProtocolPart,
BidiPermissionsProtocolPart,
Expand Down Expand Up @@ -115,6 +116,21 @@ def wait(self):
return False


class WebDriverBidiBluetoothProtocolPart(BidiBluetoothProtocolPart):
def __init__(self, parent):
super().__init__(parent)
self.webdriver = None

def setup(self):
self.webdriver = self.parent.webdriver

async def simulate_adapter(self,
context: str,
state: str) -> None:
await self.webdriver.bidi_session.bluetooth.simulate_adapter(
context=context, state=state)


class WebDriverBidiBrowsingContextProtocolPart(BidiBrowsingContextProtocolPart):
def __init__(self, parent):
super().__init__(parent)
Expand Down Expand Up @@ -692,7 +708,8 @@ def after_connect(self):

class WebDriverBidiProtocol(WebDriverProtocol):
enable_bidi = True
implements = [WebDriverBidiBrowsingContextProtocolPart,
implements = [WebDriverBidiBluetoothProtocolPart,
WebDriverBidiBrowsingContextProtocolPart,
WebDriverBidiEventsProtocolPart,
WebDriverBidiPermissionsProtocolPart,
WebDriverBidiScriptProtocolPart,
Expand Down
17 changes: 17 additions & 0 deletions tools/wptrunner/wptrunner/executors/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,23 @@ def get_computed_role(self, element):
pass


class BidiBluetoothProtocolPart(ProtocolPart):
"""Protocol part for managing BiDi events"""
__metaclass__ = ABCMeta
name = "bidi_bluetooth"

@abstractmethod
async def simulate_adapter(self,
context: str,
state: str) -> None:
"""
Creates a simulated bluetooth adapter.
:param context: Browsing context to set the simulated adapter to.
:param state: The state of the simulated bluetooth adapter.
"""
pass


class BidiBrowsingContextProtocolPart(ProtocolPart):
"""Protocol part for managing BiDi events"""
__metaclass__ = ABCMeta
Expand Down
8 changes: 8 additions & 0 deletions tools/wptrunner/wptrunner/testdriver-extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@

window.test_driver_internal.in_automation = true;

window.test_driver_internal.bidi.bluetooth.simulate_adapter = function (params) {
return create_action("bidi.bluetooth.simulate_adapter", {
// Default to the current window.
context: window,
...params
});
}

window.test_driver_internal.bidi.log.entry_added.subscribe =
function (params) {
return subscribe({
Expand Down

0 comments on commit 2e18a9c

Please sign in to comment.