File: on_task_notifications_manager_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (333 lines) | stat: -rw-r--r-- 13,740 bytes parent folder | download | duplicates (6)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright 2024 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/boca/on_task/on_task_notifications_manager.h"

#include <atomic>
#include <memory>
#include <string>
#include <utility>

#include "ash/constants/notifier_catalogs.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "chromeos/ash/components/boca/on_task/notification_constants.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notifier_id.h"

using message_center::MessageCenter;
using message_center::Notification;
using message_center::NotifierId;
using ::testing::IsNull;
using ::testing::NotNull;

namespace ash::boca {
namespace {

constexpr char kTestNotificationId[] = "TestOnTaskNotification";
constexpr std::u16string_view kTestNotificationTitle =
    u"TestOnTaskNotificationTitle";
constexpr int kTestNotificationMessageId =
    IDS_ON_TASK_ENTER_LOCKED_MODE_NOTIFICATION_MESSAGE;
constexpr int kTestCountdownNotificationMessageId =
    IDS_ON_TASK_ENTER_LOCKED_MODE_COUNTDOWN_NOTIFICATION_MESSAGE;
constexpr std::u16string_view kToastDescription = u"TestDescription";
constexpr base::TimeDelta kNotificationCountdownPeriod = base::Seconds(5);
constexpr base::TimeDelta kToastCountdownPeriod = base::Seconds(5);

// Fake delegate implementation for the `OnTaskNotificationsManager`.
class FakeOnTaskNotificationsManagerDelegate
    : public OnTaskNotificationsManager::Delegate {
 public:
  FakeOnTaskNotificationsManagerDelegate() = default;
  ~FakeOnTaskNotificationsManagerDelegate() override = default;

  // OnTaskNotificationsManager::Delegate:
  void ShowToast(ToastData toast_data) override { ++toast_count_; }
  void ShowNotification(
      std::unique_ptr<message_center::Notification> notification) override {
    CHECK_NE(notification->fullscreen_visibility(),
             message_center::FullscreenVisibility::NONE);
    ++notification_count_;
  }
  void ClearNotification(const std::string& id) override {
    notification_count_ = 0;
  }

  size_t GetToastCount() { return toast_count_.load(); }
  size_t GetNotificationCount() { return notification_count_.load(); }

 private:
  std::atomic<size_t> toast_count_;
  std::atomic<size_t> notification_count_;
};

class OnTaskNotificationsManagerTest : public ::testing::Test {
 protected:
  void SetUp() override {
    auto fake_delegate =
        std::make_unique<FakeOnTaskNotificationsManagerDelegate>();
    fake_delegate_ptr_ = fake_delegate.get();
    notifications_manager_ =
        OnTaskNotificationsManager::CreateForTest(std::move(fake_delegate));
  }

  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  std::unique_ptr<OnTaskNotificationsManager> notifications_manager_;
  raw_ptr<FakeOnTaskNotificationsManagerDelegate> fake_delegate_ptr_;
};

TEST_F(OnTaskNotificationsManagerTest, CreateToastWithNoCountdownPeriod) {
  bool callback_triggered = false;
  OnTaskNotificationsManager::ToastCreateParams create_params(
      /*id=*/kTestNotificationId,
      /*catalog_name=*/ToastCatalogName::kMaxValue,
      /*text_description_callback=*/
      base::BindLambdaForTesting([](base::TimeDelta countdown_period) {
        return std::u16string(kToastDescription);
      }),
      /*completion_callback=*/base::BindLambdaForTesting([&]() {
        callback_triggered = true;
      }));
  notifications_manager_->CreateToast(std::move(create_params));

  // Verify toast is shown after a 1 second delay because of the scheduled
  // timer.
  task_environment_.FastForwardBy(kOnTaskNotificationCountdownInterval);
  ASSERT_EQ(fake_delegate_ptr_->GetToastCount(), 1u);
  ASSERT_FALSE(callback_triggered);

  // Advance timer by 1 more second and verify callback is triggered.
  task_environment_.FastForwardBy(kOnTaskNotificationCountdownInterval);
  EXPECT_TRUE(callback_triggered);
}

TEST_F(OnTaskNotificationsManagerTest,
       NewToastsWithSameIdOverridePreviousOnes) {
  bool callback_triggered_1 = false;
  OnTaskNotificationsManager::ToastCreateParams create_params_1(
      /*id=*/kTestNotificationId,
      /*catalog_name=*/ToastCatalogName::kMaxValue,
      /*text_description_callback=*/
      base::BindLambdaForTesting([](base::TimeDelta countdown_period) {
        return std::u16string(kToastDescription);
      }),
      /*completion_callback=*/base::BindLambdaForTesting([&]() {
        callback_triggered_1 = true;
      }),
      /*countdown_period=*/kToastCountdownPeriod);
  notifications_manager_->CreateToast(std::move(create_params_1));

  // Verify toast is shown after a 1 second delay.
  task_environment_.FastForwardBy(kOnTaskNotificationCountdownInterval);
  ASSERT_EQ(fake_delegate_ptr_->GetToastCount(), 1u);

  // Trigger another toast with the same id.
  bool callback_triggered_2 = false;
  OnTaskNotificationsManager::ToastCreateParams create_params_2(
      /*id=*/kTestNotificationId,
      /*catalog_name=*/ToastCatalogName::kMaxValue,
      /*text_description_callback=*/
      base::BindLambdaForTesting([](base::TimeDelta countdown_period) {
        return std::u16string(kToastDescription);
      }),
      /*completion_callback=*/base::BindLambdaForTesting([&]() {
        callback_triggered_2 = true;
      }),
      /*countdown_period=*/kToastCountdownPeriod);
  notifications_manager_->CreateToast(std::move(create_params_2));

  // Advance timer and verify the first toast is overridden by ensuring the
  // corresponding callback is not triggered.
  task_environment_.FastForwardBy(kToastCountdownPeriod +
                                  kToastCountdownPeriod);
  EXPECT_FALSE(callback_triggered_1);
  EXPECT_TRUE(callback_triggered_2);
}

TEST_F(OnTaskNotificationsManagerTest, StopProcessingToast) {
  OnTaskNotificationsManager::ToastCreateParams create_params(
      /*id=*/kTestNotificationId,
      /*catalog_name=*/ToastCatalogName::kMaxValue,
      /*text_description_callback=*/
      base::BindLambdaForTesting([](base::TimeDelta countdown_period) {
        return std::u16string(kToastDescription);
      }),
      /*completion_callback=*/base::DoNothing(),
      /*countdown_period=*/kToastCountdownPeriod);
  notifications_manager_->CreateToast(std::move(create_params));

  // Verify toast is shown after a 1 second delay.
  task_environment_.FastForwardBy(kOnTaskNotificationCountdownInterval);
  ASSERT_EQ(fake_delegate_ptr_->GetToastCount(), 1u);

  // Attempt to stop processing notification and verify toast is not shown with
  // subsequent timer advances.
  notifications_manager_->StopProcessingNotification(kTestNotificationId);
  task_environment_.FastForwardBy(kOnTaskNotificationCountdownInterval);
  EXPECT_EQ(fake_delegate_ptr_->GetToastCount(), 1u);
}

TEST_F(OnTaskNotificationsManagerTest,
       TriggerCompletionCallbackOnToastCountdownEnd) {
  bool callback_triggered = false;
  OnTaskNotificationsManager::ToastCreateParams create_params(
      /*id=*/kTestNotificationId,
      /*catalog_name=*/ToastCatalogName::kMaxValue,
      /*text_description_callback=*/
      base::BindLambdaForTesting([](base::TimeDelta countdown_period) {
        return std::u16string(kToastDescription);
      }),
      /*completion_callback=*/
      base::BindLambdaForTesting([&]() { callback_triggered = true; }),
      /*countdown_period=*/kToastCountdownPeriod);
  notifications_manager_->CreateToast(std::move(create_params));

  // Verify toast is shown after a 1 second delay.
  task_environment_.FastForwardBy(kOnTaskNotificationCountdownInterval);
  ASSERT_EQ(fake_delegate_ptr_->GetToastCount(), 1u);
  ASSERT_FALSE(callback_triggered);

  // Toasts remain visible before count down.
  task_environment_.FastForwardBy(kOnTaskNotificationCountdownInterval);
  ASSERT_EQ(fake_delegate_ptr_->GetToastCount(), 2u);
  ASSERT_FALSE(callback_triggered);

  // Verify callback is triggered after the countdown period.
  task_environment_.FastForwardBy(kToastCountdownPeriod);
  EXPECT_TRUE(callback_triggered);
}

TEST_F(OnTaskNotificationsManagerTest,
       CreateNotificationWithNoCountdownPeriod) {
  OnTaskNotificationsManager::NotificationCreateParams create_params(
      /*id=*/kTestNotificationId,
      /*title=*/std::u16string{kTestNotificationTitle},
      /*message_id=*/kTestNotificationMessageId,
      /*notifier_id=*/NotifierId());
  notifications_manager_->CreateNotification(std::move(create_params));

  task_environment_.FastForwardBy(kOnTaskNotificationCountdownInterval);
  EXPECT_EQ(fake_delegate_ptr_->GetNotificationCount(), 1u);
}

TEST_F(OnTaskNotificationsManagerTest,
       TriggerCompletionCallbackOnNotificationCountdownEnd) {
  int callback_counter = 0;
  OnTaskNotificationsManager::NotificationCreateParams create_params(
      /*id=*/kTestNotificationId,
      /*title=*/std::u16string{kTestNotificationTitle},
      /*message_id=*/kTestCountdownNotificationMessageId,
      /*notifier_id=*/NotifierId(),
      /*completion_callback=*/base::BindLambdaForTesting([&]() {
        ++callback_counter;
      }),
      /*countdown_period=*/kNotificationCountdownPeriod,
      /*is_counting_down=*/true);
  notifications_manager_->CreateNotification(std::move(create_params));

  // Verify notification is shown after a 1 second delay.
  task_environment_.FastForwardBy(kOnTaskNotificationCountdownInterval);
  ASSERT_EQ(fake_delegate_ptr_->GetNotificationCount(), 1u);
  EXPECT_EQ(callback_counter, 0);

  // Notifications remain visible before count down.
  task_environment_.FastForwardBy(kOnTaskNotificationCountdownInterval);
  ASSERT_EQ(fake_delegate_ptr_->GetNotificationCount(), 2u);
  EXPECT_EQ(callback_counter, 0);

  // Verify callback is triggered after the countdown period.
  task_environment_.FastForwardBy(kNotificationCountdownPeriod);
  EXPECT_EQ(callback_counter, 1);
  EXPECT_EQ(fake_delegate_ptr_->GetNotificationCount(), 0u);

  // Verify notification stops processing after the callback is triggered.
  task_environment_.FastForwardBy(kOnTaskNotificationCountdownInterval);
  EXPECT_EQ(callback_counter, 1);
}

TEST_F(OnTaskNotificationsManagerTest,
       NewNotificationsWithSameIdOverridePreviousOnes) {
  bool callback_triggered_1 = false;
  OnTaskNotificationsManager::NotificationCreateParams create_params_1(
      /*id=*/kTestNotificationId,
      /*title=*/std::u16string{kTestNotificationTitle},
      /*message_id=*/kTestCountdownNotificationMessageId,
      /*notifier_id=*/NotifierId(),
      /*completion_callback=*/base::BindLambdaForTesting([&]() {
        callback_triggered_1 = true;
      }),
      /*countdown_period=*/kNotificationCountdownPeriod,
      /*is_counting_down=*/true);
  notifications_manager_->CreateNotification(std::move(create_params_1));

  // Verify notification is shown after a 1 second delay.
  task_environment_.FastForwardBy(kOnTaskNotificationCountdownInterval);
  ASSERT_EQ(fake_delegate_ptr_->GetNotificationCount(), 1u);

  // Trigger another notification with the same id.
  bool callback_triggered_2 = false;
  OnTaskNotificationsManager::NotificationCreateParams create_params_2(
      /*id=*/kTestNotificationId,
      /*title=*/std::u16string{kTestNotificationTitle},
      /*message_id=*/kTestCountdownNotificationMessageId,
      /*notifier_id=*/NotifierId(),
      /*completion_callback=*/base::BindLambdaForTesting([&]() {
        callback_triggered_2 = true;
      }),
      /*countdown_period=*/kNotificationCountdownPeriod,
      /*is_counting_down=*/true);
  notifications_manager_->CreateNotification(std::move(create_params_2));

  // Advance timer and verify the first notification is overridden by ensuring
  // the corresponding callback is not triggered.
  task_environment_.FastForwardBy(kNotificationCountdownPeriod +
                                  kNotificationCountdownPeriod);
  EXPECT_FALSE(callback_triggered_1);
  EXPECT_TRUE(callback_triggered_2);
}

TEST_F(OnTaskNotificationsManagerTest, StopProcessingNotification) {
  OnTaskNotificationsManager::NotificationCreateParams create_params(
      /*id=*/kTestNotificationId,
      /*title=*/std::u16string{kTestNotificationTitle},
      /*message_id=*/kTestNotificationMessageId,
      /*notifier_id=*/NotifierId());
  notifications_manager_->CreateNotification(std::move(create_params));

  // Verify notification is shown after a 1 second delay.
  task_environment_.FastForwardBy(kOnTaskNotificationCountdownInterval);
  EXPECT_EQ(fake_delegate_ptr_->GetNotificationCount(), 1u);

  // Attempt to stop processing notification and verify notification is not
  // shown with subsequent timer advances.
  notifications_manager_->StopProcessingNotification(kTestNotificationId);
  task_environment_.FastForwardBy(kOnTaskNotificationCountdownInterval);
  EXPECT_EQ(fake_delegate_ptr_->GetNotificationCount(), 1u);
}

TEST_F(OnTaskNotificationsManagerTest, ConfigureForLockedWindow) {
  notifications_manager_->ConfigureForLockedMode(/*locked=*/true);
  EXPECT_THAT(notifications_manager_->GetNotificationBlockerForTesting(),
              NotNull());
}

TEST_F(OnTaskNotificationsManagerTest, ConfigureForUnlockedWindow) {
  notifications_manager_->ConfigureForLockedMode(/*locked=*/false);
  EXPECT_THAT(notifications_manager_->GetNotificationBlockerForTesting(),
              IsNull());
}

}  // namespace
}  // namespace ash::boca