Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(476)

Side by Side Diff: chrome/browser/push_messaging/push_messaging_browsertest.cc

Issue 2675293003: Push API: Don't wait for network when unsubscribing (Closed)
Patch Set: Address peter's review comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 10
(...skipping 551 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 "gcm_sender_id not found in manifest", 562 "gcm_sender_id not found in manifest",
563 script_result); 563 script_result);
564 564
565 // Now run the subscribe with a key. This should succeed. 565 // Now run the subscribe with a key. This should succeed.
566 ASSERT_TRUE(RunScript("workerSubscribePush()", &script_result)); 566 ASSERT_TRUE(RunScript("workerSubscribePush()", &script_result));
567 ASSERT_NO_FATAL_FAILURE( 567 ASSERT_NO_FATAL_FAILURE(
568 EndpointToToken(script_result, true /* standard_protocol */)); 568 EndpointToToken(script_result, true /* standard_protocol */));
569 569
570 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 570 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
571 EXPECT_EQ("unsubscribe result: true", script_result); 571 EXPECT_EQ("unsubscribe result: true", script_result);
572 EXPECT_NE(push_service(), GetAppHandler());
573 } 572 }
574 573
575 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 574 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
576 ResubscribeWithoutKeyAfterSubscribingWithKeyInManifest) { 575 ResubscribeWithoutKeyAfterSubscribingWithKeyInManifest) {
577 std::string script_result; 576 std::string script_result;
578 577
579 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 578 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
580 ASSERT_EQ("ok - service worker registered", script_result); 579 ASSERT_EQ("ok - service worker registered", script_result);
581 580
582 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission()); 581 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
(...skipping 25 matching lines...) Expand all
608 // Now run the subscribe from the service worker without a key. 607 // Now run the subscribe from the service worker without a key.
609 // In this case, the sender id should be read from the datastore. 608 // In this case, the sender id should be read from the datastore.
610 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 609 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
611 std::string token2; 610 std::string token2;
612 ASSERT_NO_FATAL_FAILURE( 611 ASSERT_NO_FATAL_FAILURE(
613 EndpointToToken(script_result, false /* standard_protocol */, &token2)); 612 EndpointToToken(script_result, false /* standard_protocol */, &token2));
614 EXPECT_EQ(token1, token2); 613 EXPECT_EQ(token1, token2);
615 614
616 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 615 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
617 EXPECT_EQ("unsubscribe result: true", script_result); 616 EXPECT_EQ("unsubscribe result: true", script_result);
618 EXPECT_NE(push_service(), GetAppHandler());
619 617
620 // After unsubscribing, subscribe again from the worker with no key. 618 // After unsubscribing, subscribe again from the worker with no key.
621 // The sender id should again be read from the datastore, so the 619 // The sender id should again be read from the datastore, so the
622 // subcribe should succeed, and we should get a new subscription token. 620 // subcribe should succeed, and we should get a new subscription token.
623 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 621 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
624 std::string token3; 622 std::string token3;
625 ASSERT_NO_FATAL_FAILURE( 623 ASSERT_NO_FATAL_FAILURE(
626 EndpointToToken(script_result, false /* standard_protocol */, &token3)); 624 EndpointToToken(script_result, false /* standard_protocol */, &token3));
627 EXPECT_NE(token1, token3); 625 EXPECT_NE(token1, token3);
628 626
629 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 627 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
630 EXPECT_EQ("unsubscribe result: true", script_result); 628 EXPECT_EQ("unsubscribe result: true", script_result);
631 EXPECT_NE(push_service(), GetAppHandler());
632 } 629 }
633 630
634 IN_PROC_BROWSER_TEST_F( 631 IN_PROC_BROWSER_TEST_F(
635 PushMessagingBrowserTest, 632 PushMessagingBrowserTest,
636 ResubscribeWithoutKeyAfterSubscribingFromDocumentWithP256Key) { 633 ResubscribeWithoutKeyAfterSubscribingFromDocumentWithP256Key) {
637 std::string script_result; 634 std::string script_result;
638 635
639 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 636 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
640 ASSERT_EQ("ok - service worker registered", script_result); 637 ASSERT_EQ("ok - service worker registered", script_result);
641 638
(...skipping 18 matching lines...) Expand all
660 // Now try to resubscribe from the service worker without a key. 657 // Now try to resubscribe from the service worker without a key.
661 // This should also fail as the original key was not numeric. 658 // This should also fail as the original key was not numeric.
662 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 659 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
663 EXPECT_EQ( 660 EXPECT_EQ(
664 "AbortError - Registration failed - missing applicationServerKey, " 661 "AbortError - Registration failed - missing applicationServerKey, "
665 "and gcm_sender_id not found in manifest", 662 "and gcm_sender_id not found in manifest",
666 script_result); 663 script_result);
667 664
668 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 665 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
669 EXPECT_EQ("unsubscribe result: true", script_result); 666 EXPECT_EQ("unsubscribe result: true", script_result);
670 EXPECT_NE(push_service(), GetAppHandler());
671 667
672 // After unsubscribing, try to resubscribe again without a key. 668 // After unsubscribing, try to resubscribe again without a key.
673 // This should again fail. 669 // This should again fail.
674 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 670 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
675 EXPECT_EQ( 671 EXPECT_EQ(
676 "AbortError - Registration failed - missing applicationServerKey, " 672 "AbortError - Registration failed - missing applicationServerKey, "
677 "and gcm_sender_id not found in manifest", 673 "and gcm_sender_id not found in manifest",
678 script_result); 674 script_result);
679 } 675 }
680 676
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
763 // manifest, not a numeric applicationServerKey, but for code simplicity 759 // manifest, not a numeric applicationServerKey, but for code simplicity
764 // this case is allowed. 760 // this case is allowed.
765 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 761 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
766 std::string token2; 762 std::string token2;
767 ASSERT_NO_FATAL_FAILURE( 763 ASSERT_NO_FATAL_FAILURE(
768 EndpointToToken(script_result, false /* standard_protocol */, &token2)); 764 EndpointToToken(script_result, false /* standard_protocol */, &token2));
769 EXPECT_EQ(token1, token2); 765 EXPECT_EQ(token1, token2);
770 766
771 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 767 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
772 EXPECT_EQ("unsubscribe result: true", script_result); 768 EXPECT_EQ("unsubscribe result: true", script_result);
773 EXPECT_NE(push_service(), GetAppHandler());
774 769
775 // After unsubscribing, subscribe again from the worker with no key. 770 // After unsubscribing, subscribe again from the worker with no key.
776 // The sender id should again be read from the datastore, so the 771 // The sender id should again be read from the datastore, so the
777 // subcribe should succeed, and we should get a new subscription token. 772 // subcribe should succeed, and we should get a new subscription token.
778 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 773 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
779 std::string token3; 774 std::string token3;
780 ASSERT_NO_FATAL_FAILURE( 775 ASSERT_NO_FATAL_FAILURE(
781 EndpointToToken(script_result, false /* standard_protocol */, &token3)); 776 EndpointToToken(script_result, false /* standard_protocol */, &token3));
782 EXPECT_NE(token1, token3); 777 EXPECT_NE(token1, token3);
783 778
784 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 779 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
785 EXPECT_EQ("unsubscribe result: true", script_result); 780 EXPECT_EQ("unsubscribe result: true", script_result);
786 EXPECT_NE(push_service(), GetAppHandler());
787 } 781 }
788 782
789 IN_PROC_BROWSER_TEST_F( 783 IN_PROC_BROWSER_TEST_F(
790 PushMessagingBrowserTest, 784 PushMessagingBrowserTest,
791 ResubscribeWithoutKeyAfterSubscribingFromWorkerWithNumber) { 785 ResubscribeWithoutKeyAfterSubscribingFromWorkerWithNumber) {
792 std::string script_result; 786 std::string script_result;
793 787
794 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 788 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
795 ASSERT_EQ("ok - service worker registered", script_result); 789 ASSERT_EQ("ok - service worker registered", script_result);
796 790
(...skipping 25 matching lines...) Expand all
822 // manifest, not a numeric applicationServerKey, but for code simplicity 816 // manifest, not a numeric applicationServerKey, but for code simplicity
823 // this case is allowed. 817 // this case is allowed.
824 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 818 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
825 std::string token2; 819 std::string token2;
826 ASSERT_NO_FATAL_FAILURE( 820 ASSERT_NO_FATAL_FAILURE(
827 EndpointToToken(script_result, false /* standard_protocol */, &token2)); 821 EndpointToToken(script_result, false /* standard_protocol */, &token2));
828 EXPECT_EQ(token1, token2); 822 EXPECT_EQ(token1, token2);
829 823
830 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 824 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
831 EXPECT_EQ("unsubscribe result: true", script_result); 825 EXPECT_EQ("unsubscribe result: true", script_result);
832 EXPECT_NE(push_service(), GetAppHandler());
833 826
834 // After unsubscribing, subscribe again from the worker with no key. 827 // After unsubscribing, subscribe again from the worker with no key.
835 // The sender id should again be read from the datastore, so the 828 // The sender id should again be read from the datastore, so the
836 // subcribe should succeed, and we should get a new subscription token. 829 // subcribe should succeed, and we should get a new subscription token.
837 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 830 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
838 std::string token3; 831 std::string token3;
839 ASSERT_NO_FATAL_FAILURE( 832 ASSERT_NO_FATAL_FAILURE(
840 EndpointToToken(script_result, false /* standard_protocol */, &token3)); 833 EndpointToToken(script_result, false /* standard_protocol */, &token3));
841 EXPECT_NE(token1, token3); 834 EXPECT_NE(token1, token3);
842 835
843 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 836 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
844 EXPECT_EQ("unsubscribe result: true", script_result); 837 EXPECT_EQ("unsubscribe result: true", script_result);
845 EXPECT_NE(push_service(), GetAppHandler());
846 } 838 }
847 839
848 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, ResubscribeWithMismatchedKey) { 840 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, ResubscribeWithMismatchedKey) {
849 std::string script_result; 841 std::string script_result;
850 842
851 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 843 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
852 ASSERT_EQ("ok - service worker registered", script_result); 844 ASSERT_EQ("ok - service worker registered", script_result);
853 845
854 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission()); 846 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
855 847
(...skipping 22 matching lines...) Expand all
878 // Try to resubscribe with the original key - should succeed. 870 // Try to resubscribe with the original key - should succeed.
879 ASSERT_TRUE( 871 ASSERT_TRUE(
880 RunScript("workerSubscribePushWithNumericKey('11111')", &script_result)); 872 RunScript("workerSubscribePushWithNumericKey('11111')", &script_result));
881 std::string token2; 873 std::string token2;
882 ASSERT_NO_FATAL_FAILURE( 874 ASSERT_NO_FATAL_FAILURE(
883 EndpointToToken(script_result, false /* standard_protocol */, &token2)); 875 EndpointToToken(script_result, false /* standard_protocol */, &token2));
884 EXPECT_EQ(token1, token2); 876 EXPECT_EQ(token1, token2);
885 877
886 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 878 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
887 EXPECT_EQ("unsubscribe result: true", script_result); 879 EXPECT_EQ("unsubscribe result: true", script_result);
888 EXPECT_NE(push_service(), GetAppHandler());
889 880
890 // Resubscribe with a different key after unsubscribing. 881 // Resubscribe with a different key after unsubscribing.
891 // Should succeed, and we should get a new subscription token. 882 // Should succeed, and we should get a new subscription token.
892 ASSERT_TRUE( 883 ASSERT_TRUE(
893 RunScript("workerSubscribePushWithNumericKey('22222')", &script_result)); 884 RunScript("workerSubscribePushWithNumericKey('22222')", &script_result));
894 std::string token3; 885 std::string token3;
895 ASSERT_NO_FATAL_FAILURE( 886 ASSERT_NO_FATAL_FAILURE(
896 EndpointToToken(script_result, false /* standard_protocol */, &token3)); 887 EndpointToToken(script_result, false /* standard_protocol */, &token3));
897 EXPECT_NE(token1, token3); 888 EXPECT_NE(token1, token3);
898 889
899 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 890 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
900 EXPECT_EQ("unsubscribe result: true", script_result); 891 EXPECT_EQ("unsubscribe result: true", script_result);
901 EXPECT_NE(push_service(), GetAppHandler());
902 } 892 }
903 893
904 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribePersisted) { 894 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribePersisted) {
905 std::string script_result; 895 std::string script_result;
906 896
907 // First, test that Service Worker registration IDs are assigned in order of 897 // First, test that Service Worker registration IDs are assigned in order of
908 // registering the Service Workers, and the (fake) push subscription ids are 898 // registering the Service Workers, and the (fake) push subscription ids are
909 // assigned in order of push subscription (even when these orders are 899 // assigned in order of push subscription (even when these orders are
910 // different). 900 // different).
911 901
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
976 EXPECT_NE(push_service(), GetAppHandler()); 966 EXPECT_NE(push_service(), GetAppHandler());
977 ASSERT_NO_FATAL_FAILURE(RestartPushService()); 967 ASSERT_NO_FATAL_FAILURE(RestartPushService());
978 EXPECT_NE(push_service(), GetAppHandler()); 968 EXPECT_NE(push_service(), GetAppHandler());
979 969
980 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully()); 970 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
981 971
982 EXPECT_EQ(push_service(), GetAppHandler()); 972 EXPECT_EQ(push_service(), GetAppHandler());
983 ASSERT_NO_FATAL_FAILURE(RestartPushService()); 973 ASSERT_NO_FATAL_FAILURE(RestartPushService());
984 EXPECT_EQ(push_service(), GetAppHandler()); 974 EXPECT_EQ(push_service(), GetAppHandler());
985 975
976 std::string script_result;
977
986 // Unsubscribe. 978 // Unsubscribe.
987 std::string script_result; 979 base::RunLoop run_loop;
980 push_service()->SetUnsubscribeCallbackForTesting(run_loop.QuitClosure());
988 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 981 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
989 EXPECT_EQ("unsubscribe result: true", script_result); 982 EXPECT_EQ("unsubscribe result: true", script_result);
983 // The app handler is only guaranteed to be unregistered once the unsubscribe
984 // callback for testing has been run (PushSubscription.unsubscribe() usually
985 // resolves before that, in order to avoid blocking on network retries etc).
986 run_loop.Run();
990 987
991 EXPECT_NE(push_service(), GetAppHandler()); 988 EXPECT_NE(push_service(), GetAppHandler());
992 ASSERT_NO_FATAL_FAILURE(RestartPushService()); 989 ASSERT_NO_FATAL_FAILURE(RestartPushService());
993 EXPECT_NE(push_service(), GetAppHandler()); 990 EXPECT_NE(push_service(), GetAppHandler());
994 } 991 }
995 992
996 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventSuccess) { 993 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventSuccess) {
997 std::string script_result; 994 std::string script_result;
998 995
999 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully()); 996 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
(...skipping 688 matching lines...) Expand 10 before | Expand all | Expand 10 after
1688 histogram_tester_.ExpectBucketCount( 1685 histogram_tester_.ExpectBucketCount(
1689 "PushMessaging.UnregistrationReason", 1686 "PushMessaging.UnregistrationReason",
1690 content::PUSH_UNREGISTRATION_REASON_SERVICE_WORKER_UNREGISTERED, 1); 1687 content::PUSH_UNREGISTRATION_REASON_SERVICE_WORKER_UNREGISTERED, 1);
1691 histogram_tester_.ExpectTotalCount("PushMessaging.UnregistrationReason", 6); 1688 histogram_tester_.ExpectTotalCount("PushMessaging.UnregistrationReason", 6);
1692 1689
1693 // Now manual unsubscribe should return false. 1690 // Now manual unsubscribe should return false.
1694 ASSERT_TRUE(RunScript("unsubscribeStoredPushSubscription()", &script_result)); 1691 ASSERT_TRUE(RunScript("unsubscribeStoredPushSubscription()", &script_result));
1695 EXPECT_EQ("unsubscribe result: false", script_result); 1692 EXPECT_EQ("unsubscribe result: false", script_result);
1696 } 1693 }
1697 1694
1695 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, UnsubscribeOffline) {
1696 std::string script_result;
1697
1698 EXPECT_NE(push_service(), GetAppHandler());
1699
1700 std::string token;
1701 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully(true /* use_key */, &token));
1702
1703 gcm_service_->set_offline(true);
1704
1705 // Should quickly resolve true after deleting local state (rather than waiting
1706 // until unsubscribing over the network exceeds the maximum backoff duration).
1707 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1708 EXPECT_EQ("unsubscribe result: true", script_result);
1709 histogram_tester_.ExpectUniqueSample(
1710 "PushMessaging.UnregistrationReason",
1711 content::PUSH_UNREGISTRATION_REASON_JAVASCRIPT_API, 1);
1712
1713 // Since the service is offline, the network request to GCM is still being
1714 // retried, so the app handler shouldn't have been unregistered yet.
1715 EXPECT_EQ(push_service(), GetAppHandler());
1716 // But restarting the push service will unregister the app handler, since the
1717 // subscription is no longer stored in the PushMessagingAppIdentifier map.
1718 ASSERT_NO_FATAL_FAILURE(RestartPushService());
1719 EXPECT_NE(push_service(), GetAppHandler());
1720 }
1721
1698 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 1722 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1699 UnregisteringServiceWorkerUnsubscribes) { 1723 UnregisteringServiceWorkerUnsubscribes) {
1700 std::string script_result; 1724 std::string script_result;
1701 1725
1702 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully()); 1726 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1703 1727
1704 LoadTestPage(); // Reload to become controlled. 1728 LoadTestPage(); // Reload to become controlled.
1705 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 1729 ASSERT_TRUE(RunScript("isControlled()", &script_result));
1706 ASSERT_EQ("true - is controlled", script_result); 1730 ASSERT_EQ("true - is controlled", script_result);
1707 1731
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after
2123 BackgroundModeManager* background_mode_manager = 2147 BackgroundModeManager* background_mode_manager =
2124 g_browser_process->background_mode_manager(); 2148 g_browser_process->background_mode_manager();
2125 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 2149 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
2126 2150
2127 // Once there is a push subscription background mode is still inactive. 2151 // Once there is a push subscription background mode is still inactive.
2128 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully()); 2152 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
2129 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 2153 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
2130 2154
2131 // After dropping the last subscription it is still inactive. 2155 // After dropping the last subscription it is still inactive.
2132 std::string script_result; 2156 std::string script_result;
2157 base::RunLoop run_loop;
2158 push_service()->SetUnsubscribeCallbackForTesting(run_loop.QuitClosure());
2133 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 2159 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
2134 EXPECT_EQ("unsubscribe result: true", script_result); 2160 EXPECT_EQ("unsubscribe result: true", script_result);
2161 // Background mode is only guaranteed to have updated once the unsubscribe
2162 // callback for testing has been run (PushSubscription.unsubscribe() usually
2163 // resolves before that, in order to avoid blocking on network retries etc).
2164 run_loop.Run();
2135 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 2165 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
2136 } 2166 }
2137 2167
2138 class PushMessagingBackgroundModeEnabledBrowserTest 2168 class PushMessagingBackgroundModeEnabledBrowserTest
2139 : public PushMessagingBrowserTest { 2169 : public PushMessagingBrowserTest {
2140 public: 2170 public:
2141 ~PushMessagingBackgroundModeEnabledBrowserTest() override {} 2171 ~PushMessagingBackgroundModeEnabledBrowserTest() override {}
2142 2172
2143 // PushMessagingBrowserTest: 2173 // PushMessagingBrowserTest:
2144 void SetUpCommandLine(base::CommandLine* command_line) override { 2174 void SetUpCommandLine(base::CommandLine* command_line) override {
2145 command_line->AppendSwitch(switches::kEnablePushApiBackgroundMode); 2175 command_line->AppendSwitch(switches::kEnablePushApiBackgroundMode);
2146 PushMessagingBrowserTest::SetUpCommandLine(command_line); 2176 PushMessagingBrowserTest::SetUpCommandLine(command_line);
2147 } 2177 }
2148 }; 2178 };
2149 2179
2150 // In this test the command line enables push background mode. 2180 // In this test the command line enables push background mode.
2151 IN_PROC_BROWSER_TEST_F(PushMessagingBackgroundModeEnabledBrowserTest, 2181 IN_PROC_BROWSER_TEST_F(PushMessagingBackgroundModeEnabledBrowserTest,
2152 BackgroundModeEnabledWithCommandLine) { 2182 BackgroundModeEnabledWithCommandLine) {
2153 // Initially background mode is inactive. 2183 // Initially background mode is inactive.
2154 BackgroundModeManager* background_mode_manager = 2184 BackgroundModeManager* background_mode_manager =
2155 g_browser_process->background_mode_manager(); 2185 g_browser_process->background_mode_manager();
2156 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 2186 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
2157 2187
2158 // Once there is a push subscription background mode is active. 2188 // Once there is a push subscription background mode is active.
2159 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully()); 2189 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
2160 ASSERT_TRUE(background_mode_manager->IsBackgroundModeActive()); 2190 ASSERT_TRUE(background_mode_manager->IsBackgroundModeActive());
2161 2191
2162 // Dropping the last subscription deactivates background mode. 2192 // Dropping the last subscription deactivates background mode.
2163 std::string script_result; 2193 std::string script_result;
2194 base::RunLoop run_loop;
2195 push_service()->SetUnsubscribeCallbackForTesting(run_loop.QuitClosure());
2164 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 2196 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
2165 EXPECT_EQ("unsubscribe result: true", script_result); 2197 EXPECT_EQ("unsubscribe result: true", script_result);
2198 // Background mode is only guaranteed to have updated once the unsubscribe
2199 // callback for testing has been run (PushSubscription.unsubscribe() usually
2200 // resolves before that, in order to avoid blocking on network retries etc).
2201 run_loop.Run();
2166 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 2202 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
2167 } 2203 }
2168 2204
2169 class PushMessagingBackgroundModeDisabledBrowserTest 2205 class PushMessagingBackgroundModeDisabledBrowserTest
2170 : public PushMessagingBrowserTest { 2206 : public PushMessagingBrowserTest {
2171 public: 2207 public:
2172 ~PushMessagingBackgroundModeDisabledBrowserTest() override {} 2208 ~PushMessagingBackgroundModeDisabledBrowserTest() override {}
2173 2209
2174 // PushMessagingBrowserTest: 2210 // PushMessagingBrowserTest:
2175 void SetUpCommandLine(base::CommandLine* command_line) override { 2211 void SetUpCommandLine(base::CommandLine* command_line) override {
2176 command_line->AppendSwitch(switches::kDisablePushApiBackgroundMode); 2212 command_line->AppendSwitch(switches::kDisablePushApiBackgroundMode);
2177 PushMessagingBrowserTest::SetUpCommandLine(command_line); 2213 PushMessagingBrowserTest::SetUpCommandLine(command_line);
2178 } 2214 }
2179 }; 2215 };
2180 2216
2181 // In this test the command line disables push background mode. 2217 // In this test the command line disables push background mode.
2182 IN_PROC_BROWSER_TEST_F(PushMessagingBackgroundModeDisabledBrowserTest, 2218 IN_PROC_BROWSER_TEST_F(PushMessagingBackgroundModeDisabledBrowserTest,
2183 BackgroundModeDisabledWithCommandLine) { 2219 BackgroundModeDisabledWithCommandLine) {
2184 // Initially background mode is inactive. 2220 // Initially background mode is inactive.
2185 BackgroundModeManager* background_mode_manager = 2221 BackgroundModeManager* background_mode_manager =
2186 g_browser_process->background_mode_manager(); 2222 g_browser_process->background_mode_manager();
2187 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 2223 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
2188 2224
2189 // Once there is a push subscription background mode is still inactive. 2225 // Once there is a push subscription background mode is still inactive.
2190 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully()); 2226 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
2191 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 2227 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
2192 2228
2193 // After dropping the last subscription background mode is still inactive. 2229 // After dropping the last subscription background mode is still inactive.
2194 std::string script_result; 2230 std::string script_result;
2231 base::RunLoop run_loop;
2232 push_service()->SetUnsubscribeCallbackForTesting(run_loop.QuitClosure());
2195 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 2233 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
2196 EXPECT_EQ("unsubscribe result: true", script_result); 2234 EXPECT_EQ("unsubscribe result: true", script_result);
2235 // Background mode is only guaranteed to have updated once the unsubscribe
2236 // callback for testing has been run (PushSubscription.unsubscribe() usually
2237 // resolves before that, in order to avoid blocking on network retries etc).
2238 run_loop.Run();
2197 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 2239 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
2198 } 2240 }
2199 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) 2241 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS)
OLDNEW
« no previous file with comments | « chrome/browser/gcm/fake_gcm_profile_service.cc ('k') | chrome/browser/push_messaging/push_messaging_service_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698