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