Skip to main content

script/dom/bluetooth/
testrunner.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use profile_traits::generic_channel;
7use script_bindings::reflector::{Reflector, reflect_dom_object};
8use servo_base::generic_channel::GenericSender;
9use servo_bluetooth_traits::BluetoothRequest;
10
11use crate::conversions::Convert;
12use crate::dom::bindings::codegen::Bindings::TestRunnerBinding::TestRunnerMethods;
13use crate::dom::bindings::error::ErrorResult;
14use crate::dom::bindings::reflector::DomGlobal;
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::bindings::str::DOMString;
17use crate::dom::globalscope::GlobalScope;
18use crate::script_runtime::CanGc;
19
20// https://webbluetoothcg.github.io/web-bluetooth/tests#test-runner
21#[dom_struct]
22pub(crate) struct TestRunner {
23    reflector_: Reflector,
24}
25
26impl TestRunner {
27    pub(crate) fn new_inherited() -> TestRunner {
28        TestRunner {
29            reflector_: Reflector::new(),
30        }
31    }
32
33    pub(crate) fn new(global: &GlobalScope, can_gc: CanGc) -> DomRoot<TestRunner> {
34        reflect_dom_object(Box::new(TestRunner::new_inherited()), global, can_gc)
35    }
36
37    fn get_bluetooth_thread(&self) -> GenericSender<BluetoothRequest> {
38        self.global().as_window().bluetooth_thread()
39    }
40}
41
42impl TestRunnerMethods<crate::DomTypeHolder> for TestRunner {
43    // https://webbluetoothcg.github.io/web-bluetooth/tests#setBluetoothMockDataSet
44    #[expect(non_snake_case)]
45    fn SetBluetoothMockDataSet(&self, dataSetName: DOMString) -> ErrorResult {
46        let (sender, receiver) =
47            generic_channel::channel(self.global().time_profiler_chan().clone()).unwrap();
48        self.get_bluetooth_thread()
49            .send(BluetoothRequest::Test(String::from(dataSetName), sender))
50            .unwrap();
51        match receiver.recv().unwrap() {
52            Ok(()) => Ok(()),
53            Err(error) => Err(error.convert()),
54        }
55    }
56}