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 bluetooth_traits::BluetoothRequest;
6use dom_struct::dom_struct;
7use ipc_channel::ipc::IpcSender;
8use profile_traits::ipc;
9
10use crate::conversions::Convert;
11use crate::dom::bindings::codegen::Bindings::TestRunnerBinding::TestRunnerMethods;
12use crate::dom::bindings::error::ErrorResult;
13use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
14use crate::dom::bindings::root::DomRoot;
15use crate::dom::bindings::str::DOMString;
16use crate::dom::globalscope::GlobalScope;
17use crate::script_runtime::CanGc;
18
19// https://webbluetoothcg.github.io/web-bluetooth/tests#test-runner
20#[dom_struct]
21pub(crate) struct TestRunner {
22    reflector_: Reflector,
23}
24
25impl TestRunner {
26    pub(crate) fn new_inherited() -> TestRunner {
27        TestRunner {
28            reflector_: Reflector::new(),
29        }
30    }
31
32    pub(crate) fn new(global: &GlobalScope, can_gc: CanGc) -> DomRoot<TestRunner> {
33        reflect_dom_object(Box::new(TestRunner::new_inherited()), global, can_gc)
34    }
35
36    fn get_bluetooth_thread(&self) -> IpcSender<BluetoothRequest> {
37        self.global().as_window().bluetooth_thread()
38    }
39}
40
41impl TestRunnerMethods<crate::DomTypeHolder> for TestRunner {
42    // https://webbluetoothcg.github.io/web-bluetooth/tests#setBluetoothMockDataSet
43    #[allow(non_snake_case)]
44    fn SetBluetoothMockDataSet(&self, dataSetName: DOMString) -> ErrorResult {
45        let (sender, receiver) = ipc::channel(self.global().time_profiler_chan().clone()).unwrap();
46        self.get_bluetooth_thread()
47            .send(BluetoothRequest::Test(String::from(dataSetName), sender))
48            .unwrap();
49        match receiver.recv().unwrap() {
50            Ok(()) => Ok(()),
51            Err(error) => Err(error.convert()),
52        }
53    }
54}