From 4d19be49e34248f8c98ba5d8fea4fb4016b45d95 Mon Sep 17 00:00:00 2001 From: Ovidio Henriquez Date: Wed, 28 Mar 2018 22:32:23 -0700 Subject: [PATCH] bluetooth: FakeBluetoothChooser impl. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implements the interfaces that FakeBluetoothChooser overrides. The mojom::FakeBluetoothChooser overrides are implemented by running |event_handler_| with the appropriate arguments. The BluetoothChooser overrides do nothing at the moment, since the base class is only meant to update the chooser user interface. This change also begins to partially convert a Bluetooth chooser test to test the interface and deletes the temporary tests that were created to test this API. This change reverts commit 42624492cd5e80855f1dd3a5a8146b258def2c2a, which reverted the original change. The original change caused the new-scan-device-added.html test to fail because of a Mojo validation error resulting from not updating the third_party/.../external/wpt/resources/chromium/fake_bluetooth_chooser.mojom.js file needed by the test. Bug: 719827, 719826, 824704, 826787 Change-Id: I5e5cce7e809b4d71216c2d074a417ea822d5e527 Reviewed-on: https://chromium-review.googlesource.com/985195 Commit-Queue: Ovidio Henriquez Reviewed-by: Giovanni Ortuño Urquidi Reviewed-by: Daniel Cheng Cr-Commit-Position: refs/heads/master@{#546729} --- bluetooth/resources/bluetooth-helpers.js | 9 + .../chromium/fake_bluetooth_chooser.mojom.js | 194 +++--------------- resources/chromium/web-bluetooth-test.js | 15 ++ 3 files changed, 50 insertions(+), 168 deletions(-) diff --git a/bluetooth/resources/bluetooth-helpers.js b/bluetooth/resources/bluetooth-helpers.js index 7b23742fc4c9a3..1a5dec57df3a2d 100644 --- a/bluetooth/resources/bluetooth-helpers.js +++ b/bluetooth/resources/bluetooth-helpers.js @@ -477,6 +477,15 @@ function setUpPreconnectedDevice({ })); } +const health_thermometer_ad_packet = { + deviceAddress: '09:09:09:09:09:09', + rssi: -10, + scanRecord: { + name: 'Health Thermometer', + uuids: [health_thermometer.uuid], + }, +}; + // Returns a FakePeripheral that corresponds to a simulated pre-connected device // called 'Health Thermometer'. The device has two known serviceUUIDs: // 'generic_access' and 'health_thermometer'. diff --git a/resources/chromium/fake_bluetooth_chooser.mojom.js b/resources/chromium/fake_bluetooth_chooser.mojom.js index 535a90a4ab5c79..7a5c40c0dc4996 100644 --- a/resources/chromium/fake_bluetooth_chooser.mojom.js +++ b/resources/chromium/fake_bluetooth_chooser.mojom.js @@ -305,52 +305,6 @@ encoder.writeUint32(0); encoder.encodeStruct(codec.String, val.peripheralAddress); }; - function FakeBluetoothChooser_SelectPeripheral_ResponseParams(values) { - this.initDefaults_(); - this.initFields_(values); - } - - - FakeBluetoothChooser_SelectPeripheral_ResponseParams.prototype.initDefaults_ = function() { - }; - FakeBluetoothChooser_SelectPeripheral_ResponseParams.prototype.initFields_ = function(fields) { - for(var field in fields) { - if (this.hasOwnProperty(field)) - this[field] = fields[field]; - } - }; - - FakeBluetoothChooser_SelectPeripheral_ResponseParams.validate = function(messageValidator, offset) { - var err; - err = messageValidator.validateStructHeader(offset, codec.kStructHeaderSize); - if (err !== validator.validationError.NONE) - return err; - - var kVersionSizes = [ - {version: 0, numBytes: 8} - ]; - err = messageValidator.validateStructVersion(offset, kVersionSizes); - if (err !== validator.validationError.NONE) - return err; - - return validator.validationError.NONE; - }; - - FakeBluetoothChooser_SelectPeripheral_ResponseParams.encodedSize = codec.kStructHeaderSize + 0; - - FakeBluetoothChooser_SelectPeripheral_ResponseParams.decode = function(decoder) { - var packed; - var val = new FakeBluetoothChooser_SelectPeripheral_ResponseParams(); - var numberOfBytes = decoder.readUint32(); - var version = decoder.readUint32(); - return val; - }; - - FakeBluetoothChooser_SelectPeripheral_ResponseParams.encode = function(encoder, val) { - var packed; - encoder.writeUint32(FakeBluetoothChooser_SelectPeripheral_ResponseParams.encodedSize); - encoder.writeUint32(0); - }; function FakeBluetoothChooser_Cancel_Params(values) { this.initDefaults_(); this.initFields_(values); @@ -397,52 +351,6 @@ encoder.writeUint32(FakeBluetoothChooser_Cancel_Params.encodedSize); encoder.writeUint32(0); }; - function FakeBluetoothChooser_Cancel_ResponseParams(values) { - this.initDefaults_(); - this.initFields_(values); - } - - - FakeBluetoothChooser_Cancel_ResponseParams.prototype.initDefaults_ = function() { - }; - FakeBluetoothChooser_Cancel_ResponseParams.prototype.initFields_ = function(fields) { - for(var field in fields) { - if (this.hasOwnProperty(field)) - this[field] = fields[field]; - } - }; - - FakeBluetoothChooser_Cancel_ResponseParams.validate = function(messageValidator, offset) { - var err; - err = messageValidator.validateStructHeader(offset, codec.kStructHeaderSize); - if (err !== validator.validationError.NONE) - return err; - - var kVersionSizes = [ - {version: 0, numBytes: 8} - ]; - err = messageValidator.validateStructVersion(offset, kVersionSizes); - if (err !== validator.validationError.NONE) - return err; - - return validator.validationError.NONE; - }; - - FakeBluetoothChooser_Cancel_ResponseParams.encodedSize = codec.kStructHeaderSize + 0; - - FakeBluetoothChooser_Cancel_ResponseParams.decode = function(decoder) { - var packed; - var val = new FakeBluetoothChooser_Cancel_ResponseParams(); - var numberOfBytes = decoder.readUint32(); - var version = decoder.readUint32(); - return val; - }; - - FakeBluetoothChooser_Cancel_ResponseParams.encode = function(encoder, val) { - var packed; - encoder.writeUint32(FakeBluetoothChooser_Cancel_ResponseParams.encodedSize); - encoder.writeUint32(0); - }; function FakeBluetoothChooser_Rescan_Params(values) { this.initDefaults_(); this.initFields_(values); @@ -535,10 +443,10 @@ encoder.writeUint32(FakeBluetoothChooser_Rescan_ResponseParams.encodedSize); encoder.writeUint32(0); }; - var kFakeBluetoothChooser_WaitForEvents_Name = 457051710; - var kFakeBluetoothChooser_SelectPeripheral_Name = 1924310743; - var kFakeBluetoothChooser_Cancel_Name = 1388880682; - var kFakeBluetoothChooser_Rescan_Name = 2112671529; + var kFakeBluetoothChooser_WaitForEvents_Name = 1899695487; + var kFakeBluetoothChooser_SelectPeripheral_Name = 728735403; + var kFakeBluetoothChooser_Cancel_Name = 246208861; + var kFakeBluetoothChooser_Rescan_Name = 1031326211; function FakeBluetoothChooserPtr(handleOrPtrInfo) { this.ptr = new bindings.InterfacePtrController(FakeBluetoothChooser, @@ -591,22 +499,12 @@ FakeBluetoothChooserProxy.prototype.selectPeripheral = function(peripheralAddress) { var params = new FakeBluetoothChooser_SelectPeripheral_Params(); params.peripheralAddress = peripheralAddress; - return new Promise(function(resolve, reject) { - var builder = new codec.MessageV1Builder( - kFakeBluetoothChooser_SelectPeripheral_Name, - codec.align(FakeBluetoothChooser_SelectPeripheral_Params.encodedSize), - codec.kMessageExpectsResponse, 0); - builder.encodeStruct(FakeBluetoothChooser_SelectPeripheral_Params, params); - var message = builder.finish(); - this.receiver_.acceptAndExpectResponse(message).then(function(message) { - var reader = new codec.MessageReader(message); - var responseParams = - reader.decodeStruct(FakeBluetoothChooser_SelectPeripheral_ResponseParams); - resolve(responseParams); - }).catch(function(result) { - reject(Error("Connection error: " + result)); - }); - }.bind(this)); + var builder = new codec.MessageV0Builder( + kFakeBluetoothChooser_SelectPeripheral_Name, + codec.align(FakeBluetoothChooser_SelectPeripheral_Params.encodedSize)); + builder.encodeStruct(FakeBluetoothChooser_SelectPeripheral_Params, params); + var message = builder.finish(); + this.receiver_.accept(message); }; FakeBluetoothChooserPtr.prototype.cancel = function() { return FakeBluetoothChooserProxy.prototype.cancel @@ -615,22 +513,12 @@ FakeBluetoothChooserProxy.prototype.cancel = function() { var params = new FakeBluetoothChooser_Cancel_Params(); - return new Promise(function(resolve, reject) { - var builder = new codec.MessageV1Builder( - kFakeBluetoothChooser_Cancel_Name, - codec.align(FakeBluetoothChooser_Cancel_Params.encodedSize), - codec.kMessageExpectsResponse, 0); - builder.encodeStruct(FakeBluetoothChooser_Cancel_Params, params); - var message = builder.finish(); - this.receiver_.acceptAndExpectResponse(message).then(function(message) { - var reader = new codec.MessageReader(message); - var responseParams = - reader.decodeStruct(FakeBluetoothChooser_Cancel_ResponseParams); - resolve(responseParams); - }).catch(function(result) { - reject(Error("Connection error: " + result)); - }); - }.bind(this)); + var builder = new codec.MessageV0Builder( + kFakeBluetoothChooser_Cancel_Name, + codec.align(FakeBluetoothChooser_Cancel_Params.encodedSize)); + builder.encodeStruct(FakeBluetoothChooser_Cancel_Params, params); + var message = builder.finish(); + this.receiver_.accept(message); }; FakeBluetoothChooserPtr.prototype.rescan = function() { return FakeBluetoothChooserProxy.prototype.rescan @@ -676,6 +564,14 @@ FakeBluetoothChooserStub.prototype.accept = function(message) { var reader = new codec.MessageReader(message); switch (reader.messageName) { + case kFakeBluetoothChooser_SelectPeripheral_Name: + var params = reader.decodeStruct(FakeBluetoothChooser_SelectPeripheral_Params); + this.selectPeripheral(params.peripheralAddress); + return true; + case kFakeBluetoothChooser_Cancel_Name: + var params = reader.decodeStruct(FakeBluetoothChooser_Cancel_Params); + this.cancel(); + return true; default: return false; } @@ -701,36 +597,6 @@ responder.accept(message); }); return true; - case kFakeBluetoothChooser_SelectPeripheral_Name: - var params = reader.decodeStruct(FakeBluetoothChooser_SelectPeripheral_Params); - this.selectPeripheral(params.peripheralAddress).then(function(response) { - var responseParams = - new FakeBluetoothChooser_SelectPeripheral_ResponseParams(); - var builder = new codec.MessageV1Builder( - kFakeBluetoothChooser_SelectPeripheral_Name, - codec.align(FakeBluetoothChooser_SelectPeripheral_ResponseParams.encodedSize), - codec.kMessageIsResponse, reader.requestID); - builder.encodeStruct(FakeBluetoothChooser_SelectPeripheral_ResponseParams, - responseParams); - var message = builder.finish(); - responder.accept(message); - }); - return true; - case kFakeBluetoothChooser_Cancel_Name: - var params = reader.decodeStruct(FakeBluetoothChooser_Cancel_Params); - this.cancel().then(function(response) { - var responseParams = - new FakeBluetoothChooser_Cancel_ResponseParams(); - var builder = new codec.MessageV1Builder( - kFakeBluetoothChooser_Cancel_Name, - codec.align(FakeBluetoothChooser_Cancel_ResponseParams.encodedSize), - codec.kMessageIsResponse, reader.requestID); - builder.encodeStruct(FakeBluetoothChooser_Cancel_ResponseParams, - responseParams); - var message = builder.finish(); - responder.accept(message); - }); - return true; case kFakeBluetoothChooser_Rescan_Name: var params = reader.decodeStruct(FakeBluetoothChooser_Rescan_Params); this.rescan().then(function(response) { @@ -760,11 +626,11 @@ paramsClass = FakeBluetoothChooser_WaitForEvents_Params; break; case kFakeBluetoothChooser_SelectPeripheral_Name: - if (message.expectsResponse()) + if (!message.expectsResponse() && !message.isResponse()) paramsClass = FakeBluetoothChooser_SelectPeripheral_Params; break; case kFakeBluetoothChooser_Cancel_Name: - if (message.expectsResponse()) + if (!message.expectsResponse() && !message.isResponse()) paramsClass = FakeBluetoothChooser_Cancel_Params; break; case kFakeBluetoothChooser_Rescan_Name: @@ -785,14 +651,6 @@ if (message.isResponse()) paramsClass = FakeBluetoothChooser_WaitForEvents_ResponseParams; break; - case kFakeBluetoothChooser_SelectPeripheral_Name: - if (message.isResponse()) - paramsClass = FakeBluetoothChooser_SelectPeripheral_ResponseParams; - break; - case kFakeBluetoothChooser_Cancel_Name: - if (message.isResponse()) - paramsClass = FakeBluetoothChooser_Cancel_ResponseParams; - break; case kFakeBluetoothChooser_Rescan_Name: if (message.isResponse()) paramsClass = FakeBluetoothChooser_Rescan_ResponseParams; diff --git a/resources/chromium/web-bluetooth-test.js b/resources/chromium/web-bluetooth-test.js index e08c8c4afa2e80..6dd176f4713b17 100644 --- a/resources/chromium/web-bluetooth-test.js +++ b/resources/chromium/web-bluetooth-test.js @@ -489,6 +489,21 @@ class FakeChooser { Mojo.bindInterface(content.mojom.FakeBluetoothChooser.name, mojo.makeRequest(this.fake_bluetooth_chooser_ptr_).handle, 'process'); } + + async selectPeripheral(peripheral) { + if (!(peripheral instanceof FakePeripheral)) { + throw '|peripheral| must be an instance of FakePeripheral'; + } + await this.fake_bluetooth_chooser_ptr_.selectPeripheral(peripheral.address); + } + + async cancel() { + await this.fake_bluetooth_chooser_ptr_.cancel(); + } + + async rescan() { + await this.fake_bluetooth_chooser_ptr_.rescan(); + } } // If this line fails, it means that current environment does not support the