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/engagement/site_engagement_service.h" |
11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
14 #include "chrome/test/base/in_process_browser_test.h" | 15 #include "chrome/test/base/in_process_browser_test.h" |
15 #include "chrome/test/base/ui_test_utils.h" | 16 #include "chrome/test/base/ui_test_utils.h" |
16 #include "content/public/browser/browser_context.h" | 17 #include "content/public/browser/browser_context.h" |
17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
18 #include "content/public/common/content_switches.h" | 19 #include "content/public/common/content_switches.h" |
19 #include "content/public/test/browser_test_utils.h" | 20 #include "content/public/test/browser_test_utils.h" |
20 #include "net/test/embedded_test_server/embedded_test_server.h" | 21 #include "net/test/embedded_test_server/embedded_test_server.h" |
21 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" | 22 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" |
| 23 #include "url/gurl.h" |
22 #include "url/origin.h" | 24 #include "url/origin.h" |
23 | 25 |
24 namespace { | 26 namespace { |
25 | 27 |
26 const char kTestURL[] = "/budget_service/test.html"; | 28 const char kTestURL[] = "/budget_service/test.html"; |
27 | 29 |
28 class BudgetManagerBrowserTest : public InProcessBrowserTest { | 30 class BudgetManagerBrowserTest : public InProcessBrowserTest { |
29 public: | 31 public: |
30 BudgetManagerBrowserTest() = default; | 32 BudgetManagerBrowserTest() = default; |
31 ~BudgetManagerBrowserTest() override = default; | 33 ~BudgetManagerBrowserTest() override = default; |
(...skipping 16 matching lines...) Expand all Loading... |
48 } | 50 } |
49 | 51 |
50 // InProcessBrowserTest: | 52 // InProcessBrowserTest: |
51 void SetUpCommandLine(base::CommandLine* command_line) override { | 53 void SetUpCommandLine(base::CommandLine* command_line) override { |
52 // TODO(harkness): Remove switch once Budget API ships. (crbug.com/617971) | 54 // TODO(harkness): Remove switch once Budget API ships. (crbug.com/617971) |
53 command_line->AppendSwitch( | 55 command_line->AppendSwitch( |
54 switches::kEnableExperimentalWebPlatformFeatures); | 56 switches::kEnableExperimentalWebPlatformFeatures); |
55 InProcessBrowserTest::SetUpCommandLine(command_line); | 57 InProcessBrowserTest::SetUpCommandLine(command_line); |
56 } | 58 } |
57 | 59 |
| 60 void SetSiteEngagementScore(double score) { |
| 61 SiteEngagementService* service = |
| 62 SiteEngagementService::Get(browser()->profile()); |
| 63 service->ResetScoreForURL(https_server_->GetURL(kTestURL), score); |
| 64 } |
| 65 |
58 bool RunScript(const std::string& script, std::string* result) { | 66 bool RunScript(const std::string& script, std::string* result) { |
59 content::WebContents* web_contents = | 67 content::WebContents* web_contents = |
60 browser()->tab_strip_model()->GetActiveWebContents(); | 68 browser()->tab_strip_model()->GetActiveWebContents(); |
61 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(), | 69 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(), |
62 script, result); | 70 script, result); |
63 } | 71 } |
64 | 72 |
65 void LoadTestPage() { | 73 void LoadTestPage() { |
66 ui_test_utils::NavigateToURL(browser(), https_server_->GetURL(kTestURL)); | 74 ui_test_utils::NavigateToURL(browser(), https_server_->GetURL(kTestURL)); |
67 } | 75 } |
(...skipping 19 matching lines...) Expand all Loading... |
87 private: | 95 private: |
88 std::unique_ptr<net::EmbeddedTestServer> https_server_; | 96 std::unique_ptr<net::EmbeddedTestServer> https_server_; |
89 // Lifetime of the BudgetManager is tied to the profile of the test. | 97 // Lifetime of the BudgetManager is tied to the profile of the test. |
90 BudgetManager* budget_manager_ = nullptr; | 98 BudgetManager* budget_manager_ = nullptr; |
91 bool success_ = false; | 99 bool success_ = false; |
92 }; | 100 }; |
93 | 101 |
94 IN_PROC_BROWSER_TEST_F(BudgetManagerBrowserTest, BudgetInDocument) { | 102 IN_PROC_BROWSER_TEST_F(BudgetManagerBrowserTest, BudgetInDocument) { |
95 std::string script_result; | 103 std::string script_result; |
96 | 104 |
97 // The page will have been loaded once, which gives a budget of 3. | 105 SetSiteEngagementScore(5); |
| 106 |
| 107 // Site Engagement score of 5 gives a budget of 2. |
98 ASSERT_TRUE(RunScript("documentGetBudget()", &script_result)); | 108 ASSERT_TRUE(RunScript("documentGetBudget()", &script_result)); |
99 ASSERT_EQ("ok - budget returned value of 3", script_result); | 109 EXPECT_EQ("ok - budget returned value of 2", script_result); |
100 | 110 |
101 ASSERT_TRUE(RunScript("documentReserveBudget()", &script_result)); | 111 ASSERT_TRUE(RunScript("documentReserveBudget()", &script_result)); |
102 ASSERT_EQ("ok - reserved budget", script_result); | 112 EXPECT_EQ("ok - reserved budget", script_result); |
103 | 113 |
104 // After reserving budget, the new budget should be at 1. | 114 // After reserving budget, the new budget should be at 0. |
105 ASSERT_TRUE(RunScript("documentGetBudget()", &script_result)); | 115 ASSERT_TRUE(RunScript("documentGetBudget()", &script_result)); |
106 ASSERT_EQ("ok - budget returned value of 1", script_result); | 116 EXPECT_EQ("ok - budget returned value of 0", script_result); |
107 | 117 |
108 // A second reserve should fail because there is not enough budget. | 118 // A second reserve should fail because there is not enough budget. |
109 ASSERT_TRUE(RunScript("documentReserveBudget()", &script_result)); | 119 ASSERT_TRUE(RunScript("documentReserveBudget()", &script_result)); |
110 ASSERT_EQ("failed - not able to reserve budget", script_result); | 120 EXPECT_EQ("failed - not able to reserve budget", script_result); |
111 | 121 |
112 // Consume should succeed because there is an existing reservation. | 122 // Consume should succeed because there is an existing reservation. |
113 ConsumeReservation(); | 123 ConsumeReservation(); |
114 ASSERT_TRUE(success()); | 124 ASSERT_TRUE(success()); |
115 | 125 |
116 // Second consume should fail because the reservation is consumed. | 126 // Second consume should fail because the reservation is consumed. |
117 ConsumeReservation(); | 127 ConsumeReservation(); |
118 ASSERT_FALSE(success()); | 128 ASSERT_FALSE(success()); |
119 } | 129 } |
120 | 130 |
121 IN_PROC_BROWSER_TEST_F(BudgetManagerBrowserTest, BudgetInWorker) { | 131 IN_PROC_BROWSER_TEST_F(BudgetManagerBrowserTest, BudgetInWorker) { |
122 std::string script_result; | 132 std::string script_result; |
123 | 133 |
124 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | 134 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
125 ASSERT_EQ("ok - service worker registered", script_result); | 135 ASSERT_EQ("ok - service worker registered", script_result); |
126 | 136 |
127 LoadTestPage(); // Reload to become controlled. | 137 LoadTestPage(); // Reload to become controlled. |
| 138 SetSiteEngagementScore(12); |
128 | 139 |
129 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | 140 ASSERT_TRUE(RunScript("isControlled()", &script_result)); |
130 ASSERT_EQ("true - is controlled", script_result); | 141 ASSERT_EQ("true - is controlled", script_result); |
131 | 142 |
132 // The page will have been loaded twice and a service worker was registered, | 143 // Site engagement score of 12 gives a budget of 5. |
133 // which gives a budget of 4.5. | |
134 ASSERT_TRUE(RunScript("workerGetBudget()", &script_result)); | 144 ASSERT_TRUE(RunScript("workerGetBudget()", &script_result)); |
135 ASSERT_EQ("ok - budget returned value of 4.5", script_result); | 145 EXPECT_EQ("ok - budget returned value of 5", script_result); |
136 | 146 |
137 // With a budget of 4.5, two reservations should succeed. | 147 // With a budget of 5, two reservations should succeed. |
138 ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result)); | 148 ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result)); |
139 ASSERT_EQ("ok - reserved budget", script_result); | 149 EXPECT_EQ("ok - reserved budget", script_result); |
140 | 150 |
141 ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result)); | 151 ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result)); |
142 ASSERT_EQ("ok - reserved budget", script_result); | 152 EXPECT_EQ("ok - reserved budget", script_result); |
143 | 153 |
144 // After reserving budget, the new budget should be at 0.5. | 154 // After reserving budget, the new budget should be at 1. |
145 ASSERT_TRUE(RunScript("workerGetBudget()", &script_result)); | 155 ASSERT_TRUE(RunScript("workerGetBudget()", &script_result)); |
146 ASSERT_EQ("ok - budget returned value of 0.5", script_result); | 156 EXPECT_EQ("ok - budget returned value of 1", script_result); |
147 | 157 |
148 // A second reserve should fail because there is not enough budget. | 158 // A second reserve should fail because there is not enough budget. |
149 ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result)); | 159 ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result)); |
150 ASSERT_EQ("failed - not able to reserve budget", script_result); | 160 EXPECT_EQ("failed - not able to reserve budget", script_result); |
151 | 161 |
152 // Two consumes should succeed because there are existing reservations. | 162 // Two consumes should succeed because there are existing reservations. |
153 ConsumeReservation(); | 163 ConsumeReservation(); |
154 ASSERT_TRUE(success()); | 164 ASSERT_TRUE(success()); |
155 | 165 |
156 ConsumeReservation(); | 166 ConsumeReservation(); |
157 ASSERT_TRUE(success()); | 167 ASSERT_TRUE(success()); |
158 | 168 |
159 // One more consume should fail, because all reservations are consumed. | 169 // One more consume should fail, because all reservations are consumed. |
160 ConsumeReservation(); | 170 ConsumeReservation(); |
161 ASSERT_FALSE(success()); | 171 ASSERT_FALSE(success()); |
162 } | 172 } |
163 | 173 |
164 } // namespace | 174 } // namespace |
OLD | NEW |