OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <set> | 5 #include <set> |
6 #include <string> | 6 #include <string> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 return content::ExecuteScriptAndExtractString(shell_->web_contents(), | 147 return content::ExecuteScriptAndExtractString(shell_->web_contents(), |
148 script, result); | 148 script, result); |
149 } | 149 } |
150 | 150 |
151 void SetOnline(bool online); | 151 void SetOnline(bool online); |
152 | 152 |
153 // Returns true if the one-shot sync with tag is currently pending. Fails | 153 // Returns true if the one-shot sync with tag is currently pending. Fails |
154 // (assertion failure) if the tag isn't registered. | 154 // (assertion failure) if the tag isn't registered. |
155 bool OneShotPending(const std::string& tag); | 155 bool OneShotPending(const std::string& tag); |
156 | 156 |
| 157 std::string PopConsoleString(); |
157 bool PopConsole(const std::string& expected_msg); | 158 bool PopConsole(const std::string& expected_msg); |
158 bool RegisterServiceWorker(); | 159 bool RegisterServiceWorker(); |
159 bool RegisterOneShot(const std::string& tag); | 160 bool RegisterOneShot(const std::string& tag); |
| 161 bool RegisterOneShotFromServiceWorker(const std::string& tag); |
160 bool UnregisterOneShot(const std::string& tag); | 162 bool UnregisterOneShot(const std::string& tag); |
161 bool UnregisterOneShotTwice(const std::string& tag); | 163 bool UnregisterOneShotTwice(const std::string& tag); |
162 bool GetRegistrationOneShot(const std::string& tag); | 164 bool GetRegistrationOneShot(const std::string& tag); |
| 165 bool GetRegistrationOneShotFromServiceWorker(const std::string& tag); |
| 166 bool MatchRegistrations(const std::string& script_result, |
| 167 const std::vector<std::string>& expected_tags); |
163 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); | 168 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); |
| 169 bool GetRegistrationsOneShotFromServiceWorker( |
| 170 const std::vector<std::string>& expected_tags); |
164 bool CompleteDelayedOneShot(); | 171 bool CompleteDelayedOneShot(); |
165 bool RejectDelayedOneShot(); | 172 bool RejectDelayedOneShot(); |
166 bool NotifyWhenDoneOneShot(const std::string& tag); | 173 bool NotifyWhenDoneOneShot(const std::string& tag); |
167 bool NotifyWhenDoneImmediateOneShot(const std::string& expected_msg); | 174 bool NotifyWhenDoneImmediateOneShot(const std::string& expected_msg); |
168 bool StoreRegistrationOneShot(const std::string& tag); | 175 bool StoreRegistrationOneShot(const std::string& tag); |
169 | 176 |
170 private: | 177 private: |
171 scoped_ptr<net::SpawnedTestServer> https_server_; | 178 scoped_ptr<net::SpawnedTestServer> https_server_; |
172 Shell* shell_ = nullptr; | 179 Shell* shell_ = nullptr; |
173 | 180 |
(...skipping 30 matching lines...) Expand all Loading... |
204 BrowserThread::IO, FROM_HERE, | 211 BrowserThread::IO, FROM_HERE, |
205 base::Bind(&OneShotPendingOnIOThread, make_scoped_refptr(sync_context), | 212 base::Bind(&OneShotPendingOnIOThread, make_scoped_refptr(sync_context), |
206 make_scoped_refptr(service_worker_context), tag, | 213 make_scoped_refptr(service_worker_context), tag, |
207 https_server_->GetURL(kDefaultTestURL), callback)); | 214 https_server_->GetURL(kDefaultTestURL), callback)); |
208 | 215 |
209 run_loop.Run(); | 216 run_loop.Run(); |
210 | 217 |
211 return is_pending; | 218 return is_pending; |
212 } | 219 } |
213 | 220 |
214 bool BackgroundSyncBrowserTest::PopConsole(const std::string& expected_msg) { | 221 std::string BackgroundSyncBrowserTest::PopConsoleString() { |
215 std::string script_result; | 222 std::string script_result; |
216 EXPECT_TRUE(RunScript("resultQueue.pop()", &script_result)); | 223 EXPECT_TRUE(RunScript("resultQueue.pop()", &script_result)); |
| 224 return script_result; |
| 225 } |
| 226 |
| 227 bool BackgroundSyncBrowserTest::PopConsole(const std::string& expected_msg) { |
| 228 std::string script_result = PopConsoleString(); |
217 return script_result == expected_msg; | 229 return script_result == expected_msg; |
218 } | 230 } |
219 | 231 |
220 bool BackgroundSyncBrowserTest::RegisterServiceWorker() { | 232 bool BackgroundSyncBrowserTest::RegisterServiceWorker() { |
221 std::string script_result; | 233 std::string script_result; |
222 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result)); | 234 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
223 return script_result == BuildExpectedResult("service worker", "registered"); | 235 return script_result == BuildExpectedResult("service worker", "registered"); |
224 } | 236 } |
225 | 237 |
226 bool BackgroundSyncBrowserTest::RegisterOneShot(const std::string& tag) { | 238 bool BackgroundSyncBrowserTest::RegisterOneShot(const std::string& tag) { |
227 std::string script_result; | 239 std::string script_result; |
228 EXPECT_TRUE( | 240 EXPECT_TRUE( |
229 RunScript(BuildScriptString("registerOneShot", tag), &script_result)); | 241 RunScript(BuildScriptString("registerOneShot", tag), &script_result)); |
230 return script_result == BuildExpectedResult(tag, "registered"); | 242 return script_result == BuildExpectedResult(tag, "registered"); |
231 } | 243 } |
232 | 244 |
| 245 bool BackgroundSyncBrowserTest::RegisterOneShotFromServiceWorker( |
| 246 const std::string& tag) { |
| 247 std::string script_result; |
| 248 EXPECT_TRUE( |
| 249 RunScript(BuildScriptString("registerOneShotFromServiceWorker", tag), |
| 250 &script_result)); |
| 251 return script_result == BuildExpectedResult(tag, "register sent to SW"); |
| 252 } |
| 253 |
233 bool BackgroundSyncBrowserTest::UnregisterOneShot(const std::string& tag) { | 254 bool BackgroundSyncBrowserTest::UnregisterOneShot(const std::string& tag) { |
234 std::string script_result; | 255 std::string script_result; |
235 EXPECT_TRUE( | 256 EXPECT_TRUE( |
236 RunScript(BuildScriptString("unregisterOneShot", tag), &script_result)); | 257 RunScript(BuildScriptString("unregisterOneShot", tag), &script_result)); |
237 return script_result == BuildExpectedResult(tag, "unregistered"); | 258 return script_result == BuildExpectedResult(tag, "unregistered"); |
238 } | 259 } |
239 | 260 |
240 bool BackgroundSyncBrowserTest::UnregisterOneShotTwice(const std::string& tag) { | 261 bool BackgroundSyncBrowserTest::UnregisterOneShotTwice(const std::string& tag) { |
241 std::string script_result; | 262 std::string script_result; |
242 EXPECT_TRUE(RunScript(BuildScriptString("unregisterOneShotTwice", tag), | 263 EXPECT_TRUE(RunScript(BuildScriptString("unregisterOneShotTwice", tag), |
243 &script_result)); | 264 &script_result)); |
244 return script_result == | 265 return script_result == |
245 BuildExpectedResult(tag, "failed to unregister twice"); | 266 BuildExpectedResult(tag, "failed to unregister twice"); |
246 } | 267 } |
247 | 268 |
248 bool BackgroundSyncBrowserTest::GetRegistrationOneShot(const std::string& tag) { | 269 bool BackgroundSyncBrowserTest::GetRegistrationOneShot(const std::string& tag) { |
249 std::string script_result; | 270 std::string script_result; |
250 EXPECT_TRUE(RunScript(BuildScriptString("getRegistrationOneShot", tag), | 271 EXPECT_TRUE(RunScript(BuildScriptString("getRegistrationOneShot", tag), |
251 &script_result)); | 272 &script_result)); |
252 return script_result == BuildExpectedResult(tag, "found"); | 273 return script_result == BuildExpectedResult(tag, "found"); |
253 } | 274 } |
254 | 275 |
| 276 bool BackgroundSyncBrowserTest::GetRegistrationOneShotFromServiceWorker( |
| 277 const std::string& tag) { |
| 278 std::string script_result; |
| 279 EXPECT_TRUE(RunScript( |
| 280 BuildScriptString("getRegistrationOneShotFromServiceWorker", tag), |
| 281 &script_result)); |
| 282 EXPECT_TRUE(script_result == "ok - getRegistration sent to SW"); |
| 283 |
| 284 return PopConsole(BuildExpectedResult(tag, "found")); |
| 285 } |
| 286 |
| 287 bool BackgroundSyncBrowserTest::MatchRegistrations( |
| 288 const std::string& script_result, |
| 289 const std::vector<std::string>& expected_tags) { |
| 290 EXPECT_TRUE(base::StartsWith(script_result, kSuccessfulOperationPrefix, |
| 291 base::CompareCase::INSENSITIVE_ASCII)); |
| 292 std::string tag_string = |
| 293 script_result.substr(strlen(kSuccessfulOperationPrefix)); |
| 294 std::vector<std::string> result_tags = base::SplitString( |
| 295 tag_string, ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 296 |
| 297 return std::set<std::string>(expected_tags.begin(), expected_tags.end()) == |
| 298 std::set<std::string>(result_tags.begin(), result_tags.end()); |
| 299 } |
| 300 |
255 bool BackgroundSyncBrowserTest::GetRegistrationsOneShot( | 301 bool BackgroundSyncBrowserTest::GetRegistrationsOneShot( |
256 const std::vector<std::string>& expected_tags) { | 302 const std::vector<std::string>& expected_tags) { |
257 std::string script_result; | 303 std::string script_result; |
258 EXPECT_TRUE(RunScript("getRegistrationsOneShot()", &script_result)); | 304 EXPECT_TRUE(RunScript("getRegistrationsOneShot()", &script_result)); |
259 | 305 |
260 EXPECT_TRUE(base::StartsWith(script_result, kSuccessfulOperationPrefix, | 306 return MatchRegistrations(script_result, expected_tags); |
261 base::CompareCase::INSENSITIVE_ASCII)); | 307 } |
262 script_result = script_result.substr(strlen(kSuccessfulOperationPrefix)); | |
263 std::vector<std::string> result_tags = base::SplitString( | |
264 script_result, ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); | |
265 | 308 |
266 return std::set<std::string>(expected_tags.begin(), expected_tags.end()) == | 309 bool BackgroundSyncBrowserTest::GetRegistrationsOneShotFromServiceWorker( |
267 std::set<std::string>(result_tags.begin(), result_tags.end()); | 310 const std::vector<std::string>& expected_tags) { |
| 311 std::string script_result; |
| 312 EXPECT_TRUE( |
| 313 RunScript("getRegistrationsOneShotFromServiceWorker()", &script_result)); |
| 314 EXPECT_TRUE(script_result == "ok - getRegistrations sent to SW"); |
| 315 |
| 316 return MatchRegistrations(PopConsoleString(), expected_tags); |
268 } | 317 } |
269 | 318 |
270 bool BackgroundSyncBrowserTest::CompleteDelayedOneShot() { | 319 bool BackgroundSyncBrowserTest::CompleteDelayedOneShot() { |
271 std::string script_result; | 320 std::string script_result; |
272 EXPECT_TRUE(RunScript("completeDelayedOneShot()", &script_result)); | 321 EXPECT_TRUE(RunScript("completeDelayedOneShot()", &script_result)); |
273 return script_result == BuildExpectedResult("delay", "completing"); | 322 return script_result == BuildExpectedResult("delay", "completing"); |
274 } | 323 } |
275 | 324 |
276 bool BackgroundSyncBrowserTest::RejectDelayedOneShot() { | 325 bool BackgroundSyncBrowserTest::RejectDelayedOneShot() { |
277 std::string script_result; | 326 std::string script_result; |
(...skipping 24 matching lines...) Expand all Loading... |
302 | 351 |
303 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotFires) { | 352 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotFires) { |
304 EXPECT_TRUE(RegisterServiceWorker()); | 353 EXPECT_TRUE(RegisterServiceWorker()); |
305 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 354 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
306 | 355 |
307 EXPECT_TRUE(RegisterOneShot("foo")); | 356 EXPECT_TRUE(RegisterOneShot("foo")); |
308 EXPECT_TRUE(PopConsole("foo fired")); | 357 EXPECT_TRUE(PopConsole("foo fired")); |
309 EXPECT_FALSE(GetRegistrationOneShot("foo")); | 358 EXPECT_FALSE(GetRegistrationOneShot("foo")); |
310 } | 359 } |
311 | 360 |
| 361 // Verify that Register works in a service worker |
| 362 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 363 OneShotFromServiceWorkerFires) { |
| 364 EXPECT_TRUE(RegisterServiceWorker()); |
| 365 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 366 |
| 367 EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw")); |
| 368 EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); |
| 369 EXPECT_TRUE(PopConsole("foo_sw fired")); |
| 370 EXPECT_FALSE(GetRegistrationOneShot("foo_sw")); |
| 371 } |
| 372 |
312 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotDelaysForNetwork) { | 373 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotDelaysForNetwork) { |
313 EXPECT_TRUE(RegisterServiceWorker()); | 374 EXPECT_TRUE(RegisterServiceWorker()); |
314 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 375 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
315 | 376 |
316 // Prevent firing by going offline. | 377 // Prevent firing by going offline. |
317 SetOnline(false); | 378 SetOnline(false); |
318 EXPECT_TRUE(RegisterOneShot("foo")); | 379 EXPECT_TRUE(RegisterOneShot("foo")); |
319 EXPECT_TRUE(GetRegistrationOneShot("foo")); | 380 EXPECT_TRUE(GetRegistrationOneShot("foo")); |
320 EXPECT_TRUE(OneShotPending("foo")); | 381 EXPECT_TRUE(OneShotPending("foo")); |
321 | 382 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 SetOnline(false); | 467 SetOnline(false); |
407 registered_tags.push_back("foo"); | 468 registered_tags.push_back("foo"); |
408 registered_tags.push_back("bar"); | 469 registered_tags.push_back("bar"); |
409 | 470 |
410 for (const std::string& tag : registered_tags) | 471 for (const std::string& tag : registered_tags) |
411 EXPECT_TRUE(RegisterOneShot(tag)); | 472 EXPECT_TRUE(RegisterOneShot(tag)); |
412 | 473 |
413 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); | 474 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
414 } | 475 } |
415 | 476 |
| 477 // Verify that GetRegistrations works in a service worker |
| 478 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 479 GetRegistrationsFromServiceWorker) { |
| 480 EXPECT_TRUE(RegisterServiceWorker()); |
| 481 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 482 |
| 483 std::vector<std::string> registered_tags; |
| 484 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 485 |
| 486 SetOnline(false); |
| 487 registered_tags.push_back("foo_sw"); |
| 488 registered_tags.push_back("bar_sw"); |
| 489 |
| 490 for (const std::string& tag : registered_tags) { |
| 491 EXPECT_TRUE(RegisterOneShotFromServiceWorker(tag)); |
| 492 EXPECT_TRUE(PopConsole(BuildExpectedResult(tag, "registered in SW"))); |
| 493 } |
| 494 |
| 495 EXPECT_TRUE(GetRegistrationsOneShotFromServiceWorker(registered_tags)); |
| 496 } |
| 497 |
| 498 // Verify that GetRegistration works in a service worker |
| 499 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 500 GetRegistrationFromServiceWorker) { |
| 501 EXPECT_TRUE(RegisterServiceWorker()); |
| 502 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 503 |
| 504 std::vector<std::string> registered_tags; |
| 505 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 506 |
| 507 SetOnline(false); |
| 508 |
| 509 EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw")); |
| 510 EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); |
| 511 EXPECT_TRUE(GetRegistrationOneShotFromServiceWorker("foo_sw")); |
| 512 } |
| 513 |
416 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, Unregister) { | 514 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, Unregister) { |
417 EXPECT_TRUE(RegisterServiceWorker()); | 515 EXPECT_TRUE(RegisterServiceWorker()); |
418 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 516 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
419 | 517 |
420 SetOnline(false); | 518 SetOnline(false); |
421 EXPECT_TRUE(RegisterOneShot("foo")); | 519 EXPECT_TRUE(RegisterOneShot("foo")); |
422 EXPECT_TRUE(UnregisterOneShot("foo")); | 520 EXPECT_TRUE(UnregisterOneShot("foo")); |
423 EXPECT_FALSE(GetRegistrationOneShot("foo")); | 521 EXPECT_FALSE(GetRegistrationOneShot("foo")); |
424 } | 522 } |
425 | 523 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 EXPECT_TRUE(StoreRegistrationOneShot("delay")); | 607 EXPECT_TRUE(StoreRegistrationOneShot("delay")); |
510 | 608 |
511 EXPECT_TRUE(RejectDelayedOneShot()); | 609 EXPECT_TRUE(RejectDelayedOneShot()); |
512 EXPECT_TRUE(PopConsole("ok - delay rejected")); | 610 EXPECT_TRUE(PopConsole("ok - delay rejected")); |
513 EXPECT_TRUE(NotifyWhenDoneImmediateOneShot("ok - delay result: false")); | 611 EXPECT_TRUE(NotifyWhenDoneImmediateOneShot("ok - delay result: false")); |
514 } | 612 } |
515 | 613 |
516 } // namespace | 614 } // namespace |
517 | 615 |
518 } // namespace content | 616 } // namespace content |
OLD | NEW |