Chromium Code Reviews| 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, { | 18 [mojo_.bindings, { |
| 19 CentralState: mojo_.CentralState, | 19 CentralState: mojo_.CentralState, |
| 20 FakeBluetooth: mojo_.FakeBluetooth, | 20 FakeBluetooth: mojo_.FakeBluetooth, |
| 21 FakeBluetoothPtr: mojo_.FakeBluetoothPtr, | 21 FakeBluetoothPtr: mojo_.FakeBluetoothPtr, |
| 22 FakeCentral: mojo_.FakeCentral, | 22 FakeCentral: mojo_.FakeCentral, |
| 23 FakeCentralPtr: mojo_.FakeCentralPtr, | 23 FakeCentralPtr: mojo_.FakeCentralPtr, |
| 24 }] = mojo_.modules; | 24 }] = mojo_.modules; |
| 25 | 25 |
| 26 return mojo_; | 26 return mojo_; |
| 27 } | 27 } |
| 28 | 28 |
| 29 async function toMojoCentralState(state) { | |
| 30 let mojo = await loadFakeBluetoothInterfaces(); | |
| 31 | |
| 32 let mojo_central_state; | |
| 33 switch (state) { | |
| 34 case 'absent': | |
| 35 return mojo.CentralState.ABSENT; | |
| 36 case 'powered-off': | |
| 37 return mojo.CentralState.POWERED_OFF; | |
| 38 case 'powered-on': | |
| 39 return mojo.CentralState.POWERED_ON; | |
| 40 default: | |
| 41 throw `Unsupported value ${state} for state.`; | |
| 42 } | |
| 43 } | |
| 44 | |
| 29 class FakeBluetooth { | 45 class FakeBluetooth { |
| 30 constructor() { | 46 constructor() { |
| 31 this.fake_bluetooth_ptr_ = undefined; | 47 this.fake_bluetooth_ptr_ = undefined; |
| 48 this.fake_central_ = undefined; | |
| 32 } | 49 } |
| 33 | 50 |
| 34 // Set it to indicate whether the platform supports BLE. For example, | 51 // Set it to indicate whether the platform supports BLE. For example, |
| 35 // Windows 7 is a platform that doesn't support Low Energy. On the other | 52 // Windows 7 is a platform that doesn't support Low Energy. On the other |
| 36 // hand Windows 10 is a platform that does support LE, even if there is no | 53 // hand Windows 10 is a platform that does support LE, even if there is no |
| 37 // Bluetooth radio present. | 54 // Bluetooth radio present. |
| 38 async setLESupported(supported) { | 55 async setLESupported(supported) { |
| 39 // Call setBluetoothFakeAdapter() to clean up any fake adapters left over | 56 // Call setBluetoothFakeAdapter() to clean up any fake adapters left over |
| 40 // by legacy tests. | 57 // by legacy tests. |
| 41 // Legacy tests that use setBluetoothFakeAdapter() sometimes fail to clean | 58 // Legacy tests that use setBluetoothFakeAdapter() sometimes fail to clean |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 66 // by legacy tests. | 83 // by legacy tests. |
| 67 // Legacy tests that use setBluetoothFakeAdapter() sometimes fail to clean | 84 // Legacy tests that use setBluetoothFakeAdapter() sometimes fail to clean |
| 68 // their fake adapter. This is not a problem for these tests because the | 85 // their fake adapter. This is not a problem for these tests because the |
| 69 // next setBluetoothFakeAdapter() will clean it up anyway but it is a | 86 // next setBluetoothFakeAdapter() will clean it up anyway but it is a |
| 70 // problem for the new tests that do not use setBluetoothFakeAdapter(). | 87 // problem for the new tests that do not use setBluetoothFakeAdapter(). |
| 71 // TODO(crbug.com/569709): Remove once setBluetoothFakeAdapter is no | 88 // TODO(crbug.com/569709): Remove once setBluetoothFakeAdapter is no |
| 72 // longer used. | 89 // longer used. |
| 73 await setBluetoothFakeAdapter(''); | 90 await setBluetoothFakeAdapter(''); |
| 74 | 91 |
| 75 await this.setLESupported(true); | 92 await this.setLESupported(true); |
| 76 let mojo = await loadFakeBluetoothInterfaces(); | 93 if (this.fake_central_ === undefined) { |
|
scheib
2017/05/09 23:33:54
... continued from previous comment. This logic se
ortuno
2017/05/10 06:32:51
Yeah, I agree. It was a trade-off between convenie
| |
| 77 | 94 let {fake_central:fake_central_ptr} = |
| 78 let mojo_manager_state; | 95 await (await this.getFakeBluetoothInterface_()).simulateCentral( |
| 79 switch (state) { | 96 await toMojoCentralState(state)); |
| 80 case 'absent': | 97 this.fake_central_ = new FakeCentral(fake_central_ptr); |
| 81 mojo_manager_state = mojo.CentralState.ABSENT; | 98 } else { |
| 82 break; | 99 await this.fake_central_.setState(state); |
| 83 case 'powered-off': | |
| 84 mojo_manager_state = mojo.CentralState.POWERED_OFF; | |
| 85 break; | |
| 86 case 'powered-on': | |
| 87 mojo_manager_state = mojo.CentralState.POWERED_ON; | |
| 88 break; | |
| 89 default: | |
| 90 throw `Unsupported value ${state} for state.`; | |
| 91 } | 100 } |
| 92 | 101 |
| 93 let {fake_central:fake_central_ptr} = | 102 return this.fake_central_; |
| 94 await (await this.getFakeBluetoothInterface_()).simulateCentral( | |
| 95 mojo_manager_state); | |
| 96 | |
| 97 return new FakeCentral(fake_central_ptr); | |
| 98 } | 103 } |
| 99 | 104 |
| 100 async getFakeBluetoothInterface_() { | 105 async getFakeBluetoothInterface_() { |
| 101 if (typeof this.fake_bluetooth_ptr_ !== 'undefined') { | 106 if (typeof this.fake_bluetooth_ptr_ !== 'undefined') { |
| 102 return this.fake_bluetooth_ptr_; | 107 return this.fake_bluetooth_ptr_; |
| 103 } | 108 } |
| 104 | 109 |
| 105 let mojo = await loadFakeBluetoothInterfaces(); | 110 let mojo = await loadFakeBluetoothInterfaces(); |
| 106 | 111 |
| 107 this.fake_bluetooth_ptr_ = new mojo.FakeBluetoothPtr( | 112 this.fake_bluetooth_ptr_ = new mojo.FakeBluetoothPtr( |
| 108 mojo.interfaces.getInterface(mojo.FakeBluetooth.name)); | 113 mojo.interfaces.getInterface(mojo.FakeBluetooth.name)); |
| 109 | 114 |
| 110 return this.fake_bluetooth_ptr_; | 115 return this.fake_bluetooth_ptr_; |
| 111 } | 116 } |
| 112 } | 117 } |
| 113 | 118 |
| 114 // FakeCentral allows clients to simulate events that a device in the | 119 // FakeCentral allows clients to simulate events that a device in the |
| 115 // Central/Observer role would receive as well as monitor the operations | 120 // Central/Observer role would receive as well as monitor the operations |
| 116 // performed by the device in the Central/Observer role. | 121 // performed by the device in the Central/Observer role. |
| 117 class FakeCentral { | 122 class FakeCentral { |
| 118 constructor(fake_central_ptr) { | 123 constructor(fake_central_ptr) { |
| 119 this.fake_central_ptr_ = fake_central_ptr; | 124 this.fake_central_ptr_ = fake_central_ptr; |
| 120 this.peripherals_ = new Map(); | 125 this.peripherals_ = new Map(); |
| 121 } | 126 } |
| 122 | 127 |
| 123 // Simulates a peripheral with |address| and |name| that has already | 128 // Simulates a peripheral with |address|, |name| and |known_service_uuids| |
|
scheib
2017/05/09 23:33:54
Describe that array members of known_service_uuids
ortuno
2017/05/10 06:32:51
Done.
| |
| 124 // been connected to the system. If the peripheral existed already it | 129 // that has already been connected to the system. If the peripheral existed |
| 125 // updates its name. | 130 // already it updates its name and known UUIDs. |
| 126 // | 131 // |
| 127 // Platforms offer methods to retrieve devices that have already been | 132 // Platforms offer methods to retrieve devices that have already been |
| 128 // connected to the system or weren't connected through the UA e.g. a | 133 // connected to the system or weren't connected through the UA e.g. a user |
| 129 // user connected a peripheral through the system's settings. This method is | 134 // connected a peripheral through the system's settings. This method is |
| 130 // intended to simulate peripherals that those methods would return. | 135 // intended to simulate peripherals that those methods would return. |
| 131 async simulatePreconnectedPeripheral({address, name}) { | 136 async simulatePreconnectedPeripheral({ |
| 137 address, name, knownServiceUUIDs = []}) { | |
| 138 | |
| 139 // Canonicalize and convert to mojo UUIDs. | |
| 140 knownServiceUUIDs.forEach((val, i, arr) => { | |
| 141 knownServiceUUIDs[i] = {uuid: BluetoothUUID.getService(val)}; | |
| 142 }); | |
| 143 | |
| 132 await this.fake_central_ptr_.simulatePreconnectedPeripheral( | 144 await this.fake_central_ptr_.simulatePreconnectedPeripheral( |
| 133 address, name); | 145 address, name, knownServiceUUIDs); |
| 134 | 146 |
| 135 let peripheral = this.peripherals_.get(address); | 147 let peripheral = this.peripherals_.get(address); |
| 136 if (peripheral === undefined) { | 148 if (peripheral === undefined) { |
| 137 peripheral = new FakePeripheral(address, this); | 149 peripheral = new FakePeripheral(address, this); |
| 138 this.peripherals_.set(address, peripheral); | 150 this.peripherals_.set(address, peripheral); |
| 139 } | 151 } |
| 140 | 152 |
| 141 return peripheral; | 153 return peripheral; |
| 142 } | 154 } |
| 155 | |
| 156 async setState(state) { | |
| 157 await this.fake_central_ptr_.setState(await toMojoCentralState(state)); | |
| 158 } | |
| 143 } | 159 } |
| 144 | 160 |
| 145 class FakePeripheral { | 161 class FakePeripheral { |
| 146 constructor(address, fake_central) { | 162 constructor(address, fake_central) { |
| 147 this.address = address; | 163 this.address = address; |
| 148 this.fake_central_ = fake_central; | 164 this.fake_central_ = fake_central; |
| 149 } | 165 } |
| 150 } | 166 } |
| 151 | 167 |
| 152 navigator.bluetooth.test = new FakeBluetooth(); | 168 navigator.bluetooth.test = new FakeBluetooth(); |
| 153 })(); | 169 })(); |
| OLD | NEW |