Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(359)

Side by Side Diff: chrome/browser/budget_service/budget_manager_browsertest.cc

Issue 2370103003: Added browser tests to test the BudgetAPI. Also added BudgetAPI to ExperimentalWebPlatformFeatures. (Closed)
Patch Set: Code review comments and separated out shared code. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/test/BUILD.gn » ('j') | chrome/test/data/budget_service/budget_test.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 std::string kTestURL = "/budget_service/test.html";
Peter Beverloo 2016/09/28 13:04:27 This is a static initializer, which are bad. Inste
harkness 2016/09/28 14:34:19 Good catch, I was just being careless when I moved
26
27 class BudgetManagerBrowserTest : public InProcessBrowserTest {
28 public:
29 BudgetManagerBrowserTest() = default;
30 ~BudgetManagerBrowserTest() override = default;
31
32 // InProcessBrowserTest:
33 void SetUp() override {
34 https_server_.reset(
35 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS));
36 https_server_->ServeFilesFromSourceDirectory("chrome/test/data");
37 ASSERT_TRUE(https_server_->Start());
38
39 InProcessBrowserTest::SetUp();
40 }
41
42 // InProcessBrowserTest:
43 void SetUpOnMainThread() override {
44 LoadTestPage();
45 InProcessBrowserTest::SetUpOnMainThread();
46 budget_manager_ = BudgetManagerFactory::GetForProfile(browser()->profile());
47 }
48
49 // InProcessBrowserTest:
50 void SetUpCommandLine(base::CommandLine* command_line) override {
51 // TODO(harkness): Remove switch once Budget API ships. (crbug.com/617971)
52 command_line->AppendSwitch(
53 switches::kEnableExperimentalWebPlatformFeatures);
54 InProcessBrowserTest::SetUpCommandLine(command_line);
55 }
56
57 bool RunScript(const std::string& script, std::string* result) {
58 content::WebContents* web_contents =
59 browser()->tab_strip_model()->GetActiveWebContents();
60 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(),
61 script, result);
62 }
63
64 void LoadTestPage() {
65 ui_test_utils::NavigateToURL(browser(), https_server_->GetURL(kTestURL));
66 }
67
68 void DidConsume(base::Closure run_loop_closure, bool success) {
69 success_ = success;
70 run_loop_closure.Run();
71 }
72
73 void ConsumeReservation() {
74 base::RunLoop run_loop;
75 budget_manager()->Consume(
76 url::Origin(https_server_->GetURL(kTestURL)),
77 blink::mojom::BudgetOperationType::SILENT_PUSH,
78 base::Bind(&BudgetManagerBrowserTest::DidConsume,
79 base::Unretained(this), run_loop.QuitClosure()));
80 run_loop.Run();
81 }
82
83 BudgetManager* budget_manager() const { return budget_manager_; }
84 bool success() const { return success_; }
85
86 protected:
Peter Beverloo 2016/09/28 13:04:27 If the members are protected, the tests should acc
harkness 2016/09/28 14:34:19 Moved to private.
87 std::unique_ptr<net::EmbeddedTestServer> https_server_;
88 // Lifetime of the BudgetManager is tied to the profile of the test.
89 BudgetManager* budget_manager_;
90 bool success_ = false;
91 };
92
93 IN_PROC_BROWSER_TEST_F(BudgetManagerBrowserTest, BudgetInDocument) {
94 std::string script_result;
95
96 // The page will have been loaded once, which gives a budget of 3.
97 ASSERT_TRUE(RunScript("documentGetBudget()", &script_result));
98 ASSERT_EQ("ok - budget returned value of 3", script_result);
99
100 ASSERT_TRUE(RunScript("documentReserveBudget()", &script_result));
101 ASSERT_EQ("ok - reserved budget", script_result);
102
103 // After reserving budget, the new budget should be at 1.
104 ASSERT_TRUE(RunScript("documentGetBudget()", &script_result));
105 ASSERT_EQ("ok - budget returned value of 1", script_result);
106
107 // A second reserve should fail because there is not enough budget.
108 ASSERT_TRUE(RunScript("documentReserveBudget()", &script_result));
109 ASSERT_EQ("failed - not able to reserve budget", script_result);
110
111 // Consume should succeed because there is an existing reservation.
112 ConsumeReservation();
113 ASSERT_TRUE(success());
114
115 // Second consume should fail because the reservation is consumed.
116 ConsumeReservation();
117 ASSERT_FALSE(success());
118 }
119
120 IN_PROC_BROWSER_TEST_F(BudgetManagerBrowserTest, BudgetInWorker) {
121 std::string script_result;
122
123 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
124 ASSERT_EQ("ok - service worker registered", script_result);
125
126 LoadTestPage(); // Reload to become controlled.
127
128 ASSERT_TRUE(RunScript("isControlled()", &script_result));
129 ASSERT_EQ("true - is controlled", script_result);
130
131 // The page will have been loaded twice and a service worker was registered,
132 // which gives a budget of 4.5.
133 ASSERT_TRUE(RunScript("workerGetBudget()", &script_result));
134 ASSERT_EQ("ok - budget returned value of 4.5", script_result);
135
136 // With a budget of 4.5, two reservations should succeed.
137 ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result));
138 ASSERT_EQ("ok - reserved budget", script_result);
139
140 ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result));
141 ASSERT_EQ("ok - reserved budget", script_result);
142
143 // After reserving budget, the new budget should be at 0.5.
144 ASSERT_TRUE(RunScript("workerGetBudget()", &script_result));
145 ASSERT_EQ("ok - budget returned value of 0.5", script_result);
146
147 // A second reserve should fail because there is not enough budget.
148 ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result));
149 ASSERT_EQ("failed - not able to reserve budget", script_result);
150
151 // Two consumes should succeed because there are existing reservations.
152 ConsumeReservation();
153 ASSERT_TRUE(success());
154
155 ConsumeReservation();
156 ASSERT_TRUE(success());
157
158 // One more consume should fail, because all reservations are consumed.
159 ConsumeReservation();
160 ASSERT_FALSE(success());
161 }
162
163 } // namespace
OLDNEW
« no previous file with comments | « no previous file | chrome/test/BUILD.gn » ('j') | chrome/test/data/budget_service/budget_test.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698