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, mojo_.FakeBluetooth] = mojo_.modules; |
19 return mojo_; | 19 return mojo_; |
20 } | 20 } |
21 | 21 |
22 class FakeBluetooth { | 22 class FakeBluetooth { |
23 constructor() { | 23 constructor() { |
24 this.fake_bluetooth_ptr_ = undefined; | 24 this.fake_bluetooth_ptr_ = undefined; |
25 } | 25 } |
26 | 26 |
27 // Set it to indicate whether the platform supports BLE. For example, | 27 // 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 | 28 // 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 | 29 // hand Windows 10 is a platform that does support LE, even if there is no |
30 // Bluetooth radio available. | 30 // Bluetooth radio present. |
31 async setLESupported(available) { | 31 async setLESupported(supported) { |
32 if (typeof available !== 'boolean') throw 'Type Not Supported'; | 32 // Call setBluetoothFakeAdapter() to clean up any fake adapters left over |
33 await (await this.getFakeBluetoothInterface_()).setLESupported(available); | 33 // by legacy tests. |
| 34 // Legacy tests that use setBluetoothFakeAdapter() sometimes fail to clean |
| 35 // their fake adapter. This is not a problem for these tests because the |
| 36 // next setBluetoothFakeAdapter() will clean it up anyway but it is a |
| 37 // problem for the new tests that do not use setBluetoothFakeAdapter(). |
| 38 // TODO(crbug.com/569709): Remove once setBluetoothFakeAdapter is no |
| 39 // longer used. |
| 40 await setBluetoothFakeAdapter(''); |
| 41 |
| 42 if (typeof supported !== 'boolean') throw 'Type Not Supported'; |
| 43 await (await this.getFakeBluetoothInterface_()).setLESupported(supported); |
34 } | 44 } |
35 | 45 |
36 async getFakeBluetoothInterface_() { | 46 async getFakeBluetoothInterface_() { |
37 if (typeof this.fake_bluetooth_ptr_ !== 'undefined') { | 47 if (typeof this.fake_bluetooth_ptr_ !== 'undefined') { |
38 return this.fake_bluetooth_ptr_; | 48 return this.fake_bluetooth_ptr_; |
39 } | 49 } |
40 | 50 |
41 let mojo = await loadFakeBluetoothInterfaces(); | 51 let mojo = await loadFakeBluetoothInterfaces(); |
42 | 52 |
43 this.fake_bluetooth_ptr_ = new mojo.FakeBluetooth.FakeBluetoothPtr( | 53 this.fake_bluetooth_ptr_ = new mojo.FakeBluetooth.FakeBluetoothPtr( |
44 mojo.interfaces.getInterface( | 54 mojo.interfaces.getInterface( |
45 mojo.FakeBluetooth.FakeBluetooth.name)); | 55 mojo.FakeBluetooth.FakeBluetooth.name)); |
46 | 56 |
47 return this.fake_bluetooth_ptr_; | 57 return this.fake_bluetooth_ptr_; |
48 } | 58 } |
49 } | 59 } |
50 | 60 |
51 navigator.bluetooth.test = new FakeBluetooth(); | 61 navigator.bluetooth.test = new FakeBluetooth(); |
52 })(); | 62 })(); |
OLD | NEW |