| 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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 // (assertion failure) if the tag isn't registered. | 165 // (assertion failure) if the tag isn't registered. |
| 166 bool OneShotPending(const std::string& tag); | 166 bool OneShotPending(const std::string& tag); |
| 167 | 167 |
| 168 void ClearStoragePartitionData(); | 168 void ClearStoragePartitionData(); |
| 169 | 169 |
| 170 std::string PopConsoleString(); | 170 std::string PopConsoleString(); |
| 171 bool PopConsole(const std::string& expected_msg); | 171 bool PopConsole(const std::string& expected_msg); |
| 172 bool RegisterServiceWorker(); | 172 bool RegisterServiceWorker(); |
| 173 bool RegisterOneShot(const std::string& tag); | 173 bool RegisterOneShot(const std::string& tag); |
| 174 bool RegisterOneShotFromServiceWorker(const std::string& tag); | 174 bool RegisterOneShotFromServiceWorker(const std::string& tag); |
| 175 bool UnregisterOneShot(const std::string& tag); | |
| 176 bool UnregisterOneShotTwice(const std::string& tag); | |
| 177 bool GetRegistrationOneShot(const std::string& tag); | 175 bool GetRegistrationOneShot(const std::string& tag); |
| 178 bool GetRegistrationOneShotFromServiceWorker(const std::string& tag); | 176 bool GetRegistrationOneShotFromServiceWorker(const std::string& tag); |
| 179 bool MatchRegistrations(const std::string& script_result, | 177 bool MatchRegistrations(const std::string& script_result, |
| 180 const std::vector<std::string>& expected_tags); | 178 const std::vector<std::string>& expected_tags); |
| 181 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); | 179 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); |
| 182 bool GetRegistrationsOneShotFromServiceWorker( | 180 bool GetRegistrationsOneShotFromServiceWorker( |
| 183 const std::vector<std::string>& expected_tags); | 181 const std::vector<std::string>& expected_tags); |
| 184 bool CompleteDelayedOneShot(); | 182 bool CompleteDelayedOneShot(); |
| 185 bool RejectDelayedOneShot(); | 183 bool RejectDelayedOneShot(); |
| 186 bool NotifyWhenFinishedOneShot(const std::string& tag); | |
| 187 bool NotifyWhenFinishedImmediateOneShot(const std::string& expected_msg); | |
| 188 bool StoreRegistrationOneShot(const std::string& tag); | |
| 189 | 184 |
| 190 private: | 185 private: |
| 191 scoped_ptr<net::EmbeddedTestServer> https_server_; | 186 scoped_ptr<net::EmbeddedTestServer> https_server_; |
| 192 Shell* shell_ = nullptr; | 187 Shell* shell_ = nullptr; |
| 193 | 188 |
| 194 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); | 189 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); |
| 195 }; | 190 }; |
| 196 | 191 |
| 197 void BackgroundSyncBrowserTest::SetOnline(bool online) { | 192 void BackgroundSyncBrowserTest::SetOnline(bool online) { |
| 198 ASSERT_TRUE(shell_); | 193 ASSERT_TRUE(shell_); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 | 286 |
| 292 bool BackgroundSyncBrowserTest::RegisterOneShotFromServiceWorker( | 287 bool BackgroundSyncBrowserTest::RegisterOneShotFromServiceWorker( |
| 293 const std::string& tag) { | 288 const std::string& tag) { |
| 294 std::string script_result; | 289 std::string script_result; |
| 295 EXPECT_TRUE( | 290 EXPECT_TRUE( |
| 296 RunScript(BuildScriptString("registerOneShotFromServiceWorker", tag), | 291 RunScript(BuildScriptString("registerOneShotFromServiceWorker", tag), |
| 297 &script_result)); | 292 &script_result)); |
| 298 return script_result == BuildExpectedResult(tag, "register sent to SW"); | 293 return script_result == BuildExpectedResult(tag, "register sent to SW"); |
| 299 } | 294 } |
| 300 | 295 |
| 301 bool BackgroundSyncBrowserTest::UnregisterOneShot(const std::string& tag) { | |
| 302 std::string script_result; | |
| 303 EXPECT_TRUE( | |
| 304 RunScript(BuildScriptString("unregisterOneShot", tag), &script_result)); | |
| 305 return script_result == BuildExpectedResult(tag, "unregistered"); | |
| 306 } | |
| 307 | |
| 308 bool BackgroundSyncBrowserTest::UnregisterOneShotTwice(const std::string& tag) { | |
| 309 std::string script_result; | |
| 310 EXPECT_TRUE(RunScript(BuildScriptString("unregisterOneShotTwice", tag), | |
| 311 &script_result)); | |
| 312 return script_result == | |
| 313 BuildExpectedResult(tag, "failed to unregister twice"); | |
| 314 } | |
| 315 | |
| 316 bool BackgroundSyncBrowserTest::GetRegistrationOneShot(const std::string& tag) { | 296 bool BackgroundSyncBrowserTest::GetRegistrationOneShot(const std::string& tag) { |
| 317 std::string script_result; | 297 std::string script_result; |
| 318 EXPECT_TRUE(RunScript(BuildScriptString("getRegistrationOneShot", tag), | 298 EXPECT_TRUE(RunScript(BuildScriptString("getRegistrationOneShot", tag), |
| 319 &script_result)); | 299 &script_result)); |
| 320 return script_result == BuildExpectedResult(tag, "found"); | 300 return script_result == BuildExpectedResult(tag, "found"); |
| 321 } | 301 } |
| 322 | 302 |
| 323 bool BackgroundSyncBrowserTest::GetRegistrationOneShotFromServiceWorker( | 303 bool BackgroundSyncBrowserTest::GetRegistrationOneShotFromServiceWorker( |
| 324 const std::string& tag) { | 304 const std::string& tag) { |
| 325 std::string script_result; | 305 std::string script_result; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 EXPECT_TRUE(RunScript("completeDelayedOneShot()", &script_result)); | 348 EXPECT_TRUE(RunScript("completeDelayedOneShot()", &script_result)); |
| 369 return script_result == BuildExpectedResult("delay", "completing"); | 349 return script_result == BuildExpectedResult("delay", "completing"); |
| 370 } | 350 } |
| 371 | 351 |
| 372 bool BackgroundSyncBrowserTest::RejectDelayedOneShot() { | 352 bool BackgroundSyncBrowserTest::RejectDelayedOneShot() { |
| 373 std::string script_result; | 353 std::string script_result; |
| 374 EXPECT_TRUE(RunScript("rejectDelayedOneShot()", &script_result)); | 354 EXPECT_TRUE(RunScript("rejectDelayedOneShot()", &script_result)); |
| 375 return script_result == BuildExpectedResult("delay", "rejecting"); | 355 return script_result == BuildExpectedResult("delay", "rejecting"); |
| 376 } | 356 } |
| 377 | 357 |
| 378 bool BackgroundSyncBrowserTest::NotifyWhenFinishedOneShot( | |
| 379 const std::string& tag) { | |
| 380 EXPECT_TRUE(content::ExecuteScript( | |
| 381 shell_->web_contents(), | |
| 382 BuildScriptString("notifyWhenFinishedOneShot", tag))); | |
| 383 return PopConsole(BuildExpectedResult(tag, "finished")); | |
| 384 } | |
| 385 | |
| 386 bool BackgroundSyncBrowserTest::NotifyWhenFinishedImmediateOneShot( | |
| 387 const std::string& expected_msg) { | |
| 388 std::string script_result; | |
| 389 EXPECT_TRUE( | |
| 390 RunScript("notifyWhenFinishedImmediateOneShot()", &script_result)); | |
| 391 return script_result == expected_msg; | |
| 392 } | |
| 393 | |
| 394 bool BackgroundSyncBrowserTest::StoreRegistrationOneShot( | |
| 395 const std::string& tag) { | |
| 396 std::string script_result; | |
| 397 EXPECT_TRUE( | |
| 398 RunScript(BuildScriptString("storeRegistration", tag), &script_result)); | |
| 399 return script_result == BuildExpectedResult(tag, "stored"); | |
| 400 } | |
| 401 | |
| 402 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotFires) { | 358 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotFires) { |
| 403 EXPECT_TRUE(RegisterServiceWorker()); | 359 EXPECT_TRUE(RegisterServiceWorker()); |
| 404 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 360 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 405 | 361 |
| 406 EXPECT_TRUE(RegisterOneShot("foo")); | 362 EXPECT_TRUE(RegisterOneShot("foo")); |
| 407 EXPECT_TRUE(PopConsole("foo fired")); | 363 EXPECT_TRUE(PopConsole("foo fired")); |
| 408 EXPECT_FALSE(GetRegistrationOneShot("foo")); | 364 EXPECT_FALSE(GetRegistrationOneShot("foo")); |
| 409 } | 365 } |
| 410 | 366 |
| 411 // Verify that Register works in a service worker | 367 // Verify that Register works in a service worker |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 | 401 |
| 446 // Verify that it is firing. | 402 // Verify that it is firing. |
| 447 EXPECT_TRUE(GetRegistrationOneShot("delay")); | 403 EXPECT_TRUE(GetRegistrationOneShot("delay")); |
| 448 EXPECT_FALSE(OneShotPending("delay")); | 404 EXPECT_FALSE(OneShotPending("delay")); |
| 449 | 405 |
| 450 // Complete the task. | 406 // Complete the task. |
| 451 EXPECT_TRUE(CompleteDelayedOneShot()); | 407 EXPECT_TRUE(CompleteDelayedOneShot()); |
| 452 EXPECT_TRUE(PopConsole("ok - delay completed")); | 408 EXPECT_TRUE(PopConsole("ok - delay completed")); |
| 453 | 409 |
| 454 // Verify that it finished firing. | 410 // Verify that it finished firing. |
| 455 // TODO(jkarlin): Use registration.finished to verify that the event actually | |
| 456 // completed successfully. | |
| 457 EXPECT_FALSE(GetRegistrationOneShot("delay")); | 411 EXPECT_FALSE(GetRegistrationOneShot("delay")); |
| 458 } | 412 } |
| 459 | 413 |
| 460 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, WaitUntilReject) { | 414 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, WaitUntilReject) { |
| 461 EXPECT_TRUE(RegisterServiceWorker()); | 415 EXPECT_TRUE(RegisterServiceWorker()); |
| 462 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 416 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 463 | 417 |
| 464 SetOnline(true); | 418 SetOnline(true); |
| 465 EXPECT_TRUE(RegisterOneShot("delay")); | 419 EXPECT_TRUE(RegisterOneShot("delay")); |
| 466 | 420 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 std::vector<std::string> registered_tags; | 508 std::vector<std::string> registered_tags; |
| 555 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); | 509 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 556 | 510 |
| 557 SetOnline(false); | 511 SetOnline(false); |
| 558 | 512 |
| 559 EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw")); | 513 EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw")); |
| 560 EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); | 514 EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); |
| 561 EXPECT_TRUE(GetRegistrationOneShotFromServiceWorker("foo_sw")); | 515 EXPECT_TRUE(GetRegistrationOneShotFromServiceWorker("foo_sw")); |
| 562 } | 516 } |
| 563 | 517 |
| 564 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, Unregister) { | |
| 565 EXPECT_TRUE(RegisterServiceWorker()); | |
| 566 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | |
| 567 | |
| 568 SetOnline(false); | |
| 569 EXPECT_TRUE(RegisterOneShot("foo")); | |
| 570 EXPECT_TRUE(UnregisterOneShot("foo")); | |
| 571 EXPECT_FALSE(GetRegistrationOneShot("foo")); | |
| 572 } | |
| 573 | |
| 574 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, UnregisterTwice) { | |
| 575 EXPECT_TRUE(RegisterServiceWorker()); | |
| 576 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | |
| 577 | |
| 578 SetOnline(false); | |
| 579 EXPECT_TRUE(RegisterOneShot("foo")); | |
| 580 EXPECT_TRUE(UnregisterOneShotTwice("foo")); | |
| 581 EXPECT_FALSE(GetRegistrationOneShot("foo")); | |
| 582 } | |
| 583 | |
| 584 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, UnregisterMidSync) { | |
| 585 EXPECT_TRUE(RegisterServiceWorker()); | |
| 586 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | |
| 587 | |
| 588 EXPECT_TRUE(RegisterOneShot("unregister")); | |
| 589 EXPECT_TRUE(PopConsole("ok - unregister completed")); | |
| 590 } | |
| 591 | |
| 592 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, | |
| 593 CallFinishedBeforeSyncSucceeds) { | |
| 594 EXPECT_TRUE(RegisterServiceWorker()); | |
| 595 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | |
| 596 | |
| 597 SetOnline(false); | |
| 598 EXPECT_TRUE(RegisterOneShot("foo")); | |
| 599 EXPECT_TRUE(NotifyWhenFinishedOneShot("foo")); | |
| 600 | |
| 601 SetOnline(true); | |
| 602 // The ordering of PopConsole messages tells us that the event fired | |
| 603 // before finished resolved. | |
| 604 EXPECT_TRUE(PopConsole("foo fired")); | |
| 605 EXPECT_TRUE(PopConsole("foo finished result: true")); | |
| 606 } | |
| 607 | |
| 608 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, CallFinishedBeforeSyncFails) { | |
| 609 EXPECT_TRUE(RegisterServiceWorker()); | |
| 610 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | |
| 611 | |
| 612 SetOnline(true); | |
| 613 EXPECT_TRUE(RegisterOneShot("delay")); | |
| 614 EXPECT_FALSE(OneShotPending("delay")); | |
| 615 EXPECT_TRUE(NotifyWhenFinishedOneShot("delay")); | |
| 616 | |
| 617 EXPECT_TRUE(RejectDelayedOneShot()); | |
| 618 // The ordering of PopConsole messages tells us that the event fired | |
| 619 // before finished resolved. | |
| 620 EXPECT_TRUE(PopConsole("ok - delay rejected")); | |
| 621 EXPECT_TRUE(PopConsole("delay finished result: false")); | |
| 622 } | |
| 623 | |
| 624 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, | |
| 625 CallFinishedAfterSyncSucceeds) { | |
| 626 EXPECT_TRUE(RegisterServiceWorker()); | |
| 627 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | |
| 628 | |
| 629 SetOnline(false); | |
| 630 EXPECT_TRUE(RegisterOneShot("foo")); | |
| 631 EXPECT_TRUE(StoreRegistrationOneShot("foo")); | |
| 632 | |
| 633 SetOnline(true); | |
| 634 EXPECT_TRUE(PopConsole("foo fired")); | |
| 635 EXPECT_FALSE(GetRegistrationOneShot("foo")); | |
| 636 EXPECT_TRUE(NotifyWhenFinishedImmediateOneShot("ok - foo result: true")); | |
| 637 } | |
| 638 | |
| 639 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, | |
| 640 CallFinishedAfterSyncUnregistered) { | |
| 641 EXPECT_TRUE(RegisterServiceWorker()); | |
| 642 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | |
| 643 | |
| 644 SetOnline(false); | |
| 645 EXPECT_TRUE(RegisterOneShot("foo")); | |
| 646 EXPECT_TRUE(StoreRegistrationOneShot("foo")); | |
| 647 EXPECT_TRUE(UnregisterOneShot("foo")); | |
| 648 EXPECT_FALSE(GetRegistrationOneShot("foo")); | |
| 649 EXPECT_TRUE(NotifyWhenFinishedImmediateOneShot("ok - foo result: false")); | |
| 650 } | |
| 651 | |
| 652 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, CallFinishedAfterSyncFails) { | |
| 653 EXPECT_TRUE(RegisterServiceWorker()); | |
| 654 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | |
| 655 | |
| 656 SetOnline(true); | |
| 657 EXPECT_TRUE(RegisterOneShot("delay")); | |
| 658 EXPECT_FALSE(OneShotPending("delay")); | |
| 659 EXPECT_TRUE(StoreRegistrationOneShot("delay")); | |
| 660 | |
| 661 EXPECT_TRUE(RejectDelayedOneShot()); | |
| 662 EXPECT_TRUE(PopConsole("ok - delay rejected")); | |
| 663 EXPECT_TRUE(NotifyWhenFinishedImmediateOneShot("ok - delay result: false")); | |
| 664 } | |
| 665 | |
| 666 // Verify that a background sync registration is deleted when site data is | 518 // Verify that a background sync registration is deleted when site data is |
| 667 // cleared. | 519 // cleared. |
| 668 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, | 520 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 669 SyncRegistrationDeletedWhenClearingSiteData) { | 521 SyncRegistrationDeletedWhenClearingSiteData) { |
| 670 EXPECT_TRUE(RegisterServiceWorker()); | 522 EXPECT_TRUE(RegisterServiceWorker()); |
| 671 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 523 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 672 | 524 |
| 673 // Prevent firing by going offline. | 525 // Prevent firing by going offline. |
| 674 SetOnline(false); | 526 SetOnline(false); |
| 675 EXPECT_TRUE(RegisterOneShot("foo")); | 527 EXPECT_TRUE(RegisterOneShot("foo")); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 752 | 604 |
| 753 // Simulate a user clearing site data (including Service Workers, crucially), | 605 // Simulate a user clearing site data (including Service Workers, crucially), |
| 754 // by clearing data from the storage partition. | 606 // by clearing data from the storage partition. |
| 755 ClearStoragePartitionData(); | 607 ClearStoragePartitionData(); |
| 756 | 608 |
| 757 // Verify that it was deleted. | 609 // Verify that it was deleted. |
| 758 EXPECT_FALSE(GetRegistrationOneShot("delay")); | 610 EXPECT_FALSE(GetRegistrationOneShot("delay")); |
| 759 } | 611 } |
| 760 | 612 |
| 761 } // namespace content | 613 } // namespace content |
| OLD | NEW |