| 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"; |
| 93 static const char kOutOfBandPairingDataHash[] = "0123456789ABCDEh"; | 94 static const char kOutOfBandPairingDataHash[] = "0123456789ABCDEh"; |
| 94 static const char kOutOfBandPairingDataRandomizer[] = "0123456789ABCDEr"; | 95 static const char kOutOfBandPairingDataRandomizer[] = "0123456789ABCDEr"; |
| 95 | 96 |
| 96 static chromeos::BluetoothOutOfBandPairingData GetOutOfBandPairingData() { | 97 static chromeos::BluetoothOutOfBandPairingData GetOutOfBandPairingData() { |
| 97 chromeos::BluetoothOutOfBandPairingData data; | 98 chromeos::BluetoothOutOfBandPairingData data; |
| 98 memcpy(&(data.hash), kOutOfBandPairingDataHash, | 99 memcpy(&(data.hash), kOutOfBandPairingDataHash, |
| 99 chromeos::kBluetoothOutOfBandPairingDataSize); | 100 chromeos::kBluetoothOutOfBandPairingDataSize); |
| 100 memcpy(&(data.randomizer), kOutOfBandPairingDataRandomizer, | 101 memcpy(&(data.randomizer), kOutOfBandPairingDataRandomizer, |
| 101 chromeos::kBluetoothOutOfBandPairingDataSize); | 102 chromeos::kBluetoothOutOfBandPairingDataSize); |
| 102 return data; | 103 return data; |
| 103 } | 104 } |
| 104 | 105 |
| 105 static bool CallClosure(const base::Closure& callback) { | 106 static bool CallClosure(const base::Closure& callback) { |
| 106 callback.Run(); | 107 callback.Run(); |
| 107 return true; | 108 return true; |
| 108 } | 109 } |
| 109 | 110 |
| 110 static void CallOutOfBandPairingDataCallback( | 111 static bool CallOutOfBandPairingDataCallback( |
| 111 const chromeos::BluetoothAdapter::BluetoothOutOfBandPairingDataCallback& | 112 const chromeos::BluetoothAdapter::BluetoothOutOfBandPairingDataCallback& |
| 112 callback, | 113 callback) { |
| 113 const chromeos::BluetoothAdapter::ErrorCallback& error_callback) { | |
| 114 callback.Run(GetOutOfBandPairingData()); | 114 callback.Run(GetOutOfBandPairingData()); |
| 115 } | 115 return true; |
| 116 | |
| 117 template <bool Value> | |
| 118 static void CallProvidesServiceCallback( | |
| 119 const std::string& name, | |
| 120 const chromeos::BluetoothDevice::ProvidesServiceCallback& callback) { | |
| 121 callback.Run(Value); | |
| 122 } | 116 } |
| 123 | 117 |
| 124 } // namespace | 118 } // namespace |
| 125 | 119 |
| 126 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, IsAvailable) { | 120 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, IsAvailable) { |
| 127 EXPECT_CALL(*mock_adapter_, IsPresent()) | 121 EXPECT_CALL(*mock_adapter_, IsPresent()) |
| 128 .WillOnce(testing::Return(false)); | 122 .WillOnce(testing::Return(false)); |
| 129 | 123 |
| 130 scoped_refptr<api::BluetoothIsAvailableFunction> is_available; | 124 scoped_refptr<api::BluetoothIsAvailableFunction> is_available; |
| 131 | 125 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 162 get_address = setupFunction(new api::BluetoothGetAddressFunction); | 156 get_address = setupFunction(new api::BluetoothGetAddressFunction); |
| 163 expectStringResult(kAdapterAddress, get_address); | 157 expectStringResult(kAdapterAddress, get_address); |
| 164 } | 158 } |
| 165 | 159 |
| 166 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetName) { | 160 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetName) { |
| 167 scoped_refptr<api::BluetoothGetNameFunction> get_name; | 161 scoped_refptr<api::BluetoothGetNameFunction> get_name; |
| 168 get_name = setupFunction(new api::BluetoothGetNameFunction); | 162 get_name = setupFunction(new api::BluetoothGetNameFunction); |
| 169 expectStringResult(kName, get_name); | 163 expectStringResult(kName, get_name); |
| 170 } | 164 } |
| 171 | 165 |
| 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 |
| 172 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetLocalOutOfBandPairingData) { | 239 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetLocalOutOfBandPairingData) { |
| 173 EXPECT_CALL(*mock_adapter_, | 240 EXPECT_CALL(*mock_adapter_, |
| 174 ReadLocalOutOfBandPairingData(testing::_, testing::_)) | 241 ReadLocalOutOfBandPairingData( |
| 175 .WillOnce(testing::Invoke(CallOutOfBandPairingDataCallback)); | 242 testing::Truly(CallOutOfBandPairingDataCallback), |
| 243 testing::_)); |
| 176 | 244 |
| 177 scoped_refptr<api::BluetoothGetLocalOutOfBandPairingDataFunction> | 245 scoped_refptr<api::BluetoothGetLocalOutOfBandPairingDataFunction> |
| 178 get_oob_function(setupFunction( | 246 get_oob_function(setupFunction( |
| 179 new api::BluetoothGetLocalOutOfBandPairingDataFunction)); | 247 new api::BluetoothGetLocalOutOfBandPairingDataFunction)); |
| 180 | 248 |
| 181 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | 249 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( |
| 182 get_oob_function, "[]", browser())); | 250 get_oob_function, "[]", browser())); |
| 183 | 251 |
| 184 base::DictionaryValue* dict; | 252 base::DictionaryValue* dict; |
| 185 EXPECT_TRUE(result->GetAsDictionary(&dict)); | 253 EXPECT_TRUE(result->GetAsDictionary(&dict)); |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 event_router()->AdapterPoweredChanged(mock_adapter_, false); | 436 event_router()->AdapterPoweredChanged(mock_adapter_, false); |
| 369 event_router()->AdapterPresentChanged(mock_adapter_, true); | 437 event_router()->AdapterPresentChanged(mock_adapter_, true); |
| 370 event_router()->AdapterPresentChanged(mock_adapter_, false); | 438 event_router()->AdapterPresentChanged(mock_adapter_, false); |
| 371 event_router()->AdapterDiscoveringChanged(mock_adapter_, true); | 439 event_router()->AdapterDiscoveringChanged(mock_adapter_, true); |
| 372 event_router()->AdapterDiscoveringChanged(mock_adapter_, false); | 440 event_router()->AdapterDiscoveringChanged(mock_adapter_, false); |
| 373 | 441 |
| 374 listener.Reply("go"); | 442 listener.Reply("go"); |
| 375 | 443 |
| 376 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 444 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 377 } | 445 } |
| 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 |