bluetooth_feature_pod_controller_unittest.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // Copyright 2021 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/bluetooth_feature_pod_controller.h"
  5. #include <memory>
  6. #include <utility>
  7. #include <vector>
  8. #include "ash/constants/ash_features.h"
  9. #include "ash/resources/vector_icons/vector_icons.h"
  10. #include "ash/strings/grit/ash_strings.h"
  11. #include "ash/system/unified/detailed_view_controller.h"
  12. #include "ash/system/unified/feature_pod_button.h"
  13. #include "ash/system/unified/unified_system_tray.h"
  14. #include "ash/system/unified/unified_system_tray_bubble.h"
  15. #include "ash/system/unified/unified_system_tray_controller.h"
  16. #include "ash/system/unified/unified_system_tray_view.h"
  17. #include "ash/test/ash_test_base.h"
  18. #include "ash/test/ash_test_helper.h"
  19. #include "base/i18n/number_formatting.h"
  20. #include "base/strings/string_number_conversions.h"
  21. #include "base/strings/utf_string_conversions.h"
  22. #include "base/test/scoped_feature_list.h"
  23. #include "chromeos/services/bluetooth_config/fake_adapter_state_controller.h"
  24. #include "chromeos/services/bluetooth_config/fake_device_cache.h"
  25. #include "chromeos/services/bluetooth_config/public/mojom/cros_bluetooth_config.mojom.h"
  26. #include "chromeos/services/bluetooth_config/scoped_bluetooth_config_test_helper.h"
  27. #include "ui/base/l10n/l10n_util.h"
  28. using chromeos::bluetooth_config::ScopedBluetoothConfigTestHelper;
  29. using chromeos::bluetooth_config::mojom::BatteryProperties;
  30. using chromeos::bluetooth_config::mojom::BluetoothDeviceProperties;
  31. using chromeos::bluetooth_config::mojom::BluetoothSystemState;
  32. using chromeos::bluetooth_config::mojom::DeviceBatteryInfo;
  33. using chromeos::bluetooth_config::mojom::DeviceBatteryInfoPtr;
  34. using chromeos::bluetooth_config::mojom::DeviceConnectionState;
  35. using chromeos::bluetooth_config::mojom::PairedBluetoothDeviceProperties;
  36. using chromeos::bluetooth_config::mojom::PairedBluetoothDevicePropertiesPtr;
  37. namespace ash {
  38. // The values used to configure a Bluetooth device and validate that the
  39. // nickname, public name, and battery information is displayed correctly.
  40. const char* kDeviceNickname = "fancy squares";
  41. const char* kDevicePublicName = "Rubik's Cube";
  42. constexpr uint8_t kBatteryPercentage = 27;
  43. constexpr uint8_t kLeftBudBatteryPercentage = 23;
  44. constexpr uint8_t kRightBudBatteryPercentage = 11;
  45. constexpr uint8_t kCaseBatteryPercentage = 77;
  46. // How many devices to "pair" for tests that require multiple connected devices.
  47. constexpr int kMultipleDeviceCount = 3;
  48. class BluetoothFeaturePodControllerTest : public AshTestBase {
  49. public:
  50. void SetUp() override {
  51. AshTestBase::SetUp();
  52. feature_list_.InitAndEnableFeature(features::kBluetoothRevamp);
  53. GetPrimaryUnifiedSystemTray()->ShowBubble();
  54. bluetooth_pod_controller_ =
  55. std::make_unique<BluetoothFeaturePodController>(tray_controller());
  56. feature_pod_button_ =
  57. base::WrapUnique(bluetooth_pod_controller_->CreateButton());
  58. base::RunLoop().RunUntilIdle();
  59. }
  60. void TearDown() override {
  61. bluetooth_pod_controller_.reset();
  62. AshTestBase::TearDown();
  63. }
  64. DeviceBatteryInfoPtr CreateDefaultBatteryInfo() {
  65. DeviceBatteryInfoPtr battery_info = DeviceBatteryInfo::New();
  66. battery_info->default_properties = BatteryProperties::New();
  67. battery_info->default_properties->battery_percentage = kBatteryPercentage;
  68. return battery_info;
  69. }
  70. DeviceBatteryInfoPtr CreateMultipleBatteryInfo(
  71. absl::optional<int> left_bud_battery,
  72. absl::optional<int> case_battery,
  73. absl::optional<int> right_bud_battery) {
  74. DeviceBatteryInfoPtr battery_info = DeviceBatteryInfo::New();
  75. if (left_bud_battery) {
  76. battery_info->left_bud_info = BatteryProperties::New();
  77. battery_info->left_bud_info->battery_percentage =
  78. left_bud_battery.value();
  79. }
  80. if (case_battery) {
  81. battery_info->case_info = BatteryProperties::New();
  82. battery_info->case_info->battery_percentage = case_battery.value();
  83. }
  84. if (right_bud_battery) {
  85. battery_info->right_bud_info = BatteryProperties::New();
  86. battery_info->right_bud_info->battery_percentage =
  87. right_bud_battery.value();
  88. }
  89. return battery_info;
  90. }
  91. void ExpectBluetoothDetailedViewFocused() {
  92. EXPECT_TRUE(tray_view()->detailed_view());
  93. const FeaturePodIconButton::Views& children =
  94. tray_view()->detailed_view()->children();
  95. EXPECT_EQ(1u, children.size());
  96. EXPECT_STREQ("BluetoothDetailedViewImpl", children.at(0)->GetClassName());
  97. }
  98. void LockScreen() {
  99. bluetooth_config_test_helper()->session_manager()->SessionStarted();
  100. bluetooth_config_test_helper()->session_manager()->SetSessionState(
  101. session_manager::SessionState::LOCKED);
  102. base::RunLoop().RunUntilIdle();
  103. }
  104. void PressIcon() {
  105. bluetooth_pod_controller_->OnIconPressed();
  106. base::RunLoop().RunUntilIdle();
  107. }
  108. void PressLabel() {
  109. bluetooth_pod_controller_->OnLabelPressed();
  110. base::RunLoop().RunUntilIdle();
  111. }
  112. void SetConnectedDevice(
  113. const PairedBluetoothDevicePropertiesPtr& connected_device) {
  114. std::vector<PairedBluetoothDevicePropertiesPtr> paired_devices;
  115. paired_devices.push_back(mojo::Clone(connected_device));
  116. SetPairedDevices(std::move(paired_devices));
  117. }
  118. void SetPairedDevices(
  119. std::vector<PairedBluetoothDevicePropertiesPtr> paired_devices) {
  120. fake_device_cache()->SetPairedDevices(std::move(paired_devices));
  121. base::RunLoop().RunUntilIdle();
  122. }
  123. void SetSystemState(BluetoothSystemState system_state) {
  124. bluetooth_config_test_helper()
  125. ->fake_adapter_state_controller()
  126. ->SetSystemState(system_state);
  127. base::RunLoop().RunUntilIdle();
  128. }
  129. const gfx::VectorIcon* feature_pod_icon_button_icon() {
  130. return feature_pod_button_->icon_button_->icon_;
  131. }
  132. const ash::FeaturePodLabelButton* feature_pod_label_button() {
  133. return feature_pod_button_->label_button_;
  134. }
  135. chromeos::bluetooth_config::FakeDeviceCache* fake_device_cache() {
  136. return bluetooth_config_test_helper()->fake_device_cache();
  137. }
  138. UnifiedSystemTrayController* tray_controller() {
  139. return GetPrimaryUnifiedSystemTray()
  140. ->bubble()
  141. ->unified_system_tray_controller();
  142. }
  143. UnifiedSystemTrayView* tray_view() {
  144. return GetPrimaryUnifiedSystemTray()->bubble()->unified_view();
  145. }
  146. protected:
  147. std::unique_ptr<FeaturePodButton> feature_pod_button_;
  148. private:
  149. ScopedBluetoothConfigTestHelper* bluetooth_config_test_helper() {
  150. return ash_test_helper()->bluetooth_config_test_helper();
  151. }
  152. std::unique_ptr<BluetoothFeaturePodController> bluetooth_pod_controller_;
  153. base::test::ScopedFeatureList feature_list_;
  154. };
  155. TEST_F(BluetoothFeaturePodControllerTest,
  156. HasCorrectButtonStateWhenBluetoothStateChanges) {
  157. SetSystemState(BluetoothSystemState::kUnavailable);
  158. EXPECT_FALSE(feature_pod_button_->GetEnabled());
  159. EXPECT_FALSE(feature_pod_button_->GetVisible());
  160. for (const auto& system_state :
  161. {BluetoothSystemState::kDisabled, BluetoothSystemState::kDisabling}) {
  162. SetSystemState(system_state);
  163. EXPECT_FALSE(feature_pod_button_->IsToggled());
  164. EXPECT_TRUE(feature_pod_button_->GetVisible());
  165. }
  166. for (const auto& system_state :
  167. {BluetoothSystemState::kEnabled, BluetoothSystemState::kEnabling}) {
  168. SetSystemState(system_state);
  169. EXPECT_TRUE(feature_pod_button_->IsToggled());
  170. EXPECT_TRUE(feature_pod_button_->GetVisible());
  171. }
  172. }
  173. TEST_F(BluetoothFeaturePodControllerTest, PressingIconOrLabelChangesBluetooth) {
  174. EXPECT_TRUE(feature_pod_button_->IsToggled());
  175. PressIcon();
  176. EXPECT_FALSE(feature_pod_button_->IsToggled());
  177. PressLabel();
  178. EXPECT_TRUE(feature_pod_button_->IsToggled());
  179. }
  180. TEST_F(BluetoothFeaturePodControllerTest, HasCorrectMetadataWhenOff) {
  181. SetSystemState(BluetoothSystemState::kDisabled);
  182. EXPECT_FALSE(feature_pod_button_->IsToggled());
  183. EXPECT_TRUE(feature_pod_button_->GetVisible());
  184. const ash::FeaturePodLabelButton* label_button = feature_pod_label_button();
  185. EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH),
  186. label_button->GetLabelText());
  187. EXPECT_EQ(
  188. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH_DISABLED_SHORT),
  189. label_button->GetSubLabelText());
  190. EXPECT_EQ(l10n_util::GetStringFUTF16(
  191. IDS_ASH_STATUS_TRAY_BLUETOOTH_TOGGLE_TOOLTIP,
  192. l10n_util::GetStringUTF16(
  193. IDS_ASH_STATUS_TRAY_BLUETOOTH_DISABLED_TOOLTIP)),
  194. label_button->GetTooltipText());
  195. const ash::FeaturePodIconButton* icon_button =
  196. feature_pod_button_->icon_button();
  197. EXPECT_STREQ(kUnifiedMenuBluetoothDisabledIcon.name,
  198. feature_pod_icon_button_icon()->name);
  199. EXPECT_EQ(l10n_util::GetStringFUTF16(
  200. IDS_ASH_STATUS_TRAY_BLUETOOTH_TOGGLE_TOOLTIP,
  201. l10n_util::GetStringUTF16(
  202. IDS_ASH_STATUS_TRAY_BLUETOOTH_DISABLED_TOOLTIP)),
  203. icon_button->GetTooltipText());
  204. }
  205. TEST_F(BluetoothFeaturePodControllerTest, HasCorrectMetadataWithZeroDevices) {
  206. SetSystemState(BluetoothSystemState::kEnabled);
  207. const ash::FeaturePodLabelButton* label_button = feature_pod_label_button();
  208. EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH),
  209. label_button->GetLabelText());
  210. EXPECT_EQ(
  211. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH_ENABLED_SHORT),
  212. label_button->GetSubLabelText());
  213. EXPECT_EQ(l10n_util::GetStringFUTF16(
  214. IDS_ASH_STATUS_TRAY_BLUETOOTH_SETTINGS_TOOLTIP,
  215. l10n_util::GetStringUTF16(
  216. IDS_ASH_STATUS_TRAY_BLUETOOTH_ENABLED_TOOLTIP)),
  217. label_button->GetTooltipText());
  218. const ash::FeaturePodIconButton* icon_button =
  219. feature_pod_button_->icon_button();
  220. EXPECT_STREQ(kUnifiedMenuBluetoothIcon.name,
  221. feature_pod_icon_button_icon()->name);
  222. EXPECT_EQ(l10n_util::GetStringFUTF16(
  223. IDS_ASH_STATUS_TRAY_BLUETOOTH_TOGGLE_TOOLTIP,
  224. l10n_util::GetStringUTF16(
  225. IDS_ASH_STATUS_TRAY_BLUETOOTH_ENABLED_TOOLTIP)),
  226. icon_button->GetTooltipText());
  227. }
  228. TEST_F(BluetoothFeaturePodControllerTest, HasCorrectMetadataWithOneDevice) {
  229. SetSystemState(BluetoothSystemState::kEnabled);
  230. const std::u16string public_name = base::ASCIIToUTF16(kDevicePublicName);
  231. // Create a device with the minimal configuration, mark it as connected, and
  232. // reset the list of paired devices to only contain it.
  233. auto paired_device = PairedBluetoothDeviceProperties::New();
  234. paired_device->device_properties = BluetoothDeviceProperties::New();
  235. paired_device->device_properties->public_name = public_name;
  236. paired_device->device_properties->connection_state =
  237. DeviceConnectionState::kConnected;
  238. SetConnectedDevice(paired_device);
  239. const ash::FeaturePodLabelButton* label_button = feature_pod_label_button();
  240. EXPECT_EQ(public_name, label_button->GetLabelText());
  241. EXPECT_EQ(l10n_util::GetStringUTF16(
  242. IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_CONNECTED_LABEL),
  243. label_button->GetSubLabelText());
  244. EXPECT_EQ(l10n_util::GetStringFUTF16(
  245. IDS_ASH_STATUS_TRAY_BLUETOOTH_SETTINGS_TOOLTIP,
  246. l10n_util::GetStringFUTF16(
  247. IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_CONNECTED_TOOLTIP,
  248. public_name)),
  249. label_button->GetTooltipText());
  250. const ash::FeaturePodIconButton* icon_button =
  251. feature_pod_button_->icon_button();
  252. EXPECT_STREQ(kUnifiedMenuBluetoothConnectedIcon.name,
  253. feature_pod_icon_button_icon()->name);
  254. EXPECT_EQ(l10n_util::GetStringFUTF16(
  255. IDS_ASH_STATUS_TRAY_BLUETOOTH_TOGGLE_TOOLTIP,
  256. l10n_util::GetStringFUTF16(
  257. IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_CONNECTED_TOOLTIP,
  258. public_name)),
  259. icon_button->GetTooltipText());
  260. // Change the device nickname and reset the paired device list.
  261. paired_device->nickname = kDeviceNickname;
  262. SetConnectedDevice(paired_device);
  263. EXPECT_EQ(base::ASCIIToUTF16(kDeviceNickname), label_button->GetLabelText());
  264. // Change the device battery information and reset the paired device list.
  265. paired_device->device_properties->battery_info = CreateDefaultBatteryInfo();
  266. SetConnectedDevice(paired_device);
  267. EXPECT_EQ(l10n_util::GetStringFUTF16(
  268. IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_BATTERY_PERCENTAGE_LABEL,
  269. base::NumberToString16(kBatteryPercentage)),
  270. label_button->GetSubLabelText());
  271. }
  272. TEST_F(BluetoothFeaturePodControllerTest,
  273. HasCorrectMetadataWithOneDevice_MultipleBatteries) {
  274. SetSystemState(BluetoothSystemState::kEnabled);
  275. const std::u16string public_name = base::ASCIIToUTF16(kDevicePublicName);
  276. auto paired_device = PairedBluetoothDeviceProperties::New();
  277. paired_device->device_properties = BluetoothDeviceProperties::New();
  278. paired_device->device_properties->public_name = public_name;
  279. paired_device->device_properties->connection_state =
  280. DeviceConnectionState::kConnected;
  281. paired_device->device_properties->battery_info =
  282. CreateMultipleBatteryInfo(/*left_bud_battery=*/kLeftBudBatteryPercentage,
  283. /*case_battery=*/kCaseBatteryPercentage,
  284. /*right_battery=*/kRightBudBatteryPercentage);
  285. SetConnectedDevice(paired_device);
  286. const ash::FeaturePodLabelButton* label_button = feature_pod_label_button();
  287. EXPECT_EQ(l10n_util::GetStringFUTF16(
  288. IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_BATTERY_PERCENTAGE_LABEL,
  289. base::NumberToString16(kLeftBudBatteryPercentage)),
  290. label_button->GetSubLabelText());
  291. paired_device->device_properties->battery_info =
  292. CreateMultipleBatteryInfo(/*left_bud_battery=*/absl::nullopt,
  293. /*case_battery=*/kCaseBatteryPercentage,
  294. /*right_battery=*/kRightBudBatteryPercentage);
  295. SetConnectedDevice(paired_device);
  296. EXPECT_EQ(l10n_util::GetStringFUTF16(
  297. IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_BATTERY_PERCENTAGE_LABEL,
  298. base::NumberToString16(kRightBudBatteryPercentage)),
  299. label_button->GetSubLabelText());
  300. paired_device->device_properties->battery_info = CreateMultipleBatteryInfo(
  301. /*left_bud_battery=*/absl::nullopt,
  302. /*case_battery=*/kCaseBatteryPercentage, /*right_battery=*/absl::nullopt);
  303. SetConnectedDevice(paired_device);
  304. EXPECT_EQ(l10n_util::GetStringFUTF16(
  305. IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_BATTERY_PERCENTAGE_LABEL,
  306. base::NumberToString16(kCaseBatteryPercentage)),
  307. label_button->GetSubLabelText());
  308. }
  309. TEST_F(BluetoothFeaturePodControllerTest,
  310. HasCorrectMetadataWithMultipleDevice) {
  311. SetSystemState(BluetoothSystemState::kEnabled);
  312. // Create a device with basic battery information, mark it as connected, and
  313. // reset the list of paired devices with multiple duplicates of it.
  314. auto paired_device = PairedBluetoothDeviceProperties::New();
  315. paired_device->device_properties = BluetoothDeviceProperties::New();
  316. paired_device->device_properties->connection_state =
  317. DeviceConnectionState::kConnected;
  318. paired_device->device_properties->battery_info = CreateDefaultBatteryInfo();
  319. std::vector<PairedBluetoothDevicePropertiesPtr> paired_devices;
  320. for (int i = 0; i < kMultipleDeviceCount; ++i) {
  321. paired_devices.push_back(mojo::Clone(paired_device));
  322. }
  323. SetPairedDevices(std::move(paired_devices));
  324. const ash::FeaturePodLabelButton* label_button = feature_pod_label_button();
  325. EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH),
  326. label_button->GetLabelText());
  327. EXPECT_EQ(l10n_util::GetStringFUTF16(
  328. IDS_ASH_STATUS_TRAY_BLUETOOTH_MULTIPLE_DEVICES_CONNECTED_LABEL,
  329. base::FormatNumber(kMultipleDeviceCount)),
  330. label_button->GetSubLabelText());
  331. EXPECT_EQ(
  332. l10n_util::GetStringFUTF16(
  333. IDS_ASH_STATUS_TRAY_BLUETOOTH_SETTINGS_TOOLTIP,
  334. l10n_util::GetStringFUTF16(
  335. IDS_ASH_STATUS_TRAY_BLUETOOTH_MULTIPLE_DEVICES_CONNECTED_TOOLTIP,
  336. base::FormatNumber(kMultipleDeviceCount))),
  337. label_button->GetTooltipText());
  338. const ash::FeaturePodIconButton* icon_button =
  339. feature_pod_button_->icon_button();
  340. EXPECT_STREQ(kUnifiedMenuBluetoothConnectedIcon.name,
  341. feature_pod_icon_button_icon()->name);
  342. EXPECT_EQ(
  343. l10n_util::GetStringFUTF16(
  344. IDS_ASH_STATUS_TRAY_BLUETOOTH_TOGGLE_TOOLTIP,
  345. l10n_util::GetStringFUTF16(
  346. IDS_ASH_STATUS_TRAY_BLUETOOTH_MULTIPLE_DEVICES_CONNECTED_TOOLTIP,
  347. base::FormatNumber(kMultipleDeviceCount))),
  348. icon_button->GetTooltipText());
  349. }
  350. TEST_F(BluetoothFeaturePodControllerTest,
  351. EnablingBluetoothShowsBluetoothDetailedView) {
  352. SetSystemState(BluetoothSystemState::kDisabled);
  353. EXPECT_FALSE(feature_pod_button_->IsToggled());
  354. PressIcon();
  355. EXPECT_TRUE(feature_pod_button_->IsToggled());
  356. ExpectBluetoothDetailedViewFocused();
  357. }
  358. TEST_F(BluetoothFeaturePodControllerTest,
  359. PressingLabelWithEnabledBluetoothShowsBluetoothDetailedView) {
  360. EXPECT_TRUE(feature_pod_button_->IsToggled());
  361. PressLabel();
  362. ExpectBluetoothDetailedViewFocused();
  363. }
  364. TEST_F(BluetoothFeaturePodControllerTest,
  365. FeaturePodIsDisabledWhenBluetoothCannotBeModified) {
  366. EXPECT_TRUE(feature_pod_button_->GetEnabled());
  367. // The lock screen is one of multiple session states where Bluetooth cannot be
  368. // modified. For more information see
  369. // chromeos::bluetooth_config::SystemPropertiesProvider.
  370. LockScreen();
  371. EXPECT_FALSE(feature_pod_button_->GetEnabled());
  372. }
  373. } // namespace ash