OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/memory/scoped_vector.h" |
5 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
6 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
7 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" | 8 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" |
8 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h" | 9 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h" |
9 #include "chromeos/dbus/fake_bluetooth_device_client.h" | 10 #include "chromeos/dbus/fake_bluetooth_device_client.h" |
10 #include "chromeos/dbus/fake_bluetooth_input_client.h" | 11 #include "chromeos/dbus/fake_bluetooth_input_client.h" |
11 #include "chromeos/dbus/fake_dbus_thread_manager.h" | 12 #include "chromeos/dbus/fake_dbus_thread_manager.h" |
12 #include "dbus/object_path.h" | 13 #include "dbus/object_path.h" |
13 #include "device/bluetooth/bluetooth_adapter.h" | 14 #include "device/bluetooth/bluetooth_adapter.h" |
14 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | 15 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
(...skipping 21 matching lines...) Expand all Loading... |
36 discovering_changed_count_(0), | 37 discovering_changed_count_(0), |
37 last_present_(false), | 38 last_present_(false), |
38 last_powered_(false), | 39 last_powered_(false), |
39 last_discovering_(false), | 40 last_discovering_(false), |
40 device_added_count_(0), | 41 device_added_count_(0), |
41 device_changed_count_(0), | 42 device_changed_count_(0), |
42 device_removed_count_(0), | 43 device_removed_count_(0), |
43 last_device_(NULL), | 44 last_device_(NULL), |
44 adapter_(adapter) { | 45 adapter_(adapter) { |
45 } | 46 } |
| 47 |
46 virtual ~TestObserver() {} | 48 virtual ~TestObserver() {} |
47 | 49 |
48 virtual void AdapterPresentChanged(BluetoothAdapter* adapter, | 50 virtual void AdapterPresentChanged(BluetoothAdapter* adapter, |
49 bool present) OVERRIDE { | 51 bool present) OVERRIDE { |
50 EXPECT_EQ(adapter_, adapter); | 52 EXPECT_EQ(adapter_, adapter); |
51 | 53 |
52 ++present_changed_count_; | 54 ++present_changed_count_; |
53 last_present_ = present; | 55 last_present_ = present; |
54 } | 56 } |
55 | 57 |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 | 241 |
240 fake_bluetooth_adapter_client_->SetSimulationIntervalMs(10); | 242 fake_bluetooth_adapter_client_->SetSimulationIntervalMs(10); |
241 | 243 |
242 callback_count_ = 0; | 244 callback_count_ = 0; |
243 error_callback_count_ = 0; | 245 error_callback_count_ = 0; |
244 last_connect_error_ = BluetoothDevice::ERROR_UNKNOWN; | 246 last_connect_error_ = BluetoothDevice::ERROR_UNKNOWN; |
245 last_client_error_ = ""; | 247 last_client_error_ = ""; |
246 } | 248 } |
247 | 249 |
248 virtual void TearDown() { | 250 virtual void TearDown() { |
249 if (last_discovery_session_.get() && last_discovery_session_->IsActive()) { | 251 for (ScopedVector<BluetoothDiscoverySession>::iterator iter = |
| 252 discovery_sessions_.begin(); |
| 253 iter != discovery_sessions_.end(); |
| 254 ++iter) { |
| 255 BluetoothDiscoverySession* session = *iter; |
| 256 if (!session->IsActive()) |
| 257 continue; |
250 callback_count_ = 0; | 258 callback_count_ = 0; |
251 last_discovery_session_->Stop( | 259 session->Stop( |
252 base::Bind(&BluetoothChromeOSTest::Callback, | 260 base::Bind(&BluetoothChromeOSTest::Callback, |
253 base::Unretained(this)), | 261 base::Unretained(this)), |
254 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 262 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
255 base::Unretained(this))); | 263 base::Unretained(this))); |
256 message_loop_.Run(); | 264 message_loop_.Run(); |
257 ASSERT_EQ(1, callback_count_); | 265 ASSERT_EQ(1, callback_count_); |
258 } | 266 } |
259 last_discovery_session_.reset(); | 267 discovery_sessions_.clear(); |
260 adapter_ = NULL; | 268 adapter_ = NULL; |
261 DBusThreadManager::Shutdown(); | 269 DBusThreadManager::Shutdown(); |
262 } | 270 } |
263 | 271 |
264 // Generic callbacks | 272 // Generic callbacks |
265 void Callback() { | 273 void Callback() { |
266 ++callback_count_; | 274 ++callback_count_; |
267 QuitMessageLoop(); | 275 QuitMessageLoop(); |
268 } | 276 } |
269 | 277 |
270 void DiscoverySessionCallback( | 278 void DiscoverySessionCallback( |
271 scoped_ptr<BluetoothDiscoverySession> discovery_session) { | 279 scoped_ptr<BluetoothDiscoverySession> discovery_session) { |
272 ++callback_count_; | 280 ++callback_count_; |
273 last_discovery_session_ = discovery_session.Pass(); | 281 discovery_sessions_.push_back(discovery_session.release()); |
274 QuitMessageLoop(); | 282 QuitMessageLoop(); |
275 } | 283 } |
276 | 284 |
277 void ErrorCallback() { | 285 void ErrorCallback() { |
278 ++error_callback_count_; | 286 ++error_callback_count_; |
279 QuitMessageLoop(); | 287 QuitMessageLoop(); |
280 } | 288 } |
281 | 289 |
282 void DBusErrorCallback(const std::string& error_name, | 290 void DBusErrorCallback(const std::string& error_name, |
283 const std::string& error_message) { | 291 const std::string& error_message) { |
(...skipping 26 matching lines...) Expand all Loading... |
310 | 318 |
311 TestObserver observer(adapter_); | 319 TestObserver observer(adapter_); |
312 adapter_->AddObserver(&observer); | 320 adapter_->AddObserver(&observer); |
313 | 321 |
314 adapter_->SetPowered( | 322 adapter_->SetPowered( |
315 true, | 323 true, |
316 base::Bind(&BluetoothChromeOSTest::Callback, | 324 base::Bind(&BluetoothChromeOSTest::Callback, |
317 base::Unretained(this)), | 325 base::Unretained(this)), |
318 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 326 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
319 base::Unretained(this))); | 327 base::Unretained(this))); |
320 adapter_->StartDiscovering( | 328 adapter_->StartDiscoverySession( |
321 base::Bind(&BluetoothChromeOSTest::Callback, | 329 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
322 base::Unretained(this)), | 330 base::Unretained(this)), |
323 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 331 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
324 base::Unretained(this))); | 332 base::Unretained(this))); |
325 base::MessageLoop::current()->Run(); | 333 base::MessageLoop::current()->Run(); |
326 ASSERT_EQ(2, callback_count_); | 334 ASSERT_EQ(2, callback_count_); |
327 ASSERT_EQ(0, error_callback_count_); | 335 ASSERT_EQ(0, error_callback_count_); |
| 336 ASSERT_EQ((size_t)1, discovery_sessions_.size()); |
| 337 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); |
328 callback_count_ = 0; | 338 callback_count_ = 0; |
329 | 339 |
330 ASSERT_TRUE(adapter_->IsPowered()); | 340 ASSERT_TRUE(adapter_->IsPowered()); |
331 ASSERT_TRUE(adapter_->IsDiscovering()); | 341 ASSERT_TRUE(adapter_->IsDiscovering()); |
332 | 342 |
333 while (!observer.device_removed_count_ && | 343 while (!observer.device_removed_count_ && |
334 observer.last_device_address_ != address) | 344 observer.last_device_address_ != address) |
335 base::MessageLoop::current()->Run(); | 345 base::MessageLoop::current()->Run(); |
336 | 346 |
337 adapter_->StopDiscovering( | 347 discovery_sessions_[0]->Stop( |
338 base::Bind(&BluetoothChromeOSTest::Callback, | 348 base::Bind(&BluetoothChromeOSTest::Callback, |
339 base::Unretained(this)), | 349 base::Unretained(this)), |
340 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 350 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
341 base::Unretained(this))); | 351 base::Unretained(this))); |
342 base::MessageLoop::current()->Run(); | 352 base::MessageLoop::current()->Run(); |
343 ASSERT_EQ(1, callback_count_); | 353 ASSERT_EQ(1, callback_count_); |
344 ASSERT_EQ(0, error_callback_count_); | 354 ASSERT_EQ(0, error_callback_count_); |
345 callback_count_ = 0; | 355 callback_count_ = 0; |
346 | 356 |
347 ASSERT_FALSE(adapter_->IsDiscovering()); | 357 ASSERT_FALSE(adapter_->IsDiscovering()); |
(...skipping 11 matching lines...) Expand all Loading... |
359 protected: | 369 protected: |
360 base::MessageLoop message_loop_; | 370 base::MessageLoop message_loop_; |
361 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_; | 371 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_; |
362 FakeBluetoothDeviceClient* fake_bluetooth_device_client_; | 372 FakeBluetoothDeviceClient* fake_bluetooth_device_client_; |
363 scoped_refptr<BluetoothAdapter> adapter_; | 373 scoped_refptr<BluetoothAdapter> adapter_; |
364 | 374 |
365 int callback_count_; | 375 int callback_count_; |
366 int error_callback_count_; | 376 int error_callback_count_; |
367 enum BluetoothDevice::ConnectErrorCode last_connect_error_; | 377 enum BluetoothDevice::ConnectErrorCode last_connect_error_; |
368 std::string last_client_error_; | 378 std::string last_client_error_; |
369 scoped_ptr<BluetoothDiscoverySession> last_discovery_session_; | 379 ScopedVector<BluetoothDiscoverySession> discovery_sessions_; |
370 | 380 |
371 private: | 381 private: |
372 // Some tests use a message loop since background processing is simulated; | 382 // Some tests use a message loop since background processing is simulated; |
373 // break out of those loops. | 383 // break out of those loops. |
374 void QuitMessageLoop() { | 384 void QuitMessageLoop() { |
375 if (base::MessageLoop::current() && | 385 if (base::MessageLoop::current() && |
376 base::MessageLoop::current()->is_running()) | 386 base::MessageLoop::current()->is_running()) |
377 base::MessageLoop::current()->Quit(); | 387 base::MessageLoop::current()->Quit(); |
378 } | 388 } |
379 }; | 389 }; |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
633 | 643 |
634 TEST_F(BluetoothChromeOSTest, StopDiscovery) { | 644 TEST_F(BluetoothChromeOSTest, StopDiscovery) { |
635 GetAdapter(); | 645 GetAdapter(); |
636 | 646 |
637 adapter_->SetPowered( | 647 adapter_->SetPowered( |
638 true, | 648 true, |
639 base::Bind(&BluetoothChromeOSTest::Callback, | 649 base::Bind(&BluetoothChromeOSTest::Callback, |
640 base::Unretained(this)), | 650 base::Unretained(this)), |
641 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 651 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
642 base::Unretained(this))); | 652 base::Unretained(this))); |
643 adapter_->StartDiscovering( | 653 adapter_->StartDiscoverySession( |
644 base::Bind(&BluetoothChromeOSTest::Callback, | 654 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
645 base::Unretained(this)), | 655 base::Unretained(this)), |
646 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 656 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
647 base::Unretained(this))); | 657 base::Unretained(this))); |
648 message_loop_.Run(); | 658 message_loop_.Run(); |
649 EXPECT_EQ(2, callback_count_); | 659 EXPECT_EQ(2, callback_count_); |
650 EXPECT_EQ(0, error_callback_count_); | 660 EXPECT_EQ(0, error_callback_count_); |
651 callback_count_ = 0; | 661 callback_count_ = 0; |
652 | 662 |
653 ASSERT_TRUE(adapter_->IsPowered()); | 663 ASSERT_TRUE(adapter_->IsPowered()); |
654 ASSERT_TRUE(adapter_->IsDiscovering()); | 664 ASSERT_TRUE(adapter_->IsDiscovering()); |
| 665 ASSERT_EQ((size_t)1, discovery_sessions_.size()); |
| 666 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); |
655 | 667 |
656 // Install an observer; aside from the callback, expect the | 668 // Install an observer; aside from the callback, expect the |
657 // AdapterDiscoveringChanged method to be called and no longer to be | 669 // AdapterDiscoveringChanged method to be called and no longer to be |
658 // discovering, | 670 // discovering, |
659 TestObserver observer(adapter_); | 671 TestObserver observer(adapter_); |
660 adapter_->AddObserver(&observer); | 672 adapter_->AddObserver(&observer); |
661 | 673 |
662 adapter_->StopDiscovering( | 674 discovery_sessions_[0]->Stop( |
663 base::Bind(&BluetoothChromeOSTest::Callback, | 675 base::Bind(&BluetoothChromeOSTest::Callback, |
664 base::Unretained(this)), | 676 base::Unretained(this)), |
665 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 677 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
666 base::Unretained(this))); | |
667 message_loop_.Run(); | |
668 EXPECT_EQ(1, callback_count_); | |
669 EXPECT_EQ(0, error_callback_count_); | |
670 | |
671 EXPECT_EQ(1, observer.discovering_changed_count_); | |
672 EXPECT_FALSE(observer.last_discovering_); | |
673 | |
674 EXPECT_FALSE(adapter_->IsDiscovering()); | |
675 } | |
676 | |
677 TEST_F(BluetoothChromeOSTest, StopDiscoveryAfterTwoStarts) { | |
678 GetAdapter(); | |
679 | |
680 adapter_->SetPowered( | |
681 true, | |
682 base::Bind(&BluetoothChromeOSTest::Callback, | |
683 base::Unretained(this)), | |
684 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
685 base::Unretained(this))); | |
686 adapter_->StartDiscovering( | |
687 base::Bind(&BluetoothChromeOSTest::Callback, | |
688 base::Unretained(this)), | |
689 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
690 base::Unretained(this))); | |
691 message_loop_.Run(); | |
692 EXPECT_EQ(2, callback_count_); | |
693 EXPECT_EQ(0, error_callback_count_); | |
694 callback_count_ = 0; | |
695 | |
696 ASSERT_TRUE(adapter_->IsPowered()); | |
697 ASSERT_TRUE(adapter_->IsDiscovering()); | |
698 | |
699 // Install an observer and start discovering again; only the callback | |
700 // should be called since we were already discovering to begin with. | |
701 TestObserver observer(adapter_); | |
702 adapter_->AddObserver(&observer); | |
703 | |
704 adapter_->StartDiscovering( | |
705 base::Bind(&BluetoothChromeOSTest::Callback, | |
706 base::Unretained(this)), | |
707 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
708 base::Unretained(this))); | |
709 message_loop_.Run(); | |
710 EXPECT_EQ(1, callback_count_); | |
711 EXPECT_EQ(0, error_callback_count_); | |
712 callback_count_ = 0; | |
713 | |
714 EXPECT_EQ(0, observer.discovering_changed_count_); | |
715 | |
716 // Stop discovering; only the callback should be called since we're still | |
717 // discovering. The adapter should be still discovering. | |
718 adapter_->StopDiscovering( | |
719 base::Bind(&BluetoothChromeOSTest::Callback, | |
720 base::Unretained(this)), | |
721 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
722 base::Unretained(this))); | |
723 message_loop_.Run(); | |
724 EXPECT_EQ(1, callback_count_); | |
725 EXPECT_EQ(0, error_callback_count_); | |
726 callback_count_ = 0; | |
727 | |
728 EXPECT_EQ(0, observer.discovering_changed_count_); | |
729 | |
730 EXPECT_TRUE(adapter_->IsDiscovering()); | |
731 | |
732 // Stop discovering one more time; aside from the callback, expect the | |
733 // AdapterDiscoveringChanged method to be called and no longer to be | |
734 // discovering, | |
735 adapter_->StopDiscovering( | |
736 base::Bind(&BluetoothChromeOSTest::Callback, | |
737 base::Unretained(this)), | |
738 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
739 base::Unretained(this))); | 678 base::Unretained(this))); |
740 message_loop_.Run(); | 679 message_loop_.Run(); |
741 EXPECT_EQ(1, callback_count_); | 680 EXPECT_EQ(1, callback_count_); |
742 EXPECT_EQ(0, error_callback_count_); | 681 EXPECT_EQ(0, error_callback_count_); |
743 | 682 |
744 EXPECT_EQ(1, observer.discovering_changed_count_); | 683 EXPECT_EQ(1, observer.discovering_changed_count_); |
745 EXPECT_FALSE(observer.last_discovering_); | 684 EXPECT_FALSE(observer.last_discovering_); |
746 | 685 |
747 EXPECT_FALSE(adapter_->IsDiscovering()); | 686 EXPECT_FALSE(adapter_->IsDiscovering()); |
748 } | 687 } |
749 | 688 |
750 TEST_F(BluetoothChromeOSTest, Discovery) { | 689 TEST_F(BluetoothChromeOSTest, Discovery) { |
751 // Test a simulated discovery session. | 690 // Test a simulated discovery session. |
752 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | 691 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); |
753 GetAdapter(); | 692 GetAdapter(); |
754 | 693 |
755 TestObserver observer(adapter_); | 694 TestObserver observer(adapter_); |
756 adapter_->AddObserver(&observer); | 695 adapter_->AddObserver(&observer); |
757 | 696 |
758 adapter_->SetPowered( | 697 adapter_->SetPowered( |
759 true, | 698 true, |
760 base::Bind(&BluetoothChromeOSTest::Callback, | 699 base::Bind(&BluetoothChromeOSTest::Callback, |
761 base::Unretained(this)), | 700 base::Unretained(this)), |
762 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 701 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
763 base::Unretained(this))); | 702 base::Unretained(this))); |
764 adapter_->StartDiscovering( | 703 adapter_->StartDiscoverySession( |
765 base::Bind(&BluetoothChromeOSTest::Callback, | 704 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
766 base::Unretained(this)), | 705 base::Unretained(this)), |
767 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 706 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
768 base::Unretained(this))); | 707 base::Unretained(this))); |
769 message_loop_.Run(); | 708 message_loop_.Run(); |
770 EXPECT_EQ(2, callback_count_); | 709 EXPECT_EQ(2, callback_count_); |
771 EXPECT_EQ(0, error_callback_count_); | 710 EXPECT_EQ(0, error_callback_count_); |
772 callback_count_ = 0; | 711 callback_count_ = 0; |
773 | 712 |
774 ASSERT_TRUE(adapter_->IsPowered()); | 713 ASSERT_TRUE(adapter_->IsPowered()); |
775 ASSERT_TRUE(adapter_->IsDiscovering()); | 714 ASSERT_TRUE(adapter_->IsDiscovering()); |
| 715 ASSERT_EQ((size_t)1, discovery_sessions_.size()); |
| 716 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); |
776 | 717 |
777 // First device to appear. | 718 // First device to appear. |
778 message_loop_.Run(); | 719 message_loop_.Run(); |
779 | 720 |
780 EXPECT_EQ(1, observer.device_added_count_); | 721 EXPECT_EQ(1, observer.device_added_count_); |
781 EXPECT_EQ(FakeBluetoothDeviceClient::kLegacyAutopairAddress, | 722 EXPECT_EQ(FakeBluetoothDeviceClient::kLegacyAutopairAddress, |
782 observer.last_device_address_); | 723 observer.last_device_address_); |
783 | 724 |
784 // Next we should get another two devices... | 725 // Next we should get another two devices... |
785 message_loop_.Run(); | 726 message_loop_.Run(); |
786 EXPECT_EQ(3, observer.device_added_count_); | 727 EXPECT_EQ(3, observer.device_added_count_); |
787 | 728 |
788 // Okay, let's run forward until a device is actually removed... | 729 // Okay, let's run forward until a device is actually removed... |
789 while (!observer.device_removed_count_) | 730 while (!observer.device_removed_count_) |
790 message_loop_.Run(); | 731 message_loop_.Run(); |
791 | 732 |
792 EXPECT_EQ(1, observer.device_removed_count_); | 733 EXPECT_EQ(1, observer.device_removed_count_); |
793 EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress, | 734 EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress, |
794 observer.last_device_address_); | 735 observer.last_device_address_); |
795 } | 736 } |
796 | 737 |
797 TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) { | 738 TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) { |
798 GetAdapter(); | 739 GetAdapter(); |
799 adapter_->SetPowered( | 740 adapter_->SetPowered( |
800 true, | 741 true, |
801 base::Bind(&BluetoothChromeOSTest::Callback, | 742 base::Bind(&BluetoothChromeOSTest::Callback, |
802 base::Unretained(this)), | 743 base::Unretained(this)), |
803 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 744 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
804 base::Unretained(this))); | 745 base::Unretained(this))); |
805 adapter_->StartDiscovering( | 746 adapter_->StartDiscoverySession( |
806 base::Bind(&BluetoothChromeOSTest::Callback, | 747 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
807 base::Unretained(this)), | 748 base::Unretained(this)), |
808 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 749 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
809 base::Unretained(this))); | 750 base::Unretained(this))); |
810 message_loop_.Run(); | 751 message_loop_.Run(); |
811 EXPECT_EQ(2, callback_count_); | 752 EXPECT_EQ(2, callback_count_); |
812 EXPECT_EQ(0, error_callback_count_); | 753 EXPECT_EQ(0, error_callback_count_); |
813 callback_count_ = 0; | 754 callback_count_ = 0; |
| 755 ASSERT_EQ((size_t)1, discovery_sessions_.size()); |
| 756 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); |
814 | 757 |
815 // Stop the timers that the simulation uses | 758 // Stop the timers that the simulation uses |
816 fake_bluetooth_device_client_->EndDiscoverySimulation( | 759 fake_bluetooth_device_client_->EndDiscoverySimulation( |
817 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath)); | 760 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath)); |
818 | 761 |
819 ASSERT_TRUE(adapter_->IsPowered()); | 762 ASSERT_TRUE(adapter_->IsPowered()); |
820 ASSERT_TRUE(adapter_->IsDiscovering()); | 763 ASSERT_TRUE(adapter_->IsDiscovering()); |
821 | 764 |
822 fake_bluetooth_adapter_client_->SetVisible(false); | 765 fake_bluetooth_adapter_client_->SetVisible(false); |
823 ASSERT_FALSE(adapter_->IsPresent()); | 766 ASSERT_FALSE(adapter_->IsPresent()); |
| 767 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); |
824 | 768 |
825 // Install an observer; expect the AdapterPresentChanged, | 769 // Install an observer; expect the AdapterPresentChanged, |
826 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called | 770 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called |
827 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all | 771 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all |
828 // return true. | 772 // return true. |
829 TestObserver observer(adapter_); | 773 TestObserver observer(adapter_); |
830 adapter_->AddObserver(&observer); | 774 adapter_->AddObserver(&observer); |
831 | 775 |
832 fake_bluetooth_adapter_client_->SetVisible(true); | 776 fake_bluetooth_adapter_client_->SetVisible(true); |
833 | 777 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
881 | 825 |
882 TestObserver observer(adapter_); | 826 TestObserver observer(adapter_); |
883 adapter_->AddObserver(&observer); | 827 adapter_->AddObserver(&observer); |
884 | 828 |
885 EXPECT_EQ(0, observer.discovering_changed_count_); | 829 EXPECT_EQ(0, observer.discovering_changed_count_); |
886 EXPECT_FALSE(observer.last_discovering_); | 830 EXPECT_FALSE(observer.last_discovering_); |
887 EXPECT_FALSE(adapter_->IsDiscovering()); | 831 EXPECT_FALSE(adapter_->IsDiscovering()); |
888 | 832 |
889 // Request device discovery 3 times. | 833 // Request device discovery 3 times. |
890 for (int i = 0; i < 3; i++) { | 834 for (int i = 0; i < 3; i++) { |
891 adapter_->StartDiscovering( | 835 adapter_->StartDiscoverySession( |
892 base::Bind(&BluetoothChromeOSTest::Callback, | 836 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
893 base::Unretained(this)), | 837 base::Unretained(this)), |
894 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 838 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
895 base::Unretained(this))); | 839 base::Unretained(this))); |
896 } | 840 } |
897 // Run only once, as there should have been one D-Bus call. | 841 // Run only once, as there should have been one D-Bus call. |
898 message_loop_.Run(); | 842 message_loop_.Run(); |
899 | 843 |
900 // The observer should have received the discovering changed event exactly | 844 // The observer should have received the discovering changed event exactly |
901 // once, the success callback should have been called 3 times and the adapter | 845 // once, the success callback should have been called 3 times and the adapter |
902 // should be discovering. | 846 // should be discovering. |
903 EXPECT_EQ(1, observer.discovering_changed_count_); | 847 EXPECT_EQ(1, observer.discovering_changed_count_); |
904 EXPECT_EQ(3, callback_count_); | 848 EXPECT_EQ(3, callback_count_); |
905 EXPECT_EQ(0, error_callback_count_); | 849 EXPECT_EQ(0, error_callback_count_); |
906 EXPECT_TRUE(observer.last_discovering_); | 850 EXPECT_TRUE(observer.last_discovering_); |
907 EXPECT_TRUE(adapter_->IsDiscovering()); | 851 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 852 ASSERT_EQ((size_t)3, discovery_sessions_.size()); |
908 | 853 |
909 // Request to stop discovery twice. | 854 // Request to stop discovery twice. |
910 for (int i = 0; i < 2; i++) { | 855 for (int i = 0; i < 2; i++) { |
911 adapter_->StopDiscovering( | 856 discovery_sessions_[i]->Stop( |
912 base::Bind(&BluetoothChromeOSTest::Callback, | 857 base::Bind(&BluetoothChromeOSTest::Callback, |
913 base::Unretained(this)), | 858 base::Unretained(this)), |
914 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 859 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
915 base::Unretained(this))); | 860 base::Unretained(this))); |
916 } | 861 } |
917 | 862 |
918 // The observer should have received no additional discovering changed events, | 863 // The observer should have received no additional discovering changed events, |
919 // the success callback should have been called 2 times and the adapter should | 864 // the success callback should have been called 2 times and the adapter should |
920 // still be discovering. | 865 // still be discovering. |
921 EXPECT_EQ(1, observer.discovering_changed_count_); | 866 EXPECT_EQ(1, observer.discovering_changed_count_); |
922 EXPECT_EQ(5, callback_count_); | 867 EXPECT_EQ(5, callback_count_); |
923 EXPECT_EQ(0, error_callback_count_); | 868 EXPECT_EQ(0, error_callback_count_); |
924 EXPECT_TRUE(observer.last_discovering_); | 869 EXPECT_TRUE(observer.last_discovering_); |
925 EXPECT_TRUE(adapter_->IsDiscovering()); | 870 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 871 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 872 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); |
| 873 EXPECT_FALSE(discovery_sessions_[1]->IsActive()); |
| 874 EXPECT_TRUE(discovery_sessions_[2]->IsActive()); |
926 | 875 |
927 // Request device discovery 3 times. | 876 // Request device discovery 3 times. |
928 for (int i = 0; i < 3; i++) { | 877 for (int i = 0; i < 3; i++) { |
929 adapter_->StartDiscovering( | 878 adapter_->StartDiscoverySession( |
930 base::Bind(&BluetoothChromeOSTest::Callback, | 879 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
931 base::Unretained(this)), | 880 base::Unretained(this)), |
932 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 881 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
933 base::Unretained(this))); | 882 base::Unretained(this))); |
934 } | 883 } |
935 | 884 |
936 // The observer should have received no additional discovering changed events, | 885 // The observer should have received no additional discovering changed events, |
937 // the success callback should have been called 3 times and the adapter should | 886 // the success callback should have been called 3 times and the adapter should |
938 // still be discovering. | 887 // still be discovering. |
939 EXPECT_EQ(1, observer.discovering_changed_count_); | 888 EXPECT_EQ(1, observer.discovering_changed_count_); |
940 EXPECT_EQ(8, callback_count_); | 889 EXPECT_EQ(8, callback_count_); |
941 EXPECT_EQ(0, error_callback_count_); | 890 EXPECT_EQ(0, error_callback_count_); |
942 EXPECT_TRUE(observer.last_discovering_); | 891 EXPECT_TRUE(observer.last_discovering_); |
943 EXPECT_TRUE(adapter_->IsDiscovering()); | 892 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 893 ASSERT_EQ((size_t)6, discovery_sessions_.size()); |
944 | 894 |
945 // Request to stop discovery 4 times. | 895 // Request to stop discovery 4 times. |
946 for (int i = 0; i < 4; i++) { | 896 for (int i = 2; i < 6; i++) { |
947 adapter_->StopDiscovering( | 897 discovery_sessions_[i]->Stop( |
948 base::Bind(&BluetoothChromeOSTest::Callback, | 898 base::Bind(&BluetoothChromeOSTest::Callback, |
949 base::Unretained(this)), | 899 base::Unretained(this)), |
950 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 900 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
951 base::Unretained(this))); | 901 base::Unretained(this))); |
952 } | 902 } |
953 // Run only once, as there should have been one D-Bus call. | 903 // Run only once, as there should have been one D-Bus call. |
954 message_loop_.Run(); | 904 message_loop_.Run(); |
955 | 905 |
956 // The observer should have received the discovering changed event exactly | 906 // The observer should have received the discovering changed event exactly |
957 // once, the success callback should have been called 4 times and the adapter | 907 // once, the success callback should have been called 4 times and the adapter |
958 // should no longer be discovering. | 908 // should no longer be discovering. |
959 EXPECT_EQ(2, observer.discovering_changed_count_); | 909 EXPECT_EQ(2, observer.discovering_changed_count_); |
960 EXPECT_EQ(12, callback_count_); | 910 EXPECT_EQ(12, callback_count_); |
961 EXPECT_EQ(0, error_callback_count_); | 911 EXPECT_EQ(0, error_callback_count_); |
962 EXPECT_FALSE(observer.last_discovering_); | 912 EXPECT_FALSE(observer.last_discovering_); |
963 EXPECT_FALSE(adapter_->IsDiscovering()); | 913 EXPECT_FALSE(adapter_->IsDiscovering()); |
964 | 914 |
965 // Request to stop discovery once. | 915 // All discovery sessions should be inactive. |
966 adapter_->StopDiscovering( | 916 for (int i = 0; i < 6; i++) |
| 917 EXPECT_FALSE(discovery_sessions_[i]->IsActive()); |
| 918 |
| 919 // Request to stop discovery on of the inactive sessions. |
| 920 discovery_sessions_[0]->Stop( |
967 base::Bind(&BluetoothChromeOSTest::Callback, | 921 base::Bind(&BluetoothChromeOSTest::Callback, |
968 base::Unretained(this)), | 922 base::Unretained(this)), |
969 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 923 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
970 base::Unretained(this))); | 924 base::Unretained(this))); |
971 | 925 |
972 // The call should have failed. | 926 // The call should have failed. |
973 EXPECT_EQ(2, observer.discovering_changed_count_); | 927 EXPECT_EQ(2, observer.discovering_changed_count_); |
974 EXPECT_EQ(12, callback_count_); | 928 EXPECT_EQ(12, callback_count_); |
975 EXPECT_EQ(1, error_callback_count_); | 929 EXPECT_EQ(1, error_callback_count_); |
976 EXPECT_FALSE(observer.last_discovering_); | 930 EXPECT_FALSE(observer.last_discovering_); |
(...skipping 19 matching lines...) Expand all Loading... |
996 | 950 |
997 TestObserver observer(adapter_); | 951 TestObserver observer(adapter_); |
998 adapter_->AddObserver(&observer); | 952 adapter_->AddObserver(&observer); |
999 | 953 |
1000 EXPECT_EQ(0, observer.discovering_changed_count_); | 954 EXPECT_EQ(0, observer.discovering_changed_count_); |
1001 EXPECT_FALSE(observer.last_discovering_); | 955 EXPECT_FALSE(observer.last_discovering_); |
1002 EXPECT_FALSE(adapter_->IsDiscovering()); | 956 EXPECT_FALSE(adapter_->IsDiscovering()); |
1003 | 957 |
1004 // Request device discovery 3 times. | 958 // Request device discovery 3 times. |
1005 for (int i = 0; i < 3; i++) { | 959 for (int i = 0; i < 3; i++) { |
1006 adapter_->StartDiscovering( | 960 adapter_->StartDiscoverySession( |
1007 base::Bind(&BluetoothChromeOSTest::Callback, | 961 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
1008 base::Unretained(this)), | 962 base::Unretained(this)), |
1009 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 963 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1010 base::Unretained(this))); | 964 base::Unretained(this))); |
1011 } | 965 } |
1012 // Run only once, as there should have been one D-Bus call. | 966 // Run only once, as there should have been one D-Bus call. |
1013 message_loop_.Run(); | 967 message_loop_.Run(); |
1014 | 968 |
1015 // The observer should have received the discovering changed event exactly | 969 // The observer should have received the discovering changed event exactly |
1016 // once, the success callback should have been called 3 times and the adapter | 970 // once, the success callback should have been called 3 times and the adapter |
1017 // should be discovering. | 971 // should be discovering. |
1018 EXPECT_EQ(1, observer.discovering_changed_count_); | 972 EXPECT_EQ(1, observer.discovering_changed_count_); |
1019 EXPECT_EQ(3, callback_count_); | 973 EXPECT_EQ(3, callback_count_); |
1020 EXPECT_EQ(0, error_callback_count_); | 974 EXPECT_EQ(0, error_callback_count_); |
1021 EXPECT_TRUE(observer.last_discovering_); | 975 EXPECT_TRUE(observer.last_discovering_); |
1022 EXPECT_TRUE(adapter_->IsDiscovering()); | 976 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 977 ASSERT_EQ((size_t)3, discovery_sessions_.size()); |
| 978 |
| 979 for (int i = 0; i < 3; i++) |
| 980 EXPECT_TRUE(discovery_sessions_[i]->IsActive()); |
1023 | 981 |
1024 // Stop the timers that the simulation uses | 982 // Stop the timers that the simulation uses |
1025 fake_bluetooth_device_client_->EndDiscoverySimulation( | 983 fake_bluetooth_device_client_->EndDiscoverySimulation( |
1026 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath)); | 984 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath)); |
1027 | 985 |
1028 ASSERT_TRUE(adapter_->IsPowered()); | 986 ASSERT_TRUE(adapter_->IsPowered()); |
1029 ASSERT_TRUE(adapter_->IsDiscovering()); | 987 ASSERT_TRUE(adapter_->IsDiscovering()); |
1030 | 988 |
1031 // Stop device discovery behind the adapter. The adapter and the observer | 989 // Stop device discovery behind the adapter. The adapter and the observer |
1032 // should be notified of the change and the reference count should be reset. | 990 // should be notified of the change and the reference count should be reset. |
1033 // Even though FakeBluetoothAdapterClient does its own reference counting and | 991 // Even though FakeBluetoothAdapterClient does its own reference counting and |
1034 // we called 3 BluetoothAdapter::StartDiscovering 3 times, the | 992 // we called 3 BluetoothAdapter::StartDiscoverySession 3 times, the |
1035 // FakeBluetoothAdapterClient's count should be only 1 and a single call to | 993 // FakeBluetoothAdapterClient's count should be only 1 and a single call to |
1036 // FakeBluetoothAdapterClient::StopDiscovery should work. | 994 // FakeBluetoothAdapterClient::StopDiscovery should work. |
1037 fake_bluetooth_adapter_client_->StopDiscovery( | 995 fake_bluetooth_adapter_client_->StopDiscovery( |
1038 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), | 996 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), |
1039 base::Bind(&BluetoothChromeOSTest::Callback, | 997 base::Bind(&BluetoothChromeOSTest::Callback, |
1040 base::Unretained(this)), | 998 base::Unretained(this)), |
1041 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, | 999 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, |
1042 base::Unretained(this))); | 1000 base::Unretained(this))); |
1043 message_loop_.Run(); | 1001 message_loop_.Run(); |
1044 EXPECT_EQ(2, observer.discovering_changed_count_); | 1002 EXPECT_EQ(2, observer.discovering_changed_count_); |
1045 EXPECT_EQ(4, callback_count_); | 1003 EXPECT_EQ(4, callback_count_); |
1046 EXPECT_EQ(0, error_callback_count_); | 1004 EXPECT_EQ(0, error_callback_count_); |
1047 EXPECT_FALSE(observer.last_discovering_); | 1005 EXPECT_FALSE(observer.last_discovering_); |
1048 EXPECT_FALSE(adapter_->IsDiscovering()); | 1006 EXPECT_FALSE(adapter_->IsDiscovering()); |
1049 | 1007 |
| 1008 // All discovery session instances should have been updated. |
| 1009 for (int i = 0; i < 3; i++) |
| 1010 EXPECT_FALSE(discovery_sessions_[i]->IsActive()); |
| 1011 discovery_sessions_.clear(); |
| 1012 |
1050 // It should be possible to successfully start discovery. | 1013 // It should be possible to successfully start discovery. |
1051 for (int i = 0; i < 2; i++) { | 1014 for (int i = 0; i < 2; i++) { |
1052 adapter_->StartDiscovering( | 1015 adapter_->StartDiscoverySession( |
1053 base::Bind(&BluetoothChromeOSTest::Callback, | 1016 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
1054 base::Unretained(this)), | 1017 base::Unretained(this)), |
1055 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 1018 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1056 base::Unretained(this))); | 1019 base::Unretained(this))); |
1057 } | 1020 } |
1058 // Run only once, as there should have been one D-Bus call. | 1021 // Run only once, as there should have been one D-Bus call. |
1059 message_loop_.Run(); | 1022 message_loop_.Run(); |
1060 EXPECT_EQ(3, observer.discovering_changed_count_); | 1023 EXPECT_EQ(3, observer.discovering_changed_count_); |
1061 EXPECT_EQ(6, callback_count_); | 1024 EXPECT_EQ(6, callback_count_); |
1062 EXPECT_EQ(0, error_callback_count_); | 1025 EXPECT_EQ(0, error_callback_count_); |
1063 EXPECT_TRUE(observer.last_discovering_); | 1026 EXPECT_TRUE(observer.last_discovering_); |
1064 EXPECT_TRUE(adapter_->IsDiscovering()); | 1027 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 1028 ASSERT_EQ((size_t)2, discovery_sessions_.size()); |
| 1029 |
| 1030 for (int i = 0; i < 2; i++) |
| 1031 EXPECT_TRUE(discovery_sessions_[i]->IsActive()); |
1065 | 1032 |
1066 fake_bluetooth_device_client_->EndDiscoverySimulation( | 1033 fake_bluetooth_device_client_->EndDiscoverySimulation( |
1067 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath)); | 1034 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath)); |
1068 | 1035 |
1069 // Make the adapter disappear and appear. This will make it come back as | 1036 // Make the adapter disappear and appear. This will make it come back as |
1070 // discovering. When this happens, the reference count should become and | 1037 // discovering. When this happens, the reference count should become and |
1071 // remain 0 as no new request was made through the BluetoothAdapter. | 1038 // remain 0 as no new request was made through the BluetoothAdapter. |
1072 fake_bluetooth_adapter_client_->SetVisible(false); | 1039 fake_bluetooth_adapter_client_->SetVisible(false); |
1073 ASSERT_FALSE(adapter_->IsPresent()); | 1040 ASSERT_FALSE(adapter_->IsPresent()); |
1074 EXPECT_EQ(4, observer.discovering_changed_count_); | 1041 EXPECT_EQ(4, observer.discovering_changed_count_); |
1075 EXPECT_EQ(6, callback_count_); | 1042 EXPECT_EQ(6, callback_count_); |
1076 EXPECT_EQ(0, error_callback_count_); | 1043 EXPECT_EQ(0, error_callback_count_); |
1077 EXPECT_FALSE(observer.last_discovering_); | 1044 EXPECT_FALSE(observer.last_discovering_); |
1078 EXPECT_FALSE(adapter_->IsDiscovering()); | 1045 EXPECT_FALSE(adapter_->IsDiscovering()); |
1079 | 1046 |
| 1047 for (int i = 0; i < 2; i++) |
| 1048 EXPECT_FALSE(discovery_sessions_[i]->IsActive()); |
| 1049 discovery_sessions_.clear(); |
| 1050 |
1080 fake_bluetooth_adapter_client_->SetVisible(true); | 1051 fake_bluetooth_adapter_client_->SetVisible(true); |
1081 ASSERT_TRUE(adapter_->IsPresent()); | 1052 ASSERT_TRUE(adapter_->IsPresent()); |
1082 EXPECT_EQ(5, observer.discovering_changed_count_); | 1053 EXPECT_EQ(5, observer.discovering_changed_count_); |
1083 EXPECT_EQ(6, callback_count_); | 1054 EXPECT_EQ(6, callback_count_); |
1084 EXPECT_EQ(0, error_callback_count_); | 1055 EXPECT_EQ(0, error_callback_count_); |
1085 EXPECT_TRUE(observer.last_discovering_); | 1056 EXPECT_TRUE(observer.last_discovering_); |
1086 EXPECT_TRUE(adapter_->IsDiscovering()); | 1057 EXPECT_TRUE(adapter_->IsDiscovering()); |
1087 | 1058 |
1088 // Start and stop discovery. At this point, FakeBluetoothAdapterClient has | 1059 // Start and stop discovery. At this point, FakeBluetoothAdapterClient has |
1089 // a reference count that is equal to 1. Pretend that this was done by an | 1060 // a reference count that is equal to 1. Pretend that this was done by an |
1090 // application other than us. Starting and stopping discovery will succeed | 1061 // application other than us. Starting and stopping discovery will succeed |
1091 // but it won't cause the discovery state to change. | 1062 // but it won't cause the discovery state to change. |
1092 adapter_->StartDiscovering( | 1063 adapter_->StartDiscoverySession( |
1093 base::Bind(&BluetoothChromeOSTest::Callback, | 1064 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
1094 base::Unretained(this)), | 1065 base::Unretained(this)), |
1095 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 1066 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1096 base::Unretained(this))); | 1067 base::Unretained(this))); |
1097 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call. | 1068 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call. |
1098 EXPECT_EQ(5, observer.discovering_changed_count_); | 1069 EXPECT_EQ(5, observer.discovering_changed_count_); |
1099 EXPECT_EQ(7, callback_count_); | 1070 EXPECT_EQ(7, callback_count_); |
1100 EXPECT_EQ(0, error_callback_count_); | 1071 EXPECT_EQ(0, error_callback_count_); |
1101 EXPECT_TRUE(observer.last_discovering_); | 1072 EXPECT_TRUE(observer.last_discovering_); |
1102 EXPECT_TRUE(adapter_->IsDiscovering()); | 1073 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 1074 ASSERT_EQ((size_t)1, discovery_sessions_.size()); |
| 1075 EXPECT_TRUE(discovery_sessions_[0]->IsActive()); |
1103 | 1076 |
1104 adapter_->StopDiscovering( | 1077 discovery_sessions_[0]->Stop( |
1105 base::Bind(&BluetoothChromeOSTest::Callback, | 1078 base::Bind(&BluetoothChromeOSTest::Callback, |
1106 base::Unretained(this)), | 1079 base::Unretained(this)), |
1107 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 1080 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1108 base::Unretained(this))); | 1081 base::Unretained(this))); |
1109 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call. | 1082 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call. |
1110 EXPECT_EQ(5, observer.discovering_changed_count_); | 1083 EXPECT_EQ(5, observer.discovering_changed_count_); |
1111 EXPECT_EQ(8, callback_count_); | 1084 EXPECT_EQ(8, callback_count_); |
1112 EXPECT_EQ(0, error_callback_count_); | 1085 EXPECT_EQ(0, error_callback_count_); |
1113 EXPECT_TRUE(observer.last_discovering_); | 1086 EXPECT_TRUE(observer.last_discovering_); |
1114 EXPECT_TRUE(adapter_->IsDiscovering()); | 1087 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 1088 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); |
| 1089 discovery_sessions_.clear(); |
1115 | 1090 |
1116 // Start discovery again. | 1091 // Start discovery again. |
1117 adapter_->StartDiscovering( | 1092 adapter_->StartDiscoverySession( |
1118 base::Bind(&BluetoothChromeOSTest::Callback, | 1093 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
1119 base::Unretained(this)), | 1094 base::Unretained(this)), |
1120 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 1095 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1121 base::Unretained(this))); | 1096 base::Unretained(this))); |
1122 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call. | 1097 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call. |
1123 EXPECT_EQ(5, observer.discovering_changed_count_); | 1098 EXPECT_EQ(5, observer.discovering_changed_count_); |
1124 EXPECT_EQ(9, callback_count_); | 1099 EXPECT_EQ(9, callback_count_); |
1125 EXPECT_EQ(0, error_callback_count_); | 1100 EXPECT_EQ(0, error_callback_count_); |
1126 EXPECT_TRUE(observer.last_discovering_); | 1101 EXPECT_TRUE(observer.last_discovering_); |
1127 EXPECT_TRUE(adapter_->IsDiscovering()); | 1102 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 1103 ASSERT_EQ((size_t)1, discovery_sessions_.size()); |
| 1104 EXPECT_TRUE(discovery_sessions_[0]->IsActive()); |
1128 | 1105 |
1129 // Stop discovery via D-Bus. The fake client's reference count will drop but | 1106 // Stop discovery via D-Bus. The fake client's reference count will drop but |
1130 // the discovery state won't change since our BluetoothAdapter also just | 1107 // the discovery state won't change since our BluetoothAdapter also just |
1131 // requested it via D-Bus. | 1108 // requested it via D-Bus. |
1132 fake_bluetooth_adapter_client_->StopDiscovery( | 1109 fake_bluetooth_adapter_client_->StopDiscovery( |
1133 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), | 1110 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), |
1134 base::Bind(&BluetoothChromeOSTest::Callback, | 1111 base::Bind(&BluetoothChromeOSTest::Callback, |
1135 base::Unretained(this)), | 1112 base::Unretained(this)), |
1136 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, | 1113 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, |
1137 base::Unretained(this))); | 1114 base::Unretained(this))); |
1138 message_loop_.Run(); | 1115 message_loop_.Run(); |
1139 EXPECT_EQ(5, observer.discovering_changed_count_); | 1116 EXPECT_EQ(5, observer.discovering_changed_count_); |
1140 EXPECT_EQ(10, callback_count_); | 1117 EXPECT_EQ(10, callback_count_); |
1141 EXPECT_EQ(0, error_callback_count_); | 1118 EXPECT_EQ(0, error_callback_count_); |
1142 EXPECT_TRUE(observer.last_discovering_); | 1119 EXPECT_TRUE(observer.last_discovering_); |
1143 EXPECT_TRUE(adapter_->IsDiscovering()); | 1120 EXPECT_TRUE(adapter_->IsDiscovering()); |
1144 | 1121 |
1145 // Now end the discovery session. This should change the adapter's discovery | 1122 // Now end the discovery session. This should change the adapter's discovery |
1146 // state. | 1123 // state. |
1147 adapter_->StopDiscovering( | 1124 discovery_sessions_[0]->Stop( |
1148 base::Bind(&BluetoothChromeOSTest::Callback, | 1125 base::Bind(&BluetoothChromeOSTest::Callback, |
1149 base::Unretained(this)), | 1126 base::Unretained(this)), |
1150 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 1127 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1151 base::Unretained(this))); | 1128 base::Unretained(this))); |
1152 message_loop_.Run(); | 1129 message_loop_.Run(); |
1153 EXPECT_EQ(6, observer.discovering_changed_count_); | 1130 EXPECT_EQ(6, observer.discovering_changed_count_); |
1154 EXPECT_EQ(11, callback_count_); | 1131 EXPECT_EQ(11, callback_count_); |
1155 EXPECT_EQ(0, error_callback_count_); | 1132 EXPECT_EQ(0, error_callback_count_); |
1156 EXPECT_FALSE(observer.last_discovering_); | 1133 EXPECT_FALSE(observer.last_discovering_); |
1157 EXPECT_FALSE(adapter_->IsDiscovering()); | 1134 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 1135 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); |
| 1136 |
| 1137 adapter_->RemoveObserver(&observer); |
| 1138 } |
| 1139 |
| 1140 TEST_F(BluetoothChromeOSTest, InvalidatedDiscoverySessions) { |
| 1141 GetAdapter(); |
| 1142 adapter_->SetPowered( |
| 1143 true, |
| 1144 base::Bind(&BluetoothChromeOSTest::Callback, |
| 1145 base::Unretained(this)), |
| 1146 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
| 1147 base::Unretained(this))); |
| 1148 EXPECT_EQ(1, callback_count_); |
| 1149 EXPECT_EQ(0, error_callback_count_); |
| 1150 EXPECT_TRUE(adapter_->IsPowered()); |
| 1151 callback_count_ = 0; |
| 1152 |
| 1153 TestObserver observer(adapter_); |
| 1154 adapter_->AddObserver(&observer); |
| 1155 |
| 1156 EXPECT_EQ(0, observer.discovering_changed_count_); |
| 1157 EXPECT_FALSE(observer.last_discovering_); |
| 1158 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 1159 |
| 1160 // Request device discovery 3 times. |
| 1161 for (int i = 0; i < 3; i++) { |
| 1162 adapter_->StartDiscoverySession( |
| 1163 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
| 1164 base::Unretained(this)), |
| 1165 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
| 1166 base::Unretained(this))); |
| 1167 } |
| 1168 // Run only once, as there should have been one D-Bus call. |
| 1169 message_loop_.Run(); |
| 1170 |
| 1171 // The observer should have received the discovering changed event exactly |
| 1172 // once, the success callback should have been called 3 times and the adapter |
| 1173 // should be discovering. |
| 1174 EXPECT_EQ(1, observer.discovering_changed_count_); |
| 1175 EXPECT_EQ(3, callback_count_); |
| 1176 EXPECT_EQ(0, error_callback_count_); |
| 1177 EXPECT_TRUE(observer.last_discovering_); |
| 1178 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 1179 ASSERT_EQ((size_t)3, discovery_sessions_.size()); |
| 1180 |
| 1181 for (int i = 0; i < 3; i++) |
| 1182 EXPECT_TRUE(discovery_sessions_[i]->IsActive()); |
| 1183 |
| 1184 // Stop the timers that the simulation uses |
| 1185 fake_bluetooth_device_client_->EndDiscoverySimulation( |
| 1186 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath)); |
| 1187 |
| 1188 ASSERT_TRUE(adapter_->IsPowered()); |
| 1189 ASSERT_TRUE(adapter_->IsDiscovering()); |
| 1190 |
| 1191 // Delete all but one discovery session. |
| 1192 discovery_sessions_.pop_back(); |
| 1193 discovery_sessions_.pop_back(); |
| 1194 ASSERT_EQ((size_t)1, discovery_sessions_.size()); |
| 1195 EXPECT_TRUE(discovery_sessions_[0]->IsActive()); |
| 1196 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 1197 |
| 1198 // Stop device discovery behind the adapter. The one active discovery session |
| 1199 // should become inactive, but more importantly, we shouldn't run into any |
| 1200 // memory errors as the sessions that we explicitly deleted should get |
| 1201 // cleaned up. |
| 1202 fake_bluetooth_adapter_client_->StopDiscovery( |
| 1203 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), |
| 1204 base::Bind(&BluetoothChromeOSTest::Callback, |
| 1205 base::Unretained(this)), |
| 1206 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, |
| 1207 base::Unretained(this))); |
| 1208 message_loop_.Run(); |
| 1209 EXPECT_EQ(2, observer.discovering_changed_count_); |
| 1210 EXPECT_EQ(4, callback_count_); |
| 1211 EXPECT_EQ(0, error_callback_count_); |
| 1212 EXPECT_FALSE(observer.last_discovering_); |
| 1213 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 1214 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); |
1158 } | 1215 } |
1159 | 1216 |
1160 TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) { | 1217 TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) { |
1161 GetAdapter(); | 1218 GetAdapter(); |
1162 | 1219 |
1163 adapter_->SetPowered( | 1220 adapter_->SetPowered( |
1164 true, | 1221 true, |
1165 base::Bind(&BluetoothChromeOSTest::Callback, | 1222 base::Bind(&BluetoothChromeOSTest::Callback, |
1166 base::Unretained(this)), | 1223 base::Unretained(this)), |
1167 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 1224 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1168 base::Unretained(this))); | 1225 base::Unretained(this))); |
1169 EXPECT_EQ(1, callback_count_); | 1226 EXPECT_EQ(1, callback_count_); |
1170 EXPECT_EQ(0, error_callback_count_); | 1227 EXPECT_EQ(0, error_callback_count_); |
1171 EXPECT_TRUE(adapter_->IsPowered()); | 1228 EXPECT_TRUE(adapter_->IsPowered()); |
1172 callback_count_ = 0; | 1229 callback_count_ = 0; |
1173 | 1230 |
1174 TestObserver observer(adapter_); | 1231 TestObserver observer(adapter_); |
1175 adapter_->AddObserver(&observer); | 1232 adapter_->AddObserver(&observer); |
1176 | 1233 |
1177 EXPECT_EQ(0, observer.discovering_changed_count_); | 1234 EXPECT_EQ(0, observer.discovering_changed_count_); |
1178 EXPECT_FALSE(observer.last_discovering_); | 1235 EXPECT_FALSE(observer.last_discovering_); |
1179 EXPECT_FALSE(adapter_->IsDiscovering()); | 1236 EXPECT_FALSE(adapter_->IsDiscovering()); |
1180 | 1237 |
1181 // Request to start discovery. The call should be pending. | 1238 // Request to start discovery. The call should be pending. |
1182 adapter_->StartDiscovering( | 1239 adapter_->StartDiscoverySession( |
1183 base::Bind(&BluetoothChromeOSTest::Callback, | 1240 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
1184 base::Unretained(this)), | 1241 base::Unretained(this)), |
1185 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 1242 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1186 base::Unretained(this))); | 1243 base::Unretained(this))); |
1187 EXPECT_EQ(0, callback_count_); | 1244 EXPECT_EQ(0, callback_count_); |
1188 | 1245 |
1189 fake_bluetooth_device_client_->EndDiscoverySimulation( | 1246 fake_bluetooth_device_client_->EndDiscoverySimulation( |
1190 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath)); | 1247 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath)); |
1191 | 1248 |
1192 // The underlying adapter has started discovery, but our call hasn't returned | 1249 // The underlying adapter has started discovery, but our call hasn't returned |
1193 // yet. | 1250 // yet. |
1194 EXPECT_EQ(1, observer.discovering_changed_count_); | 1251 EXPECT_EQ(1, observer.discovering_changed_count_); |
1195 EXPECT_TRUE(observer.last_discovering_); | 1252 EXPECT_TRUE(observer.last_discovering_); |
1196 EXPECT_TRUE(adapter_->IsDiscovering()); | 1253 EXPECT_TRUE(adapter_->IsDiscovering()); |
1197 | 1254 EXPECT_TRUE(discovery_sessions_.empty()); |
1198 // Try to stop discovery. This should fail while there is a call pending. | |
1199 adapter_->StopDiscovering( | |
1200 base::Bind(&BluetoothChromeOSTest::Callback, | |
1201 base::Unretained(this)), | |
1202 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1203 base::Unretained(this))); | |
1204 EXPECT_EQ(0, callback_count_); | |
1205 EXPECT_EQ(1, error_callback_count_); | |
1206 EXPECT_EQ(1, observer.discovering_changed_count_); | |
1207 EXPECT_TRUE(observer.last_discovering_); | |
1208 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1209 | 1255 |
1210 // Request to start discovery twice. These should get queued and there should | 1256 // Request to start discovery twice. These should get queued and there should |
1211 // be no change in state. | 1257 // be no change in state. |
1212 for (int i = 0; i < 2; i++) { | 1258 for (int i = 0; i < 2; i++) { |
1213 adapter_->StartDiscovering( | 1259 adapter_->StartDiscoverySession( |
1214 base::Bind(&BluetoothChromeOSTest::Callback, | 1260 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
1215 base::Unretained(this)), | 1261 base::Unretained(this)), |
1216 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 1262 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1217 base::Unretained(this))); | 1263 base::Unretained(this))); |
1218 } | 1264 } |
1219 EXPECT_EQ(0, callback_count_); | 1265 EXPECT_EQ(0, callback_count_); |
1220 EXPECT_EQ(1, error_callback_count_); | 1266 EXPECT_EQ(0, error_callback_count_); |
1221 EXPECT_EQ(1, observer.discovering_changed_count_); | 1267 EXPECT_EQ(1, observer.discovering_changed_count_); |
1222 EXPECT_TRUE(observer.last_discovering_); | 1268 EXPECT_TRUE(observer.last_discovering_); |
1223 EXPECT_TRUE(adapter_->IsDiscovering()); | 1269 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 1270 EXPECT_TRUE(discovery_sessions_.empty()); |
1224 | 1271 |
1225 // Process the pending call. The queued calls should execute and the discovery | 1272 // Process the pending call. The queued calls should execute and the discovery |
1226 // session reference count should increase. | 1273 // session reference count should increase. |
1227 message_loop_.Run(); | 1274 message_loop_.Run(); |
1228 EXPECT_EQ(3, callback_count_); | 1275 EXPECT_EQ(3, callback_count_); |
1229 EXPECT_EQ(1, error_callback_count_); | 1276 EXPECT_EQ(0, error_callback_count_); |
1230 EXPECT_EQ(1, observer.discovering_changed_count_); | 1277 EXPECT_EQ(1, observer.discovering_changed_count_); |
1231 EXPECT_TRUE(observer.last_discovering_); | 1278 EXPECT_TRUE(observer.last_discovering_); |
1232 EXPECT_TRUE(adapter_->IsDiscovering()); | 1279 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 1280 ASSERT_EQ((size_t)3, discovery_sessions_.size()); |
1233 | 1281 |
1234 // Verify the reference count by removing sessions 3 times. The last request | 1282 // Verify the reference count by removing sessions 3 times. The last request |
1235 // should remain pending. | 1283 // should remain pending. |
1236 for (int i = 0; i < 3; i++) { | 1284 for (int i = 0; i < 3; i++) { |
1237 adapter_->StopDiscovering( | 1285 discovery_sessions_[i]->Stop( |
1238 base::Bind(&BluetoothChromeOSTest::Callback, | 1286 base::Bind(&BluetoothChromeOSTest::Callback, |
1239 base::Unretained(this)), | 1287 base::Unretained(this)), |
1240 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 1288 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1241 base::Unretained(this))); | 1289 base::Unretained(this))); |
1242 } | 1290 } |
1243 EXPECT_EQ(5, callback_count_); | 1291 EXPECT_EQ(5, callback_count_); |
1244 EXPECT_EQ(1, error_callback_count_); | 1292 EXPECT_EQ(0, error_callback_count_); |
1245 EXPECT_EQ(2, observer.discovering_changed_count_); | 1293 EXPECT_EQ(2, observer.discovering_changed_count_); |
1246 EXPECT_FALSE(observer.last_discovering_); | 1294 EXPECT_FALSE(observer.last_discovering_); |
1247 EXPECT_FALSE(adapter_->IsDiscovering()); | 1295 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 1296 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); |
| 1297 EXPECT_FALSE(discovery_sessions_[1]->IsActive()); |
| 1298 EXPECT_TRUE(discovery_sessions_[2]->IsActive()); |
1248 | 1299 |
1249 // Request to stop should fail. | 1300 // Request to stop the session whose call is pending should fail. |
1250 adapter_->StopDiscovering( | 1301 discovery_sessions_[2]->Stop( |
1251 base::Bind(&BluetoothChromeOSTest::Callback, | 1302 base::Bind(&BluetoothChromeOSTest::Callback, |
1252 base::Unretained(this)), | 1303 base::Unretained(this)), |
1253 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 1304 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1254 base::Unretained(this))); | 1305 base::Unretained(this))); |
1255 EXPECT_EQ(5, callback_count_); | 1306 EXPECT_EQ(5, callback_count_); |
1256 EXPECT_EQ(2, error_callback_count_); | 1307 EXPECT_EQ(1, error_callback_count_); |
1257 EXPECT_EQ(2, observer.discovering_changed_count_); | 1308 EXPECT_EQ(2, observer.discovering_changed_count_); |
1258 EXPECT_FALSE(observer.last_discovering_); | 1309 EXPECT_FALSE(observer.last_discovering_); |
1259 EXPECT_FALSE(adapter_->IsDiscovering()); | 1310 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 1311 EXPECT_TRUE(discovery_sessions_[2]->IsActive()); |
1260 | 1312 |
1261 // Request to start should get queued. | 1313 // Request to start should get queued. |
1262 adapter_->StartDiscovering( | 1314 adapter_->StartDiscoverySession( |
1263 base::Bind(&BluetoothChromeOSTest::Callback, | 1315 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
1264 base::Unretained(this)), | 1316 base::Unretained(this)), |
1265 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 1317 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1266 base::Unretained(this))); | 1318 base::Unretained(this))); |
1267 EXPECT_EQ(5, callback_count_); | 1319 EXPECT_EQ(5, callback_count_); |
1268 EXPECT_EQ(2, error_callback_count_); | 1320 EXPECT_EQ(1, error_callback_count_); |
1269 EXPECT_EQ(2, observer.discovering_changed_count_); | 1321 EXPECT_EQ(2, observer.discovering_changed_count_); |
1270 EXPECT_FALSE(observer.last_discovering_); | 1322 EXPECT_FALSE(observer.last_discovering_); |
1271 EXPECT_FALSE(adapter_->IsDiscovering()); | 1323 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 1324 ASSERT_EQ((size_t)3, discovery_sessions_.size()); |
1272 | 1325 |
1273 // Run the pending request. | 1326 // Run the pending request. |
1274 message_loop_.Run(); | 1327 message_loop_.Run(); |
1275 EXPECT_EQ(6, callback_count_); | 1328 EXPECT_EQ(6, callback_count_); |
1276 EXPECT_EQ(2, error_callback_count_); | 1329 EXPECT_EQ(1, error_callback_count_); |
1277 EXPECT_EQ(3, observer.discovering_changed_count_); | 1330 EXPECT_EQ(3, observer.discovering_changed_count_); |
1278 EXPECT_TRUE(observer.last_discovering_); | 1331 EXPECT_TRUE(observer.last_discovering_); |
1279 EXPECT_TRUE(adapter_->IsDiscovering()); | 1332 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 1333 ASSERT_EQ((size_t)3, discovery_sessions_.size()); |
| 1334 EXPECT_FALSE(discovery_sessions_[2]->IsActive()); |
1280 | 1335 |
1281 // The queued request to start discovery should have been issued but is still | 1336 // The queued request to start discovery should have been issued but is still |
1282 // pending. Run the loop and verify. | 1337 // pending. Run the loop and verify. |
1283 message_loop_.Run(); | 1338 message_loop_.Run(); |
1284 EXPECT_EQ(7, callback_count_); | 1339 EXPECT_EQ(7, callback_count_); |
1285 EXPECT_EQ(2, error_callback_count_); | 1340 EXPECT_EQ(1, error_callback_count_); |
1286 EXPECT_EQ(3, observer.discovering_changed_count_); | 1341 EXPECT_EQ(3, observer.discovering_changed_count_); |
1287 EXPECT_TRUE(observer.last_discovering_); | 1342 EXPECT_TRUE(observer.last_discovering_); |
1288 EXPECT_TRUE(adapter_->IsDiscovering()); | 1343 EXPECT_TRUE(adapter_->IsDiscovering()); |
| 1344 ASSERT_EQ((size_t)4, discovery_sessions_.size()); |
| 1345 EXPECT_TRUE(discovery_sessions_[3]->IsActive()); |
| 1346 |
| 1347 adapter_->RemoveObserver(&observer); |
1289 } | 1348 } |
1290 | 1349 |
1291 TEST_F(BluetoothChromeOSTest, StartDiscoverySession) { | 1350 TEST_F(BluetoothChromeOSTest, StartDiscoverySession) { |
1292 GetAdapter(); | 1351 GetAdapter(); |
1293 | 1352 |
1294 adapter_->SetPowered( | 1353 adapter_->SetPowered( |
1295 true, | 1354 true, |
1296 base::Bind(&BluetoothChromeOSTest::Callback, | 1355 base::Bind(&BluetoothChromeOSTest::Callback, |
1297 base::Unretained(this)), | 1356 base::Unretained(this)), |
1298 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 1357 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1299 base::Unretained(this))); | 1358 base::Unretained(this))); |
1300 EXPECT_EQ(1, callback_count_); | 1359 EXPECT_EQ(1, callback_count_); |
1301 EXPECT_EQ(0, error_callback_count_); | 1360 EXPECT_EQ(0, error_callback_count_); |
1302 EXPECT_TRUE(adapter_->IsPowered()); | 1361 EXPECT_TRUE(adapter_->IsPowered()); |
1303 callback_count_ = 0; | 1362 callback_count_ = 0; |
1304 | 1363 |
1305 TestObserver observer(adapter_); | 1364 TestObserver observer(adapter_); |
1306 adapter_->AddObserver(&observer); | 1365 adapter_->AddObserver(&observer); |
1307 | 1366 |
1308 EXPECT_EQ(0, observer.discovering_changed_count_); | 1367 EXPECT_EQ(0, observer.discovering_changed_count_); |
1309 EXPECT_FALSE(observer.last_discovering_); | 1368 EXPECT_FALSE(observer.last_discovering_); |
1310 EXPECT_FALSE(adapter_->IsDiscovering()); | 1369 EXPECT_FALSE(adapter_->IsDiscovering()); |
1311 EXPECT_FALSE(last_discovery_session_.get()); | 1370 EXPECT_TRUE(discovery_sessions_.empty()); |
1312 | 1371 |
1313 // Request a new discovery session. | 1372 // Request a new discovery session. |
1314 adapter_->StartDiscoverySession( | 1373 adapter_->StartDiscoverySession( |
1315 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | 1374 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
1316 base::Unretained(this)), | 1375 base::Unretained(this)), |
1317 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 1376 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1318 base::Unretained(this))); | 1377 base::Unretained(this))); |
1319 message_loop_.Run(); | 1378 message_loop_.Run(); |
1320 EXPECT_EQ(1, observer.discovering_changed_count_); | 1379 EXPECT_EQ(1, observer.discovering_changed_count_); |
1321 EXPECT_EQ(1, callback_count_); | 1380 EXPECT_EQ(1, callback_count_); |
1322 EXPECT_EQ(0, error_callback_count_); | 1381 EXPECT_EQ(0, error_callback_count_); |
1323 EXPECT_TRUE(observer.last_discovering_); | 1382 EXPECT_TRUE(observer.last_discovering_); |
1324 EXPECT_TRUE(adapter_->IsDiscovering()); | 1383 EXPECT_TRUE(adapter_->IsDiscovering()); |
1325 EXPECT_TRUE(last_discovery_session_.get()); | 1384 ASSERT_EQ((size_t)1, discovery_sessions_.size()); |
1326 EXPECT_TRUE(last_discovery_session_->IsActive()); | 1385 EXPECT_TRUE(discovery_sessions_[0]->IsActive()); |
1327 | 1386 |
1328 // Start another session. A new one should be returned in the callback, which | 1387 // Start another session. A new one should be returned in the callback, which |
1329 // in turn will destroy the previous session. Adapter should still be | 1388 // in turn will destroy the previous session. Adapter should still be |
1330 // discovering and the reference count should be 1. | 1389 // discovering and the reference count should be 1. |
1331 adapter_->StartDiscoverySession( | 1390 adapter_->StartDiscoverySession( |
1332 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | 1391 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
1333 base::Unretained(this)), | 1392 base::Unretained(this)), |
1334 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 1393 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1335 base::Unretained(this))); | 1394 base::Unretained(this))); |
1336 message_loop_.Run(); | 1395 message_loop_.Run(); |
1337 EXPECT_EQ(1, observer.discovering_changed_count_); | 1396 EXPECT_EQ(1, observer.discovering_changed_count_); |
1338 EXPECT_EQ(2, callback_count_); | 1397 EXPECT_EQ(2, callback_count_); |
1339 EXPECT_EQ(0, error_callback_count_); | 1398 EXPECT_EQ(0, error_callback_count_); |
1340 EXPECT_TRUE(observer.last_discovering_); | 1399 EXPECT_TRUE(observer.last_discovering_); |
1341 EXPECT_TRUE(adapter_->IsDiscovering()); | 1400 EXPECT_TRUE(adapter_->IsDiscovering()); |
1342 EXPECT_TRUE(last_discovery_session_.get()); | 1401 ASSERT_EQ((size_t)2, discovery_sessions_.size()); |
1343 EXPECT_TRUE(last_discovery_session_->IsActive()); | 1402 EXPECT_TRUE(discovery_sessions_[0]->IsActive()); |
1344 | |
1345 // Hold on to the current discovery session to prevent it from getting | |
1346 // destroyed during the next call to StartDiscoverySession. | |
1347 scoped_ptr<BluetoothDiscoverySession> discovery_session = | |
1348 last_discovery_session_.Pass(); | |
1349 | 1403 |
1350 // Request a new session. | 1404 // Request a new session. |
1351 adapter_->StartDiscoverySession( | 1405 adapter_->StartDiscoverySession( |
1352 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | 1406 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, |
1353 base::Unretained(this)), | 1407 base::Unretained(this)), |
1354 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 1408 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1355 base::Unretained(this))); | 1409 base::Unretained(this))); |
1356 message_loop_.Run(); | 1410 message_loop_.Run(); |
1357 EXPECT_EQ(1, observer.discovering_changed_count_); | 1411 EXPECT_EQ(1, observer.discovering_changed_count_); |
1358 EXPECT_EQ(3, callback_count_); | 1412 EXPECT_EQ(3, callback_count_); |
1359 EXPECT_EQ(0, error_callback_count_); | 1413 EXPECT_EQ(0, error_callback_count_); |
1360 EXPECT_TRUE(observer.last_discovering_); | 1414 EXPECT_TRUE(observer.last_discovering_); |
1361 EXPECT_TRUE(adapter_->IsDiscovering()); | 1415 EXPECT_TRUE(adapter_->IsDiscovering()); |
1362 EXPECT_TRUE(last_discovery_session_.get()); | 1416 ASSERT_EQ((size_t)3, discovery_sessions_.size()); |
1363 EXPECT_TRUE(last_discovery_session_->IsActive()); | 1417 EXPECT_TRUE(discovery_sessions_[1]->IsActive()); |
1364 EXPECT_NE(last_discovery_session_.get(), discovery_session); | 1418 EXPECT_NE(discovery_sessions_[0], discovery_sessions_[1]); |
1365 | 1419 |
1366 // Stop the previous discovery session. The session should end but discovery | 1420 // Stop the previous discovery session. The session should end but discovery |
1367 // should continue. | 1421 // should continue. |
1368 discovery_session->Stop( | 1422 discovery_sessions_[0]->Stop( |
1369 base::Bind(&BluetoothChromeOSTest::Callback, | 1423 base::Bind(&BluetoothChromeOSTest::Callback, |
1370 base::Unretained(this)), | 1424 base::Unretained(this)), |
1371 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | 1425 base::Bind(&BluetoothChromeOSTest::ErrorCallback, |
1372 base::Unretained(this))); | 1426 base::Unretained(this))); |
1373 message_loop_.Run(); | 1427 message_loop_.Run(); |
1374 EXPECT_EQ(1, observer.discovering_changed_count_); | 1428 EXPECT_EQ(1, observer.discovering_changed_count_); |
1375 EXPECT_EQ(4, callback_count_); | 1429 EXPECT_EQ(4, callback_count_); |
1376 EXPECT_EQ(0, error_callback_count_); | 1430 EXPECT_EQ(0, error_callback_count_); |
1377 EXPECT_TRUE(observer.last_discovering_); | 1431 EXPECT_TRUE(observer.last_discovering_); |
1378 EXPECT_TRUE(adapter_->IsDiscovering()); | 1432 EXPECT_TRUE(adapter_->IsDiscovering()); |
1379 EXPECT_TRUE(last_discovery_session_.get()); | 1433 ASSERT_EQ((size_t)3, discovery_sessions_.size()); |
1380 EXPECT_TRUE(last_discovery_session_->IsActive()); | 1434 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); |
1381 EXPECT_FALSE(discovery_session->IsActive()); | 1435 EXPECT_TRUE(discovery_sessions_[1]->IsActive()); |
1382 | 1436 |
1383 // Delete the current active session. Discovery should eventually stop. | 1437 // Delete the current active session. Discovery should eventually stop. |
1384 last_discovery_session_.reset(); | 1438 discovery_sessions_.clear(); |
1385 while (observer.last_discovering_) { | 1439 while (observer.last_discovering_) |
1386 message_loop_.RunUntilIdle(); | 1440 message_loop_.RunUntilIdle(); |
1387 } | 1441 |
1388 EXPECT_EQ(2, observer.discovering_changed_count_); | 1442 EXPECT_EQ(2, observer.discovering_changed_count_); |
1389 EXPECT_EQ(4, callback_count_); | 1443 EXPECT_EQ(4, callback_count_); |
1390 EXPECT_EQ(0, error_callback_count_); | 1444 EXPECT_EQ(0, error_callback_count_); |
1391 EXPECT_FALSE(observer.last_discovering_); | 1445 EXPECT_FALSE(observer.last_discovering_); |
1392 EXPECT_FALSE(adapter_->IsDiscovering()); | 1446 EXPECT_FALSE(adapter_->IsDiscovering()); |
1393 EXPECT_FALSE(last_discovery_session_.get()); | |
1394 | 1447 |
1395 adapter_->RemoveObserver(&observer); | 1448 adapter_->RemoveObserver(&observer); |
1396 } | 1449 } |
1397 | 1450 |
1398 TEST_F(BluetoothChromeOSTest, DeviceProperties) { | 1451 TEST_F(BluetoothChromeOSTest, DeviceProperties) { |
1399 GetAdapter(); | 1452 GetAdapter(); |
1400 | 1453 |
1401 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | 1454 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
1402 ASSERT_EQ(1U, devices.size()); | 1455 ASSERT_EQ(1U, devices.size()); |
1403 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, | 1456 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress, |
(...skipping 1732 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3136 | 3189 |
3137 // Unknown vendor specification identifier. | 3190 // Unknown vendor specification identifier. |
3138 properties->modalias.ReplaceValue("chrome:v00E0p2400d0400"); | 3191 properties->modalias.ReplaceValue("chrome:v00E0p2400d0400"); |
3139 | 3192 |
3140 EXPECT_EQ(0, device->GetVendorID()); | 3193 EXPECT_EQ(0, device->GetVendorID()); |
3141 EXPECT_EQ(0, device->GetProductID()); | 3194 EXPECT_EQ(0, device->GetProductID()); |
3142 EXPECT_EQ(0, device->GetDeviceID()); | 3195 EXPECT_EQ(0, device->GetDeviceID()); |
3143 } | 3196 } |
3144 | 3197 |
3145 } // namespace chromeos | 3198 } // namespace chromeos |
OLD | NEW |