do_not_disturb_controller_impl_unittest.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/components/phonehub/do_not_disturb_controller_impl.h"
  5. #include <memory>
  6. #include "ash/components/phonehub/fake_message_sender.h"
  7. #include "ash/components/phonehub/fake_user_action_recorder.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace ash {
  10. namespace phonehub {
  11. namespace {
  12. class FakeObserver : public DoNotDisturbController::Observer {
  13. public:
  14. FakeObserver() = default;
  15. ~FakeObserver() override = default;
  16. size_t num_calls() const { return num_calls_; }
  17. // DoNotDisturbController::Observer:
  18. void OnDndStateChanged() override { ++num_calls_; }
  19. private:
  20. size_t num_calls_ = 0;
  21. };
  22. } // namespace
  23. class DoNotDisturbControllerImplTest : public testing::Test {
  24. protected:
  25. DoNotDisturbControllerImplTest() = default;
  26. DoNotDisturbControllerImplTest(const DoNotDisturbControllerImplTest&) =
  27. delete;
  28. DoNotDisturbControllerImplTest& operator=(
  29. const DoNotDisturbControllerImplTest&) = delete;
  30. ~DoNotDisturbControllerImplTest() override = default;
  31. // testing::Test:
  32. void SetUp() override {
  33. controller_ = std::make_unique<DoNotDisturbControllerImpl>(
  34. &fake_message_sender_, &fake_user_action_recorder_);
  35. controller_->AddObserver(&fake_observer_);
  36. }
  37. void TearDown() override { controller_->RemoveObserver(&fake_observer_); }
  38. bool IsDndEnabled() const { return controller_->IsDndEnabled(); }
  39. bool CanRequestNewDndState() const {
  40. return controller_->CanRequestNewDndState();
  41. }
  42. void SetDoNotDisturbInternal(bool is_dnd_enabled,
  43. bool can_request_new_dnd_state) {
  44. controller_->SetDoNotDisturbStateInternal(is_dnd_enabled,
  45. can_request_new_dnd_state);
  46. }
  47. void RequestNewDoNotDisturbState(bool enabled) {
  48. controller_->RequestNewDoNotDisturbState(enabled);
  49. }
  50. bool GetRecentUpdateNotificationModeRequest() {
  51. return fake_message_sender_.GetRecentUpdateNotificationModeRequest();
  52. }
  53. size_t GetUpdateNotificationModeRequestCallCount() {
  54. return fake_message_sender_.GetUpdateNotificationModeRequestCallCount();
  55. }
  56. size_t GetNumObserverCalls() const { return fake_observer_.num_calls(); }
  57. FakeUserActionRecorder fake_user_action_recorder_;
  58. private:
  59. FakeObserver fake_observer_;
  60. FakeMessageSender fake_message_sender_;
  61. std::unique_ptr<DoNotDisturbControllerImpl> controller_;
  62. };
  63. TEST_F(DoNotDisturbControllerImplTest, SetInternalStatesWithObservers) {
  64. EXPECT_FALSE(IsDndEnabled());
  65. SetDoNotDisturbInternal(/*is_dnd_enabled=*/true,
  66. /*can_request_new_dnd_state=*/true);
  67. EXPECT_TRUE(IsDndEnabled());
  68. EXPECT_TRUE(CanRequestNewDndState());
  69. EXPECT_EQ(1u, GetNumObserverCalls());
  70. SetDoNotDisturbInternal(/*is_dnd_enabled=*/false,
  71. /*can_request_new_dnd_state=*/true);
  72. EXPECT_FALSE(IsDndEnabled());
  73. EXPECT_TRUE(CanRequestNewDndState());
  74. EXPECT_EQ(2u, GetNumObserverCalls());
  75. // Setting internal dnd state with the same previous state will not trigger an
  76. // observer event.
  77. SetDoNotDisturbInternal(/*is_dnd_enabled=*/false,
  78. /*can_request_new_dnd_state=*/true);
  79. EXPECT_FALSE(IsDndEnabled());
  80. EXPECT_TRUE(CanRequestNewDndState());
  81. EXPECT_EQ(2u, GetNumObserverCalls());
  82. // Changing |can_request_new_dnd_state| should also trigger an observer event.
  83. SetDoNotDisturbInternal(/*is_dnd_enabled=*/false,
  84. /*can_request_new_dnd_state=*/false);
  85. EXPECT_FALSE(IsDndEnabled());
  86. EXPECT_FALSE(CanRequestNewDndState());
  87. EXPECT_EQ(3u, GetNumObserverCalls());
  88. // No new changes, expect no observer call.
  89. SetDoNotDisturbInternal(/*is_dnd_enabled=*/false,
  90. /*can_request_new_dnd_state=*/false);
  91. EXPECT_FALSE(IsDndEnabled());
  92. EXPECT_FALSE(CanRequestNewDndState());
  93. EXPECT_EQ(3u, GetNumObserverCalls());
  94. // Both states are changed, expect an observer call.
  95. SetDoNotDisturbInternal(/*is_dnd_enabled=*/true,
  96. /*can_request_new_dnd_state=*/true);
  97. EXPECT_TRUE(IsDndEnabled());
  98. EXPECT_TRUE(CanRequestNewDndState());
  99. EXPECT_EQ(4u, GetNumObserverCalls());
  100. }
  101. TEST_F(DoNotDisturbControllerImplTest, RequestNewDoNotDisturbState) {
  102. RequestNewDoNotDisturbState(/*enabled=*/true);
  103. EXPECT_EQ(1u, fake_user_action_recorder_.num_dnd_attempts());
  104. EXPECT_TRUE(GetRecentUpdateNotificationModeRequest());
  105. EXPECT_EQ(1u, GetUpdateNotificationModeRequestCallCount());
  106. // Simulate receiving a response and setting the internal value.
  107. SetDoNotDisturbInternal(/*is_dnd_enabled=*/true,
  108. /*can_request_new_dnd_state=*/true);
  109. RequestNewDoNotDisturbState(/*enabled=*/false);
  110. EXPECT_EQ(2u, fake_user_action_recorder_.num_dnd_attempts());
  111. EXPECT_FALSE(GetRecentUpdateNotificationModeRequest());
  112. EXPECT_EQ(2u, GetUpdateNotificationModeRequestCallCount());
  113. // Simulate receiving a response and setting the internal value.
  114. SetDoNotDisturbInternal(/*is_dnd_enabled=*/false,
  115. /*can_request_new_dnd_state=*/true);
  116. // Requesting for a the same state as the currently set state is a no-op.
  117. RequestNewDoNotDisturbState(/*enabled=*/false);
  118. EXPECT_EQ(2u, fake_user_action_recorder_.num_dnd_attempts());
  119. EXPECT_FALSE(GetRecentUpdateNotificationModeRequest());
  120. EXPECT_EQ(2u, GetUpdateNotificationModeRequestCallCount());
  121. }
  122. } // namespace phonehub
  123. } // namespace ash