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.
9 #include "base/command_line.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/infobars/infobar_service.h"
13 #include "chrome/browser/notifications/notification_test_util.h"
14 #include "chrome/browser/notifications/platform_notification_service_impl.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h"
17 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
18 #include "chrome/browser/services/gcm/push_messaging_application_id.h"
19 #include "chrome/browser/services/gcm/push_messaging_constants.h"
20 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/test/base/in_process_browser_test.h"
23 #include "chrome/test/base/ui_test_utils.h"
24 #include "components/content_settings/core/browser/host_content_settings_map.h"
25 #include "components/content_settings/core/common/content_settings.h"
26 #include "components/content_settings/core/common/content_settings_types.h"
27 #include "components/gcm_driver/gcm_client.h"
28 #include "components/infobars/core/confirm_infobar_delegate.h"
29 #include "components/infobars/core/infobar.h"
30 #include "components/infobars/core/infobar_manager.h"
31 #include "content/public/browser/web_contents.h"
32 #include "content/public/common/content_switches.h"
33 #include "content/public/test/browser_test_utils.h"
34 #include "ui/base/window_open_disposition.h"
36 #if defined(OS_ANDROID)
37 #include "base/android/build_info.h"
43 // Responds to a confirm infobar by accepting or cancelling it. Responds to at
45 class InfoBarResponder
: public infobars::InfoBarManager::Observer
{
47 InfoBarResponder(Browser
* browser
, bool accept
)
48 : infobar_service_(InfoBarService::FromWebContents(
49 browser
->tab_strip_model()->GetActiveWebContents())),
51 has_observed_(false) {
52 infobar_service_
->AddObserver(this);
55 ~InfoBarResponder() override
{ infobar_service_
->RemoveObserver(this); }
57 // infobars::InfoBarManager::Observer
58 void OnInfoBarAdded(infobars::InfoBar
* infobar
) override
{
62 ConfirmInfoBarDelegate
* delegate
=
63 infobar
->delegate()->AsConfirmInfoBarDelegate();
66 // Respond to the infobar asynchronously, like a person.
67 base::MessageLoop::current()->PostTask(
70 &InfoBarResponder::Respond
, base::Unretained(this), delegate
));
74 void Respond(ConfirmInfoBarDelegate
* delegate
) {
82 InfoBarService
* infobar_service_
;
87 // Class to instantiate on the stack that is meant to be used with
88 // FakeGCMProfileService. The ::Run() method follows the signature of
89 // FakeGCMProfileService::UnregisterCallback.
90 class UnregistrationCallback
{
92 UnregistrationCallback() : done_(false), waiting_(false) {}
94 void Run(const std::string
& app_id
) {
98 base::MessageLoop::current()->Quit();
101 void WaitUntilSatisfied() {
107 content::RunMessageLoop();
110 const std::string
& app_id() {
120 // Class to instantiate on the stack that is meant to be used with
121 // StubNotificationUIManager::SetNotificationAddedCallback. Mind that Run()
122 // might be invoked prior to WaitUntilSatisfied() being called.
123 class NotificationAddedCallback
{
125 NotificationAddedCallback() : done_(false), waiting_(false) {}
130 base::MessageLoop::current()->Quit();
133 void WaitUntilSatisfied() {
139 content::RunMessageLoop();
147 // The Push API depends on Web Notifications, which is only available on Android
148 // Jelly Bean and later.
149 bool IsPushSupported() {
150 #if defined(OS_ANDROID)
151 if (base::android::BuildInfo::GetInstance()->sdk_int() <
152 base::android::SDK_VERSION_JELLY_BEAN
) {
153 DVLOG(0) << "The Push API is only supported in Android 4.1 and later.";
162 class PushMessagingBrowserTest
: public InProcessBrowserTest
{
164 PushMessagingBrowserTest() : gcm_service_(nullptr) {}
165 ~PushMessagingBrowserTest() override
{}
167 // InProcessBrowserTest:
168 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
169 command_line
->AppendSwitch(switches::kEnablePushMessagePayload
);
170 command_line
->AppendSwitch(switches::kEnablePushMessagingHasPermission
);
172 InProcessBrowserTest::SetUpCommandLine(command_line
);
175 // InProcessBrowserTest:
176 void SetUp() override
{
177 https_server_
.reset(new net::SpawnedTestServer(
178 net::SpawnedTestServer::TYPE_HTTPS
,
179 net::BaseTestServer::SSLOptions(
180 net::BaseTestServer::SSLOptions::CERT_OK
),
181 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/"))));
182 ASSERT_TRUE(https_server_
->Start());
184 #if defined(ENABLE_NOTIFICATIONS)
185 notification_manager_
.reset(new StubNotificationUIManager
);
186 notification_service()->SetNotificationUIManagerForTesting(
187 notification_manager());
190 InProcessBrowserTest::SetUp();
193 // InProcessBrowserTest:
194 void SetUpOnMainThread() override
{
195 gcm_service_
= static_cast<FakeGCMProfileService
*>(
196 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
197 browser()->profile(), &FakeGCMProfileService::Build
));
198 gcm_service_
->set_collect(true);
202 InProcessBrowserTest::SetUpOnMainThread();
205 // InProcessBrowserTest:
206 void TearDown() override
{
207 #if defined(ENABLE_NOTIFICATIONS)
208 notification_service()->SetNotificationUIManagerForTesting(nullptr);
211 InProcessBrowserTest::TearDown();
214 void LoadTestPage(const std::string
& path
) {
215 ui_test_utils::NavigateToURL(browser(), https_server_
->GetURL(path
));
218 void LoadTestPage() {
219 LoadTestPage(GetTestURL());
222 bool RunScript(const std::string
& script
, std::string
* result
) {
223 return RunScript(script
, result
, nullptr);
226 bool RunScript(const std::string
& script
, std::string
* result
,
227 content::WebContents
* web_contents
) {
229 web_contents
= browser()->tab_strip_model()->GetActiveWebContents();
230 return content::ExecuteScriptAndExtractString(web_contents
->GetMainFrame(),
235 void TryToRegisterSuccessfully(
236 const std::string
& expected_push_registration_id
);
238 PushMessagingApplicationId
GetServiceWorkerAppId(
239 int64 service_worker_registration_id
);
241 net::SpawnedTestServer
* https_server() const { return https_server_
.get(); }
243 FakeGCMProfileService
* gcm_service() const { return gcm_service_
; }
245 #if defined(ENABLE_NOTIFICATIONS)
246 StubNotificationUIManager
* notification_manager() const {
247 return notification_manager_
.get();
250 PlatformNotificationServiceImpl
* notification_service() const {
251 return PlatformNotificationServiceImpl::GetInstance();
255 PushMessagingServiceImpl
* push_service() {
256 return static_cast<PushMessagingServiceImpl
*>(
257 gcm_service_
->push_messaging_service());
261 virtual std::string
GetTestURL() {
262 return "files/push_messaging/test.html";
266 scoped_ptr
<net::SpawnedTestServer
> https_server_
;
267 FakeGCMProfileService
* gcm_service_
;
268 scoped_ptr
<StubNotificationUIManager
> notification_manager_
;
270 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest
);
273 class PushMessagingBadManifestBrowserTest
: public PushMessagingBrowserTest
{
274 std::string
GetTestURL() override
{
275 return "files/push_messaging/test_bad_manifest.html";
279 IN_PROC_BROWSER_TEST_F(PushMessagingBadManifestBrowserTest
,
280 RegisterFailsNotVisibleMessages
) {
281 if (!IsPushSupported())
284 std::string script_result
;
286 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
287 ASSERT_EQ("ok - service worker registered", script_result
);
288 ASSERT_TRUE(RunScript("registerPush()", &script_result
));
289 EXPECT_EQ("AbortError - Registration failed - permission denied",
293 void PushMessagingBrowserTest::TryToRegisterSuccessfully(
294 const std::string
& expected_push_registration_id
) {
295 std::string script_result
;
297 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result
));
298 EXPECT_EQ("ok - service worker registered", script_result
);
300 InfoBarResponder
accepting_responder(browser(), true);
301 EXPECT_TRUE(RunScript("requestNotificationPermission()", &script_result
));
302 EXPECT_EQ("permission status - granted", script_result
);
304 EXPECT_TRUE(RunScript("registerPush()", &script_result
));
305 EXPECT_EQ(std::string(kPushMessagingEndpoint
) + " - "
306 + expected_push_registration_id
, script_result
);
309 PushMessagingApplicationId
PushMessagingBrowserTest::GetServiceWorkerAppId(
310 int64 service_worker_registration_id
) {
311 GURL origin
= https_server()->GetURL(std::string()).GetOrigin();
312 PushMessagingApplicationId application_id
= PushMessagingApplicationId::Get(
313 browser()->profile(), origin
, service_worker_registration_id
);
314 EXPECT_TRUE(application_id
.IsValid());
315 return application_id
;
318 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
319 RegisterSuccessNotificationsGranted
) {
320 if (!IsPushSupported())
323 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
325 PushMessagingApplicationId app_id
= GetServiceWorkerAppId(0LL);
326 EXPECT_EQ(app_id
.app_id_guid(), gcm_service()->last_registered_app_id());
327 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
330 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
331 RegisterSuccessNotificationsPrompt
) {
332 if (!IsPushSupported())
335 std::string script_result
;
337 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
338 ASSERT_EQ("ok - service worker registered", script_result
);
340 InfoBarResponder
accepting_responder(browser(), true);
341 ASSERT_TRUE(RunScript("registerPush()", &script_result
));
342 EXPECT_EQ(std::string(kPushMessagingEndpoint
) + " - 1-0", script_result
);
344 PushMessagingApplicationId app_id
= GetServiceWorkerAppId(0LL);
345 EXPECT_EQ(app_id
.app_id_guid(), gcm_service()->last_registered_app_id());
346 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
349 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
350 RegisterFailureNotificationsBlocked
) {
351 if (!IsPushSupported())
354 std::string script_result
;
356 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
357 ASSERT_EQ("ok - service worker registered", script_result
);
359 InfoBarResponder
cancelling_responder(browser(), false);
360 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result
));
361 ASSERT_EQ("permission status - denied", script_result
);
363 ASSERT_TRUE(RunScript("registerPush()", &script_result
));
364 EXPECT_EQ("AbortError - Registration failed - permission denied",
368 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, RegisterFailureNoManifest
) {
369 if (!IsPushSupported())
372 std::string script_result
;
374 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
375 ASSERT_EQ("ok - service worker registered", script_result
);
377 InfoBarResponder
accepting_responder(browser(), true);
378 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result
));
379 ASSERT_EQ("permission status - granted", script_result
);
381 ASSERT_TRUE(RunScript("removeManifest()", &script_result
));
382 ASSERT_EQ("manifest removed", script_result
);
384 ASSERT_TRUE(RunScript("registerPush()", &script_result
));
385 EXPECT_EQ("AbortError - Registration failed - no sender id provided",
389 // TODO(johnme): Test registering from a worker - see https://crbug.com/437298.
391 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, RegisterPersisted
) {
392 if (!IsPushSupported())
395 std::string script_result
;
397 // First, test that Service Worker registration IDs are assigned in order of
398 // registering the Service Workers, and the (fake) push registration ids are
399 // assigned in order of push registration (even when these orders are
402 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
403 PushMessagingApplicationId app_id_sw0
= GetServiceWorkerAppId(0LL);
404 EXPECT_EQ(app_id_sw0
.app_id_guid(), gcm_service()->last_registered_app_id());
406 LoadTestPage("files/push_messaging/subscope1/test.html");
407 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
408 ASSERT_EQ("ok - service worker registered", script_result
);
410 LoadTestPage("files/push_messaging/subscope2/test.html");
411 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
412 ASSERT_EQ("ok - service worker registered", script_result
);
414 // Note that we need to reload the page after registering, otherwise
415 // navigator.serviceWorker.ready is going to be resolved with the parent
416 // Service Worker which still controls the page.
417 LoadTestPage("files/push_messaging/subscope2/test.html");
418 TryToRegisterSuccessfully("1-1" /* expected_push_registration_id */);
419 PushMessagingApplicationId app_id_sw2
= GetServiceWorkerAppId(2LL);
420 EXPECT_EQ(app_id_sw2
.app_id_guid(), gcm_service()->last_registered_app_id());
422 LoadTestPage("files/push_messaging/subscope1/test.html");
423 TryToRegisterSuccessfully("1-2" /* expected_push_registration_id */);
424 PushMessagingApplicationId app_id_sw1
= GetServiceWorkerAppId(1LL);
425 EXPECT_EQ(app_id_sw1
.app_id_guid(), gcm_service()->last_registered_app_id());
427 // Now test that the Service Worker registration IDs and push registration IDs
428 // generated above were persisted to SW storage, by checking that they are
429 // unchanged despite requesting them in a different order.
430 // TODO(johnme): Ideally we would restart the browser at this point to check
431 // they were persisted to disk, but that's not currently possible since the
432 // test server uses random port numbers for each test (even PRE_Foo and Foo),
433 // so we wouldn't be able to load the test pages with the same origin.
435 LoadTestPage("files/push_messaging/subscope1/test.html");
436 TryToRegisterSuccessfully("1-2" /* expected_push_registration_id */);
437 EXPECT_EQ(app_id_sw1
.app_id_guid(), gcm_service()->last_registered_app_id());
439 LoadTestPage("files/push_messaging/subscope2/test.html");
440 TryToRegisterSuccessfully("1-1" /* expected_push_registration_id */);
441 EXPECT_EQ(app_id_sw1
.app_id_guid(), gcm_service()->last_registered_app_id());
444 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
445 EXPECT_EQ(app_id_sw1
.app_id_guid(), gcm_service()->last_registered_app_id());
448 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, PushEventSuccess
) {
449 if (!IsPushSupported())
452 std::string script_result
;
454 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
456 PushMessagingApplicationId app_id
= GetServiceWorkerAppId(0LL);
457 EXPECT_EQ(app_id
.app_id_guid(), gcm_service()->last_registered_app_id());
458 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
460 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
461 ASSERT_EQ("false - is not controlled", script_result
);
463 LoadTestPage(); // Reload to become controlled.
465 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
466 ASSERT_EQ("true - is controlled", script_result
);
468 GCMClient::IncomingMessage message
;
469 message
.sender_id
= "1234567890";
470 message
.data
["data"] = "testdata";
471 push_service()->OnMessage(app_id
.app_id_guid(), message
);
472 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result
));
473 EXPECT_EQ("testdata", script_result
);
476 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, PushEventNoServiceWorker
) {
477 if (!IsPushSupported())
480 std::string script_result
;
482 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
484 PushMessagingApplicationId app_id
= GetServiceWorkerAppId(0LL);
485 EXPECT_EQ(app_id
.app_id_guid(), gcm_service()->last_registered_app_id());
486 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
488 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
489 ASSERT_EQ("false - is not controlled", script_result
);
491 LoadTestPage(); // Reload to become controlled.
493 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
494 ASSERT_EQ("true - is controlled", script_result
);
496 // Unregister service worker. Sending a message should now fail.
497 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result
));
498 ASSERT_EQ("service worker unregistration status: true", script_result
);
500 // When the push service will receive it next message, given that there is no
501 // SW available, it should unregister |app_id|.
502 UnregistrationCallback callback
;
503 gcm_service()->SetUnregisterCallback(base::Bind(&UnregistrationCallback::Run
,
504 base::Unretained(&callback
)));
506 GCMClient::IncomingMessage message
;
507 message
.sender_id
= "1234567890";
508 message
.data
["data"] = "testdata";
509 push_service()->OnMessage(app_id
.app_id_guid(), message
);
511 callback
.WaitUntilSatisfied();
512 EXPECT_EQ(app_id
.app_id_guid(), callback
.app_id());
514 // No push data should have been received.
515 ASSERT_TRUE(RunScript("resultQueue.popImmediately()", &script_result
));
516 EXPECT_EQ("null", script_result
);
519 #if defined(ENABLE_NOTIFICATIONS)
520 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
521 PushEventEnforcesUserVisibleNotification
) {
522 if (!IsPushSupported())
525 std::string script_result
;
527 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
529 PushMessagingApplicationId app_id
= GetServiceWorkerAppId(0LL);
530 EXPECT_EQ(app_id
.app_id_guid(), gcm_service()->last_registered_app_id());
531 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
533 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
534 ASSERT_EQ("false - is not controlled", script_result
);
536 LoadTestPage(); // Reload to become controlled.
538 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
539 ASSERT_EQ("true - is controlled", script_result
);
541 notification_manager()->CancelAll();
542 ASSERT_EQ(0u, notification_manager()->GetNotificationCount());
544 // We'll need to specify the web_contents in which to eval script, since we're
545 // going to run script in a background tab.
546 content::WebContents
* web_contents
=
547 browser()->tab_strip_model()->GetActiveWebContents();
549 // If the site is visible in an active tab, we should not force a notification
550 // to be shown. Try it twice, since we allow one mistake per 10 push events.
551 GCMClient::IncomingMessage message
;
552 message
.sender_id
= "1234567890";
553 for (int n
= 0; n
< 2; n
++) {
554 message
.data
["data"] = "testdata";
555 push_service()->OnMessage(app_id
.app_id_guid(), message
);
556 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result
));
557 EXPECT_EQ("testdata", script_result
);
558 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
561 // Open a blank foreground tab so site is no longer visible.
562 ui_test_utils::NavigateToURLWithDisposition(
563 browser(), GURL("about:blank"), NEW_FOREGROUND_TAB
,
564 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
);
566 // If the Service Worker push event handler does not show a notification, we
567 // should show a forced one, but only on the 2nd occurrence since we allow one
568 // mistake per 10 push events.
569 message
.data
["data"] = "testdata";
570 push_service()->OnMessage(app_id
.app_id_guid(), message
);
571 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result
, web_contents
));
572 EXPECT_EQ("testdata", script_result
);
573 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
574 message
.data
["data"] = "testdata";
575 push_service()->OnMessage(app_id
.app_id_guid(), message
);
576 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result
, web_contents
));
577 EXPECT_EQ("testdata", script_result
);
578 EXPECT_EQ(1u, notification_manager()->GetNotificationCount());
579 EXPECT_EQ(base::ASCIIToUTF16(kPushMessagingForcedNotificationTag
),
580 notification_manager()->GetNotificationAt(0).replace_id());
582 // Currently, this notification will stick around until the user or webapp
583 // explicitly dismisses it (though we may change this later).
584 message
.data
["data"] = "shownotification";
585 push_service()->OnMessage(app_id
.app_id_guid(), message
);
586 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result
, web_contents
));
587 EXPECT_EQ("shownotification", script_result
);
588 EXPECT_EQ(2u, notification_manager()->GetNotificationCount());
590 notification_manager()->CancelAll();
591 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
593 // However if the Service Worker push event handler shows a notification, we
594 // should not show a forced one.
595 message
.data
["data"] = "shownotification";
596 for (int n
= 0; n
< 9; n
++) {
597 push_service()->OnMessage(app_id
.app_id_guid(), message
);
598 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result
, web_contents
));
599 EXPECT_EQ("shownotification", script_result
);
600 EXPECT_EQ(1u, notification_manager()->GetNotificationCount());
601 EXPECT_EQ(base::ASCIIToUTF16("push_test_tag"),
602 notification_manager()->GetNotificationAt(0).replace_id());
603 notification_manager()->CancelAll();
606 // Now that 10 push messages in a row have shown notifications, we should
607 // allow the next one to mistakenly not show a notification.
608 message
.data
["data"] = "testdata";
609 push_service()->OnMessage(app_id
.app_id_guid(), message
);
610 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result
, web_contents
));
611 EXPECT_EQ("testdata", script_result
);
612 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
615 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
616 PushEventNotificationWithoutEventWaitUntil
) {
617 if (!IsPushSupported())
620 std::string script_result
;
621 content::WebContents
* web_contents
=
622 browser()->tab_strip_model()->GetActiveWebContents();
624 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
626 PushMessagingApplicationId app_id
= GetServiceWorkerAppId(0LL);
627 EXPECT_EQ(app_id
.app_id_guid(), gcm_service()->last_registered_app_id());
628 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
630 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
631 ASSERT_EQ("false - is not controlled", script_result
);
633 LoadTestPage(); // Reload to become controlled.
635 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
636 ASSERT_EQ("true - is controlled", script_result
);
638 NotificationAddedCallback callback
;
639 notification_manager()->SetNotificationAddedCallback(
640 base::Bind(&NotificationAddedCallback::Run
, base::Unretained(&callback
)));
642 GCMClient::IncomingMessage message
;
643 message
.sender_id
= "1234567890";
644 message
.data
["data"] = "shownotification-without-waituntil";
645 push_service()->OnMessage(app_id
.app_id_guid(), message
);
646 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result
, web_contents
));
647 EXPECT_EQ("immediate:shownotification-without-waituntil", script_result
);
649 callback
.WaitUntilSatisfied();
651 ASSERT_EQ(1u, notification_manager()->GetNotificationCount());
652 EXPECT_EQ(base::ASCIIToUTF16("push_test_tag"),
653 notification_manager()->GetNotificationAt(0).replace_id());
655 // Verify that the renderer process hasn't crashed.
656 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
657 EXPECT_EQ("permission status - granted", script_result
);
661 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, HasPermissionSaysDefault
) {
662 if (!IsPushSupported())
665 std::string script_result
;
667 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
668 ASSERT_EQ("ok - service worker registered", script_result
);
670 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
671 ASSERT_EQ("permission status - default", script_result
);
674 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, HasPermissionSaysGranted
) {
675 if (!IsPushSupported())
678 std::string script_result
;
680 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
681 ASSERT_EQ("ok - service worker registered", script_result
);
683 InfoBarResponder
accepting_responder(browser(), true);
684 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result
));
685 EXPECT_EQ("permission status - granted", script_result
);
687 ASSERT_TRUE(RunScript("registerPush()", &script_result
));
688 EXPECT_EQ(std::string(kPushMessagingEndpoint
) + " - 1-0", script_result
);
690 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
691 EXPECT_EQ("permission status - granted", script_result
);
694 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, HasPermissionSaysDenied
) {
695 if (!IsPushSupported())
698 std::string script_result
;
700 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
701 ASSERT_EQ("ok - service worker registered", script_result
);
703 InfoBarResponder
cancelling_responder(browser(), false);
704 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result
));
705 EXPECT_EQ("permission status - denied", script_result
);
707 ASSERT_TRUE(RunScript("registerPush()", &script_result
));
708 EXPECT_EQ("AbortError - Registration failed - permission denied",
711 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
712 EXPECT_EQ("permission status - denied", script_result
);
715 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, UnregisterSuccess
) {
716 if (!IsPushSupported())
719 std::string script_result
;
721 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
723 gcm_service()->AddExpectedUnregisterResponse(GCMClient::SUCCESS
);
725 ASSERT_TRUE(RunScript("unregister()", &script_result
));
726 EXPECT_EQ("unregister result: true", script_result
);
729 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, UnregisterNetworkError
) {
730 if (!IsPushSupported())
733 std::string script_result
;
735 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
737 gcm_service()->AddExpectedUnregisterResponse(GCMClient::NETWORK_ERROR
);
739 ASSERT_TRUE(RunScript("unregister()", &script_result
));
740 EXPECT_EQ("unregister error: NetworkError: "
741 "Unregistration failed - could not connect to push server",
745 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, UnregisterAbortError
) {
746 if (!IsPushSupported())
749 std::string script_result
;
751 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
753 gcm_service()->AddExpectedUnregisterResponse(GCMClient::UNKNOWN_ERROR
);
755 ASSERT_TRUE(RunScript("unregister()", &script_result
));
756 EXPECT_EQ("unregister error: "
757 "AbortError: Unregistration failed - push service error",
761 #if defined(OS_ANDROID)
762 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, PushUnavailableOnAndroidICS
) {
763 // This test should only run on Android ICS to confirm that the Push API
764 // is not available on that version of Android.
765 if (IsPushSupported())
768 std::string script_result
;
769 ASSERT_TRUE(RunScript("window.PushManager", &script_result
));
770 EXPECT_EQ("undefined", script_result
);
774 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
775 GlobalResetPushPermissionUnregisters
) {
776 std::string script_result
;
778 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
780 ASSERT_TRUE(RunScript("hasRegistration()", &script_result
));
781 EXPECT_EQ("true - registered", script_result
);
783 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
784 EXPECT_EQ("permission status - granted", script_result
);
786 browser()->profile()->GetHostContentSettingsMap()->
787 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
);
789 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
790 EXPECT_EQ("permission status - default", script_result
);
792 ASSERT_TRUE(RunScript("hasRegistration()", &script_result
));
793 EXPECT_EQ("false - not registered", script_result
);
796 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
797 LocalResetPushPermissionUnregisters
) {
798 std::string script_result
;
800 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
802 ASSERT_TRUE(RunScript("hasRegistration()", &script_result
));
803 EXPECT_EQ("true - registered", script_result
);
805 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
806 EXPECT_EQ("permission status - granted", script_result
);
808 GURL origin
= https_server()->GetURL(std::string()).GetOrigin();
809 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
810 ContentSettingsPattern::FromURLNoWildcard(origin
),
811 ContentSettingsPattern::FromURLNoWildcard(origin
),
812 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
814 CONTENT_SETTING_DEFAULT
);
816 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
817 EXPECT_EQ("permission status - default", script_result
);
819 ASSERT_TRUE(RunScript("hasRegistration()", &script_result
));
820 EXPECT_EQ("false - not registered", script_result
);
823 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
824 DenyPushPermissionUnregisters
) {
825 std::string script_result
;
827 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
829 ASSERT_TRUE(RunScript("hasRegistration()", &script_result
));
830 EXPECT_EQ("true - registered", script_result
);
832 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
833 EXPECT_EQ("permission status - granted", script_result
);
835 GURL origin
= https_server()->GetURL(std::string()).GetOrigin();
836 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
837 ContentSettingsPattern::FromURLNoWildcard(origin
),
838 ContentSettingsPattern::FromURLNoWildcard(origin
),
839 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
841 CONTENT_SETTING_BLOCK
);
843 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
844 EXPECT_EQ("permission status - denied", script_result
);
846 ASSERT_TRUE(RunScript("hasRegistration()", &script_result
));
847 EXPECT_EQ("false - not registered", script_result
);
850 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
851 GlobalResetNotificationsPermissionUnregisters
) {
852 std::string script_result
;
854 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
856 ASSERT_TRUE(RunScript("hasRegistration()", &script_result
));
857 EXPECT_EQ("true - registered", script_result
);
859 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
860 EXPECT_EQ("permission status - granted", script_result
);
862 browser()->profile()->GetHostContentSettingsMap()->
863 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS
);
865 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
866 EXPECT_EQ("permission status - default", script_result
);
868 ASSERT_TRUE(RunScript("hasRegistration()", &script_result
));
869 EXPECT_EQ("false - not registered", script_result
);
872 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
873 LocalResetNotificationsPermissionUnregisters
) {
874 std::string script_result
;
876 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
878 ASSERT_TRUE(RunScript("hasRegistration()", &script_result
));
879 EXPECT_EQ("true - registered", script_result
);
881 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
882 EXPECT_EQ("permission status - granted", script_result
);
884 GURL origin
= https_server()->GetURL(std::string()).GetOrigin();
885 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
886 ContentSettingsPattern::FromURLNoWildcard(origin
),
887 ContentSettingsPattern::Wildcard(),
888 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
890 CONTENT_SETTING_DEFAULT
);
892 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
893 EXPECT_EQ("permission status - default", script_result
);
895 ASSERT_TRUE(RunScript("hasRegistration()", &script_result
));
896 EXPECT_EQ("false - not registered", script_result
);
899 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
900 DenyNotificationsPermissionUnregisters
) {
901 std::string script_result
;
903 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
905 ASSERT_TRUE(RunScript("hasRegistration()", &script_result
));
906 EXPECT_EQ("true - registered", script_result
);
908 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
909 EXPECT_EQ("permission status - granted", script_result
);
911 GURL origin
= https_server()->GetURL(std::string()).GetOrigin();
912 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
913 ContentSettingsPattern::FromURLNoWildcard(origin
),
914 ContentSettingsPattern::Wildcard(),
915 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
917 CONTENT_SETTING_BLOCK
);
919 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
920 EXPECT_EQ("permission status - denied", script_result
);
922 ASSERT_TRUE(RunScript("hasRegistration()", &script_result
));
923 EXPECT_EQ("false - not registered", script_result
);
926 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
927 GrantAlreadyGrantedPermissionDoesNotUnregister
) {
928 std::string script_result
;
930 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
932 ASSERT_TRUE(RunScript("hasRegistration()", &script_result
));
933 EXPECT_EQ("true - registered", script_result
);
935 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
936 EXPECT_EQ("permission status - granted", script_result
);
938 GURL origin
= https_server()->GetURL(std::string()).GetOrigin();
939 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
940 ContentSettingsPattern::FromURLNoWildcard(origin
),
941 ContentSettingsPattern::Wildcard(),
942 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
944 CONTENT_SETTING_ALLOW
);
945 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
946 ContentSettingsPattern::FromURLNoWildcard(origin
),
947 ContentSettingsPattern::FromURLNoWildcard(origin
),
948 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
950 CONTENT_SETTING_ALLOW
);
952 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
953 EXPECT_EQ("permission status - granted", script_result
);
955 ASSERT_TRUE(RunScript("hasRegistration()", &script_result
));
956 EXPECT_EQ("true - registered", script_result
);
959 // This test is testing some non-trivial content settings rules and make sure
960 // that they are respected with regards to automatic unregistration. In other
961 // words, it checks that the push service does not end up unregistering origins
962 // that have push permission with some non-common rules.
963 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
964 AutomaticUnregistrationFollowsContentSettingRules
) {
965 std::string script_result
;
967 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
969 ASSERT_TRUE(RunScript("hasRegistration()", &script_result
));
970 EXPECT_EQ("true - registered", script_result
);
972 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
973 EXPECT_EQ("permission status - granted", script_result
);
975 GURL origin
= https_server()->GetURL(std::string()).GetOrigin();
976 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
977 ContentSettingsPattern::Wildcard(),
978 ContentSettingsPattern::Wildcard(),
979 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
981 CONTENT_SETTING_ALLOW
);
982 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
983 ContentSettingsPattern::FromString("https://*"),
984 ContentSettingsPattern::FromString("https://*"),
985 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
987 CONTENT_SETTING_ALLOW
);
988 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
989 ContentSettingsPattern::FromURLNoWildcard(origin
),
990 ContentSettingsPattern::Wildcard(),
991 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
993 CONTENT_SETTING_DEFAULT
);
994 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
995 ContentSettingsPattern::FromURLNoWildcard(origin
),
996 ContentSettingsPattern::FromURLNoWildcard(origin
),
997 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
999 CONTENT_SETTING_DEFAULT
);
1001 // The two first rules should give |origin| the permission to use Push even
1002 // if the rules it used to have have been reset.
1003 // The Push service should not unsubcribe |origin| because at no point it was
1004 // left without permission to use Push.
1006 ASSERT_TRUE(RunScript("hasPermission()", &script_result
));
1007 EXPECT_EQ("permission status - granted", script_result
);
1009 ASSERT_TRUE(RunScript("hasRegistration()", &script_result
));
1010 EXPECT_EQ("true - registered", script_result
);