Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(342)

Side by Side Diff: chrome/browser/extensions/api/bluetooth/bluetooth_apitest.cc

Issue 189463002: Migrate chrome.bluetooth API backend to use device::BluetoothDiscoverySession. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" 8 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h"
9 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" 9 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h"
10 #include "chrome/browser/extensions/extension_apitest.h" 10 #include "chrome/browser/extensions/extension_apitest.h"
11 #include "chrome/browser/extensions/extension_function_test_utils.h" 11 #include "chrome/browser/extensions/extension_function_test_utils.h"
12 #include "chrome/browser/extensions/extension_service.h" 12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_test_message_listener.h" 13 #include "chrome/browser/extensions/extension_test_message_listener.h"
14 #include "chrome/browser/ui/browser.h" 14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/test/base/ui_test_utils.h" 15 #include "chrome/test/base/ui_test_utils.h"
16 #include "device/bluetooth/bluetooth_adapter.h" 16 #include "device/bluetooth/bluetooth_adapter.h"
17 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" 17 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h"
18 #include "device/bluetooth/test/mock_bluetooth_adapter.h" 18 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
19 #include "device/bluetooth/test/mock_bluetooth_device.h" 19 #include "device/bluetooth/test/mock_bluetooth_device.h"
20 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h"
20 #include "device/bluetooth/test/mock_bluetooth_profile.h" 21 #include "device/bluetooth/test/mock_bluetooth_profile.h"
21 #include "device/bluetooth/test/mock_bluetooth_socket.h" 22 #include "device/bluetooth/test/mock_bluetooth_socket.h"
22 #include "testing/gmock/include/gmock/gmock.h" 23 #include "testing/gmock/include/gmock/gmock.h"
23 24
24 using device::BluetoothAdapter; 25 using device::BluetoothAdapter;
25 using device::BluetoothDevice; 26 using device::BluetoothDevice;
27 using device::BluetoothDiscoverySession;
26 using device::BluetoothOutOfBandPairingData; 28 using device::BluetoothOutOfBandPairingData;
27 using device::BluetoothProfile; 29 using device::BluetoothProfile;
28 using device::MockBluetoothAdapter; 30 using device::MockBluetoothAdapter;
29 using device::MockBluetoothDevice; 31 using device::MockBluetoothDevice;
32 using device::MockBluetoothDiscoverySession;
30 using device::MockBluetoothProfile; 33 using device::MockBluetoothProfile;
31 using extensions::Extension; 34 using extensions::Extension;
32 35
33 namespace utils = extension_function_test_utils; 36 namespace utils = extension_function_test_utils;
34 namespace api = extensions::api; 37 namespace api = extensions::api;
35 38
36 namespace { 39 namespace {
37 40
38 static const char* kAdapterAddress = "A1:A2:A3:A4:A5:A6"; 41 static const char* kAdapterAddress = "A1:A2:A3:A4:A5:A6";
39 static const char* kName = "whatsinaname"; 42 static const char* kName = "whatsinaname";
(...skipping 18 matching lines...) Expand all
58 event_router()->SetAdapterForTest(mock_adapter_); 61 event_router()->SetAdapterForTest(mock_adapter_);
59 62
60 device1_.reset(new testing::NiceMock<MockBluetoothDevice>( 63 device1_.reset(new testing::NiceMock<MockBluetoothDevice>(
61 mock_adapter_, 0, "d1", "11:12:13:14:15:16", 64 mock_adapter_, 0, "d1", "11:12:13:14:15:16",
62 true /* paired */, true /* connected */)); 65 true /* paired */, true /* connected */));
63 device2_.reset(new testing::NiceMock<MockBluetoothDevice>( 66 device2_.reset(new testing::NiceMock<MockBluetoothDevice>(
64 mock_adapter_, 0, "d2", "21:22:23:24:25:26", 67 mock_adapter_, 0, "d2", "21:22:23:24:25:26",
65 false /* paired */, false /* connected */)); 68 false /* paired */, false /* connected */));
66 } 69 }
67 70
71 void DiscoverySessionCallback(
72 const BluetoothAdapter::DiscoverySessionCallback& callback,
73 const BluetoothAdapter::ErrorCallback& error_callback) {
74 if (mock_session_.get()) {
75 callback.Run(
76 scoped_ptr<BluetoothDiscoverySession>(mock_session_.release()));
77 return;
78 }
79 error_callback.Run();
80 }
81
68 template <class T> 82 template <class T>
69 T* setupFunction(T* function) { 83 T* setupFunction(T* function) {
70 function->set_extension(empty_extension_.get()); 84 function->set_extension(empty_extension_.get());
71 function->set_has_callback(true); 85 function->set_has_callback(true);
72 return function; 86 return function;
73 } 87 }
74 88
75 protected: 89 protected:
76 testing::StrictMock<MockBluetoothAdapter>* mock_adapter_; 90 testing::StrictMock<MockBluetoothAdapter>* mock_adapter_;
91 scoped_ptr<testing::NiceMock<MockBluetoothDiscoverySession> > mock_session_;
77 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device1_; 92 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device1_;
78 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device2_; 93 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device2_;
79 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile1_; 94 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile1_;
80 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile2_; 95 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile2_;
81 96
82 extensions::ExtensionBluetoothEventRouter* event_router() { 97 extensions::ExtensionBluetoothEventRouter* event_router() {
83 return extensions::BluetoothAPI::Get(browser()->profile()) 98 return extensions::BluetoothAPI::Get(browser()->profile())
84 ->bluetooth_event_router(); 99 ->bluetooth_event_router();
85 } 100 }
86 101
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 memcpy(&(data.randomizer), kOutOfBandPairingDataRandomizer, 137 memcpy(&(data.randomizer), kOutOfBandPairingDataRandomizer,
123 device::kBluetoothOutOfBandPairingDataSize); 138 device::kBluetoothOutOfBandPairingDataSize);
124 return data; 139 return data;
125 } 140 }
126 141
127 static bool CallClosure(const base::Closure& callback) { 142 static bool CallClosure(const base::Closure& callback) {
128 callback.Run(); 143 callback.Run();
129 return true; 144 return true;
130 } 145 }
131 146
132 static void CallDiscoveryCallback( 147 static void StopDiscoverySessionCallback(const base::Closure& callback,
133 const base::Closure& callback, 148 const base::Closure& error_callback) {
134 const BluetoothAdapter::ErrorCallback& error_callback) {
135 callback.Run(); 149 callback.Run();
136 } 150 }
137 151
138 static void CallDiscoveryErrorCallback(
139 const base::Closure& callback,
140 const BluetoothAdapter::ErrorCallback& error_callback) {
141 error_callback.Run();
142 }
143
144 static void CallOutOfBandPairingDataCallback( 152 static void CallOutOfBandPairingDataCallback(
145 const BluetoothAdapter::BluetoothOutOfBandPairingDataCallback& callback, 153 const BluetoothAdapter::BluetoothOutOfBandPairingDataCallback& callback,
146 const BluetoothAdapter::ErrorCallback& error_callback) { 154 const BluetoothAdapter::ErrorCallback& error_callback) {
147 callback.Run(GetOutOfBandPairingData()); 155 callback.Run(GetOutOfBandPairingData());
148 } 156 }
149 157
150 } // namespace 158 } // namespace
151 159
152 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Profiles) { 160 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Profiles) {
153 // Run in context of an extension that has permissions for the profiles 161 // Run in context of an extension that has permissions for the profiles
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 std::string error(utils::RunFunctionAndReturnError( 317 std::string error(utils::RunFunctionAndReturnError(
310 set_oob_function.get(), params, browser())); 318 set_oob_function.get(), params, browser()));
311 EXPECT_FALSE(error.empty()); 319 EXPECT_FALSE(error.empty());
312 320
313 // TODO(bryeung): Also test setting the data when there is support for 321 // TODO(bryeung): Also test setting the data when there is support for
314 // ArrayBuffers in the arguments to the RunFunctionAnd* methods. 322 // ArrayBuffers in the arguments to the RunFunctionAnd* methods.
315 // crbug.com/132796 323 // crbug.com/132796
316 } 324 }
317 325
318 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) { 326 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) {
319 // Try with a failure to start 327 // Try with a failure to start. This will return an error as we haven't
320 EXPECT_CALL(*mock_adapter_, StartDiscovering(testing::_, testing::_)) 328 // initialied a session object.
321 .WillOnce(testing::Invoke(CallDiscoveryErrorCallback)); 329 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_))
330 .WillOnce(
331 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback));
332
322 // StartDiscovery failure will remove the adapter that is no longer used. 333 // StartDiscovery failure will remove the adapter that is no longer used.
323 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)); 334 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_));
324 scoped_refptr<api::BluetoothStartDiscoveryFunction> start_function; 335 scoped_refptr<api::BluetoothStartDiscoveryFunction> start_function;
325 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction); 336 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction);
326 std::string error( 337 std::string error(
327 utils::RunFunctionAndReturnError(start_function.get(), "[]", browser())); 338 utils::RunFunctionAndReturnError(start_function.get(), "[]", browser()));
328 ASSERT_FALSE(error.empty()); 339 ASSERT_FALSE(error.empty());
329 340
330 // Reset for a successful start 341 // Reset the adapter and initiate a discovery session. The ownership of the
342 // mock session will be passed to the event router.
343 ASSERT_FALSE(mock_session_.get());
331 SetUpMockAdapter(); 344 SetUpMockAdapter();
332 EXPECT_CALL(*mock_adapter_, StartDiscovering(testing::_, testing::_))
333 .WillOnce(testing::Invoke(CallDiscoveryCallback));
334 345
346 // 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.
348 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>());
349 MockBluetoothDiscoverySession* session = mock_session_.get();
350 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_))
351 .WillOnce(
352 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback));
335 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction); 353 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction);
336 (void) 354 (void)
337 utils::RunFunctionAndReturnError(start_function.get(), "[]", browser()); 355 utils::RunFunctionAndReturnError(start_function.get(), "[]", browser());
338 356
339 // Reset to try stopping 357 // End the discovery session. The StopDiscovery function should succeed.
340 testing::Mock::VerifyAndClearExpectations(mock_adapter_); 358 testing::Mock::VerifyAndClearExpectations(mock_adapter_);
341 EXPECT_CALL(*mock_adapter_, StopDiscovering(testing::_, testing::_)) 359 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(true));
342 .WillOnce(testing::Invoke(CallDiscoveryCallback)); 360 EXPECT_CALL(*session, Stop(testing::_, testing::_))
361 .WillOnce(testing::Invoke(StopDiscoverySessionCallback));
362
343 // StopDiscovery success will remove the adapter that is no longer used. 363 // StopDiscovery success will remove the adapter that is no longer used.
344 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)); 364 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_));
345 scoped_refptr<api::BluetoothStopDiscoveryFunction> stop_function; 365 scoped_refptr<api::BluetoothStopDiscoveryFunction> stop_function;
346 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction); 366 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction);
347 (void) utils::RunFunctionAndReturnSingleResult( 367 (void) utils::RunFunctionAndReturnSingleResult(
348 stop_function.get(), "[]", browser()); 368 stop_function.get(), "[]", browser());
349 369
350 // Reset to try stopping with an error 370 // Reset the adapter. Simulate failure for stop discovery. The event router
371 // still owns the session. Make it appear inactive.
351 SetUpMockAdapter(); 372 SetUpMockAdapter();
352 EXPECT_CALL(*mock_adapter_, StopDiscovering(testing::_, testing::_)) 373 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(false));
353 .WillOnce(testing::Invoke(CallDiscoveryErrorCallback));
354 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)); 374 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_));
355 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction); 375 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction);
356 error = 376 error =
357 utils::RunFunctionAndReturnError(stop_function.get(), "[]", browser()); 377 utils::RunFunctionAndReturnError(stop_function.get(), "[]", browser());
358 ASSERT_FALSE(error.empty()); 378 ASSERT_FALSE(error.empty());
359 SetUpMockAdapter(); 379 SetUpMockAdapter();
360 } 380 }
361 381
362 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DiscoveryCallback) { 382 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DiscoveryCallback) {
363 EXPECT_CALL(*mock_adapter_, StartDiscovering(testing::_, testing::_)) 383 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>());
364 .WillOnce(testing::Invoke(CallDiscoveryCallback)); 384 MockBluetoothDiscoverySession* session = mock_session_.get();
365 EXPECT_CALL(*mock_adapter_, StopDiscovering(testing::_, testing::_)) 385 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_))
366 .WillOnce(testing::Invoke(CallDiscoveryCallback)); 386 .WillOnce(
387 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback));
388 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(true));
389 EXPECT_CALL(*session, Stop(testing::_, testing::_))
390 .WillOnce(testing::Invoke(StopDiscoverySessionCallback));
367 391
368 ResultCatcher catcher; 392 ResultCatcher catcher;
369 catcher.RestrictToProfile(browser()->profile()); 393 catcher.RestrictToProfile(browser()->profile());
370 394
371 ExtensionTestMessageListener discovery_started("ready", true); 395 ExtensionTestMessageListener discovery_started("ready", true);
372 ASSERT_TRUE(LoadExtension( 396 ASSERT_TRUE(LoadExtension(
373 test_data_dir_.AppendASCII("bluetooth/discovery_callback"))); 397 test_data_dir_.AppendASCII("bluetooth/discovery_callback")));
374 EXPECT_TRUE(discovery_started.WaitUntilSatisfied()); 398 EXPECT_TRUE(discovery_started.WaitUntilSatisfied());
375 399
376 event_router()->DeviceAdded(mock_adapter_, device1_.get()); 400 event_router()->DeviceAdded(mock_adapter_, device1_.get());
(...skipping 24 matching lines...) Expand all
401 EXPECT_CALL(*mock_adapter_, IsDiscovering()) 425 EXPECT_CALL(*mock_adapter_, IsDiscovering())
402 .WillOnce(testing::Return(true)); 426 .WillOnce(testing::Return(true));
403 event_router()->AdapterDiscoveringChanged(mock_adapter_, true); 427 event_router()->AdapterDiscoveringChanged(mock_adapter_, true);
404 428
405 // Cache a device before the extension starts discovering 429 // Cache a device before the extension starts discovering
406 event_router()->DeviceAdded(mock_adapter_, device1_.get()); 430 event_router()->DeviceAdded(mock_adapter_, device1_.get());
407 431
408 ResultCatcher catcher; 432 ResultCatcher catcher;
409 catcher.RestrictToProfile(browser()->profile()); 433 catcher.RestrictToProfile(browser()->profile());
410 434
411 EXPECT_CALL(*mock_adapter_, StartDiscovering(testing::_, testing::_)) 435 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>());
412 .WillOnce(testing::Invoke(CallDiscoveryCallback)); 436 MockBluetoothDiscoverySession* session = mock_session_.get();
413 EXPECT_CALL(*mock_adapter_, StopDiscovering(testing::_, testing::_)) 437 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_))
414 .WillOnce(testing::Invoke(CallDiscoveryCallback)); 438 .WillOnce(
439 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback));
440 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(true));
441 EXPECT_CALL(*session, Stop(testing::_, testing::_))
442 .WillOnce(testing::Invoke(StopDiscoverySessionCallback));
415 443
416 ExtensionTestMessageListener discovery_started("ready", true); 444 ExtensionTestMessageListener discovery_started("ready", true);
417 ASSERT_TRUE(LoadExtension( 445 ASSERT_TRUE(LoadExtension(
418 test_data_dir_.AppendASCII("bluetooth/discovery_in_progress"))); 446 test_data_dir_.AppendASCII("bluetooth/discovery_in_progress")));
419 EXPECT_TRUE(discovery_started.WaitUntilSatisfied()); 447 EXPECT_TRUE(discovery_started.WaitUntilSatisfied());
420 448
421 // This should be received in addition to the cached device above. 449 // Only this should be received. No additional notification should be sent for
450 // devices discovered before the discovery session started.
422 event_router()->DeviceAdded(mock_adapter_, device2_.get()); 451 event_router()->DeviceAdded(mock_adapter_, device2_.get());
423 452
424 discovery_started.Reply("go"); 453 discovery_started.Reply("go");
425 ExtensionTestMessageListener discovery_stopped("ready", true); 454 ExtensionTestMessageListener discovery_stopped("ready", true);
426 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)); 455 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_));
427 EXPECT_TRUE(discovery_stopped.WaitUntilSatisfied()); 456 EXPECT_TRUE(discovery_stopped.WaitUntilSatisfied());
428 457
429 SetUpMockAdapter(); 458 SetUpMockAdapter();
430 // This should never be received. 459 // This should never be received.
431 event_router()->DeviceAdded(mock_adapter_, device2_.get()); 460 event_router()->DeviceAdded(mock_adapter_, device2_.get());
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
570 // Load and wait for setup 599 // Load and wait for setup
571 ExtensionTestMessageListener listener("ready", true); 600 ExtensionTestMessageListener listener("ready", true);
572 ASSERT_TRUE(LoadExtension( 601 ASSERT_TRUE(LoadExtension(
573 test_data_dir_.AppendASCII("bluetooth/get_devices_error"))); 602 test_data_dir_.AppendASCII("bluetooth/get_devices_error")));
574 EXPECT_TRUE(listener.WaitUntilSatisfied()); 603 EXPECT_TRUE(listener.WaitUntilSatisfied());
575 604
576 listener.Reply("go"); 605 listener.Reply("go");
577 606
578 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); 607 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
579 } 608 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698