Move StartsWith[ASCII] to base namespace.
[chromium-blink-merge.git] / chrome / browser / push_messaging / push_messaging_browsertest.cc
blob0cd1a74eda5a83814646feaaee026aff34de5d3a
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
3 // found in the LICENSE file.
5 #include <map>
6 #include <string>
8 #include "base/barrier_closure.h"
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/browsing_data/browsing_data_helper.h"
15 #include "chrome/browser/browsing_data/browsing_data_remover.h"
16 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h"
17 #include "chrome/browser/infobars/infobar_responder.h"
18 #include "chrome/browser/infobars/infobar_service.h"
19 #include "chrome/browser/notifications/notification_test_util.h"
20 #include "chrome/browser/notifications/platform_notification_service_impl.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h"
23 #include "chrome/browser/push_messaging/push_messaging_constants.h"
24 #include "chrome/browser/push_messaging/push_messaging_service_factory.h"
25 #include "chrome/browser/push_messaging/push_messaging_service_impl.h"
26 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h"
27 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
28 #include "chrome/browser/ui/browser.h"
29 #include "chrome/browser/ui/tabs/tab_strip_model.h"
30 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
31 #include "chrome/common/chrome_switches.h"
32 #include "chrome/test/base/in_process_browser_test.h"
33 #include "chrome/test/base/ui_test_utils.h"
34 #include "components/content_settings/core/browser/host_content_settings_map.h"
35 #include "components/content_settings/core/common/content_settings.h"
36 #include "components/content_settings/core/common/content_settings_types.h"
37 #include "components/gcm_driver/gcm_client.h"
38 #include "content/public/browser/web_contents.h"
39 #include "content/public/common/content_switches.h"
40 #include "content/public/test/browser_test_utils.h"
41 #include "content/public/test/test_utils.h"
42 #include "ui/base/window_open_disposition.h"
45 namespace {
46 // Class to instantiate on the stack that is meant to be used with
47 // FakeGCMProfileService. The ::Run() method follows the signature of
48 // FakeGCMProfileService::UnregisterCallback.
49 class UnregistrationCallback {
50 public:
51 UnregistrationCallback()
52 : message_loop_runner_(new content::MessageLoopRunner) {}
54 void Run(const std::string& app_id) {
55 app_id_ = app_id;
56 message_loop_runner_->Quit();
59 void WaitUntilSatisfied() {
60 message_loop_runner_->Run();
63 const std::string& app_id() {
64 return app_id_;
67 private:
68 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
69 std::string app_id_;
72 } // namespace
74 class PushMessagingBrowserTest : public InProcessBrowserTest,
75 public testing::WithParamInterface<bool> {
76 public:
77 PushMessagingBrowserTest() : gcm_service_(nullptr) {}
78 ~PushMessagingBrowserTest() override {}
80 // InProcessBrowserTest:
81 void SetUpCommandLine(base::CommandLine* command_line) override {
82 command_line->AppendSwitch(switches::kEnablePushMessagePayload);
84 if (GetParam()) {
85 command_line->AppendSwitch(switches::kEnablePermissionsBubbles);
86 EXPECT_TRUE(PermissionBubbleManager::Enabled());
87 } else {
88 command_line->AppendSwitch(switches::kDisablePermissionsBubbles);
89 EXPECT_FALSE(PermissionBubbleManager::Enabled());
92 InProcessBrowserTest::SetUpCommandLine(command_line);
95 // InProcessBrowserTest:
96 void SetUp() override {
97 https_server_.reset(new net::SpawnedTestServer(
98 net::SpawnedTestServer::TYPE_HTTPS,
99 net::BaseTestServer::SSLOptions(
100 net::BaseTestServer::SSLOptions::CERT_OK),
101 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/"))));
102 ASSERT_TRUE(https_server_->Start());
104 #if defined(ENABLE_NOTIFICATIONS)
105 notification_manager_.reset(new StubNotificationUIManager);
106 notification_service()->SetNotificationUIManagerForTesting(
107 notification_manager());
108 #endif
110 InProcessBrowserTest::SetUp();
113 // InProcessBrowserTest:
114 void SetUpOnMainThread() override {
115 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>(
116 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
117 GetBrowser()->profile(), &gcm::FakeGCMProfileService::Build));
118 gcm_service_->set_collect(true);
119 push_service_ =
120 PushMessagingServiceFactory::GetForProfile(GetBrowser()->profile());
122 LoadTestPage();
124 InProcessBrowserTest::SetUpOnMainThread();
127 // InProcessBrowserTest:
128 void TearDown() override {
129 #if defined(ENABLE_NOTIFICATIONS)
130 notification_service()->SetNotificationUIManagerForTesting(nullptr);
131 #endif
133 InProcessBrowserTest::TearDown();
136 void LoadTestPage(const std::string& path) {
137 ui_test_utils::NavigateToURL(GetBrowser(), https_server_->GetURL(path));
140 void LoadTestPage() {
141 LoadTestPage(GetTestURL());
144 bool RunScript(const std::string& script, std::string* result) {
145 return RunScript(script, result, nullptr);
148 bool RunScript(const std::string& script, std::string* result,
149 content::WebContents* web_contents) {
150 if (!web_contents)
151 web_contents = GetBrowser()->tab_strip_model()->GetActiveWebContents();
152 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(),
153 script,
154 result);
157 PermissionBubbleManager* GetPermissionBubbleManager() {
158 return PermissionBubbleManager::FromWebContents(
159 GetBrowser()->tab_strip_model()->GetActiveWebContents());
162 void RequestAndAcceptPermission();
163 void RequestAndDenyPermission();
165 void TryToSubscribeSuccessfully(
166 const std::string& expected_push_subscription_id);
168 std::string GetEndpointForSubscriptionId(const std::string& subscription_id) {
169 return std::string(kPushMessagingEndpoint) + "/" + subscription_id;
172 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration(
173 int64 service_worker_registration_id);
175 void SendMessageAndWaitUntilHandled(
176 const PushMessagingAppIdentifier& app_identifier,
177 const gcm::GCMClient::IncomingMessage& message);
179 net::SpawnedTestServer* https_server() const { return https_server_.get(); }
181 gcm::FakeGCMProfileService* gcm_service() const { return gcm_service_; }
183 #if defined(ENABLE_NOTIFICATIONS)
184 // To be called when delivery of a push message has finished. The |run_loop|
185 // will be told to quit after |messages_required| messages were received.
186 void OnDeliveryFinished(std::vector<size_t>* number_of_notifications_shown,
187 const base::Closure& done_closure) {
188 DCHECK(number_of_notifications_shown);
190 number_of_notifications_shown->push_back(
191 notification_manager_->GetNotificationCount());
193 done_closure.Run();
196 StubNotificationUIManager* notification_manager() const {
197 return notification_manager_.get();
200 PlatformNotificationServiceImpl* notification_service() const {
201 return PlatformNotificationServiceImpl::GetInstance();
203 #endif
205 PushMessagingServiceImpl* push_service() const { return push_service_; }
207 protected:
208 virtual std::string GetTestURL() {
209 return "files/push_messaging/test.html";
212 virtual Browser* GetBrowser() const { return browser(); }
214 InfoBarService* GetInfoBarService() {
215 return InfoBarService::FromWebContents(
216 GetBrowser()->tab_strip_model()->GetActiveWebContents());
219 private:
220 scoped_ptr<net::SpawnedTestServer> https_server_;
221 gcm::FakeGCMProfileService* gcm_service_;
222 PushMessagingServiceImpl* push_service_;
224 #if defined(ENABLE_NOTIFICATIONS)
225 scoped_ptr<StubNotificationUIManager> notification_manager_;
226 #endif
228 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest);
231 class PushMessagingBrowserTestEmptySubscriptionOptions
232 : public PushMessagingBrowserTest {
233 std::string GetTestURL() override {
234 return "files/push_messaging/test_no_subscription_options.html";
238 void PushMessagingBrowserTest::RequestAndAcceptPermission() {
239 std::string script_result;
241 if (PermissionBubbleManager::Enabled()) {
242 GetPermissionBubbleManager()->set_auto_response_for_test(
243 PermissionBubbleManager::ACCEPT_ALL);
244 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result));
245 } else {
246 InfoBarResponder infobar_accept_responder(GetInfoBarService(),
247 InfoBarResponder::ACCEPT);
248 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result));
250 EXPECT_EQ("permission status - granted", script_result);
253 void PushMessagingBrowserTest::RequestAndDenyPermission() {
254 std::string script_result;
256 if (PermissionBubbleManager::Enabled()) {
257 GetPermissionBubbleManager()->set_auto_response_for_test(
258 PermissionBubbleManager::DENY_ALL);
259 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result));
260 } else {
261 InfoBarResponder infobar_deny_responder(GetInfoBarService(),
262 InfoBarResponder::DENY);
263 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result));
265 EXPECT_EQ("permission status - denied", script_result);
268 void PushMessagingBrowserTest::TryToSubscribeSuccessfully(
269 const std::string& expected_push_subscription_id) {
270 std::string script_result;
272 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result));
273 EXPECT_EQ("ok - service worker registered", script_result);
275 RequestAndAcceptPermission();
277 EXPECT_TRUE(RunScript("subscribePush()", &script_result));
278 EXPECT_EQ(GetEndpointForSubscriptionId(expected_push_subscription_id),
279 script_result);
282 PushMessagingAppIdentifier
283 PushMessagingBrowserTest::GetAppIdentifierForServiceWorkerRegistration(
284 int64 service_worker_registration_id) {
285 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
286 PushMessagingAppIdentifier app_identifier =
287 PushMessagingAppIdentifier::FindByServiceWorker(
288 GetBrowser()->profile(), origin, service_worker_registration_id);
289 EXPECT_FALSE(app_identifier.is_null());
290 return app_identifier;
293 void PushMessagingBrowserTest::SendMessageAndWaitUntilHandled(
294 const PushMessagingAppIdentifier& app_identifier,
295 const gcm::GCMClient::IncomingMessage& message) {
296 base::RunLoop run_loop;
297 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure());
298 push_service()->OnMessage(app_identifier.app_id(), message);
299 run_loop.Run();
302 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest,
303 SubscribeSuccessNotificationsGranted) {
304 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
306 PushMessagingAppIdentifier app_identifier =
307 GetAppIdentifierForServiceWorkerRegistration(0LL);
308 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
309 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
312 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest,
313 SubscribeSuccessNotificationsPrompt) {
314 std::string script_result;
316 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
317 ASSERT_EQ("ok - service worker registered", script_result);
319 if (PermissionBubbleManager::Enabled()) {
320 GetPermissionBubbleManager()->set_auto_response_for_test(
321 PermissionBubbleManager::ACCEPT_ALL);
322 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
323 } else {
324 InfoBarResponder infobar_accept_responder(GetInfoBarService(),
325 InfoBarResponder::ACCEPT);
326 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
328 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"), script_result);
330 PushMessagingAppIdentifier app_identifier =
331 GetAppIdentifierForServiceWorkerRegistration(0LL);
332 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
333 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
336 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest,
337 SubscribeFailureNotificationsBlocked) {
338 std::string script_result;
340 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
341 ASSERT_EQ("ok - service worker registered", script_result);
343 RequestAndDenyPermission();
345 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
346 EXPECT_EQ("AbortError - Registration failed - permission denied",
347 script_result);
350 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest, SubscribeFailureNoManifest) {
351 std::string script_result;
353 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
354 ASSERT_EQ("ok - service worker registered", script_result);
356 RequestAndAcceptPermission();
358 ASSERT_TRUE(RunScript("removeManifest()", &script_result));
359 ASSERT_EQ("manifest removed", script_result);
361 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
362 EXPECT_EQ("AbortError - Registration failed - no sender id provided",
363 script_result);
366 // TODO(johnme): Test subscribing from a worker - see https://crbug.com/437298.
368 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTestEmptySubscriptionOptions,
369 RegisterFailureEmptyPushSubscriptionOptions) {
370 std::string script_result;
372 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
373 ASSERT_EQ("ok - service worker registered", script_result);
375 RequestAndAcceptPermission();
377 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
378 EXPECT_EQ("AbortError - Registration failed - permission denied",
379 script_result);
382 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest, SubscribePersisted) {
383 std::string script_result;
385 // First, test that Service Worker registration IDs are assigned in order of
386 // registering the Service Workers, and the (fake) push subscription ids are
387 // assigned in order of push subscription (even when these orders are
388 // different).
390 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
391 PushMessagingAppIdentifier sw0_identifier =
392 GetAppIdentifierForServiceWorkerRegistration(0LL);
393 EXPECT_EQ(sw0_identifier.app_id(), gcm_service()->last_registered_app_id());
395 LoadTestPage("files/push_messaging/subscope1/test.html");
396 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
397 ASSERT_EQ("ok - service worker registered", script_result);
399 LoadTestPage("files/push_messaging/subscope2/test.html");
400 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
401 ASSERT_EQ("ok - service worker registered", script_result);
403 // Note that we need to reload the page after registering, otherwise
404 // navigator.serviceWorker.ready is going to be resolved with the parent
405 // Service Worker which still controls the page.
406 LoadTestPage("files/push_messaging/subscope2/test.html");
407 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */);
408 PushMessagingAppIdentifier sw2_identifier =
409 GetAppIdentifierForServiceWorkerRegistration(2LL);
410 EXPECT_EQ(sw2_identifier.app_id(), gcm_service()->last_registered_app_id());
412 LoadTestPage("files/push_messaging/subscope1/test.html");
413 TryToSubscribeSuccessfully("1-2" /* expected_push_subscription_id */);
414 PushMessagingAppIdentifier sw1_identifier =
415 GetAppIdentifierForServiceWorkerRegistration(1LL);
416 EXPECT_EQ(sw1_identifier.app_id(), gcm_service()->last_registered_app_id());
418 // Now test that the Service Worker registration IDs and push subscription IDs
419 // generated above were persisted to SW storage, by checking that they are
420 // unchanged despite requesting them in a different order.
421 // TODO(johnme): Ideally we would restart the browser at this point to check
422 // they were persisted to disk, but that's not currently possible since the
423 // test server uses random port numbers for each test (even PRE_Foo and Foo),
424 // so we wouldn't be able to load the test pages with the same origin.
426 LoadTestPage("files/push_messaging/subscope1/test.html");
427 TryToSubscribeSuccessfully("1-2" /* expected_push_subscription_id */);
428 EXPECT_EQ(sw1_identifier.app_id(), gcm_service()->last_registered_app_id());
430 LoadTestPage("files/push_messaging/subscope2/test.html");
431 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */);
432 EXPECT_EQ(sw1_identifier.app_id(), gcm_service()->last_registered_app_id());
434 LoadTestPage();
435 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
436 EXPECT_EQ(sw1_identifier.app_id(), gcm_service()->last_registered_app_id());
439 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest, PushEventSuccess) {
440 std::string script_result;
442 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
444 PushMessagingAppIdentifier app_identifier =
445 GetAppIdentifierForServiceWorkerRegistration(0LL);
446 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
447 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
449 ASSERT_TRUE(RunScript("isControlled()", &script_result));
450 ASSERT_EQ("false - is not controlled", script_result);
452 LoadTestPage(); // Reload to become controlled.
454 ASSERT_TRUE(RunScript("isControlled()", &script_result));
455 ASSERT_EQ("true - is controlled", script_result);
457 gcm::GCMClient::IncomingMessage message;
458 message.sender_id = "1234567890";
459 message.data["data"] = "testdata";
460 push_service()->OnMessage(app_identifier.app_id(), message);
461 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result));
462 EXPECT_EQ("testdata", script_result);
465 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest, PushEventNoServiceWorker) {
466 std::string script_result;
468 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
470 PushMessagingAppIdentifier app_identifier =
471 GetAppIdentifierForServiceWorkerRegistration(0LL);
472 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
473 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
475 ASSERT_TRUE(RunScript("isControlled()", &script_result));
476 ASSERT_EQ("false - is not controlled", script_result);
478 LoadTestPage(); // Reload to become controlled.
480 ASSERT_TRUE(RunScript("isControlled()", &script_result));
481 ASSERT_EQ("true - is controlled", script_result);
483 // Unregister service worker. Sending a message should now fail.
484 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result));
485 ASSERT_EQ("service worker unregistration status: true", script_result);
487 // When the push service will receive it next message, given that there is no
488 // SW available, it should unregister |app_identifier.app_id()|.
489 UnregistrationCallback callback;
490 gcm_service()->SetUnregisterCallback(base::Bind(&UnregistrationCallback::Run,
491 base::Unretained(&callback)));
493 gcm::GCMClient::IncomingMessage message;
494 message.sender_id = "1234567890";
495 message.data["data"] = "testdata";
496 push_service()->OnMessage(app_identifier.app_id(), message);
498 callback.WaitUntilSatisfied();
499 EXPECT_EQ(app_identifier.app_id(), callback.app_id());
501 // No push data should have been received.
502 ASSERT_TRUE(RunScript("resultQueue.popImmediately()", &script_result));
503 EXPECT_EQ("null", script_result);
506 #if defined(ENABLE_NOTIFICATIONS)
507 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest,
508 PushEventEnforcesUserVisibleNotification) {
509 std::string script_result;
511 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
513 PushMessagingAppIdentifier app_identifier =
514 GetAppIdentifierForServiceWorkerRegistration(0LL);
515 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
516 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
518 ASSERT_TRUE(RunScript("isControlled()", &script_result));
519 ASSERT_EQ("false - is not controlled", script_result);
521 LoadTestPage(); // Reload to become controlled.
523 ASSERT_TRUE(RunScript("isControlled()", &script_result));
524 ASSERT_EQ("true - is controlled", script_result);
526 notification_manager()->CancelAll();
527 ASSERT_EQ(0u, notification_manager()->GetNotificationCount());
529 // We'll need to specify the web_contents in which to eval script, since we're
530 // going to run script in a background tab.
531 content::WebContents* web_contents =
532 GetBrowser()->tab_strip_model()->GetActiveWebContents();
534 // If the site is visible in an active tab, we should not force a notification
535 // to be shown. Try it twice, since we allow one mistake per 10 push events.
536 gcm::GCMClient::IncomingMessage message;
537 message.sender_id = "1234567890";
538 for (int n = 0; n < 2; n++) {
539 message.data["data"] = "testdata";
540 SendMessageAndWaitUntilHandled(app_identifier, message);
541 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result));
542 EXPECT_EQ("testdata", script_result);
543 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
546 // Open a blank foreground tab so site is no longer visible.
547 ui_test_utils::NavigateToURLWithDisposition(
548 GetBrowser(), GURL("about:blank"), NEW_FOREGROUND_TAB,
549 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
551 // If the Service Worker push event handler does not show a notification, we
552 // should show a forced one, but only on the 2nd occurrence since we allow one
553 // mistake per 10 push events.
554 message.data["data"] = "testdata";
555 SendMessageAndWaitUntilHandled(app_identifier, message);
556 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
557 EXPECT_EQ("testdata", script_result);
558 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
559 message.data["data"] = "testdata";
560 SendMessageAndWaitUntilHandled(app_identifier, message);
561 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
562 EXPECT_EQ("testdata", script_result);
564 ASSERT_EQ(1u, notification_manager()->GetNotificationCount());
566 const Notification& forced_notification =
567 notification_manager()->GetNotificationAt(0);
569 EXPECT_EQ(kPushMessagingForcedNotificationTag, forced_notification.tag());
570 EXPECT_TRUE(forced_notification.silent());
573 // The notification will be automatically dismissed when the developer shows
574 // a new notification themselves at a later point in time.
575 message.data["data"] = "shownotification";
576 SendMessageAndWaitUntilHandled(app_identifier, message);
577 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
578 EXPECT_EQ("shownotification", script_result);
580 ASSERT_EQ(1u, notification_manager()->GetNotificationCount());
582 const Notification& first_notification =
583 notification_manager()->GetNotificationAt(0);
585 EXPECT_NE(kPushMessagingForcedNotificationTag, first_notification.tag());
588 notification_manager()->CancelAll();
589 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
591 // However if the Service Worker push event handler shows a notification, we
592 // should not show a forced one.
593 message.data["data"] = "shownotification";
594 for (int n = 0; n < 9; n++) {
595 SendMessageAndWaitUntilHandled(app_identifier, message);
596 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
597 EXPECT_EQ("shownotification", script_result);
598 EXPECT_EQ(1u, notification_manager()->GetNotificationCount());
599 EXPECT_EQ("push_test_tag",
600 notification_manager()->GetNotificationAt(0).tag());
601 notification_manager()->CancelAll();
604 // Now that 10 push messages in a row have shown notifications, we should
605 // allow the next one to mistakenly not show a notification.
606 message.data["data"] = "testdata";
607 SendMessageAndWaitUntilHandled(app_identifier, message);
608 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
609 EXPECT_EQ("testdata", script_result);
610 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
613 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest,
614 PushEventEnforcesUserVisibleNotificationAfterQueue) {
615 std::string script_result;
617 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
619 PushMessagingAppIdentifier app_identifier =
620 GetAppIdentifierForServiceWorkerRegistration(0LL);
621 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
622 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
624 ASSERT_TRUE(RunScript("isControlled()", &script_result));
625 ASSERT_EQ("false - is not controlled", script_result);
627 LoadTestPage(); // Reload to become controlled.
629 ASSERT_TRUE(RunScript("isControlled()", &script_result));
630 ASSERT_EQ("true - is controlled", script_result);
632 // Fire off two push messages in sequence, only the second one of which will
633 // display a notification. The additional round-trip and I/O required by the
634 // second message, which shows a notification, should give us a reasonable
635 // confidence that the ordering will be maintained.
637 std::vector<size_t> number_of_notifications_shown;
639 gcm::GCMClient::IncomingMessage message;
640 message.sender_id = "1234567890";
643 base::RunLoop run_loop;
644 push_service()->SetMessageCallbackForTesting(
645 base::Bind(&PushMessagingBrowserTest::OnDeliveryFinished,
646 base::Unretained(this),
647 &number_of_notifications_shown,
648 base::BarrierClosure(2 /* num_closures */,
649 run_loop.QuitClosure())));
651 message.data["data"] = "testdata";
652 push_service()->OnMessage(app_identifier.app_id(), message);
654 message.data["data"] = "shownotification";
655 push_service()->OnMessage(app_identifier.app_id(), message);
657 run_loop.Run();
660 ASSERT_EQ(2u, number_of_notifications_shown.size());
661 EXPECT_EQ(0u, number_of_notifications_shown[0]);
662 EXPECT_EQ(1u, number_of_notifications_shown[1]);
665 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest,
666 PushEventNotificationWithoutEventWaitUntil) {
667 std::string script_result;
668 content::WebContents* web_contents =
669 GetBrowser()->tab_strip_model()->GetActiveWebContents();
671 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
673 PushMessagingAppIdentifier app_identifier =
674 GetAppIdentifierForServiceWorkerRegistration(0LL);
675 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
676 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
678 ASSERT_TRUE(RunScript("isControlled()", &script_result));
679 ASSERT_EQ("false - is not controlled", script_result);
681 LoadTestPage(); // Reload to become controlled.
683 ASSERT_TRUE(RunScript("isControlled()", &script_result));
684 ASSERT_EQ("true - is controlled", script_result);
686 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
687 new content::MessageLoopRunner;
688 notification_manager()->SetNotificationAddedCallback(
689 message_loop_runner->QuitClosure());
691 gcm::GCMClient::IncomingMessage message;
692 message.sender_id = "1234567890";
693 message.data["data"] = "shownotification-without-waituntil";
694 push_service()->OnMessage(app_identifier.app_id(), message);
695 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
696 EXPECT_EQ("immediate:shownotification-without-waituntil", script_result);
698 message_loop_runner->Run();
700 ASSERT_EQ(1u, notification_manager()->GetNotificationCount());
701 EXPECT_EQ("push_test_tag",
702 notification_manager()->GetNotificationAt(0).tag());
704 // Verify that the renderer process hasn't crashed.
705 ASSERT_TRUE(RunScript("permissionState()", &script_result));
706 EXPECT_EQ("permission status - granted", script_result);
708 #endif
710 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest, PermissionStateSaysPrompt) {
711 std::string script_result;
713 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
714 ASSERT_EQ("ok - service worker registered", script_result);
716 ASSERT_TRUE(RunScript("permissionState()", &script_result));
717 ASSERT_EQ("permission status - prompt", script_result);
720 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest, PermissionStateSaysGranted) {
721 std::string script_result;
723 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
724 ASSERT_EQ("ok - service worker registered", script_result);
726 RequestAndAcceptPermission();
728 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
729 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"),
730 script_result);
732 ASSERT_TRUE(RunScript("permissionState()", &script_result));
733 EXPECT_EQ("permission status - granted", script_result);
736 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest, PermissionStateSaysDenied) {
737 std::string script_result;
739 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
740 ASSERT_EQ("ok - service worker registered", script_result);
742 RequestAndDenyPermission();
744 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
745 EXPECT_EQ("AbortError - Registration failed - permission denied",
746 script_result);
748 ASSERT_TRUE(RunScript("permissionState()", &script_result));
749 EXPECT_EQ("permission status - denied", script_result);
752 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest, UnsubscribeSuccess) {
753 std::string script_result;
755 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result));
756 EXPECT_EQ("ok - service worker registered", script_result);
758 // Resolves true if there was a subscription.
759 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
760 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
761 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
762 EXPECT_EQ("unsubscribe result: true", script_result);
764 // Resolves false if there was no longer a subscription.
765 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
766 EXPECT_EQ("unsubscribe result: false", script_result);
768 // Doesn't reject if there was a network error (deactivates subscription
769 // locally anyway).
770 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */);
771 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::NETWORK_ERROR);
772 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
773 EXPECT_EQ("unsubscribe result: true", script_result);
774 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
775 EXPECT_EQ("false - not subscribed", script_result);
777 // Doesn't reject if there were other push service errors (deactivates
778 // subscription locally anyway).
779 TryToSubscribeSuccessfully("1-2" /* expected_push_subscription_id */);
780 gcm_service()->AddExpectedUnregisterResponse(
781 gcm::GCMClient::INVALID_PARAMETER);
782 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
783 EXPECT_EQ("unsubscribe result: true", script_result);
785 // Unsubscribing (with an existing reference to a PushSubscription), after
786 // unregistering the Service Worker, just means push subscription isn't found.
787 TryToSubscribeSuccessfully("1-3" /* expected_push_subscription_id */);
788 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result));
789 ASSERT_EQ("service worker unregistration status: true", script_result);
790 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
791 EXPECT_EQ("unsubscribe result: false", script_result);
794 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest,
795 GlobalResetPushPermissionUnsubscribes) {
796 std::string script_result;
798 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
800 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
801 EXPECT_EQ("true - subscribed", script_result);
803 ASSERT_TRUE(RunScript("permissionState()", &script_result));
804 EXPECT_EQ("permission status - granted", script_result);
806 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
807 new content::MessageLoopRunner;
808 push_service()->SetContentSettingChangedCallbackForTesting(
809 message_loop_runner->QuitClosure());
811 GetBrowser()->profile()->GetHostContentSettingsMap()->
812 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING);
814 message_loop_runner->Run();
816 ASSERT_TRUE(RunScript("permissionState()", &script_result));
817 EXPECT_EQ("permission status - prompt", script_result);
819 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
820 EXPECT_EQ("false - not subscribed", script_result);
823 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest,
824 LocalResetPushPermissionUnsubscribes) {
825 std::string script_result;
827 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
829 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
830 EXPECT_EQ("true - subscribed", script_result);
832 ASSERT_TRUE(RunScript("permissionState()", &script_result));
833 EXPECT_EQ("permission status - granted", script_result);
835 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
836 new content::MessageLoopRunner;
837 push_service()->SetContentSettingChangedCallbackForTesting(
838 message_loop_runner->QuitClosure());
840 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
841 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
842 ContentSettingsPattern::FromURLNoWildcard(origin),
843 ContentSettingsPattern::FromURLNoWildcard(origin),
844 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
845 std::string(),
846 CONTENT_SETTING_DEFAULT);
848 message_loop_runner->Run();
850 ASSERT_TRUE(RunScript("permissionState()", &script_result));
851 EXPECT_EQ("permission status - prompt", script_result);
853 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
854 EXPECT_EQ("false - not subscribed", script_result);
857 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest,
858 DenyPushPermissionUnsubscribes) {
859 std::string script_result;
861 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
863 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
864 EXPECT_EQ("true - subscribed", script_result);
866 ASSERT_TRUE(RunScript("permissionState()", &script_result));
867 EXPECT_EQ("permission status - granted", script_result);
869 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
870 new content::MessageLoopRunner;
871 push_service()->SetContentSettingChangedCallbackForTesting(
872 message_loop_runner->QuitClosure());
874 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
875 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
876 ContentSettingsPattern::FromURLNoWildcard(origin),
877 ContentSettingsPattern::FromURLNoWildcard(origin),
878 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
879 std::string(),
880 CONTENT_SETTING_BLOCK);
882 message_loop_runner->Run();
884 ASSERT_TRUE(RunScript("permissionState()", &script_result));
885 EXPECT_EQ("permission status - denied", script_result);
887 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
888 EXPECT_EQ("false - not subscribed", script_result);
891 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest,
892 GlobalResetNotificationsPermissionUnsubscribes) {
893 std::string script_result;
895 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
897 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
898 EXPECT_EQ("true - subscribed", script_result);
900 ASSERT_TRUE(RunScript("permissionState()", &script_result));
901 EXPECT_EQ("permission status - granted", script_result);
903 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
904 new content::MessageLoopRunner;
905 push_service()->SetContentSettingChangedCallbackForTesting(
906 message_loop_runner->QuitClosure());
908 GetBrowser()->profile()->GetHostContentSettingsMap()->
909 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
911 message_loop_runner->Run();
913 ASSERT_TRUE(RunScript("permissionState()", &script_result));
914 EXPECT_EQ("permission status - prompt", script_result);
916 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
917 EXPECT_EQ("false - not subscribed", script_result);
920 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest,
921 LocalResetNotificationsPermissionUnsubscribes) {
922 std::string script_result;
924 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
926 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
927 EXPECT_EQ("true - subscribed", script_result);
929 ASSERT_TRUE(RunScript("permissionState()", &script_result));
930 EXPECT_EQ("permission status - granted", script_result);
932 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
933 new content::MessageLoopRunner;
934 push_service()->SetContentSettingChangedCallbackForTesting(
935 message_loop_runner->QuitClosure());
937 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
938 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
939 ContentSettingsPattern::FromURLNoWildcard(origin),
940 ContentSettingsPattern::Wildcard(),
941 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
942 std::string(),
943 CONTENT_SETTING_DEFAULT);
945 message_loop_runner->Run();
947 ASSERT_TRUE(RunScript("permissionState()", &script_result));
948 EXPECT_EQ("permission status - prompt", script_result);
950 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
951 EXPECT_EQ("false - not subscribed", script_result);
954 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest,
955 DenyNotificationsPermissionUnsubscribes) {
956 std::string script_result;
958 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
960 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
961 EXPECT_EQ("true - subscribed", script_result);
963 ASSERT_TRUE(RunScript("permissionState()", &script_result));
964 EXPECT_EQ("permission status - granted", script_result);
966 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
967 new content::MessageLoopRunner;
968 push_service()->SetContentSettingChangedCallbackForTesting(
969 message_loop_runner->QuitClosure());
971 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
972 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
973 ContentSettingsPattern::FromURLNoWildcard(origin),
974 ContentSettingsPattern::Wildcard(),
975 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
976 std::string(),
977 CONTENT_SETTING_BLOCK);
979 message_loop_runner->Run();
981 ASSERT_TRUE(RunScript("permissionState()", &script_result));
982 EXPECT_EQ("permission status - denied", script_result);
984 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
985 EXPECT_EQ("false - not subscribed", script_result);
988 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest,
989 GrantAlreadyGrantedPermissionDoesNotUnsubscribe) {
990 std::string script_result;
992 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
994 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
995 EXPECT_EQ("true - subscribed", script_result);
997 ASSERT_TRUE(RunScript("permissionState()", &script_result));
998 EXPECT_EQ("permission status - granted", script_result);
1000 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1001 new content::MessageLoopRunner;
1002 push_service()->SetContentSettingChangedCallbackForTesting(
1003 base::BarrierClosure(2, message_loop_runner->QuitClosure()));
1005 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
1006 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
1007 ContentSettingsPattern::FromURLNoWildcard(origin),
1008 ContentSettingsPattern::Wildcard(),
1009 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
1010 std::string(),
1011 CONTENT_SETTING_ALLOW);
1012 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
1013 ContentSettingsPattern::FromURLNoWildcard(origin),
1014 ContentSettingsPattern::FromURLNoWildcard(origin),
1015 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
1016 std::string(),
1017 CONTENT_SETTING_ALLOW);
1019 message_loop_runner->Run();
1021 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1022 EXPECT_EQ("permission status - granted", script_result);
1024 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1025 EXPECT_EQ("true - subscribed", script_result);
1028 // This test is testing some non-trivial content settings rules and make sure
1029 // that they are respected with regards to automatic unsubscription. In other
1030 // words, it checks that the push service does not end up unsubscribing origins
1031 // that have push permission with some non-common rules.
1032 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest,
1033 AutomaticUnsubscriptionFollowsContentSettingRules) {
1034 std::string script_result;
1036 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
1038 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1039 EXPECT_EQ("true - subscribed", script_result);
1041 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1042 EXPECT_EQ("permission status - granted", script_result);
1044 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1045 new content::MessageLoopRunner;
1046 push_service()->SetContentSettingChangedCallbackForTesting(
1047 base::BarrierClosure(4, message_loop_runner->QuitClosure()));
1049 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
1050 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
1051 ContentSettingsPattern::Wildcard(),
1052 ContentSettingsPattern::Wildcard(),
1053 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
1054 std::string(),
1055 CONTENT_SETTING_ALLOW);
1056 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
1057 ContentSettingsPattern::FromString("https://*"),
1058 ContentSettingsPattern::FromString("https://*"),
1059 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
1060 std::string(),
1061 CONTENT_SETTING_ALLOW);
1062 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
1063 ContentSettingsPattern::FromURLNoWildcard(origin),
1064 ContentSettingsPattern::Wildcard(),
1065 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
1066 std::string(),
1067 CONTENT_SETTING_DEFAULT);
1068 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
1069 ContentSettingsPattern::FromURLNoWildcard(origin),
1070 ContentSettingsPattern::FromURLNoWildcard(origin),
1071 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
1072 std::string(),
1073 CONTENT_SETTING_DEFAULT);
1075 message_loop_runner->Run();
1077 // The two first rules should give |origin| the permission to use Push even
1078 // if the rules it used to have have been reset.
1079 // The Push service should not unsubcribe |origin| because at no point it was
1080 // left without permission to use Push.
1082 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1083 EXPECT_EQ("permission status - granted", script_result);
1085 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1086 EXPECT_EQ("true - subscribed", script_result);
1089 // Checks that automatically unsubscribing due to a revoked permission is
1090 // handled well if the sender ID needed to unsubscribe was already deleted.
1091 IN_PROC_BROWSER_TEST_P(PushMessagingBrowserTest,
1092 ResetPushPermissionAfterClearingSiteData) {
1093 std::string script_result;
1095 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
1097 PushMessagingAppIdentifier app_identifier =
1098 GetAppIdentifierForServiceWorkerRegistration(0LL);
1099 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
1100 PushMessagingAppIdentifier stored_app_identifier =
1101 PushMessagingAppIdentifier::FindByAppId(GetBrowser()->profile(),
1102 app_identifier.app_id());
1103 EXPECT_FALSE(stored_app_identifier.is_null());
1105 // Simulate a user clearing site data (including Service Workers, crucially).
1106 BrowsingDataRemover* remover =
1107 BrowsingDataRemover::CreateForUnboundedRange(GetBrowser()->profile());
1108 BrowsingDataRemoverCompletionObserver observer(remover);
1109 remover->Remove(BrowsingDataRemover::REMOVE_SITE_DATA,
1110 BrowsingDataHelper::UNPROTECTED_WEB);
1111 observer.BlockUntilCompletion();
1112 // BrowsingDataRemover deletes itself.
1114 base::RunLoop run_loop;
1115 push_service()->SetContentSettingChangedCallbackForTesting(
1116 run_loop.QuitClosure());
1118 // This shouldn't (asynchronously) cause a DCHECK.
1119 // TODO(johnme): Get this test running on Android, which has a different
1120 // codepath due to sender_id being required for unsubscribing there.
1121 GetBrowser()->profile()->GetHostContentSettingsMap()->
1122 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING);
1124 run_loop.Run();
1126 // |app_identifier| should no longer be stored in prefs.
1127 PushMessagingAppIdentifier stored_app_identifier2 =
1128 PushMessagingAppIdentifier::FindByAppId(GetBrowser()->profile(),
1129 app_identifier.app_id());
1130 EXPECT_TRUE(stored_app_identifier2.is_null());
1133 class PushMessagingIncognitoBrowserTest : public PushMessagingBrowserTest {
1134 public:
1135 ~PushMessagingIncognitoBrowserTest() override {}
1137 // PushMessagingBrowserTest:
1138 void SetUpOnMainThread() override {
1139 incognito_browser_ = CreateIncognitoBrowser();
1140 PushMessagingBrowserTest::SetUpOnMainThread();
1143 Browser* GetBrowser() const override { return incognito_browser_; }
1145 private:
1146 Browser* incognito_browser_ = nullptr;
1149 // Regression test for https://crbug.com/476474
1150 IN_PROC_BROWSER_TEST_P(PushMessagingIncognitoBrowserTest,
1151 IncognitoGetSubscriptionDoesNotHang) {
1152 ASSERT_TRUE(GetBrowser()->profile()->IsOffTheRecord());
1154 std::string script_result;
1156 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
1157 ASSERT_EQ("ok - service worker registered", script_result);
1159 // In Incognito mode the promise returned by getSubscription should not hang,
1160 // it should just fulfill with null.
1161 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1162 ASSERT_EQ("false - not subscribed", script_result);
1165 INSTANTIATE_TEST_CASE_P(PushMessagingBrowserTestWithParams,
1166 PushMessagingBrowserTest,
1167 testing::Values(true, false));
1168 INSTANTIATE_TEST_CASE_P(PushMessagingIncognitoBrowserTestWithParams,
1169 PushMessagingIncognitoBrowserTest,
1170 testing::Values(true, false));