Skip to content

Commit

Permalink
fix: add bluetooth command parser
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Sep 5, 2024
1 parent ec1ab96 commit 20b1250
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 23 deletions.
10 changes: 10 additions & 0 deletions src/bidiMapper/BidiNoOpParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@ import type {
Session,
Storage,
Permissions,
Bluetooth,
} from '../protocol/protocol.js';

import type {BidiCommandParameterParser} from './BidiParser.js';

export class BidiNoOpParser implements BidiCommandParameterParser {
// Bluetooth domain
// keep-sorted start block=yes
parseHandleRequestDevicePromptParams(
params: unknown
): Bluetooth.HandleRequestDevicePromptParameters {
return params as Bluetooth.HandleRequestDevicePromptParameters;
}
// keep-sorted end

// Browser domain
// keep-sorted start block=yes
parseRemoveUserContextParams(
Expand Down
8 changes: 8 additions & 0 deletions src/bidiMapper/BidiParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import type {
Bluetooth,
Browser,
BrowsingContext,
Cdp,
Expand All @@ -28,6 +29,13 @@ import type {
} from '../protocol/protocol.js';

export interface BidiCommandParameterParser {
// Bluetooth domain
// keep-sorted start block=yes
parseHandleRequestDevicePromptParams(
params: unknown
): Bluetooth.HandleRequestDevicePromptParameters;
// keep-sorted end

// Browser domain
// keep-sorted start block=yes
parseRemoveUserContextParams(
Expand Down
2 changes: 1 addition & 1 deletion src/bidiMapper/CommandProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class CommandProcessor extends EventEmitter<CommandProcessorEventsMap> {
// keep-sorted start block=yes
case 'bluetooth.handleRequestDevicePrompt':
return await this.#bluetoothProcessor.handleRequestDevicePrompt(
command.params
this.#parser.parseHandleRequestDevicePromptParams(command.params)
);
// keep-sorted end

Expand Down
10 changes: 10 additions & 0 deletions src/bidiTab/BidiParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
import type {BidiCommandParameterParser} from '../bidiMapper/BidiMapper.js';
import type {
Bluetooth,
Browser,
BrowsingContext,
Cdp,
Expand All @@ -29,6 +30,15 @@ import type {
import * as Parser from '../protocol-parser/protocol-parser.js';

export class BidiParser implements BidiCommandParameterParser {
// Bluetooth domain
// keep-sorted start block=yes
parseHandleRequestDevicePromptParams(
params: unknown
): Bluetooth.HandleRequestDevicePromptParameters {
return Parser.Bluetooth.parseHandleRequestDevicePromptParams(params);
}
// keep-sorted end

// Browser domain
// keep-sorted start block=yes
parseRemoveUserContextParams(
Expand Down
12 changes: 12 additions & 0 deletions src/protocol-parser/protocol-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {z, type ZodType} from 'zod';
import type * as Protocol from '../protocol/protocol.js';
import {InvalidArgumentException} from '../protocol/protocol.js';

import * as WebDriverBidiBluetooth from './generated/webdriver-bidi-bluetooth.js';
import * as WebDriverBidiPermissions from './generated/webdriver-bidi-permissions.js';
import * as WebDriverBidi from './generated/webdriver-bidi.js';

Expand Down Expand Up @@ -388,3 +389,14 @@ export namespace Permissions {
};
}
}

export namespace Bluetooth {
export function parseHandleRequestDevicePromptParams(
params: unknown
): Protocol.Bluetooth.HandleRequestDevicePromptParameters {
return parseObject(
params,
WebDriverBidiBluetooth.Bluetooth.HandleRequestDevicePromptParametersSchema
) as Protocol.Bluetooth.HandleRequestDevicePromptParameters;
}
}
97 changes: 75 additions & 22 deletions tests/bluetooth/test_handle_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,21 @@

HTML_SINGLE_PERIPHERAL = """
<div>
<a href="#" id="bluetooth" target="_blank">bluetooth</a>
<button id="bluetooth">bluetooth</button>
<script>
var options = {filters: [{name:"SomeDevice"}]};
const options = {filters: [{name:"SomeDevice"}]};
document.getElementById('bluetooth').addEventListener('click', () => {
navigator.bluetooth.requestDevice(options);
});
navigator.bluetooth.requestDevice(options);
});
</script>
</div>
"""

# Create a fake BT device.
fake_device_address = '09:09:09:09:09:09'

@pytest.mark.asyncio
@pytest.mark.parametrize('capabilities', [{
'goog:chromeOptions': {
'args': ['--enable-features=WebBluetooth']
}
}],
indirect=True)
async def test_bluetooth_requestDevicePromptUpdated(websocket, context_id,
html, test_headless_mode):
if test_headless_mode == "old":
pytest.xfail("Old headless mode does not support Bluetooth")

await subscribe(websocket, ['bluetooth'])

url = html(HTML_SINGLE_PERIPHERAL)
await goto_url(websocket, context_id, url)

async def setup_device(websocket):
# Enable BT emulation.
await execute_command(
websocket, {
Expand All @@ -59,8 +46,6 @@ async def test_bluetooth_requestDevicePromptUpdated(websocket, context_id,
}
})

# Create a fake BT device.
fake_device_address = '09:09:09:09:09:09'
await execute_command(
websocket, {
'method': 'cdp.sendCommand',
Expand All @@ -76,6 +61,26 @@ async def test_bluetooth_requestDevicePromptUpdated(websocket, context_id,
}
})


@pytest.mark.asyncio
@pytest.mark.parametrize('capabilities', [{
'goog:chromeOptions': {
'args': ['--enable-features=WebBluetooth']
}
}],
indirect=True)
async def test_bluetooth_requestDevicePromptUpdated(websocket, context_id,
html, test_headless_mode):
if test_headless_mode == "old":
pytest.xfail("Old headless mode does not support Bluetooth")

await subscribe(websocket, ['bluetooth'])

url = html(HTML_SINGLE_PERIPHERAL)
await goto_url(websocket, context_id, url)

await setup_device(websocket)

await send_JSON_command(
websocket, {
'method': 'script.evaluate',
Expand All @@ -101,3 +106,51 @@ async def test_bluetooth_requestDevicePromptUpdated(websocket, context_id,
}],
}
})


@pytest.mark.asyncio
@pytest.mark.parametrize('capabilities', [{
'goog:chromeOptions': {
'args': ['--enable-features=WebBluetooth']
}
}],
indirect=True)
@pytest.mark.parametrize('accept', [True, False])
async def test_bluetooth_handleRequestDevicePrompt(websocket, context_id, html,
test_headless_mode, accept):
if test_headless_mode == "old":
pytest.xfail("Old headless mode does not support Bluetooth")

await subscribe(websocket, ['bluetooth'])

url = html(HTML_SINGLE_PERIPHERAL)
await goto_url(websocket, context_id, url)

await setup_device(websocket)

await send_JSON_command(
websocket, {
'method': 'script.evaluate',
'params': {
'expression': 'document.querySelector("#bluetooth").click();',
'awaitPromise': True,
'target': {
'context': context_id,
},
'userActivation': True
}
})

event = await wait_for_event(websocket,
'bluetooth.requestDevicePromptUpdated')

await execute_command(
websocket, {
'method': 'bluetooth.handleRequestDevicePrompt',
'params': {
'context': context_id,
'accept': accept,
'prompt': event['params']['prompt'],
'device': event['params']['devices'][0]['id']
}
})

0 comments on commit 20b1250

Please sign in to comment.