silence_phone_quick_action_controller_unittest.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/silence_phone_quick_action_controller.h"
  5. #include "ash/components/phonehub/fake_do_not_disturb_controller.h"
  6. #include "ash/test/ash_test_base.h"
  7. namespace ash {
  8. class SilencePhoneQuickActionControllerTest : public AshTestBase {
  9. public:
  10. SilencePhoneQuickActionControllerTest() = default;
  11. ~SilencePhoneQuickActionControllerTest() override = default;
  12. // AshTestBase:
  13. void SetUp() override {
  14. AshTestBase::SetUp();
  15. dnd_controller_ = std::make_unique<phonehub::FakeDoNotDisturbController>();
  16. controller_ = std::make_unique<SilencePhoneQuickActionController>(
  17. dnd_controller_.get());
  18. item_ = base::WrapUnique(controller_->CreateItem());
  19. }
  20. void TearDown() override {
  21. item_.reset();
  22. controller_.reset();
  23. dnd_controller_.reset();
  24. AshTestBase::TearDown();
  25. }
  26. protected:
  27. SilencePhoneQuickActionController* controller() { return controller_.get(); }
  28. phonehub::FakeDoNotDisturbController* dnd_controller() {
  29. return dnd_controller_.get();
  30. }
  31. bool IsButtonDisabled() {
  32. return SilencePhoneQuickActionController::ActionState::kDisabled ==
  33. controller_->GetItemState();
  34. }
  35. private:
  36. std::unique_ptr<QuickActionItem> item_;
  37. std::unique_ptr<SilencePhoneQuickActionController> controller_;
  38. std::unique_ptr<phonehub::FakeDoNotDisturbController> dnd_controller_;
  39. };
  40. TEST_F(SilencePhoneQuickActionControllerTest, ItemStateChanged) {
  41. // Set request to fail to avoid triggering state's changes by the model.
  42. dnd_controller()->SetShouldRequestFail(true);
  43. // Allow the button to be enabled.
  44. dnd_controller()->SetDoNotDisturbStateInternal(
  45. /*is_dnd_enabled=*/false, /*can_request_new_dnd_state=*/true);
  46. // Press the button to enabled state will trigger observer.
  47. controller()->OnButtonPressed(false /* is_now_enabled */);
  48. // Item state changed to enabled.
  49. EXPECT_TRUE(controller()->IsItemEnabled());
  50. // Press the button to disabled state will trigger observer.
  51. controller()->OnButtonPressed(true /* is_now_enabled */);
  52. // Item state changed to disabled.
  53. EXPECT_FALSE(controller()->IsItemEnabled());
  54. dnd_controller()->SetShouldRequestFail(false);
  55. }
  56. TEST_F(SilencePhoneQuickActionControllerTest, CanRequestNewDndState) {
  57. // Set DoNotDisturbState to not allow any new request.
  58. dnd_controller()->SetDoNotDisturbStateInternal(
  59. /*is_dnd_enabled=*/false, /*can_request_new_dnd_state=*/false);
  60. // Since no new state requests are allowed, expect the button to be disabled.
  61. EXPECT_FALSE(controller()->IsItemEnabled());
  62. EXPECT_TRUE(IsButtonDisabled());
  63. // Simulate that the phone updated its DoNotDisturb state, but is still in a
  64. // work profile.
  65. dnd_controller()->SetDoNotDisturbStateInternal(
  66. /*is_dnd_enabled=*/true, /*can_request_new_dnd_state=*/false);
  67. // The button should still be disabled despite the phone has DoNotDisturb mode
  68. // enabled. However, the underlying toggle has been flipped to enabled.
  69. EXPECT_TRUE(controller()->IsItemEnabled());
  70. EXPECT_TRUE(IsButtonDisabled());
  71. // Flip toggle state back to enabled, but still in a work profile.
  72. dnd_controller()->SetDoNotDisturbStateInternal(
  73. /*is_dnd_enabled=*/false, /*can_request_new_dnd_state=*/false);
  74. EXPECT_FALSE(controller()->IsItemEnabled());
  75. EXPECT_TRUE(IsButtonDisabled());
  76. }
  77. } // namespace ash