quick_actions_view_unittest.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/phonehub/quick_actions_view.h"
  5. #include "ash/components/phonehub/fake_phone_hub_manager.h"
  6. #include "ash/constants/ash_features.h"
  7. #include "ash/system/phonehub/quick_action_item.h"
  8. #include "ash/test/ash_test_base.h"
  9. #include "base/test/scoped_feature_list.h"
  10. #include "ui/events/test/test_event.h"
  11. #include "ui/views/test/button_test_api.h"
  12. namespace ash {
  13. namespace {
  14. using FindMyDeviceController = phonehub::FindMyDeviceController;
  15. using TetherController = phonehub::TetherController;
  16. constexpr base::TimeDelta kWaitForRequestTimeout = base::Seconds(10);
  17. } // namespace
  18. class QuickActionsViewTest : public AshTestBase {
  19. public:
  20. QuickActionsViewTest()
  21. : AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  22. ~QuickActionsViewTest() override = default;
  23. // AshTestBase:
  24. void SetUp() override {
  25. feature_list_.InitAndEnableFeature(chromeos::features::kPhoneHub);
  26. AshTestBase::SetUp();
  27. quick_actions_view_ =
  28. std::make_unique<QuickActionsView>(&phone_hub_manager_);
  29. }
  30. void TearDown() override {
  31. quick_actions_view_.reset();
  32. AshTestBase::TearDown();
  33. }
  34. protected:
  35. QuickActionsView* actions_view() { return quick_actions_view_.get(); }
  36. phonehub::FakeTetherController* tether_controller() {
  37. return phone_hub_manager_.fake_tether_controller();
  38. }
  39. phonehub::FakeDoNotDisturbController* dnd_controller() {
  40. return phone_hub_manager_.fake_do_not_disturb_controller();
  41. }
  42. phonehub::FakeFindMyDeviceController* find_my_device_controller() {
  43. return phone_hub_manager_.fake_find_my_device_controller();
  44. }
  45. private:
  46. std::unique_ptr<QuickActionsView> quick_actions_view_;
  47. phonehub::FakePhoneHubManager phone_hub_manager_;
  48. base::test::ScopedFeatureList feature_list_;
  49. };
  50. TEST_F(QuickActionsViewTest, EnableHotspotVisibility) {
  51. tether_controller()->SetStatus(
  52. TetherController::Status::kIneligibleForFeature);
  53. // Enable Hotspot button should not be shown if the feature is ineligible.
  54. EXPECT_FALSE(actions_view()->enable_hotspot_for_testing()->GetVisible());
  55. tether_controller()->SetStatus(
  56. TetherController::Status::kConnectionAvailable);
  57. // Enable Hotspot button should be shown if the feature is available.
  58. EXPECT_TRUE(actions_view()->enable_hotspot_for_testing()->GetVisible());
  59. }
  60. TEST_F(QuickActionsViewTest, EnableHotspotToggle) {
  61. tether_controller()->SetStatus(
  62. TetherController::Status::kConnectionAvailable);
  63. // Simulate a toggle press. Status should be connecting.
  64. views::test::ButtonTestApi test_api(
  65. actions_view()->enable_hotspot_for_testing()->icon_button());
  66. test_api.NotifyClick(ui::test::TestEvent());
  67. EXPECT_EQ(TetherController::Status::kConnecting,
  68. tether_controller()->GetStatus());
  69. tether_controller()->SetStatus(TetherController::Status::kConnected);
  70. // Toggling again will change the state.
  71. test_api.NotifyClick(ui::test::TestEvent());
  72. EXPECT_EQ(TetherController::Status::kConnecting,
  73. tether_controller()->GetStatus());
  74. }
  75. TEST_F(QuickActionsViewTest, SilencePhoneToggle) {
  76. // Allow silence phone to be toggle-able.
  77. dnd_controller()->SetDoNotDisturbStateInternal(
  78. /*is_dnd_enabled=*/false,
  79. /*can_request_new_dnd_state=*/true);
  80. // Initially, silence phone is not enabled.
  81. EXPECT_FALSE(dnd_controller()->IsDndEnabled());
  82. // Toggling the button will enable the feature.
  83. views::test::ButtonTestApi test_api(
  84. actions_view()->silence_phone_for_testing()->icon_button());
  85. test_api.NotifyClick(ui::test::TestEvent());
  86. EXPECT_TRUE(dnd_controller()->IsDndEnabled());
  87. // Toggle again to disable.
  88. test_api.NotifyClick(ui::test::TestEvent());
  89. EXPECT_FALSE(dnd_controller()->IsDndEnabled());
  90. // Test the error state.
  91. dnd_controller()->SetShouldRequestFail(true);
  92. test_api.NotifyClick(ui::test::TestEvent());
  93. // In error state, do not disturb is disabled but the button should still be
  94. // on after being pressed.
  95. EXPECT_FALSE(dnd_controller()->IsDndEnabled());
  96. EXPECT_TRUE(actions_view()->silence_phone_for_testing()->IsToggled());
  97. // After a certain time, the button should be corrected to be off.
  98. task_environment()->FastForwardBy(kWaitForRequestTimeout);
  99. EXPECT_FALSE(actions_view()->silence_phone_for_testing()->IsToggled());
  100. dnd_controller()->SetShouldRequestFail(false);
  101. }
  102. TEST_F(QuickActionsViewTest, LocatePhoneToggle) {
  103. // Initially, locate phone is not enabled.
  104. EXPECT_EQ(FindMyDeviceController::Status::kRingingOff,
  105. find_my_device_controller()->GetPhoneRingingStatus());
  106. // Toggling the button will enable the feature.
  107. views::test::ButtonTestApi test_api(
  108. actions_view()->locate_phone_for_testing()->icon_button());
  109. test_api.NotifyClick(ui::test::TestEvent());
  110. EXPECT_EQ(FindMyDeviceController::Status::kRingingOn,
  111. find_my_device_controller()->GetPhoneRingingStatus());
  112. // Toggle again to disable.
  113. test_api.NotifyClick(ui::test::TestEvent());
  114. EXPECT_EQ(FindMyDeviceController::Status::kRingingOff,
  115. find_my_device_controller()->GetPhoneRingingStatus());
  116. // Test the error state.
  117. find_my_device_controller()->SetShouldRequestFail(true);
  118. test_api.NotifyClick(ui::test::TestEvent());
  119. // In error state, find my device is disabled but the button should still be
  120. // on after being pressed.
  121. EXPECT_EQ(FindMyDeviceController::Status::kRingingOff,
  122. find_my_device_controller()->GetPhoneRingingStatus());
  123. EXPECT_TRUE(actions_view()->locate_phone_for_testing()->IsToggled());
  124. // After a certain time, the button should be corrected to be off.
  125. task_environment()->FastForwardBy(kWaitForRequestTimeout);
  126. EXPECT_FALSE(actions_view()->locate_phone_for_testing()->IsToggled());
  127. find_my_device_controller()->SetShouldRequestFail(false);
  128. }
  129. } // namespace ash