Chromium Code Reviews| 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/notifications/message_center_display_service.h" | 27 #include "chrome/browser/notifications/message_center_display_service.h" |
| 27 #include "chrome/browser/notifications/notification_test_util.h" | 28 #include "chrome/browser/notifications/notification_test_util.h" |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 244 SiteEngagementService* service = | 245 SiteEngagementService* service = |
| 245 SiteEngagementService::Get(GetBrowser()->profile()); | 246 SiteEngagementService::Get(GetBrowser()->profile()); |
| 246 service->ResetScoreForURL(url, score); | 247 service->ResetScoreForURL(url, score); |
| 247 } | 248 } |
| 248 | 249 |
| 249 protected: | 250 protected: |
| 250 virtual std::string GetTestURL() { return "/push_messaging/test.html"; } | 251 virtual std::string GetTestURL() { return "/push_messaging/test.html"; } |
| 251 | 252 |
| 252 virtual Browser* GetBrowser() const { return browser(); } | 253 virtual Browser* GetBrowser() const { return browser(); } |
| 253 | 254 |
| 255 base::HistogramTester* GetHistogramTester() { return &histogram_tester_; } | |
| 256 | |
| 254 private: | 257 private: |
| 255 std::unique_ptr<net::EmbeddedTestServer> https_server_; | 258 std::unique_ptr<net::EmbeddedTestServer> https_server_; |
| 256 gcm::FakeGCMProfileService* gcm_service_; | 259 gcm::FakeGCMProfileService* gcm_service_; |
| 257 PushMessagingServiceImpl* push_service_; | 260 PushMessagingServiceImpl* push_service_; |
| 261 base::HistogramTester histogram_tester_; | |
| 258 | 262 |
| 259 #if defined(ENABLE_NOTIFICATIONS) | 263 #if defined(ENABLE_NOTIFICATIONS) |
| 260 std::unique_ptr<StubNotificationUIManager> notification_manager_; | 264 std::unique_ptr<StubNotificationUIManager> notification_manager_; |
| 261 std::unique_ptr<MessageCenterDisplayService> display_service_; | 265 std::unique_ptr<MessageCenterDisplayService> display_service_; |
| 262 #endif | 266 #endif |
| 263 | 267 |
| 264 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); | 268 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); |
| 265 }; | 269 }; |
| 266 | 270 |
| 267 class PushMessagingBrowserTestEmptySubscriptionOptions | 271 class PushMessagingBrowserTestEmptySubscriptionOptions |
| (...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 732 ASSERT_EQ("true - is controlled", script_result); | 736 ASSERT_EQ("true - is controlled", script_result); |
| 733 | 737 |
| 734 notification_manager()->CancelAll(); | 738 notification_manager()->CancelAll(); |
| 735 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); | 739 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 736 | 740 |
| 737 // We'll need to specify the web_contents in which to eval script, since we're | 741 // We'll need to specify the web_contents in which to eval script, since we're |
| 738 // going to run script in a background tab. | 742 // going to run script in a background tab. |
| 739 content::WebContents* web_contents = | 743 content::WebContents* web_contents = |
| 740 GetBrowser()->tab_strip_model()->GetActiveWebContents(); | 744 GetBrowser()->tab_strip_model()->GetActiveWebContents(); |
| 741 | 745 |
| 746 // Set the site engagement score for the site. Setting it to 4 means it should | |
| 747 // have enough budget for two non-shown notification, which cost 2 each. | |
| 748 SetSiteEngagementScore(web_contents->GetURL(), 4.0); | |
| 749 | |
| 742 // If the site is visible in an active tab, we should not force a notification | 750 // If the site is visible in an active tab, we should not force a notification |
| 743 // to be shown. Try it twice, since we allow one mistake per 10 push events. | 751 // to be shown. Try it twice, since we allow one mistake per 10 push events. |
| 744 gcm::IncomingMessage message; | 752 gcm::IncomingMessage message; |
| 745 message.sender_id = GetTestApplicationServerKey(); | 753 message.sender_id = GetTestApplicationServerKey(); |
| 746 message.decrypted = true; | 754 message.decrypted = true; |
| 747 for (int n = 0; n < 2; n++) { | 755 for (int n = 0; n < 2; n++) { |
| 748 message.raw_data = "testdata"; | 756 message.raw_data = "testdata"; |
| 749 SendMessageAndWaitUntilHandled(app_identifier, message); | 757 SendMessageAndWaitUntilHandled(app_identifier, message); |
| 750 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); | 758 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); |
| 751 EXPECT_EQ("testdata", script_result); | 759 EXPECT_EQ("testdata", script_result); |
| 752 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); | 760 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 753 } | 761 } |
| 754 | 762 |
| 755 // Open a blank foreground tab so site is no longer visible. | 763 // Open a blank foreground tab so site is no longer visible. |
| 756 ui_test_utils::NavigateToURLWithDisposition( | 764 ui_test_utils::NavigateToURLWithDisposition( |
| 757 GetBrowser(), GURL("about:blank"), NEW_FOREGROUND_TAB, | 765 GetBrowser(), GURL("about:blank"), NEW_FOREGROUND_TAB, |
| 758 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | 766 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); |
| 759 | 767 |
| 760 // Set the site engagement score for the site. Setting it to 2 means we should | |
| 761 // have enough budget for two non-shown notification. | |
| 762 SetSiteEngagementScore(web_contents->GetURL(), 2.0); | |
| 763 | |
| 764 // If the Service Worker push event handler shows a notification, we | 768 // If the Service Worker push event handler shows a notification, we |
| 765 // should not show a forced one. | 769 // should not show a forced one. |
| 766 message.raw_data = "shownotification"; | 770 message.raw_data = "shownotification"; |
| 767 SendMessageAndWaitUntilHandled(app_identifier, message); | 771 SendMessageAndWaitUntilHandled(app_identifier, message); |
| 768 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | 772 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 769 EXPECT_EQ("shownotification", script_result); | 773 EXPECT_EQ("shownotification", script_result); |
| 770 EXPECT_EQ(1u, notification_manager()->GetNotificationCount()); | 774 EXPECT_EQ(1u, notification_manager()->GetNotificationCount()); |
| 771 EXPECT_EQ("push_test_tag", | 775 EXPECT_EQ("push_test_tag", |
| 772 notification_manager()->GetNotificationAt(0).tag()); | 776 notification_manager()->GetNotificationAt(0).tag()); |
| 773 notification_manager()->CancelAll(); | 777 notification_manager()->CancelAll(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 806 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | 810 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 807 EXPECT_EQ("shownotification", script_result); | 811 EXPECT_EQ("shownotification", script_result); |
| 808 | 812 |
| 809 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); | 813 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); |
| 810 { | 814 { |
| 811 const Notification& first_notification = | 815 const Notification& first_notification = |
| 812 notification_manager()->GetNotificationAt(0); | 816 notification_manager()->GetNotificationAt(0); |
| 813 | 817 |
| 814 EXPECT_NE(kPushMessagingForcedNotificationTag, first_notification.tag()); | 818 EXPECT_NE(kPushMessagingForcedNotificationTag, first_notification.tag()); |
| 815 } | 819 } |
| 820 | |
| 821 // Check that the UMA has been recorded correctly. | |
| 822 // There should be a total of 7 budget samples, spread across 3 buckets. The | |
| 823 // first four notifications (before any budget is consumed) have budget of 4, | |
| 824 // which is the starting SES. The next one has 2 (one hidden notification) and | |
| 825 // the final two have 0 (two hidden notifications. | |
| 826 std::vector<base::Bucket> buckets = | |
| 827 GetHistogramTester()->GetAllSamples("PushMessaging.BackgroundBudget"); | |
| 828 ASSERT_EQ(buckets.size(), 3.0); | |
| 829 // First bucket is for 0 budget, which has 2 samples. | |
| 830 EXPECT_EQ(buckets[0].min, 0); | |
|
Peter Beverloo
2016/05/17 11:04:34
nit: EXPECT_EQ(expected, actual)
Elsewhere and wi
harkness
2016/05/17 13:08:16
Done.
| |
| 831 EXPECT_EQ(buckets[0].count, 2); | |
| 832 // Second bucket is for 2 budget, which has 1 sample. | |
| 833 EXPECT_EQ(buckets[1].min, 2); | |
| 834 EXPECT_EQ(buckets[1].count, 1); | |
| 835 // Final bucket is for 4 budget, which has 4 samples. | |
| 836 EXPECT_EQ(buckets[2].min, 4); | |
| 837 EXPECT_EQ(buckets[2].count, 4); | |
| 838 | |
| 839 std::vector<base::Bucket> sesForNoBudgetBuckets = | |
|
Peter Beverloo
2016/05/17 11:04:34
nit: Chromium style guide wants us to name things
harkness
2016/05/17 13:08:16
Somewhat shortened, but I still kept some context.
| |
| 840 GetHistogramTester()->GetAllSamples("PushMessaging.SESForNoBudgetOrigin"); | |
| 841 EXPECT_EQ(sesForNoBudgetBuckets.size(), 1.0); | |
| 842 EXPECT_EQ(sesForNoBudgetBuckets[0].min, 4); | |
| 843 EXPECT_EQ(sesForNoBudgetBuckets[0].count, 2); | |
| 844 | |
| 845 std::vector<base::Bucket> sesForLowBudgetBuckets = | |
| 846 GetHistogramTester()->GetAllSamples( | |
| 847 "PushMessaging.SESForLowBudgetOrigin"); | |
| 848 EXPECT_EQ(sesForLowBudgetBuckets.size(), 1.0); | |
| 849 EXPECT_EQ(sesForLowBudgetBuckets[0].min, 4); | |
| 850 EXPECT_EQ(sesForLowBudgetBuckets[0].count, 1); | |
| 816 } | 851 } |
| 817 | 852 |
| 818 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | 853 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, |
| 819 PushEventEnforcesUserVisibleNotificationAfterQueue) { | 854 PushEventEnforcesUserVisibleNotificationAfterQueue) { |
| 820 std::string script_result; | 855 std::string script_result; |
| 821 | 856 |
| 822 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); | 857 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); |
| 823 | 858 |
| 824 PushMessagingAppIdentifier app_identifier = | 859 PushMessagingAppIdentifier app_identifier = |
| 825 GetAppIdentifierForServiceWorkerRegistration(0LL); | 860 GetAppIdentifierForServiceWorkerRegistration(0LL); |
| (...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1458 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); | 1493 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); |
| 1459 | 1494 |
| 1460 // After dropping the last subscription background mode is still inactive. | 1495 // After dropping the last subscription background mode is still inactive. |
| 1461 std::string script_result; | 1496 std::string script_result; |
| 1462 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS); | 1497 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS); |
| 1463 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); | 1498 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); |
| 1464 EXPECT_EQ("unsubscribe result: true", script_result); | 1499 EXPECT_EQ("unsubscribe result: true", script_result); |
| 1465 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); | 1500 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); |
| 1466 } | 1501 } |
| 1467 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) | 1502 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) |
| OLD | NEW |