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 "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" | 9 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" |
9 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" | 10 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" |
10 #include "chrome/browser/extensions/extension_apitest.h" | 11 #include "chrome/browser/extensions/extension_apitest.h" |
11 #include "chrome/browser/extensions/extension_function_test_utils.h" | 12 #include "chrome/browser/extensions/extension_function_test_utils.h" |
12 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
13 #include "chrome/browser/extensions/extension_test_message_listener.h" | 14 #include "chrome/browser/extensions/extension_test_message_listener.h" |
14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/test/base/ui_test_utils.h" | 16 #include "chrome/test/base/ui_test_utils.h" |
16 #include "device/bluetooth/bluetooth_adapter.h" | 17 #include "device/bluetooth/bluetooth_adapter.h" |
17 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" | 18 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 // The browser will clean this up when it is torn down | 60 // The browser will clean this up when it is torn down |
60 mock_adapter_ = new testing::StrictMock<MockBluetoothAdapter>(); | 61 mock_adapter_ = new testing::StrictMock<MockBluetoothAdapter>(); |
61 event_router()->SetAdapterForTest(mock_adapter_); | 62 event_router()->SetAdapterForTest(mock_adapter_); |
62 | 63 |
63 device1_.reset(new testing::NiceMock<MockBluetoothDevice>( | 64 device1_.reset(new testing::NiceMock<MockBluetoothDevice>( |
64 mock_adapter_, 0, "d1", "11:12:13:14:15:16", | 65 mock_adapter_, 0, "d1", "11:12:13:14:15:16", |
65 true /* paired */, true /* connected */)); | 66 true /* paired */, true /* connected */)); |
66 device2_.reset(new testing::NiceMock<MockBluetoothDevice>( | 67 device2_.reset(new testing::NiceMock<MockBluetoothDevice>( |
67 mock_adapter_, 0, "d2", "21:22:23:24:25:26", | 68 mock_adapter_, 0, "d2", "21:22:23:24:25:26", |
68 false /* paired */, false /* connected */)); | 69 false /* paired */, false /* connected */)); |
| 70 device3_.reset(new testing::NiceMock<MockBluetoothDevice>( |
| 71 mock_adapter_, 0, "d3", "31:32:33:34:35:36", |
| 72 false /* paired */, false /* connected */)); |
| 73 |
69 } | 74 } |
70 | 75 |
71 void DiscoverySessionCallback( | 76 void DiscoverySessionCallback( |
72 const BluetoothAdapter::DiscoverySessionCallback& callback, | 77 const BluetoothAdapter::DiscoverySessionCallback& callback, |
73 const BluetoothAdapter::ErrorCallback& error_callback) { | 78 const BluetoothAdapter::ErrorCallback& error_callback) { |
74 if (mock_session_.get()) { | 79 if (mock_session_.get()) { |
75 callback.Run( | 80 callback.Run( |
76 scoped_ptr<BluetoothDiscoverySession>(mock_session_.release())); | 81 scoped_ptr<BluetoothDiscoverySession>(mock_session_.release())); |
77 return; | 82 return; |
78 } | 83 } |
79 error_callback.Run(); | 84 error_callback.Run(); |
80 } | 85 } |
81 | 86 |
82 template <class T> | 87 template <class T> |
83 T* setupFunction(T* function) { | 88 T* setupFunction(T* function) { |
84 function->set_extension(empty_extension_.get()); | 89 function->set_extension(empty_extension_.get()); |
85 function->set_has_callback(true); | 90 function->set_has_callback(true); |
86 return function; | 91 return function; |
87 } | 92 } |
88 | 93 |
89 protected: | 94 protected: |
90 testing::StrictMock<MockBluetoothAdapter>* mock_adapter_; | 95 testing::StrictMock<MockBluetoothAdapter>* mock_adapter_; |
91 scoped_ptr<testing::NiceMock<MockBluetoothDiscoverySession> > mock_session_; | 96 scoped_ptr<testing::NiceMock<MockBluetoothDiscoverySession> > mock_session_; |
92 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device1_; | 97 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device1_; |
93 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device2_; | 98 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device2_; |
| 99 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device3_; |
94 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile1_; | 100 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile1_; |
95 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile2_; | 101 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile2_; |
96 | 102 |
97 extensions::ExtensionBluetoothEventRouter* event_router() { | 103 extensions::ExtensionBluetoothEventRouter* event_router() { |
98 return extensions::BluetoothAPI::Get(browser()->profile()) | 104 return extensions::BluetoothAPI::Get(browser()->profile()) |
99 ->bluetooth_event_router(); | 105 ->bluetooth_event_router(); |
100 } | 106 } |
101 | 107 |
102 private: | 108 private: |
103 scoped_refptr<Extension> empty_extension_; | 109 scoped_refptr<Extension> empty_extension_; |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 new api::BluetoothSetOutOfBandPairingDataFunction); | 322 new api::BluetoothSetOutOfBandPairingDataFunction); |
317 std::string error(utils::RunFunctionAndReturnError( | 323 std::string error(utils::RunFunctionAndReturnError( |
318 set_oob_function.get(), params, browser())); | 324 set_oob_function.get(), params, browser())); |
319 EXPECT_FALSE(error.empty()); | 325 EXPECT_FALSE(error.empty()); |
320 | 326 |
321 // TODO(bryeung): Also test setting the data when there is support for | 327 // TODO(bryeung): Also test setting the data when there is support for |
322 // ArrayBuffers in the arguments to the RunFunctionAnd* methods. | 328 // ArrayBuffers in the arguments to the RunFunctionAnd* methods. |
323 // crbug.com/132796 | 329 // crbug.com/132796 |
324 } | 330 } |
325 | 331 |
| 332 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DeviceEvents) { |
| 333 ResultCatcher catcher; |
| 334 catcher.RestrictToProfile(browser()->profile()); |
| 335 |
| 336 ASSERT_TRUE(LoadExtension( |
| 337 test_data_dir_.AppendASCII("bluetooth/device_events"))); |
| 338 |
| 339 ExtensionTestMessageListener events_received("ready", true); |
| 340 event_router()->DeviceAdded(mock_adapter_, device1_.get()); |
| 341 event_router()->DeviceAdded(mock_adapter_, device2_.get()); |
| 342 |
| 343 EXPECT_CALL(*device2_.get(), GetDeviceName()) |
| 344 .WillRepeatedly(testing::Return("the real d2")); |
| 345 EXPECT_CALL(*device2_.get(), GetName()) |
| 346 .WillRepeatedly(testing::Return(base::UTF8ToUTF16("the real d2"))); |
| 347 event_router()->DeviceChanged(mock_adapter_, device2_.get()); |
| 348 |
| 349 event_router()->DeviceAdded(mock_adapter_, device3_.get()); |
| 350 event_router()->DeviceRemoved(mock_adapter_, device1_.get()); |
| 351 EXPECT_TRUE(events_received.WaitUntilSatisfied()); |
| 352 events_received.Reply("go"); |
| 353 |
| 354 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 355 } |
| 356 |
326 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) { | 357 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) { |
327 // Try with a failure to start. This will return an error as we haven't | 358 // Try with a failure to start. This will return an error as we haven't |
328 // initialied a session object. | 359 // initialied a session object. |
329 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_)) | 360 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_)) |
330 .WillOnce( | 361 .WillOnce( |
331 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback)); | 362 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback)); |
332 | 363 |
333 // StartDiscovery failure will remove the adapter that is no longer used. | 364 // StartDiscovery failure will not reference the adapter. |
334 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)); | |
335 scoped_refptr<api::BluetoothStartDiscoveryFunction> start_function; | 365 scoped_refptr<api::BluetoothStartDiscoveryFunction> start_function; |
336 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction); | 366 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction); |
337 std::string error( | 367 std::string error( |
338 utils::RunFunctionAndReturnError(start_function.get(), "[]", browser())); | 368 utils::RunFunctionAndReturnError(start_function.get(), "[]", browser())); |
339 ASSERT_FALSE(error.empty()); | 369 ASSERT_FALSE(error.empty()); |
340 | 370 |
341 // Reset the adapter and initiate a discovery session. The ownership of the | 371 // Reset the adapter and initiate a discovery session. The ownership of the |
342 // mock session will be passed to the event router. | 372 // mock session will be passed to the event router. |
343 ASSERT_FALSE(mock_session_.get()); | 373 ASSERT_FALSE(mock_session_.get()); |
344 SetUpMockAdapter(); | 374 SetUpMockAdapter(); |
345 | 375 |
346 // Create a mock session to be returned as a result. Get a handle to it as | 376 // 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. | 377 // its ownership will be passed and |mock_session_| will be reset. |
348 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>()); | 378 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>()); |
349 MockBluetoothDiscoverySession* session = mock_session_.get(); | 379 MockBluetoothDiscoverySession* session = mock_session_.get(); |
350 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_)) | 380 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_)) |
351 .WillOnce( | 381 .WillOnce( |
352 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback)); | 382 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback)); |
353 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction); | 383 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction); |
354 (void) | 384 (void) |
355 utils::RunFunctionAndReturnError(start_function.get(), "[]", browser()); | 385 utils::RunFunctionAndReturnError(start_function.get(), "[]", browser()); |
356 | 386 |
357 // End the discovery session. The StopDiscovery function should succeed. | 387 // End the discovery session. The StopDiscovery function should succeed. |
358 testing::Mock::VerifyAndClearExpectations(mock_adapter_); | 388 testing::Mock::VerifyAndClearExpectations(mock_adapter_); |
359 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(true)); | 389 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(true)); |
360 EXPECT_CALL(*session, Stop(testing::_, testing::_)) | 390 EXPECT_CALL(*session, Stop(testing::_, testing::_)) |
361 .WillOnce(testing::Invoke(StopDiscoverySessionCallback)); | 391 .WillOnce(testing::Invoke(StopDiscoverySessionCallback)); |
362 | 392 |
363 // StopDiscovery success will remove the adapter that is no longer used. | 393 // StopDiscovery success will remove the session object, unreferencing the |
364 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)); | 394 // adapter. |
365 scoped_refptr<api::BluetoothStopDiscoveryFunction> stop_function; | 395 scoped_refptr<api::BluetoothStopDiscoveryFunction> stop_function; |
366 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction); | 396 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction); |
367 (void) utils::RunFunctionAndReturnSingleResult( | 397 (void) utils::RunFunctionAndReturnSingleResult( |
368 stop_function.get(), "[]", browser()); | 398 stop_function.get(), "[]", browser()); |
369 | 399 |
370 // Reset the adapter. Simulate failure for stop discovery. The event router | 400 // Reset the adapter. Simulate failure for stop discovery. The event router |
371 // still owns the session. Make it appear inactive. | 401 // still owns the session. Make it appear inactive. |
372 SetUpMockAdapter(); | 402 SetUpMockAdapter(); |
373 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(false)); | 403 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(false)); |
374 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)); | |
375 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction); | 404 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction); |
376 error = | 405 error = |
377 utils::RunFunctionAndReturnError(stop_function.get(), "[]", browser()); | 406 utils::RunFunctionAndReturnError(stop_function.get(), "[]", browser()); |
378 ASSERT_FALSE(error.empty()); | 407 ASSERT_FALSE(error.empty()); |
379 SetUpMockAdapter(); | 408 SetUpMockAdapter(); |
380 } | 409 } |
381 | 410 |
382 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DiscoveryCallback) { | 411 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DiscoveryCallback) { |
383 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>()); | 412 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>()); |
384 MockBluetoothDiscoverySession* session = mock_session_.get(); | 413 MockBluetoothDiscoverySession* session = mock_session_.get(); |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 } | 593 } |
565 | 594 |
566 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) { | 595 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) { |
567 ResultCatcher catcher; | 596 ResultCatcher catcher; |
568 catcher.RestrictToProfile(browser()->profile()); | 597 catcher.RestrictToProfile(browser()->profile()); |
569 | 598 |
570 BluetoothAdapter::ConstDeviceList devices; | 599 BluetoothAdapter::ConstDeviceList devices; |
571 devices.push_back(device1_.get()); | 600 devices.push_back(device1_.get()); |
572 devices.push_back(device2_.get()); | 601 devices.push_back(device2_.get()); |
573 | 602 |
574 EXPECT_CALL(*device1_, ProvidesServiceWithUUID(testing::_)) | |
575 .WillOnce(testing::Return(false)); | |
576 | |
577 EXPECT_CALL(*device2_, ProvidesServiceWithUUID(testing::_)) | |
578 .WillOnce(testing::Return(true)); | |
579 | |
580 EXPECT_CALL(*mock_adapter_, GetDevices()) | 603 EXPECT_CALL(*mock_adapter_, GetDevices()) |
581 .Times(2) | 604 .Times(1) |
582 .WillRepeatedly(testing::Return(devices)); | 605 .WillRepeatedly(testing::Return(devices)); |
583 | 606 |
584 // Load and wait for setup | 607 // Load and wait for setup |
585 ExtensionTestMessageListener listener("ready", true); | 608 ExtensionTestMessageListener listener("ready", true); |
586 ASSERT_TRUE( | 609 ASSERT_TRUE( |
587 LoadExtension(test_data_dir_.AppendASCII("bluetooth/get_devices"))); | 610 LoadExtension(test_data_dir_.AppendASCII("bluetooth/get_devices"))); |
588 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 611 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
589 | 612 |
590 listener.Reply("go"); | 613 listener.Reply("go"); |
591 | 614 |
592 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 615 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
593 } | 616 } |
594 | |
595 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevicesError) { | |
596 ResultCatcher catcher; | |
597 catcher.RestrictToProfile(browser()->profile()); | |
598 | |
599 // Load and wait for setup | |
600 ExtensionTestMessageListener listener("ready", true); | |
601 ASSERT_TRUE(LoadExtension( | |
602 test_data_dir_.AppendASCII("bluetooth/get_devices_error"))); | |
603 EXPECT_TRUE(listener.WaitUntilSatisfied()); | |
604 | |
605 listener.Reply("go"); | |
606 | |
607 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | |
608 } | |
OLD | NEW |