File: notification_scheduler_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (437 lines) | stat: -rw-r--r-- 16,746 bytes parent folder | download | duplicates (7)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/notifications/scheduler/internal/notification_scheduler.h"

#include <map>
#include <memory>
#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/task_environment.h"
#include "chrome/browser/notifications/scheduler/internal/notification_entry.h"
#include "chrome/browser/notifications/scheduler/internal/notification_scheduler_context.h"
#include "chrome/browser/notifications/scheduler/internal/scheduler_config.h"
#include "chrome/browser/notifications/scheduler/public/notification_scheduler_client_registrar.h"
#include "chrome/browser/notifications/scheduler/public/notification_scheduler_types.h"
#include "chrome/browser/notifications/scheduler/test/mock_background_task_coordinator.h"
#include "chrome/browser/notifications/scheduler/test/mock_display_agent.h"
#include "chrome/browser/notifications/scheduler/test/mock_display_decider.h"
#include "chrome/browser/notifications/scheduler/test/mock_impression_history_tracker.h"
#include "chrome/browser/notifications/scheduler/test/mock_notification_background_task_scheduler.h"
#include "chrome/browser/notifications/scheduler/test/mock_notification_scheduler_client.h"
#include "chrome/browser/notifications/scheduler/test/mock_scheduled_notification_manager.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::_;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::NiceMock;
using ::testing::SetArgPointee;

namespace notifications {
namespace {

const char kGuid[] = "guid";
const char16_t kTitle[] = u"title";

class NotificationSchedulerTest : public testing::Test {
 public:
  NotificationSchedulerTest()
      : registrar_(nullptr),
        impression_tracker_(nullptr),
        notification_manager_(nullptr),
        client_(nullptr),
        task_coordinator_(nullptr),
        display_agent_(nullptr),
        display_decider_(nullptr) {}
  NotificationSchedulerTest(const NotificationSchedulerTest&) = delete;
  NotificationSchedulerTest& operator=(const NotificationSchedulerTest&) =
      delete;
  ~NotificationSchedulerTest() override = default;

  void SetUp() override {
    auto registrar = std::make_unique<NotificationSchedulerClientRegistrar>();
    auto task_coordinator =
        std::make_unique<test::MockBackgroundTaskCoordinator>();
    auto impression_tracker =
        std::make_unique<NiceMock<test::MockImpressionHistoryTracker>>();
    auto notification_manager =
        std::make_unique<NiceMock<test::MockScheduledNotificationManager>>();
    auto display_agent = std::make_unique<test::MockDisplayAgent>();
    auto display_decider = std::make_unique<test::MockDisplayDecider>();
    auto config = SchedulerConfig::Create();

    registrar_ = registrar.get();
    impression_tracker_ = impression_tracker.get();
    notification_manager_ = notification_manager.get();
    task_coordinator_ = task_coordinator.get();
    display_agent_ = display_agent.get();
    display_decider_ = display_decider.get();

    // Register mock clients.
    auto client = std::make_unique<test::MockNotificationSchedulerClient>();
    client_ = client.get();
    registrar_->RegisterClient(SchedulerClientType::kTest1, std::move(client));

    auto context = std::make_unique<NotificationSchedulerContext>(
        std::move(registrar), std::move(task_coordinator),
        std::move(impression_tracker), std::move(notification_manager),
        std::move(display_agent), std::move(display_decider),
        std::move(config));
    notification_scheduler_ = NotificationScheduler::Create(std::move(context));
  }

 protected:
  void Init() {
    EXPECT_CALL(*impression_tracker(), Init(_, _))
        .WillOnce(Invoke([&](ImpressionHistoryTracker::Delegate* delegate,
                             ImpressionHistoryTracker::InitCallback callback) {
          std::move(callback).Run(true);
        }));

    EXPECT_CALL(*notification_manager(), Init(_))
        .WillOnce(
            Invoke([&](ScheduledNotificationManager::InitCallback callback) {
              std::move(callback).Run(true);
            }));

    base::RunLoop run_loop;
    scheduler()->Init(
        base::BindOnce([](bool success) { EXPECT_TRUE(success); }));

    EXPECT_CALL(*client(), OnSchedulerInitialized(true, _))
        .WillOnce(InvokeWithoutArgs([&]() { run_loop.Quit(); }));

    run_loop.Run();
  }

  // Starts the background task and wait for task finished callback to invoke.
  void OnStartTask() {
    base::RunLoop loop;
    auto task_finish_callback =
        base::BindOnce([](base::RepeatingClosure quit_closure,
                          bool needs_reschedule) { quit_closure.Run(); },
                       loop.QuitClosure());
    scheduler()->OnStartTask(std::move(task_finish_callback));
    loop.Run();
  }

  NotificationScheduler* scheduler() { return notification_scheduler_.get(); }

  test::MockImpressionHistoryTracker* impression_tracker() {
    return impression_tracker_;
  }

  test::MockScheduledNotificationManager* notification_manager() {
    return notification_manager_;
  }

  test::MockNotificationSchedulerClient* client() { return client_; }

  test::MockBackgroundTaskCoordinator* task_coordinator() {
    return task_coordinator_;
  }

  test::MockDisplayAgent* display_agent() { return display_agent_; }

  test::MockDisplayDecider* display_decider() { return display_decider_; }

 private:
  base::test::TaskEnvironment task_environment_;
  raw_ptr<NotificationSchedulerClientRegistrar, DanglingUntriaged> registrar_;
  raw_ptr<test::MockImpressionHistoryTracker, DanglingUntriaged>
      impression_tracker_;
  raw_ptr<test::MockScheduledNotificationManager, DanglingUntriaged>
      notification_manager_;
  raw_ptr<test::MockNotificationSchedulerClient, DanglingUntriaged> client_;
  raw_ptr<test::MockBackgroundTaskCoordinator, DanglingUntriaged>
      task_coordinator_;
  raw_ptr<test::MockDisplayAgent, DanglingUntriaged> display_agent_;
  raw_ptr<test::MockDisplayDecider, DanglingUntriaged> display_decider_;

  std::unique_ptr<NotificationScheduler> notification_scheduler_;
};

// Tests successful initialization flow.
TEST_F(NotificationSchedulerTest, InitSuccess) {
  Init();
}

// Tests the case when impression tracker failed to initialize.
TEST_F(NotificationSchedulerTest, InitImpressionTrackerFailed) {
  EXPECT_CALL(*impression_tracker(), Init(_, _))
      .WillOnce(Invoke([](ImpressionHistoryTracker::Delegate* delegate,
                          ImpressionHistoryTracker::InitCallback callback) {
        // Impression tracker failed to load.
        std::move(callback).Run(false);
      }));

  EXPECT_CALL(*notification_manager(), Init(_)).Times(0);

  base::RunLoop run_loop;
  scheduler()->Init(
      base::BindOnce([](bool success) { EXPECT_FALSE(success); }));

  EXPECT_CALL(*client(), OnSchedulerInitialized(false, _))
      .WillOnce(InvokeWithoutArgs([&]() { run_loop.Quit(); }));

  run_loop.Run();
}

// Tests the case when scheduled notification manager failed to initialize.
TEST_F(NotificationSchedulerTest, InitScheduledNotificationManagerFailed) {
  EXPECT_CALL(*impression_tracker(), Init(_, _))
      .WillOnce(Invoke([](ImpressionHistoryTracker::Delegate* delegate,
                          ImpressionHistoryTracker::InitCallback callback) {
        std::move(callback).Run(true);
      }));

  EXPECT_CALL(*notification_manager(), Init(_))
      .WillOnce(Invoke([](ScheduledNotificationManager::InitCallback callback) {
        // Scheduled notification manager failed to load.
        std::move(callback).Run(false);
      }));

  base::RunLoop run_loop;
  scheduler()->Init(
      base::BindOnce([](bool success) { EXPECT_FALSE(success); }));

  EXPECT_CALL(*client(), OnSchedulerInitialized(false, _))
      .WillOnce(InvokeWithoutArgs([&]() { run_loop.Quit(); }));

  run_loop.Run();
}

// Test to schedule a notification.
TEST_F(NotificationSchedulerTest, Schedule) {
  Init();
  auto param = std::unique_ptr<NotificationParams>();
  EXPECT_CALL(*notification_manager(), ScheduleNotification(_, _))
      .WillOnce(
          Invoke([](std::unique_ptr<NotificationParams>,
                    ScheduledNotificationManager::ScheduleCallback callback) {
            std::move(callback).Run(true);
          }));
  EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _));
  scheduler()->Schedule(std::move(param));
}

// When failed to add to the scheduled notification manager, no background task
// is triggered.
TEST_F(NotificationSchedulerTest, ScheduleFailed) {
  Init();
  auto param = std::unique_ptr<NotificationParams>();
  EXPECT_CALL(*notification_manager(), ScheduleNotification(_, _))
      .WillOnce(
          Invoke([](std::unique_ptr<NotificationParams>,
                    ScheduledNotificationManager::ScheduleCallback callback) {
            std::move(callback).Run(false);
          }));
  EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _)).Times(0);
  scheduler()->Schedule(std::move(param));
}

// Test to delete notifications.
TEST_F(NotificationSchedulerTest, DeleteAllNotifications) {
  Init();

  // Currently we don't reschedule background task even if all the notifications
  // are deleted.
  EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _)).Times(0);
  EXPECT_CALL(*notification_manager(),
              DeleteNotifications(SchedulerClientType::kTest1));
  scheduler()->DeleteAllNotifications(SchedulerClientType::kTest1);
}

// Test to get client overview.
TEST_F(NotificationSchedulerTest, GetClientOverview) {
  Init();
  EXPECT_CALL(*impression_tracker(),
              GetImpressionDetail(SchedulerClientType::kTest1, _))
      .WillOnce(Invoke([](SchedulerClientType type,
                          ImpressionDetail::ImpressionDetailCallback callback) {
        std::move(callback).Run(ImpressionDetail());
      }));
  EXPECT_CALL(*notification_manager(), GetNotifications(_, _));
  scheduler()->GetClientOverview(SchedulerClientType::kTest1,
                                 base::DoNothing());
}

// Test to verify user actions are propagated through correctly.
TEST_F(NotificationSchedulerTest, OnUserAction) {
  Init();

  base::RunLoop loop;
  UserActionData action_data(SchedulerClientType::kTest1,
                             UserActionType::kButtonClick, kGuid);
  EXPECT_CALL(*impression_tracker(), OnUserAction(action_data));
  EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _)).Times(1);
  EXPECT_CALL(*client(), OnUserAction(_)).WillOnce(InvokeWithoutArgs([&]() {
    loop.Quit();
  }));

  scheduler()->OnUserAction(action_data);
  loop.Run();
}

// Test to simulate a background task flow without any notification shown.
TEST_F(NotificationSchedulerTest, BackgroundTaskStartShowNothing) {
  Init();

  // No notification picked to show.
  DisplayDecider::Results result;
  EXPECT_CALL(*display_decider(), FindNotificationsToShow(_, _, _))
      .WillOnce(SetArgPointee<2>(result));

  EXPECT_CALL(*display_agent(), ShowNotification(_, _)).Times(0);
  EXPECT_CALL(*notification_manager(), DisplayNotification(_, _)).Times(0);
  EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _));

  OnStartTask();
}

MATCHER_P(NotificationDataEq, title, "Verify notification data.") {
  EXPECT_EQ(arg->title, title);
  return true;
}

MATCHER_P2(SystemDataEq, type, guid, "Verify system data.") {
  EXPECT_EQ(arg->type, type);
  EXPECT_EQ(arg->guid, guid);
  return true;
}

// Test to simulate a background task flow with some notifications shown.
TEST_F(NotificationSchedulerTest, BackgroundTaskStartShowNotification) {
  Init();

  // Mock the notification to show.
  auto entry =
      std::make_unique<NotificationEntry>(SchedulerClientType::kTest1, kGuid);
  EXPECT_CALL(
      *display_agent(),
      ShowNotification(NotificationDataEq(kTitle),
                       SystemDataEq(SchedulerClientType::kTest1, kGuid)));
  DisplayDecider::Results result({kGuid});
  EXPECT_CALL(*display_decider(), FindNotificationsToShow(_, _, _))
      .WillOnce(SetArgPointee<2>(result));

  EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _));
  EXPECT_CALL(*impression_tracker(), AddImpression(_, _, _, _, _));
  EXPECT_CALL(*notification_manager(), DisplayNotification(_, _))
      .WillOnce(
          Invoke([&](const std::string& guid,
                     ScheduledNotificationManager::DisplayCallback callback) {
            std::move(callback).Run(std::move(entry));
          }));

  EXPECT_CALL(*client(), BeforeShowNotification(_, _))
      .WillOnce(Invoke(
          [&](std::unique_ptr<NotificationData> notification_data,
              NotificationSchedulerClient::NotificationDataCallback callback) {
            // The client updates the notification data here.
            notification_data->title = kTitle;
            std::move(callback).Run(std::move(notification_data));
          }));

  OnStartTask();
}

// Verifies if the entry is failed to load, the background task flow can still
// be finished.
TEST_F(NotificationSchedulerTest, BackgroundTaskStartNoEntry) {
  Init();

  DisplayDecider::Results result({kGuid});
  EXPECT_CALL(*display_decider(), FindNotificationsToShow(_, _, _))
      .WillOnce(SetArgPointee<2>(result));

  EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _));
  EXPECT_CALL(*impression_tracker(), AddImpression(_, _, _, _, _)).Times(0);
  EXPECT_CALL(*display_agent(), ShowNotification(_, _)).Times(0);
  EXPECT_CALL(*client(), BeforeShowNotification(_, _)).Times(0);
  EXPECT_CALL(*notification_manager(), DisplayNotification(_, _))
      .WillOnce(
          Invoke([&](const std::string& guid,
                     ScheduledNotificationManager::DisplayCallback callback) {
            std::move(callback).Run(nullptr /*entry*/);
          }));

  OnStartTask();
}

// Verifies if the client is not found during display flow, the background task
// flow can still be finished.
TEST_F(NotificationSchedulerTest, BackgroundTaskStartNoClient) {
  Init();

  // Creates entry without corresponding client.
  auto entry_no_client =
      std::make_unique<NotificationEntry>(SchedulerClientType::kTest2, kGuid);

  DisplayDecider::Results result({kGuid});
  EXPECT_CALL(*display_decider(), FindNotificationsToShow(_, _, _))
      .WillOnce(SetArgPointee<2>(result));

  EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _));
  EXPECT_CALL(*impression_tracker(), AddImpression(_, _, _, _, _)).Times(0);
  EXPECT_CALL(*display_agent(), ShowNotification(_, _)).Times(0);
  EXPECT_CALL(*client(), BeforeShowNotification(_, _)).Times(0);
  EXPECT_CALL(*notification_manager(), DisplayNotification(_, _))
      .WillOnce(
          Invoke([&](const std::string& guid,
                     ScheduledNotificationManager::DisplayCallback callback) {
            std::move(callback).Run(std::move(entry_no_client));
          }));

  OnStartTask();
}

// Verifies the case that the client dropped the notification data.
TEST_F(NotificationSchedulerTest, ClientDropNotification) {
  Init();

  // Mock the notification to show.
  auto entry =
      std::make_unique<NotificationEntry>(SchedulerClientType::kTest1, kGuid);
  DisplayDecider::Results result({kGuid});
  EXPECT_CALL(*display_decider(), FindNotificationsToShow(_, _, _))
      .WillOnce(SetArgPointee<2>(result));
  EXPECT_CALL(*notification_manager(), DisplayNotification(_, _))
      .WillOnce(
          Invoke([&](const std::string& guid,
                     ScheduledNotificationManager::DisplayCallback callback) {
            std::move(callback).Run(std::move(entry));
          }));

  // The client drops the notification data before showing the notification.
  EXPECT_CALL(*client(), BeforeShowNotification(_, _))
      .WillOnce(Invoke(
          [&](std::unique_ptr<NotificationData> notification_data,
              NotificationSchedulerClient::NotificationDataCallback callback) {
            std::move(callback).Run(nullptr);
          }));

  EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _));
  EXPECT_CALL(*impression_tracker(), AddImpression(_, _, _, _, _)).Times(0);
  EXPECT_CALL(*display_agent(), ShowNotification(_, _)).Times(0);

  OnStartTask();
}

// Test to simulate a background task stopped by the OS.
TEST_F(NotificationSchedulerTest, BackgroundTaskStop) {
  Init();
  EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _));
  scheduler()->OnStopTask();
}

}  // namespace
}  // namespace notifications