| 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 "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | 7 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" |
| 8 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_adapter.h" | 8 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_adapter.h" |
| 9 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_device.h" | 9 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_device.h" |
| 10 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" | 10 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 chromeos::ExtensionBluetoothEventRouter* event_router() { | 83 chromeos::ExtensionBluetoothEventRouter* event_router() { |
| 84 return browser()->profile()->GetExtensionService()-> | 84 return browser()->profile()->GetExtensionService()-> |
| 85 bluetooth_event_router(); | 85 bluetooth_event_router(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 private: | 88 private: |
| 89 scoped_refptr<Extension> empty_extension_; | 89 scoped_refptr<Extension> empty_extension_; |
| 90 }; | 90 }; |
| 91 | 91 |
| 92 // This is the canonical UUID for the short UUID 0010. | 92 // This is the canonical UUID for the short UUID 0010. |
| 93 static const char kCanonicalUuid[] = "00000010-0000-1000-8000-00805f9b34fb"; | |
| 94 static const char kOutOfBandPairingDataHash[] = "0123456789ABCDEh"; | 93 static const char kOutOfBandPairingDataHash[] = "0123456789ABCDEh"; |
| 95 static const char kOutOfBandPairingDataRandomizer[] = "0123456789ABCDEr"; | 94 static const char kOutOfBandPairingDataRandomizer[] = "0123456789ABCDEr"; |
| 96 | 95 |
| 97 static chromeos::BluetoothOutOfBandPairingData GetOutOfBandPairingData() { | 96 static chromeos::BluetoothOutOfBandPairingData GetOutOfBandPairingData() { |
| 98 chromeos::BluetoothOutOfBandPairingData data; | 97 chromeos::BluetoothOutOfBandPairingData data; |
| 99 memcpy(&(data.hash), kOutOfBandPairingDataHash, | 98 memcpy(&(data.hash), kOutOfBandPairingDataHash, |
| 100 chromeos::kBluetoothOutOfBandPairingDataSize); | 99 chromeos::kBluetoothOutOfBandPairingDataSize); |
| 101 memcpy(&(data.randomizer), kOutOfBandPairingDataRandomizer, | 100 memcpy(&(data.randomizer), kOutOfBandPairingDataRandomizer, |
| 102 chromeos::kBluetoothOutOfBandPairingDataSize); | 101 chromeos::kBluetoothOutOfBandPairingDataSize); |
| 103 return data; | 102 return data; |
| 104 } | 103 } |
| 105 | 104 |
| 106 static bool CallClosure(const base::Closure& callback) { | 105 static bool CallClosure(const base::Closure& callback) { |
| 107 callback.Run(); | 106 callback.Run(); |
| 108 return true; | 107 return true; |
| 109 } | 108 } |
| 110 | 109 |
| 111 static bool CallOutOfBandPairingDataCallback( | 110 static void CallOutOfBandPairingDataCallback( |
| 112 const chromeos::BluetoothAdapter::BluetoothOutOfBandPairingDataCallback& | 111 const chromeos::BluetoothAdapter::BluetoothOutOfBandPairingDataCallback& |
| 113 callback) { | 112 callback, |
| 113 const chromeos::BluetoothAdapter::ErrorCallback& error_callback) { |
| 114 callback.Run(GetOutOfBandPairingData()); | 114 callback.Run(GetOutOfBandPairingData()); |
| 115 return true; | 115 } |
| 116 |
| 117 template <bool Value> |
| 118 static void CallProvidesServiceCallback( |
| 119 const std::string& name, |
| 120 const chromeos::BluetoothDevice::ProvidesServiceCallback& callback) { |
| 121 callback.Run(Value); |
| 116 } | 122 } |
| 117 | 123 |
| 118 } // namespace | 124 } // namespace |
| 119 | 125 |
| 120 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, IsAvailable) { | 126 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, IsAvailable) { |
| 121 EXPECT_CALL(*mock_adapter_, IsPresent()) | 127 EXPECT_CALL(*mock_adapter_, IsPresent()) |
| 122 .WillOnce(testing::Return(false)); | 128 .WillOnce(testing::Return(false)); |
| 123 | 129 |
| 124 scoped_refptr<api::BluetoothIsAvailableFunction> is_available; | 130 scoped_refptr<api::BluetoothIsAvailableFunction> is_available; |
| 125 | 131 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 156 get_address = setupFunction(new api::BluetoothGetAddressFunction); | 162 get_address = setupFunction(new api::BluetoothGetAddressFunction); |
| 157 expectStringResult(kAdapterAddress, get_address); | 163 expectStringResult(kAdapterAddress, get_address); |
| 158 } | 164 } |
| 159 | 165 |
| 160 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetName) { | 166 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetName) { |
| 161 scoped_refptr<api::BluetoothGetNameFunction> get_name; | 167 scoped_refptr<api::BluetoothGetNameFunction> get_name; |
| 162 get_name = setupFunction(new api::BluetoothGetNameFunction); | 168 get_name = setupFunction(new api::BluetoothGetNameFunction); |
| 163 expectStringResult(kName, get_name); | 169 expectStringResult(kName, get_name); |
| 164 } | 170 } |
| 165 | 171 |
| 166 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) { | |
| 167 chromeos::BluetoothAdapter::ConstDeviceList devices; | |
| 168 devices.push_back(device1_.get()); | |
| 169 devices.push_back(device2_.get()); | |
| 170 | |
| 171 EXPECT_CALL(*device1_, ProvidesServiceWithUUID(kCanonicalUuid)) | |
| 172 .WillOnce(testing::Return(false)); | |
| 173 EXPECT_CALL(*device2_, ProvidesServiceWithUUID(kCanonicalUuid)) | |
| 174 .WillOnce(testing::Return(true)); | |
| 175 | |
| 176 EXPECT_CALL(*mock_adapter_, GetDevices()) | |
| 177 .WillOnce(testing::Return(devices)); | |
| 178 | |
| 179 scoped_refptr<api::BluetoothGetDevicesFunction> get_devices; | |
| 180 | |
| 181 get_devices = setupFunction(new api::BluetoothGetDevicesFunction); | |
| 182 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
| 183 get_devices, | |
| 184 "[{\"uuid\":\"0010\"}]", | |
| 185 browser())); | |
| 186 | |
| 187 ASSERT_EQ(base::Value::TYPE_LIST, result->GetType()); | |
| 188 base::ListValue* list; | |
| 189 ASSERT_TRUE(result->GetAsList(&list)); | |
| 190 | |
| 191 EXPECT_EQ(1u, list->GetSize()); | |
| 192 base::Value* device_value; | |
| 193 EXPECT_TRUE(list->Get(0, &device_value)); | |
| 194 EXPECT_EQ(base::Value::TYPE_DICTIONARY, device_value->GetType()); | |
| 195 base::DictionaryValue* device; | |
| 196 ASSERT_TRUE(device_value->GetAsDictionary(&device)); | |
| 197 | |
| 198 std::string name; | |
| 199 ASSERT_TRUE(device->GetString("name", &name)); | |
| 200 EXPECT_EQ("d2", name); | |
| 201 std::string address; | |
| 202 ASSERT_TRUE(device->GetString("address", &address)); | |
| 203 EXPECT_EQ("21:22:23:24:25:26", address); | |
| 204 bool paired; | |
| 205 ASSERT_TRUE(device->GetBoolean("paired", &paired)); | |
| 206 EXPECT_FALSE(paired); | |
| 207 bool bonded; | |
| 208 ASSERT_TRUE(device->GetBoolean("bonded", &bonded)); | |
| 209 EXPECT_TRUE(bonded); | |
| 210 bool connected; | |
| 211 ASSERT_TRUE(device->GetBoolean("connected", &connected)); | |
| 212 EXPECT_FALSE(connected); | |
| 213 | |
| 214 // Try again with no options | |
| 215 testing::Mock::VerifyAndClearExpectations(mock_adapter_); | |
| 216 EXPECT_CALL(*mock_adapter_, GetDevices()) | |
| 217 .WillOnce(testing::Return(devices)); | |
| 218 | |
| 219 get_devices = setupFunction(new api::BluetoothGetDevicesFunction); | |
| 220 result.reset( | |
| 221 utils::RunFunctionAndReturnSingleResult(get_devices, "[{}]", browser())); | |
| 222 | |
| 223 ASSERT_EQ(base::Value::TYPE_LIST, result->GetType()); | |
| 224 ASSERT_TRUE(result->GetAsList(&list)); | |
| 225 | |
| 226 EXPECT_EQ(2u, list->GetSize()); | |
| 227 | |
| 228 // Try again with an error | |
| 229 testing::Mock::VerifyAndClearExpectations(mock_adapter_); | |
| 230 | |
| 231 get_devices = setupFunction(new api::BluetoothGetDevicesFunction); | |
| 232 std::string error( | |
| 233 utils::RunFunctionAndReturnError(get_devices, | |
| 234 "[{\"uuid\":\"foo\"}]", | |
| 235 browser())); | |
| 236 EXPECT_FALSE(error.empty()); | |
| 237 } | |
| 238 | |
| 239 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetLocalOutOfBandPairingData) { | 172 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetLocalOutOfBandPairingData) { |
| 240 EXPECT_CALL(*mock_adapter_, | 173 EXPECT_CALL(*mock_adapter_, |
| 241 ReadLocalOutOfBandPairingData( | 174 ReadLocalOutOfBandPairingData(testing::_, testing::_)) |
| 242 testing::Truly(CallOutOfBandPairingDataCallback), | 175 .WillOnce(testing::Invoke(CallOutOfBandPairingDataCallback)); |
| 243 testing::_)); | |
| 244 | 176 |
| 245 scoped_refptr<api::BluetoothGetLocalOutOfBandPairingDataFunction> | 177 scoped_refptr<api::BluetoothGetLocalOutOfBandPairingDataFunction> |
| 246 get_oob_function(setupFunction( | 178 get_oob_function(setupFunction( |
| 247 new api::BluetoothGetLocalOutOfBandPairingDataFunction)); | 179 new api::BluetoothGetLocalOutOfBandPairingDataFunction)); |
| 248 | 180 |
| 249 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | 181 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( |
| 250 get_oob_function, "[]", browser())); | 182 get_oob_function, "[]", browser())); |
| 251 | 183 |
| 252 base::DictionaryValue* dict; | 184 base::DictionaryValue* dict; |
| 253 EXPECT_TRUE(result->GetAsDictionary(&dict)); | 185 EXPECT_TRUE(result->GetAsDictionary(&dict)); |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 event_router()->AdapterPoweredChanged(mock_adapter_, false); | 368 event_router()->AdapterPoweredChanged(mock_adapter_, false); |
| 437 event_router()->AdapterPresentChanged(mock_adapter_, true); | 369 event_router()->AdapterPresentChanged(mock_adapter_, true); |
| 438 event_router()->AdapterPresentChanged(mock_adapter_, false); | 370 event_router()->AdapterPresentChanged(mock_adapter_, false); |
| 439 event_router()->AdapterDiscoveringChanged(mock_adapter_, true); | 371 event_router()->AdapterDiscoveringChanged(mock_adapter_, true); |
| 440 event_router()->AdapterDiscoveringChanged(mock_adapter_, false); | 372 event_router()->AdapterDiscoveringChanged(mock_adapter_, false); |
| 441 | 373 |
| 442 listener.Reply("go"); | 374 listener.Reply("go"); |
| 443 | 375 |
| 444 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 376 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 445 } | 377 } |
| 378 |
| 379 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) { |
| 380 ResultCatcher catcher; |
| 381 catcher.RestrictToProfile(browser()->profile()); |
| 382 |
| 383 chromeos::BluetoothAdapter::ConstDeviceList devices; |
| 384 devices.push_back(device1_.get()); |
| 385 devices.push_back(device2_.get()); |
| 386 |
| 387 EXPECT_CALL(*device1_, ProvidesServiceWithUUID(testing::_)) |
| 388 .WillOnce(testing::Return(false)); |
| 389 EXPECT_CALL(*device1_, ProvidesServiceWithName(testing::_, testing::_)) |
| 390 .WillOnce(testing::Invoke(CallProvidesServiceCallback<true>)); |
| 391 |
| 392 EXPECT_CALL(*device2_, ProvidesServiceWithUUID(testing::_)) |
| 393 .WillOnce(testing::Return(true)); |
| 394 EXPECT_CALL(*device2_, ProvidesServiceWithName(testing::_, testing::_)) |
| 395 .WillOnce(testing::Invoke(CallProvidesServiceCallback<false>)); |
| 396 |
| 397 EXPECT_CALL(*mock_adapter_, GetDevices()) |
| 398 .Times(3) |
| 399 .WillRepeatedly(testing::Return(devices)); |
| 400 |
| 401 // Load and wait for setup |
| 402 ExtensionTestMessageListener listener("ready", true); |
| 403 const extensions::Extension* extension = |
| 404 LoadExtension(test_data_dir_.AppendASCII("bluetooth")); |
| 405 GURL page_url = extension->GetResourceURL("test_getdevices.html"); |
| 406 ui_test_utils::NavigateToURL(browser(), page_url); |
| 407 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 408 |
| 409 listener.Reply("go"); |
| 410 |
| 411 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 412 } |
| 413 |
| 414 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevicesConcurrently) { |
| 415 ResultCatcher catcher; |
| 416 catcher.RestrictToProfile(browser()->profile()); |
| 417 |
| 418 chromeos::BluetoothAdapter::ConstDeviceList devices; |
| 419 devices.push_back(device1_.get()); |
| 420 |
| 421 // Save the callback to delay execution so that we can force the calls to |
| 422 // happen concurrently. This will be called after the listener is satisfied. |
| 423 chromeos::BluetoothDevice::ProvidesServiceCallback callback; |
| 424 EXPECT_CALL(*device1_, ProvidesServiceWithName(testing::_, testing::_)) |
| 425 .WillOnce(testing::SaveArg<1>(&callback)); |
| 426 |
| 427 EXPECT_CALL(*mock_adapter_, GetDevices()) |
| 428 .WillOnce(testing::Return(devices)); |
| 429 |
| 430 // Load and wait for setup |
| 431 ExtensionTestMessageListener listener("ready", true); |
| 432 const extensions::Extension* extension = |
| 433 LoadExtension(test_data_dir_.AppendASCII("bluetooth")); |
| 434 GURL page_url = |
| 435 extension->GetResourceURL("test_getdevices_concurrently.html"); |
| 436 ui_test_utils::NavigateToURL(browser(), page_url); |
| 437 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 438 |
| 439 callback.Run(false); |
| 440 listener.Reply("go"); |
| 441 |
| 442 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 443 } |
| OLD | NEW |