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

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

Issue 1851423003: Make Web Push use InstanceID tokens instead of GCM registrations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@iid4default
Patch Set: Rebase Created 4 years, 3 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 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 <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/bind_helpers.h" 7 #include "base/bind_helpers.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/extensions/extension_apitest.h" 11 #include "chrome/browser/extensions/extension_apitest.h"
12 #include "chrome/browser/extensions/extension_service.h" 12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/notifications/desktop_notification_profile_util.h" 13 #include "chrome/browser/notifications/desktop_notification_profile_util.h"
14 #include "chrome/browser/permissions/permission_manager.h" 14 #include "chrome/browser/permissions/permission_manager.h"
15 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" 15 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h"
16 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" 16 #include "chrome/browser/push_messaging/push_messaging_service_factory.h"
17 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" 17 #include "chrome/browser/push_messaging/push_messaging_service_impl.h"
18 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" 18 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h"
19 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" 19 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h" 20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/test/base/ui_test_utils.h" 21 #include "chrome/test/base/ui_test_utils.h"
22 #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h"
22 #include "components/version_info/version_info.h" 23 #include "components/version_info/version_info.h"
23 #include "content/public/browser/navigation_controller.h" 24 #include "content/public/browser/navigation_controller.h"
24 #include "content/public/browser/navigation_entry.h" 25 #include "content/public/browser/navigation_entry.h"
25 #include "content/public/browser/permission_type.h" 26 #include "content/public/browser/permission_type.h"
26 #include "content/public/browser/web_contents.h" 27 #include "content/public/browser/web_contents.h"
27 #include "content/public/common/content_switches.h" 28 #include "content/public/common/content_switches.h"
28 #include "content/public/common/origin_util.h" 29 #include "content/public/common/origin_util.h"
29 #include "content/public/common/page_type.h" 30 #include "content/public/common/page_type.h"
30 #include "content/public/test/background_sync_test_util.h" 31 #include "content/public/test/background_sync_test_util.h"
31 #include "content/public/test/browser_test_utils.h" 32 #include "content/public/test/browser_test_utils.h"
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 ServiceWorkerTest::SetUp(); 190 ServiceWorkerTest::SetUp();
190 } 191 }
191 192
192 private: 193 private:
193 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerBackgroundSyncTest); 194 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerBackgroundSyncTest);
194 }; 195 };
195 196
196 class ServiceWorkerPushMessagingTest : public ServiceWorkerTest { 197 class ServiceWorkerPushMessagingTest : public ServiceWorkerTest {
197 public: 198 public:
198 ServiceWorkerPushMessagingTest() 199 ServiceWorkerPushMessagingTest()
199 : gcm_service_(nullptr), push_service_(nullptr) {} 200 : gcm_driver_(nullptr), push_service_(nullptr) {}
200 ~ServiceWorkerPushMessagingTest() override {} 201 ~ServiceWorkerPushMessagingTest() override {}
201 202
202 void GrantNotificationPermissionForTest(const GURL& url) { 203 void GrantNotificationPermissionForTest(const GURL& url) {
203 GURL origin = url.GetOrigin(); 204 GURL origin = url.GetOrigin();
204 DesktopNotificationProfileUtil::GrantPermission(profile(), origin); 205 DesktopNotificationProfileUtil::GrantPermission(profile(), origin);
205 ASSERT_EQ(blink::mojom::PermissionStatus::GRANTED, 206 ASSERT_EQ(blink::mojom::PermissionStatus::GRANTED,
206 PermissionManager::Get(profile())->GetPermissionStatus( 207 PermissionManager::Get(profile())->GetPermissionStatus(
207 content::PermissionType::NOTIFICATIONS, origin, origin)); 208 content::PermissionType::NOTIFICATIONS, origin, origin));
208 } 209 }
209 210
210 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration( 211 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration(
211 int64_t service_worker_registration_id, 212 int64_t service_worker_registration_id,
212 const GURL& origin) { 213 const GURL& origin) {
213 PushMessagingAppIdentifier app_identifier = 214 PushMessagingAppIdentifier app_identifier =
214 PushMessagingAppIdentifier::FindByServiceWorker( 215 PushMessagingAppIdentifier::FindByServiceWorker(
215 profile(), origin, service_worker_registration_id); 216 profile(), origin, service_worker_registration_id);
216 217
217 EXPECT_FALSE(app_identifier.is_null()); 218 EXPECT_FALSE(app_identifier.is_null());
218 return app_identifier; 219 return app_identifier;
219 } 220 }
220 221
221 // ExtensionApiTest overrides. 222 // ExtensionApiTest overrides.
222 void SetUpCommandLine(base::CommandLine* command_line) override { 223 void SetUpCommandLine(base::CommandLine* command_line) override {
223 command_line->AppendSwitch( 224 command_line->AppendSwitch(
224 switches::kEnableExperimentalWebPlatformFeatures); 225 switches::kEnableExperimentalWebPlatformFeatures);
225 ServiceWorkerTest::SetUpCommandLine(command_line); 226 ServiceWorkerTest::SetUpCommandLine(command_line);
226 } 227 }
227 void SetUpOnMainThread() override { 228 void SetUpOnMainThread() override {
228 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>( 229 gcm::FakeGCMProfileService* gcm_service =
229 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( 230 static_cast<gcm::FakeGCMProfileService*>(
230 profile(), &gcm::FakeGCMProfileService::Build)); 231 gcm::GCMProfileServiceFactory::GetInstance()
231 gcm_service_->set_collect(true); 232 ->SetTestingFactoryAndUse(profile(),
233 &gcm::FakeGCMProfileService::Build));
234 gcm_driver_ = static_cast<instance_id::FakeGCMDriverForInstanceID*>(
235 gcm_service->driver());
232 push_service_ = PushMessagingServiceFactory::GetForProfile(profile()); 236 push_service_ = PushMessagingServiceFactory::GetForProfile(profile());
233 237
234 ServiceWorkerTest::SetUpOnMainThread(); 238 ServiceWorkerTest::SetUpOnMainThread();
235 } 239 }
236 240
237 gcm::FakeGCMProfileService* gcm_service() const { return gcm_service_; } 241 instance_id::FakeGCMDriverForInstanceID* gcm_driver() const {
242 return gcm_driver_;
243 }
238 PushMessagingServiceImpl* push_service() const { return push_service_; } 244 PushMessagingServiceImpl* push_service() const { return push_service_; }
239 245
240 private: 246 private:
241 gcm::FakeGCMProfileService* gcm_service_; 247 instance_id::FakeGCMDriverForInstanceID* gcm_driver_;
242 PushMessagingServiceImpl* push_service_; 248 PushMessagingServiceImpl* push_service_;
243 249
244 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerPushMessagingTest); 250 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerPushMessagingTest);
245 }; 251 };
246 252
247 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterSucceeds) { 253 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterSucceeds) {
248 StartTestFromBackgroundPage("register.js", kExpectSuccess); 254 StartTestFromBackgroundPage("register.js", kExpectSuccess);
249 } 255 }
250 256
251 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, UpdateRefreshesServiceWorker) { 257 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, UpdateRefreshesServiceWorker) {
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after
755 761
756 // Start the ServiceWorker. 762 // Start the ServiceWorker.
757 ExtensionTestMessageListener ready_listener("SERVICE_WORKER_READY", false); 763 ExtensionTestMessageListener ready_listener("SERVICE_WORKER_READY", false);
758 ready_listener.set_failure_message("SERVICE_WORKER_FAILURE"); 764 ready_listener.set_failure_message("SERVICE_WORKER_FAILURE");
759 const char* kScript = "window.runServiceWorker()"; 765 const char* kScript = "window.runServiceWorker()";
760 EXPECT_TRUE(content::ExecuteScript(web_contents->GetMainFrame(), kScript)); 766 EXPECT_TRUE(content::ExecuteScript(web_contents->GetMainFrame(), kScript));
761 EXPECT_TRUE(ready_listener.WaitUntilSatisfied()); 767 EXPECT_TRUE(ready_listener.WaitUntilSatisfied());
762 768
763 PushMessagingAppIdentifier app_identifier = 769 PushMessagingAppIdentifier app_identifier =
764 GetAppIdentifierForServiceWorkerRegistration(0LL, extension_url); 770 GetAppIdentifierForServiceWorkerRegistration(0LL, extension_url);
765 ASSERT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id()); 771 ASSERT_EQ(app_identifier.app_id(), gcm_driver()->last_gettoken_app_id());
766 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); 772 EXPECT_EQ("1234567890", gcm_driver()->last_gettoken_authorized_entity());
767 773
768 base::RunLoop run_loop; 774 base::RunLoop run_loop;
769 // Send a push message via gcm and expect the ServiceWorker to receive it. 775 // Send a push message via gcm and expect the ServiceWorker to receive it.
770 ExtensionTestMessageListener push_message_listener("OK", false); 776 ExtensionTestMessageListener push_message_listener("OK", false);
771 push_message_listener.set_failure_message("FAIL"); 777 push_message_listener.set_failure_message("FAIL");
772 gcm::IncomingMessage message; 778 gcm::IncomingMessage message;
773 message.sender_id = "1234567890"; 779 message.sender_id = "1234567890";
774 message.raw_data = "testdata"; 780 message.raw_data = "testdata";
775 message.decrypted = true; 781 message.decrypted = true;
776 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure()); 782 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure());
777 push_service()->OnMessage(app_identifier.app_id(), message); 783 push_service()->OnMessage(app_identifier.app_id(), message);
778 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied()); 784 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied());
779 run_loop.Run(); // Wait until the message is handled by push service. 785 run_loop.Run(); // Wait until the message is handled by push service.
780 } 786 }
781 787
782 } // namespace extensions 788 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698