| 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/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 | 492 |
| 493 notification_manager()->CancelAll(); | 493 notification_manager()->CancelAll(); |
| 494 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); | 494 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 495 | 495 |
| 496 // We'll need to specify the web_contents in which to eval script, since we're | 496 // We'll need to specify the web_contents in which to eval script, since we're |
| 497 // going to run script in a background tab. | 497 // going to run script in a background tab. |
| 498 content::WebContents* web_contents = | 498 content::WebContents* web_contents = |
| 499 browser()->tab_strip_model()->GetActiveWebContents(); | 499 browser()->tab_strip_model()->GetActiveWebContents(); |
| 500 | 500 |
| 501 // If the site is visible in an active tab, we should not force a notification | 501 // If the site is visible in an active tab, we should not force a notification |
| 502 // to be shown. | 502 // to be shown. Try it twice, since we allow one mistake per 10 push events. |
| 503 GCMClient::IncomingMessage message; | 503 GCMClient::IncomingMessage message; |
| 504 message.data["data"] = "testdata"; | 504 for (int n = 0; n < 2; n++) { |
| 505 push_service()->OnMessage(app_id.ToString(), message); | 505 message.data["data"] = "testdata"; |
| 506 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); | 506 push_service()->OnMessage(app_id.ToString(), message); |
| 507 EXPECT_EQ("testdata", script_result); | 507 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); |
| 508 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); | 508 EXPECT_EQ("testdata", script_result); |
| 509 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 510 } |
| 509 | 511 |
| 510 // Open a blank foreground tab so site is no longer visible. | 512 // Open a blank foreground tab so site is no longer visible. |
| 511 ui_test_utils::NavigateToURLWithDisposition( | 513 ui_test_utils::NavigateToURLWithDisposition( |
| 512 browser(), GURL("about:blank"), NEW_FOREGROUND_TAB, | 514 browser(), GURL("about:blank"), NEW_FOREGROUND_TAB, |
| 513 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | 515 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); |
| 514 | 516 |
| 515 // If the Service Worker push event handler shows a notification, we should | 517 // If the Service Worker push event handler does not show a notification, we |
| 516 // not show a forced one. | 518 // should show a forced one, but only on the 2nd occurrence since we allow one |
| 517 message.data["data"] = "shownotification"; | 519 // mistake per 10 push events. |
| 518 push_service()->OnMessage(app_id.ToString(), message); | |
| 519 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | |
| 520 EXPECT_EQ("shownotification", script_result); | |
| 521 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); | |
| 522 EXPECT_EQ(base::ASCIIToUTF16("push_test_tag"), | |
| 523 notification_manager()->GetNotificationAt(0).replace_id()); | |
| 524 | |
| 525 notification_manager()->CancelAll(); | |
| 526 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); | |
| 527 | |
| 528 // However if the Service Worker push event handler does not show a | |
| 529 // notification, we should show a forced one. | |
| 530 message.data["data"] = "testdata"; | 520 message.data["data"] = "testdata"; |
| 531 push_service()->OnMessage(app_id.ToString(), message); | 521 push_service()->OnMessage(app_id.ToString(), message); |
| 532 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | 522 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 533 EXPECT_EQ("testdata", script_result); | 523 EXPECT_EQ("testdata", script_result); |
| 534 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); | 524 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 525 message.data["data"] = "testdata"; |
| 526 push_service()->OnMessage(app_id.ToString(), message); |
| 527 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 528 EXPECT_EQ("testdata", script_result); |
| 529 EXPECT_EQ(1u, notification_manager()->GetNotificationCount()); |
| 535 EXPECT_EQ(base::ASCIIToUTF16(kPushMessagingForcedNotificationTag), | 530 EXPECT_EQ(base::ASCIIToUTF16(kPushMessagingForcedNotificationTag), |
| 536 notification_manager()->GetNotificationAt(0).replace_id()); | 531 notification_manager()->GetNotificationAt(0).replace_id()); |
| 537 | 532 |
| 538 // Currently, this notification will stick around until the user or webapp | 533 // Currently, this notification will stick around until the user or webapp |
| 539 // explicitly dismisses it (though we may change this later). | 534 // explicitly dismisses it (though we may change this later). |
| 540 message.data["data"] = "shownotification"; | 535 message.data["data"] = "shownotification"; |
| 541 push_service()->OnMessage(app_id.ToString(), message); | 536 push_service()->OnMessage(app_id.ToString(), message); |
| 542 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | 537 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 543 EXPECT_EQ("shownotification", script_result); | 538 EXPECT_EQ("shownotification", script_result); |
| 544 ASSERT_EQ(2u, notification_manager()->GetNotificationCount()); | 539 EXPECT_EQ(2u, notification_manager()->GetNotificationCount()); |
| 540 |
| 541 notification_manager()->CancelAll(); |
| 542 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 543 |
| 544 // However if the Service Worker push event handler shows a notification, we |
| 545 // should not show a forced one. |
| 546 message.data["data"] = "shownotification"; |
| 547 for (int n = 0; n < 9; n++) { |
| 548 push_service()->OnMessage(app_id.ToString(), message); |
| 549 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 550 EXPECT_EQ("shownotification", script_result); |
| 551 EXPECT_EQ(1u, notification_manager()->GetNotificationCount()); |
| 552 EXPECT_EQ(base::ASCIIToUTF16("push_test_tag"), |
| 553 notification_manager()->GetNotificationAt(0).replace_id()); |
| 554 notification_manager()->CancelAll(); |
| 555 } |
| 556 |
| 557 // Now that 10 push messages in a row have shown notifications, we should |
| 558 // allow the next one to mistakenly not show a notification. |
| 559 message.data["data"] = "testdata"; |
| 560 push_service()->OnMessage(app_id.ToString(), message); |
| 561 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 562 EXPECT_EQ("testdata", script_result); |
| 563 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 545 } | 564 } |
| 546 #endif | 565 #endif |
| 547 | 566 |
| 548 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, HasPermissionSaysDefault) { | 567 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, HasPermissionSaysDefault) { |
| 549 std::string script_result; | 568 std::string script_result; |
| 550 | 569 |
| 551 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | 570 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 552 ASSERT_EQ("ok - service worker registered", script_result); | 571 ASSERT_EQ("ok - service worker registered", script_result); |
| 553 | 572 |
| 554 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | 573 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 621 | 640 |
| 622 gcm_service()->AddExpectedUnregisterResponse(GCMClient::UNKNOWN_ERROR); | 641 gcm_service()->AddExpectedUnregisterResponse(GCMClient::UNKNOWN_ERROR); |
| 623 | 642 |
| 624 ASSERT_TRUE(RunScript("unregister()", &script_result)); | 643 ASSERT_TRUE(RunScript("unregister()", &script_result)); |
| 625 EXPECT_EQ("unregister error: " | 644 EXPECT_EQ("unregister error: " |
| 626 "UnknownError: Unexpected error while trying to unregister from the" | 645 "UnknownError: Unexpected error while trying to unregister from the" |
| 627 " push server.", script_result); | 646 " push server.", script_result); |
| 628 } | 647 } |
| 629 | 648 |
| 630 } // namespace gcm | 649 } // namespace gcm |
| OLD | NEW |