OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/bind_helpers.h" | |
6 #include "base/command_line.h" | |
7 #include "base/run_loop.h" | |
8 #include "chrome/browser/budget_service/budget_manager.h" | |
9 #include "chrome/browser/budget_service/budget_manager_factory.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "chrome/browser/ui/browser.h" | |
12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
13 #include "chrome/test/base/in_process_browser_test.h" | |
14 #include "chrome/test/base/ui_test_utils.h" | |
15 #include "content/public/browser/browser_context.h" | |
16 #include "content/public/browser/web_contents.h" | |
17 #include "content/public/common/content_switches.h" | |
18 #include "content/public/test/browser_test_utils.h" | |
19 #include "net/test/embedded_test_server/embedded_test_server.h" | |
20 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi ce.mojom.h" | |
21 #include "url/origin.h" | |
22 | |
23 namespace { | |
24 | |
25 // const char kManifestSenderId[] = "1234567890"; | |
Peter Beverloo
2016/09/27 15:57:47
nit: delete commented out code
harkness
2016/09/28 12:29:20
Done.
| |
26 | |
27 } // namespace | |
Peter Beverloo
2016/09/27 15:57:47
Since you don't need `friends` declarations, can w
harkness
2016/09/28 12:29:21
Done.
| |
28 | |
29 class BudgetManagerBrowserTest : public InProcessBrowserTest { | |
30 public: | |
31 BudgetManagerBrowserTest() {} | |
32 ~BudgetManagerBrowserTest() override {} | |
Peter Beverloo
2016/09/27 15:57:47
nit: {} --> " = default;"
for both the construc
harkness
2016/09/28 12:29:20
Done.
| |
33 | |
34 // InProcessBrowserTest: | |
35 void SetUp() override { | |
36 https_server_.reset( | |
37 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS)); | |
38 https_server_->ServeFilesFromSourceDirectory("chrome/test/data"); | |
39 ASSERT_TRUE(https_server_->Start()); | |
40 | |
41 InProcessBrowserTest::SetUp(); | |
42 } | |
43 | |
44 // InProcessBrowserTest: | |
45 void SetUpOnMainThread() override { | |
46 LoadTestPage(); | |
47 InProcessBrowserTest::SetUpOnMainThread(); | |
48 budget_manager_ = | |
49 BudgetManagerFactory::GetForProfile(GetBrowser()->profile()); | |
50 } | |
51 | |
52 // InProcessBrowserTest: | |
53 void SetUpCommandLine(base::CommandLine* command_line) override { | |
54 command_line->AppendSwitch( | |
Peter Beverloo
2016/09/27 15:57:47
nit: add a TODO w/ a bug reference to remove this
harkness
2016/09/28 12:29:20
Done.
| |
55 switches::kEnableExperimentalWebPlatformFeatures); | |
56 InProcessBrowserTest::SetUpCommandLine(command_line); | |
57 } | |
58 | |
59 bool RunScript(const std::string& script, std::string* result) { | |
60 content::WebContents* web_contents = | |
61 GetBrowser()->tab_strip_model()->GetActiveWebContents(); | |
62 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(), | |
63 script, result); | |
64 } | |
65 | |
66 void LoadTestPage() { | |
67 ui_test_utils::NavigateToURL(GetBrowser(), | |
68 https_server_->GetURL(GetTestURL())); | |
69 } | |
70 | |
71 void DidConsume(base::Closure run_loop_closure, bool success) { | |
72 success_ = success; | |
73 run_loop_closure.Run(); | |
74 } | |
75 | |
76 void ConsumeReservation() { | |
77 base::RunLoop run_loop; | |
78 budget_manager()->Consume( | |
79 url::Origin(https_server_->GetURL(GetTestURL())), | |
80 blink::mojom::BudgetOperationType::SILENT_PUSH, | |
81 base::Bind(&BudgetManagerBrowserTest::DidConsume, | |
82 base::Unretained(this), run_loop.QuitClosure())); | |
83 run_loop.Run(); | |
84 } | |
85 | |
86 BudgetManager* budget_manager() const { return budget_manager_; } | |
87 bool success() const { return success_; } | |
88 | |
89 protected: | |
90 virtual std::string GetTestURL() { return "/budget_service/test.html"; } | |
91 virtual Browser* GetBrowser() const { return browser(); } | |
Peter Beverloo
2016/09/27 15:57:47
These two don't have to be virtual? Why do they ex
harkness
2016/09/28 12:29:20
Done.
| |
92 | |
93 std::unique_ptr<net::EmbeddedTestServer> https_server_; | |
94 BudgetManager* budget_manager_; | |
Peter Beverloo
2016/09/27 15:57:47
nit: Document why it's OK to hold a weak reference
harkness
2016/09/28 12:29:20
Done.
| |
95 bool success_; | |
Peter Beverloo
2016/09/27 15:57:47
Please initialise scalars with a value.
harkness
2016/09/28 12:29:20
Done.
Peter Beverloo
2016/09/28 13:04:26
Sorry - |budget_manager_|, as a pointer, is also a
harkness
2016/09/28 14:34:19
Done.
| |
96 }; | |
97 | |
98 IN_PROC_BROWSER_TEST_F(BudgetManagerBrowserTest, BudgetInDocument) { | |
99 std::string script_result; | |
100 | |
101 // The page will have been loaded once, which gives a budget of 3. | |
102 ASSERT_TRUE(RunScript("documentGetBudget()", &script_result)); | |
103 ASSERT_EQ("ok - budget returned value of 3", script_result); | |
104 | |
105 ASSERT_TRUE(RunScript("documentReserveBudget()", &script_result)); | |
106 ASSERT_EQ("ok - reserved budget", script_result); | |
107 | |
108 // After reserving budget, the new budget should be at 1. | |
109 ASSERT_TRUE(RunScript("documentGetBudget()", &script_result)); | |
110 ASSERT_EQ("ok - budget returned value of 1", script_result); | |
111 | |
112 // A second reserve should fail because there is not enough budget. | |
113 ASSERT_TRUE(RunScript("documentReserveBudget()", &script_result)); | |
114 ASSERT_EQ("failed - not able to reserve budget", script_result); | |
115 | |
116 // Consume should succeed because there is an existing reservation. | |
117 ConsumeReservation(); | |
118 ASSERT_TRUE(success()); | |
119 | |
120 // Second consume should fail because the reservation is consumed. | |
121 ConsumeReservation(); | |
122 ASSERT_FALSE(success()); | |
123 } | |
124 | |
125 IN_PROC_BROWSER_TEST_F(BudgetManagerBrowserTest, BudgetInWorker) { | |
126 std::string script_result; | |
127 | |
128 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | |
129 ASSERT_EQ("ok - service worker registered", script_result); | |
130 | |
131 LoadTestPage(); // Reload to become controlled. | |
132 | |
133 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | |
134 ASSERT_EQ("true - is controlled", script_result); | |
135 | |
136 // The page will have been loaded twice and a service worker was registered, | |
137 // which gives a budget of 4.5. | |
138 ASSERT_TRUE(RunScript("workerGetBudget()", &script_result)); | |
139 ASSERT_EQ("ok - budget returned value of 4.5", script_result); | |
140 | |
141 // With a budget of 4.5, two reservations should succeed. | |
142 ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result)); | |
143 ASSERT_EQ("ok - reserved budget", script_result); | |
144 | |
145 ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result)); | |
146 ASSERT_EQ("ok - reserved budget", script_result); | |
147 | |
148 // After reserving budget, the new budget should be at 0.5. | |
149 ASSERT_TRUE(RunScript("workerGetBudget()", &script_result)); | |
150 ASSERT_EQ("ok - budget returned value of 0.5", script_result); | |
151 | |
152 // A second reserve should fail because there is not enough budget. | |
153 ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result)); | |
154 ASSERT_EQ("failed - not able to reserve budget", script_result); | |
155 | |
156 // Two consumes should succeed because there are existing reservations. | |
157 ConsumeReservation(); | |
158 ASSERT_TRUE(success()); | |
159 | |
160 ConsumeReservation(); | |
161 ASSERT_TRUE(success()); | |
162 | |
163 // One more consume should fail, because all reservations are consumed. | |
164 ConsumeReservation(); | |
165 ASSERT_FALSE(success()); | |
166 } | |
OLD | NEW |