| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/barrier_closure.h" | 11 #include "base/barrier_closure.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/test/histogram_tester.h" |
| 18 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 19 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/browsing_data/browsing_data_helper.h" | 21 #include "chrome/browser/browsing_data/browsing_data_helper.h" |
| 21 #include "chrome/browser/browsing_data/browsing_data_remover.h" | 22 #include "chrome/browser/browsing_data/browsing_data_remover.h" |
| 22 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" | 23 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" |
| 23 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h" | 24 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h" |
| 24 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 25 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 25 #include "chrome/browser/engagement/site_engagement_service.h" | 26 #include "chrome/browser/engagement/site_engagement_service.h" |
| 26 #include "chrome/browser/lifetime/keep_alive_registry.h" | 27 #include "chrome/browser/lifetime/keep_alive_registry.h" |
| 27 #include "chrome/browser/lifetime/keep_alive_types.h" | 28 #include "chrome/browser/lifetime/keep_alive_types.h" |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 SiteEngagementService* service = | 264 SiteEngagementService* service = |
| 264 SiteEngagementService::Get(GetBrowser()->profile()); | 265 SiteEngagementService::Get(GetBrowser()->profile()); |
| 265 service->ResetScoreForURL(url, score); | 266 service->ResetScoreForURL(url, score); |
| 266 } | 267 } |
| 267 | 268 |
| 268 protected: | 269 protected: |
| 269 virtual std::string GetTestURL() { return "/push_messaging/test.html"; } | 270 virtual std::string GetTestURL() { return "/push_messaging/test.html"; } |
| 270 | 271 |
| 271 virtual Browser* GetBrowser() const { return browser(); } | 272 virtual Browser* GetBrowser() const { return browser(); } |
| 272 | 273 |
| 274 base::HistogramTester* GetHistogramTester() { return &histogram_tester_; } |
| 275 |
| 273 private: | 276 private: |
| 274 std::unique_ptr<net::EmbeddedTestServer> https_server_; | 277 std::unique_ptr<net::EmbeddedTestServer> https_server_; |
| 275 gcm::FakeGCMProfileService* gcm_service_; | 278 gcm::FakeGCMProfileService* gcm_service_; |
| 276 PushMessagingServiceImpl* push_service_; | 279 PushMessagingServiceImpl* push_service_; |
| 280 base::HistogramTester histogram_tester_; |
| 277 | 281 |
| 278 #if defined(ENABLE_NOTIFICATIONS) | 282 #if defined(ENABLE_NOTIFICATIONS) |
| 279 std::unique_ptr<StubNotificationUIManager> notification_manager_; | 283 std::unique_ptr<StubNotificationUIManager> notification_manager_; |
| 280 std::unique_ptr<MessageCenterDisplayService> display_service_; | 284 std::unique_ptr<MessageCenterDisplayService> display_service_; |
| 281 #endif | 285 #endif |
| 282 | 286 |
| 283 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); | 287 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); |
| 284 }; | 288 }; |
| 285 | 289 |
| 286 class PushMessagingBrowserTestEmptySubscriptionOptions | 290 class PushMessagingBrowserTestEmptySubscriptionOptions |
| (...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 756 ASSERT_EQ("true - is controlled", script_result); | 760 ASSERT_EQ("true - is controlled", script_result); |
| 757 | 761 |
| 758 notification_manager()->CancelAll(); | 762 notification_manager()->CancelAll(); |
| 759 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); | 763 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 760 | 764 |
| 761 // We'll need to specify the web_contents in which to eval script, since we're | 765 // We'll need to specify the web_contents in which to eval script, since we're |
| 762 // going to run script in a background tab. | 766 // going to run script in a background tab. |
| 763 content::WebContents* web_contents = | 767 content::WebContents* web_contents = |
| 764 GetBrowser()->tab_strip_model()->GetActiveWebContents(); | 768 GetBrowser()->tab_strip_model()->GetActiveWebContents(); |
| 765 | 769 |
| 770 // Set the site engagement score for the site. Setting it to 4 means it should |
| 771 // have enough budget for two non-shown notification, which cost 2 each. |
| 772 SetSiteEngagementScore(web_contents->GetURL(), 4.0); |
| 773 |
| 766 // If the site is visible in an active tab, we should not force a notification | 774 // If the site is visible in an active tab, we should not force a notification |
| 767 // to be shown. Try it twice, since we allow one mistake per 10 push events. | 775 // to be shown. Try it twice, since we allow one mistake per 10 push events. |
| 768 gcm::IncomingMessage message; | 776 gcm::IncomingMessage message; |
| 769 message.sender_id = GetTestApplicationServerKey(); | 777 message.sender_id = GetTestApplicationServerKey(); |
| 770 message.decrypted = true; | 778 message.decrypted = true; |
| 771 for (int n = 0; n < 2; n++) { | 779 for (int n = 0; n < 2; n++) { |
| 772 message.raw_data = "testdata"; | 780 message.raw_data = "testdata"; |
| 773 SendMessageAndWaitUntilHandled(app_identifier, message); | 781 SendMessageAndWaitUntilHandled(app_identifier, message); |
| 774 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); | 782 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); |
| 775 EXPECT_EQ("testdata", script_result); | 783 EXPECT_EQ("testdata", script_result); |
| 776 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); | 784 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 777 } | 785 } |
| 778 | 786 |
| 779 // Open a blank foreground tab so site is no longer visible. | 787 // Open a blank foreground tab so site is no longer visible. |
| 780 ui_test_utils::NavigateToURLWithDisposition( | 788 ui_test_utils::NavigateToURLWithDisposition( |
| 781 GetBrowser(), GURL("about:blank"), NEW_FOREGROUND_TAB, | 789 GetBrowser(), GURL("about:blank"), NEW_FOREGROUND_TAB, |
| 782 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | 790 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); |
| 783 | 791 |
| 784 // Set the site engagement score for the site. Setting it to 2 means we should | |
| 785 // have enough budget for two non-shown notification. | |
| 786 SetSiteEngagementScore(web_contents->GetURL(), 2.0); | |
| 787 | |
| 788 // If the Service Worker push event handler shows a notification, we | 792 // If the Service Worker push event handler shows a notification, we |
| 789 // should not show a forced one. | 793 // should not show a forced one. |
| 790 message.raw_data = "shownotification"; | 794 message.raw_data = "shownotification"; |
| 791 SendMessageAndWaitUntilHandled(app_identifier, message); | 795 SendMessageAndWaitUntilHandled(app_identifier, message); |
| 792 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | 796 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 793 EXPECT_EQ("shownotification", script_result); | 797 EXPECT_EQ("shownotification", script_result); |
| 794 EXPECT_EQ(1u, notification_manager()->GetNotificationCount()); | 798 EXPECT_EQ(1u, notification_manager()->GetNotificationCount()); |
| 795 EXPECT_EQ("push_test_tag", | 799 EXPECT_EQ("push_test_tag", |
| 796 notification_manager()->GetNotificationAt(0).tag()); | 800 notification_manager()->GetNotificationAt(0).tag()); |
| 797 notification_manager()->CancelAll(); | 801 notification_manager()->CancelAll(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 830 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | 834 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 831 EXPECT_EQ("shownotification", script_result); | 835 EXPECT_EQ("shownotification", script_result); |
| 832 | 836 |
| 833 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); | 837 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); |
| 834 { | 838 { |
| 835 const Notification& first_notification = | 839 const Notification& first_notification = |
| 836 notification_manager()->GetNotificationAt(0); | 840 notification_manager()->GetNotificationAt(0); |
| 837 | 841 |
| 838 EXPECT_NE(kPushMessagingForcedNotificationTag, first_notification.tag()); | 842 EXPECT_NE(kPushMessagingForcedNotificationTag, first_notification.tag()); |
| 839 } | 843 } |
| 844 |
| 845 // Check that the UMA has been recorded correctly. |
| 846 // There should be a total of 7 budget samples, spread across 3 buckets. The |
| 847 // first four notifications (before any budget is consumed) have budget of 4, |
| 848 // which is the starting SES. The next one has 2 (one hidden notification) and |
| 849 // the final two have 0 (two hidden notifications. |
| 850 std::vector<base::Bucket> buckets = |
| 851 GetHistogramTester()->GetAllSamples("PushMessaging.BackgroundBudget"); |
| 852 ASSERT_EQ(3.0, buckets.size()); |
| 853 // First bucket is for 0 budget, which has 2 samples. |
| 854 EXPECT_EQ(0, buckets[0].min); |
| 855 EXPECT_EQ(2, buckets[0].count); |
| 856 // Second bucket is for 2 budget, which has 1 sample. |
| 857 EXPECT_EQ(2, buckets[1].min); |
| 858 EXPECT_EQ(1, buckets[1].count); |
| 859 // Final bucket is for 4 budget, which has 4 samples. |
| 860 EXPECT_EQ(4, buckets[2].min); |
| 861 EXPECT_EQ(4, buckets[2].count); |
| 862 |
| 863 std::vector<base::Bucket> no_budget_buckets = |
| 864 GetHistogramTester()->GetAllSamples("PushMessaging.SESForNoBudgetOrigin"); |
| 865 ASSERT_EQ(1.0, no_budget_buckets.size()); |
| 866 EXPECT_EQ(4, no_budget_buckets[0].min); |
| 867 EXPECT_EQ(2, no_budget_buckets[0].count); |
| 868 |
| 869 std::vector<base::Bucket> low_budget_buckets = |
| 870 GetHistogramTester()->GetAllSamples( |
| 871 "PushMessaging.SESForLowBudgetOrigin"); |
| 872 ASSERT_EQ(1.0, low_budget_buckets.size()); |
| 873 EXPECT_EQ(4, low_budget_buckets[0].min); |
| 874 EXPECT_EQ(1, low_budget_buckets[0].count); |
| 840 } | 875 } |
| 841 | 876 |
| 842 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | 877 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, |
| 843 PushEventAllowSilentPushCommandLineFlag) { | 878 PushEventAllowSilentPushCommandLineFlag) { |
| 844 std::string script_result; | 879 std::string script_result; |
| 845 | 880 |
| 846 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); | 881 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); |
| 847 | 882 |
| 848 PushMessagingAppIdentifier app_identifier = | 883 PushMessagingAppIdentifier app_identifier = |
| 849 GetAppIdentifierForServiceWorkerRegistration(0LL); | 884 GetAppIdentifierForServiceWorkerRegistration(0LL); |
| (...skipping 706 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1556 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); | 1591 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); |
| 1557 | 1592 |
| 1558 // After dropping the last subscription background mode is still inactive. | 1593 // After dropping the last subscription background mode is still inactive. |
| 1559 std::string script_result; | 1594 std::string script_result; |
| 1560 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS); | 1595 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS); |
| 1561 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); | 1596 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); |
| 1562 EXPECT_EQ("unsubscribe result: true", script_result); | 1597 EXPECT_EQ("unsubscribe result: true", script_result); |
| 1563 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); | 1598 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); |
| 1564 } | 1599 } |
| 1565 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) | 1600 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) |
| OLD | NEW |