Index: chrome/browser/extensions/service_worker_apitest.cc |
diff --git a/chrome/browser/extensions/service_worker_apitest.cc b/chrome/browser/extensions/service_worker_apitest.cc |
index b1018d8c447bc438492006a67f62da02ef7e9eb7..0a3eb4c058ef15f91b8d3d3870b92cb51b0d540f 100644 |
--- a/chrome/browser/extensions/service_worker_apitest.cc |
+++ b/chrome/browser/extensions/service_worker_apitest.cc |
@@ -6,12 +6,19 @@ |
#include "base/strings/stringprintf.h" |
#include "chrome/browser/extensions/extension_apitest.h" |
#include "chrome/browser/extensions/extension_service.h" |
+#include "chrome/browser/notifications/desktop_notification_profile_util.h" |
+#include "chrome/browser/push_messaging/push_messaging_app_identifier.h" |
+#include "chrome/browser/push_messaging/push_messaging_service_factory.h" |
+#include "chrome/browser/push_messaging/push_messaging_service_impl.h" |
+#include "chrome/browser/services/gcm/fake_gcm_profile_service.h" |
+#include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
#include "chrome/browser/ui/tabs/tab_strip_model.h" |
#include "chrome/test/base/ui_test_utils.h" |
#include "components/version_info/version_info.h" |
#include "content/public/browser/navigation_controller.h" |
#include "content/public/browser/navigation_entry.h" |
#include "content/public/browser/web_contents.h" |
+#include "content/public/common/content_switches.h" |
#include "content/public/common/page_type.h" |
#include "content/public/test/browser_test_utils.h" |
#include "extensions/browser/extension_host.h" |
@@ -113,6 +120,56 @@ class ServiceWorkerTest : public ExtensionApiTest { |
DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest); |
}; |
+class ServiceWorkerPushMessagingTest : public ServiceWorkerTest { |
+ public: |
+ ServiceWorkerPushMessagingTest() |
+ : gcm_service_(nullptr), push_service_(nullptr) {} |
+ ~ServiceWorkerPushMessagingTest() override {} |
+ |
+ void GrantNotificationPermissionForTest(const GURL& url) { |
+ GURL origin = url.GetOrigin(); |
+ DesktopNotificationProfileUtil::GrantPermission(profile(), origin); |
+ ASSERT_EQ( |
+ CONTENT_SETTING_ALLOW, |
+ DesktopNotificationProfileUtil::GetContentSetting(profile(), origin)); |
+ } |
+ |
+ PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration( |
+ int64 service_worker_registration_id, |
+ const GURL& origin) { |
+ PushMessagingAppIdentifier app_identifier = |
+ PushMessagingAppIdentifier::FindByServiceWorker( |
+ profile(), origin, service_worker_registration_id); |
+ |
+ EXPECT_FALSE(app_identifier.is_null()); |
+ return app_identifier; |
+ } |
+ |
+ // ExtensionApiTest overrides. |
+ void SetUpCommandLine(base::CommandLine* command_line) override { |
+ command_line->AppendSwitch(switches::kEnablePushMessagePayload); |
+ ServiceWorkerTest::SetUpCommandLine(command_line); |
+ } |
+ void SetUpOnMainThread() override { |
+ gcm_service_ = static_cast<gcm::FakeGCMProfileService*>( |
+ gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
+ profile(), &gcm::FakeGCMProfileService::Build)); |
+ gcm_service_->set_collect(true); |
+ push_service_ = PushMessagingServiceFactory::GetForProfile(profile()); |
+ |
+ ServiceWorkerTest::SetUpOnMainThread(); |
+ } |
+ |
+ gcm::FakeGCMProfileService* gcm_service() const { return gcm_service_; } |
+ PushMessagingServiceImpl* push_service() const { return push_service_; } |
+ |
+ private: |
+ gcm::FakeGCMProfileService* gcm_service_; |
+ PushMessagingServiceImpl* push_service_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ServiceWorkerPushMessagingTest); |
+}; |
+ |
IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterSucceedsOnTrunk) { |
StartTestFromBackgroundPage("register.js", kExpectSuccess); |
} |
@@ -342,4 +399,41 @@ IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, NotificationAPI) { |
"page.html")); |
} |
+IN_PROC_BROWSER_TEST_F(ServiceWorkerPushMessagingTest, OnPush) { |
+ const Extension* extension = LoadExtensionWithFlags( |
+ test_data_dir_.AppendASCII("service_worker/push_messaging"), kFlagNone); |
+ ASSERT_TRUE(extension); |
+ GURL extension_url = extension->url(); |
+ |
+ ASSERT_NO_FATAL_FAILURE(GrantNotificationPermissionForTest(extension_url)); |
+ |
+ GURL url = extension->GetResourceURL("page.html"); |
+ ui_test_utils::NavigateToURL(browser(), url); |
+ |
+ content::WebContents* web_contents = |
+ browser()->tab_strip_model()->GetActiveWebContents(); |
+ |
+ // Start the ServiceWorker. |
+ ExtensionTestMessageListener ready_listener("SERVICE_WORKER_READY", false); |
+ ready_listener.set_failure_message("SERVICE_WORKER_FAILURE"); |
+ const char* kScript = "window.runServiceWorker()"; |
+ EXPECT_TRUE(content::ExecuteScript(web_contents->GetMainFrame(), kScript)); |
+ EXPECT_TRUE(ready_listener.WaitUntilSatisfied()); |
+ |
+ PushMessagingAppIdentifier app_identifier = |
+ GetAppIdentifierForServiceWorkerRegistration(0LL, extension_url); |
+ ASSERT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id()); |
+ EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); |
+ |
+ // Send a push message via gcm and expect the ServiceWorker to receive it. |
+ ExtensionTestMessageListener push_message_listener("OK", false); |
+ push_message_listener.set_failure_message("FAIL"); |
+ gcm::IncomingMessage message; |
+ message.sender_id = "1234567890"; |
+ message.raw_data = "testdata"; |
+ message.decrypted = true; |
+ push_service()->OnMessage(app_identifier.app_id(), message); |
+ EXPECT_TRUE(push_message_listener.WaitUntilSatisfied()); |
+} |
+ |
} // namespace extensions |