| // Copyright 2020 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/phonehub/phone_status_view.h" |
| |
| #include "ash/constants/ash_features.h" |
| #include "ash/test/ash_test_base.h" |
| #include "base/optional.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "base/test/scoped_feature_list.h" |
| #include "chromeos/components/phonehub/mutable_phone_model.h" |
| #include "ui/views/controls/image_view.h" |
| #include "ui/views/controls/label.h" |
| #include "ui/views/test/button_test_api.h" |
| |
| namespace ash { |
| |
| using PhoneStatusModel = chromeos::phonehub::PhoneStatusModel; |
| |
| class DummyEvent : public ui::Event { |
| public: |
| DummyEvent() : Event(ui::ET_UNKNOWN, base::TimeTicks(), 0) {} |
| ~DummyEvent() override = default; |
| }; |
| |
| class PhoneStatusViewTest : public AshTestBase, |
| public PhoneStatusView::Delegate { |
| public: |
| PhoneStatusViewTest() = default; |
| ~PhoneStatusViewTest() override = default; |
| |
| // AshTestBase: |
| void SetUp() override { |
| feature_list_.InitAndEnableFeature(chromeos::features::kPhoneHub); |
| AshTestBase::SetUp(); |
| |
| status_view_ = std::make_unique<PhoneStatusView>(&phone_model_, this); |
| } |
| |
| void TearDown() override { |
| status_view_.reset(); |
| AshTestBase::TearDown(); |
| } |
| |
| // PhoneStatusView::Delegate: |
| bool CanOpenConnectedDeviceSettings() override { |
| return can_open_connected_device_settings_; |
| } |
| |
| void OpenConnectedDevicesSettings() override { |
| connected_device_settings_opened_ = true; |
| } |
| |
| protected: |
| std::unique_ptr<PhoneStatusView> status_view_; |
| chromeos::phonehub::MutablePhoneModel phone_model_; |
| base::test::ScopedFeatureList feature_list_; |
| bool can_open_connected_device_settings_ = false; |
| bool connected_device_settings_opened_ = false; |
| }; |
| |
| TEST_F(PhoneStatusViewTest, PhoneStatusLabelsContent) { |
| std::u16string expected_name_text = base::UTF8ToUTF16("Test Phone Name"); |
| std::u16string expected_provider_text = base::UTF8ToUTF16("Test Provider"); |
| std::u16string expected_battery_text = base::UTF8ToUTF16("10%"); |
| |
| phone_model_.SetPhoneName(expected_name_text); |
| |
| PhoneStatusModel::MobileConnectionMetadata metadata = { |
| .signal_strength = PhoneStatusModel::SignalStrength::kZeroBars, |
| .mobile_provider = expected_provider_text, |
| }; |
| auto phone_status = |
| PhoneStatusModel(PhoneStatusModel::MobileStatus::kSimWithReception, |
| metadata, PhoneStatusModel::ChargingState::kNotCharging, |
| PhoneStatusModel::BatterySaverState::kOff, 10); |
| phone_model_.SetPhoneStatusModel(phone_status); |
| |
| // All labels should display phone's status and information. |
| EXPECT_EQ(expected_name_text, status_view_->phone_name_label_->GetText()); |
| EXPECT_EQ(expected_battery_text, status_view_->battery_label_->GetText()); |
| |
| expected_name_text = base::UTF8ToUTF16("New Phone Name"); |
| expected_provider_text = base::UTF8ToUTF16("New Provider"); |
| expected_battery_text = base::UTF8ToUTF16("20%"); |
| |
| phone_model_.SetPhoneName(expected_name_text); |
| metadata.mobile_provider = expected_provider_text; |
| phone_status = |
| PhoneStatusModel(PhoneStatusModel::MobileStatus::kSimWithReception, |
| metadata, PhoneStatusModel::ChargingState::kNotCharging, |
| PhoneStatusModel::BatterySaverState::kOff, 20); |
| phone_model_.SetPhoneStatusModel(phone_status); |
| |
| // Changes in the model should be reflected in the labels. |
| EXPECT_EQ(expected_name_text, status_view_->phone_name_label_->GetText()); |
| EXPECT_EQ(expected_battery_text, status_view_->battery_label_->GetText()); |
| |
| // Simulate phone disconnected with a null |PhoneStatusModel| returned. |
| phone_model_.SetPhoneStatusModel(base::nullopt); |
| |
| // Existing phone status will be cleared to reflect the model change. |
| EXPECT_TRUE(status_view_->battery_label_->GetText().empty()); |
| EXPECT_TRUE(status_view_->battery_icon_->GetImage().isNull()); |
| EXPECT_TRUE(status_view_->signal_icon_->GetImage().isNull()); |
| } |
| |
| TEST_F(PhoneStatusViewTest, ClickOnSettings) { |
| // The settings button is not visible if we can't open the settings. |
| EXPECT_FALSE(status_view_->settings_button_->GetVisible()); |
| |
| // The settings button is visible if we can open settings. |
| can_open_connected_device_settings_ = true; |
| status_view_ = std::make_unique<PhoneStatusView>(&phone_model_, this); |
| EXPECT_TRUE(status_view_->settings_button_->GetVisible()); |
| |
| // Click on the settings button. |
| views::test::ButtonTestApi(status_view_->settings_button_) |
| .NotifyClick(DummyEvent()); |
| EXPECT_TRUE(connected_device_settings_opened_); |
| } |
| |
| } // namespace ash |