| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <map> | 5 #include <map> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/barrier_closure.h" | 8 #include "base/barrier_closure.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 script, | 192 script, |
| 193 result); | 193 result); |
| 194 } | 194 } |
| 195 | 195 |
| 196 void TryToRegisterSuccessfully( | 196 void TryToRegisterSuccessfully( |
| 197 const std::string& expected_push_registration_id); | 197 const std::string& expected_push_registration_id); |
| 198 | 198 |
| 199 PushMessagingApplicationId GetServiceWorkerAppId( | 199 PushMessagingApplicationId GetServiceWorkerAppId( |
| 200 int64 service_worker_registration_id); | 200 int64 service_worker_registration_id); |
| 201 | 201 |
| 202 void SendMessageAndWaitUntilHandled( |
| 203 const PushMessagingApplicationId& app_id, |
| 204 const gcm::GCMClient::IncomingMessage& message); |
| 205 |
| 202 net::SpawnedTestServer* https_server() const { return https_server_.get(); } | 206 net::SpawnedTestServer* https_server() const { return https_server_.get(); } |
| 203 | 207 |
| 204 gcm::FakeGCMProfileService* gcm_service() const { return gcm_service_; } | 208 gcm::FakeGCMProfileService* gcm_service() const { return gcm_service_; } |
| 205 | 209 |
| 206 #if defined(ENABLE_NOTIFICATIONS) | 210 #if defined(ENABLE_NOTIFICATIONS) |
| 207 StubNotificationUIManager* notification_manager() const { | 211 StubNotificationUIManager* notification_manager() const { |
| 208 return notification_manager_.get(); | 212 return notification_manager_.get(); |
| 209 } | 213 } |
| 210 | 214 |
| 211 PlatformNotificationServiceImpl* notification_service() const { | 215 PlatformNotificationServiceImpl* notification_service() const { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 | 268 |
| 265 PushMessagingApplicationId PushMessagingBrowserTest::GetServiceWorkerAppId( | 269 PushMessagingApplicationId PushMessagingBrowserTest::GetServiceWorkerAppId( |
| 266 int64 service_worker_registration_id) { | 270 int64 service_worker_registration_id) { |
| 267 GURL origin = https_server()->GetURL(std::string()).GetOrigin(); | 271 GURL origin = https_server()->GetURL(std::string()).GetOrigin(); |
| 268 PushMessagingApplicationId application_id = PushMessagingApplicationId::Get( | 272 PushMessagingApplicationId application_id = PushMessagingApplicationId::Get( |
| 269 browser()->profile(), origin, service_worker_registration_id); | 273 browser()->profile(), origin, service_worker_registration_id); |
| 270 EXPECT_TRUE(application_id.IsValid()); | 274 EXPECT_TRUE(application_id.IsValid()); |
| 271 return application_id; | 275 return application_id; |
| 272 } | 276 } |
| 273 | 277 |
| 278 void PushMessagingBrowserTest::SendMessageAndWaitUntilHandled( |
| 279 const PushMessagingApplicationId& app_id, |
| 280 const gcm::GCMClient::IncomingMessage& message) { |
| 281 base::RunLoop run_loop; |
| 282 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure()); |
| 283 push_service()->OnMessage(app_id.app_id_guid(), message); |
| 284 run_loop.Run(); |
| 285 } |
| 286 |
| 274 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | 287 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, |
| 275 RegisterSuccessNotificationsGranted) { | 288 RegisterSuccessNotificationsGranted) { |
| 276 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | 289 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); |
| 277 | 290 |
| 278 PushMessagingApplicationId app_id = GetServiceWorkerAppId(0LL); | 291 PushMessagingApplicationId app_id = GetServiceWorkerAppId(0LL); |
| 279 EXPECT_EQ(app_id.app_id_guid(), gcm_service()->last_registered_app_id()); | 292 EXPECT_EQ(app_id.app_id_guid(), gcm_service()->last_registered_app_id()); |
| 280 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); | 293 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); |
| 281 } | 294 } |
| 282 | 295 |
| 283 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | 296 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 477 // going to run script in a background tab. | 490 // going to run script in a background tab. |
| 478 content::WebContents* web_contents = | 491 content::WebContents* web_contents = |
| 479 browser()->tab_strip_model()->GetActiveWebContents(); | 492 browser()->tab_strip_model()->GetActiveWebContents(); |
| 480 | 493 |
| 481 // If the site is visible in an active tab, we should not force a notification | 494 // If the site is visible in an active tab, we should not force a notification |
| 482 // to be shown. Try it twice, since we allow one mistake per 10 push events. | 495 // to be shown. Try it twice, since we allow one mistake per 10 push events. |
| 483 gcm::GCMClient::IncomingMessage message; | 496 gcm::GCMClient::IncomingMessage message; |
| 484 message.sender_id = "1234567890"; | 497 message.sender_id = "1234567890"; |
| 485 for (int n = 0; n < 2; n++) { | 498 for (int n = 0; n < 2; n++) { |
| 486 message.data["data"] = "testdata"; | 499 message.data["data"] = "testdata"; |
| 487 push_service()->OnMessage(app_id.app_id_guid(), message); | 500 SendMessageAndWaitUntilHandled(app_id, message); |
| 488 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); | 501 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); |
| 489 EXPECT_EQ("testdata", script_result); | 502 EXPECT_EQ("testdata", script_result); |
| 490 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); | 503 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 491 } | 504 } |
| 492 | 505 |
| 493 // Open a blank foreground tab so site is no longer visible. | 506 // Open a blank foreground tab so site is no longer visible. |
| 494 ui_test_utils::NavigateToURLWithDisposition( | 507 ui_test_utils::NavigateToURLWithDisposition( |
| 495 browser(), GURL("about:blank"), NEW_FOREGROUND_TAB, | 508 browser(), GURL("about:blank"), NEW_FOREGROUND_TAB, |
| 496 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | 509 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); |
| 497 | 510 |
| 498 // If the Service Worker push event handler does not show a notification, we | 511 // If the Service Worker push event handler does not show a notification, we |
| 499 // should show a forced one, but only on the 2nd occurrence since we allow one | 512 // should show a forced one, but only on the 2nd occurrence since we allow one |
| 500 // mistake per 10 push events. | 513 // mistake per 10 push events. |
| 501 message.data["data"] = "testdata"; | 514 message.data["data"] = "testdata"; |
| 502 push_service()->OnMessage(app_id.app_id_guid(), message); | 515 SendMessageAndWaitUntilHandled(app_id, message); |
| 503 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | 516 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 504 EXPECT_EQ("testdata", script_result); | 517 EXPECT_EQ("testdata", script_result); |
| 505 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); | 518 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 506 message.data["data"] = "testdata"; | 519 message.data["data"] = "testdata"; |
| 507 push_service()->OnMessage(app_id.app_id_guid(), message); | 520 SendMessageAndWaitUntilHandled(app_id, message); |
| 508 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | 521 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 509 EXPECT_EQ("testdata", script_result); | 522 EXPECT_EQ("testdata", script_result); |
| 510 | 523 |
| 511 EXPECT_EQ(1u, notification_manager()->GetNotificationCount()); | 524 EXPECT_EQ(1u, notification_manager()->GetNotificationCount()); |
| 512 const Notification& forced_notification = | 525 const Notification& forced_notification = |
| 513 notification_manager()->GetNotificationAt(0); | 526 notification_manager()->GetNotificationAt(0); |
| 514 | 527 |
| 515 EXPECT_EQ(kPushMessagingForcedNotificationTag, forced_notification.tag()); | 528 EXPECT_EQ(kPushMessagingForcedNotificationTag, forced_notification.tag()); |
| 516 EXPECT_TRUE(forced_notification.silent()); | 529 EXPECT_TRUE(forced_notification.silent()); |
| 517 | 530 |
| 518 // Currently, this notification will stick around until the user or webapp | 531 // Currently, this notification will stick around until the user or webapp |
| 519 // explicitly dismisses it (though we may change this later). | 532 // explicitly dismisses it (though we may change this later). |
| 520 message.data["data"] = "shownotification"; | 533 message.data["data"] = "shownotification"; |
| 521 push_service()->OnMessage(app_id.app_id_guid(), message); | 534 SendMessageAndWaitUntilHandled(app_id, message); |
| 522 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | 535 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 523 EXPECT_EQ("shownotification", script_result); | 536 EXPECT_EQ("shownotification", script_result); |
| 524 EXPECT_EQ(2u, notification_manager()->GetNotificationCount()); | 537 EXPECT_EQ(2u, notification_manager()->GetNotificationCount()); |
| 525 | 538 |
| 526 notification_manager()->CancelAll(); | 539 notification_manager()->CancelAll(); |
| 527 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); | 540 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 528 | 541 |
| 529 // However if the Service Worker push event handler shows a notification, we | 542 // However if the Service Worker push event handler shows a notification, we |
| 530 // should not show a forced one. | 543 // should not show a forced one. |
| 531 message.data["data"] = "shownotification"; | 544 message.data["data"] = "shownotification"; |
| 532 for (int n = 0; n < 9; n++) { | 545 for (int n = 0; n < 9; n++) { |
| 533 push_service()->OnMessage(app_id.app_id_guid(), message); | 546 SendMessageAndWaitUntilHandled(app_id, message); |
| 534 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | 547 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 535 EXPECT_EQ("shownotification", script_result); | 548 EXPECT_EQ("shownotification", script_result); |
| 536 EXPECT_EQ(1u, notification_manager()->GetNotificationCount()); | 549 EXPECT_EQ(1u, notification_manager()->GetNotificationCount()); |
| 537 EXPECT_EQ("push_test_tag", | 550 EXPECT_EQ("push_test_tag", |
| 538 notification_manager()->GetNotificationAt(0).tag()); | 551 notification_manager()->GetNotificationAt(0).tag()); |
| 539 notification_manager()->CancelAll(); | 552 notification_manager()->CancelAll(); |
| 540 } | 553 } |
| 541 | 554 |
| 542 // Now that 10 push messages in a row have shown notifications, we should | 555 // Now that 10 push messages in a row have shown notifications, we should |
| 543 // allow the next one to mistakenly not show a notification. | 556 // allow the next one to mistakenly not show a notification. |
| 544 message.data["data"] = "testdata"; | 557 message.data["data"] = "testdata"; |
| 545 push_service()->OnMessage(app_id.app_id_guid(), message); | 558 SendMessageAndWaitUntilHandled(app_id, message); |
| 546 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | 559 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 547 EXPECT_EQ("testdata", script_result); | 560 EXPECT_EQ("testdata", script_result); |
| 548 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); | 561 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 549 } | 562 } |
| 550 | 563 |
| 551 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | 564 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, |
| 552 PushEventNotificationWithoutEventWaitUntil) { | 565 PushEventNotificationWithoutEventWaitUntil) { |
| 553 std::string script_result; | 566 std::string script_result; |
| 554 content::WebContents* web_contents = | 567 content::WebContents* web_contents = |
| 555 browser()->tab_strip_model()->GetActiveWebContents(); | 568 browser()->tab_strip_model()->GetActiveWebContents(); |
| (...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1007 browser()->profile()->GetHostContentSettingsMap()-> | 1020 browser()->profile()->GetHostContentSettingsMap()-> |
| 1008 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); | 1021 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); |
| 1009 | 1022 |
| 1010 run_loop.Run(); | 1023 run_loop.Run(); |
| 1011 | 1024 |
| 1012 // app_id should no longer be stored in prefs | 1025 // app_id should no longer be stored in prefs |
| 1013 PushMessagingApplicationId stored_app_id2 = PushMessagingApplicationId::Get( | 1026 PushMessagingApplicationId stored_app_id2 = PushMessagingApplicationId::Get( |
| 1014 browser()->profile(), app_id.app_id_guid()); | 1027 browser()->profile(), app_id.app_id_guid()); |
| 1015 EXPECT_FALSE(stored_app_id2.IsValid()); | 1028 EXPECT_FALSE(stored_app_id2.IsValid()); |
| 1016 } | 1029 } |
| OLD | NEW |