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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
ash / system / do_not_disturb_notification_controller_unittest.cc [blame]
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/do_not_disturb_notification_controller.h"
#include <string>
#include "ash/constants/ash_features.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/focus_mode/focus_mode_controller.h"
#include "ash/system/focus_mode/focus_mode_util.h"
#include "ash/test/ash_test_base.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
namespace ash {
namespace {
using message_center::MessageCenter;
std::u16string GetDoNotDisturbDescription() {
return l10n_util::GetStringUTF16(
IDS_ASH_DO_NOT_DISTURB_NOTIFICATION_DESCRIPTION);
}
message_center::Notification* GetDoNotDisturbNotification() {
return MessageCenter::Get()->FindNotificationById(
DoNotDisturbNotificationController::kDoNotDisturbNotificationId);
}
} // namespace
class DoNotDisturbNotificationControllerTest : public AshTestBase {
public:
DoNotDisturbNotificationControllerTest() = default;
DoNotDisturbNotificationControllerTest(
const DoNotDisturbNotificationControllerTest&) = delete;
DoNotDisturbNotificationControllerTest& operator=(
const DoNotDisturbNotificationControllerTest&) = delete;
~DoNotDisturbNotificationControllerTest() override = default;
};
// Tests that enabling/disabling Do not disturb mode adds/removes the Do not
// disturb notification.
TEST_F(DoNotDisturbNotificationControllerTest, AddRemoveNotification) {
auto* message_center = MessageCenter::Get();
ASSERT_FALSE(GetDoNotDisturbNotification());
// Turn on Do not disturb mode.
message_center->SetQuietMode(true);
auto* notification = GetDoNotDisturbNotification();
EXPECT_TRUE(notification);
EXPECT_EQ(notification->message(), GetDoNotDisturbDescription());
// Turn off Do not disturb mode.
message_center->SetQuietMode(false);
EXPECT_FALSE(GetDoNotDisturbNotification());
}
// Tests that clicking the notification's "Turn off" button turns off Do not
// disturb mode and dismisses the Do not disturb notification.
TEST_F(DoNotDisturbNotificationControllerTest,
NotificationButtonTurnsOffDoNotDisturbMode) {
// Show the notification by turning on Do not disturb mode.
auto* message_center = MessageCenter::Get();
message_center->SetQuietMode(true);
auto* notification = GetDoNotDisturbNotification();
ASSERT_TRUE(notification);
ASSERT_EQ(notification->message(), GetDoNotDisturbDescription());
// Simulate a click on the notification's "Turn off" button.
notification->delegate()->Click(0, std::nullopt);
EXPECT_FALSE(GetDoNotDisturbNotification());
EXPECT_FALSE(message_center->IsQuietMode());
}
class DoNotDisturbNotificationControllerWithFocusModeTest : public AshTestBase {
public:
DoNotDisturbNotificationControllerWithFocusModeTest()
: scoped_feature_list_(features::kFocusMode) {}
~DoNotDisturbNotificationControllerWithFocusModeTest() override = default;
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
// Tests if the correct notification id shows up when the system DND is on
// before starting a focus session.
TEST_F(DoNotDisturbNotificationControllerWithFocusModeTest,
CheckNotificationTypesIfSystemDNDIsOnInitially) {
auto* message_center = MessageCenter::Get();
auto* focus_mode_controller = FocusModeController::Get();
// Turn on system DND mode before starting a session.
message_center->SetQuietMode(true);
auto* notification = GetDoNotDisturbNotification();
ASSERT_TRUE(notification);
EXPECT_EQ(notification->message(), GetDoNotDisturbDescription());
// Start a focus session where `turn_on_do_not_disturb` is defaulted to
// `true`.
EXPECT_TRUE(focus_mode_controller->turn_on_do_not_disturb());
focus_mode_controller->ToggleFocusMode();
EXPECT_TRUE(focus_mode_controller->in_focus_session());
notification = GetDoNotDisturbNotification();
EXPECT_TRUE(notification);
EXPECT_EQ(notification->message(), GetDoNotDisturbDescription());
// Check the notification creation during the focus session. First, disable
// the DND mode in the focus session.
notification->delegate()->Click(0, std::nullopt);
EXPECT_FALSE(GetDoNotDisturbNotification());
EXPECT_FALSE(message_center->IsQuietMode());
// Second, enable the DND mode.
message_center->SetQuietMode(true);
notification = GetDoNotDisturbNotification();
EXPECT_TRUE(notification);
EXPECT_EQ(notification->message(), GetDoNotDisturbDescription());
// End the focus session, and the system DND state will be restored to `true`.
focus_mode_controller->ToggleFocusMode();
EXPECT_FALSE(focus_mode_controller->in_focus_session());
notification = GetDoNotDisturbNotification();
EXPECT_TRUE(notification);
EXPECT_EQ(notification->message(), GetDoNotDisturbDescription());
}
// Tests if the correct notification id shows up when the system DND is off
// before starting a focus session.
TEST_F(DoNotDisturbNotificationControllerWithFocusModeTest,
CheckNotificationTypesIfSystemDNDIsOffInitially) {
auto* focus_mode_controller = FocusModeController::Get();
auto* message_center = MessageCenter::Get();
// The system DND is off before starting a focus session.
ASSERT_FALSE(GetDoNotDisturbNotification());
EXPECT_FALSE(message_center->IsQuietMode());
EXPECT_NE(message_center::QuietModeSourceType::kFocusMode,
message_center->GetLastQuietModeChangeSourceType());
// Start a focus session where `turn_on_do_not_disturb` is defaulted to
// `true`.
EXPECT_TRUE(focus_mode_controller->turn_on_do_not_disturb());
focus_mode_controller->ToggleFocusMode();
EXPECT_TRUE(focus_mode_controller->in_focus_session());
auto* notification = GetDoNotDisturbNotification();
EXPECT_TRUE(notification);
const base::Time end_time = focus_mode_controller->GetActualEndTime();
EXPECT_EQ(
notification->message(),
focus_mode_util::GetNotificationDescriptionForFocusSession(end_time));
// Check that quiet mode is active, and it was triggered by focus mode.
EXPECT_TRUE(message_center->IsQuietMode());
EXPECT_EQ(message_center::QuietModeSourceType::kFocusMode,
message_center->GetLastQuietModeChangeSourceType());
// End the focus session, and the system DND state will be restored to
// `false`.
focus_mode_controller->ToggleFocusMode();
EXPECT_FALSE(focus_mode_controller->in_focus_session());
EXPECT_FALSE(GetDoNotDisturbNotification());
// Check that quiet mode is no longer active, and it was triggered by focus
// mode.
EXPECT_FALSE(message_center->IsQuietMode());
EXPECT_EQ(message_center::QuietModeSourceType::kFocusMode,
message_center->GetLastQuietModeChangeSourceType());
}
// Tests that if the focus session is extended by 10 minutes; correspondingly,
// the notification will show the updated end time after adding the 10 minutes.
TEST_F(DoNotDisturbNotificationControllerWithFocusModeTest,
CheckNotificationTextAfterExtendingFocusSession) {
auto* focus_mode_controller = FocusModeController::Get();
// The system DND is off before starting a focus session.
ASSERT_FALSE(GetDoNotDisturbNotification());
// Start a focus session where `turn_on_do_not_disturb` is defaulted to
// `true`.
EXPECT_TRUE(focus_mode_controller->turn_on_do_not_disturb());
focus_mode_controller->ToggleFocusMode();
EXPECT_TRUE(focus_mode_controller->in_focus_session());
const base::Time end_time1 = focus_mode_controller->GetActualEndTime();
auto* notification = GetDoNotDisturbNotification();
EXPECT_TRUE(notification);
EXPECT_EQ(
notification->message(),
focus_mode_util::GetNotificationDescriptionForFocusSession(end_time1));
// Extend the focus duration.
focus_mode_controller->ExtendSessionDuration();
const base::Time end_time2 = focus_mode_controller->GetActualEndTime();
EXPECT_EQ(end_time2 - end_time1, base::Minutes(10));
notification = GetDoNotDisturbNotification();
EXPECT_TRUE(notification);
EXPECT_EQ(
notification->message(),
focus_mode_util::GetNotificationDescriptionForFocusSession(end_time2));
// End the focus session, and the system DND state will be restored to
// `false`.
focus_mode_controller->ToggleFocusMode();
EXPECT_FALSE(focus_mode_controller->in_focus_session());
EXPECT_FALSE(GetDoNotDisturbNotification());
}
} // namespace ash