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