123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- // Copyright 2021 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "ash/system/bluetooth/bluetooth_feature_pod_controller.h"
- #include <memory>
- #include <utility>
- #include <vector>
- #include "ash/constants/ash_features.h"
- #include "ash/resources/vector_icons/vector_icons.h"
- #include "ash/strings/grit/ash_strings.h"
- #include "ash/system/unified/detailed_view_controller.h"
- #include "ash/system/unified/feature_pod_button.h"
- #include "ash/system/unified/unified_system_tray.h"
- #include "ash/system/unified/unified_system_tray_bubble.h"
- #include "ash/system/unified/unified_system_tray_controller.h"
- #include "ash/system/unified/unified_system_tray_view.h"
- #include "ash/test/ash_test_base.h"
- #include "ash/test/ash_test_helper.h"
- #include "base/i18n/number_formatting.h"
- #include "base/strings/string_number_conversions.h"
- #include "base/strings/utf_string_conversions.h"
- #include "base/test/scoped_feature_list.h"
- #include "chromeos/services/bluetooth_config/fake_adapter_state_controller.h"
- #include "chromeos/services/bluetooth_config/fake_device_cache.h"
- #include "chromeos/services/bluetooth_config/public/mojom/cros_bluetooth_config.mojom.h"
- #include "chromeos/services/bluetooth_config/scoped_bluetooth_config_test_helper.h"
- #include "ui/base/l10n/l10n_util.h"
- using chromeos::bluetooth_config::ScopedBluetoothConfigTestHelper;
- using chromeos::bluetooth_config::mojom::BatteryProperties;
- using chromeos::bluetooth_config::mojom::BluetoothDeviceProperties;
- using chromeos::bluetooth_config::mojom::BluetoothSystemState;
- using chromeos::bluetooth_config::mojom::DeviceBatteryInfo;
- using chromeos::bluetooth_config::mojom::DeviceBatteryInfoPtr;
- using chromeos::bluetooth_config::mojom::DeviceConnectionState;
- using chromeos::bluetooth_config::mojom::PairedBluetoothDeviceProperties;
- using chromeos::bluetooth_config::mojom::PairedBluetoothDevicePropertiesPtr;
- namespace ash {
- // The values used to configure a Bluetooth device and validate that the
- // nickname, public name, and battery information is displayed correctly.
- const char* kDeviceNickname = "fancy squares";
- const char* kDevicePublicName = "Rubik's Cube";
- constexpr uint8_t kBatteryPercentage = 27;
- constexpr uint8_t kLeftBudBatteryPercentage = 23;
- constexpr uint8_t kRightBudBatteryPercentage = 11;
- constexpr uint8_t kCaseBatteryPercentage = 77;
- // How many devices to "pair" for tests that require multiple connected devices.
- constexpr int kMultipleDeviceCount = 3;
- class BluetoothFeaturePodControllerTest : public AshTestBase {
- public:
- void SetUp() override {
- AshTestBase::SetUp();
- feature_list_.InitAndEnableFeature(features::kBluetoothRevamp);
- GetPrimaryUnifiedSystemTray()->ShowBubble();
- bluetooth_pod_controller_ =
- std::make_unique<BluetoothFeaturePodController>(tray_controller());
- feature_pod_button_ =
- base::WrapUnique(bluetooth_pod_controller_->CreateButton());
- base::RunLoop().RunUntilIdle();
- }
- void TearDown() override {
- bluetooth_pod_controller_.reset();
- AshTestBase::TearDown();
- }
- DeviceBatteryInfoPtr CreateDefaultBatteryInfo() {
- DeviceBatteryInfoPtr battery_info = DeviceBatteryInfo::New();
- battery_info->default_properties = BatteryProperties::New();
- battery_info->default_properties->battery_percentage = kBatteryPercentage;
- return battery_info;
- }
- DeviceBatteryInfoPtr CreateMultipleBatteryInfo(
- absl::optional<int> left_bud_battery,
- absl::optional<int> case_battery,
- absl::optional<int> right_bud_battery) {
- DeviceBatteryInfoPtr battery_info = DeviceBatteryInfo::New();
- if (left_bud_battery) {
- battery_info->left_bud_info = BatteryProperties::New();
- battery_info->left_bud_info->battery_percentage =
- left_bud_battery.value();
- }
- if (case_battery) {
- battery_info->case_info = BatteryProperties::New();
- battery_info->case_info->battery_percentage = case_battery.value();
- }
- if (right_bud_battery) {
- battery_info->right_bud_info = BatteryProperties::New();
- battery_info->right_bud_info->battery_percentage =
- right_bud_battery.value();
- }
- return battery_info;
- }
- void ExpectBluetoothDetailedViewFocused() {
- EXPECT_TRUE(tray_view()->detailed_view());
- const FeaturePodIconButton::Views& children =
- tray_view()->detailed_view()->children();
- EXPECT_EQ(1u, children.size());
- EXPECT_STREQ("BluetoothDetailedViewImpl", children.at(0)->GetClassName());
- }
- void LockScreen() {
- bluetooth_config_test_helper()->session_manager()->SessionStarted();
- bluetooth_config_test_helper()->session_manager()->SetSessionState(
- session_manager::SessionState::LOCKED);
- base::RunLoop().RunUntilIdle();
- }
- void PressIcon() {
- bluetooth_pod_controller_->OnIconPressed();
- base::RunLoop().RunUntilIdle();
- }
- void PressLabel() {
- bluetooth_pod_controller_->OnLabelPressed();
- base::RunLoop().RunUntilIdle();
- }
- void SetConnectedDevice(
- const PairedBluetoothDevicePropertiesPtr& connected_device) {
- std::vector<PairedBluetoothDevicePropertiesPtr> paired_devices;
- paired_devices.push_back(mojo::Clone(connected_device));
- SetPairedDevices(std::move(paired_devices));
- }
- void SetPairedDevices(
- std::vector<PairedBluetoothDevicePropertiesPtr> paired_devices) {
- fake_device_cache()->SetPairedDevices(std::move(paired_devices));
- base::RunLoop().RunUntilIdle();
- }
- void SetSystemState(BluetoothSystemState system_state) {
- bluetooth_config_test_helper()
- ->fake_adapter_state_controller()
- ->SetSystemState(system_state);
- base::RunLoop().RunUntilIdle();
- }
- const gfx::VectorIcon* feature_pod_icon_button_icon() {
- return feature_pod_button_->icon_button_->icon_;
- }
- const ash::FeaturePodLabelButton* feature_pod_label_button() {
- return feature_pod_button_->label_button_;
- }
- chromeos::bluetooth_config::FakeDeviceCache* fake_device_cache() {
- return bluetooth_config_test_helper()->fake_device_cache();
- }
- UnifiedSystemTrayController* tray_controller() {
- return GetPrimaryUnifiedSystemTray()
- ->bubble()
- ->unified_system_tray_controller();
- }
- UnifiedSystemTrayView* tray_view() {
- return GetPrimaryUnifiedSystemTray()->bubble()->unified_view();
- }
- protected:
- std::unique_ptr<FeaturePodButton> feature_pod_button_;
- private:
- ScopedBluetoothConfigTestHelper* bluetooth_config_test_helper() {
- return ash_test_helper()->bluetooth_config_test_helper();
- }
- std::unique_ptr<BluetoothFeaturePodController> bluetooth_pod_controller_;
- base::test::ScopedFeatureList feature_list_;
- };
- TEST_F(BluetoothFeaturePodControllerTest,
- HasCorrectButtonStateWhenBluetoothStateChanges) {
- SetSystemState(BluetoothSystemState::kUnavailable);
- EXPECT_FALSE(feature_pod_button_->GetEnabled());
- EXPECT_FALSE(feature_pod_button_->GetVisible());
- for (const auto& system_state :
- {BluetoothSystemState::kDisabled, BluetoothSystemState::kDisabling}) {
- SetSystemState(system_state);
- EXPECT_FALSE(feature_pod_button_->IsToggled());
- EXPECT_TRUE(feature_pod_button_->GetVisible());
- }
- for (const auto& system_state :
- {BluetoothSystemState::kEnabled, BluetoothSystemState::kEnabling}) {
- SetSystemState(system_state);
- EXPECT_TRUE(feature_pod_button_->IsToggled());
- EXPECT_TRUE(feature_pod_button_->GetVisible());
- }
- }
- TEST_F(BluetoothFeaturePodControllerTest, PressingIconOrLabelChangesBluetooth) {
- EXPECT_TRUE(feature_pod_button_->IsToggled());
- PressIcon();
- EXPECT_FALSE(feature_pod_button_->IsToggled());
- PressLabel();
- EXPECT_TRUE(feature_pod_button_->IsToggled());
- }
- TEST_F(BluetoothFeaturePodControllerTest, HasCorrectMetadataWhenOff) {
- SetSystemState(BluetoothSystemState::kDisabled);
- EXPECT_FALSE(feature_pod_button_->IsToggled());
- EXPECT_TRUE(feature_pod_button_->GetVisible());
- const ash::FeaturePodLabelButton* label_button = feature_pod_label_button();
- EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH),
- label_button->GetLabelText());
- EXPECT_EQ(
- l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH_DISABLED_SHORT),
- label_button->GetSubLabelText());
- EXPECT_EQ(l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_TOGGLE_TOOLTIP,
- l10n_util::GetStringUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_DISABLED_TOOLTIP)),
- label_button->GetTooltipText());
- const ash::FeaturePodIconButton* icon_button =
- feature_pod_button_->icon_button();
- EXPECT_STREQ(kUnifiedMenuBluetoothDisabledIcon.name,
- feature_pod_icon_button_icon()->name);
- EXPECT_EQ(l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_TOGGLE_TOOLTIP,
- l10n_util::GetStringUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_DISABLED_TOOLTIP)),
- icon_button->GetTooltipText());
- }
- TEST_F(BluetoothFeaturePodControllerTest, HasCorrectMetadataWithZeroDevices) {
- SetSystemState(BluetoothSystemState::kEnabled);
- const ash::FeaturePodLabelButton* label_button = feature_pod_label_button();
- EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH),
- label_button->GetLabelText());
- EXPECT_EQ(
- l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH_ENABLED_SHORT),
- label_button->GetSubLabelText());
- EXPECT_EQ(l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_SETTINGS_TOOLTIP,
- l10n_util::GetStringUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_ENABLED_TOOLTIP)),
- label_button->GetTooltipText());
- const ash::FeaturePodIconButton* icon_button =
- feature_pod_button_->icon_button();
- EXPECT_STREQ(kUnifiedMenuBluetoothIcon.name,
- feature_pod_icon_button_icon()->name);
- EXPECT_EQ(l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_TOGGLE_TOOLTIP,
- l10n_util::GetStringUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_ENABLED_TOOLTIP)),
- icon_button->GetTooltipText());
- }
- TEST_F(BluetoothFeaturePodControllerTest, HasCorrectMetadataWithOneDevice) {
- SetSystemState(BluetoothSystemState::kEnabled);
- const std::u16string public_name = base::ASCIIToUTF16(kDevicePublicName);
- // Create a device with the minimal configuration, mark it as connected, and
- // reset the list of paired devices to only contain it.
- auto paired_device = PairedBluetoothDeviceProperties::New();
- paired_device->device_properties = BluetoothDeviceProperties::New();
- paired_device->device_properties->public_name = public_name;
- paired_device->device_properties->connection_state =
- DeviceConnectionState::kConnected;
- SetConnectedDevice(paired_device);
- const ash::FeaturePodLabelButton* label_button = feature_pod_label_button();
- EXPECT_EQ(public_name, label_button->GetLabelText());
- EXPECT_EQ(l10n_util::GetStringUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_CONNECTED_LABEL),
- label_button->GetSubLabelText());
- EXPECT_EQ(l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_SETTINGS_TOOLTIP,
- l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_CONNECTED_TOOLTIP,
- public_name)),
- label_button->GetTooltipText());
- const ash::FeaturePodIconButton* icon_button =
- feature_pod_button_->icon_button();
- EXPECT_STREQ(kUnifiedMenuBluetoothConnectedIcon.name,
- feature_pod_icon_button_icon()->name);
- EXPECT_EQ(l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_TOGGLE_TOOLTIP,
- l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_CONNECTED_TOOLTIP,
- public_name)),
- icon_button->GetTooltipText());
- // Change the device nickname and reset the paired device list.
- paired_device->nickname = kDeviceNickname;
- SetConnectedDevice(paired_device);
- EXPECT_EQ(base::ASCIIToUTF16(kDeviceNickname), label_button->GetLabelText());
- // Change the device battery information and reset the paired device list.
- paired_device->device_properties->battery_info = CreateDefaultBatteryInfo();
- SetConnectedDevice(paired_device);
- EXPECT_EQ(l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_BATTERY_PERCENTAGE_LABEL,
- base::NumberToString16(kBatteryPercentage)),
- label_button->GetSubLabelText());
- }
- TEST_F(BluetoothFeaturePodControllerTest,
- HasCorrectMetadataWithOneDevice_MultipleBatteries) {
- SetSystemState(BluetoothSystemState::kEnabled);
- const std::u16string public_name = base::ASCIIToUTF16(kDevicePublicName);
- auto paired_device = PairedBluetoothDeviceProperties::New();
- paired_device->device_properties = BluetoothDeviceProperties::New();
- paired_device->device_properties->public_name = public_name;
- paired_device->device_properties->connection_state =
- DeviceConnectionState::kConnected;
- paired_device->device_properties->battery_info =
- CreateMultipleBatteryInfo(/*left_bud_battery=*/kLeftBudBatteryPercentage,
- /*case_battery=*/kCaseBatteryPercentage,
- /*right_battery=*/kRightBudBatteryPercentage);
- SetConnectedDevice(paired_device);
- const ash::FeaturePodLabelButton* label_button = feature_pod_label_button();
- EXPECT_EQ(l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_BATTERY_PERCENTAGE_LABEL,
- base::NumberToString16(kLeftBudBatteryPercentage)),
- label_button->GetSubLabelText());
- paired_device->device_properties->battery_info =
- CreateMultipleBatteryInfo(/*left_bud_battery=*/absl::nullopt,
- /*case_battery=*/kCaseBatteryPercentage,
- /*right_battery=*/kRightBudBatteryPercentage);
- SetConnectedDevice(paired_device);
- EXPECT_EQ(l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_BATTERY_PERCENTAGE_LABEL,
- base::NumberToString16(kRightBudBatteryPercentage)),
- label_button->GetSubLabelText());
- paired_device->device_properties->battery_info = CreateMultipleBatteryInfo(
- /*left_bud_battery=*/absl::nullopt,
- /*case_battery=*/kCaseBatteryPercentage, /*right_battery=*/absl::nullopt);
- SetConnectedDevice(paired_device);
- EXPECT_EQ(l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_BATTERY_PERCENTAGE_LABEL,
- base::NumberToString16(kCaseBatteryPercentage)),
- label_button->GetSubLabelText());
- }
- TEST_F(BluetoothFeaturePodControllerTest,
- HasCorrectMetadataWithMultipleDevice) {
- SetSystemState(BluetoothSystemState::kEnabled);
- // Create a device with basic battery information, mark it as connected, and
- // reset the list of paired devices with multiple duplicates of it.
- auto paired_device = PairedBluetoothDeviceProperties::New();
- paired_device->device_properties = BluetoothDeviceProperties::New();
- paired_device->device_properties->connection_state =
- DeviceConnectionState::kConnected;
- paired_device->device_properties->battery_info = CreateDefaultBatteryInfo();
- std::vector<PairedBluetoothDevicePropertiesPtr> paired_devices;
- for (int i = 0; i < kMultipleDeviceCount; ++i) {
- paired_devices.push_back(mojo::Clone(paired_device));
- }
- SetPairedDevices(std::move(paired_devices));
- const ash::FeaturePodLabelButton* label_button = feature_pod_label_button();
- EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH),
- label_button->GetLabelText());
- EXPECT_EQ(l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_MULTIPLE_DEVICES_CONNECTED_LABEL,
- base::FormatNumber(kMultipleDeviceCount)),
- label_button->GetSubLabelText());
- EXPECT_EQ(
- l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_SETTINGS_TOOLTIP,
- l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_MULTIPLE_DEVICES_CONNECTED_TOOLTIP,
- base::FormatNumber(kMultipleDeviceCount))),
- label_button->GetTooltipText());
- const ash::FeaturePodIconButton* icon_button =
- feature_pod_button_->icon_button();
- EXPECT_STREQ(kUnifiedMenuBluetoothConnectedIcon.name,
- feature_pod_icon_button_icon()->name);
- EXPECT_EQ(
- l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_TOGGLE_TOOLTIP,
- l10n_util::GetStringFUTF16(
- IDS_ASH_STATUS_TRAY_BLUETOOTH_MULTIPLE_DEVICES_CONNECTED_TOOLTIP,
- base::FormatNumber(kMultipleDeviceCount))),
- icon_button->GetTooltipText());
- }
- TEST_F(BluetoothFeaturePodControllerTest,
- EnablingBluetoothShowsBluetoothDetailedView) {
- SetSystemState(BluetoothSystemState::kDisabled);
- EXPECT_FALSE(feature_pod_button_->IsToggled());
- PressIcon();
- EXPECT_TRUE(feature_pod_button_->IsToggled());
- ExpectBluetoothDetailedViewFocused();
- }
- TEST_F(BluetoothFeaturePodControllerTest,
- PressingLabelWithEnabledBluetoothShowsBluetoothDetailedView) {
- EXPECT_TRUE(feature_pod_button_->IsToggled());
- PressLabel();
- ExpectBluetoothDetailedViewFocused();
- }
- TEST_F(BluetoothFeaturePodControllerTest,
- FeaturePodIsDisabledWhenBluetoothCannotBeModified) {
- EXPECT_TRUE(feature_pod_button_->GetEnabled());
- // The lock screen is one of multiple session states where Bluetooth cannot be
- // modified. For more information see
- // chromeos::bluetooth_config::SystemPropertiesProvider.
- LockScreen();
- EXPECT_FALSE(feature_pod_button_->GetEnabled());
- }
- } // namespace ash
|