unified_bluetooth_detailed_view_controller_unittest.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/system/bluetooth/unified_bluetooth_detailed_view_controller.h"
  5. #include <memory>
  6. #include "ash/constants/ash_features.h"
  7. #include "ash/system/bluetooth/bluetooth_detailed_view_legacy.h"
  8. #include "ash/system/bluetooth/bluetooth_power_controller.h"
  9. #include "ash/system/bluetooth/tray_bluetooth_helper.h"
  10. #include "ash/system/unified/unified_system_tray_controller.h"
  11. #include "ash/system/unified/unified_system_tray_model.h"
  12. #include "ash/test/ash_test_base.h"
  13. #include "base/memory/scoped_refptr.h"
  14. #include "base/strings/stringprintf.h"
  15. #include "base/test/scoped_feature_list.h"
  16. #include "device/bluetooth/dbus/bluez_dbus_manager.h"
  17. #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h"
  18. #include "device/bluetooth/dbus/fake_bluetooth_device_client.h"
  19. #include "ui/views/view.h"
  20. namespace ash {
  21. namespace {
  22. const base::TimeDelta kUpdateFrequencyMs = base::Milliseconds(1000);
  23. } // namespace
  24. class UnifiedBluetoothDetailedViewControllerTest : public AshTestBase {
  25. public:
  26. UnifiedBluetoothDetailedViewControllerTest()
  27. : AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  28. UnifiedBluetoothDetailedViewControllerTest(
  29. const UnifiedBluetoothDetailedViewControllerTest&) = delete;
  30. UnifiedBluetoothDetailedViewControllerTest& operator=(
  31. const UnifiedBluetoothDetailedViewControllerTest&) = delete;
  32. ~UnifiedBluetoothDetailedViewControllerTest() override = default;
  33. void SetUp() override {
  34. // These tests should only be run with the kBluetoothRevamp feature flag is
  35. // disabled, and so we force it off here and ensure that the local state
  36. // prefs that would have been registered had the feature flag been off are
  37. // registered.
  38. if (ash::features::IsBluetoothRevampEnabled()) {
  39. feature_list_.InitAndDisableFeature(features::kBluetoothRevamp);
  40. BluetoothPowerController::RegisterLocalStatePrefs(
  41. local_state()->registry());
  42. }
  43. AshTestBase::SetUp();
  44. // Set fake adapter client to powered-on and initialize with zero simulation
  45. // interval.
  46. adapter_client_ = static_cast<bluez::FakeBluetoothAdapterClient*>(
  47. bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient());
  48. adapter_client_->SetSimulationIntervalMs(0);
  49. SetAdapterPowered(true);
  50. // Enable fake device client and initialize with zero simulation interval.
  51. device_client_ = static_cast<bluez::FakeBluetoothDeviceClient*>(
  52. bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient());
  53. device_client_->SetSimulationIntervalMs(0);
  54. task_environment()->RunUntilIdle();
  55. tray_model_ = base::MakeRefCounted<UnifiedSystemTrayModel>(nullptr);
  56. tray_controller_ =
  57. std::make_unique<UnifiedSystemTrayController>(tray_model_.get());
  58. bt_detailed_view_controller_ =
  59. std::make_unique<UnifiedBluetoothDetailedViewController>(
  60. tray_controller_.get());
  61. }
  62. void TearDown() override {
  63. bt_detailed_view_controller_.reset();
  64. tray_controller_.reset();
  65. tray_model_.reset();
  66. AshTestBase::TearDown();
  67. }
  68. void SetAdapterPowered(bool powered) {
  69. adapter_client_
  70. ->GetProperties(
  71. dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath))
  72. ->powered.ReplaceValue(powered);
  73. task_environment()->RunUntilIdle();
  74. }
  75. void AddTestDevice() {
  76. bluez::FakeBluetoothDeviceClient::IncomingDeviceProperties props;
  77. props.device_path = base::StringPrintf("/fake/hci0/dev%02d", next_id_);
  78. props.device_address = base::StringPrintf("00:00:00:00:00:%02d", next_id_);
  79. props.device_name = "Test Device";
  80. props.device_class = 0x01;
  81. device_client_->CreateDeviceWithProperties(
  82. dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
  83. props);
  84. next_id_++;
  85. }
  86. void RemoveAllDevices() {
  87. dbus::ObjectPath fake_adapter_path(
  88. bluez::FakeBluetoothAdapterClient::kAdapterPath);
  89. std::vector<dbus::ObjectPath> device_paths =
  90. device_client_->GetDevicesForAdapter(fake_adapter_path);
  91. for (auto& device_path : device_paths) {
  92. device_client_->RemoveDevice(fake_adapter_path, device_path);
  93. }
  94. }
  95. UnifiedBluetoothDetailedViewController* bt_detailed_view_controller() {
  96. return bt_detailed_view_controller_.get();
  97. }
  98. bluez::FakeBluetoothAdapterClient* adapter_client() {
  99. return adapter_client_;
  100. }
  101. private:
  102. base::test::ScopedFeatureList feature_list_;
  103. bluez::FakeBluetoothAdapterClient* adapter_client_;
  104. bluez::FakeBluetoothDeviceClient* device_client_;
  105. scoped_refptr<UnifiedSystemTrayModel> tray_model_;
  106. std::unique_ptr<UnifiedSystemTrayController> tray_controller_;
  107. std::unique_ptr<UnifiedBluetoothDetailedViewController>
  108. bt_detailed_view_controller_;
  109. int next_id_ = 1;
  110. };
  111. TEST_F(UnifiedBluetoothDetailedViewControllerTest, UpdateScrollListTest) {
  112. std::unique_ptr<BluetoothDetailedViewLegacy> bluetooth_detailed_view =
  113. base::WrapUnique(static_cast<BluetoothDetailedViewLegacy*>(
  114. bt_detailed_view_controller()->CreateView()));
  115. AddTestDevice();
  116. task_environment()->FastForwardBy(kUpdateFrequencyMs);
  117. // Verify that default devices simulated by FakeBluetoothDeviceClient are
  118. // displayed.
  119. const views::View* scroll_content = bluetooth_detailed_view->GetViewByID(
  120. BluetoothDetailedViewLegacy::kScrollContentID);
  121. const size_t scroll_content_size = scroll_content->children().size();
  122. // Expect at least 1 paired device, 1 unpaired device and 2 headers.
  123. EXPECT_GE(scroll_content_size, 4u);
  124. // Fast forward to next bluetooth list sync and verify that child views
  125. // are re-used after update.
  126. views::View* scroll_content_child = scroll_content->children()[1];
  127. task_environment()->FastForwardBy(kUpdateFrequencyMs);
  128. EXPECT_EQ(scroll_content_child, scroll_content->children()[1]);
  129. // Verify that newly added devices is displayed.
  130. AddTestDevice();
  131. task_environment()->FastForwardBy(kUpdateFrequencyMs);
  132. EXPECT_EQ(scroll_content_size + 1u, scroll_content->children().size());
  133. }
  134. TEST_F(UnifiedBluetoothDetailedViewControllerTest,
  135. UpdateScrollListPowerCycleTest) {
  136. // Disable discovery simulation and remove all existing
  137. // devices so that only the "Scanning" message is displayed.
  138. adapter_client()->SetDiscoverySimulation(false);
  139. RemoveAllDevices();
  140. std::unique_ptr<BluetoothDetailedViewLegacy> bluetooth_detailed_view =
  141. base::WrapUnique(static_cast<BluetoothDetailedViewLegacy*>(
  142. bt_detailed_view_controller()->CreateView()));
  143. task_environment()->FastForwardBy(kUpdateFrequencyMs);
  144. const views::View* scroll_content = bluetooth_detailed_view->GetViewByID(
  145. BluetoothDetailedViewLegacy::kScrollContentID);
  146. // Only the scanning message should be displayed.
  147. EXPECT_EQ(1u, scroll_content->children().size());
  148. // Verify that if bluetooth is powered off and back on again
  149. // the scroll list is cleared and populated back again properly.
  150. SetAdapterPowered(false);
  151. EXPECT_EQ(0u, scroll_content->children().size());
  152. SetAdapterPowered(true);
  153. EXPECT_EQ(1u, scroll_content->children().size());
  154. }
  155. } // namespace ash