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 CentralState: mojo_.CentralState, |
| 20 FakeBluetooth: mojo_.FakeBluetooth, |
| 21 FakeBluetoothPtr: mojo_.FakeBluetoothPtr, |
| 22 FakeCentral: mojo_.FakeCentral, |
| 23 FakeCentralPtr: mojo_.FakeCentralPtr, |
| 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 | 42 |
| 43 // Returns a promise that resolves with a FakeCentral that clients can use |
| 44 // to simulate events that a device in the Central/Observer role would |
| 45 // receive as well as monitor the operations performed by the device in the |
| 46 // Central/Observer role. |
| 47 // Calls sets LE as supported. |
| 48 async simulateCentral({state}) { |
| 49 // Call setBluetoothFakeAdapter() to clean up any fake adapters left over |
| 50 // by legacy tests. |
| 51 // Legacy tests that use setBluetoothFakeAdapter() sometimes fail to clean |
| 52 // their fake adapter. This is not a problem for these tests because the |
| 53 // next setBluetoothFakeAdapter() will clean it up anyway but it is a |
| 54 // problem for the new tests that do not use setBluetoothFakeAdapter(). |
| 55 // TODO(crbug.com/569709): Remove once setBluetoothFakeAdapter is no |
| 56 // longer used. |
| 57 await setBluetoothFakeAdapter(''); |
| 58 |
| 59 await this.setLESupported(true); |
| 60 let mojo = await loadFakeBluetoothInterfaces(); |
| 61 |
| 62 let mojo_manager_state; |
| 63 switch (state) { |
| 64 case 'absent': |
| 65 mojo_manager_state = mojo.CentralState.ABSENT; |
| 66 break; |
| 67 case 'powered-off': |
| 68 mojo_manager_state = mojo.CentralState.POWERED_OFF; |
| 69 break; |
| 70 case 'powered-on': |
| 71 mojo_manager_state = mojo.CentralState.POWERED_ON; |
| 72 break; |
| 73 default: |
| 74 throw `Unsupported value ${state} for state.`; |
| 75 } |
| 76 |
| 77 let {fake_central:fake_central_ptr} = |
| 78 await (await this.getFakeBluetoothInterface_()).simulateCentral( |
| 79 mojo_manager_state); |
| 80 |
| 81 return new FakeCentral(fake_central_ptr); |
| 82 } |
| 83 |
36 async getFakeBluetoothInterface_() { | 84 async getFakeBluetoothInterface_() { |
37 if (typeof this.fake_bluetooth_ptr_ !== 'undefined') { | 85 if (typeof this.fake_bluetooth_ptr_ !== 'undefined') { |
38 return this.fake_bluetooth_ptr_; | 86 return this.fake_bluetooth_ptr_; |
39 } | 87 } |
40 | 88 |
41 let mojo = await loadFakeBluetoothInterfaces(); | 89 let mojo = await loadFakeBluetoothInterfaces(); |
42 | 90 |
43 this.fake_bluetooth_ptr_ = new mojo.FakeBluetooth.FakeBluetoothPtr( | 91 this.fake_bluetooth_ptr_ = new mojo.FakeBluetoothPtr( |
44 mojo.interfaces.getInterface( | 92 mojo.interfaces.getInterface(mojo.FakeBluetooth.name)); |
45 mojo.FakeBluetooth.FakeBluetooth.name)); | |
46 | 93 |
47 return this.fake_bluetooth_ptr_; | 94 return this.fake_bluetooth_ptr_; |
48 } | 95 } |
49 } | 96 } |
50 | 97 |
| 98 // FakeCentral allows clients to simulate events that a device in the |
| 99 // Central/Observer role would receive as well as monitor the operations |
| 100 // performed by the device in the Central/Observer role. |
| 101 class FakeCentral { |
| 102 constructor(fake_central_ptr) { |
| 103 this.fake_central_ptr = fake_central_ptr; |
| 104 } |
| 105 } |
| 106 |
51 navigator.bluetooth.test = new FakeBluetooth(); | 107 navigator.bluetooth.test = new FakeBluetooth(); |
52 })(); | 108 })(); |
OLD | NEW |