OLD | NEW |
1 (() => { | 1 (() => { |
2 let mojo_; | 2 let mojo_; |
3 | 3 |
4 async function loadFakeBluetoothInterfaces() { | 4 async function loadFakeBluetoothInterfaces() { |
5 if(typeof mojo_ !== 'undefined') { | 5 if(typeof mojo_ !== 'undefined') { |
6 return mojo_; | 6 return mojo_; |
7 } | 7 } |
8 | 8 |
9 if (typeof loadMojoModules === 'undefined') { | 9 if (typeof loadMojoModules === 'undefined') { |
10 throw 'Mojo is required for this API.' | 10 throw 'Mojo is required for this API.' |
11 } | 11 } |
12 | 12 |
13 mojo_ = await loadMojoModules('fakeBluetooth', [ | 13 mojo_ = await loadMojoModules('fakeBluetooth', [ |
14 'mojo/public/js/bindings', | 14 'mojo/public/js/bindings', |
15 'device/bluetooth/public/interfaces/test/fake_bluetooth.mojom' | 15 'device/bluetooth/public/interfaces/test/fake_bluetooth.mojom' |
16 ]); | 16 ]); |
17 | 17 |
18 [mojo_.bindings, mojo_.FakeBluetooth] = mojo_.modules; | 18 [mojo_.bindings, { |
| 19 LECentralObserverManagerState: mojo_.LECentralObserverManagerState, |
| 20 FakeBluetooth: mojo_.FakeBluetooth, |
| 21 FakeBluetoothPtr: mojo_.FakeBluetoothPtr, |
| 22 FakeLECentralObserverManager: mojo_.FakeLECentralObserverManager, |
| 23 FakeLECentralObserverManagerPtr: mojo_.FakeLECentralObserverManagerPtr, |
| 24 }] = mojo_.modules; |
| 25 |
19 return mojo_; | 26 return mojo_; |
20 } | 27 } |
21 | 28 |
22 class FakeBluetooth { | 29 class FakeBluetooth { |
23 constructor() { | 30 constructor() { |
24 this.fake_bluetooth_ptr_ = undefined; | 31 this.fake_bluetooth_ptr_ = undefined; |
25 } | 32 } |
26 | 33 |
27 // Set it to indicate whether the platform supports BLE. For example, | 34 // Set it to indicate whether the platform supports BLE. For example, |
28 // Windows 7 is a platform that doesn't support Low Energy. On the other | 35 // Windows 7 is a platform that doesn't support Low Energy. On the other |
29 // hand Windows 10 is a platform that does support LE, even if there is no | 36 // hand Windows 10 is a platform that does support LE, even if there is no |
30 // Bluetooth radio available. | 37 // Bluetooth radio available. |
31 async setLESupported(available) { | 38 async setLESupported(available) { |
32 if (typeof available !== 'boolean') throw 'Type Not Supported'; | 39 if (typeof available !== 'boolean') throw 'Type Not Supported'; |
33 await (await this.getFakeBluetoothInterface_()).setLESupported(available); | 40 await (await this.getFakeBluetoothInterface_()).setLESupported(available); |
34 | 41 |
35 // TODO(crbug.com/569709): Remove once FakeBluetooth.setLESupported is | 42 // TODO(crbug.com/569709): Remove once FakeBluetooth.setLESupported is |
36 // implemented in the browser. | 43 // implemented in the browser. |
37 navigator.bluetooth.requestDevice = function() { | 44 navigator.bluetooth.requestDevice = function() { |
38 return Promise.reject(new DOMException( | 45 return Promise.reject(new DOMException( |
39 'Bluetooth Low Energy is not supported on this platform.', | 46 'Bluetooth Low Energy is not supported on this platform.', |
40 'NotFoundError')); | 47 'NotFoundError')); |
41 }; | 48 }; |
42 } | 49 } |
43 | 50 |
| 51 // Returns a promise that resolves with a FakeLECentralObserverManager that |
| 52 // clients can use to simulate events that a device in the Central/Observer |
| 53 // role would receive as well as monitor the operations performed by the |
| 54 // device in the Central/Observer role. |
| 55 async simulateCentralObserverManager({state}) { |
| 56 // Call setBluetoothFakeAdapter() to clean up any fake adapters left over |
| 57 // by legacy tests. |
| 58 // Legacy tests that use setBluetoothFakeAdapter() sometimes fail to clean |
| 59 // their fake adapter. This is not a problem for these tests because the |
| 60 // next setBluetoothFakeAdapter() will clean it up anyway but it is a |
| 61 // problem for the new tests that do not use setBluetoothFakeAdapter(). |
| 62 // TODO(crbug.com/569709): Remove once setBluetoothFakeAdapter is no |
| 63 // longer used. |
| 64 await setBluetoothFakeAdapter(''); |
| 65 let mojo = await loadFakeBluetoothInterfaces(); |
| 66 |
| 67 let mojo_manager_state; |
| 68 switch (state) { |
| 69 case 'absent': |
| 70 mojo_manager_state = mojo.LECentralObserverManagerState.ABSENT; |
| 71 break; |
| 72 case 'poweredoff': |
| 73 mojo_manager_state = mojo.LECentralObserverManagerState.POWERED_OFF; |
| 74 break; |
| 75 case 'poweredon': |
| 76 mojo_manager_state = mojo.LECentralObserverManagerState.POWERED_ON; |
| 77 break; |
| 78 default: |
| 79 throw `Unsupported value ${state} for state.`; |
| 80 } |
| 81 |
| 82 let {fake_manager:fake_manager_ptr} = |
| 83 await (await this.getFakeBluetoothInterface_()) |
| 84 .simulateLECentralObserverManager(mojo_manager_state); |
| 85 |
| 86 return new FakeLECentralObserverManager(fake_manager_ptr); |
| 87 } |
| 88 |
44 async getFakeBluetoothInterface_() { | 89 async getFakeBluetoothInterface_() { |
45 if (typeof this.fake_bluetooth_ptr_ !== 'undefined') { | 90 if (typeof this.fake_bluetooth_ptr_ !== 'undefined') { |
46 return this.fake_bluetooth_ptr_; | 91 return this.fake_bluetooth_ptr_; |
47 } | 92 } |
48 | 93 |
49 let mojo = await loadFakeBluetoothInterfaces(); | 94 let mojo = await loadFakeBluetoothInterfaces(); |
50 | 95 |
51 this.fake_bluetooth_ptr_ = new mojo.FakeBluetooth.FakeBluetoothPtr( | 96 this.fake_bluetooth_ptr_ = new mojo.FakeBluetoothPtr( |
52 mojo.interfaces.getInterface( | 97 mojo.interfaces.getInterface(mojo.FakeBluetooth.name)); |
53 mojo.FakeBluetooth.FakeBluetooth.name)); | |
54 | 98 |
55 return this.fake_bluetooth_ptr_; | 99 return this.fake_bluetooth_ptr_; |
56 } | 100 } |
57 } | 101 } |
58 | 102 |
| 103 // FakeLECentralObserverManager allows clients to simulate events that a devic
e |
| 104 // in the Central/Observer role would receive as well as monitor the operation
s |
| 105 // performed by the device in the Central/Observer role. |
| 106 class FakeLECentralObserverManager { |
| 107 constructor(fake_manager_ptr) { |
| 108 this.fake_manager_ptr = fake_manager_ptr; |
| 109 } |
| 110 } |
| 111 |
59 navigator.bluetooth.test = new FakeBluetooth(); | 112 navigator.bluetooth.test = new FakeBluetooth(); |
60 })(); | 113 })(); |
OLD | NEW |