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