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.
8 #include "base/barrier_closure.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/browser_process.h"
15 #include "chrome/browser/browsing_data/browsing_data_helper.h"
16 #include "chrome/browser/browsing_data/browsing_data_remover.h"
17 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h"
18 #include "chrome/browser/content_settings/host_content_settings_map_factory.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/common/gcm_messages.h"
38 #include "components/gcm_driver/gcm_client.h"
39 #include "content/public/browser/web_contents.h"
40 #include "content/public/common/content_switches.h"
41 #include "content/public/test/browser_test_utils.h"
42 #include "content/public/test/test_utils.h"
43 #include "ui/base/window_open_disposition.h"
45 #if defined(ENABLE_BACKGROUND)
46 #include "chrome/browser/background/background_mode_manager.h"
50 // Class to instantiate on the stack that is meant to be used with
51 // FakeGCMProfileService. The ::Run() method follows the signature of
52 // FakeGCMProfileService::UnregisterCallback.
53 class UnregistrationCallback
{
55 UnregistrationCallback()
56 : message_loop_runner_(new content::MessageLoopRunner
) {}
58 void Run(const std::string
& app_id
) {
60 message_loop_runner_
->Quit();
63 void WaitUntilSatisfied() {
64 message_loop_runner_
->Run();
67 const std::string
& app_id() {
72 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
78 class PushMessagingBrowserTest
: public InProcessBrowserTest
{
80 PushMessagingBrowserTest() : gcm_service_(nullptr) {}
81 ~PushMessagingBrowserTest() override
{}
83 // InProcessBrowserTest:
84 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
85 command_line
->AppendSwitch(switches::kEnablePushMessagePayload
);
86 InProcessBrowserTest::SetUpCommandLine(command_line
);
89 // InProcessBrowserTest:
90 void SetUp() override
{
91 https_server_
.reset(new net::SpawnedTestServer(
92 net::SpawnedTestServer::TYPE_HTTPS
,
93 net::BaseTestServer::SSLOptions(
94 net::BaseTestServer::SSLOptions::CERT_OK
),
95 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/"))));
96 ASSERT_TRUE(https_server_
->Start());
98 #if defined(ENABLE_NOTIFICATIONS)
99 notification_manager_
.reset(new StubNotificationUIManager
);
100 notification_service()->SetNotificationUIManagerForTesting(
101 notification_manager());
104 InProcessBrowserTest::SetUp();
107 // InProcessBrowserTest:
108 void SetUpOnMainThread() override
{
109 gcm_service_
= static_cast<gcm::FakeGCMProfileService
*>(
110 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
111 GetBrowser()->profile(), &gcm::FakeGCMProfileService::Build
));
112 gcm_service_
->set_collect(true);
114 PushMessagingServiceFactory::GetForProfile(GetBrowser()->profile());
118 InProcessBrowserTest::SetUpOnMainThread();
121 // InProcessBrowserTest:
122 void TearDown() override
{
123 #if defined(ENABLE_NOTIFICATIONS)
124 notification_service()->SetNotificationUIManagerForTesting(nullptr);
127 InProcessBrowserTest::TearDown();
130 void LoadTestPage(const std::string
& path
) {
131 ui_test_utils::NavigateToURL(GetBrowser(), https_server_
->GetURL(path
));
134 void LoadTestPage() {
135 LoadTestPage(GetTestURL());
138 bool RunScript(const std::string
& script
, std::string
* result
) {
139 return RunScript(script
, result
, nullptr);
142 bool RunScript(const std::string
& script
, std::string
* result
,
143 content::WebContents
* web_contents
) {
145 web_contents
= GetBrowser()->tab_strip_model()->GetActiveWebContents();
146 return content::ExecuteScriptAndExtractString(web_contents
->GetMainFrame(),
151 PermissionBubbleManager
* GetPermissionBubbleManager() {
152 return PermissionBubbleManager::FromWebContents(
153 GetBrowser()->tab_strip_model()->GetActiveWebContents());
156 void RequestAndAcceptPermission();
157 void RequestAndDenyPermission();
159 void TryToSubscribeSuccessfully(
160 const std::string
& expected_push_subscription_id
);
162 std::string
GetEndpointForSubscriptionId(const std::string
& subscription_id
) {
163 return std::string(kPushMessagingEndpoint
) + "/" + subscription_id
;
166 PushMessagingAppIdentifier
GetAppIdentifierForServiceWorkerRegistration(
167 int64 service_worker_registration_id
);
169 void SendMessageAndWaitUntilHandled(
170 const PushMessagingAppIdentifier
& app_identifier
,
171 const gcm::IncomingMessage
& message
);
173 net::SpawnedTestServer
* https_server() const { return https_server_
.get(); }
175 gcm::FakeGCMProfileService
* gcm_service() const { return gcm_service_
; }
177 #if defined(ENABLE_NOTIFICATIONS)
178 // To be called when delivery of a push message has finished. The |run_loop|
179 // will be told to quit after |messages_required| messages were received.
180 void OnDeliveryFinished(std::vector
<size_t>* number_of_notifications_shown
,
181 const base::Closure
& done_closure
) {
182 DCHECK(number_of_notifications_shown
);
184 number_of_notifications_shown
->push_back(
185 notification_manager_
->GetNotificationCount());
190 StubNotificationUIManager
* notification_manager() const {
191 return notification_manager_
.get();
194 PlatformNotificationServiceImpl
* notification_service() const {
195 return PlatformNotificationServiceImpl::GetInstance();
199 PushMessagingServiceImpl
* push_service() const { return push_service_
; }
202 virtual std::string
GetTestURL() {
203 return "files/push_messaging/test.html";
206 virtual Browser
* GetBrowser() const { return browser(); }
209 scoped_ptr
<net::SpawnedTestServer
> https_server_
;
210 gcm::FakeGCMProfileService
* gcm_service_
;
211 PushMessagingServiceImpl
* push_service_
;
213 #if defined(ENABLE_NOTIFICATIONS)
214 scoped_ptr
<StubNotificationUIManager
> notification_manager_
;
217 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest
);
220 class PushMessagingBrowserTestEmptySubscriptionOptions
221 : public PushMessagingBrowserTest
{
222 std::string
GetTestURL() override
{
223 return "files/push_messaging/test_no_subscription_options.html";
227 void PushMessagingBrowserTest::RequestAndAcceptPermission() {
228 std::string script_result
;
229 GetPermissionBubbleManager()->set_auto_response_for_test(
230 PermissionBubbleManager::ACCEPT_ALL
);
231 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result
));
232 EXPECT_EQ("permission status - granted", script_result
);
235 void PushMessagingBrowserTest::RequestAndDenyPermission() {
236 std::string script_result
;
237 GetPermissionBubbleManager()->set_auto_response_for_test(
238 PermissionBubbleManager::DENY_ALL
);
239 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result
));
240 EXPECT_EQ("permission status - denied", script_result
);
243 void PushMessagingBrowserTest::TryToSubscribeSuccessfully(
244 const std::string
& expected_push_subscription_id
) {
245 std::string script_result
;
247 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result
));
248 EXPECT_EQ("ok - service worker registered", script_result
);
250 RequestAndAcceptPermission();
252 EXPECT_TRUE(RunScript("subscribePush()", &script_result
));
253 EXPECT_EQ(GetEndpointForSubscriptionId(expected_push_subscription_id
),
257 PushMessagingAppIdentifier
258 PushMessagingBrowserTest::GetAppIdentifierForServiceWorkerRegistration(
259 int64 service_worker_registration_id
) {
260 GURL origin
= https_server()->GetURL(std::string()).GetOrigin();
261 PushMessagingAppIdentifier app_identifier
=
262 PushMessagingAppIdentifier::FindByServiceWorker(
263 GetBrowser()->profile(), origin
, service_worker_registration_id
);
264 EXPECT_FALSE(app_identifier
.is_null());
265 return app_identifier
;
268 void PushMessagingBrowserTest::SendMessageAndWaitUntilHandled(
269 const PushMessagingAppIdentifier
& app_identifier
,
270 const gcm::IncomingMessage
& message
) {
271 base::RunLoop run_loop
;
272 push_service()->SetMessageCallbackForTesting(run_loop
.QuitClosure());
273 push_service()->OnMessage(app_identifier
.app_id(), message
);
277 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
278 SubscribeSuccessNotificationsGranted
) {
279 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
281 PushMessagingAppIdentifier app_identifier
=
282 GetAppIdentifierForServiceWorkerRegistration(0LL);
283 EXPECT_EQ(app_identifier
.app_id(), gcm_service()->last_registered_app_id());
284 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
287 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
288 SubscribeSuccessNotificationsPrompt
) {
289 std::string script_result
;
291 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
292 ASSERT_EQ("ok - service worker registered", script_result
);
294 GetPermissionBubbleManager()->set_auto_response_for_test(
295 PermissionBubbleManager::ACCEPT_ALL
);
296 ASSERT_TRUE(RunScript("subscribePush()", &script_result
));
297 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"), script_result
);
299 PushMessagingAppIdentifier app_identifier
=
300 GetAppIdentifierForServiceWorkerRegistration(0LL);
301 EXPECT_EQ(app_identifier
.app_id(), gcm_service()->last_registered_app_id());
302 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
305 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
306 SubscribeFailureNotificationsBlocked
) {
307 std::string script_result
;
309 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
310 ASSERT_EQ("ok - service worker registered", script_result
);
312 RequestAndDenyPermission();
314 ASSERT_TRUE(RunScript("subscribePush()", &script_result
));
315 EXPECT_EQ("PermissionDeniedError - Registration failed - permission denied",
319 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, SubscribeFailureNoManifest
) {
320 std::string script_result
;
322 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
323 ASSERT_EQ("ok - service worker registered", script_result
);
325 RequestAndAcceptPermission();
327 ASSERT_TRUE(RunScript("removeManifest()", &script_result
));
328 ASSERT_EQ("manifest removed", script_result
);
330 ASSERT_TRUE(RunScript("subscribePush()", &script_result
));
331 EXPECT_EQ("AbortError - Registration failed - no sender id provided",
335 // TODO(johnme): Test subscribing from a worker - see https://crbug.com/437298.
337 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTestEmptySubscriptionOptions
,
338 RegisterFailureEmptyPushSubscriptionOptions
) {
339 std::string script_result
;
341 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
342 ASSERT_EQ("ok - service worker registered", script_result
);
344 RequestAndAcceptPermission();
346 ASSERT_TRUE(RunScript("subscribePush()", &script_result
));
347 EXPECT_EQ("PermissionDeniedError - Registration failed - permission denied",
351 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, SubscribePersisted
) {
352 std::string script_result
;
354 // First, test that Service Worker registration IDs are assigned in order of
355 // registering the Service Workers, and the (fake) push subscription ids are
356 // assigned in order of push subscription (even when these orders are
359 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
360 PushMessagingAppIdentifier sw0_identifier
=
361 GetAppIdentifierForServiceWorkerRegistration(0LL);
362 EXPECT_EQ(sw0_identifier
.app_id(), gcm_service()->last_registered_app_id());
364 LoadTestPage("files/push_messaging/subscope1/test.html");
365 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
366 ASSERT_EQ("ok - service worker registered", script_result
);
368 LoadTestPage("files/push_messaging/subscope2/test.html");
369 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
370 ASSERT_EQ("ok - service worker registered", script_result
);
372 // Note that we need to reload the page after registering, otherwise
373 // navigator.serviceWorker.ready is going to be resolved with the parent
374 // Service Worker which still controls the page.
375 LoadTestPage("files/push_messaging/subscope2/test.html");
376 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */);
377 PushMessagingAppIdentifier sw2_identifier
=
378 GetAppIdentifierForServiceWorkerRegistration(2LL);
379 EXPECT_EQ(sw2_identifier
.app_id(), gcm_service()->last_registered_app_id());
381 LoadTestPage("files/push_messaging/subscope1/test.html");
382 TryToSubscribeSuccessfully("1-2" /* expected_push_subscription_id */);
383 PushMessagingAppIdentifier sw1_identifier
=
384 GetAppIdentifierForServiceWorkerRegistration(1LL);
385 EXPECT_EQ(sw1_identifier
.app_id(), gcm_service()->last_registered_app_id());
387 // Now test that the Service Worker registration IDs and push subscription IDs
388 // generated above were persisted to SW storage, by checking that they are
389 // unchanged despite requesting them in a different order.
390 // TODO(johnme): Ideally we would restart the browser at this point to check
391 // they were persisted to disk, but that's not currently possible since the
392 // test server uses random port numbers for each test (even PRE_Foo and Foo),
393 // so we wouldn't be able to load the test pages with the same origin.
395 LoadTestPage("files/push_messaging/subscope1/test.html");
396 TryToSubscribeSuccessfully("1-2" /* expected_push_subscription_id */);
397 EXPECT_EQ(sw1_identifier
.app_id(), gcm_service()->last_registered_app_id());
399 LoadTestPage("files/push_messaging/subscope2/test.html");
400 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */);
401 EXPECT_EQ(sw1_identifier
.app_id(), gcm_service()->last_registered_app_id());
404 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
405 EXPECT_EQ(sw1_identifier
.app_id(), gcm_service()->last_registered_app_id());
408 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, PushEventSuccess
) {
409 std::string script_result
;
411 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
413 PushMessagingAppIdentifier app_identifier
=
414 GetAppIdentifierForServiceWorkerRegistration(0LL);
415 EXPECT_EQ(app_identifier
.app_id(), gcm_service()->last_registered_app_id());
416 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
418 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
419 ASSERT_EQ("false - is not controlled", script_result
);
421 LoadTestPage(); // Reload to become controlled.
423 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
424 ASSERT_EQ("true - is controlled", script_result
);
426 gcm::IncomingMessage message
;
427 message
.sender_id
= "1234567890";
428 message
.data
["data"] = "testdata";
429 push_service()->OnMessage(app_identifier
.app_id(), message
);
430 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result
));
431 EXPECT_EQ("testdata", script_result
);
434 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, PushEventNoServiceWorker
) {
435 std::string script_result
;
437 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
439 PushMessagingAppIdentifier app_identifier
=
440 GetAppIdentifierForServiceWorkerRegistration(0LL);
441 EXPECT_EQ(app_identifier
.app_id(), gcm_service()->last_registered_app_id());
442 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
444 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
445 ASSERT_EQ("false - is not controlled", script_result
);
447 LoadTestPage(); // Reload to become controlled.
449 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
450 ASSERT_EQ("true - is controlled", script_result
);
452 // Unregister service worker. Sending a message should now fail.
453 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result
));
454 ASSERT_EQ("service worker unregistration status: true", script_result
);
456 // When the push service will receive it next message, given that there is no
457 // SW available, it should unregister |app_identifier.app_id()|.
458 UnregistrationCallback callback
;
459 gcm_service()->SetUnregisterCallback(base::Bind(&UnregistrationCallback::Run
,
460 base::Unretained(&callback
)));
462 gcm::IncomingMessage message
;
463 message
.sender_id
= "1234567890";
464 message
.data
["data"] = "testdata";
465 push_service()->OnMessage(app_identifier
.app_id(), message
);
467 callback
.WaitUntilSatisfied();
468 EXPECT_EQ(app_identifier
.app_id(), callback
.app_id());
470 // No push data should have been received.
471 ASSERT_TRUE(RunScript("resultQueue.popImmediately()", &script_result
));
472 EXPECT_EQ("null", script_result
);
475 #if defined(ENABLE_NOTIFICATIONS)
476 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
477 PushEventEnforcesUserVisibleNotification
) {
478 std::string script_result
;
480 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
482 PushMessagingAppIdentifier app_identifier
=
483 GetAppIdentifierForServiceWorkerRegistration(0LL);
484 EXPECT_EQ(app_identifier
.app_id(), gcm_service()->last_registered_app_id());
485 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
487 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
488 ASSERT_EQ("false - is not controlled", script_result
);
490 LoadTestPage(); // Reload to become controlled.
492 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
493 ASSERT_EQ("true - is controlled", script_result
);
495 notification_manager()->CancelAll();
496 ASSERT_EQ(0u, notification_manager()->GetNotificationCount());
498 // We'll need to specify the web_contents in which to eval script, since we're
499 // going to run script in a background tab.
500 content::WebContents
* web_contents
=
501 GetBrowser()->tab_strip_model()->GetActiveWebContents();
503 // If the site is visible in an active tab, we should not force a notification
504 // to be shown. Try it twice, since we allow one mistake per 10 push events.
505 gcm::IncomingMessage message
;
506 message
.sender_id
= "1234567890";
507 for (int n
= 0; n
< 2; n
++) {
508 message
.data
["data"] = "testdata";
509 SendMessageAndWaitUntilHandled(app_identifier
, message
);
510 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result
));
511 EXPECT_EQ("testdata", script_result
);
512 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
515 // Open a blank foreground tab so site is no longer visible.
516 ui_test_utils::NavigateToURLWithDisposition(
517 GetBrowser(), GURL("about:blank"), NEW_FOREGROUND_TAB
,
518 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
);
520 // If the Service Worker push event handler does not show a notification, we
521 // should show a forced one, but only on the 2nd occurrence since we allow one
522 // mistake per 10 push events.
523 message
.data
["data"] = "testdata";
524 SendMessageAndWaitUntilHandled(app_identifier
, message
);
525 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result
, web_contents
));
526 EXPECT_EQ("testdata", script_result
);
527 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
528 message
.data
["data"] = "testdata";
529 SendMessageAndWaitUntilHandled(app_identifier
, message
);
530 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result
, web_contents
));
531 EXPECT_EQ("testdata", script_result
);
533 ASSERT_EQ(1u, notification_manager()->GetNotificationCount());
535 const Notification
& forced_notification
=
536 notification_manager()->GetNotificationAt(0);
538 EXPECT_EQ(kPushMessagingForcedNotificationTag
, forced_notification
.tag());
539 EXPECT_TRUE(forced_notification
.silent());
542 // The notification will be automatically dismissed when the developer shows
543 // a new notification themselves at a later point in time.
544 message
.data
["data"] = "shownotification";
545 SendMessageAndWaitUntilHandled(app_identifier
, message
);
546 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result
, web_contents
));
547 EXPECT_EQ("shownotification", script_result
);
549 ASSERT_EQ(1u, notification_manager()->GetNotificationCount());
551 const Notification
& first_notification
=
552 notification_manager()->GetNotificationAt(0);
554 EXPECT_NE(kPushMessagingForcedNotificationTag
, first_notification
.tag());
557 notification_manager()->CancelAll();
558 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
560 // However if the Service Worker push event handler shows a notification, we
561 // should not show a forced one.
562 message
.data
["data"] = "shownotification";
563 for (int n
= 0; n
< 9; n
++) {
564 SendMessageAndWaitUntilHandled(app_identifier
, message
);
565 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result
, web_contents
));
566 EXPECT_EQ("shownotification", script_result
);
567 EXPECT_EQ(1u, notification_manager()->GetNotificationCount());
568 EXPECT_EQ("push_test_tag",
569 notification_manager()->GetNotificationAt(0).tag());
570 notification_manager()->CancelAll();
573 // Now that 10 push messages in a row have shown notifications, we should
574 // allow the next one to mistakenly not show a notification.
575 message
.data
["data"] = "testdata";
576 SendMessageAndWaitUntilHandled(app_identifier
, message
);
577 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result
, web_contents
));
578 EXPECT_EQ("testdata", script_result
);
579 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
582 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
583 PushEventEnforcesUserVisibleNotificationAfterQueue
) {
584 std::string script_result
;
586 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
588 PushMessagingAppIdentifier app_identifier
=
589 GetAppIdentifierForServiceWorkerRegistration(0LL);
590 EXPECT_EQ(app_identifier
.app_id(), gcm_service()->last_registered_app_id());
591 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
593 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
594 ASSERT_EQ("false - is not controlled", script_result
);
596 LoadTestPage(); // Reload to become controlled.
598 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
599 ASSERT_EQ("true - is controlled", script_result
);
601 // Fire off two push messages in sequence, only the second one of which will
602 // display a notification. The additional round-trip and I/O required by the
603 // second message, which shows a notification, should give us a reasonable
604 // confidence that the ordering will be maintained.
606 std::vector
<size_t> number_of_notifications_shown
;
608 gcm::IncomingMessage message
;
609 message
.sender_id
= "1234567890";
612 base::RunLoop run_loop
;
613 push_service()->SetMessageCallbackForTesting(
614 base::Bind(&PushMessagingBrowserTest::OnDeliveryFinished
,
615 base::Unretained(this),
616 &number_of_notifications_shown
,
617 base::BarrierClosure(2 /* num_closures */,
618 run_loop
.QuitClosure())));
620 message
.data
["data"] = "testdata";
621 push_service()->OnMessage(app_identifier
.app_id(), message
);
623 message
.data
["data"] = "shownotification";
624 push_service()->OnMessage(app_identifier
.app_id(), message
);
629 ASSERT_EQ(2u, number_of_notifications_shown
.size());
630 EXPECT_EQ(0u, number_of_notifications_shown
[0]);
631 EXPECT_EQ(1u, number_of_notifications_shown
[1]);
634 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
635 PushEventNotificationWithoutEventWaitUntil
) {
636 std::string script_result
;
637 content::WebContents
* web_contents
=
638 GetBrowser()->tab_strip_model()->GetActiveWebContents();
640 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
642 PushMessagingAppIdentifier app_identifier
=
643 GetAppIdentifierForServiceWorkerRegistration(0LL);
644 EXPECT_EQ(app_identifier
.app_id(), gcm_service()->last_registered_app_id());
645 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
647 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
648 ASSERT_EQ("false - is not controlled", script_result
);
650 LoadTestPage(); // Reload to become controlled.
652 ASSERT_TRUE(RunScript("isControlled()", &script_result
));
653 ASSERT_EQ("true - is controlled", script_result
);
655 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner
=
656 new content::MessageLoopRunner
;
657 notification_manager()->SetNotificationAddedCallback(
658 message_loop_runner
->QuitClosure());
660 gcm::IncomingMessage message
;
661 message
.sender_id
= "1234567890";
662 message
.data
["data"] = "shownotification-without-waituntil";
663 push_service()->OnMessage(app_identifier
.app_id(), message
);
664 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result
, web_contents
));
665 EXPECT_EQ("immediate:shownotification-without-waituntil", script_result
);
667 message_loop_runner
->Run();
669 ASSERT_EQ(1u, notification_manager()->GetNotificationCount());
670 EXPECT_EQ("push_test_tag",
671 notification_manager()->GetNotificationAt(0).tag());
673 // Verify that the renderer process hasn't crashed.
674 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
675 EXPECT_EQ("permission status - granted", script_result
);
679 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, PermissionStateSaysPrompt
) {
680 std::string script_result
;
682 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
683 ASSERT_EQ("ok - service worker registered", script_result
);
685 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
686 ASSERT_EQ("permission status - prompt", script_result
);
689 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, PermissionStateSaysGranted
) {
690 std::string script_result
;
692 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
693 ASSERT_EQ("ok - service worker registered", script_result
);
695 RequestAndAcceptPermission();
697 ASSERT_TRUE(RunScript("subscribePush()", &script_result
));
698 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"),
701 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
702 EXPECT_EQ("permission status - granted", script_result
);
705 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, PermissionStateSaysDenied
) {
706 std::string script_result
;
708 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
709 ASSERT_EQ("ok - service worker registered", script_result
);
711 RequestAndDenyPermission();
713 ASSERT_TRUE(RunScript("subscribePush()", &script_result
));
714 EXPECT_EQ("PermissionDeniedError - Registration failed - permission denied",
717 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
718 EXPECT_EQ("permission status - denied", script_result
);
721 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, UnsubscribeSuccess
) {
722 std::string script_result
;
724 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result
));
725 EXPECT_EQ("ok - service worker registered", script_result
);
727 // Resolves true if there was a subscription.
728 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
729 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS
);
730 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result
));
731 EXPECT_EQ("unsubscribe result: true", script_result
);
733 // Resolves false if there was no longer a subscription.
734 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result
));
735 EXPECT_EQ("unsubscribe result: false", script_result
);
737 // Doesn't reject if there was a network error (deactivates subscription
739 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */);
740 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::NETWORK_ERROR
);
741 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result
));
742 EXPECT_EQ("unsubscribe result: true", script_result
);
743 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
744 EXPECT_EQ("false - not subscribed", script_result
);
746 // Doesn't reject if there were other push service errors (deactivates
747 // subscription locally anyway).
748 TryToSubscribeSuccessfully("1-2" /* expected_push_subscription_id */);
749 gcm_service()->AddExpectedUnregisterResponse(
750 gcm::GCMClient::INVALID_PARAMETER
);
751 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result
));
752 EXPECT_EQ("unsubscribe result: true", script_result
);
754 // Unsubscribing (with an existing reference to a PushSubscription), after
755 // unregistering the Service Worker, just means push subscription isn't found.
756 TryToSubscribeSuccessfully("1-3" /* expected_push_subscription_id */);
757 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result
));
758 ASSERT_EQ("service worker unregistration status: true", script_result
);
759 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result
));
760 EXPECT_EQ("unsubscribe result: false", script_result
);
763 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
764 GlobalResetPushPermissionUnsubscribes
) {
765 std::string script_result
;
767 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
769 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
770 EXPECT_EQ("true - subscribed", script_result
);
772 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
773 EXPECT_EQ("permission status - granted", script_result
);
775 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner
=
776 new content::MessageLoopRunner
;
777 push_service()->SetContentSettingChangedCallbackForTesting(
778 message_loop_runner
->QuitClosure());
780 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())->
781 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
);
783 message_loop_runner
->Run();
785 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
786 EXPECT_EQ("permission status - prompt", script_result
);
788 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
789 EXPECT_EQ("false - not subscribed", script_result
);
792 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
793 LocalResetPushPermissionUnsubscribes
) {
794 std::string script_result
;
796 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
798 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
799 EXPECT_EQ("true - subscribed", script_result
);
801 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
802 EXPECT_EQ("permission status - granted", script_result
);
804 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner
=
805 new content::MessageLoopRunner
;
806 push_service()->SetContentSettingChangedCallbackForTesting(
807 message_loop_runner
->QuitClosure());
809 GURL origin
= https_server()->GetURL(std::string()).GetOrigin();
810 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())
811 ->SetContentSetting(ContentSettingsPattern::FromURLNoWildcard(origin
),
812 ContentSettingsPattern::FromURLNoWildcard(origin
),
813 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
815 CONTENT_SETTING_DEFAULT
);
817 message_loop_runner
->Run();
819 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
820 EXPECT_EQ("permission status - prompt", script_result
);
822 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
823 EXPECT_EQ("false - not subscribed", script_result
);
826 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
827 DenyPushPermissionUnsubscribes
) {
828 std::string script_result
;
830 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
832 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
833 EXPECT_EQ("true - subscribed", script_result
);
835 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
836 EXPECT_EQ("permission status - granted", script_result
);
838 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner
=
839 new content::MessageLoopRunner
;
840 push_service()->SetContentSettingChangedCallbackForTesting(
841 message_loop_runner
->QuitClosure());
843 GURL origin
= https_server()->GetURL(std::string()).GetOrigin();
844 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())
845 ->SetContentSetting(ContentSettingsPattern::FromURLNoWildcard(origin
),
846 ContentSettingsPattern::FromURLNoWildcard(origin
),
847 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
849 CONTENT_SETTING_BLOCK
);
851 message_loop_runner
->Run();
853 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
854 EXPECT_EQ("permission status - denied", script_result
);
856 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
857 EXPECT_EQ("false - not subscribed", script_result
);
860 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
861 GlobalResetNotificationsPermissionUnsubscribes
) {
862 std::string script_result
;
864 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
866 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
867 EXPECT_EQ("true - subscribed", script_result
);
869 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
870 EXPECT_EQ("permission status - granted", script_result
);
872 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner
=
873 new content::MessageLoopRunner
;
874 push_service()->SetContentSettingChangedCallbackForTesting(
875 message_loop_runner
->QuitClosure());
877 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())->
878 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS
);
880 message_loop_runner
->Run();
882 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
883 EXPECT_EQ("permission status - prompt", script_result
);
885 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
886 EXPECT_EQ("false - not subscribed", script_result
);
889 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
890 LocalResetNotificationsPermissionUnsubscribes
) {
891 std::string script_result
;
893 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
895 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
896 EXPECT_EQ("true - subscribed", script_result
);
898 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
899 EXPECT_EQ("permission status - granted", script_result
);
901 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner
=
902 new content::MessageLoopRunner
;
903 push_service()->SetContentSettingChangedCallbackForTesting(
904 message_loop_runner
->QuitClosure());
906 GURL origin
= https_server()->GetURL(std::string()).GetOrigin();
907 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())
908 ->SetContentSetting(ContentSettingsPattern::FromURLNoWildcard(origin
),
909 ContentSettingsPattern::Wildcard(),
910 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
912 CONTENT_SETTING_DEFAULT
);
914 message_loop_runner
->Run();
916 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
917 EXPECT_EQ("permission status - prompt", script_result
);
919 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
920 EXPECT_EQ("false - not subscribed", script_result
);
923 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
924 DenyNotificationsPermissionUnsubscribes
) {
925 std::string script_result
;
927 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
929 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
930 EXPECT_EQ("true - subscribed", script_result
);
932 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
933 EXPECT_EQ("permission status - granted", script_result
);
935 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner
=
936 new content::MessageLoopRunner
;
937 push_service()->SetContentSettingChangedCallbackForTesting(
938 message_loop_runner
->QuitClosure());
940 GURL origin
= https_server()->GetURL(std::string()).GetOrigin();
941 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())
942 ->SetContentSetting(ContentSettingsPattern::FromURLNoWildcard(origin
),
943 ContentSettingsPattern::Wildcard(),
944 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
946 CONTENT_SETTING_BLOCK
);
948 message_loop_runner
->Run();
950 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
951 EXPECT_EQ("permission status - denied", script_result
);
953 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
954 EXPECT_EQ("false - not subscribed", script_result
);
957 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
958 GrantAlreadyGrantedPermissionDoesNotUnsubscribe
) {
959 std::string script_result
;
961 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
963 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
964 EXPECT_EQ("true - subscribed", script_result
);
966 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
967 EXPECT_EQ("permission status - granted", script_result
);
969 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner
=
970 new content::MessageLoopRunner
;
971 push_service()->SetContentSettingChangedCallbackForTesting(
972 base::BarrierClosure(2, message_loop_runner
->QuitClosure()));
974 GURL origin
= https_server()->GetURL(std::string()).GetOrigin();
975 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())
976 ->SetContentSetting(ContentSettingsPattern::FromURLNoWildcard(origin
),
977 ContentSettingsPattern::Wildcard(),
978 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
980 CONTENT_SETTING_ALLOW
);
981 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())
982 ->SetContentSetting(ContentSettingsPattern::FromURLNoWildcard(origin
),
983 ContentSettingsPattern::FromURLNoWildcard(origin
),
984 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
986 CONTENT_SETTING_ALLOW
);
988 message_loop_runner
->Run();
990 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
991 EXPECT_EQ("permission status - granted", script_result
);
993 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
994 EXPECT_EQ("true - subscribed", script_result
);
997 // This test is testing some non-trivial content settings rules and make sure
998 // that they are respected with regards to automatic unsubscription. In other
999 // words, it checks that the push service does not end up unsubscribing origins
1000 // that have push permission with some non-common rules.
1001 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
1002 AutomaticUnsubscriptionFollowsContentSettingRules
) {
1003 std::string script_result
;
1005 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
1007 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
1008 EXPECT_EQ("true - subscribed", script_result
);
1010 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
1011 EXPECT_EQ("permission status - granted", script_result
);
1013 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner
=
1014 new content::MessageLoopRunner
;
1015 push_service()->SetContentSettingChangedCallbackForTesting(
1016 base::BarrierClosure(4, message_loop_runner
->QuitClosure()));
1018 GURL origin
= https_server()->GetURL(std::string()).GetOrigin();
1019 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())
1020 ->SetContentSetting(ContentSettingsPattern::Wildcard(),
1021 ContentSettingsPattern::Wildcard(),
1022 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
1024 CONTENT_SETTING_ALLOW
);
1025 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())
1026 ->SetContentSetting(ContentSettingsPattern::FromString("https://*"),
1027 ContentSettingsPattern::FromString("https://*"),
1028 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
1030 CONTENT_SETTING_ALLOW
);
1031 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())
1032 ->SetContentSetting(ContentSettingsPattern::FromURLNoWildcard(origin
),
1033 ContentSettingsPattern::Wildcard(),
1034 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
1036 CONTENT_SETTING_DEFAULT
);
1037 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())
1038 ->SetContentSetting(ContentSettingsPattern::FromURLNoWildcard(origin
),
1039 ContentSettingsPattern::FromURLNoWildcard(origin
),
1040 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
1042 CONTENT_SETTING_DEFAULT
);
1044 message_loop_runner
->Run();
1046 // The two first rules should give |origin| the permission to use Push even
1047 // if the rules it used to have have been reset.
1048 // The Push service should not unsubcribe |origin| because at no point it was
1049 // left without permission to use Push.
1051 ASSERT_TRUE(RunScript("permissionState()", &script_result
));
1052 EXPECT_EQ("permission status - granted", script_result
);
1054 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
1055 EXPECT_EQ("true - subscribed", script_result
);
1058 // Checks that automatically unsubscribing due to a revoked permission is
1059 // handled well if the sender ID needed to unsubscribe was already deleted.
1060 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
1061 ResetPushPermissionAfterClearingSiteData
) {
1062 std::string script_result
;
1064 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
1066 PushMessagingAppIdentifier app_identifier
=
1067 GetAppIdentifierForServiceWorkerRegistration(0LL);
1068 EXPECT_EQ(app_identifier
.app_id(), gcm_service()->last_registered_app_id());
1069 PushMessagingAppIdentifier stored_app_identifier
=
1070 PushMessagingAppIdentifier::FindByAppId(GetBrowser()->profile(),
1071 app_identifier
.app_id());
1072 EXPECT_FALSE(stored_app_identifier
.is_null());
1074 // Simulate a user clearing site data (including Service Workers, crucially).
1075 BrowsingDataRemover
* remover
=
1076 BrowsingDataRemover::CreateForUnboundedRange(GetBrowser()->profile());
1077 BrowsingDataRemoverCompletionObserver
observer(remover
);
1078 remover
->Remove(BrowsingDataRemover::REMOVE_SITE_DATA
,
1079 BrowsingDataHelper::UNPROTECTED_WEB
);
1080 observer
.BlockUntilCompletion();
1081 // BrowsingDataRemover deletes itself.
1083 base::RunLoop run_loop
;
1084 push_service()->SetContentSettingChangedCallbackForTesting(
1085 run_loop
.QuitClosure());
1087 // This shouldn't (asynchronously) cause a DCHECK.
1088 // TODO(johnme): Get this test running on Android, which has a different
1089 // codepath due to sender_id being required for unsubscribing there.
1090 HostContentSettingsMapFactory::GetForProfile(GetBrowser()->profile())
1091 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
);
1095 // |app_identifier| should no longer be stored in prefs.
1096 PushMessagingAppIdentifier stored_app_identifier2
=
1097 PushMessagingAppIdentifier::FindByAppId(GetBrowser()->profile(),
1098 app_identifier
.app_id());
1099 EXPECT_TRUE(stored_app_identifier2
.is_null());
1102 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
, EncryptionKeyUniqueness
) {
1103 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
1105 std::string first_public_key
;
1106 ASSERT_TRUE(RunScript("getCurve25519dh()", &first_public_key
));
1107 EXPECT_GE(first_public_key
.size(), 32u);
1109 std::string script_result
;
1110 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS
);
1111 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result
));
1112 EXPECT_EQ("unsubscribe result: true", script_result
);
1114 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */);
1116 std::string second_public_key
;
1117 ASSERT_TRUE(RunScript("getCurve25519dh()", &second_public_key
));
1118 EXPECT_GE(second_public_key
.size(), 32u);
1120 EXPECT_NE(first_public_key
, second_public_key
);
1123 class PushMessagingIncognitoBrowserTest
: public PushMessagingBrowserTest
{
1125 ~PushMessagingIncognitoBrowserTest() override
{}
1127 // PushMessagingBrowserTest:
1128 void SetUpOnMainThread() override
{
1129 incognito_browser_
= CreateIncognitoBrowser();
1130 PushMessagingBrowserTest::SetUpOnMainThread();
1133 Browser
* GetBrowser() const override
{ return incognito_browser_
; }
1136 Browser
* incognito_browser_
= nullptr;
1139 // Regression test for https://crbug.com/476474
1140 IN_PROC_BROWSER_TEST_F(PushMessagingIncognitoBrowserTest
,
1141 IncognitoGetSubscriptionDoesNotHang
) {
1142 ASSERT_TRUE(GetBrowser()->profile()->IsOffTheRecord());
1144 std::string script_result
;
1146 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result
));
1147 ASSERT_EQ("ok - service worker registered", script_result
);
1149 // In Incognito mode the promise returned by getSubscription should not hang,
1150 // it should just fulfill with null.
1151 ASSERT_TRUE(RunScript("hasSubscription()", &script_result
));
1152 ASSERT_EQ("false - not subscribed", script_result
);
1155 // None of the following should matter on ChromeOS: crbug.com/527045
1156 #if defined(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS)
1157 // Push background mode is disabled by default.
1158 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest
,
1159 BackgroundModeDisabledByDefault
) {
1160 // Initially background mode is inactive.
1161 BackgroundModeManager
* background_mode_manager
=
1162 g_browser_process
->background_mode_manager();
1163 ASSERT_FALSE(background_mode_manager
->IsBackgroundModeActive());
1165 // Once there is a push subscription background mode is still inactive.
1166 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
1167 ASSERT_FALSE(background_mode_manager
->IsBackgroundModeActive());
1169 // After dropping the last subscription it is still inactive.
1170 std::string script_result
;
1171 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS
);
1172 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result
));
1173 EXPECT_EQ("unsubscribe result: true", script_result
);
1174 ASSERT_FALSE(background_mode_manager
->IsBackgroundModeActive());
1177 class PushMessagingBackgroundModeEnabledBrowserTest
1178 : public PushMessagingBrowserTest
{
1180 ~PushMessagingBackgroundModeEnabledBrowserTest() override
{}
1182 // PushMessagingBrowserTest:
1183 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
1184 command_line
->AppendSwitch(switches::kEnablePushApiBackgroundMode
);
1185 PushMessagingBrowserTest::SetUpCommandLine(command_line
);
1189 // In this test the command line enables push background mode.
1190 IN_PROC_BROWSER_TEST_F(PushMessagingBackgroundModeEnabledBrowserTest
,
1191 BackgroundModeEnabledWithCommandLine
) {
1192 // Initially background mode is inactive.
1193 BackgroundModeManager
* background_mode_manager
=
1194 g_browser_process
->background_mode_manager();
1195 ASSERT_FALSE(background_mode_manager
->IsBackgroundModeActive());
1197 // Once there is a push subscription background mode is active.
1198 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
1199 ASSERT_TRUE(background_mode_manager
->IsBackgroundModeActive());
1201 // Dropping the last subscription deactivates background mode.
1202 std::string script_result
;
1203 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS
);
1204 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result
));
1205 EXPECT_EQ("unsubscribe result: true", script_result
);
1206 ASSERT_FALSE(background_mode_manager
->IsBackgroundModeActive());
1209 class PushMessagingBackgroundModeDisabledBrowserTest
1210 : public PushMessagingBrowserTest
{
1212 ~PushMessagingBackgroundModeDisabledBrowserTest() override
{}
1214 // PushMessagingBrowserTest:
1215 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
1216 command_line
->AppendSwitch(switches::kDisablePushApiBackgroundMode
);
1217 PushMessagingBrowserTest::SetUpCommandLine(command_line
);
1221 // In this test the command line disables push background mode.
1222 IN_PROC_BROWSER_TEST_F(PushMessagingBackgroundModeDisabledBrowserTest
,
1223 BackgroundModeDisabledWithCommandLine
) {
1224 // Initially background mode is inactive.
1225 BackgroundModeManager
* background_mode_manager
=
1226 g_browser_process
->background_mode_manager();
1227 ASSERT_FALSE(background_mode_manager
->IsBackgroundModeActive());
1229 // Once there is a push subscription background mode is still inactive.
1230 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
1231 ASSERT_FALSE(background_mode_manager
->IsBackgroundModeActive());
1233 // After dropping the last subscription background mode is still inactive.
1234 std::string script_result
;
1235 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS
);
1236 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result
));
1237 EXPECT_EQ("unsubscribe result: true", script_result
);
1238 ASSERT_FALSE(background_mode_manager
->IsBackgroundModeActive());
1240 #endif // defined(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS)