forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_background_sync_manager.h
203 lines (167 loc) · 7.04 KB
/
test_background_sync_manager.h
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_TEST_TEST_BACKGROUND_SYNC_MANAGER_H_
#define CONTENT_TEST_TEST_BACKGROUND_SYNC_MANAGER_H_
#include <stdint.h>
#include <memory>
#include <string>
#include "base/callback_forward.h"
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "content/browser/background_sync/background_sync_manager.h"
#include "content/browser/service_worker/service_worker_storage.h"
namespace url {
class Origin;
} // namespace url
namespace content {
struct BackgroundSyncParameters;
class DevToolsBackgroundServicesContextImpl;
class ServiceWorkerContextWrapper;
class ServiceWorkerVersion;
// A BackgroundSyncManager for use in unit tests. This class provides control
// over internal behavior and state, to allow tests to simulate various
// scenarios. Examples include (but are not limited to):
// - Delaying and corrupting the backend storage.
// - Controlling the firing of service worker onsync events.
// - Setting the network state
class TestBackgroundSyncManager : public BackgroundSyncManager {
public:
using DispatchSyncCallback =
base::RepeatingCallback<void(scoped_refptr<ServiceWorkerVersion>,
ServiceWorkerVersion::StatusCallback)>;
TestBackgroundSyncManager(
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context,
scoped_refptr<DevToolsBackgroundServicesContextImpl> devtools_context);
~TestBackgroundSyncManager() override;
// Force a call to the internal Init() method.
void DoInit();
// Resume any delayed backend operation.
void ResumeBackendOperation();
// Clear the delayed task if it exists. Delayed tasks are tasks posted with a
// delay to trigger the BackgroundSyncManager to try firing its sync events
// again.
void ClearDelayedTask();
// Determine whether backend operations succeed or fail due to
// corruption.
void set_corrupt_backend(bool corrupt_backend) {
corrupt_backend_ = corrupt_backend;
}
// Delay backend operations until explicitly resumed by calling
// ResumeBackendOperation().
void set_delay_backend(bool delay_backend) { delay_backend_ = delay_backend; }
// Set a callback for when the sync event is dispatched, so tests can observe
// when the event happens.
void set_dispatch_sync_callback(const DispatchSyncCallback& callback) {
dispatch_sync_callback_ = callback;
}
// Set a callback for when the periodicSync event is dispatched, so tests can
// observe it.
void set_dispatch_periodic_sync_callback(
const DispatchSyncCallback& callback) {
dispatch_periodic_sync_callback_ = callback;
}
// Sets the response to checks for a main frame for register attempts.
void set_has_main_frame_provider_host(bool value) {
has_main_frame_provider_host_ = value;
}
bool IsDelayedTaskScheduledOneShotSync() const {
return !delayed_one_shot_sync_task_.callback().is_null();
}
bool IsDelayedTaskScheduledPeriodicSync() const {
return !delayed_periodic_sync_task_.callback().is_null();
}
void RunOneShotSyncDelayedTask() {
std::move(delayed_one_shot_sync_task_.callback()).Run();
}
void RunPeriodicSyncDelayedTask() {
std::move(delayed_periodic_sync_task_.callback()).Run();
}
// Accessors to internal state
base::TimeDelta delayed_one_shot_sync_task_delta() const {
return delayed_one_shot_sync_task_delta_;
}
base::TimeDelta delayed_periodic_sync_task_delta() const {
return delayed_periodic_sync_task_delta_;
}
bool last_chance() const { return last_chance_; }
const BackgroundSyncParameters* background_sync_parameters() const {
return parameters_.get();
}
bool IsBrowserWakeupForOneShotSyncScheduled() const {
return delayed_processing_scheduled_one_shot_sync_;
}
bool IsBrowserWakeupForPeriodicSyncScheduled() const {
return delayed_processing_scheduled_periodic_sync_;
}
bool EqualsSoonestOneShotWakeupDelta(base::TimeDelta compare_to) const {
return soonest_one_shot_sync_wakeup_delta_ == compare_to;
}
bool EqualsSoonestPeriodicSyncWakeupDelta(base::TimeDelta compare_to) const {
return soonest_periodic_sync_wakeup_delta_ == compare_to;
}
void DispatchPeriodicSyncEvent(
const std::string& tag,
scoped_refptr<ServiceWorkerVersion> active_version,
ServiceWorkerVersion::StatusCallback callback) override;
// Override to allow the test to cache the result.
base::TimeDelta GetSoonestWakeupDelta(
blink::mojom::BackgroundSyncType sync_type,
base::Time last_browser_wakeup_for_periodic_sync) override;
protected:
// Override to allow delays to be injected by tests.
void StoreDataInBackend(
int64_t sw_registration_id,
const url::Origin& origin,
const std::string& key,
const std::string& data,
ServiceWorkerStorage::StatusCallback callback) override;
// Override to allow delays to be injected by tests.
void GetDataFromBackend(
const std::string& key,
ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback callback)
override;
// Override to avoid actual dispatching of the event, just call the provided
// callback instead.
void DispatchSyncEvent(
const std::string& tag,
scoped_refptr<ServiceWorkerVersion> active_version,
bool last_chance,
ServiceWorkerVersion::StatusCallback callback) override;
// Override to just store delayed task, and allow tests to control the clock
// and when delayed tasks are executed.
void ScheduleDelayedTask(blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay) override;
// Override to avoid actual check for main frame, instead return the value set
// by tests.
void HasMainFrameProviderHost(const url::Origin& origin,
BoolCallback callback) override;
private:
// Callback to resume the StoreDataInBackend operation, after explicit
// delays injected by tests.
void StoreDataInBackendContinue(
int64_t sw_registration_id,
const url::Origin& origin,
const std::string& key,
const std::string& data,
ServiceWorkerStorage::StatusCallback callback);
// Callback to resume the GetDataFromBackend operation, after explicit delays
// injected by tests.
void GetDataFromBackendContinue(
const std::string& key,
ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback callback);
bool corrupt_backend_ = false;
bool delay_backend_ = false;
bool has_main_frame_provider_host_ = true;
bool last_chance_ = false;
base::OnceClosure continuation_;
DispatchSyncCallback dispatch_sync_callback_;
DispatchSyncCallback dispatch_periodic_sync_callback_;
base::TimeDelta delayed_one_shot_sync_task_delta_;
base::TimeDelta delayed_periodic_sync_task_delta_;
base::TimeDelta soonest_one_shot_sync_wakeup_delta_;
base::TimeDelta soonest_periodic_sync_wakeup_delta_;
DISALLOW_COPY_AND_ASSIGN(TestBackgroundSyncManager);
};
} // namespace content
#endif // CONTENT_TEST_TEST_BACKGROUND_SYNC_MANAGER_H_