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 "base/test/histogram_tester.h" |
19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
20 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
21 #include "chrome/browser/browsing_data/browsing_data_helper.h" | 21 #include "chrome/browser/browsing_data/browsing_data_helper.h" |
22 #include "chrome/browser/browsing_data/browsing_data_remover.h" | 22 #include "chrome/browser/browsing_data/browsing_data_remover.h" |
23 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" | 23 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" |
24 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h" | 24 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h" |
25 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 25 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
26 #include "chrome/browser/engagement/site_engagement_score.h" | |
26 #include "chrome/browser/engagement/site_engagement_service.h" | 27 #include "chrome/browser/engagement/site_engagement_service.h" |
27 #include "chrome/browser/gcm/fake_gcm_profile_service.h" | 28 #include "chrome/browser/gcm/fake_gcm_profile_service.h" |
28 #include "chrome/browser/gcm/gcm_profile_service_factory.h" | 29 #include "chrome/browser/gcm/gcm_profile_service_factory.h" |
29 #include "chrome/browser/lifetime/keep_alive_registry.h" | 30 #include "chrome/browser/lifetime/keep_alive_registry.h" |
30 #include "chrome/browser/lifetime/keep_alive_types.h" | 31 #include "chrome/browser/lifetime/keep_alive_types.h" |
31 #include "chrome/browser/notifications/message_center_display_service.h" | 32 #include "chrome/browser/notifications/message_center_display_service.h" |
32 #include "chrome/browser/notifications/notification_test_util.h" | 33 #include "chrome/browser/notifications/notification_test_util.h" |
33 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 34 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
34 #include "chrome/browser/permissions/permission_request_manager.h" | 35 #include "chrome/browser/permissions/permission_request_manager.h" |
35 #include "chrome/browser/profiles/profile.h" | 36 #include "chrome/browser/profiles/profile.h" |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 | 118 |
118 // InProcessBrowserTest: | 119 // InProcessBrowserTest: |
119 void SetUp() override { | 120 void SetUp() override { |
120 https_server_.reset( | 121 https_server_.reset( |
121 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS)); | 122 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS)); |
122 https_server_->ServeFilesFromSourceDirectory("chrome/test/data"); | 123 https_server_->ServeFilesFromSourceDirectory("chrome/test/data"); |
123 ASSERT_TRUE(https_server_->Start()); | 124 ASSERT_TRUE(https_server_->Start()); |
124 | 125 |
125 notification_manager_.reset(new StubNotificationUIManager); | 126 notification_manager_.reset(new StubNotificationUIManager); |
126 | 127 |
128 SiteEngagementScore::SetParamValuesForTesting(); | |
127 InProcessBrowserTest::SetUp(); | 129 InProcessBrowserTest::SetUp(); |
128 } | 130 } |
129 void SetUpCommandLine(base::CommandLine* command_line) override { | 131 void SetUpCommandLine(base::CommandLine* command_line) override { |
130 // Enable experimental features for subscription restrictions. | 132 // Enable experimental features for subscription restrictions. |
131 command_line->AppendSwitch( | 133 command_line->AppendSwitch( |
132 switches::kEnableExperimentalWebPlatformFeatures); | 134 switches::kEnableExperimentalWebPlatformFeatures); |
133 InProcessBrowserTest::SetUpCommandLine(command_line); | 135 InProcessBrowserTest::SetUpCommandLine(command_line); |
134 } | 136 } |
135 | 137 |
136 // InProcessBrowserTest: | 138 // InProcessBrowserTest: |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 StubNotificationUIManager* notification_manager() const { | 267 StubNotificationUIManager* notification_manager() const { |
266 return notification_manager_.get(); | 268 return notification_manager_.get(); |
267 } | 269 } |
268 | 270 |
269 PlatformNotificationServiceImpl* notification_service() const { | 271 PlatformNotificationServiceImpl* notification_service() const { |
270 return PlatformNotificationServiceImpl::GetInstance(); | 272 return PlatformNotificationServiceImpl::GetInstance(); |
271 } | 273 } |
272 | 274 |
273 PushMessagingServiceImpl* push_service() const { return push_service_; } | 275 PushMessagingServiceImpl* push_service() const { return push_service_; } |
274 | 276 |
275 void SetSiteEngagementScore(const GURL& url, double score) { | 277 void SetSiteEngagementScore(const GURL& url, |
278 double score, | |
279 double expected_score) { | |
280 // There will be a bonus of 5.0 points for having notification permission | |
281 // granted, so we assert that the final score is as expected. | |
276 SiteEngagementService* service = | 282 SiteEngagementService* service = |
277 SiteEngagementService::Get(GetBrowser()->profile()); | 283 SiteEngagementService::Get(GetBrowser()->profile()); |
278 service->ResetScoreForURL(url, score); | 284 service->ResetScoreForURL(url, score); |
benwells
2017/03/08 04:19:33
This name is a bit confusing - should we make it R
dominickn
2017/03/08 04:44:52
Filed crbug.com/699381
| |
285 EXPECT_EQ(expected_score, service->GetScore(url)); | |
279 } | 286 } |
280 | 287 |
281 protected: | 288 protected: |
282 virtual std::string GetTestURL() { return "/push_messaging/test.html"; } | 289 virtual std::string GetTestURL() { return "/push_messaging/test.html"; } |
283 | 290 |
284 virtual std::string GetNoManifestTestURL() { | 291 virtual std::string GetNoManifestTestURL() { |
285 return "/push_messaging/test_no_manifest.html"; | 292 return "/push_messaging/test_no_manifest.html"; |
286 } | 293 } |
287 | 294 |
288 virtual Browser* GetBrowser() const { return browser(); } | 295 virtual Browser* GetBrowser() const { return browser(); } |
(...skipping 944 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1233 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); | 1240 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); |
1234 | 1241 |
1235 // We'll need to specify the web_contents in which to eval script, since we're | 1242 // We'll need to specify the web_contents in which to eval script, since we're |
1236 // going to run script in a background tab. | 1243 // going to run script in a background tab. |
1237 content::WebContents* web_contents = | 1244 content::WebContents* web_contents = |
1238 GetBrowser()->tab_strip_model()->GetActiveWebContents(); | 1245 GetBrowser()->tab_strip_model()->GetActiveWebContents(); |
1239 | 1246 |
1240 // Set the site engagement score for the site. Setting it to 10 means it | 1247 // Set the site engagement score for the site. Setting it to 10 means it |
1241 // should have a budget of 4, enough for two non-shown notification, which | 1248 // should have a budget of 4, enough for two non-shown notification, which |
1242 // cost 2 each. | 1249 // cost 2 each. |
1243 SetSiteEngagementScore(web_contents->GetURL(), 10.0); | 1250 SetSiteEngagementScore(web_contents->GetURL(), 5.0, 10.0); |
1244 | 1251 |
1245 // If the site is visible in an active tab, we should not force a notification | 1252 // If the site is visible in an active tab, we should not force a notification |
1246 // to be shown. Try it twice, since we allow one mistake per 10 push events. | 1253 // to be shown. Try it twice, since we allow one mistake per 10 push events. |
1247 gcm::IncomingMessage message; | 1254 gcm::IncomingMessage message; |
1248 message.sender_id = GetTestApplicationServerKey(); | 1255 message.sender_id = GetTestApplicationServerKey(); |
1249 message.decrypted = true; | 1256 message.decrypted = true; |
1250 for (int n = 0; n < 2; n++) { | 1257 for (int n = 0; n < 2; n++) { |
1251 message.raw_data = "testdata"; | 1258 message.raw_data = "testdata"; |
1252 SendMessageAndWaitUntilHandled(app_identifier, message); | 1259 SendMessageAndWaitUntilHandled(app_identifier, message); |
1253 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); | 1260 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1335 ASSERT_EQ("true - is controlled", script_result); | 1342 ASSERT_EQ("true - is controlled", script_result); |
1336 | 1343 |
1337 notification_manager()->CancelAll(); | 1344 notification_manager()->CancelAll(); |
1338 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); | 1345 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); |
1339 | 1346 |
1340 // We'll need to specify the web_contents in which to eval script, since we're | 1347 // We'll need to specify the web_contents in which to eval script, since we're |
1341 // going to run script in a background tab. | 1348 // going to run script in a background tab. |
1342 content::WebContents* web_contents = | 1349 content::WebContents* web_contents = |
1343 GetBrowser()->tab_strip_model()->GetActiveWebContents(); | 1350 GetBrowser()->tab_strip_model()->GetActiveWebContents(); |
1344 | 1351 |
1352 SetSiteEngagementScore(web_contents->GetURL(), 0.0, 5.0); | |
1353 | |
1345 ui_test_utils::NavigateToURLWithDisposition( | 1354 ui_test_utils::NavigateToURLWithDisposition( |
1346 GetBrowser(), GURL("about:blank"), | 1355 GetBrowser(), GURL("about:blank"), |
1347 WindowOpenDisposition::NEW_FOREGROUND_TAB, | 1356 WindowOpenDisposition::NEW_FOREGROUND_TAB, |
1348 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | 1357 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); |
1349 | 1358 |
1350 SetSiteEngagementScore(web_contents->GetURL(), 0.0); | 1359 // Send a missed notification to use up the budget. |
1351 | |
1352 // If the Service Worker push event handler does not show a notification, we | |
1353 // should show a forced one providing there is no foreground tab and the | |
1354 // origin ran out of budget. | |
1355 gcm::IncomingMessage message; | 1360 gcm::IncomingMessage message; |
1356 message.sender_id = GetTestApplicationServerKey(); | 1361 message.sender_id = GetTestApplicationServerKey(); |
1357 message.raw_data = "testdata"; | 1362 message.raw_data = "testdata"; |
1358 message.decrypted = true; | 1363 message.decrypted = true; |
1359 | 1364 |
1360 SendMessageAndWaitUntilHandled(app_identifier, message); | 1365 SendMessageAndWaitUntilHandled(app_identifier, message); |
1361 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | 1366 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
1362 EXPECT_EQ("testdata", script_result); | 1367 EXPECT_EQ("testdata", script_result); |
1368 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); | |
1369 | |
1370 // If the Service Worker push event handler does not show a notification, we | |
1371 // should show a forced one providing there is no foreground tab and the | |
1372 // origin ran out of budget. | |
1373 SendMessageAndWaitUntilHandled(app_identifier, message); | |
1374 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | |
1375 EXPECT_EQ("testdata", script_result); | |
1363 | 1376 |
1364 // Because the --allow-silent-push command line flag has not been passed, | 1377 // Because the --allow-silent-push command line flag has not been passed, |
1365 // this should have shown a default notification. | 1378 // this should have shown a default notification. |
1366 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); | 1379 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); |
1367 { | 1380 { |
1368 const Notification& forced_notification = | 1381 const Notification& forced_notification = |
1369 notification_manager()->GetNotificationAt(0); | 1382 notification_manager()->GetNotificationAt(0); |
1370 | 1383 |
1371 EXPECT_EQ(kPushMessagingForcedNotificationTag, forced_notification.tag()); | 1384 EXPECT_EQ(kPushMessagingForcedNotificationTag, forced_notification.tag()); |
1372 EXPECT_TRUE(forced_notification.silent()); | 1385 EXPECT_TRUE(forced_notification.silent()); |
(...skipping 859 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2232 push_service()->SetUnsubscribeCallbackForTesting(run_loop.QuitClosure()); | 2245 push_service()->SetUnsubscribeCallbackForTesting(run_loop.QuitClosure()); |
2233 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); | 2246 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); |
2234 EXPECT_EQ("unsubscribe result: true", script_result); | 2247 EXPECT_EQ("unsubscribe result: true", script_result); |
2235 // Background mode is only guaranteed to have updated once the unsubscribe | 2248 // Background mode is only guaranteed to have updated once the unsubscribe |
2236 // callback for testing has been run (PushSubscription.unsubscribe() usually | 2249 // callback for testing has been run (PushSubscription.unsubscribe() usually |
2237 // resolves before that, in order to avoid blocking on network retries etc). | 2250 // resolves before that, in order to avoid blocking on network retries etc). |
2238 run_loop.Run(); | 2251 run_loop.Run(); |
2239 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); | 2252 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); |
2240 } | 2253 } |
2241 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) | 2254 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) |
OLD | NEW |