File: notification_schedule_service_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (251 lines) | stat: -rw-r--r-- 8,992 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
// 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 <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/notifications/scheduler/notification_background_task_scheduler_impl.h"
#include "chrome/browser/notifications/scheduler/public/display_agent.h"
#include "chrome/browser/notifications/scheduler/public/features.h"
#include "chrome/browser/notifications/scheduler/public/notification_params.h"
#include "chrome/browser/notifications/scheduler/public/notification_schedule_service.h"
#include "chrome/browser/notifications/scheduler/public/notification_scheduler_client.h"
#include "chrome/browser/notifications/scheduler/public/notification_scheduler_client_registrar.h"
#include "chrome/browser/notifications/scheduler/schedule_service_factory_helper.h"
#include "chrome/browser/notifications/scheduler/test/mock_notification_background_task_scheduler.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_test.h"

using ::testing::_;

namespace notifications {
namespace {

const base::FilePath::CharType kTestDir[] =
    FILE_PATH_LITERAL("NotificationScheduleServiceTest");

class TestClient : public NotificationSchedulerClient {
 public:
  TestClient() = default;
  TestClient(const TestClient&) = delete;
  TestClient& operator=(const TestClient&) = delete;
  ~TestClient() override = default;

  const std::vector<NotificationData>& shown_notification_data() const {
    return shown_notification_data_;
  }

 private:
  // NotificationSchedulerClient implementation.
  void BeforeShowNotification(
      std::unique_ptr<NotificationData> notification_data,
      NotificationDataCallback callback) override {
    if (notification_data)
      shown_notification_data_.emplace_back(*notification_data);
    std::move(callback).Run(std::move(notification_data));
  }

  void OnSchedulerInitialized(bool success,
                              std::set<std::string> guids) override {
    DCHECK(success);
  }

  void OnUserAction(const UserActionData& action_data) override {}

  void GetThrottleConfig(ThrottleConfigCallback callback) override {
    std::move(callback).Run(nullptr);
  }

  // Any NotificationData received before showing the notification.
  std::vector<NotificationData> shown_notification_data_;
};

class TestBackgroundTaskScheduler : public NotificationBackgroundTaskScheduler {
 public:
  TestBackgroundTaskScheduler() = default;
  TestBackgroundTaskScheduler(const TestBackgroundTaskScheduler&) = delete;
  TestBackgroundTaskScheduler& operator=(const TestBackgroundTaskScheduler&) =
      delete;
  ~TestBackgroundTaskScheduler() override = default;

  // Waits until a background task has been updated.
  void WaitForTaskUpdated() {
    DCHECK(!run_loop_);
    run_loop_ = std::make_unique<base::RunLoop>();
    run_loop_->Run();
  }

  test::MockNotificationBackgroundTaskScheduler* mock_background_task() {
    return &mock_background_task_;
  }

 private:
  void QuitRunLoopIfNeeded() {
    if (run_loop_ && run_loop_->running()) {
      run_loop_->Quit();
    }
  }

  // NotificationBackgroundTaskScheduler implementation.
  void Schedule(base::TimeDelta window_start,
                base::TimeDelta window_end) override {
    QuitRunLoopIfNeeded();
    mock_background_task_.Schedule(window_start, window_end);
  }

  void Cancel() override {
    QuitRunLoopIfNeeded();
    mock_background_task_.Cancel();
  }

  std::unique_ptr<base::RunLoop> run_loop_;

  // Delegates to a mock to setup call expectations.
  test::MockNotificationBackgroundTaskScheduler mock_background_task_;
};

// Browser test for notification scheduling system. Uses real database
// instances. Mainly to cover service initialization flow in chrome layer.
class NotificationScheduleServiceTest : public InProcessBrowserTest {
 public:
  NotificationScheduleServiceTest() : task_scheduler_(nullptr) {
    scoped_feature_list_.InitWithFeatures(
        {features::kNotificationScheduleService}, {});
  }
  NotificationScheduleServiceTest(const NotificationScheduleServiceTest&) =
      delete;
  NotificationScheduleServiceTest& operator=(
      const NotificationScheduleServiceTest&) = delete;

  ~NotificationScheduleServiceTest() override = default;

 protected:
  void SetUpOnMainThread() override {
    InProcessBrowserTest::SetUpOnMainThread();
    ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir());
  }

  void TearDownOnMainThread() override {
    InProcessBrowserTest::TearDownOnMainThread();
    ASSERT_TRUE(tmp_dir_.Delete());
  }

  // Initializes |service_|. Injects database test data before this call.
  void Init() {
    auto* profile = browser()->profile();
    auto client = std::make_unique<TestClient>();
    clients_[SchedulerClientType::kTest1] = client.get();
    auto client_registrar =
        std::make_unique<NotificationSchedulerClientRegistrar>();
    client_registrar->RegisterClient(SchedulerClientType::kTest1,
                                     std::move(client));

    auto display_agent = notifications::DisplayAgent::Create();
    auto background_task_scheduler =
        std::make_unique<TestBackgroundTaskScheduler>();
    task_scheduler_ = background_task_scheduler.get();
    auto* db_provider =
        profile->GetDefaultStoragePartition()->GetProtoDatabaseProvider();
    service_ = CreateNotificationScheduleService(
        std::move(client_registrar), std::move(background_task_scheduler),
        std::move(display_agent), db_provider,
        tmp_dir_.GetPath().Append(kTestDir), profile->IsOffTheRecord());
  }

  // Helper function to schedule a notification immediately to show.
  void ScheduleNotification() {
    ScheduleParams schedule_params;
    schedule_params.deliver_time_start = base::Time::Now();
    schedule_params.deliver_time_end = base::Time::Now() + base::Minutes(5);
    NotificationData data;
    data.title = u"title";
    data.message = u"message";
    auto params = std::make_unique<notifications::NotificationParams>(
        notifications::SchedulerClientType::kTest1, std::move(data),
        std::move(schedule_params));
    schedule_service()->Schedule(std::move(params));
  }

  void RunBackgroundTask() {
    base::RunLoop loop;
    using TaskFinishedCallback =
        NotificationBackgroundTaskScheduler::Handler::TaskFinishedCallback;
    TaskFinishedCallback task_finish_callback =
        base::BindOnce([](base::RepeatingClosure quit_closure,
                          bool needs_reschedule) { quit_closure.Run(); },
                       loop.QuitClosure());
    schedule_service()->GetBackgroundTaskSchedulerHandler()->OnStartTask(
        std::move(task_finish_callback));
    loop.Run();
  }

  NotificationScheduleService* schedule_service() {
    return static_cast<NotificationScheduleService*>(service_.get());
  }

  TestBackgroundTaskScheduler* task_scheduler() {
    DCHECK(task_scheduler_);
    return task_scheduler_;
  }

  TestClient* client(SchedulerClientType type) {
    auto it = clients_.find(type);
    return it == clients_.end() ? nullptr : clients_[type];
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
  base::ScopedTempDir tmp_dir_;
  std::unique_ptr<KeyedService> service_;
  raw_ptr<TestBackgroundTaskScheduler> task_scheduler_;
  std::map<SchedulerClientType, raw_ptr<TestClient, CtnExperimental>> clients_;
};

// Test to schedule a notification.
IN_PROC_BROWSER_TEST_F(NotificationScheduleServiceTest, ScheduleNotification) {
  Init();
  EXPECT_CALL(*task_scheduler()->mock_background_task(), Schedule(_, _))
      .Times(1);
  ScheduleNotification();
  task_scheduler()->WaitForTaskUpdated();
}

// Test to run a background task to show a notification.
IN_PROC_BROWSER_TEST_F(NotificationScheduleServiceTest, ShowNotification) {
  Init();
  EXPECT_CALL(*task_scheduler()->mock_background_task(), Schedule(_, _))
      .Times(1)
      .RetiresOnSaturation();

  // Schedule one notification.
  ScheduleNotification();
  task_scheduler()->WaitForTaskUpdated();

  // Trigger a background task. Expected to show one notification, and no
  // background task will be scheduled since there is no notification entry in
  // the database.
  EXPECT_CALL(*task_scheduler()->mock_background_task(), Cancel())
      .Times(1)
      .RetiresOnSaturation();
  EXPECT_CALL(*task_scheduler()->mock_background_task(), Schedule(_, _))
      .Times(0);
  RunBackgroundTask();
  EXPECT_EQ(
      1u,
      client(SchedulerClientType::kTest1)->shown_notification_data().size());
}

}  // namespace
}  // namespace notifications