| 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" | |
| 8 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_adapter.h" | |
| 9 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_device.h" | |
| 10 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" | |
| 11 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" | 7 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" |
| 8 #include "chrome/browser/extensions/bluetooth_event_router.h" |
| 12 #include "chrome/browser/extensions/extension_apitest.h" | 9 #include "chrome/browser/extensions/extension_apitest.h" |
| 13 #include "chrome/browser/extensions/extension_function_test_utils.h" | 10 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/extensions/extension_test_message_listener.h" | 12 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 16 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 18 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
| 19 #include "chromeos/dbus/bluetooth_out_of_band_client.h" | 16 #include "chromeos/dbus/bluetooth_out_of_band_client.h" |
| 20 #include "chromeos/dbus/bluetooth_out_of_band_pairing_data.h" | 17 #include "device/bluetooth/bluetooth_adapter.h" |
| 18 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" |
| 19 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 20 #include "device/bluetooth/test/mock_bluetooth_device.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 22 | 22 |
| 23 using device::BluetoothAdapter; |
| 24 using device::BluetoothDevice; |
| 25 using device::BluetoothOutOfBandPairingData; |
| 26 using device::MockBluetoothAdapter; |
| 27 using device::MockBluetoothDevice; |
| 23 using extensions::Extension; | 28 using extensions::Extension; |
| 24 | 29 |
| 25 namespace utils = extension_function_test_utils; | 30 namespace utils = extension_function_test_utils; |
| 26 namespace api = extensions::api; | 31 namespace api = extensions::api; |
| 27 | 32 |
| 28 namespace { | 33 namespace { |
| 29 | 34 |
| 30 static const char* kAdapterAddress = "A1:A2:A3:A4:A5:A6"; | 35 static const char* kAdapterAddress = "A1:A2:A3:A4:A5:A6"; |
| 31 static const char* kName = "whatsinaname"; | 36 static const char* kName = "whatsinaname"; |
| 32 | 37 |
| 33 class BluetoothApiTest : public ExtensionApiTest { | 38 class BluetoothApiTest : public ExtensionApiTest { |
| 34 public: | 39 public: |
| 35 BluetoothApiTest() : empty_extension_(utils::CreateEmptyExtension()) {} | 40 BluetoothApiTest() : empty_extension_(utils::CreateEmptyExtension()) {} |
| 36 | 41 |
| 37 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 42 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 38 ExtensionApiTest::SetUpCommandLine(command_line); | 43 ExtensionApiTest::SetUpCommandLine(command_line); |
| 39 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); | 44 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); |
| 40 } | 45 } |
| 41 | 46 |
| 42 virtual void SetUpOnMainThread() OVERRIDE { | 47 virtual void SetUpOnMainThread() OVERRIDE { |
| 43 // The browser will clean this up when it is torn down | 48 // The browser will clean this up when it is torn down |
| 44 mock_adapter_ = new testing::StrictMock<chromeos::MockBluetoothAdapter>( | 49 mock_adapter_ = new testing::StrictMock<MockBluetoothAdapter>( |
| 45 kAdapterAddress, kName); | 50 kAdapterAddress, kName); |
| 46 event_router()->SetAdapterForTest(mock_adapter_); | 51 event_router()->SetAdapterForTest(mock_adapter_); |
| 47 | 52 |
| 48 device1_.reset(new testing::NiceMock<chromeos::MockBluetoothDevice>( | 53 device1_.reset(new testing::NiceMock<MockBluetoothDevice>( |
| 49 mock_adapter_, "d1", "11:12:13:14:15:16", | 54 mock_adapter_, "d1", "11:12:13:14:15:16", |
| 50 true /* paired */, false /* bonded */, true /* connected */)); | 55 true /* paired */, false /* bonded */, true /* connected */)); |
| 51 device2_.reset(new testing::NiceMock<chromeos::MockBluetoothDevice>( | 56 device2_.reset(new testing::NiceMock<MockBluetoothDevice>( |
| 52 mock_adapter_, "d2", "21:22:23:24:25:26", | 57 mock_adapter_, "d2", "21:22:23:24:25:26", |
| 53 false /* paired */, true /* bonded */, false /* connected */)); | 58 false /* paired */, true /* bonded */, false /* connected */)); |
| 54 } | 59 } |
| 55 | 60 |
| 56 virtual void CleanUpOnMainThread() OVERRIDE { | 61 virtual void CleanUpOnMainThread() OVERRIDE { |
| 57 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)); | 62 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)); |
| 58 } | 63 } |
| 59 | 64 |
| 60 void expectBooleanResult(bool expected, | 65 void expectBooleanResult(bool expected, |
| 61 UIThreadExtensionFunction* function) { | 66 UIThreadExtensionFunction* function) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 80 } | 85 } |
| 81 | 86 |
| 82 template <class T> | 87 template <class T> |
| 83 T* setupFunction(T* function) { | 88 T* setupFunction(T* function) { |
| 84 function->set_extension(empty_extension_.get()); | 89 function->set_extension(empty_extension_.get()); |
| 85 function->set_has_callback(true); | 90 function->set_has_callback(true); |
| 86 return function; | 91 return function; |
| 87 } | 92 } |
| 88 | 93 |
| 89 protected: | 94 protected: |
| 90 testing::StrictMock<chromeos::MockBluetoothAdapter>* mock_adapter_; | 95 testing::StrictMock<MockBluetoothAdapter>* mock_adapter_; |
| 91 scoped_ptr<testing::NiceMock<chromeos::MockBluetoothDevice> > device1_; | 96 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device1_; |
| 92 scoped_ptr<testing::NiceMock<chromeos::MockBluetoothDevice> > device2_; | 97 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device2_; |
| 93 | 98 |
| 94 chromeos::ExtensionBluetoothEventRouter* event_router() { | 99 extensions::ExtensionBluetoothEventRouter* event_router() { |
| 95 return browser()->profile()->GetExtensionService()-> | 100 return browser()->profile()->GetExtensionService()-> |
| 96 bluetooth_event_router(); | 101 bluetooth_event_router(); |
| 97 } | 102 } |
| 98 | 103 |
| 99 private: | 104 private: |
| 100 scoped_refptr<Extension> empty_extension_; | 105 scoped_refptr<Extension> empty_extension_; |
| 101 }; | 106 }; |
| 102 | 107 |
| 103 // This is the canonical UUID for the short UUID 0010. | 108 // This is the canonical UUID for the short UUID 0010. |
| 104 static const char kOutOfBandPairingDataHash[] = "0123456789ABCDEh"; | 109 static const char kOutOfBandPairingDataHash[] = "0123456789ABCDEh"; |
| 105 static const char kOutOfBandPairingDataRandomizer[] = "0123456789ABCDEr"; | 110 static const char kOutOfBandPairingDataRandomizer[] = "0123456789ABCDEr"; |
| 106 | 111 |
| 107 static chromeos::BluetoothOutOfBandPairingData GetOutOfBandPairingData() { | 112 static BluetoothOutOfBandPairingData GetOutOfBandPairingData() { |
| 108 chromeos::BluetoothOutOfBandPairingData data; | 113 BluetoothOutOfBandPairingData data; |
| 109 memcpy(&(data.hash), kOutOfBandPairingDataHash, | 114 memcpy(&(data.hash), kOutOfBandPairingDataHash, |
| 110 chromeos::kBluetoothOutOfBandPairingDataSize); | 115 device::kBluetoothOutOfBandPairingDataSize); |
| 111 memcpy(&(data.randomizer), kOutOfBandPairingDataRandomizer, | 116 memcpy(&(data.randomizer), kOutOfBandPairingDataRandomizer, |
| 112 chromeos::kBluetoothOutOfBandPairingDataSize); | 117 device::kBluetoothOutOfBandPairingDataSize); |
| 113 return data; | 118 return data; |
| 114 } | 119 } |
| 115 | 120 |
| 116 static bool CallClosure(const base::Closure& callback) { | 121 static bool CallClosure(const base::Closure& callback) { |
| 117 callback.Run(); | 122 callback.Run(); |
| 118 return true; | 123 return true; |
| 119 } | 124 } |
| 120 | 125 |
| 121 static void CallOutOfBandPairingDataCallback( | 126 static void CallOutOfBandPairingDataCallback( |
| 122 const chromeos::BluetoothAdapter::BluetoothOutOfBandPairingDataCallback& | 127 const BluetoothAdapter::BluetoothOutOfBandPairingDataCallback& callback, |
| 123 callback, | 128 const BluetoothAdapter::ErrorCallback& error_callback) { |
| 124 const chromeos::BluetoothAdapter::ErrorCallback& error_callback) { | |
| 125 callback.Run(GetOutOfBandPairingData()); | 129 callback.Run(GetOutOfBandPairingData()); |
| 126 } | 130 } |
| 127 | 131 |
| 128 template <bool Value> | 132 template <bool Value> |
| 129 static void CallProvidesServiceCallback( | 133 static void CallProvidesServiceCallback( |
| 130 const std::string& name, | 134 const std::string& name, |
| 131 const chromeos::BluetoothDevice::ProvidesServiceCallback& callback) { | 135 const BluetoothDevice::ProvidesServiceCallback& callback) { |
| 132 callback.Run(Value); | 136 callback.Run(Value); |
| 133 } | 137 } |
| 134 | 138 |
| 135 } // namespace | 139 } // namespace |
| 136 | 140 |
| 137 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, IsAvailable) { | 141 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, IsAvailable) { |
| 138 EXPECT_CALL(*mock_adapter_, IsPresent()) | 142 EXPECT_CALL(*mock_adapter_, IsPresent()) |
| 139 .WillOnce(testing::Return(false)); | 143 .WillOnce(testing::Return(false)); |
| 140 | 144 |
| 141 scoped_refptr<api::BluetoothIsAvailableFunction> is_available; | 145 scoped_refptr<api::BluetoothIsAvailableFunction> is_available; |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 | 362 |
| 359 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 363 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 360 } | 364 } |
| 361 | 365 |
| 362 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Events) { | 366 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Events) { |
| 363 ResultCatcher catcher; | 367 ResultCatcher catcher; |
| 364 catcher.RestrictToProfile(browser()->profile()); | 368 catcher.RestrictToProfile(browser()->profile()); |
| 365 | 369 |
| 366 // Load and wait for setup | 370 // Load and wait for setup |
| 367 ExtensionTestMessageListener listener("ready", true); | 371 ExtensionTestMessageListener listener("ready", true); |
| 368 const extensions::Extension* extension = | 372 const Extension* extension = |
| 369 LoadExtension(test_data_dir_.AppendASCII("bluetooth")); | 373 LoadExtension(test_data_dir_.AppendASCII("bluetooth")); |
| 370 GURL page_url = extension->GetResourceURL("test_events.html"); | 374 GURL page_url = extension->GetResourceURL("test_events.html"); |
| 371 ui_test_utils::NavigateToURL(browser(), page_url); | 375 ui_test_utils::NavigateToURL(browser(), page_url); |
| 372 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 376 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 373 | 377 |
| 374 event_router()->AdapterPoweredChanged(mock_adapter_, true); | 378 event_router()->AdapterPoweredChanged(mock_adapter_, true); |
| 375 event_router()->AdapterPoweredChanged(mock_adapter_, false); | 379 event_router()->AdapterPoweredChanged(mock_adapter_, false); |
| 376 event_router()->AdapterPresentChanged(mock_adapter_, true); | 380 event_router()->AdapterPresentChanged(mock_adapter_, true); |
| 377 event_router()->AdapterPresentChanged(mock_adapter_, false); | 381 event_router()->AdapterPresentChanged(mock_adapter_, false); |
| 378 event_router()->AdapterDiscoveringChanged(mock_adapter_, true); | 382 event_router()->AdapterDiscoveringChanged(mock_adapter_, true); |
| 379 event_router()->AdapterDiscoveringChanged(mock_adapter_, false); | 383 event_router()->AdapterDiscoveringChanged(mock_adapter_, false); |
| 380 | 384 |
| 381 listener.Reply("go"); | 385 listener.Reply("go"); |
| 382 | 386 |
| 383 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 387 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 384 } | 388 } |
| 385 | 389 |
| 386 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) { | 390 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) { |
| 387 ResultCatcher catcher; | 391 ResultCatcher catcher; |
| 388 catcher.RestrictToProfile(browser()->profile()); | 392 catcher.RestrictToProfile(browser()->profile()); |
| 389 | 393 |
| 390 chromeos::BluetoothAdapter::ConstDeviceList devices; | 394 BluetoothAdapter::ConstDeviceList devices; |
| 391 devices.push_back(device1_.get()); | 395 devices.push_back(device1_.get()); |
| 392 devices.push_back(device2_.get()); | 396 devices.push_back(device2_.get()); |
| 393 | 397 |
| 394 EXPECT_CALL(*device1_, ProvidesServiceWithUUID(testing::_)) | 398 EXPECT_CALL(*device1_, ProvidesServiceWithUUID(testing::_)) |
| 395 .WillOnce(testing::Return(false)); | 399 .WillOnce(testing::Return(false)); |
| 396 EXPECT_CALL(*device1_, ProvidesServiceWithName(testing::_, testing::_)) | 400 EXPECT_CALL(*device1_, ProvidesServiceWithName(testing::_, testing::_)) |
| 397 .WillOnce(testing::Invoke(CallProvidesServiceCallback<true>)); | 401 .WillOnce(testing::Invoke(CallProvidesServiceCallback<true>)); |
| 398 | 402 |
| 399 EXPECT_CALL(*device2_, ProvidesServiceWithUUID(testing::_)) | 403 EXPECT_CALL(*device2_, ProvidesServiceWithUUID(testing::_)) |
| 400 .WillOnce(testing::Return(true)); | 404 .WillOnce(testing::Return(true)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 413 | 417 |
| 414 listener.Reply("go"); | 418 listener.Reply("go"); |
| 415 | 419 |
| 416 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 420 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 417 } | 421 } |
| 418 | 422 |
| 419 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevicesConcurrently) { | 423 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevicesConcurrently) { |
| 420 ResultCatcher catcher; | 424 ResultCatcher catcher; |
| 421 catcher.RestrictToProfile(browser()->profile()); | 425 catcher.RestrictToProfile(browser()->profile()); |
| 422 | 426 |
| 423 chromeos::BluetoothAdapter::ConstDeviceList devices; | 427 BluetoothAdapter::ConstDeviceList devices; |
| 424 devices.push_back(device1_.get()); | 428 devices.push_back(device1_.get()); |
| 425 | 429 |
| 426 // Save the callback to delay execution so that we can force the calls to | 430 // Save the callback to delay execution so that we can force the calls to |
| 427 // happen concurrently. This will be called after the listener is satisfied. | 431 // happen concurrently. This will be called after the listener is satisfied. |
| 428 chromeos::BluetoothDevice::ProvidesServiceCallback callback; | 432 BluetoothDevice::ProvidesServiceCallback callback; |
| 429 EXPECT_CALL(*device1_, ProvidesServiceWithName(testing::_, testing::_)) | 433 EXPECT_CALL(*device1_, ProvidesServiceWithName(testing::_, testing::_)) |
| 430 .WillOnce(testing::SaveArg<1>(&callback)); | 434 .WillOnce(testing::SaveArg<1>(&callback)); |
| 431 | 435 |
| 432 EXPECT_CALL(*mock_adapter_, GetDevices()) | 436 EXPECT_CALL(*mock_adapter_, GetDevices()) |
| 433 .WillOnce(testing::Return(devices)); | 437 .WillOnce(testing::Return(devices)); |
| 434 | 438 |
| 435 // Load and wait for setup | 439 // Load and wait for setup |
| 436 ExtensionTestMessageListener listener("ready", true); | 440 ExtensionTestMessageListener listener("ready", true); |
| 437 ASSERT_TRUE(LoadExtension( | 441 ASSERT_TRUE(LoadExtension( |
| 438 test_data_dir_.AppendASCII("bluetooth/get_devices_concurrently"))); | 442 test_data_dir_.AppendASCII("bluetooth/get_devices_concurrently"))); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 451 // Load and wait for setup | 455 // Load and wait for setup |
| 452 ExtensionTestMessageListener listener("ready", true); | 456 ExtensionTestMessageListener listener("ready", true); |
| 453 ASSERT_TRUE(LoadExtension( | 457 ASSERT_TRUE(LoadExtension( |
| 454 test_data_dir_.AppendASCII("bluetooth/get_devices_error"))); | 458 test_data_dir_.AppendASCII("bluetooth/get_devices_error"))); |
| 455 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 459 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 456 | 460 |
| 457 listener.Reply("go"); | 461 listener.Reply("go"); |
| 458 | 462 |
| 459 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 463 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 460 } | 464 } |
| OLD | NEW |