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 "base/bind_helpers.h" | 5 #include "base/bind_helpers.h" |
6 #include "base/strings/stringprintf.h" | 6 #include "base/strings/stringprintf.h" |
7 #include "chrome/browser/extensions/extension_apitest.h" | 7 #include "chrome/browser/extensions/extension_apitest.h" |
8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/notifications/desktop_notification_profile_util.h" |
| 10 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" |
| 11 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" |
| 12 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" |
| 13 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" |
| 14 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
9 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
10 #include "chrome/test/base/ui_test_utils.h" | 16 #include "chrome/test/base/ui_test_utils.h" |
11 #include "components/version_info/version_info.h" | 17 #include "components/version_info/version_info.h" |
12 #include "content/public/browser/navigation_controller.h" | 18 #include "content/public/browser/navigation_controller.h" |
13 #include "content/public/browser/navigation_entry.h" | 19 #include "content/public/browser/navigation_entry.h" |
14 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/common/content_switches.h" |
15 #include "content/public/common/page_type.h" | 22 #include "content/public/common/page_type.h" |
16 #include "content/public/test/browser_test_utils.h" | 23 #include "content/public/test/browser_test_utils.h" |
17 #include "extensions/browser/extension_host.h" | 24 #include "extensions/browser/extension_host.h" |
18 #include "extensions/browser/process_manager.h" | 25 #include "extensions/browser/process_manager.h" |
19 #include "extensions/test/background_page_watcher.h" | 26 #include "extensions/test/background_page_watcher.h" |
20 #include "extensions/test/extension_test_message_listener.h" | 27 #include "extensions/test/extension_test_message_listener.h" |
21 | 28 |
22 namespace extensions { | 29 namespace extensions { |
23 | 30 |
24 namespace { | 31 namespace { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 return ExtractInnerText(Navigate(url)); | 113 return ExtractInnerText(Navigate(url)); |
107 } | 114 } |
108 | 115 |
109 private: | 116 private: |
110 // Sets the channel to "trunk" since service workers are restricted to trunk. | 117 // Sets the channel to "trunk" since service workers are restricted to trunk. |
111 ScopedCurrentChannel current_channel_; | 118 ScopedCurrentChannel current_channel_; |
112 | 119 |
113 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest); | 120 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest); |
114 }; | 121 }; |
115 | 122 |
| 123 class ServiceWorkerPushMessagingTest : public ServiceWorkerTest { |
| 124 public: |
| 125 ServiceWorkerPushMessagingTest() |
| 126 : gcm_service_(nullptr), push_service_(nullptr) {} |
| 127 ~ServiceWorkerPushMessagingTest() override {} |
| 128 |
| 129 void GrantNotificationPermissionForTest(const GURL& url) { |
| 130 GURL origin = url.GetOrigin(); |
| 131 DesktopNotificationProfileUtil::GrantPermission(profile(), origin); |
| 132 ASSERT_EQ( |
| 133 CONTENT_SETTING_ALLOW, |
| 134 DesktopNotificationProfileUtil::GetContentSetting(profile(), origin)); |
| 135 } |
| 136 |
| 137 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration( |
| 138 int64 service_worker_registration_id, |
| 139 const GURL& origin) { |
| 140 PushMessagingAppIdentifier app_identifier = |
| 141 PushMessagingAppIdentifier::FindByServiceWorker( |
| 142 profile(), origin, service_worker_registration_id); |
| 143 |
| 144 EXPECT_FALSE(app_identifier.is_null()); |
| 145 return app_identifier; |
| 146 } |
| 147 |
| 148 // ExtensionApiTest overrides. |
| 149 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 150 command_line->AppendSwitch(switches::kEnablePushMessagePayload); |
| 151 ServiceWorkerTest::SetUpCommandLine(command_line); |
| 152 } |
| 153 void SetUpOnMainThread() override { |
| 154 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>( |
| 155 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 156 profile(), &gcm::FakeGCMProfileService::Build)); |
| 157 gcm_service_->set_collect(true); |
| 158 push_service_ = PushMessagingServiceFactory::GetForProfile(profile()); |
| 159 |
| 160 ServiceWorkerTest::SetUpOnMainThread(); |
| 161 } |
| 162 |
| 163 gcm::FakeGCMProfileService* gcm_service() const { return gcm_service_; } |
| 164 PushMessagingServiceImpl* push_service() const { return push_service_; } |
| 165 |
| 166 private: |
| 167 gcm::FakeGCMProfileService* gcm_service_; |
| 168 PushMessagingServiceImpl* push_service_; |
| 169 |
| 170 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerPushMessagingTest); |
| 171 }; |
| 172 |
116 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterSucceedsOnTrunk) { | 173 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterSucceedsOnTrunk) { |
117 StartTestFromBackgroundPage("register.js", kExpectSuccess); | 174 StartTestFromBackgroundPage("register.js", kExpectSuccess); |
118 } | 175 } |
119 | 176 |
120 // This feature is restricted to trunk, so on dev it should have existing | 177 // This feature is restricted to trunk, so on dev it should have existing |
121 // behavior - which is for it to fail. | 178 // behavior - which is for it to fail. |
122 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterFailsOnDev) { | 179 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterFailsOnDev) { |
123 ScopedCurrentChannel current_channel_override( | 180 ScopedCurrentChannel current_channel_override( |
124 version_info::Channel::DEV); | 181 version_info::Channel::DEV); |
125 std::string error; | 182 std::string error; |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 // should fail. | 392 // should fail. |
336 // Note that this also tests that service workers can be registered from tabs. | 393 // Note that this also tests that service workers can be registered from tabs. |
337 EXPECT_TRUE(RunExtensionSubtest("service_worker/no_background", "page.html")); | 394 EXPECT_TRUE(RunExtensionSubtest("service_worker/no_background", "page.html")); |
338 } | 395 } |
339 | 396 |
340 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, NotificationAPI) { | 397 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, NotificationAPI) { |
341 EXPECT_TRUE(RunExtensionSubtest("service_worker/notifications/has_permission", | 398 EXPECT_TRUE(RunExtensionSubtest("service_worker/notifications/has_permission", |
342 "page.html")); | 399 "page.html")); |
343 } | 400 } |
344 | 401 |
| 402 IN_PROC_BROWSER_TEST_F(ServiceWorkerPushMessagingTest, OnPush) { |
| 403 const Extension* extension = LoadExtensionWithFlags( |
| 404 test_data_dir_.AppendASCII("service_worker/push_messaging"), kFlagNone); |
| 405 ASSERT_TRUE(extension); |
| 406 GURL extension_url = extension->url(); |
| 407 |
| 408 ASSERT_NO_FATAL_FAILURE(GrantNotificationPermissionForTest(extension_url)); |
| 409 |
| 410 GURL url = extension->GetResourceURL("page.html"); |
| 411 ui_test_utils::NavigateToURL(browser(), url); |
| 412 |
| 413 content::WebContents* web_contents = |
| 414 browser()->tab_strip_model()->GetActiveWebContents(); |
| 415 |
| 416 // Start the ServiceWorker. |
| 417 ExtensionTestMessageListener ready_listener("SERVICE_WORKER_READY", false); |
| 418 ready_listener.set_failure_message("SERVICE_WORKER_FAILURE"); |
| 419 const char* kScript = "window.runServiceWorker()"; |
| 420 EXPECT_TRUE(content::ExecuteScript(web_contents->GetMainFrame(), kScript)); |
| 421 EXPECT_TRUE(ready_listener.WaitUntilSatisfied()); |
| 422 |
| 423 PushMessagingAppIdentifier app_identifier = |
| 424 GetAppIdentifierForServiceWorkerRegistration(0LL, extension_url); |
| 425 ASSERT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id()); |
| 426 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); |
| 427 |
| 428 // Send a push message via gcm and expect the ServiceWorker to receive it. |
| 429 ExtensionTestMessageListener push_message_listener("OK", false); |
| 430 push_message_listener.set_failure_message("FAIL"); |
| 431 gcm::IncomingMessage message; |
| 432 message.sender_id = "1234567890"; |
| 433 message.raw_data = "testdata"; |
| 434 message.decrypted = true; |
| 435 push_service()->OnMessage(app_identifier.app_id(), message); |
| 436 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied()); |
| 437 } |
| 438 |
345 } // namespace extensions | 439 } // namespace extensions |
OLD | NEW |