| 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" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 | 63 |
| 64 device1_.reset(new testing::NiceMock<MockBluetoothDevice>( | 64 device1_.reset(new testing::NiceMock<MockBluetoothDevice>( |
| 65 mock_adapter_, 0, "d1", "11:12:13:14:15:16", | 65 mock_adapter_, 0, "d1", "11:12:13:14:15:16", |
| 66 true /* paired */, true /* connected */)); | 66 true /* paired */, true /* connected */)); |
| 67 device2_.reset(new testing::NiceMock<MockBluetoothDevice>( | 67 device2_.reset(new testing::NiceMock<MockBluetoothDevice>( |
| 68 mock_adapter_, 0, "d2", "21:22:23:24:25:26", | 68 mock_adapter_, 0, "d2", "21:22:23:24:25:26", |
| 69 false /* paired */, false /* connected */)); | 69 false /* paired */, false /* connected */)); |
| 70 device3_.reset(new testing::NiceMock<MockBluetoothDevice>( | 70 device3_.reset(new testing::NiceMock<MockBluetoothDevice>( |
| 71 mock_adapter_, 0, "d3", "31:32:33:34:35:36", | 71 mock_adapter_, 0, "d3", "31:32:33:34:35:36", |
| 72 false /* paired */, false /* connected */)); | 72 false /* paired */, false /* connected */)); |
| 73 | |
| 74 } | 73 } |
| 75 | 74 |
| 76 void DiscoverySessionCallback( | 75 void DiscoverySessionCallback( |
| 77 const BluetoothAdapter::DiscoverySessionCallback& callback, | 76 const BluetoothAdapter::DiscoverySessionCallback& callback, |
| 78 const BluetoothAdapter::ErrorCallback& error_callback) { | 77 const BluetoothAdapter::ErrorCallback& error_callback) { |
| 79 if (mock_session_.get()) { | 78 if (mock_session_.get()) { |
| 80 callback.Run( | 79 callback.Run( |
| 81 scoped_ptr<BluetoothDiscoverySession>(mock_session_.release())); | 80 scoped_ptr<BluetoothDiscoverySession>(mock_session_.release())); |
| 82 return; | 81 return; |
| 83 } | 82 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 94 protected: | 93 protected: |
| 95 testing::StrictMock<MockBluetoothAdapter>* mock_adapter_; | 94 testing::StrictMock<MockBluetoothAdapter>* mock_adapter_; |
| 96 scoped_ptr<testing::NiceMock<MockBluetoothDiscoverySession> > mock_session_; | 95 scoped_ptr<testing::NiceMock<MockBluetoothDiscoverySession> > mock_session_; |
| 97 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device1_; | 96 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device1_; |
| 98 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device2_; | 97 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device2_; |
| 99 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device3_; | 98 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device3_; |
| 100 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile1_; | 99 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile1_; |
| 101 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile2_; | 100 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile2_; |
| 102 | 101 |
| 103 extensions::ExtensionBluetoothEventRouter* event_router() { | 102 extensions::ExtensionBluetoothEventRouter* event_router() { |
| 104 return extensions::BluetoothAPI::Get(browser()->profile()) | 103 return bluetooth_api()->event_router(); |
| 105 ->bluetooth_event_router(); | 104 } |
| 105 |
| 106 extensions::BluetoothAPI* bluetooth_api() { |
| 107 return extensions::BluetoothAPI::Get(browser()->profile()); |
| 106 } | 108 } |
| 107 | 109 |
| 108 private: | 110 private: |
| 109 scoped_refptr<Extension> empty_extension_; | 111 scoped_refptr<Extension> empty_extension_; |
| 110 }; | 112 }; |
| 111 | 113 |
| 112 class TestBluetoothAddProfileFunction | 114 class TestBluetoothAddProfileFunction |
| 113 : public api::BluetoothAddProfileFunction { | 115 : public api::BluetoothAddProfileFunction { |
| 114 public: | 116 public: |
| 115 explicit TestBluetoothAddProfileFunction(BluetoothProfile* profile) | 117 explicit TestBluetoothAddProfileFunction(BluetoothProfile* profile) |
| (...skipping 27 matching lines...) Expand all Loading... |
| 143 memcpy(&(data.randomizer), kOutOfBandPairingDataRandomizer, | 145 memcpy(&(data.randomizer), kOutOfBandPairingDataRandomizer, |
| 144 device::kBluetoothOutOfBandPairingDataSize); | 146 device::kBluetoothOutOfBandPairingDataSize); |
| 145 return data; | 147 return data; |
| 146 } | 148 } |
| 147 | 149 |
| 148 static bool CallClosure(const base::Closure& callback) { | 150 static bool CallClosure(const base::Closure& callback) { |
| 149 callback.Run(); | 151 callback.Run(); |
| 150 return true; | 152 return true; |
| 151 } | 153 } |
| 152 | 154 |
| 155 static bool CallErrorClosure(const BluetoothDevice::ErrorCallback& callback) { |
| 156 callback.Run(); |
| 157 return true; |
| 158 } |
| 159 |
| 153 static void StopDiscoverySessionCallback(const base::Closure& callback, | 160 static void StopDiscoverySessionCallback(const base::Closure& callback, |
| 154 const base::Closure& error_callback) { | 161 const base::Closure& error_callback) { |
| 155 callback.Run(); | 162 callback.Run(); |
| 156 } | 163 } |
| 157 | 164 |
| 158 static void CallOutOfBandPairingDataCallback( | 165 static void CallOutOfBandPairingDataCallback( |
| 159 const BluetoothAdapter::BluetoothOutOfBandPairingDataCallback& callback, | 166 const BluetoothAdapter::BluetoothOutOfBandPairingDataCallback& callback, |
| 160 const BluetoothAdapter::ErrorCallback& error_callback) { | 167 const BluetoothAdapter::ErrorCallback& error_callback) { |
| 161 callback.Run(GetOutOfBandPairingData()); | 168 callback.Run(GetOutOfBandPairingData()); |
| 162 } | 169 } |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 new api::BluetoothSetOutOfBandPairingDataFunction); | 314 new api::BluetoothSetOutOfBandPairingDataFunction); |
| 308 // There isn't actually a result. | 315 // There isn't actually a result. |
| 309 (void) utils::RunFunctionAndReturnSingleResult( | 316 (void) utils::RunFunctionAndReturnSingleResult( |
| 310 set_oob_function.get(), params, browser()); | 317 set_oob_function.get(), params, browser()); |
| 311 | 318 |
| 312 // Try again with an error | 319 // Try again with an error |
| 313 testing::Mock::VerifyAndClearExpectations(mock_adapter_); | 320 testing::Mock::VerifyAndClearExpectations(mock_adapter_); |
| 314 testing::Mock::VerifyAndClearExpectations(device1_.get()); | 321 testing::Mock::VerifyAndClearExpectations(device1_.get()); |
| 315 EXPECT_CALL(*mock_adapter_, GetDevice(device1_->GetAddress())) | 322 EXPECT_CALL(*mock_adapter_, GetDevice(device1_->GetAddress())) |
| 316 .WillOnce(testing::Return(device1_.get())); | 323 .WillOnce(testing::Return(device1_.get())); |
| 317 EXPECT_CALL(*device1_, | 324 EXPECT_CALL( |
| 318 ClearOutOfBandPairingData(testing::_, | 325 *device1_, |
| 319 testing::Truly(CallClosure))); | 326 ClearOutOfBandPairingData(testing::_, testing::Truly(CallErrorClosure))); |
| 320 | 327 |
| 321 set_oob_function = setupFunction( | 328 set_oob_function = setupFunction( |
| 322 new api::BluetoothSetOutOfBandPairingDataFunction); | 329 new api::BluetoothSetOutOfBandPairingDataFunction); |
| 323 std::string error(utils::RunFunctionAndReturnError( | 330 std::string error(utils::RunFunctionAndReturnError( |
| 324 set_oob_function.get(), params, browser())); | 331 set_oob_function.get(), params, browser())); |
| 325 EXPECT_FALSE(error.empty()); | 332 EXPECT_FALSE(error.empty()); |
| 326 | 333 |
| 327 // TODO(bryeung): Also test setting the data when there is support for | 334 // TODO(bryeung): Also test setting the data when there is support for |
| 328 // ArrayBuffers in the arguments to the RunFunctionAnd* methods. | 335 // ArrayBuffers in the arguments to the RunFunctionAnd* methods. |
| 329 // crbug.com/132796 | 336 // crbug.com/132796 |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 // Load and wait for setup | 558 // Load and wait for setup |
| 552 ExtensionTestMessageListener listener("ready", true); | 559 ExtensionTestMessageListener listener("ready", true); |
| 553 scoped_refptr<const Extension> extension( | 560 scoped_refptr<const Extension> extension( |
| 554 LoadExtension(test_data_dir_.AppendASCII("bluetooth/on_connection"))); | 561 LoadExtension(test_data_dir_.AppendASCII("bluetooth/on_connection"))); |
| 555 ASSERT_TRUE(extension.get()); | 562 ASSERT_TRUE(extension.get()); |
| 556 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 563 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 557 | 564 |
| 558 scoped_refptr<device::MockBluetoothSocket> socket = | 565 scoped_refptr<device::MockBluetoothSocket> socket = |
| 559 new device::MockBluetoothSocket(); | 566 new device::MockBluetoothSocket(); |
| 560 | 567 |
| 568 EXPECT_CALL(*mock_adapter_, GetDevice(device1_->GetAddress())) |
| 569 .WillOnce(testing::Return(device1_.get())); |
| 570 |
| 561 event_router()->AddProfile("1234", extension->id(), profile1_.get()); | 571 event_router()->AddProfile("1234", extension->id(), profile1_.get()); |
| 562 event_router()->DispatchConnectionEvent( | 572 bluetooth_api()->DispatchConnectionEvent( |
| 563 extension->id(), "1234", device1_.get(), socket); | 573 extension->id(), "1234", device1_.get(), socket); |
| 574 // Connection events are dispatched using a couple of PostTask to the UI |
| 575 // thread. Waiting until idle ensures the event is dispatched to the |
| 576 // receiver(s). |
| 577 base::RunLoop().RunUntilIdle(); |
| 564 | 578 |
| 565 listener.Reply("go"); | 579 listener.Reply("go"); |
| 566 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 580 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 567 event_router()->RemoveProfile("1234"); | 581 event_router()->RemoveProfile("1234"); |
| 568 } | 582 } |
| 569 | 583 |
| 570 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetProfiles) { | 584 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetProfiles) { |
| 571 ResultCatcher catcher; | 585 ResultCatcher catcher; |
| 572 catcher.RestrictToProfile(browser()->profile()); | 586 catcher.RestrictToProfile(browser()->profile()); |
| 573 | 587 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 654 // Load and wait for setup | 668 // Load and wait for setup |
| 655 ExtensionTestMessageListener listener("ready", true); | 669 ExtensionTestMessageListener listener("ready", true); |
| 656 ASSERT_TRUE( | 670 ASSERT_TRUE( |
| 657 LoadExtension(test_data_dir_.AppendASCII("bluetooth/device_info"))); | 671 LoadExtension(test_data_dir_.AppendASCII("bluetooth/device_info"))); |
| 658 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 672 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 659 | 673 |
| 660 listener.Reply("go"); | 674 listener.Reply("go"); |
| 661 | 675 |
| 662 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 676 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 663 } | 677 } |
| OLD | NEW |