Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / push_messaging / push_messaging_browsertest.cc
blob0983c75dc630acfc6c4b4deb3465cab2083abf90
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/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/notifications/notification_test_util.h"
19 #include "chrome/browser/notifications/platform_notification_service_impl.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h"
22 #include "chrome/browser/push_messaging/push_messaging_constants.h"
23 #include "chrome/browser/push_messaging/push_messaging_service_factory.h"
24 #include "chrome/browser/push_messaging/push_messaging_service_impl.h"
25 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h"
26 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
27 #include "chrome/browser/ui/browser.h"
28 #include "chrome/browser/ui/tabs/tab_strip_model.h"
29 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
30 #include "chrome/common/chrome_switches.h"
31 #include "chrome/test/base/in_process_browser_test.h"
32 #include "chrome/test/base/ui_test_utils.h"
33 #include "components/content_settings/core/browser/host_content_settings_map.h"
34 #include "components/content_settings/core/common/content_settings.h"
35 #include "components/content_settings/core/common/content_settings_types.h"
36 #include "components/gcm_driver/common/gcm_messages.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"
44 #if defined(ENABLE_BACKGROUND)
45 #include "chrome/browser/background/background_mode_manager.h"
46 #endif
48 namespace {
49 // Class to instantiate on the stack that is meant to be used with
50 // FakeGCMProfileService. The ::Run() method follows the signature of
51 // FakeGCMProfileService::UnregisterCallback.
52 class UnregistrationCallback {
53 public:
54 UnregistrationCallback()
55 : message_loop_runner_(new content::MessageLoopRunner) {}
57 void Run(const std::string& app_id) {
58 app_id_ = app_id;
59 message_loop_runner_->Quit();
62 void WaitUntilSatisfied() {
63 message_loop_runner_->Run();
66 const std::string& app_id() {
67 return app_id_;
70 private:
71 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
72 std::string app_id_;
75 } // namespace
77 class PushMessagingBrowserTest : public InProcessBrowserTest {
78 public:
79 PushMessagingBrowserTest() : gcm_service_(nullptr) {}
80 ~PushMessagingBrowserTest() override {}
82 // InProcessBrowserTest:
83 void SetUpCommandLine(base::CommandLine* command_line) override {
84 command_line->AppendSwitch(switches::kEnablePushMessagePayload);
85 InProcessBrowserTest::SetUpCommandLine(command_line);
88 // InProcessBrowserTest:
89 void SetUp() override {
90 https_server_.reset(new net::SpawnedTestServer(
91 net::SpawnedTestServer::TYPE_HTTPS,
92 net::BaseTestServer::SSLOptions(
93 net::BaseTestServer::SSLOptions::CERT_OK),
94 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/"))));
95 ASSERT_TRUE(https_server_->Start());
97 #if defined(ENABLE_NOTIFICATIONS)
98 notification_manager_.reset(new StubNotificationUIManager);
99 notification_service()->SetNotificationUIManagerForTesting(
100 notification_manager());
101 #endif
103 InProcessBrowserTest::SetUp();
106 // InProcessBrowserTest:
107 void SetUpOnMainThread() override {
108 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>(
109 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
110 GetBrowser()->profile(), &gcm::FakeGCMProfileService::Build));
111 gcm_service_->set_collect(true);
112 push_service_ =
113 PushMessagingServiceFactory::GetForProfile(GetBrowser()->profile());
115 LoadTestPage();
117 InProcessBrowserTest::SetUpOnMainThread();
120 // InProcessBrowserTest:
121 void TearDown() override {
122 #if defined(ENABLE_NOTIFICATIONS)
123 notification_service()->SetNotificationUIManagerForTesting(nullptr);
124 #endif
126 InProcessBrowserTest::TearDown();
129 void LoadTestPage(const std::string& path) {
130 ui_test_utils::NavigateToURL(GetBrowser(), https_server_->GetURL(path));
133 void LoadTestPage() {
134 LoadTestPage(GetTestURL());
137 bool RunScript(const std::string& script, std::string* result) {
138 return RunScript(script, result, nullptr);
141 bool RunScript(const std::string& script, std::string* result,
142 content::WebContents* web_contents) {
143 if (!web_contents)
144 web_contents = GetBrowser()->tab_strip_model()->GetActiveWebContents();
145 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(),
146 script,
147 result);
150 PermissionBubbleManager* GetPermissionBubbleManager() {
151 return PermissionBubbleManager::FromWebContents(
152 GetBrowser()->tab_strip_model()->GetActiveWebContents());
155 void RequestAndAcceptPermission();
156 void RequestAndDenyPermission();
158 void TryToSubscribeSuccessfully(
159 const std::string& expected_push_subscription_id);
161 std::string GetEndpointForSubscriptionId(const std::string& subscription_id) {
162 return std::string(kPushMessagingEndpoint) + "/" + subscription_id;
165 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration(
166 int64 service_worker_registration_id);
168 void SendMessageAndWaitUntilHandled(
169 const PushMessagingAppIdentifier& app_identifier,
170 const gcm::IncomingMessage& message);
172 net::SpawnedTestServer* https_server() const { return https_server_.get(); }
174 gcm::FakeGCMProfileService* gcm_service() const { return gcm_service_; }
176 #if defined(ENABLE_NOTIFICATIONS)
177 // To be called when delivery of a push message has finished. The |run_loop|
178 // will be told to quit after |messages_required| messages were received.
179 void OnDeliveryFinished(std::vector<size_t>* number_of_notifications_shown,
180 const base::Closure& done_closure) {
181 DCHECK(number_of_notifications_shown);
183 number_of_notifications_shown->push_back(
184 notification_manager_->GetNotificationCount());
186 done_closure.Run();
189 StubNotificationUIManager* notification_manager() const {
190 return notification_manager_.get();
193 PlatformNotificationServiceImpl* notification_service() const {
194 return PlatformNotificationServiceImpl::GetInstance();
196 #endif
198 PushMessagingServiceImpl* push_service() const { return push_service_; }
200 protected:
201 virtual std::string GetTestURL() {
202 return "files/push_messaging/test.html";
205 virtual Browser* GetBrowser() const { return browser(); }
207 private:
208 scoped_ptr<net::SpawnedTestServer> https_server_;
209 gcm::FakeGCMProfileService* gcm_service_;
210 PushMessagingServiceImpl* push_service_;
212 #if defined(ENABLE_NOTIFICATIONS)
213 scoped_ptr<StubNotificationUIManager> notification_manager_;
214 #endif
216 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest);
219 class PushMessagingBrowserTestEmptySubscriptionOptions
220 : public PushMessagingBrowserTest {
221 std::string GetTestURL() override {
222 return "files/push_messaging/test_no_subscription_options.html";
226 void PushMessagingBrowserTest::RequestAndAcceptPermission() {
227 std::string script_result;
228 GetPermissionBubbleManager()->set_auto_response_for_test(
229 PermissionBubbleManager::ACCEPT_ALL);
230 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result));
231 EXPECT_EQ("permission status - granted", script_result);
234 void PushMessagingBrowserTest::RequestAndDenyPermission() {
235 std::string script_result;
236 GetPermissionBubbleManager()->set_auto_response_for_test(
237 PermissionBubbleManager::DENY_ALL);
238 EXPECT_TRUE(RunScript("requestNotificationPermission();", &script_result));
239 EXPECT_EQ("permission status - denied", script_result);
242 void PushMessagingBrowserTest::TryToSubscribeSuccessfully(
243 const std::string& expected_push_subscription_id) {
244 std::string script_result;
246 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result));
247 EXPECT_EQ("ok - service worker registered", script_result);
249 RequestAndAcceptPermission();
251 EXPECT_TRUE(RunScript("subscribePush()", &script_result));
252 EXPECT_EQ(GetEndpointForSubscriptionId(expected_push_subscription_id),
253 script_result);
256 PushMessagingAppIdentifier
257 PushMessagingBrowserTest::GetAppIdentifierForServiceWorkerRegistration(
258 int64 service_worker_registration_id) {
259 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
260 PushMessagingAppIdentifier app_identifier =
261 PushMessagingAppIdentifier::FindByServiceWorker(
262 GetBrowser()->profile(), origin, service_worker_registration_id);
263 EXPECT_FALSE(app_identifier.is_null());
264 return app_identifier;
267 void PushMessagingBrowserTest::SendMessageAndWaitUntilHandled(
268 const PushMessagingAppIdentifier& app_identifier,
269 const gcm::IncomingMessage& message) {
270 base::RunLoop run_loop;
271 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure());
272 push_service()->OnMessage(app_identifier.app_id(), message);
273 run_loop.Run();
276 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
277 SubscribeSuccessNotificationsGranted) {
278 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
280 PushMessagingAppIdentifier app_identifier =
281 GetAppIdentifierForServiceWorkerRegistration(0LL);
282 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
283 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
286 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
287 SubscribeSuccessNotificationsPrompt) {
288 std::string script_result;
290 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
291 ASSERT_EQ("ok - service worker registered", script_result);
293 GetPermissionBubbleManager()->set_auto_response_for_test(
294 PermissionBubbleManager::ACCEPT_ALL);
295 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
296 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"), script_result);
298 PushMessagingAppIdentifier app_identifier =
299 GetAppIdentifierForServiceWorkerRegistration(0LL);
300 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
301 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
304 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
305 SubscribeFailureNotificationsBlocked) {
306 std::string script_result;
308 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
309 ASSERT_EQ("ok - service worker registered", script_result);
311 RequestAndDenyPermission();
313 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
314 EXPECT_EQ("PermissionDeniedError - Registration failed - permission denied",
315 script_result);
318 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeFailureNoManifest) {
319 std::string script_result;
321 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
322 ASSERT_EQ("ok - service worker registered", script_result);
324 RequestAndAcceptPermission();
326 ASSERT_TRUE(RunScript("removeManifest()", &script_result));
327 ASSERT_EQ("manifest removed", script_result);
329 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
330 EXPECT_EQ("AbortError - Registration failed - no sender id provided",
331 script_result);
334 // TODO(johnme): Test subscribing from a worker - see https://crbug.com/437298.
336 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTestEmptySubscriptionOptions,
337 RegisterFailureEmptyPushSubscriptionOptions) {
338 std::string script_result;
340 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
341 ASSERT_EQ("ok - service worker registered", script_result);
343 RequestAndAcceptPermission();
345 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
346 EXPECT_EQ("PermissionDeniedError - Registration failed - permission denied",
347 script_result);
350 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribePersisted) {
351 std::string script_result;
353 // First, test that Service Worker registration IDs are assigned in order of
354 // registering the Service Workers, and the (fake) push subscription ids are
355 // assigned in order of push subscription (even when these orders are
356 // different).
358 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
359 PushMessagingAppIdentifier sw0_identifier =
360 GetAppIdentifierForServiceWorkerRegistration(0LL);
361 EXPECT_EQ(sw0_identifier.app_id(), gcm_service()->last_registered_app_id());
363 LoadTestPage("files/push_messaging/subscope1/test.html");
364 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
365 ASSERT_EQ("ok - service worker registered", script_result);
367 LoadTestPage("files/push_messaging/subscope2/test.html");
368 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
369 ASSERT_EQ("ok - service worker registered", script_result);
371 // Note that we need to reload the page after registering, otherwise
372 // navigator.serviceWorker.ready is going to be resolved with the parent
373 // Service Worker which still controls the page.
374 LoadTestPage("files/push_messaging/subscope2/test.html");
375 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */);
376 PushMessagingAppIdentifier sw2_identifier =
377 GetAppIdentifierForServiceWorkerRegistration(2LL);
378 EXPECT_EQ(sw2_identifier.app_id(), gcm_service()->last_registered_app_id());
380 LoadTestPage("files/push_messaging/subscope1/test.html");
381 TryToSubscribeSuccessfully("1-2" /* expected_push_subscription_id */);
382 PushMessagingAppIdentifier sw1_identifier =
383 GetAppIdentifierForServiceWorkerRegistration(1LL);
384 EXPECT_EQ(sw1_identifier.app_id(), gcm_service()->last_registered_app_id());
386 // Now test that the Service Worker registration IDs and push subscription IDs
387 // generated above were persisted to SW storage, by checking that they are
388 // unchanged despite requesting them in a different order.
389 // TODO(johnme): Ideally we would restart the browser at this point to check
390 // they were persisted to disk, but that's not currently possible since the
391 // test server uses random port numbers for each test (even PRE_Foo and Foo),
392 // so we wouldn't be able to load the test pages with the same origin.
394 LoadTestPage("files/push_messaging/subscope1/test.html");
395 TryToSubscribeSuccessfully("1-2" /* expected_push_subscription_id */);
396 EXPECT_EQ(sw1_identifier.app_id(), gcm_service()->last_registered_app_id());
398 LoadTestPage("files/push_messaging/subscope2/test.html");
399 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */);
400 EXPECT_EQ(sw1_identifier.app_id(), gcm_service()->last_registered_app_id());
402 LoadTestPage();
403 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
404 EXPECT_EQ(sw1_identifier.app_id(), gcm_service()->last_registered_app_id());
407 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventSuccess) {
408 std::string script_result;
410 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
412 PushMessagingAppIdentifier app_identifier =
413 GetAppIdentifierForServiceWorkerRegistration(0LL);
414 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
415 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
417 ASSERT_TRUE(RunScript("isControlled()", &script_result));
418 ASSERT_EQ("false - is not controlled", script_result);
420 LoadTestPage(); // Reload to become controlled.
422 ASSERT_TRUE(RunScript("isControlled()", &script_result));
423 ASSERT_EQ("true - is controlled", script_result);
425 gcm::IncomingMessage message;
426 message.sender_id = "1234567890";
427 message.data["data"] = "testdata";
428 push_service()->OnMessage(app_identifier.app_id(), message);
429 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result));
430 EXPECT_EQ("testdata", script_result);
433 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoServiceWorker) {
434 std::string script_result;
436 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
438 PushMessagingAppIdentifier app_identifier =
439 GetAppIdentifierForServiceWorkerRegistration(0LL);
440 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
441 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
443 ASSERT_TRUE(RunScript("isControlled()", &script_result));
444 ASSERT_EQ("false - is not controlled", script_result);
446 LoadTestPage(); // Reload to become controlled.
448 ASSERT_TRUE(RunScript("isControlled()", &script_result));
449 ASSERT_EQ("true - is controlled", script_result);
451 // Unregister service worker. Sending a message should now fail.
452 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result));
453 ASSERT_EQ("service worker unregistration status: true", script_result);
455 // When the push service will receive it next message, given that there is no
456 // SW available, it should unregister |app_identifier.app_id()|.
457 UnregistrationCallback callback;
458 gcm_service()->SetUnregisterCallback(base::Bind(&UnregistrationCallback::Run,
459 base::Unretained(&callback)));
461 gcm::IncomingMessage message;
462 message.sender_id = "1234567890";
463 message.data["data"] = "testdata";
464 push_service()->OnMessage(app_identifier.app_id(), message);
466 callback.WaitUntilSatisfied();
467 EXPECT_EQ(app_identifier.app_id(), callback.app_id());
469 // No push data should have been received.
470 ASSERT_TRUE(RunScript("resultQueue.popImmediately()", &script_result));
471 EXPECT_EQ("null", script_result);
474 #if defined(ENABLE_NOTIFICATIONS)
475 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
476 PushEventEnforcesUserVisibleNotification) {
477 std::string script_result;
479 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
481 PushMessagingAppIdentifier app_identifier =
482 GetAppIdentifierForServiceWorkerRegistration(0LL);
483 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
484 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
486 ASSERT_TRUE(RunScript("isControlled()", &script_result));
487 ASSERT_EQ("false - is not controlled", script_result);
489 LoadTestPage(); // Reload to become controlled.
491 ASSERT_TRUE(RunScript("isControlled()", &script_result));
492 ASSERT_EQ("true - is controlled", script_result);
494 notification_manager()->CancelAll();
495 ASSERT_EQ(0u, notification_manager()->GetNotificationCount());
497 // We'll need to specify the web_contents in which to eval script, since we're
498 // going to run script in a background tab.
499 content::WebContents* web_contents =
500 GetBrowser()->tab_strip_model()->GetActiveWebContents();
502 // If the site is visible in an active tab, we should not force a notification
503 // to be shown. Try it twice, since we allow one mistake per 10 push events.
504 gcm::IncomingMessage message;
505 message.sender_id = "1234567890";
506 for (int n = 0; n < 2; n++) {
507 message.data["data"] = "testdata";
508 SendMessageAndWaitUntilHandled(app_identifier, message);
509 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result));
510 EXPECT_EQ("testdata", script_result);
511 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
514 // Open a blank foreground tab so site is no longer visible.
515 ui_test_utils::NavigateToURLWithDisposition(
516 GetBrowser(), GURL("about:blank"), NEW_FOREGROUND_TAB,
517 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
519 // If the Service Worker push event handler does not show a notification, we
520 // should show a forced one, but only on the 2nd occurrence since we allow one
521 // mistake per 10 push events.
522 message.data["data"] = "testdata";
523 SendMessageAndWaitUntilHandled(app_identifier, message);
524 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
525 EXPECT_EQ("testdata", script_result);
526 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
527 message.data["data"] = "testdata";
528 SendMessageAndWaitUntilHandled(app_identifier, message);
529 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
530 EXPECT_EQ("testdata", script_result);
532 ASSERT_EQ(1u, notification_manager()->GetNotificationCount());
534 const Notification& forced_notification =
535 notification_manager()->GetNotificationAt(0);
537 EXPECT_EQ(kPushMessagingForcedNotificationTag, forced_notification.tag());
538 EXPECT_TRUE(forced_notification.silent());
541 // The notification will be automatically dismissed when the developer shows
542 // a new notification themselves at a later point in time.
543 message.data["data"] = "shownotification";
544 SendMessageAndWaitUntilHandled(app_identifier, message);
545 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
546 EXPECT_EQ("shownotification", script_result);
548 ASSERT_EQ(1u, notification_manager()->GetNotificationCount());
550 const Notification& first_notification =
551 notification_manager()->GetNotificationAt(0);
553 EXPECT_NE(kPushMessagingForcedNotificationTag, first_notification.tag());
556 notification_manager()->CancelAll();
557 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
559 // However if the Service Worker push event handler shows a notification, we
560 // should not show a forced one.
561 message.data["data"] = "shownotification";
562 for (int n = 0; n < 9; n++) {
563 SendMessageAndWaitUntilHandled(app_identifier, message);
564 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
565 EXPECT_EQ("shownotification", script_result);
566 EXPECT_EQ(1u, notification_manager()->GetNotificationCount());
567 EXPECT_EQ("push_test_tag",
568 notification_manager()->GetNotificationAt(0).tag());
569 notification_manager()->CancelAll();
572 // Now that 10 push messages in a row have shown notifications, we should
573 // allow the next one to mistakenly not show a notification.
574 message.data["data"] = "testdata";
575 SendMessageAndWaitUntilHandled(app_identifier, message);
576 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
577 EXPECT_EQ("testdata", script_result);
578 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
581 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
582 PushEventEnforcesUserVisibleNotificationAfterQueue) {
583 std::string script_result;
585 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
587 PushMessagingAppIdentifier app_identifier =
588 GetAppIdentifierForServiceWorkerRegistration(0LL);
589 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
590 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
592 ASSERT_TRUE(RunScript("isControlled()", &script_result));
593 ASSERT_EQ("false - is not controlled", script_result);
595 LoadTestPage(); // Reload to become controlled.
597 ASSERT_TRUE(RunScript("isControlled()", &script_result));
598 ASSERT_EQ("true - is controlled", script_result);
600 // Fire off two push messages in sequence, only the second one of which will
601 // display a notification. The additional round-trip and I/O required by the
602 // second message, which shows a notification, should give us a reasonable
603 // confidence that the ordering will be maintained.
605 std::vector<size_t> number_of_notifications_shown;
607 gcm::IncomingMessage message;
608 message.sender_id = "1234567890";
611 base::RunLoop run_loop;
612 push_service()->SetMessageCallbackForTesting(
613 base::Bind(&PushMessagingBrowserTest::OnDeliveryFinished,
614 base::Unretained(this),
615 &number_of_notifications_shown,
616 base::BarrierClosure(2 /* num_closures */,
617 run_loop.QuitClosure())));
619 message.data["data"] = "testdata";
620 push_service()->OnMessage(app_identifier.app_id(), message);
622 message.data["data"] = "shownotification";
623 push_service()->OnMessage(app_identifier.app_id(), message);
625 run_loop.Run();
628 ASSERT_EQ(2u, number_of_notifications_shown.size());
629 EXPECT_EQ(0u, number_of_notifications_shown[0]);
630 EXPECT_EQ(1u, number_of_notifications_shown[1]);
633 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
634 PushEventNotificationWithoutEventWaitUntil) {
635 std::string script_result;
636 content::WebContents* web_contents =
637 GetBrowser()->tab_strip_model()->GetActiveWebContents();
639 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
641 PushMessagingAppIdentifier app_identifier =
642 GetAppIdentifierForServiceWorkerRegistration(0LL);
643 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
644 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
646 ASSERT_TRUE(RunScript("isControlled()", &script_result));
647 ASSERT_EQ("false - is not controlled", script_result);
649 LoadTestPage(); // Reload to become controlled.
651 ASSERT_TRUE(RunScript("isControlled()", &script_result));
652 ASSERT_EQ("true - is controlled", script_result);
654 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
655 new content::MessageLoopRunner;
656 notification_manager()->SetNotificationAddedCallback(
657 message_loop_runner->QuitClosure());
659 gcm::IncomingMessage message;
660 message.sender_id = "1234567890";
661 message.data["data"] = "shownotification-without-waituntil";
662 push_service()->OnMessage(app_identifier.app_id(), message);
663 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
664 EXPECT_EQ("immediate:shownotification-without-waituntil", script_result);
666 message_loop_runner->Run();
668 ASSERT_EQ(1u, notification_manager()->GetNotificationCount());
669 EXPECT_EQ("push_test_tag",
670 notification_manager()->GetNotificationAt(0).tag());
672 // Verify that the renderer process hasn't crashed.
673 ASSERT_TRUE(RunScript("permissionState()", &script_result));
674 EXPECT_EQ("permission status - granted", script_result);
676 #endif
678 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PermissionStateSaysPrompt) {
679 std::string script_result;
681 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
682 ASSERT_EQ("ok - service worker registered", script_result);
684 ASSERT_TRUE(RunScript("permissionState()", &script_result));
685 ASSERT_EQ("permission status - prompt", script_result);
688 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PermissionStateSaysGranted) {
689 std::string script_result;
691 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
692 ASSERT_EQ("ok - service worker registered", script_result);
694 RequestAndAcceptPermission();
696 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
697 EXPECT_EQ(GetEndpointForSubscriptionId("1-0"),
698 script_result);
700 ASSERT_TRUE(RunScript("permissionState()", &script_result));
701 EXPECT_EQ("permission status - granted", script_result);
704 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PermissionStateSaysDenied) {
705 std::string script_result;
707 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
708 ASSERT_EQ("ok - service worker registered", script_result);
710 RequestAndDenyPermission();
712 ASSERT_TRUE(RunScript("subscribePush()", &script_result));
713 EXPECT_EQ("PermissionDeniedError - Registration failed - permission denied",
714 script_result);
716 ASSERT_TRUE(RunScript("permissionState()", &script_result));
717 EXPECT_EQ("permission status - denied", script_result);
720 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, UnsubscribeSuccess) {
721 std::string script_result;
723 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result));
724 EXPECT_EQ("ok - service worker registered", script_result);
726 // Resolves true if there was a subscription.
727 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
728 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
729 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
730 EXPECT_EQ("unsubscribe result: true", script_result);
732 // Resolves false if there was no longer a subscription.
733 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
734 EXPECT_EQ("unsubscribe result: false", script_result);
736 // Doesn't reject if there was a network error (deactivates subscription
737 // locally anyway).
738 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */);
739 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::NETWORK_ERROR);
740 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
741 EXPECT_EQ("unsubscribe result: true", script_result);
742 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
743 EXPECT_EQ("false - not subscribed", script_result);
745 // Doesn't reject if there were other push service errors (deactivates
746 // subscription locally anyway).
747 TryToSubscribeSuccessfully("1-2" /* expected_push_subscription_id */);
748 gcm_service()->AddExpectedUnregisterResponse(
749 gcm::GCMClient::INVALID_PARAMETER);
750 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
751 EXPECT_EQ("unsubscribe result: true", script_result);
753 // Unsubscribing (with an existing reference to a PushSubscription), after
754 // unregistering the Service Worker, just means push subscription isn't found.
755 TryToSubscribeSuccessfully("1-3" /* expected_push_subscription_id */);
756 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result));
757 ASSERT_EQ("service worker unregistration status: true", script_result);
758 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
759 EXPECT_EQ("unsubscribe result: false", script_result);
762 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
763 GlobalResetPushPermissionUnsubscribes) {
764 std::string script_result;
766 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
768 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
769 EXPECT_EQ("true - subscribed", script_result);
771 ASSERT_TRUE(RunScript("permissionState()", &script_result));
772 EXPECT_EQ("permission status - granted", script_result);
774 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
775 new content::MessageLoopRunner;
776 push_service()->SetContentSettingChangedCallbackForTesting(
777 message_loop_runner->QuitClosure());
779 GetBrowser()->profile()->GetHostContentSettingsMap()->
780 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING);
782 message_loop_runner->Run();
784 ASSERT_TRUE(RunScript("permissionState()", &script_result));
785 EXPECT_EQ("permission status - prompt", script_result);
787 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
788 EXPECT_EQ("false - not subscribed", script_result);
791 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
792 LocalResetPushPermissionUnsubscribes) {
793 std::string script_result;
795 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
797 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
798 EXPECT_EQ("true - subscribed", script_result);
800 ASSERT_TRUE(RunScript("permissionState()", &script_result));
801 EXPECT_EQ("permission status - granted", script_result);
803 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
804 new content::MessageLoopRunner;
805 push_service()->SetContentSettingChangedCallbackForTesting(
806 message_loop_runner->QuitClosure());
808 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
809 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
810 ContentSettingsPattern::FromURLNoWildcard(origin),
811 ContentSettingsPattern::FromURLNoWildcard(origin),
812 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
813 std::string(),
814 CONTENT_SETTING_DEFAULT);
816 message_loop_runner->Run();
818 ASSERT_TRUE(RunScript("permissionState()", &script_result));
819 EXPECT_EQ("permission status - prompt", script_result);
821 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
822 EXPECT_EQ("false - not subscribed", script_result);
825 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
826 DenyPushPermissionUnsubscribes) {
827 std::string script_result;
829 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
831 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
832 EXPECT_EQ("true - subscribed", script_result);
834 ASSERT_TRUE(RunScript("permissionState()", &script_result));
835 EXPECT_EQ("permission status - granted", script_result);
837 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
838 new content::MessageLoopRunner;
839 push_service()->SetContentSettingChangedCallbackForTesting(
840 message_loop_runner->QuitClosure());
842 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
843 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
844 ContentSettingsPattern::FromURLNoWildcard(origin),
845 ContentSettingsPattern::FromURLNoWildcard(origin),
846 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
847 std::string(),
848 CONTENT_SETTING_BLOCK);
850 message_loop_runner->Run();
852 ASSERT_TRUE(RunScript("permissionState()", &script_result));
853 EXPECT_EQ("permission status - denied", script_result);
855 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
856 EXPECT_EQ("false - not subscribed", script_result);
859 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
860 GlobalResetNotificationsPermissionUnsubscribes) {
861 std::string script_result;
863 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
865 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
866 EXPECT_EQ("true - subscribed", script_result);
868 ASSERT_TRUE(RunScript("permissionState()", &script_result));
869 EXPECT_EQ("permission status - granted", script_result);
871 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
872 new content::MessageLoopRunner;
873 push_service()->SetContentSettingChangedCallbackForTesting(
874 message_loop_runner->QuitClosure());
876 GetBrowser()->profile()->GetHostContentSettingsMap()->
877 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
879 message_loop_runner->Run();
881 ASSERT_TRUE(RunScript("permissionState()", &script_result));
882 EXPECT_EQ("permission status - prompt", script_result);
884 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
885 EXPECT_EQ("false - not subscribed", script_result);
888 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
889 LocalResetNotificationsPermissionUnsubscribes) {
890 std::string script_result;
892 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
894 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
895 EXPECT_EQ("true - subscribed", script_result);
897 ASSERT_TRUE(RunScript("permissionState()", &script_result));
898 EXPECT_EQ("permission status - granted", script_result);
900 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
901 new content::MessageLoopRunner;
902 push_service()->SetContentSettingChangedCallbackForTesting(
903 message_loop_runner->QuitClosure());
905 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
906 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
907 ContentSettingsPattern::FromURLNoWildcard(origin),
908 ContentSettingsPattern::Wildcard(),
909 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
910 std::string(),
911 CONTENT_SETTING_DEFAULT);
913 message_loop_runner->Run();
915 ASSERT_TRUE(RunScript("permissionState()", &script_result));
916 EXPECT_EQ("permission status - prompt", script_result);
918 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
919 EXPECT_EQ("false - not subscribed", script_result);
922 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
923 DenyNotificationsPermissionUnsubscribes) {
924 std::string script_result;
926 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
928 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
929 EXPECT_EQ("true - subscribed", script_result);
931 ASSERT_TRUE(RunScript("permissionState()", &script_result));
932 EXPECT_EQ("permission status - granted", script_result);
934 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
935 new content::MessageLoopRunner;
936 push_service()->SetContentSettingChangedCallbackForTesting(
937 message_loop_runner->QuitClosure());
939 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
940 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
941 ContentSettingsPattern::FromURLNoWildcard(origin),
942 ContentSettingsPattern::Wildcard(),
943 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
944 std::string(),
945 CONTENT_SETTING_BLOCK);
947 message_loop_runner->Run();
949 ASSERT_TRUE(RunScript("permissionState()", &script_result));
950 EXPECT_EQ("permission status - denied", script_result);
952 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
953 EXPECT_EQ("false - not subscribed", script_result);
956 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
957 GrantAlreadyGrantedPermissionDoesNotUnsubscribe) {
958 std::string script_result;
960 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
962 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
963 EXPECT_EQ("true - subscribed", script_result);
965 ASSERT_TRUE(RunScript("permissionState()", &script_result));
966 EXPECT_EQ("permission status - granted", script_result);
968 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
969 new content::MessageLoopRunner;
970 push_service()->SetContentSettingChangedCallbackForTesting(
971 base::BarrierClosure(2, message_loop_runner->QuitClosure()));
973 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
974 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
975 ContentSettingsPattern::FromURLNoWildcard(origin),
976 ContentSettingsPattern::Wildcard(),
977 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
978 std::string(),
979 CONTENT_SETTING_ALLOW);
980 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
981 ContentSettingsPattern::FromURLNoWildcard(origin),
982 ContentSettingsPattern::FromURLNoWildcard(origin),
983 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
984 std::string(),
985 CONTENT_SETTING_ALLOW);
987 message_loop_runner->Run();
989 ASSERT_TRUE(RunScript("permissionState()", &script_result));
990 EXPECT_EQ("permission status - granted", script_result);
992 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
993 EXPECT_EQ("true - subscribed", script_result);
996 // This test is testing some non-trivial content settings rules and make sure
997 // that they are respected with regards to automatic unsubscription. In other
998 // words, it checks that the push service does not end up unsubscribing origins
999 // that have push permission with some non-common rules.
1000 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1001 AutomaticUnsubscriptionFollowsContentSettingRules) {
1002 std::string script_result;
1004 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
1006 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1007 EXPECT_EQ("true - subscribed", script_result);
1009 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1010 EXPECT_EQ("permission status - granted", script_result);
1012 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
1013 new content::MessageLoopRunner;
1014 push_service()->SetContentSettingChangedCallbackForTesting(
1015 base::BarrierClosure(4, message_loop_runner->QuitClosure()));
1017 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
1018 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
1019 ContentSettingsPattern::Wildcard(),
1020 ContentSettingsPattern::Wildcard(),
1021 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
1022 std::string(),
1023 CONTENT_SETTING_ALLOW);
1024 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
1025 ContentSettingsPattern::FromString("https://*"),
1026 ContentSettingsPattern::FromString("https://*"),
1027 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
1028 std::string(),
1029 CONTENT_SETTING_ALLOW);
1030 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
1031 ContentSettingsPattern::FromURLNoWildcard(origin),
1032 ContentSettingsPattern::Wildcard(),
1033 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
1034 std::string(),
1035 CONTENT_SETTING_DEFAULT);
1036 GetBrowser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
1037 ContentSettingsPattern::FromURLNoWildcard(origin),
1038 ContentSettingsPattern::FromURLNoWildcard(origin),
1039 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
1040 std::string(),
1041 CONTENT_SETTING_DEFAULT);
1043 message_loop_runner->Run();
1045 // The two first rules should give |origin| the permission to use Push even
1046 // if the rules it used to have have been reset.
1047 // The Push service should not unsubcribe |origin| because at no point it was
1048 // left without permission to use Push.
1050 ASSERT_TRUE(RunScript("permissionState()", &script_result));
1051 EXPECT_EQ("permission status - granted", script_result);
1053 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1054 EXPECT_EQ("true - subscribed", script_result);
1057 // Checks that automatically unsubscribing due to a revoked permission is
1058 // handled well if the sender ID needed to unsubscribe was already deleted.
1059 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1060 ResetPushPermissionAfterClearingSiteData) {
1061 std::string script_result;
1063 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
1065 PushMessagingAppIdentifier app_identifier =
1066 GetAppIdentifierForServiceWorkerRegistration(0LL);
1067 EXPECT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
1068 PushMessagingAppIdentifier stored_app_identifier =
1069 PushMessagingAppIdentifier::FindByAppId(GetBrowser()->profile(),
1070 app_identifier.app_id());
1071 EXPECT_FALSE(stored_app_identifier.is_null());
1073 // Simulate a user clearing site data (including Service Workers, crucially).
1074 BrowsingDataRemover* remover =
1075 BrowsingDataRemover::CreateForUnboundedRange(GetBrowser()->profile());
1076 BrowsingDataRemoverCompletionObserver observer(remover);
1077 remover->Remove(BrowsingDataRemover::REMOVE_SITE_DATA,
1078 BrowsingDataHelper::UNPROTECTED_WEB);
1079 observer.BlockUntilCompletion();
1080 // BrowsingDataRemover deletes itself.
1082 base::RunLoop run_loop;
1083 push_service()->SetContentSettingChangedCallbackForTesting(
1084 run_loop.QuitClosure());
1086 // This shouldn't (asynchronously) cause a DCHECK.
1087 // TODO(johnme): Get this test running on Android, which has a different
1088 // codepath due to sender_id being required for unsubscribing there.
1089 GetBrowser()->profile()->GetHostContentSettingsMap()->
1090 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING);
1092 run_loop.Run();
1094 // |app_identifier| should no longer be stored in prefs.
1095 PushMessagingAppIdentifier stored_app_identifier2 =
1096 PushMessagingAppIdentifier::FindByAppId(GetBrowser()->profile(),
1097 app_identifier.app_id());
1098 EXPECT_TRUE(stored_app_identifier2.is_null());
1101 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, EncryptionKeyUniqueness) {
1102 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
1104 std::string first_public_key;
1105 ASSERT_TRUE(RunScript("getCurve25519dh()", &first_public_key));
1106 EXPECT_GE(first_public_key.size(), 32u);
1108 std::string script_result;
1109 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
1110 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1111 EXPECT_EQ("unsubscribe result: true", script_result);
1113 TryToSubscribeSuccessfully("1-1" /* expected_push_subscription_id */);
1115 std::string second_public_key;
1116 ASSERT_TRUE(RunScript("getCurve25519dh()", &second_public_key));
1117 EXPECT_GE(second_public_key.size(), 32u);
1119 EXPECT_NE(first_public_key, second_public_key);
1122 class PushMessagingIncognitoBrowserTest : public PushMessagingBrowserTest {
1123 public:
1124 ~PushMessagingIncognitoBrowserTest() override {}
1126 // PushMessagingBrowserTest:
1127 void SetUpOnMainThread() override {
1128 incognito_browser_ = CreateIncognitoBrowser();
1129 PushMessagingBrowserTest::SetUpOnMainThread();
1132 Browser* GetBrowser() const override { return incognito_browser_; }
1134 private:
1135 Browser* incognito_browser_ = nullptr;
1138 // Regression test for https://crbug.com/476474
1139 IN_PROC_BROWSER_TEST_F(PushMessagingIncognitoBrowserTest,
1140 IncognitoGetSubscriptionDoesNotHang) {
1141 ASSERT_TRUE(GetBrowser()->profile()->IsOffTheRecord());
1143 std::string script_result;
1145 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
1146 ASSERT_EQ("ok - service worker registered", script_result);
1148 // In Incognito mode the promise returned by getSubscription should not hang,
1149 // it should just fulfill with null.
1150 ASSERT_TRUE(RunScript("hasSubscription()", &script_result));
1151 ASSERT_EQ("false - not subscribed", script_result);
1154 // None of the following should matter on ChromeOS: crbug.com/527045
1155 #if defined(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS)
1156 // Push background mode is disabled by default.
1157 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
1158 BackgroundModeDisabledByDefault) {
1159 // Initially background mode is inactive.
1160 BackgroundModeManager* background_mode_manager =
1161 g_browser_process->background_mode_manager();
1162 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1164 // Once there is a push subscription background mode is still inactive.
1165 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
1166 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1168 // After dropping the last subscription it is still inactive.
1169 std::string script_result;
1170 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
1171 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1172 EXPECT_EQ("unsubscribe result: true", script_result);
1173 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1176 class PushMessagingBackgroundModeEnabledBrowserTest
1177 : public PushMessagingBrowserTest {
1178 public:
1179 ~PushMessagingBackgroundModeEnabledBrowserTest() override {}
1181 // PushMessagingBrowserTest:
1182 void SetUpCommandLine(base::CommandLine* command_line) override {
1183 command_line->AppendSwitch(switches::kEnablePushApiBackgroundMode);
1184 PushMessagingBrowserTest::SetUpCommandLine(command_line);
1188 // In this test the command line enables push background mode.
1189 IN_PROC_BROWSER_TEST_F(PushMessagingBackgroundModeEnabledBrowserTest,
1190 BackgroundModeEnabledWithCommandLine) {
1191 // Initially background mode is inactive.
1192 BackgroundModeManager* background_mode_manager =
1193 g_browser_process->background_mode_manager();
1194 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1196 // Once there is a push subscription background mode is active.
1197 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
1198 ASSERT_TRUE(background_mode_manager->IsBackgroundModeActive());
1200 // Dropping the last subscription deactivates background mode.
1201 std::string script_result;
1202 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
1203 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1204 EXPECT_EQ("unsubscribe result: true", script_result);
1205 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1208 class PushMessagingBackgroundModeDisabledBrowserTest
1209 : public PushMessagingBrowserTest {
1210 public:
1211 ~PushMessagingBackgroundModeDisabledBrowserTest() override {}
1213 // PushMessagingBrowserTest:
1214 void SetUpCommandLine(base::CommandLine* command_line) override {
1215 command_line->AppendSwitch(switches::kDisablePushApiBackgroundMode);
1216 PushMessagingBrowserTest::SetUpCommandLine(command_line);
1220 // In this test the command line disables push background mode.
1221 IN_PROC_BROWSER_TEST_F(PushMessagingBackgroundModeDisabledBrowserTest,
1222 BackgroundModeDisabledWithCommandLine) {
1223 // Initially background mode is inactive.
1224 BackgroundModeManager* background_mode_manager =
1225 g_browser_process->background_mode_manager();
1226 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1228 // Once there is a push subscription background mode is still inactive.
1229 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */);
1230 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1232 // After dropping the last subscription background mode is still inactive.
1233 std::string script_result;
1234 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
1235 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1236 EXPECT_EQ("unsubscribe result: true", script_result);
1237 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1239 #endif // defined(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS)