| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/bind_helpers.h" | 5 #include "base/bind_helpers.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #include "chrome/browser/budget_service/budget_manager.h" | 9 #include "chrome/browser/budget_service/budget_manager.h" |
| 10 #include "chrome/browser/budget_service/budget_manager_factory.h" | 10 #include "chrome/browser/budget_service/budget_manager_factory.h" |
| 11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 12 #include "chrome/browser/engagement/site_engagement_score.h" |
| 11 #include "chrome/browser/engagement/site_engagement_service.h" | 13 #include "chrome/browser/engagement/site_engagement_service.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #include "chrome/test/base/in_process_browser_test.h" | 17 #include "chrome/test/base/in_process_browser_test.h" |
| 16 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
| 19 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 20 #include "components/content_settings/core/common/content_settings_types.h" |
| 17 #include "content/public/browser/browser_context.h" | 21 #include "content/public/browser/browser_context.h" |
| 18 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/common/content_switches.h" | 23 #include "content/public/common/content_switches.h" |
| 20 #include "content/public/test/browser_test_utils.h" | 24 #include "content/public/test/browser_test_utils.h" |
| 21 #include "net/test/embedded_test_server/embedded_test_server.h" | 25 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 22 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" | 26 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" |
| 23 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 24 #include "url/origin.h" | 28 #include "url/origin.h" |
| 25 | 29 |
| 26 namespace { | 30 namespace { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 37 https_server_.reset( | 41 https_server_.reset( |
| 38 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS)); | 42 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS)); |
| 39 https_server_->ServeFilesFromSourceDirectory("chrome/test/data"); | 43 https_server_->ServeFilesFromSourceDirectory("chrome/test/data"); |
| 40 ASSERT_TRUE(https_server_->Start()); | 44 ASSERT_TRUE(https_server_->Start()); |
| 41 | 45 |
| 42 InProcessBrowserTest::SetUp(); | 46 InProcessBrowserTest::SetUp(); |
| 43 } | 47 } |
| 44 | 48 |
| 45 // InProcessBrowserTest: | 49 // InProcessBrowserTest: |
| 46 void SetUpOnMainThread() override { | 50 void SetUpOnMainThread() override { |
| 51 SiteEngagementScore::SetParamValuesForTesting(); |
| 52 |
| 53 // Grant Notification permission for these tests. See the privacy |
| 54 // requirement for this outlined in https://crbug.com/710809. |
| 55 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 56 ->SetContentSettingDefaultScope(https_server_->base_url(), GURL(), |
| 57 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 58 std::string(), CONTENT_SETTING_ALLOW); |
| 59 |
| 47 LoadTestPage(); | 60 LoadTestPage(); |
| 48 InProcessBrowserTest::SetUpOnMainThread(); | 61 InProcessBrowserTest::SetUpOnMainThread(); |
| 49 budget_manager_ = BudgetManagerFactory::GetForProfile(browser()->profile()); | 62 budget_manager_ = BudgetManagerFactory::GetForProfile(browser()->profile()); |
| 50 } | 63 } |
| 51 | 64 |
| 52 // InProcessBrowserTest: | 65 // InProcessBrowserTest: |
| 53 void SetUpCommandLine(base::CommandLine* command_line) override { | 66 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 54 // TODO(harkness): Remove switch once Budget API ships. (crbug.com/617971) | 67 // TODO(harkness): Remove switch once Budget API ships. (crbug.com/617971) |
| 55 command_line->AppendSwitch( | 68 command_line->AppendSwitch( |
| 56 switches::kEnableExperimentalWebPlatformFeatures); | 69 switches::kEnableExperimentalWebPlatformFeatures); |
| 57 InProcessBrowserTest::SetUpCommandLine(command_line); | 70 InProcessBrowserTest::SetUpCommandLine(command_line); |
| 58 } | 71 } |
| 59 | 72 |
| 73 // Sets the absolute Site Engagement |score| for the testing origin, assuming |
| 74 // that notification permission has been granted. |
| 75 // The |score| must be higher than the bonus points awarded to an origin for |
| 76 // having the Notification permission granted. Should be wrapped in the |
| 77 // ASSERT_NO_FATAL_FAILURE macro because it contains an ASSERT_GE. |
| 60 void SetSiteEngagementScore(double score) { | 78 void SetSiteEngagementScore(double score) { |
| 61 SiteEngagementService* service = | 79 SiteEngagementService* service = |
| 62 SiteEngagementService::Get(browser()->profile()); | 80 SiteEngagementService::Get(browser()->profile()); |
| 63 service->ResetBaseScoreForURL(https_server_->GetURL(kTestURL), score); | 81 |
| 82 double notification_permission_bonus = |
| 83 SiteEngagementScore::GetNotificationPermissionPoints(); |
| 84 ASSERT_GE(score, notification_permission_bonus); |
| 85 |
| 86 service->ResetBaseScoreForURL(https_server_->GetURL(kTestURL), |
| 87 score - notification_permission_bonus); |
| 64 } | 88 } |
| 65 | 89 |
| 66 bool RunScript(const std::string& script, std::string* result) { | 90 bool RunScript(const std::string& script, std::string* result) { |
| 67 content::WebContents* web_contents = | 91 content::WebContents* web_contents = |
| 68 browser()->tab_strip_model()->GetActiveWebContents(); | 92 browser()->tab_strip_model()->GetActiveWebContents(); |
| 69 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(), | 93 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(), |
| 70 script, result); | 94 script, result); |
| 71 } | 95 } |
| 72 | 96 |
| 73 void LoadTestPage() { | 97 void LoadTestPage() { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 95 private: | 119 private: |
| 96 std::unique_ptr<net::EmbeddedTestServer> https_server_; | 120 std::unique_ptr<net::EmbeddedTestServer> https_server_; |
| 97 // Lifetime of the BudgetManager is tied to the profile of the test. | 121 // Lifetime of the BudgetManager is tied to the profile of the test. |
| 98 BudgetManager* budget_manager_ = nullptr; | 122 BudgetManager* budget_manager_ = nullptr; |
| 99 bool success_ = false; | 123 bool success_ = false; |
| 100 }; | 124 }; |
| 101 | 125 |
| 102 IN_PROC_BROWSER_TEST_F(BudgetManagerBrowserTest, BudgetInDocument) { | 126 IN_PROC_BROWSER_TEST_F(BudgetManagerBrowserTest, BudgetInDocument) { |
| 103 std::string script_result; | 127 std::string script_result; |
| 104 | 128 |
| 105 SetSiteEngagementScore(5); | 129 ASSERT_NO_FATAL_FAILURE(SetSiteEngagementScore(5)); |
| 106 | 130 |
| 107 // Site Engagement score of 5 gives a budget of 2. | 131 // Site Engagement score of 5 gives a budget of 2. |
| 108 ASSERT_TRUE(RunScript("documentGetBudget()", &script_result)); | 132 ASSERT_TRUE(RunScript("documentGetBudget()", &script_result)); |
| 109 EXPECT_EQ("ok - budget returned value of 2", script_result); | 133 EXPECT_EQ("ok - budget returned value of 2", script_result); |
| 110 | 134 |
| 111 ASSERT_TRUE(RunScript("documentReserveBudget()", &script_result)); | 135 ASSERT_TRUE(RunScript("documentReserveBudget()", &script_result)); |
| 112 EXPECT_EQ("ok - reserved budget", script_result); | 136 EXPECT_EQ("ok - reserved budget", script_result); |
| 113 | 137 |
| 114 // After reserving budget, the new budget should be at 0. | 138 // After reserving budget, the new budget should be at 0. |
| 115 ASSERT_TRUE(RunScript("documentGetBudget()", &script_result)); | 139 ASSERT_TRUE(RunScript("documentGetBudget()", &script_result)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 128 ASSERT_FALSE(success()); | 152 ASSERT_FALSE(success()); |
| 129 } | 153 } |
| 130 | 154 |
| 131 IN_PROC_BROWSER_TEST_F(BudgetManagerBrowserTest, BudgetInWorker) { | 155 IN_PROC_BROWSER_TEST_F(BudgetManagerBrowserTest, BudgetInWorker) { |
| 132 std::string script_result; | 156 std::string script_result; |
| 133 | 157 |
| 134 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | 158 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 135 ASSERT_EQ("ok - service worker registered", script_result); | 159 ASSERT_EQ("ok - service worker registered", script_result); |
| 136 | 160 |
| 137 LoadTestPage(); // Reload to become controlled. | 161 LoadTestPage(); // Reload to become controlled. |
| 138 SetSiteEngagementScore(12); | 162 ASSERT_NO_FATAL_FAILURE(SetSiteEngagementScore(12)); |
| 139 | 163 |
| 140 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | 164 ASSERT_TRUE(RunScript("isControlled()", &script_result)); |
| 141 ASSERT_EQ("true - is controlled", script_result); | 165 ASSERT_EQ("true - is controlled", script_result); |
| 142 | 166 |
| 143 // Site engagement score of 12 gives a budget of 5. | 167 // Site engagement score of 12 gives a budget of 5. |
| 144 ASSERT_TRUE(RunScript("workerGetBudget()", &script_result)); | 168 ASSERT_TRUE(RunScript("workerGetBudget()", &script_result)); |
| 145 EXPECT_EQ("ok - budget returned value of 5", script_result); | 169 EXPECT_EQ("ok - budget returned value of 5", script_result); |
| 146 | 170 |
| 147 // With a budget of 5, two reservations should succeed. | 171 // With a budget of 5, two reservations should succeed. |
| 148 ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result)); | 172 ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 165 | 189 |
| 166 ConsumeReservation(); | 190 ConsumeReservation(); |
| 167 ASSERT_TRUE(success()); | 191 ASSERT_TRUE(success()); |
| 168 | 192 |
| 169 // One more consume should fail, because all reservations are consumed. | 193 // One more consume should fail, because all reservations are consumed. |
| 170 ConsumeReservation(); | 194 ConsumeReservation(); |
| 171 ASSERT_FALSE(success()); | 195 ASSERT_FALSE(success()); |
| 172 } | 196 } |
| 173 | 197 |
| 174 } // namespace | 198 } // namespace |
| OLD | NEW |