| 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 InfoBarService* infobar_service_; | 82 InfoBarService* infobar_service_; |
| 83 bool accept_; | 83 bool accept_; |
| 84 bool has_observed_; | 84 bool has_observed_; |
| 85 }; | 85 }; |
| 86 | 86 |
| 87 // Class to instantiate on the stack that is meant to be used with | 87 // Class to instantiate on the stack that is meant to be used with |
| 88 // FakeGCMProfileService. The ::Run() method follows the signature of | 88 // FakeGCMProfileService. The ::Run() method follows the signature of |
| 89 // FakeGCMProfileService::UnregisterCallback. | 89 // FakeGCMProfileService::UnregisterCallback. |
| 90 class UnregistrationCallback { | 90 class UnregistrationCallback { |
| 91 public: | 91 public: |
| 92 UnregistrationCallback() : done_(false) {} | 92 UnregistrationCallback() : done_(false), waiting_(false) {} |
| 93 | 93 |
| 94 void Run(const std::string& app_id) { | 94 void Run(const std::string& app_id) { |
| 95 app_id_ = app_id; | 95 app_id_ = app_id; |
| 96 done_ = true; | 96 done_ = true; |
| 97 base::MessageLoop::current()->Quit(); | 97 if (waiting_) |
| 98 base::MessageLoop::current()->Quit(); |
| 98 } | 99 } |
| 99 | 100 |
| 100 void WaitUntilSatisfied() { | 101 void WaitUntilSatisfied() { |
| 102 if (done_) |
| 103 return; |
| 104 |
| 105 waiting_ = true; |
| 101 while (!done_) | 106 while (!done_) |
| 102 content::RunMessageLoop(); | 107 content::RunMessageLoop(); |
| 103 } | 108 } |
| 104 | 109 |
| 105 const std::string& app_id() { | 110 const std::string& app_id() { |
| 106 return app_id_; | 111 return app_id_; |
| 107 } | 112 } |
| 108 | 113 |
| 109 private: | 114 private: |
| 110 bool done_; | 115 bool done_; |
| 116 bool waiting_; |
| 111 std::string app_id_; | 117 std::string app_id_; |
| 112 }; | 118 }; |
| 113 | 119 |
| 114 // Class to instantiate on the stack that is meant to be used with | 120 // Class to instantiate on the stack that is meant to be used with |
| 115 // StubNotificationUIManager::SetNotificationAddedCallback. Mind that Run() | 121 // StubNotificationUIManager::SetNotificationAddedCallback. Mind that Run() |
| 116 // might be invoked prior to WaitUntilSatisfied() being called. | 122 // might be invoked prior to WaitUntilSatisfied() being called. |
| 117 class NotificationAddedCallback { | 123 class NotificationAddedCallback { |
| 118 public: | 124 public: |
| 119 NotificationAddedCallback() : done_(false), waiting_(false) {} | 125 NotificationAddedCallback() : done_(false), waiting_(false) {} |
| 120 | 126 |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 | 457 |
| 452 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | 458 ASSERT_TRUE(RunScript("isControlled()", &script_result)); |
| 453 ASSERT_EQ("false - is not controlled", script_result); | 459 ASSERT_EQ("false - is not controlled", script_result); |
| 454 | 460 |
| 455 LoadTestPage(); // Reload to become controlled. | 461 LoadTestPage(); // Reload to become controlled. |
| 456 | 462 |
| 457 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | 463 ASSERT_TRUE(RunScript("isControlled()", &script_result)); |
| 458 ASSERT_EQ("true - is controlled", script_result); | 464 ASSERT_EQ("true - is controlled", script_result); |
| 459 | 465 |
| 460 GCMClient::IncomingMessage message; | 466 GCMClient::IncomingMessage message; |
| 467 message.sender_id = "1234567890"; |
| 461 message.data["data"] = "testdata"; | 468 message.data["data"] = "testdata"; |
| 462 push_service()->OnMessage(app_id.app_id_guid(), message); | 469 push_service()->OnMessage(app_id.app_id_guid(), message); |
| 463 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); | 470 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); |
| 464 EXPECT_EQ("testdata", script_result); | 471 EXPECT_EQ("testdata", script_result); |
| 465 } | 472 } |
| 466 | 473 |
| 467 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoServiceWorker) { | 474 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoServiceWorker) { |
| 468 if (!IsPushSupported()) | 475 if (!IsPushSupported()) |
| 469 return; | 476 return; |
| 470 | 477 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 488 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result)); | 495 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result)); |
| 489 ASSERT_EQ("service worker unregistration status: true", script_result); | 496 ASSERT_EQ("service worker unregistration status: true", script_result); |
| 490 | 497 |
| 491 // When the push service will receive it next message, given that there is no | 498 // When the push service will receive it next message, given that there is no |
| 492 // SW available, it should unregister |app_id|. | 499 // SW available, it should unregister |app_id|. |
| 493 UnregistrationCallback callback; | 500 UnregistrationCallback callback; |
| 494 gcm_service()->SetUnregisterCallback(base::Bind(&UnregistrationCallback::Run, | 501 gcm_service()->SetUnregisterCallback(base::Bind(&UnregistrationCallback::Run, |
| 495 base::Unretained(&callback))); | 502 base::Unretained(&callback))); |
| 496 | 503 |
| 497 GCMClient::IncomingMessage message; | 504 GCMClient::IncomingMessage message; |
| 505 message.sender_id = "1234567890"; |
| 498 message.data["data"] = "testdata"; | 506 message.data["data"] = "testdata"; |
| 499 push_service()->OnMessage(app_id.app_id_guid(), message); | 507 push_service()->OnMessage(app_id.app_id_guid(), message); |
| 500 | 508 |
| 501 callback.WaitUntilSatisfied(); | |
| 502 EXPECT_EQ(app_id.app_id_guid(), callback.app_id()); | |
| 503 | |
| 504 // No push data should have been received. | |
| 505 ASSERT_TRUE(RunScript("resultQueue.popImmediately()", &script_result)); | |
| 506 EXPECT_EQ("null", script_result); | |
| 507 } | |
| 508 | |
| 509 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoPermission) { | |
| 510 if (!IsPushSupported()) | |
| 511 return; | |
| 512 | |
| 513 std::string script_result; | |
| 514 | |
| 515 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
| 516 | |
| 517 PushMessagingApplicationId app_id = GetServiceWorkerAppId(0LL); | |
| 518 EXPECT_EQ(app_id.app_id_guid(), gcm_service()->last_registered_app_id()); | |
| 519 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); | |
| 520 | |
| 521 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | |
| 522 ASSERT_EQ("false - is not controlled", script_result); | |
| 523 | |
| 524 LoadTestPage(); // Reload to become controlled. | |
| 525 | |
| 526 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | |
| 527 ASSERT_EQ("true - is controlled", script_result); | |
| 528 | |
| 529 // Revoke Push permission. | |
| 530 browser()->profile()->GetHostContentSettingsMap()-> | |
| 531 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); | |
| 532 | |
| 533 // When the push service will receive its next message, given that there is no | |
| 534 // SW available, it should unregister |app_id|. | |
| 535 UnregistrationCallback callback; | |
| 536 gcm_service()->SetUnregisterCallback(base::Bind(&UnregistrationCallback::Run, | |
| 537 base::Unretained(&callback))); | |
| 538 | |
| 539 GCMClient::IncomingMessage message; | |
| 540 message.data["data"] = "testdata"; | |
| 541 push_service()->OnMessage(app_id.app_id_guid(), message); | |
| 542 | |
| 543 callback.WaitUntilSatisfied(); | 509 callback.WaitUntilSatisfied(); |
| 544 EXPECT_EQ(app_id.app_id_guid(), callback.app_id()); | 510 EXPECT_EQ(app_id.app_id_guid(), callback.app_id()); |
| 545 | 511 |
| 546 // No push data should have been received. | 512 // No push data should have been received. |
| 547 ASSERT_TRUE(RunScript("resultQueue.popImmediately()", &script_result)); | 513 ASSERT_TRUE(RunScript("resultQueue.popImmediately()", &script_result)); |
| 548 EXPECT_EQ("null", script_result); | 514 EXPECT_EQ("null", script_result); |
| 549 } | 515 } |
| 550 | 516 |
| 551 #if defined(ENABLE_NOTIFICATIONS) | 517 #if defined(ENABLE_NOTIFICATIONS) |
| 552 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | 518 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 574 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); | 540 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 575 | 541 |
| 576 // We'll need to specify the web_contents in which to eval script, since we're | 542 // We'll need to specify the web_contents in which to eval script, since we're |
| 577 // going to run script in a background tab. | 543 // going to run script in a background tab. |
| 578 content::WebContents* web_contents = | 544 content::WebContents* web_contents = |
| 579 browser()->tab_strip_model()->GetActiveWebContents(); | 545 browser()->tab_strip_model()->GetActiveWebContents(); |
| 580 | 546 |
| 581 // If the site is visible in an active tab, we should not force a notification | 547 // If the site is visible in an active tab, we should not force a notification |
| 582 // to be shown. Try it twice, since we allow one mistake per 10 push events. | 548 // to be shown. Try it twice, since we allow one mistake per 10 push events. |
| 583 GCMClient::IncomingMessage message; | 549 GCMClient::IncomingMessage message; |
| 550 message.sender_id = "1234567890"; |
| 584 for (int n = 0; n < 2; n++) { | 551 for (int n = 0; n < 2; n++) { |
| 585 message.data["data"] = "testdata"; | 552 message.data["data"] = "testdata"; |
| 586 push_service()->OnMessage(app_id.app_id_guid(), message); | 553 push_service()->OnMessage(app_id.app_id_guid(), message); |
| 587 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); | 554 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); |
| 588 EXPECT_EQ("testdata", script_result); | 555 EXPECT_EQ("testdata", script_result); |
| 589 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); | 556 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 590 } | 557 } |
| 591 | 558 |
| 592 // Open a blank foreground tab so site is no longer visible. | 559 // Open a blank foreground tab so site is no longer visible. |
| 593 ui_test_utils::NavigateToURLWithDisposition( | 560 ui_test_utils::NavigateToURLWithDisposition( |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 664 LoadTestPage(); // Reload to become controlled. | 631 LoadTestPage(); // Reload to become controlled. |
| 665 | 632 |
| 666 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | 633 ASSERT_TRUE(RunScript("isControlled()", &script_result)); |
| 667 ASSERT_EQ("true - is controlled", script_result); | 634 ASSERT_EQ("true - is controlled", script_result); |
| 668 | 635 |
| 669 NotificationAddedCallback callback; | 636 NotificationAddedCallback callback; |
| 670 notification_manager()->SetNotificationAddedCallback( | 637 notification_manager()->SetNotificationAddedCallback( |
| 671 base::Bind(&NotificationAddedCallback::Run, base::Unretained(&callback))); | 638 base::Bind(&NotificationAddedCallback::Run, base::Unretained(&callback))); |
| 672 | 639 |
| 673 GCMClient::IncomingMessage message; | 640 GCMClient::IncomingMessage message; |
| 641 message.sender_id = "1234567890"; |
| 674 message.data["data"] = "shownotification-without-waituntil"; | 642 message.data["data"] = "shownotification-without-waituntil"; |
| 675 push_service()->OnMessage(app_id.app_id_guid(), message); | 643 push_service()->OnMessage(app_id.app_id_guid(), message); |
| 676 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | 644 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 677 EXPECT_EQ("immediate:shownotification-without-waituntil", script_result); | 645 EXPECT_EQ("immediate:shownotification-without-waituntil", script_result); |
| 678 | 646 |
| 679 callback.WaitUntilSatisfied(); | 647 callback.WaitUntilSatisfied(); |
| 680 | 648 |
| 681 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); | 649 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); |
| 682 EXPECT_EQ(base::ASCIIToUTF16("push_test_tag"), | 650 EXPECT_EQ(base::ASCIIToUTF16("push_test_tag"), |
| 683 notification_manager()->GetNotificationAt(0).replace_id()); | 651 notification_manager()->GetNotificationAt(0).replace_id()); |
| (...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1034 // left without permission to use Push. | 1002 // left without permission to use Push. |
| 1035 | 1003 |
| 1036 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | 1004 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); |
| 1037 EXPECT_EQ("permission status - granted", script_result); | 1005 EXPECT_EQ("permission status - granted", script_result); |
| 1038 | 1006 |
| 1039 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | 1007 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); |
| 1040 EXPECT_EQ("true - registered", script_result); | 1008 EXPECT_EQ("true - registered", script_result); |
| 1041 } | 1009 } |
| 1042 | 1010 |
| 1043 } // namespace gcm | 1011 } // namespace gcm |
| OLD | NEW |