Chromium Code Reviews| 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 "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" | 9 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" |
| 9 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" | 10 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" |
| 10 #include "chrome/browser/extensions/extension_apitest.h" | 11 #include "chrome/browser/extensions/extension_apitest.h" |
| 11 #include "chrome/browser/extensions/extension_function_test_utils.h" | 12 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/extensions/extension_test_message_listener.h" | 14 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/test/base/ui_test_utils.h" | 16 #include "chrome/test/base/ui_test_utils.h" |
| 16 #include "device/bluetooth/bluetooth_adapter.h" | 17 #include "device/bluetooth/bluetooth_adapter.h" |
| 17 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" | 18 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 // The browser will clean this up when it is torn down | 60 // The browser will clean this up when it is torn down |
| 60 mock_adapter_ = new testing::StrictMock<MockBluetoothAdapter>(); | 61 mock_adapter_ = new testing::StrictMock<MockBluetoothAdapter>(); |
| 61 event_router()->SetAdapterForTest(mock_adapter_); | 62 event_router()->SetAdapterForTest(mock_adapter_); |
| 62 | 63 |
| 63 device1_.reset(new testing::NiceMock<MockBluetoothDevice>( | 64 device1_.reset(new testing::NiceMock<MockBluetoothDevice>( |
| 64 mock_adapter_, 0, "d1", "11:12:13:14:15:16", | 65 mock_adapter_, 0, "d1", "11:12:13:14:15:16", |
| 65 true /* paired */, true /* connected */)); | 66 true /* paired */, true /* connected */)); |
| 66 device2_.reset(new testing::NiceMock<MockBluetoothDevice>( | 67 device2_.reset(new testing::NiceMock<MockBluetoothDevice>( |
| 67 mock_adapter_, 0, "d2", "21:22:23:24:25:26", | 68 mock_adapter_, 0, "d2", "21:22:23:24:25:26", |
| 68 false /* paired */, false /* connected */)); | 69 false /* paired */, false /* connected */)); |
| 70 device3_.reset(new testing::NiceMock<MockBluetoothDevice>( | |
| 71 mock_adapter_, 0, "d3", "31:32:33:34:35:36", | |
| 72 false /* paired */, false /* connected */)); | |
| 73 | |
| 69 } | 74 } |
| 70 | 75 |
| 71 void DiscoverySessionCallback( | 76 void DiscoverySessionCallback( |
| 72 const BluetoothAdapter::DiscoverySessionCallback& callback, | 77 const BluetoothAdapter::DiscoverySessionCallback& callback, |
| 73 const BluetoothAdapter::ErrorCallback& error_callback) { | 78 const BluetoothAdapter::ErrorCallback& error_callback) { |
| 74 if (mock_session_.get()) { | 79 if (mock_session_.get()) { |
| 75 callback.Run( | 80 callback.Run( |
| 76 scoped_ptr<BluetoothDiscoverySession>(mock_session_.release())); | 81 scoped_ptr<BluetoothDiscoverySession>(mock_session_.release())); |
| 77 return; | 82 return; |
| 78 } | 83 } |
| 79 error_callback.Run(); | 84 error_callback.Run(); |
| 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<MockBluetoothAdapter>* mock_adapter_; | 95 testing::StrictMock<MockBluetoothAdapter>* mock_adapter_; |
| 91 scoped_ptr<testing::NiceMock<MockBluetoothDiscoverySession> > mock_session_; | 96 scoped_ptr<testing::NiceMock<MockBluetoothDiscoverySession> > mock_session_; |
| 92 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device1_; | 97 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device1_; |
| 93 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device2_; | 98 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device2_; |
| 99 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device3_; | |
| 94 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile1_; | 100 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile1_; |
| 95 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile2_; | 101 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile2_; |
| 96 | 102 |
| 97 extensions::ExtensionBluetoothEventRouter* event_router() { | 103 extensions::ExtensionBluetoothEventRouter* event_router() { |
| 98 return extensions::BluetoothAPI::Get(browser()->profile()) | 104 return extensions::BluetoothAPI::Get(browser()->profile()) |
| 99 ->bluetooth_event_router(); | 105 ->bluetooth_event_router(); |
| 100 } | 106 } |
| 101 | 107 |
| 102 private: | 108 private: |
| 103 scoped_refptr<Extension> empty_extension_; | 109 scoped_refptr<Extension> empty_extension_; |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 new api::BluetoothSetOutOfBandPairingDataFunction); | 322 new api::BluetoothSetOutOfBandPairingDataFunction); |
| 317 std::string error(utils::RunFunctionAndReturnError( | 323 std::string error(utils::RunFunctionAndReturnError( |
| 318 set_oob_function.get(), params, browser())); | 324 set_oob_function.get(), params, browser())); |
| 319 EXPECT_FALSE(error.empty()); | 325 EXPECT_FALSE(error.empty()); |
| 320 | 326 |
| 321 // TODO(bryeung): Also test setting the data when there is support for | 327 // TODO(bryeung): Also test setting the data when there is support for |
| 322 // ArrayBuffers in the arguments to the RunFunctionAnd* methods. | 328 // ArrayBuffers in the arguments to the RunFunctionAnd* methods. |
| 323 // crbug.com/132796 | 329 // crbug.com/132796 |
| 324 } | 330 } |
| 325 | 331 |
| 332 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DeviceEvents) { | |
| 333 ResultCatcher catcher; | |
| 334 catcher.RestrictToProfile(browser()->profile()); | |
| 335 | |
| 336 ASSERT_TRUE(LoadExtension( | |
| 337 test_data_dir_.AppendASCII("bluetooth/device_events"))); | |
| 338 | |
| 339 ExtensionTestMessageListener events_received("ready", true); | |
| 340 event_router()->DeviceAdded(mock_adapter_, device1_.get()); | |
| 341 event_router()->DeviceAdded(mock_adapter_, device2_.get()); | |
| 342 | |
| 343 EXPECT_CALL(*device2_.get(), GetDeviceName()) | |
| 344 .WillRepeatedly(testing::Return("the real d2")); | |
| 345 EXPECT_CALL(*device2_.get(), GetName()) | |
| 346 .WillRepeatedly(testing::Return(base::UTF8ToUTF16("the real d2"))); | |
| 347 event_router()->DeviceChanged(mock_adapter_, device2_.get()); | |
| 348 | |
| 349 event_router()->DeviceAdded(mock_adapter_, device3_.get()); | |
| 350 event_router()->DeviceRemoved(mock_adapter_, device1_.get()); | |
| 351 EXPECT_TRUE(events_received.WaitUntilSatisfied()); | |
| 352 events_received.Reply("go"); | |
| 353 | |
| 354 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | |
| 355 } | |
| 356 | |
| 326 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) { | 357 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) { |
| 327 // Try with a failure to start. This will return an error as we haven't | 358 // Try with a failure to start. This will return an error as we haven't |
| 328 // initialied a session object. | 359 // initialied a session object. |
| 329 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_)) | 360 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_)) |
| 330 .WillOnce( | 361 .WillOnce( |
| 331 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback)); | 362 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback)); |
| 332 | 363 |
| 333 // StartDiscovery failure will remove the adapter that is no longer used. | 364 // StartDiscovery failure will not reference the adapter. |
| 334 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)); | |
| 335 scoped_refptr<api::BluetoothStartDiscoveryFunction> start_function; | 365 scoped_refptr<api::BluetoothStartDiscoveryFunction> start_function; |
| 336 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction); | 366 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction); |
| 337 std::string error( | 367 std::string error( |
| 338 utils::RunFunctionAndReturnError(start_function.get(), "[]", browser())); | 368 utils::RunFunctionAndReturnError(start_function.get(), "[]", browser())); |
| 339 ASSERT_FALSE(error.empty()); | 369 ASSERT_FALSE(error.empty()); |
| 340 | 370 |
| 341 // Reset the adapter and initiate a discovery session. The ownership of the | 371 // Reset the adapter and initiate a discovery session. The ownership of the |
| 342 // mock session will be passed to the event router. | 372 // mock session will be passed to the event router. |
| 343 ASSERT_FALSE(mock_session_.get()); | 373 ASSERT_FALSE(mock_session_.get()); |
| 344 SetUpMockAdapter(); | 374 SetUpMockAdapter(); |
| 345 | 375 |
| 346 // Create a mock session to be returned as a result. Get a handle to it as | 376 // Create a mock session to be returned as a result. Get a handle to it as |
| 347 // its ownership will be passed and |mock_session_| will be reset. | 377 // its ownership will be passed and |mock_session_| will be reset. |
| 348 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>( | 378 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>( |
| 349 scoped_refptr<BluetoothAdapter>(mock_adapter_))); | 379 scoped_refptr<BluetoothAdapter>(mock_adapter_))); |
| 350 MockBluetoothDiscoverySession* session = mock_session_.get(); | 380 MockBluetoothDiscoverySession* session = mock_session_.get(); |
| 351 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_)) | 381 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_)) |
| 352 .WillOnce( | 382 .WillOnce( |
| 353 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback)); | 383 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback)); |
| 354 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction); | 384 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction); |
| 355 (void) | 385 (void) |
| 356 utils::RunFunctionAndReturnError(start_function.get(), "[]", browser()); | 386 utils::RunFunctionAndReturnError(start_function.get(), "[]", browser()); |
| 357 | 387 |
| 358 // End the discovery session. The StopDiscovery function should succeed. | 388 // End the discovery session. The StopDiscovery function should succeed. |
| 359 testing::Mock::VerifyAndClearExpectations(mock_adapter_); | 389 testing::Mock::VerifyAndClearExpectations(mock_adapter_); |
| 360 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(true)); | 390 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(true)); |
| 361 EXPECT_CALL(*session, Stop(testing::_, testing::_)) | 391 EXPECT_CALL(*session, Stop(testing::_, testing::_)) |
| 362 .WillOnce(testing::Invoke(StopDiscoverySessionCallback)); | 392 .WillOnce(testing::Invoke(StopDiscoverySessionCallback)); |
| 363 | 393 |
| 364 // StopDiscovery success will remove the adapter that is no longer used. | 394 // StopDiscovery success will remove the session object, unreferncing the |
|
armansito
2014/03/05 21:44:15
s/unreferncing/unreferencing/
keybuk
2014/03/06 21:33:44
Done.
| |
| 365 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)); | 395 // adapter. |
| 366 scoped_refptr<api::BluetoothStopDiscoveryFunction> stop_function; | 396 scoped_refptr<api::BluetoothStopDiscoveryFunction> stop_function; |
| 367 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction); | 397 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction); |
| 368 (void) utils::RunFunctionAndReturnSingleResult( | 398 (void) utils::RunFunctionAndReturnSingleResult( |
| 369 stop_function.get(), "[]", browser()); | 399 stop_function.get(), "[]", browser()); |
| 370 | 400 |
| 371 // Reset the adapter. Simulate failure for stop discovery. The event router | 401 // Reset the adapter. Simulate failure for stop discovery. The event router |
| 372 // still owns the session. Make it appear inactive. | 402 // still owns the session. Make it appear inactive. |
| 373 SetUpMockAdapter(); | 403 SetUpMockAdapter(); |
| 374 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(false)); | 404 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(false)); |
| 375 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)); | |
| 376 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction); | 405 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction); |
| 377 error = | 406 error = |
| 378 utils::RunFunctionAndReturnError(stop_function.get(), "[]", browser()); | 407 utils::RunFunctionAndReturnError(stop_function.get(), "[]", browser()); |
| 379 ASSERT_FALSE(error.empty()); | 408 ASSERT_FALSE(error.empty()); |
| 380 SetUpMockAdapter(); | 409 SetUpMockAdapter(); |
| 381 } | 410 } |
| 382 | 411 |
| 383 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DiscoveryCallback) { | 412 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DiscoveryCallback) { |
| 384 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>( | 413 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>( |
| 385 scoped_refptr<BluetoothAdapter>(mock_adapter_))); | 414 scoped_refptr<BluetoothAdapter>(mock_adapter_))); |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 567 } | 596 } |
| 568 | 597 |
| 569 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) { | 598 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) { |
| 570 ResultCatcher catcher; | 599 ResultCatcher catcher; |
| 571 catcher.RestrictToProfile(browser()->profile()); | 600 catcher.RestrictToProfile(browser()->profile()); |
| 572 | 601 |
| 573 BluetoothAdapter::ConstDeviceList devices; | 602 BluetoothAdapter::ConstDeviceList devices; |
| 574 devices.push_back(device1_.get()); | 603 devices.push_back(device1_.get()); |
| 575 devices.push_back(device2_.get()); | 604 devices.push_back(device2_.get()); |
| 576 | 605 |
| 577 EXPECT_CALL(*device1_, ProvidesServiceWithUUID(testing::_)) | |
| 578 .WillOnce(testing::Return(false)); | |
| 579 | |
| 580 EXPECT_CALL(*device2_, ProvidesServiceWithUUID(testing::_)) | |
| 581 .WillOnce(testing::Return(true)); | |
| 582 | |
| 583 EXPECT_CALL(*mock_adapter_, GetDevices()) | 606 EXPECT_CALL(*mock_adapter_, GetDevices()) |
| 584 .Times(2) | 607 .Times(1) |
| 585 .WillRepeatedly(testing::Return(devices)); | 608 .WillRepeatedly(testing::Return(devices)); |
| 586 | 609 |
| 587 // Load and wait for setup | 610 // Load and wait for setup |
| 588 ExtensionTestMessageListener listener("ready", true); | 611 ExtensionTestMessageListener listener("ready", true); |
| 589 ASSERT_TRUE( | 612 ASSERT_TRUE( |
| 590 LoadExtension(test_data_dir_.AppendASCII("bluetooth/get_devices"))); | 613 LoadExtension(test_data_dir_.AppendASCII("bluetooth/get_devices"))); |
| 591 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 614 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 592 | 615 |
| 593 listener.Reply("go"); | 616 listener.Reply("go"); |
| 594 | 617 |
| 595 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 618 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 596 } | 619 } |
| 597 | |
| 598 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevicesError) { | |
| 599 ResultCatcher catcher; | |
| 600 catcher.RestrictToProfile(browser()->profile()); | |
| 601 | |
| 602 // Load and wait for setup | |
| 603 ExtensionTestMessageListener listener("ready", true); | |
| 604 ASSERT_TRUE(LoadExtension( | |
| 605 test_data_dir_.AppendASCII("bluetooth/get_devices_error"))); | |
| 606 EXPECT_TRUE(listener.WaitUntilSatisfied()); | |
| 607 | |
| 608 listener.Reply("go"); | |
| 609 | |
| 610 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | |
| 611 } | |
| OLD | NEW |