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 function toMojoCentralState(state) { | |
30 if (mojo_ === undefined) { | |
scheib
2017/05/10 17:51:14
OK to leave it, but I think the console error mess
ortuno
2017/05/10 23:34:23
True. Done.
| |
31 throw 'Mojo not initialized.'; | |
32 } | |
33 | |
34 switch (state) { | |
35 case 'absent': | |
36 return mojo_.CentralState.ABSENT; | |
37 case 'powered-off': | |
38 return mojo_.CentralState.POWERED_OFF; | |
39 case 'powered-on': | |
40 return mojo_.CentralState.POWERED_ON; | |
41 default: | |
42 throw `Unsupported value ${state} for state.`; | |
43 } | |
44 } | |
45 | |
29 class FakeBluetooth { | 46 class FakeBluetooth { |
30 constructor() { | 47 constructor() { |
31 this.fake_bluetooth_ptr_ = undefined; | 48 this.fake_bluetooth_ptr_ = 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 |
42 // their fake adapter. This is not a problem for these tests because the | 59 // their fake adapter. This is not a problem for these tests because the |
43 // next setBluetoothFakeAdapter() will clean it up anyway but it is a | 60 // next setBluetoothFakeAdapter() will clean it up anyway but it is a |
44 // problem for the new tests that do not use setBluetoothFakeAdapter(). | 61 // problem for the new tests that do not use setBluetoothFakeAdapter(). |
45 // TODO(crbug.com/569709): Remove once setBluetoothFakeAdapter is no | 62 // TODO(crbug.com/569709): Remove once setBluetoothFakeAdapter is no |
46 // longer used. | 63 // longer used. |
47 await setBluetoothFakeAdapter(''); | 64 await setBluetoothFakeAdapter(''); |
65 await this.initFakeBluetoothInterfacePtr_(); | |
48 | 66 |
49 if (typeof supported !== 'boolean') throw 'Type Not Supported'; | 67 if (typeof supported !== 'boolean') throw 'Type Not Supported'; |
50 await (await this.getFakeBluetoothInterface_()).setLESupported(supported); | 68 await this.fake_bluetooth_ptr_.setLESupported(supported); |
51 } | 69 } |
52 | 70 |
53 // Returns a promise that resolves with a FakeCentral that clients can use | 71 // Returns a promise that resolves with a FakeCentral that clients can use |
54 // to simulate events that a device in the Central/Observer role would | 72 // to simulate events that a device in the Central/Observer role would |
55 // receive as well as monitor the operations performed by the device in the | 73 // receive as well as monitor the operations performed by the device in the |
56 // Central/Observer role. | 74 // Central/Observer role. |
57 // Calls sets LE as supported. | 75 // Calls sets LE as supported. |
58 // | 76 // |
59 // A "Central" object would allow its clients to receive advertising events | 77 // A "Central" object would allow its clients to receive advertising events |
60 // and initiate connections to peripherals i.e. operations of two roles | 78 // and initiate connections to peripherals i.e. operations of two roles |
61 // defined by the Bluetooth Spec: Observer and Central. | 79 // defined by the Bluetooth Spec: Observer and Central. |
62 // See Bluetooth 4.2 Vol 3 Part C 2.2.2 "Roles when Operating over an | 80 // See Bluetooth 4.2 Vol 3 Part C 2.2.2 "Roles when Operating over an |
63 // LE Physical Transport". | 81 // LE Physical Transport". |
64 async simulateCentral({state}) { | 82 async simulateCentral({state}) { |
65 // Call setBluetoothFakeAdapter() to clean up any fake adapters left over | 83 // Call setBluetoothFakeAdapter() to clean up any fake adapters left over |
66 // by legacy tests. | 84 // by legacy tests. |
67 // Legacy tests that use setBluetoothFakeAdapter() sometimes fail to clean | 85 // Legacy tests that use setBluetoothFakeAdapter() sometimes fail to clean |
68 // their fake adapter. This is not a problem for these tests because the | 86 // 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 | 87 // next setBluetoothFakeAdapter() will clean it up anyway but it is a |
70 // problem for the new tests that do not use setBluetoothFakeAdapter(). | 88 // problem for the new tests that do not use setBluetoothFakeAdapter(). |
71 // TODO(crbug.com/569709): Remove once setBluetoothFakeAdapter is no | 89 // TODO(crbug.com/569709): Remove once setBluetoothFakeAdapter is no |
72 // longer used. | 90 // longer used. |
73 await setBluetoothFakeAdapter(''); | 91 await setBluetoothFakeAdapter(''); |
92 await this.initFakeBluetoothInterfacePtr_(); | |
74 | 93 |
75 await this.setLESupported(true); | 94 await this.setLESupported(true); |
76 let mojo = await loadFakeBluetoothInterfaces(); | |
77 | |
78 let mojo_manager_state; | |
79 switch (state) { | |
80 case 'absent': | |
81 mojo_manager_state = mojo.CentralState.ABSENT; | |
82 break; | |
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 } | |
92 | 95 |
93 let {fake_central:fake_central_ptr} = | 96 let {fake_central:fake_central_ptr} = |
94 await (await this.getFakeBluetoothInterface_()).simulateCentral( | 97 await this.fake_bluetooth_ptr_.simulateCentral( |
95 mojo_manager_state); | 98 toMojoCentralState(state)); |
96 | |
97 return new FakeCentral(fake_central_ptr); | 99 return new FakeCentral(fake_central_ptr); |
98 } | 100 } |
99 | 101 |
100 async getFakeBluetoothInterface_() { | 102 async initFakeBluetoothInterfacePtr_() { |
101 if (typeof this.fake_bluetooth_ptr_ !== 'undefined') { | 103 if (typeof this.fake_bluetooth_ptr_ !== 'undefined') { |
102 return this.fake_bluetooth_ptr_; | 104 return this.fake_bluetooth_ptr_; |
103 } | 105 } |
104 | 106 |
105 let mojo = await loadFakeBluetoothInterfaces(); | 107 let mojo = await loadFakeBluetoothInterfaces(); |
106 | 108 |
107 this.fake_bluetooth_ptr_ = new mojo.FakeBluetoothPtr( | 109 this.fake_bluetooth_ptr_ = new mojo.FakeBluetoothPtr( |
108 mojo.interfaces.getInterface(mojo.FakeBluetooth.name)); | 110 mojo.interfaces.getInterface(mojo.FakeBluetooth.name)); |
109 | |
110 return this.fake_bluetooth_ptr_; | |
111 } | 111 } |
112 } | 112 } |
113 | 113 |
114 // FakeCentral allows clients to simulate events that a device in the | 114 // FakeCentral allows clients to simulate events that a device in the |
115 // Central/Observer role would receive as well as monitor the operations | 115 // Central/Observer role would receive as well as monitor the operations |
116 // performed by the device in the Central/Observer role. | 116 // performed by the device in the Central/Observer role. |
117 class FakeCentral { | 117 class FakeCentral { |
118 constructor(fake_central_ptr) { | 118 constructor(fake_central_ptr) { |
119 this.fake_central_ptr_ = fake_central_ptr; | 119 this.fake_central_ptr_ = fake_central_ptr; |
120 this.peripherals_ = new Map(); | 120 this.peripherals_ = new Map(); |
121 } | 121 } |
122 | 122 |
123 // Simulates a peripheral with |address| and |name| that has already | 123 // Simulates a peripheral with |address|, |name| and |known_service_uuids| |
124 // been connected to the system. If the peripheral existed already it | 124 // that has already been connected to the system. If the peripheral existed |
125 // updates its name. | 125 // already it updates its name and known UUIDs. |known_service_uuids| should |
126 // be an array of BluetoothServiceUUIDs | |
127 // https://webbluetoothcg.github.io/web-bluetooth/#typedefdef-bluetoothservi ceuuid | |
126 // | 128 // |
127 // Platforms offer methods to retrieve devices that have already been | 129 // 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 | 130 // 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 | 131 // connected a peripheral through the system's settings. This method is |
130 // intended to simulate peripherals that those methods would return. | 132 // intended to simulate peripherals that those methods would return. |
131 async simulatePreconnectedPeripheral({address, name}) { | 133 async simulatePreconnectedPeripheral({ |
134 address, name, knownServiceUUIDs = []}) { | |
135 | |
136 // Canonicalize and convert to mojo UUIDs. | |
137 knownServiceUUIDs.forEach((val, i, arr) => { | |
138 knownServiceUUIDs[i] = {uuid: BluetoothUUID.getService(val)}; | |
139 }); | |
140 | |
132 await this.fake_central_ptr_.simulatePreconnectedPeripheral( | 141 await this.fake_central_ptr_.simulatePreconnectedPeripheral( |
133 address, name); | 142 address, name, knownServiceUUIDs); |
134 | 143 |
135 let peripheral = this.peripherals_.get(address); | 144 let peripheral = this.peripherals_.get(address); |
136 if (peripheral === undefined) { | 145 if (peripheral === undefined) { |
137 peripheral = new FakePeripheral(address, this); | 146 peripheral = new FakePeripheral(address, this); |
138 this.peripherals_.set(address, peripheral); | 147 this.peripherals_.set(address, peripheral); |
139 } | 148 } |
140 | 149 |
141 return peripheral; | 150 return peripheral; |
142 } | 151 } |
143 } | 152 } |
144 | 153 |
145 class FakePeripheral { | 154 class FakePeripheral { |
146 constructor(address, fake_central) { | 155 constructor(address, fake_central) { |
147 this.address = address; | 156 this.address = address; |
148 this.fake_central_ = fake_central; | 157 this.fake_central_ = fake_central; |
149 } | 158 } |
150 } | 159 } |
151 | 160 |
152 navigator.bluetooth.test = new FakeBluetooth(); | 161 navigator.bluetooth.test = new FakeBluetooth(); |
153 })(); | 162 })(); |
OLD | NEW |