Chromium Code Reviews| Index: chrome/browser/budget_service/budget_manager_browsertest.cc |
| diff --git a/chrome/browser/budget_service/budget_manager_browsertest.cc b/chrome/browser/budget_service/budget_manager_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d7df998fc50d5d7dd364d5ae9b86022a26d58075 |
| --- /dev/null |
| +++ b/chrome/browser/budget_service/budget_manager_browsertest.cc |
| @@ -0,0 +1,166 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/bind_helpers.h" |
| +#include "base/command_line.h" |
| +#include "base/run_loop.h" |
| +#include "chrome/browser/budget_service/budget_manager.h" |
| +#include "chrome/browser/budget_service/budget_manager_factory.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "chrome/test/base/in_process_browser_test.h" |
| +#include "chrome/test/base/ui_test_utils.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/common/content_switches.h" |
| +#include "content/public/test/browser_test_utils.h" |
| +#include "net/test/embedded_test_server/embedded_test_server.h" |
| +#include "third_party/WebKit/public/platform/modules/budget_service/budget_service.mojom.h" |
| +#include "url/origin.h" |
| + |
| +namespace { |
| + |
| +// 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.
|
| + |
| +} // 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.
|
| + |
| +class BudgetManagerBrowserTest : public InProcessBrowserTest { |
| + public: |
| + BudgetManagerBrowserTest() {} |
| + ~BudgetManagerBrowserTest() override {} |
|
Peter Beverloo
2016/09/27 15:57:47
nit: {} --> " = default;"
for both the construc
harkness
2016/09/28 12:29:20
Done.
|
| + |
| + // InProcessBrowserTest: |
| + void SetUp() override { |
| + https_server_.reset( |
| + new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS)); |
| + https_server_->ServeFilesFromSourceDirectory("chrome/test/data"); |
| + ASSERT_TRUE(https_server_->Start()); |
| + |
| + InProcessBrowserTest::SetUp(); |
| + } |
| + |
| + // InProcessBrowserTest: |
| + void SetUpOnMainThread() override { |
| + LoadTestPage(); |
| + InProcessBrowserTest::SetUpOnMainThread(); |
| + budget_manager_ = |
| + BudgetManagerFactory::GetForProfile(GetBrowser()->profile()); |
| + } |
| + |
| + // InProcessBrowserTest: |
| + void SetUpCommandLine(base::CommandLine* command_line) override { |
| + 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.
|
| + switches::kEnableExperimentalWebPlatformFeatures); |
| + InProcessBrowserTest::SetUpCommandLine(command_line); |
| + } |
| + |
| + bool RunScript(const std::string& script, std::string* result) { |
| + content::WebContents* web_contents = |
| + GetBrowser()->tab_strip_model()->GetActiveWebContents(); |
| + return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(), |
| + script, result); |
| + } |
| + |
| + void LoadTestPage() { |
| + ui_test_utils::NavigateToURL(GetBrowser(), |
| + https_server_->GetURL(GetTestURL())); |
| + } |
| + |
| + void DidConsume(base::Closure run_loop_closure, bool success) { |
| + success_ = success; |
| + run_loop_closure.Run(); |
| + } |
| + |
| + void ConsumeReservation() { |
| + base::RunLoop run_loop; |
| + budget_manager()->Consume( |
| + url::Origin(https_server_->GetURL(GetTestURL())), |
| + blink::mojom::BudgetOperationType::SILENT_PUSH, |
| + base::Bind(&BudgetManagerBrowserTest::DidConsume, |
| + base::Unretained(this), run_loop.QuitClosure())); |
| + run_loop.Run(); |
| + } |
| + |
| + BudgetManager* budget_manager() const { return budget_manager_; } |
| + bool success() const { return success_; } |
| + |
| + protected: |
| + virtual std::string GetTestURL() { return "/budget_service/test.html"; } |
| + 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.
|
| + |
| + std::unique_ptr<net::EmbeddedTestServer> https_server_; |
| + 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.
|
| + 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.
|
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(BudgetManagerBrowserTest, BudgetInDocument) { |
| + std::string script_result; |
| + |
| + // The page will have been loaded once, which gives a budget of 3. |
| + ASSERT_TRUE(RunScript("documentGetBudget()", &script_result)); |
| + ASSERT_EQ("ok - budget returned value of 3", script_result); |
| + |
| + ASSERT_TRUE(RunScript("documentReserveBudget()", &script_result)); |
| + ASSERT_EQ("ok - reserved budget", script_result); |
| + |
| + // After reserving budget, the new budget should be at 1. |
| + ASSERT_TRUE(RunScript("documentGetBudget()", &script_result)); |
| + ASSERT_EQ("ok - budget returned value of 1", script_result); |
| + |
| + // A second reserve should fail because there is not enough budget. |
| + ASSERT_TRUE(RunScript("documentReserveBudget()", &script_result)); |
| + ASSERT_EQ("failed - not able to reserve budget", script_result); |
| + |
| + // Consume should succeed because there is an existing reservation. |
| + ConsumeReservation(); |
| + ASSERT_TRUE(success()); |
| + |
| + // Second consume should fail because the reservation is consumed. |
| + ConsumeReservation(); |
| + ASSERT_FALSE(success()); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(BudgetManagerBrowserTest, BudgetInWorker) { |
| + std::string script_result; |
| + |
| + ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| + ASSERT_EQ("ok - service worker registered", script_result); |
| + |
| + LoadTestPage(); // Reload to become controlled. |
| + |
| + ASSERT_TRUE(RunScript("isControlled()", &script_result)); |
| + ASSERT_EQ("true - is controlled", script_result); |
| + |
| + // The page will have been loaded twice and a service worker was registered, |
| + // which gives a budget of 4.5. |
| + ASSERT_TRUE(RunScript("workerGetBudget()", &script_result)); |
| + ASSERT_EQ("ok - budget returned value of 4.5", script_result); |
| + |
| + // With a budget of 4.5, two reservations should succeed. |
| + ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result)); |
| + ASSERT_EQ("ok - reserved budget", script_result); |
| + |
| + ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result)); |
| + ASSERT_EQ("ok - reserved budget", script_result); |
| + |
| + // After reserving budget, the new budget should be at 0.5. |
| + ASSERT_TRUE(RunScript("workerGetBudget()", &script_result)); |
| + ASSERT_EQ("ok - budget returned value of 0.5", script_result); |
| + |
| + // A second reserve should fail because there is not enough budget. |
| + ASSERT_TRUE(RunScript("workerReserveBudget()", &script_result)); |
| + ASSERT_EQ("failed - not able to reserve budget", script_result); |
| + |
| + // Two consumes should succeed because there are existing reservations. |
| + ConsumeReservation(); |
| + ASSERT_TRUE(success()); |
| + |
| + ConsumeReservation(); |
| + ASSERT_TRUE(success()); |
| + |
| + // One more consume should fail, because all reservations are consumed. |
| + ConsumeReservation(); |
| + ASSERT_FALSE(success()); |
| +} |