| 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 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 // Sets the BackgroundSyncManager's max sync attempts per registration. | 175 // Sets the BackgroundSyncManager's max sync attempts per registration. |
| 176 void SetMaxSyncAttempts(int max_sync_attempts); | 176 void SetMaxSyncAttempts(int max_sync_attempts); |
| 177 | 177 |
| 178 void ClearStoragePartitionData(); | 178 void ClearStoragePartitionData(); |
| 179 | 179 |
| 180 std::string PopConsoleString(); | 180 std::string PopConsoleString(); |
| 181 bool PopConsole(const std::string& expected_msg); | 181 bool PopConsole(const std::string& expected_msg); |
| 182 bool RegisterServiceWorker(); | 182 bool RegisterServiceWorker(); |
| 183 bool RegisterOneShot(const std::string& tag); | 183 bool RegisterOneShot(const std::string& tag); |
| 184 bool RegisterOneShotFromServiceWorker(const std::string& tag); | 184 bool RegisterOneShotFromServiceWorker(const std::string& tag); |
| 185 bool UnregisterOneShot(const std::string& tag); | |
| 186 bool UnregisterOneShotTwice(const std::string& tag); | |
| 187 bool GetRegistrationOneShot(const std::string& tag); | 185 bool GetRegistrationOneShot(const std::string& tag); |
| 188 bool GetRegistrationOneShotFromServiceWorker(const std::string& tag); | 186 bool GetRegistrationOneShotFromServiceWorker(const std::string& tag); |
| 189 bool MatchRegistrations(const std::string& script_result, | 187 bool MatchRegistrations(const std::string& script_result, |
| 190 const std::vector<std::string>& expected_tags); | 188 const std::vector<std::string>& expected_tags); |
| 191 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); | 189 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); |
| 192 bool GetRegistrationsOneShotFromServiceWorker( | 190 bool GetRegistrationsOneShotFromServiceWorker( |
| 193 const std::vector<std::string>& expected_tags); | 191 const std::vector<std::string>& expected_tags); |
| 194 bool CompleteDelayedOneShot(); | 192 bool CompleteDelayedOneShot(); |
| 195 bool RejectDelayedOneShot(); | 193 bool RejectDelayedOneShot(); |
| 196 bool NotifyWhenFinishedOneShot(const std::string& tag); | |
| 197 bool NotifyWhenFinishedImmediateOneShot(const std::string& expected_msg); | |
| 198 bool StoreRegistrationOneShot(const std::string& tag); | |
| 199 | 194 |
| 200 private: | 195 private: |
| 201 scoped_ptr<net::EmbeddedTestServer> https_server_; | 196 scoped_ptr<net::EmbeddedTestServer> https_server_; |
| 202 Shell* shell_ = nullptr; | 197 Shell* shell_ = nullptr; |
| 203 | 198 |
| 204 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); | 199 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); |
| 205 }; | 200 }; |
| 206 | 201 |
| 207 void BackgroundSyncBrowserTest::SetOnline(bool online) { | 202 void BackgroundSyncBrowserTest::SetOnline(bool online) { |
| 208 ASSERT_TRUE(shell_); | 203 ASSERT_TRUE(shell_); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 | 311 |
| 317 bool BackgroundSyncBrowserTest::RegisterOneShotFromServiceWorker( | 312 bool BackgroundSyncBrowserTest::RegisterOneShotFromServiceWorker( |
| 318 const std::string& tag) { | 313 const std::string& tag) { |
| 319 std::string script_result; | 314 std::string script_result; |
| 320 EXPECT_TRUE( | 315 EXPECT_TRUE( |
| 321 RunScript(BuildScriptString("registerOneShotFromServiceWorker", tag), | 316 RunScript(BuildScriptString("registerOneShotFromServiceWorker", tag), |
| 322 &script_result)); | 317 &script_result)); |
| 323 return script_result == BuildExpectedResult(tag, "register sent to SW"); | 318 return script_result == BuildExpectedResult(tag, "register sent to SW"); |
| 324 } | 319 } |
| 325 | 320 |
| 326 bool BackgroundSyncBrowserTest::UnregisterOneShot(const std::string& tag) { | |
| 327 std::string script_result; | |
| 328 EXPECT_TRUE( | |
| 329 RunScript(BuildScriptString("unregisterOneShot", tag), &script_result)); | |
| 330 return script_result == BuildExpectedResult(tag, "unregistered"); | |
| 331 } | |
| 332 | |
| 333 bool BackgroundSyncBrowserTest::UnregisterOneShotTwice(const std::string& tag) { | |
| 334 std::string script_result; | |
| 335 EXPECT_TRUE(RunScript(BuildScriptString("unregisterOneShotTwice", tag), | |
| 336 &script_result)); | |
| 337 return script_result == | |
| 338 BuildExpectedResult(tag, "failed to unregister twice"); | |
| 339 } | |
| 340 | |
| 341 bool BackgroundSyncBrowserTest::GetRegistrationOneShot(const std::string& tag) { | 321 bool BackgroundSyncBrowserTest::GetRegistrationOneShot(const std::string& tag) { |
| 342 std::string script_result; | 322 std::string script_result; |
| 343 EXPECT_TRUE(RunScript(BuildScriptString("getRegistrationOneShot", tag), | 323 EXPECT_TRUE(RunScript(BuildScriptString("getRegistrationOneShot", tag), |
| 344 &script_result)); | 324 &script_result)); |
| 345 return script_result == BuildExpectedResult(tag, "found"); | 325 return script_result == BuildExpectedResult(tag, "found"); |
| 346 } | 326 } |
| 347 | 327 |
| 348 bool BackgroundSyncBrowserTest::GetRegistrationOneShotFromServiceWorker( | 328 bool BackgroundSyncBrowserTest::GetRegistrationOneShotFromServiceWorker( |
| 349 const std::string& tag) { | 329 const std::string& tag) { |
| 350 std::string script_result; | 330 std::string script_result; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 EXPECT_TRUE(RunScript("completeDelayedOneShot()", &script_result)); | 373 EXPECT_TRUE(RunScript("completeDelayedOneShot()", &script_result)); |
| 394 return script_result == BuildExpectedResult("delay", "completing"); | 374 return script_result == BuildExpectedResult("delay", "completing"); |
| 395 } | 375 } |
| 396 | 376 |
| 397 bool BackgroundSyncBrowserTest::RejectDelayedOneShot() { | 377 bool BackgroundSyncBrowserTest::RejectDelayedOneShot() { |
| 398 std::string script_result; | 378 std::string script_result; |
| 399 EXPECT_TRUE(RunScript("rejectDelayedOneShot()", &script_result)); | 379 EXPECT_TRUE(RunScript("rejectDelayedOneShot()", &script_result)); |
| 400 return script_result == BuildExpectedResult("delay", "rejecting"); | 380 return script_result == BuildExpectedResult("delay", "rejecting"); |
| 401 } | 381 } |
| 402 | 382 |
| 403 bool BackgroundSyncBrowserTest::NotifyWhenFinishedOneShot( | |
| 404 const std::string& tag) { | |
| 405 EXPECT_TRUE(content::ExecuteScript( | |
| 406 shell_->web_contents(), | |
| 407 BuildScriptString("notifyWhenFinishedOneShot", tag))); | |
| 408 return PopConsole(BuildExpectedResult(tag, "finished")); | |
| 409 } | |
| 410 | |
| 411 bool BackgroundSyncBrowserTest::NotifyWhenFinishedImmediateOneShot( | |
| 412 const std::string& expected_msg) { | |
| 413 std::string script_result; | |
| 414 EXPECT_TRUE( | |
| 415 RunScript("notifyWhenFinishedImmediateOneShot()", &script_result)); | |
| 416 return script_result == expected_msg; | |
| 417 } | |
| 418 | |
| 419 bool BackgroundSyncBrowserTest::StoreRegistrationOneShot( | |
| 420 const std::string& tag) { | |
| 421 std::string script_result; | |
| 422 EXPECT_TRUE( | |
| 423 RunScript(BuildScriptString("storeRegistration", tag), &script_result)); | |
| 424 return script_result == BuildExpectedResult(tag, "stored"); | |
| 425 } | |
| 426 | |
| 427 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotFires) { | 383 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotFires) { |
| 428 EXPECT_TRUE(RegisterServiceWorker()); | 384 EXPECT_TRUE(RegisterServiceWorker()); |
| 429 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 385 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 430 | 386 |
| 431 EXPECT_TRUE(RegisterOneShot("foo")); | 387 EXPECT_TRUE(RegisterOneShot("foo")); |
| 432 EXPECT_TRUE(PopConsole("foo fired")); | 388 EXPECT_TRUE(PopConsole("foo fired")); |
| 433 EXPECT_FALSE(GetRegistrationOneShot("foo")); | 389 EXPECT_FALSE(GetRegistrationOneShot("foo")); |
| 434 } | 390 } |
| 435 | 391 |
| 436 // Verify that Register works in a service worker | 392 // Verify that Register works in a service worker |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 | 426 |
| 471 // Verify that it is firing. | 427 // Verify that it is firing. |
| 472 EXPECT_TRUE(GetRegistrationOneShot("delay")); | 428 EXPECT_TRUE(GetRegistrationOneShot("delay")); |
| 473 EXPECT_FALSE(OneShotPending("delay")); | 429 EXPECT_FALSE(OneShotPending("delay")); |
| 474 | 430 |
| 475 // Complete the task. | 431 // Complete the task. |
| 476 EXPECT_TRUE(CompleteDelayedOneShot()); | 432 EXPECT_TRUE(CompleteDelayedOneShot()); |
| 477 EXPECT_TRUE(PopConsole("ok - delay completed")); | 433 EXPECT_TRUE(PopConsole("ok - delay completed")); |
| 478 | 434 |
| 479 // Verify that it finished firing. | 435 // Verify that it finished firing. |
| 480 // TODO(jkarlin): Use registration.finished to verify that the event actually | |
| 481 // completed successfully. | |
| 482 EXPECT_FALSE(GetRegistrationOneShot("delay")); | 436 EXPECT_FALSE(GetRegistrationOneShot("delay")); |
| 483 } | 437 } |
| 484 | 438 |
| 485 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, WaitUntilReject) { | 439 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, WaitUntilReject) { |
| 486 EXPECT_TRUE(RegisterServiceWorker()); | 440 EXPECT_TRUE(RegisterServiceWorker()); |
| 487 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 441 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 488 | 442 |
| 489 SetOnline(true); | 443 SetOnline(true); |
| 490 EXPECT_TRUE(RegisterOneShot("delay")); | 444 EXPECT_TRUE(RegisterOneShot("delay")); |
| 491 | 445 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 std::vector<std::string> registered_tags; | 533 std::vector<std::string> registered_tags; |
| 580 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); | 534 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 581 | 535 |
| 582 SetOnline(false); | 536 SetOnline(false); |
| 583 | 537 |
| 584 EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw")); | 538 EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw")); |
| 585 EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); | 539 EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); |
| 586 EXPECT_TRUE(GetRegistrationOneShotFromServiceWorker("foo_sw")); | 540 EXPECT_TRUE(GetRegistrationOneShotFromServiceWorker("foo_sw")); |
| 587 } | 541 } |
| 588 | 542 |
| 589 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, Unregister) { | |
| 590 EXPECT_TRUE(RegisterServiceWorker()); | |
| 591 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | |
| 592 | |
| 593 SetOnline(false); | |
| 594 EXPECT_TRUE(RegisterOneShot("foo")); | |
| 595 EXPECT_TRUE(UnregisterOneShot("foo")); | |
| 596 EXPECT_FALSE(GetRegistrationOneShot("foo")); | |
| 597 } | |
| 598 | |
| 599 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, UnregisterTwice) { | |
| 600 EXPECT_TRUE(RegisterServiceWorker()); | |
| 601 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | |
| 602 | |
| 603 SetOnline(false); | |
| 604 EXPECT_TRUE(RegisterOneShot("foo")); | |
| 605 EXPECT_TRUE(UnregisterOneShotTwice("foo")); | |
| 606 EXPECT_FALSE(GetRegistrationOneShot("foo")); | |
| 607 } | |
| 608 | |
| 609 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, UnregisterMidSync) { | |
| 610 EXPECT_TRUE(RegisterServiceWorker()); | |
| 611 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | |
| 612 | |
| 613 EXPECT_TRUE(RegisterOneShot("unregister")); | |
| 614 EXPECT_TRUE(PopConsole("ok - unregister completed")); | |
| 615 } | |
| 616 | |
| 617 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, | |
| 618 CallFinishedBeforeSyncSucceeds) { | |
| 619 EXPECT_TRUE(RegisterServiceWorker()); | |
| 620 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | |
| 621 | |
| 622 SetOnline(false); | |
| 623 EXPECT_TRUE(RegisterOneShot("foo")); | |
| 624 EXPECT_TRUE(NotifyWhenFinishedOneShot("foo")); | |
| 625 | |
| 626 SetOnline(true); | |
| 627 // The ordering of PopConsole messages tells us that the event fired | |
| 628 // before finished resolved. | |
| 629 EXPECT_TRUE(PopConsole("foo fired")); | |
| 630 EXPECT_TRUE(PopConsole("foo finished result: true")); | |
| 631 } | |
| 632 | |
| 633 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, CallFinishedBeforeSyncFails) { | |
| 634 EXPECT_TRUE(RegisterServiceWorker()); | |
| 635 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | |
| 636 | |
| 637 SetOnline(true); | |
| 638 EXPECT_TRUE(RegisterOneShot("delay")); | |
| 639 EXPECT_FALSE(OneShotPending("delay")); | |
| 640 EXPECT_TRUE(NotifyWhenFinishedOneShot("delay")); | |
| 641 | |
| 642 EXPECT_TRUE(RejectDelayedOneShot()); | |
| 643 // The ordering of PopConsole messages tells us that the event fired | |
| 644 // before finished resolved. | |
| 645 EXPECT_TRUE(PopConsole("ok - delay rejected")); | |
| 646 EXPECT_TRUE(PopConsole("delay finished result: false")); | |
| 647 } | |
| 648 | |
| 649 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, | |
| 650 CallFinishedAfterSyncSucceeds) { | |
| 651 EXPECT_TRUE(RegisterServiceWorker()); | |
| 652 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | |
| 653 | |
| 654 SetOnline(false); | |
| 655 EXPECT_TRUE(RegisterOneShot("foo")); | |
| 656 EXPECT_TRUE(StoreRegistrationOneShot("foo")); | |
| 657 | |
| 658 SetOnline(true); | |
| 659 EXPECT_TRUE(PopConsole("foo fired")); | |
| 660 EXPECT_FALSE(GetRegistrationOneShot("foo")); | |
| 661 EXPECT_TRUE(NotifyWhenFinishedImmediateOneShot("ok - foo result: true")); | |
| 662 } | |
| 663 | |
| 664 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, | |
| 665 CallFinishedAfterSyncUnregistered) { | |
| 666 EXPECT_TRUE(RegisterServiceWorker()); | |
| 667 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | |
| 668 | |
| 669 SetOnline(false); | |
| 670 EXPECT_TRUE(RegisterOneShot("foo")); | |
| 671 EXPECT_TRUE(StoreRegistrationOneShot("foo")); | |
| 672 EXPECT_TRUE(UnregisterOneShot("foo")); | |
| 673 EXPECT_FALSE(GetRegistrationOneShot("foo")); | |
| 674 EXPECT_TRUE(NotifyWhenFinishedImmediateOneShot("ok - foo result: false")); | |
| 675 } | |
| 676 | |
| 677 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, CallFinishedAfterSyncFails) { | |
| 678 EXPECT_TRUE(RegisterServiceWorker()); | |
| 679 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | |
| 680 | |
| 681 SetOnline(true); | |
| 682 EXPECT_TRUE(RegisterOneShot("delay")); | |
| 683 EXPECT_FALSE(OneShotPending("delay")); | |
| 684 EXPECT_TRUE(StoreRegistrationOneShot("delay")); | |
| 685 | |
| 686 EXPECT_TRUE(RejectDelayedOneShot()); | |
| 687 EXPECT_TRUE(PopConsole("ok - delay rejected")); | |
| 688 EXPECT_TRUE(NotifyWhenFinishedImmediateOneShot("ok - delay result: false")); | |
| 689 } | |
| 690 | |
| 691 // Verify that a background sync registration is deleted when site data is | 543 // Verify that a background sync registration is deleted when site data is |
| 692 // cleared. | 544 // cleared. |
| 693 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, | 545 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 694 SyncRegistrationDeletedWhenClearingSiteData) { | 546 SyncRegistrationDeletedWhenClearingSiteData) { |
| 695 EXPECT_TRUE(RegisterServiceWorker()); | 547 EXPECT_TRUE(RegisterServiceWorker()); |
| 696 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 548 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 697 | 549 |
| 698 // Prevent firing by going offline. | 550 // Prevent firing by going offline. |
| 699 SetOnline(false); | 551 SetOnline(false); |
| 700 EXPECT_TRUE(RegisterOneShot("foo")); | 552 EXPECT_TRUE(RegisterOneShot("foo")); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 791 | 643 |
| 792 EXPECT_TRUE(RegisterOneShot("delay")); | 644 EXPECT_TRUE(RegisterOneShot("delay")); |
| 793 EXPECT_TRUE(RejectDelayedOneShot()); | 645 EXPECT_TRUE(RejectDelayedOneShot()); |
| 794 EXPECT_TRUE(PopConsole("ok - delay rejected")); | 646 EXPECT_TRUE(PopConsole("ok - delay rejected")); |
| 795 | 647 |
| 796 // Verify that the oneshot is still around and waiting to try again. | 648 // Verify that the oneshot is still around and waiting to try again. |
| 797 EXPECT_TRUE(OneShotPending("delay")); | 649 EXPECT_TRUE(OneShotPending("delay")); |
| 798 } | 650 } |
| 799 | 651 |
| 800 } // namespace content | 652 } // namespace content |
| OLD | NEW |