forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground_sync_base_browsertest.cc
239 lines (201 loc) · 9.03 KB
/
background_sync_base_browsertest.cc
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
// Copyright 2019 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.
#include "content/browser/background_sync/background_sync_base_browsertest.h"
#include <memory>
#include <set>
#include <vector>
#include "base/metrics/field_trial_param_associator.h"
#include "base/strings/stringprintf.h"
#include "base/task/post_task.h"
#include "content/browser/background_sync/background_sync_manager.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/test/background_sync_test_util.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/test/mock_background_sync_controller.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
namespace content {
BackgroundSyncBaseBrowserTest::BackgroundSyncBaseBrowserTest() {}
BackgroundSyncBaseBrowserTest::~BackgroundSyncBaseBrowserTest() {}
std::string BackgroundSyncBaseBrowserTest::BuildScriptString(
const std::string& function,
const std::string& argument) {
return base::StringPrintf("%s('%s');", function.c_str(), argument.c_str());
}
std::string BackgroundSyncBaseBrowserTest::BuildExpectedResult(
const std::string& tag,
const std::string& action) {
return base::StringPrintf("%s%s %s", kSuccessfulOperationPrefix, tag.c_str(),
action.c_str());
}
bool BackgroundSyncBaseBrowserTest::RegistrationPending(
const std::string& tag) {
bool is_pending;
base::RunLoop run_loop;
StoragePartitionImpl* storage = GetStorage();
BackgroundSyncContextImpl* sync_context = storage->GetBackgroundSyncContext();
ServiceWorkerContextWrapper* service_worker_context =
static_cast<ServiceWorkerContextWrapper*>(
storage->GetServiceWorkerContext());
auto callback = base::BindOnce(
&BackgroundSyncBaseBrowserTest::RegistrationPendingCallback,
base::Unretained(this), run_loop.QuitClosure(),
base::ThreadTaskRunnerHandle::Get(), &is_pending);
RegistrationPendingOnCoreThread(base::WrapRefCounted(sync_context),
base::WrapRefCounted(service_worker_context),
tag, https_server_->GetURL(kDefaultTestURL),
std::move(callback));
run_loop.Run();
return is_pending;
}
bool BackgroundSyncBaseBrowserTest::CompleteDelayedSyncEvent() {
std::string script_result = RunScript("completeDelayedSyncEvent()");
return script_result == BuildExpectedResult("delay", "completing");
}
void BackgroundSyncBaseBrowserTest::RegistrationPendingCallback(
base::OnceClosure quit,
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
bool* result_out,
bool result) {
*result_out = result;
task_runner->PostTask(FROM_HERE, std::move(quit));
}
void BackgroundSyncBaseBrowserTest::RegistrationPendingDidGetSyncRegistration(
const std::string& tag,
base::OnceCallback<void(bool)> callback,
BackgroundSyncStatus error_type,
std::vector<std::unique_ptr<BackgroundSyncRegistration>> registrations) {
ASSERT_EQ(BACKGROUND_SYNC_STATUS_OK, error_type);
// Find the right registration in the list and check its status.
for (const auto& registration : registrations) {
if (registration->options()->tag == tag) {
std::move(callback).Run(registration->sync_state() ==
blink::mojom::BackgroundSyncState::PENDING);
return;
}
}
ADD_FAILURE() << "Registration should exist";
}
void BackgroundSyncBaseBrowserTest::RegistrationPendingDidGetSWRegistration(
const scoped_refptr<BackgroundSyncContextImpl> sync_context,
const std::string& tag,
base::OnceCallback<void(bool)> callback,
blink::ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration) {
ASSERT_EQ(blink::ServiceWorkerStatusCode::kOk, status);
int64_t service_worker_id = registration->id();
BackgroundSyncManager* sync_manager = sync_context->background_sync_manager();
sync_manager->GetOneShotSyncRegistrations(
service_worker_id,
base::BindOnce(&BackgroundSyncBaseBrowserTest::
RegistrationPendingDidGetSyncRegistration,
base::Unretained(this), tag, std::move(callback)));
}
void BackgroundSyncBaseBrowserTest::RegistrationPendingOnCoreThread(
const scoped_refptr<BackgroundSyncContextImpl> sync_context,
const scoped_refptr<ServiceWorkerContextWrapper> sw_context,
const std::string& tag,
const GURL& url,
base::OnceCallback<void(bool)> callback) {
sw_context->FindReadyRegistrationForClientUrl(
url, blink::StorageKey(url::Origin::Create(url)),
base::BindOnce(&BackgroundSyncBaseBrowserTest::
RegistrationPendingDidGetSWRegistration,
base::Unretained(this), sync_context, tag,
std::move(callback)));
}
void BackgroundSyncBaseBrowserTest::SetUp() {
const char kTrialName[] = "BackgroundSync";
const char kGroupName[] = "BackgroundSync";
const char kFeatureName[] = "PeriodicBackgroundSync";
scoped_refptr<base::FieldTrial> trial =
base::FieldTrialList::CreateFieldTrial(kTrialName, kGroupName);
std::map<std::string, std::string> params;
params["max_sync_attempts"] = "1";
params["min_periodic_sync_events_interval_sec"] = "5";
base::FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams(
kTrialName, kGroupName, params);
std::unique_ptr<base::FeatureList> feature_list(
std::make_unique<base::FeatureList>());
feature_list->RegisterFieldTrialOverride(
kFeatureName, base::FeatureList::OVERRIDE_ENABLE_FEATURE, trial.get());
scoped_feature_list_.InitWithFeatureList(std::move(feature_list));
background_sync_test_util::SetIgnoreNetworkChanges(true);
ContentBrowserTest::SetUp();
}
void BackgroundSyncBaseBrowserTest::SetIncognitoMode(bool incognito) {
shell_ = incognito ? CreateOffTheRecordBrowser() : shell();
// Let any async shell creation logic finish.
base::RunLoop().RunUntilIdle();
}
StoragePartitionImpl* BackgroundSyncBaseBrowserTest::GetStorage() {
WebContents* web_contents = shell_->web_contents();
return static_cast<StoragePartitionImpl*>(
web_contents->GetBrowserContext()->GetStoragePartition(
web_contents->GetSiteInstance()));
}
WebContents* BackgroundSyncBaseBrowserTest::web_contents() {
return shell_->web_contents();
}
void BackgroundSyncBaseBrowserTest::SetUpOnMainThread() {
https_server_ = std::make_unique<net::EmbeddedTestServer>(
net::EmbeddedTestServer::TYPE_HTTPS);
https_server_->ServeFilesFromSourceDirectory(GetTestDataFilePath());
ASSERT_TRUE(https_server_->Start());
SetIncognitoMode(false);
background_sync_test_util::SetOnline(web_contents(), true);
ASSERT_TRUE(LoadTestPage(kDefaultTestURL));
ContentBrowserTest::SetUpOnMainThread();
}
void BackgroundSyncBaseBrowserTest::TearDownOnMainThread() {
https_server_.reset();
}
bool BackgroundSyncBaseBrowserTest::LoadTestPage(const std::string& path) {
return NavigateToURL(shell_, https_server_->GetURL(path));
}
std::string BackgroundSyncBaseBrowserTest::RunScript(
const std::string& script) {
return EvalJs(web_contents(), script, EXECUTE_SCRIPT_USE_MANUAL_REPLY)
.ExtractString();
}
void BackgroundSyncBaseBrowserTest::SetTestClock(base::SimpleTestClock* clock) {
DCHECK(clock);
StoragePartitionImpl* storage = GetStorage();
BackgroundSyncContextImpl* sync_context = storage->GetBackgroundSyncContext();
BackgroundSyncManager* background_sync_manager =
sync_context->background_sync_manager();
background_sync_manager->set_clock(clock);
}
void BackgroundSyncBaseBrowserTest::ClearStoragePartitionData() {
// Clear data from the storage partition. Parameters are set to clear data
// for service workers, for all origins, for an unbounded time range.
StoragePartitionImpl* storage = GetStorage();
uint32_t storage_partition_mask =
StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS;
uint32_t quota_storage_mask =
StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL;
GURL delete_origin = GURL();
const base::Time delete_begin = base::Time();
base::Time delete_end = base::Time::Max();
base::RunLoop run_loop;
storage->ClearData(storage_partition_mask, quota_storage_mask, delete_origin,
delete_begin, delete_end, run_loop.QuitClosure());
run_loop.Run();
}
std::string BackgroundSyncBaseBrowserTest::PopConsoleString() {
return RunScript("resultQueue.pop()");
}
bool BackgroundSyncBaseBrowserTest::PopConsole(
const std::string& expected_msg) {
std::string script_result = PopConsoleString();
return script_result == expected_msg;
}
bool BackgroundSyncBaseBrowserTest::RegisterServiceWorker() {
std::string script_result = RunScript("registerServiceWorker()");
return script_result == BuildExpectedResult("service worker", "registered");
}
} // namespace content