| OLD | NEW |
| 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 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 180 |
| 181 InProcessBrowserTest::TearDown(); | 181 InProcessBrowserTest::TearDown(); |
| 182 } | 182 } |
| 183 | 183 |
| 184 void LoadTestPage(const std::string& path) { | 184 void LoadTestPage(const std::string& path) { |
| 185 ui_test_utils::NavigateToURL(GetBrowser(), https_server_->GetURL(path)); | 185 ui_test_utils::NavigateToURL(GetBrowser(), https_server_->GetURL(path)); |
| 186 } | 186 } |
| 187 | 187 |
| 188 void LoadTestPage() { LoadTestPage(GetTestURL()); } | 188 void LoadTestPage() { LoadTestPage(GetTestURL()); } |
| 189 | 189 |
| 190 void LoadTestPageWithoutManifest() { LoadTestPage(GetNoManifestTestURL()); } |
| 191 |
| 190 bool RunScript(const std::string& script, std::string* result) { | 192 bool RunScript(const std::string& script, std::string* result) { |
| 191 return RunScript(script, result, nullptr); | 193 return RunScript(script, result, nullptr); |
| 192 } | 194 } |
| 193 | 195 |
| 194 bool RunScript(const std::string& script, std::string* result, | 196 bool RunScript(const std::string& script, std::string* result, |
| 195 content::WebContents* web_contents) { | 197 content::WebContents* web_contents) { |
| 196 if (!web_contents) | 198 if (!web_contents) |
| 197 web_contents = GetBrowser()->tab_strip_model()->GetActiveWebContents(); | 199 web_contents = GetBrowser()->tab_strip_model()->GetActiveWebContents(); |
| 198 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(), | 200 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(), |
| 199 script, result); | 201 script, result); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 | 266 |
| 265 void SetSiteEngagementScore(const GURL& url, double score) { | 267 void SetSiteEngagementScore(const GURL& url, double score) { |
| 266 SiteEngagementService* service = | 268 SiteEngagementService* service = |
| 267 SiteEngagementService::Get(GetBrowser()->profile()); | 269 SiteEngagementService::Get(GetBrowser()->profile()); |
| 268 service->ResetScoreForURL(url, score); | 270 service->ResetScoreForURL(url, score); |
| 269 } | 271 } |
| 270 | 272 |
| 271 protected: | 273 protected: |
| 272 virtual std::string GetTestURL() { return "/push_messaging/test.html"; } | 274 virtual std::string GetTestURL() { return "/push_messaging/test.html"; } |
| 273 | 275 |
| 276 virtual std::string GetNoManifestTestURL() { |
| 277 return "/push_messaging/test_no_manifest.html"; |
| 278 } |
| 279 |
| 274 virtual Browser* GetBrowser() const { return browser(); } | 280 virtual Browser* GetBrowser() const { return browser(); } |
| 275 | 281 |
| 276 gcm::FakeGCMProfileService* gcm_service_; | 282 gcm::FakeGCMProfileService* gcm_service_; |
| 277 instance_id::FakeGCMDriverForInstanceID* gcm_driver_; | 283 instance_id::FakeGCMDriverForInstanceID* gcm_driver_; |
| 278 base::HistogramTester histogram_tester_; | 284 base::HistogramTester histogram_tester_; |
| 279 | 285 |
| 280 private: | 286 private: |
| 281 std::unique_ptr<net::EmbeddedTestServer> https_server_; | 287 std::unique_ptr<net::EmbeddedTestServer> https_server_; |
| 282 PushMessagingServiceImpl* push_service_; | 288 PushMessagingServiceImpl* push_service_; |
| 283 | 289 |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 532 | 538 |
| 533 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | 539 ASSERT_TRUE(RunScript("isControlled()", &script_result)); |
| 534 ASSERT_EQ("true - is controlled", script_result); | 540 ASSERT_EQ("true - is controlled", script_result); |
| 535 | 541 |
| 536 // Try to subscribe from a worker without a key. This should fail. | 542 // Try to subscribe from a worker without a key. This should fail. |
| 537 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); | 543 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); |
| 538 EXPECT_EQ( | 544 EXPECT_EQ( |
| 539 "AbortError - Registration failed - missing applicationServerKey, and " | 545 "AbortError - Registration failed - missing applicationServerKey, and " |
| 540 "gcm_sender_id not found in manifest", | 546 "gcm_sender_id not found in manifest", |
| 541 script_result); | 547 script_result); |
| 542 // Now run the subscribe from the service worker with a key. This | 548 |
| 543 // should succeed, and write the key to the datastore. | 549 // Now run the subscribe with a key. This should succeed. |
| 544 ASSERT_TRUE(RunScript("workerSubscribePush()", &script_result)); | 550 ASSERT_TRUE(RunScript("workerSubscribePush()", &script_result)); |
| 545 std::string token1; | |
| 546 ASSERT_NO_FATAL_FAILURE( | 551 ASSERT_NO_FATAL_FAILURE( |
| 547 EndpointToToken(script_result, true /* standard_protocol */, &token1)); | 552 EndpointToToken(script_result, true /* standard_protocol */)); |
| 548 | |
| 549 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); | |
| 550 EXPECT_EQ("unsubscribe result: true", script_result); | |
| 551 EXPECT_NE(push_service(), GetAppHandler()); | |
| 552 | |
| 553 // Now run the subscribe from the service worker without a key. | |
| 554 // In this case, the key will be read from the datastore. | |
| 555 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); | |
| 556 std::string token2; | |
| 557 ASSERT_NO_FATAL_FAILURE( | |
| 558 EndpointToToken(script_result, true /* standard_protocol */, &token2)); | |
| 559 EXPECT_NE(token1, token2); | |
| 560 | 553 |
| 561 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); | 554 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); |
| 562 EXPECT_EQ("unsubscribe result: true", script_result); | 555 EXPECT_EQ("unsubscribe result: true", script_result); |
| 563 EXPECT_NE(push_service(), GetAppHandler()); | 556 EXPECT_NE(push_service(), GetAppHandler()); |
| 564 } | 557 } |
| 565 | 558 |
| 566 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeWorkerUsingManifest) { | 559 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, |
| 560 ResubscribeWithoutKeyAfterSubscribingWithKeyInManifest) { |
| 567 std::string script_result; | 561 std::string script_result; |
| 568 | 562 |
| 569 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | 563 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 564 ASSERT_EQ("ok - service worker registered", script_result); |
| 565 |
| 566 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission()); |
| 567 |
| 568 LoadTestPage(); // Reload to become controlled. |
| 569 |
| 570 ASSERT_TRUE(RunScript("isControlled()", &script_result)); |
| 571 ASSERT_EQ("true - is controlled", script_result); |
| 572 |
| 573 // Run the subscription from the document without a key, this will trigger |
| 574 // the code to read sender id from the manifest and will write it to the |
| 575 // datastore. |
| 576 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result)); |
| 577 std::string token1; |
| 578 ASSERT_NO_FATAL_FAILURE( |
| 579 EndpointToToken(script_result, false /* standard_protocol */, &token1)); |
| 580 |
| 581 ASSERT_TRUE(RunScript("removeManifest()", &script_result)); |
| 582 ASSERT_EQ("manifest removed", script_result); |
| 583 |
| 584 // Try to resubscribe from the document without a key or manifest. |
| 585 // This should fail. |
| 586 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result)); |
| 587 EXPECT_EQ( |
| 588 "AbortError - Registration failed - missing applicationServerKey, " |
| 589 "and manifest empty or missing", |
| 590 script_result); |
| 591 |
| 592 // Now run the subscribe from the service worker without a key. |
| 593 // In this case, the sender id should be read from the datastore. |
| 594 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); |
| 595 std::string token2; |
| 596 ASSERT_NO_FATAL_FAILURE( |
| 597 EndpointToToken(script_result, false /* standard_protocol */, &token2)); |
| 598 EXPECT_EQ(token1, token2); |
| 599 |
| 600 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); |
| 601 EXPECT_EQ("unsubscribe result: true", script_result); |
| 602 EXPECT_NE(push_service(), GetAppHandler()); |
| 603 |
| 604 // After unsubscribing, subscribe again from the worker with no key. |
| 605 // The sender id should again be read from the datastore, so the |
| 606 // subcribe should succeed, and we should get a new subscription token. |
| 607 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); |
| 608 std::string token3; |
| 609 ASSERT_NO_FATAL_FAILURE( |
| 610 EndpointToToken(script_result, false /* standard_protocol */, &token3)); |
| 611 EXPECT_NE(token1, token3); |
| 612 |
| 613 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); |
| 614 EXPECT_EQ("unsubscribe result: true", script_result); |
| 615 EXPECT_NE(push_service(), GetAppHandler()); |
| 616 } |
| 617 |
| 618 IN_PROC_BROWSER_TEST_F( |
| 619 PushMessagingBrowserTest, |
| 620 ResubscribeWithoutKeyAfterSubscribingFromDocumentWithP256Key) { |
| 621 std::string script_result; |
| 622 |
| 623 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 570 ASSERT_EQ("ok - service worker registered", script_result); | 624 ASSERT_EQ("ok - service worker registered", script_result); |
| 571 | 625 |
| 572 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission()); | 626 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission()); |
| 573 | 627 |
| 574 LoadTestPage(); // Reload to become controlled. | 628 LoadTestPageWithoutManifest(); // Reload to become controlled. |
| 575 | 629 |
| 576 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | 630 ASSERT_TRUE(RunScript("isControlled()", &script_result)); |
| 577 ASSERT_EQ("true - is controlled", script_result); | 631 ASSERT_EQ("true - is controlled", script_result); |
| 578 | 632 |
| 579 // Try to subscribe from a worker without a key. This should fail. | 633 // Run the subscription from the document with a key. |
| 634 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result)); |
| 635 ASSERT_NO_FATAL_FAILURE(EndpointToToken(script_result)); |
| 636 |
| 637 // Try to resubscribe from the document without a key - should fail. |
| 638 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result)); |
| 639 EXPECT_EQ( |
| 640 "AbortError - Registration failed - missing applicationServerKey, " |
| 641 "and manifest empty or missing", |
| 642 script_result); |
| 643 |
| 644 // Now try to resubscribe from the service worker without a key. |
| 645 // This should also fail as the original key was not numeric. |
| 646 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); |
| 647 EXPECT_EQ( |
| 648 "AbortError - Registration failed - missing applicationServerKey, " |
| 649 "and gcm_sender_id not found in manifest", |
| 650 script_result); |
| 651 |
| 652 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); |
| 653 EXPECT_EQ("unsubscribe result: true", script_result); |
| 654 EXPECT_NE(push_service(), GetAppHandler()); |
| 655 |
| 656 // After unsubscribing, try to resubscribe again without a key. |
| 657 // This should again fail. |
| 658 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); |
| 659 EXPECT_EQ( |
| 660 "AbortError - Registration failed - missing applicationServerKey, " |
| 661 "and gcm_sender_id not found in manifest", |
| 662 script_result); |
| 663 } |
| 664 |
| 665 IN_PROC_BROWSER_TEST_F( |
| 666 PushMessagingBrowserTest, |
| 667 ResubscribeWithoutKeyAfterSubscribingFromWorkerWithP256Key) { |
| 668 std::string script_result; |
| 669 |
| 670 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 671 ASSERT_EQ("ok - service worker registered", script_result); |
| 672 |
| 673 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission()); |
| 674 |
| 675 LoadTestPageWithoutManifest(); // Reload to become controlled. |
| 676 |
| 677 ASSERT_TRUE(RunScript("isControlled()", &script_result)); |
| 678 ASSERT_EQ("true - is controlled", script_result); |
| 679 |
| 680 // Run the subscribe from the service worker with a key. |
| 681 // This should succeed. |
| 682 ASSERT_TRUE(RunScript("workerSubscribePush()", &script_result)); |
| 683 ASSERT_NO_FATAL_FAILURE( |
| 684 EndpointToToken(script_result, true /* standard_protocol */)); |
| 685 |
| 686 // Try to resubscribe from the document without a key - should fail. |
| 687 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result)); |
| 688 EXPECT_EQ( |
| 689 "AbortError - Registration failed - missing applicationServerKey, " |
| 690 "and manifest empty or missing", |
| 691 script_result); |
| 692 |
| 693 // Now try to resubscribe from the service worker without a key. |
| 694 // This should also fail as the original key was not numeric. |
| 580 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); | 695 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); |
| 581 EXPECT_EQ( | 696 EXPECT_EQ( |
| 582 "AbortError - Registration failed - missing applicationServerKey, and " | 697 "AbortError - Registration failed - missing applicationServerKey, and " |
| 583 "gcm_sender_id not found in manifest", | 698 "gcm_sender_id not found in manifest", |
| 584 script_result); | 699 script_result); |
| 585 EXPECT_NE(push_service(), GetAppHandler()); | |
| 586 | |
| 587 // Run the subscription from the document without a key, this will trigger | |
| 588 // the code to read sender id from the manifest and will write it to the | |
| 589 // datastore. | |
| 590 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result)); | |
| 591 std::string token1; | |
| 592 ASSERT_NO_FATAL_FAILURE( | |
| 593 EndpointToToken(script_result, false /* standard_protocol */, &token1)); | |
| 594 | 700 |
| 595 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); | 701 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); |
| 596 EXPECT_EQ("unsubscribe result: true", script_result); | 702 EXPECT_EQ("unsubscribe result: true", script_result); |
| 597 EXPECT_NE(push_service(), GetAppHandler()); | |
| 598 | 703 |
| 599 // Now run the subscribe from the service worker without a key. | 704 // After unsubscribing, try to resubscribe again without a key. |
| 600 // In this case, the sender id will be read from the datastore. | 705 // This should again fail. |
| 601 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); | 706 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); |
| 602 std::string token2; | 707 EXPECT_EQ( |
| 603 ASSERT_NO_FATAL_FAILURE( | 708 "AbortError - Registration failed - missing applicationServerKey, " |
| 604 EndpointToToken(script_result, false /* standard_protocol */, &token2)); | 709 "and gcm_sender_id not found in manifest", |
| 605 EXPECT_NE(token1, token2); | 710 script_result); |
| 606 | |
| 607 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); | |
| 608 EXPECT_EQ("unsubscribe result: true", script_result); | |
| 609 EXPECT_NE(push_service(), GetAppHandler()); | |
| 610 } | 711 } |
| 611 | 712 |
| 612 // Disabled on Windows and Linux due to flakiness (http://crbug.com/554003). | 713 // Disabled on Windows and Linux due to flakiness (http://crbug.com/554003). |
| 613 #if defined(OS_WIN) || defined(OS_LINUX) | 714 #if defined(OS_WIN) || defined(OS_LINUX) |
| 614 #define MAYBE_SubscribePersisted DISABLED_SubscribePersisted | 715 #define MAYBE_SubscribePersisted DISABLED_SubscribePersisted |
| 615 #else | 716 #else |
| 616 #define MAYBE_SubscribePersisted SubscribePersisted | 717 #define MAYBE_SubscribePersisted SubscribePersisted |
| 617 #endif | 718 #endif |
| 618 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, MAYBE_SubscribePersisted) { | 719 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, MAYBE_SubscribePersisted) { |
| 619 std::string script_result; | 720 std::string script_result; |
| (...skipping 1258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1878 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully()); | 1979 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully()); |
| 1879 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); | 1980 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); |
| 1880 | 1981 |
| 1881 // After dropping the last subscription background mode is still inactive. | 1982 // After dropping the last subscription background mode is still inactive. |
| 1882 std::string script_result; | 1983 std::string script_result; |
| 1883 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); | 1984 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); |
| 1884 EXPECT_EQ("unsubscribe result: true", script_result); | 1985 EXPECT_EQ("unsubscribe result: true", script_result); |
| 1885 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); | 1986 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); |
| 1886 } | 1987 } |
| 1887 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) | 1988 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) |
| OLD | NEW |