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