| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string.h> | 5 #include <string.h> |
| 6 | 6 |
| 7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" | 9 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" |
| 10 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" | 10 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" |
| 11 #include "chrome/browser/extensions/extension_apitest.h" | 11 #include "chrome/browser/extensions/extension_apitest.h" |
| 12 #include "chrome/browser/extensions/extension_function_test_utils.h" | 12 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
| 14 #include "chrome/browser/extensions/extension_test_message_listener.h" | 14 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/test/base/ui_test_utils.h" | 16 #include "chrome/test/base/ui_test_utils.h" |
| 17 #include "device/bluetooth/bluetooth_adapter.h" | 17 #include "device/bluetooth/bluetooth_adapter.h" |
| 18 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" | |
| 19 #include "device/bluetooth/bluetooth_uuid.h" | 18 #include "device/bluetooth/bluetooth_uuid.h" |
| 20 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 19 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 21 #include "device/bluetooth/test/mock_bluetooth_device.h" | 20 #include "device/bluetooth/test/mock_bluetooth_device.h" |
| 22 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" | 21 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" |
| 23 #include "device/bluetooth/test/mock_bluetooth_profile.h" | 22 #include "device/bluetooth/test/mock_bluetooth_profile.h" |
| 24 #include "device/bluetooth/test/mock_bluetooth_socket.h" | 23 #include "device/bluetooth/test/mock_bluetooth_socket.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 26 | 25 |
| 27 using device::BluetoothAdapter; | 26 using device::BluetoothAdapter; |
| 28 using device::BluetoothDevice; | 27 using device::BluetoothDevice; |
| 29 using device::BluetoothDiscoverySession; | 28 using device::BluetoothDiscoverySession; |
| 30 using device::BluetoothOutOfBandPairingData; | |
| 31 using device::BluetoothProfile; | 29 using device::BluetoothProfile; |
| 32 using device::BluetoothUUID; | 30 using device::BluetoothUUID; |
| 33 using device::MockBluetoothAdapter; | 31 using device::MockBluetoothAdapter; |
| 34 using device::MockBluetoothDevice; | 32 using device::MockBluetoothDevice; |
| 35 using device::MockBluetoothDiscoverySession; | 33 using device::MockBluetoothDiscoverySession; |
| 36 using device::MockBluetoothProfile; | 34 using device::MockBluetoothProfile; |
| 37 using extensions::Extension; | 35 using extensions::Extension; |
| 38 | 36 |
| 39 namespace utils = extension_function_test_utils; | 37 namespace utils = extension_function_test_utils; |
| 40 namespace api = extensions::api; | 38 namespace api = extensions::api; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 const device::BluetoothProfile::Options& options, | 129 const device::BluetoothProfile::Options& options, |
| 132 const device::BluetoothProfile::ProfileCallback& callback) OVERRIDE { | 130 const device::BluetoothProfile::ProfileCallback& callback) OVERRIDE { |
| 133 callback.Run(profile_); | 131 callback.Run(profile_); |
| 134 } | 132 } |
| 135 | 133 |
| 136 private: | 134 private: |
| 137 // TestBluetoothAddProfileFunction does not own |profile_|. | 135 // TestBluetoothAddProfileFunction does not own |profile_|. |
| 138 BluetoothProfile* profile_; | 136 BluetoothProfile* profile_; |
| 139 }; | 137 }; |
| 140 | 138 |
| 141 // This is the canonical UUID for the short UUID 0010. | |
| 142 static const char kOutOfBandPairingDataHash[] = "0123456789ABCDEh"; | |
| 143 static const char kOutOfBandPairingDataRandomizer[] = "0123456789ABCDEr"; | |
| 144 | |
| 145 static BluetoothOutOfBandPairingData GetOutOfBandPairingData() { | |
| 146 BluetoothOutOfBandPairingData data; | |
| 147 memcpy(&(data.hash), kOutOfBandPairingDataHash, | |
| 148 device::kBluetoothOutOfBandPairingDataSize); | |
| 149 memcpy(&(data.randomizer), kOutOfBandPairingDataRandomizer, | |
| 150 device::kBluetoothOutOfBandPairingDataSize); | |
| 151 return data; | |
| 152 } | |
| 153 | |
| 154 static bool CallClosure(const base::Closure& callback) { | |
| 155 callback.Run(); | |
| 156 return true; | |
| 157 } | |
| 158 | |
| 159 static bool CallErrorClosure(const BluetoothDevice::ErrorCallback& callback) { | |
| 160 callback.Run(); | |
| 161 return true; | |
| 162 } | |
| 163 | |
| 164 static void StopDiscoverySessionCallback(const base::Closure& callback, | 139 static void StopDiscoverySessionCallback(const base::Closure& callback, |
| 165 const base::Closure& error_callback) { | 140 const base::Closure& error_callback) { |
| 166 callback.Run(); | 141 callback.Run(); |
| 167 } | 142 } |
| 168 | 143 |
| 169 static void CallOutOfBandPairingDataCallback( | |
| 170 const BluetoothAdapter::BluetoothOutOfBandPairingDataCallback& callback, | |
| 171 const BluetoothAdapter::ErrorCallback& error_callback) { | |
| 172 callback.Run(GetOutOfBandPairingData()); | |
| 173 } | |
| 174 | |
| 175 } // namespace | 144 } // namespace |
| 176 | 145 |
| 177 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Profiles) { | 146 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Profiles) { |
| 178 // Run in context of an extension that has permissions for the profiles | 147 // Run in context of an extension that has permissions for the profiles |
| 179 // we intend to register. | 148 // we intend to register. |
| 180 scoped_refptr<const Extension> extension( | 149 scoped_refptr<const Extension> extension( |
| 181 LoadExtension(test_data_dir_.AppendASCII("bluetooth/profiles"))); | 150 LoadExtension(test_data_dir_.AppendASCII("bluetooth/profiles"))); |
| 182 ASSERT_TRUE(extension.get()); | 151 ASSERT_TRUE(extension.get()); |
| 183 | 152 |
| 184 EXPECT_CALL(*profile1_, SetConnectionCallback(testing::_)); | 153 EXPECT_CALL(*profile1_, SetConnectionCallback(testing::_)); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 api::bluetooth::AdapterState state; | 227 api::bluetooth::AdapterState state; |
| 259 ASSERT_TRUE(api::bluetooth::AdapterState::Populate(*result, &state)); | 228 ASSERT_TRUE(api::bluetooth::AdapterState::Populate(*result, &state)); |
| 260 | 229 |
| 261 EXPECT_FALSE(state.available); | 230 EXPECT_FALSE(state.available); |
| 262 EXPECT_TRUE(state.powered); | 231 EXPECT_TRUE(state.powered); |
| 263 EXPECT_FALSE(state.discovering); | 232 EXPECT_FALSE(state.discovering); |
| 264 EXPECT_EQ(kName, state.name); | 233 EXPECT_EQ(kName, state.name); |
| 265 EXPECT_EQ(kAdapterAddress, state.address); | 234 EXPECT_EQ(kAdapterAddress, state.address); |
| 266 } | 235 } |
| 267 | 236 |
| 268 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetLocalOutOfBandPairingData) { | |
| 269 EXPECT_CALL(*mock_adapter_, | |
| 270 ReadLocalOutOfBandPairingData(testing::_, testing::_)) | |
| 271 .WillOnce(testing::Invoke(CallOutOfBandPairingDataCallback)); | |
| 272 | |
| 273 scoped_refptr<api::BluetoothGetLocalOutOfBandPairingDataFunction> | |
| 274 get_oob_function(setupFunction( | |
| 275 new api::BluetoothGetLocalOutOfBandPairingDataFunction)); | |
| 276 | |
| 277 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
| 278 get_oob_function.get(), "[]", browser())); | |
| 279 | |
| 280 base::DictionaryValue* dict; | |
| 281 EXPECT_TRUE(result->GetAsDictionary(&dict)); | |
| 282 | |
| 283 base::BinaryValue* binary_value; | |
| 284 EXPECT_TRUE(dict->GetBinary("hash", &binary_value)); | |
| 285 EXPECT_STREQ(kOutOfBandPairingDataHash, | |
| 286 std::string(binary_value->GetBuffer(), binary_value->GetSize()).c_str()); | |
| 287 EXPECT_TRUE(dict->GetBinary("randomizer", &binary_value)); | |
| 288 EXPECT_STREQ(kOutOfBandPairingDataRandomizer, | |
| 289 std::string(binary_value->GetBuffer(), binary_value->GetSize()).c_str()); | |
| 290 | |
| 291 // Try again with an error | |
| 292 testing::Mock::VerifyAndClearExpectations(mock_adapter_); | |
| 293 EXPECT_CALL(*mock_adapter_, | |
| 294 ReadLocalOutOfBandPairingData( | |
| 295 testing::_, | |
| 296 testing::Truly(CallClosure))); | |
| 297 | |
| 298 get_oob_function = | |
| 299 setupFunction(new api::BluetoothGetLocalOutOfBandPairingDataFunction); | |
| 300 | |
| 301 std::string error(utils::RunFunctionAndReturnError( | |
| 302 get_oob_function.get(), "[]", browser())); | |
| 303 EXPECT_FALSE(error.empty()); | |
| 304 } | |
| 305 | |
| 306 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, SetOutOfBandPairingData) { | |
| 307 EXPECT_CALL(*mock_adapter_, GetDevice(device1_->GetAddress())) | |
| 308 .WillOnce(testing::Return(device1_.get())); | |
| 309 EXPECT_CALL(*device1_, | |
| 310 ClearOutOfBandPairingData(testing::Truly(CallClosure), | |
| 311 testing::_)); | |
| 312 | |
| 313 std::string params = base::StringPrintf( | |
| 314 "[{\"deviceAddress\":\"%s\"}]", device1_->GetAddress().c_str()); | |
| 315 | |
| 316 scoped_refptr<api::BluetoothSetOutOfBandPairingDataFunction> set_oob_function; | |
| 317 set_oob_function = setupFunction( | |
| 318 new api::BluetoothSetOutOfBandPairingDataFunction); | |
| 319 // There isn't actually a result. | |
| 320 (void) utils::RunFunctionAndReturnSingleResult( | |
| 321 set_oob_function.get(), params, browser()); | |
| 322 | |
| 323 // Try again with an error | |
| 324 testing::Mock::VerifyAndClearExpectations(mock_adapter_); | |
| 325 testing::Mock::VerifyAndClearExpectations(device1_.get()); | |
| 326 EXPECT_CALL(*mock_adapter_, GetDevice(device1_->GetAddress())) | |
| 327 .WillOnce(testing::Return(device1_.get())); | |
| 328 EXPECT_CALL( | |
| 329 *device1_, | |
| 330 ClearOutOfBandPairingData(testing::_, testing::Truly(CallErrorClosure))); | |
| 331 | |
| 332 set_oob_function = setupFunction( | |
| 333 new api::BluetoothSetOutOfBandPairingDataFunction); | |
| 334 std::string error(utils::RunFunctionAndReturnError( | |
| 335 set_oob_function.get(), params, browser())); | |
| 336 EXPECT_FALSE(error.empty()); | |
| 337 | |
| 338 // TODO(bryeung): Also test setting the data when there is support for | |
| 339 // ArrayBuffers in the arguments to the RunFunctionAnd* methods. | |
| 340 // crbug.com/132796 | |
| 341 } | |
| 342 | |
| 343 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DeviceEvents) { | 237 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DeviceEvents) { |
| 344 ResultCatcher catcher; | 238 ResultCatcher catcher; |
| 345 catcher.RestrictToProfile(browser()->profile()); | 239 catcher.RestrictToProfile(browser()->profile()); |
| 346 | 240 |
| 347 ASSERT_TRUE(LoadExtension( | 241 ASSERT_TRUE(LoadExtension( |
| 348 test_data_dir_.AppendASCII("bluetooth/device_events"))); | 242 test_data_dir_.AppendASCII("bluetooth/device_events"))); |
| 349 | 243 |
| 350 ExtensionTestMessageListener events_received("ready", true); | 244 ExtensionTestMessageListener events_received("ready", true); |
| 351 event_router()->DeviceAdded(mock_adapter_, device1_.get()); | 245 event_router()->DeviceAdded(mock_adapter_, device1_.get()); |
| 352 event_router()->DeviceAdded(mock_adapter_, device2_.get()); | 246 event_router()->DeviceAdded(mock_adapter_, device2_.get()); |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 681 // Load and wait for setup | 575 // Load and wait for setup |
| 682 ExtensionTestMessageListener listener("ready", true); | 576 ExtensionTestMessageListener listener("ready", true); |
| 683 ASSERT_TRUE( | 577 ASSERT_TRUE( |
| 684 LoadExtension(test_data_dir_.AppendASCII("bluetooth/device_info"))); | 578 LoadExtension(test_data_dir_.AppendASCII("bluetooth/device_info"))); |
| 685 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 579 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 686 | 580 |
| 687 listener.Reply("go"); | 581 listener.Reply("go"); |
| 688 | 582 |
| 689 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 583 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 690 } | 584 } |
| OLD | NEW |