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

Side by Side Diff: chrome/browser/extensions/service_worker_apitest.cc

Issue 1440873004: Extension SW - add basic push messaging tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sync @ tott Created 5 years, 1 month 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
« no previous file with comments | « no previous file | chrome/test/data/extensions/api_test/service_worker/push_messaging/manifest.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/extensions/extension_apitest.h" 8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/extensions/extension_service.h" 9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/notifications/desktop_notification_profile_util.h"
11 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h"
12 #include "chrome/browser/push_messaging/push_messaging_service_factory.h"
13 #include "chrome/browser/push_messaging/push_messaging_service_impl.h"
14 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h"
15 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h" 16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/test/base/ui_test_utils.h" 17 #include "chrome/test/base/ui_test_utils.h"
12 #include "components/version_info/version_info.h" 18 #include "components/version_info/version_info.h"
13 #include "content/public/browser/navigation_controller.h" 19 #include "content/public/browser/navigation_controller.h"
14 #include "content/public/browser/navigation_entry.h" 20 #include "content/public/browser/navigation_entry.h"
15 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/content_switches.h" 22 #include "content/public/common/content_switches.h"
17 #include "content/public/common/page_type.h" 23 #include "content/public/common/page_type.h"
18 #include "content/public/test/background_sync_test_util.h" 24 #include "content/public/test/background_sync_test_util.h"
19 #include "content/public/test/browser_test_utils.h" 25 #include "content/public/test/browser_test_utils.h"
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 137
132 void SetUp() override { 138 void SetUp() override {
133 content::background_sync_test_util::SetIgnoreNetworkChangeNotifier(true); 139 content::background_sync_test_util::SetIgnoreNetworkChangeNotifier(true);
134 ServiceWorkerTest::SetUp(); 140 ServiceWorkerTest::SetUp();
135 } 141 }
136 142
137 private: 143 private:
138 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerBackgroundSyncTest); 144 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerBackgroundSyncTest);
139 }; 145 };
140 146
147 class ServiceWorkerPushMessagingTest : public ServiceWorkerTest {
148 public:
149 ServiceWorkerPushMessagingTest()
150 : gcm_service_(nullptr), push_service_(nullptr) {}
151 ~ServiceWorkerPushMessagingTest() override {}
152
153 void GrantNotificationPermissionForTest(const GURL& url) {
154 GURL origin = url.GetOrigin();
155 DesktopNotificationProfileUtil::GrantPermission(profile(), origin);
156 ASSERT_EQ(
157 CONTENT_SETTING_ALLOW,
158 DesktopNotificationProfileUtil::GetContentSetting(profile(), origin));
159 }
160
161 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration(
162 int64 service_worker_registration_id,
163 const GURL& origin) {
164 PushMessagingAppIdentifier app_identifier =
165 PushMessagingAppIdentifier::FindByServiceWorker(
166 profile(), origin, service_worker_registration_id);
167
168 EXPECT_FALSE(app_identifier.is_null());
169 return app_identifier;
170 }
171
172 // ExtensionApiTest overrides.
173 void SetUpCommandLine(base::CommandLine* command_line) override {
174 command_line->AppendSwitch(switches::kEnablePushMessagePayload);
175 ServiceWorkerTest::SetUpCommandLine(command_line);
176 }
177 void SetUpOnMainThread() override {
178 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>(
179 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
180 profile(), &gcm::FakeGCMProfileService::Build));
181 gcm_service_->set_collect(true);
182 push_service_ = PushMessagingServiceFactory::GetForProfile(profile());
183
184 ServiceWorkerTest::SetUpOnMainThread();
185 }
186
187 gcm::FakeGCMProfileService* gcm_service() const { return gcm_service_; }
188 PushMessagingServiceImpl* push_service() const { return push_service_; }
189
190 private:
191 gcm::FakeGCMProfileService* gcm_service_;
192 PushMessagingServiceImpl* push_service_;
193
194 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerPushMessagingTest);
195 };
196
141 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterSucceedsOnTrunk) { 197 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterSucceedsOnTrunk) {
142 StartTestFromBackgroundPage("register.js", kExpectSuccess); 198 StartTestFromBackgroundPage("register.js", kExpectSuccess);
143 } 199 }
144 200
145 // This feature is restricted to trunk, so on dev it should have existing 201 // This feature is restricted to trunk, so on dev it should have existing
146 // behavior - which is for it to fail. 202 // behavior - which is for it to fail.
147 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterFailsOnDev) { 203 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterFailsOnDev) {
148 ScopedCurrentChannel current_channel_override( 204 ScopedCurrentChannel current_channel_override(
149 version_info::Channel::DEV); 205 version_info::Channel::DEV);
150 std::string error; 206 std::string error;
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 462
407 std::string value; 463 std::string value;
408 ASSERT_TRUE( 464 ASSERT_TRUE(
409 content::ExecuteScriptAndExtractString(tab, "register();", &value)); 465 content::ExecuteScriptAndExtractString(tab, "register();", &value));
410 EXPECT_EQ("SW controlled", value); 466 EXPECT_EQ("SW controlled", value);
411 467
412 ASSERT_TRUE(RunExtensionTest("service_worker/content_script_fetch")) 468 ASSERT_TRUE(RunExtensionTest("service_worker/content_script_fetch"))
413 << message_; 469 << message_;
414 } 470 }
415 471
472 IN_PROC_BROWSER_TEST_F(ServiceWorkerPushMessagingTest, OnPush) {
473 const Extension* extension = LoadExtensionWithFlags(
474 test_data_dir_.AppendASCII("service_worker/push_messaging"), kFlagNone);
475 ASSERT_TRUE(extension);
476 GURL extension_url = extension->url();
477
478 ASSERT_NO_FATAL_FAILURE(GrantNotificationPermissionForTest(extension_url));
479
480 GURL url = extension->GetResourceURL("page.html");
481 ui_test_utils::NavigateToURL(browser(), url);
482
483 content::WebContents* web_contents =
484 browser()->tab_strip_model()->GetActiveWebContents();
485
486 // Start the ServiceWorker.
487 ExtensionTestMessageListener ready_listener("SERVICE_WORKER_READY", false);
488 ready_listener.set_failure_message("SERVICE_WORKER_FAILURE");
489 const char* kScript = "window.runServiceWorker()";
490 EXPECT_TRUE(content::ExecuteScript(web_contents->GetMainFrame(), kScript));
491 EXPECT_TRUE(ready_listener.WaitUntilSatisfied());
492
493 PushMessagingAppIdentifier app_identifier =
494 GetAppIdentifierForServiceWorkerRegistration(0LL, extension_url);
495 ASSERT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
496 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
497
498 // Send a push message via gcm and expect the ServiceWorker to receive it.
499 ExtensionTestMessageListener push_message_listener("OK", false);
500 push_message_listener.set_failure_message("FAIL");
501 gcm::IncomingMessage message;
502 message.sender_id = "1234567890";
503 message.raw_data = "testdata";
504 message.decrypted = true;
505 push_service()->OnMessage(app_identifier.app_id(), message);
506 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied());
507 }
508
416 } // namespace extensions 509 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/extensions/api_test/service_worker/push_messaging/manifest.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698