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 // |
| 49 // A "Central" object would allow its clients to receive advertising events |
| 50 // and initiate connections to peripherals i.e. operations of two roles |
| 51 // defined by the Bluetooth Spec: Observer and Central. |
| 52 // See Bluetooth 4.2 Vol 3 Part C 2.2.2 "Roles when Operating over an |
| 53 // LE Physical Transport". |
| 54 async simulateCentral({state}) { |
| 55 // Call setBluetoothFakeAdapter() to clean up any fake adapters left over |
| 56 // by legacy tests. |
| 57 // Legacy tests that use setBluetoothFakeAdapter() sometimes fail to clean |
| 58 // their fake adapter. This is not a problem for these tests because the |
| 59 // next setBluetoothFakeAdapter() will clean it up anyway but it is a |
| 60 // problem for the new tests that do not use setBluetoothFakeAdapter(). |
| 61 // TODO(crbug.com/569709): Remove once setBluetoothFakeAdapter is no |
| 62 // longer used. |
| 63 await setBluetoothFakeAdapter(''); |
| 64 |
| 65 await this.setLESupported(true); |
| 66 let mojo = await loadFakeBluetoothInterfaces(); |
| 67 |
| 68 let mojo_manager_state; |
| 69 switch (state) { |
| 70 case 'absent': |
| 71 mojo_manager_state = mojo.CentralState.ABSENT; |
| 72 break; |
| 73 case 'powered-off': |
| 74 mojo_manager_state = mojo.CentralState.POWERED_OFF; |
| 75 break; |
| 76 case 'powered-on': |
| 77 mojo_manager_state = mojo.CentralState.POWERED_ON; |
| 78 break; |
| 79 default: |
| 80 throw `Unsupported value ${state} for state.`; |
| 81 } |
| 82 |
| 83 let {fake_central:fake_central_ptr} = |
| 84 await (await this.getFakeBluetoothInterface_()).simulateCentral( |
| 85 mojo_manager_state); |
| 86 |
| 87 return new FakeCentral(fake_central_ptr); |
| 88 } |
| 89 |
36 async getFakeBluetoothInterface_() { | 90 async getFakeBluetoothInterface_() { |
37 if (typeof this.fake_bluetooth_ptr_ !== 'undefined') { | 91 if (typeof this.fake_bluetooth_ptr_ !== 'undefined') { |
38 return this.fake_bluetooth_ptr_; | 92 return this.fake_bluetooth_ptr_; |
39 } | 93 } |
40 | 94 |
41 let mojo = await loadFakeBluetoothInterfaces(); | 95 let mojo = await loadFakeBluetoothInterfaces(); |
42 | 96 |
43 this.fake_bluetooth_ptr_ = new mojo.FakeBluetooth.FakeBluetoothPtr( | 97 this.fake_bluetooth_ptr_ = new mojo.FakeBluetoothPtr( |
44 mojo.interfaces.getInterface( | 98 mojo.interfaces.getInterface(mojo.FakeBluetooth.name)); |
45 mojo.FakeBluetooth.FakeBluetooth.name)); | |
46 | 99 |
47 return this.fake_bluetooth_ptr_; | 100 return this.fake_bluetooth_ptr_; |
48 } | 101 } |
49 } | 102 } |
50 | 103 |
| 104 // FakeCentral allows clients to simulate events that a device in the |
| 105 // Central/Observer role would receive as well as monitor the operations |
| 106 // performed by the device in the Central/Observer role. |
| 107 class FakeCentral { |
| 108 constructor(fake_central_ptr) { |
| 109 this.fake_central_ptr = fake_central_ptr; |
| 110 } |
| 111 } |
| 112 |
51 navigator.bluetooth.test = new FakeBluetooth(); | 113 navigator.bluetooth.test = new FakeBluetooth(); |
52 })(); | 114 })(); |
OLD | NEW |