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
content / browser / background_sync / background_sync_scheduler_unittest.cc [blame]
// 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 "content/browser/background_sync/background_sync_scheduler.h"
#include <map>
#include <vector>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "content/browser/background_sync/background_sync_scheduler.h"
#include "content/browser/storage_partition_impl.h"
#include "content/common/content_export.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/content_client.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "content/test/mock_background_sync_controller.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace content {
namespace {
const char kUrl_1[] = "https://example.com";
const char kUrl_2[] = "https://whereswaldo.com";
class TestBrowserClient : public ContentBrowserClient {
public:
TestBrowserClient() = default;
~TestBrowserClient() override = default;
StoragePartitionConfig GetStoragePartitionConfigForSite(
BrowserContext* browser_context,
const GURL& site) override {
return content::StoragePartitionConfig::Create(
browser_context, "PartitionDomain" + site.spec(),
"Partition" + site.spec(), false /* in_memory */);
}
};
} // namespace
class BackgroundSyncSchedulerTest : public testing::Test {
public:
BackgroundSyncSchedulerTest()
: task_environment_(BrowserTaskEnvironment::MainThreadType::UI) {}
void ScheduleDelayedProcessing(const GURL& url,
blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay,
base::OnceClosure delayed_task) {
auto* scheduler = BackgroundSyncScheduler::GetFor(&test_browser_context_);
DCHECK(scheduler);
auto* storage_partition = static_cast<StoragePartitionImpl*>(
test_browser_context_.GetStoragePartitionForUrl(url));
DCHECK(storage_partition);
scheduler->ScheduleDelayedProcessing(storage_partition, sync_type, delay,
std::move(delayed_task));
}
void CancelDelayedProcessing(const GURL& url,
blink::mojom::BackgroundSyncType sync_type) {
auto* scheduler = BackgroundSyncScheduler::GetFor(&test_browser_context_);
DCHECK(scheduler);
auto* storage_partition = static_cast<StoragePartitionImpl*>(
test_browser_context_.GetStoragePartitionForUrl(url));
DCHECK(storage_partition);
scheduler->CancelDelayedProcessing(storage_partition, sync_type);
}
MockBackgroundSyncController* GetController() {
return static_cast<MockBackgroundSyncController*>(
test_browser_context_.GetBackgroundSyncController());
}
base::TimeDelta GetBrowserWakeupDelay(
blink::mojom::BackgroundSyncType sync_type) {
return GetController()->GetBrowserWakeupDelay(sync_type);
}
base::TimeTicks GetBrowserWakeupTime() {
auto* scheduler = BackgroundSyncScheduler::GetFor(&test_browser_context_);
DCHECK(scheduler);
return scheduler
->scheduled_wakeup_time_[blink::mojom::BackgroundSyncType::ONE_SHOT];
}
void SetUp() override {
original_client_ = SetBrowserClientForTesting(&browser_client_);
}
void TearDown() override { SetBrowserClientForTesting(original_client_); }
protected:
BrowserTaskEnvironment task_environment_;
TestBrowserClient browser_client_;
raw_ptr<ContentBrowserClient> original_client_;
TestBrowserContext test_browser_context_;
};
TEST_F(BackgroundSyncSchedulerTest, ScheduleInvokesCallback) {
base::RunLoop run_loop;
ScheduleDelayedProcessing(GURL(kUrl_1),
blink::mojom::BackgroundSyncType::ONE_SHOT,
base::Milliseconds(1), run_loop.QuitClosure());
run_loop.Run();
}
TEST_F(BackgroundSyncSchedulerTest, ZeroDelayScheduleDoesNotInvokeCallback) {
bool was_called = false;
ScheduleDelayedProcessing(
GURL(kUrl_1), blink::mojom::BackgroundSyncType::ONE_SHOT,
base::TimeDelta(),
base::BindOnce([](bool* was_called) { *was_called = true; },
&was_called));
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(was_called);
}
TEST_F(BackgroundSyncSchedulerTest, CancelDoesNotInvokeCallback) {
bool was_called = false;
ScheduleDelayedProcessing(
GURL(kUrl_1), blink::mojom::BackgroundSyncType::ONE_SHOT,
base::Minutes(1),
base::BindOnce([](bool* was_called) { *was_called = true; },
&was_called));
base::RunLoop().RunUntilIdle();
ASSERT_FALSE(was_called);
CancelDelayedProcessing(GURL(kUrl_1),
blink::mojom::BackgroundSyncType::ONE_SHOT);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(was_called);
}
TEST_F(BackgroundSyncSchedulerTest, SchedulingTwiceOverwritesTimer) {
bool was_called = false;
ScheduleDelayedProcessing(
GURL(kUrl_1), blink::mojom::BackgroundSyncType::ONE_SHOT,
base::Seconds(1),
base::BindOnce([](bool* was_called) { *was_called = true; },
&was_called));
base::RunLoop().RunUntilIdle();
ASSERT_FALSE(was_called);
base::RunLoop run_loop;
ScheduleDelayedProcessing(GURL(kUrl_1),
blink::mojom::BackgroundSyncType::ONE_SHOT,
base::Milliseconds(1), run_loop.QuitClosure());
run_loop.Run();
EXPECT_FALSE(was_called);
}
TEST_F(BackgroundSyncSchedulerTest, MultipleStoragePartitions) {
base::RunLoop run_loop_1, run_loop_2;
ScheduleDelayedProcessing(GURL(kUrl_1),
blink::mojom::BackgroundSyncType::ONE_SHOT,
base::Seconds(1), run_loop_1.QuitClosure());
ScheduleDelayedProcessing(GURL(kUrl_2),
blink::mojom::BackgroundSyncType::ONE_SHOT,
base::Milliseconds(1), run_loop_2.QuitClosure());
run_loop_1.Run();
run_loop_2.Run();
}
TEST_F(BackgroundSyncSchedulerTest, ScheduleBothTypesOfSync) {
base::RunLoop run_loop_1, run_loop_2;
ScheduleDelayedProcessing(GURL(kUrl_1),
blink::mojom::BackgroundSyncType::ONE_SHOT,
base::Milliseconds(1), run_loop_1.QuitClosure());
ScheduleDelayedProcessing(GURL(kUrl_1),
blink::mojom::BackgroundSyncType::PERIODIC,
base::Milliseconds(1), run_loop_2.QuitClosure());
run_loop_1.Run();
run_loop_2.Run();
}
#if BUILDFLAG(IS_ANDROID)
TEST_F(BackgroundSyncSchedulerTest, BrowserWakeupScheduled) {
ScheduleDelayedProcessing(GURL(kUrl_1),
blink::mojom::BackgroundSyncType::ONE_SHOT,
base::Seconds(1), base::DoNothing());
EXPECT_LE(GetBrowserWakeupDelay(blink::mojom::BackgroundSyncType::ONE_SHOT),
base::Seconds(1));
ScheduleDelayedProcessing(GURL(kUrl_2),
blink::mojom::BackgroundSyncType::ONE_SHOT,
base::Milliseconds(1), base::DoNothing());
EXPECT_LE(GetBrowserWakeupDelay(blink::mojom::BackgroundSyncType::ONE_SHOT),
base::Milliseconds(1));
}
TEST_F(BackgroundSyncSchedulerTest,
BrowserWakeupScheduleSecondAfterFirstFinishes) {
base::RunLoop run_loop_1;
ScheduleDelayedProcessing(GURL(kUrl_1),
blink::mojom::BackgroundSyncType::ONE_SHOT,
base::Milliseconds(1), run_loop_1.QuitClosure());
EXPECT_LE(GetBrowserWakeupDelay(blink::mojom::BackgroundSyncType::ONE_SHOT),
base::Milliseconds(1));
run_loop_1.Run();
ScheduleDelayedProcessing(GURL(kUrl_2),
blink::mojom::BackgroundSyncType::ONE_SHOT,
base::Minutes(1), base::DoNothing());
EXPECT_GT(GetBrowserWakeupDelay(blink::mojom::BackgroundSyncType::ONE_SHOT),
base::Milliseconds(1));
EXPECT_LE(GetBrowserWakeupDelay(blink::mojom::BackgroundSyncType::ONE_SHOT),
base::Minutes(1));
}
TEST_F(BackgroundSyncSchedulerTest, BrowserWakeupScheduleOneOfEachType) {
ScheduleDelayedProcessing(GURL(kUrl_1),
blink::mojom::BackgroundSyncType::PERIODIC,
base::Seconds(1), base::DoNothing());
base::RunLoop().RunUntilIdle();
EXPECT_LE(GetBrowserWakeupDelay(blink::mojom::BackgroundSyncType::PERIODIC),
base::Seconds(1));
ScheduleDelayedProcessing(GURL(kUrl_2),
blink::mojom::BackgroundSyncType::ONE_SHOT,
base::Minutes(1), base::DoNothing());
base::RunLoop().RunUntilIdle();
EXPECT_LE(GetBrowserWakeupDelay(blink::mojom::BackgroundSyncType::ONE_SHOT),
base::Minutes(1));
}
TEST_F(BackgroundSyncSchedulerTest, BrowserWakeupScheduleThenCancel) {
ScheduleDelayedProcessing(GURL(kUrl_1),
blink::mojom::BackgroundSyncType::PERIODIC,
base::Minutes(1), base::DoNothing());
base::RunLoop().RunUntilIdle();
EXPECT_LE(GetBrowserWakeupDelay(blink::mojom::BackgroundSyncType::PERIODIC),
base::Minutes(1));
CancelDelayedProcessing(GURL(kUrl_1),
blink::mojom::BackgroundSyncType::PERIODIC);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(GetBrowserWakeupDelay(blink::mojom::BackgroundSyncType::PERIODIC),
base::TimeDelta::Max());
}
TEST_F(BackgroundSyncSchedulerTest, CancelingOneTypeDoesNotAffectAnother) {
ScheduleDelayedProcessing(GURL(kUrl_1),
blink::mojom::BackgroundSyncType::PERIODIC,
base::Minutes(1), base::DoNothing());
ScheduleDelayedProcessing(GURL(kUrl_2),
blink::mojom::BackgroundSyncType::ONE_SHOT,
base::Seconds(1), base::DoNothing());
CancelDelayedProcessing(GURL(kUrl_1),
blink::mojom::BackgroundSyncType::PERIODIC);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(GetBrowserWakeupDelay(blink::mojom::BackgroundSyncType::PERIODIC),
base::TimeDelta::Max());
EXPECT_LE(GetBrowserWakeupDelay(blink::mojom::BackgroundSyncType::ONE_SHOT),
base::Seconds(1));
}
TEST_F(BackgroundSyncSchedulerTest,
CancelingProcessingForOneStorageParitionUpdatesBrowserWakeup) {
ScheduleDelayedProcessing(GURL(kUrl_1),
blink::mojom::BackgroundSyncType::ONE_SHOT,
base::Minutes(1), base::DoNothing());
ScheduleDelayedProcessing(GURL(kUrl_2),
blink::mojom::BackgroundSyncType::ONE_SHOT,
base::Seconds(1), base::DoNothing());
base::RunLoop().RunUntilIdle();
EXPECT_LE(GetBrowserWakeupDelay(blink::mojom::BackgroundSyncType::ONE_SHOT),
base::Seconds(1));
auto wakeup_time1 = GetBrowserWakeupTime();
CancelDelayedProcessing(GURL(kUrl_2),
blink::mojom::BackgroundSyncType::ONE_SHOT);
base::RunLoop().RunUntilIdle();
EXPECT_LE(GetBrowserWakeupDelay(blink::mojom::BackgroundSyncType::ONE_SHOT),
base::Minutes(1));
EXPECT_GT(GetBrowserWakeupDelay(blink::mojom::BackgroundSyncType::ONE_SHOT),
base::Seconds(1));
EXPECT_LT(wakeup_time1, GetBrowserWakeupTime());
}
#endif
} // namespace content