Commit dc60a0a4 authored by thestig's avatar thestig Committed by Commit bot

Clean up WebUIMojoTest.

Review-Url: https://codereview.chromium.org/2250953004
Cr-Commit-Position: refs/heads/master@{#412753}
parent 810329d5
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#include <limits> #include <limits>
#include <utility> #include <utility>
#include "base/command_line.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/macros.h" #include "base/macros.h"
...@@ -38,6 +37,8 @@ ...@@ -38,6 +37,8 @@
namespace content { namespace content {
namespace { namespace {
bool g_got_message = false;
base::FilePath GetFilePathForJSResource(const std::string& path) { base::FilePath GetFilePathForJSResource(const std::string& path) {
base::ThreadRestrictions::ScopedAllowIO allow_io_from_test_callbacks; base::ThreadRestrictions::ScopedAllowIO allow_io_from_test_callbacks;
...@@ -50,29 +51,23 @@ base::FilePath GetFilePathForJSResource(const std::string& path) { ...@@ -50,29 +51,23 @@ base::FilePath GetFilePathForJSResource(const std::string& path) {
return exe_dir.AppendASCII(binding_path); return exe_dir.AppendASCII(binding_path);
} }
bool got_message = false;
// The bindings for the page are generated from a .mojom file. This code looks // The bindings for the page are generated from a .mojom file. This code looks
// up the generated file from disk and returns it. // up the generated file from disk and returns it.
bool GetResource(const std::string& id, bool GetResource(const std::string& id,
const WebUIDataSource::GotDataCallback& callback) { const WebUIDataSource::GotDataCallback& callback) {
base::ThreadRestrictions::ScopedAllowIO allow_io_from_test_callbacks; base::ThreadRestrictions::ScopedAllowIO allow_io_from_test_callbacks;
if (id.find(".mojom") != std::string::npos) { std::string contents;
std::string contents; if (base::EndsWith(id, ".mojom", base::CompareCase::SENSITIVE)) {
CHECK(base::ReadFileToString(GetFilePathForJSResource(id), &contents)) CHECK(base::ReadFileToString(GetFilePathForJSResource(id), &contents))
<< id; << id;
base::RefCountedString* ref_contents = new base::RefCountedString; } else {
ref_contents->data() = contents; base::FilePath path;
callback.Run(ref_contents); CHECK(base::PathService::Get(content::DIR_TEST_DATA, &path));
return true; path = path.AppendASCII(id.substr(0, id.find("?")));
CHECK(base::ReadFileToString(path, &contents)) << path.value();
} }
base::FilePath path;
CHECK(base::PathService::Get(content::DIR_TEST_DATA, &path));
path = path.AppendASCII(id.substr(0, id.find("?")));
std::string contents;
CHECK(base::ReadFileToString(path, &contents)) << path.value();
base::RefCountedString* ref_contents = new base::RefCountedString; base::RefCountedString* ref_contents = new base::RefCountedString;
ref_contents->data() = contents; ref_contents->data() = contents;
callback.Run(ref_contents); callback.Run(ref_contents);
...@@ -92,12 +87,12 @@ class BrowserTargetImpl : public mojom::BrowserTarget { ...@@ -92,12 +87,12 @@ class BrowserTargetImpl : public mojom::BrowserTarget {
closure.Run(); closure.Run();
} }
void Stop() override { void Stop() override {
got_message = true; g_got_message = true;
run_loop_->Quit(); run_loop_->Quit();
} }
protected: protected:
base::RunLoop* run_loop_; base::RunLoop* const run_loop_;
private: private:
mojo::Binding<mojom::BrowserTarget> binding_; mojo::Binding<mojom::BrowserTarget> binding_;
...@@ -109,15 +104,14 @@ class TestWebUIController : public WebUIController { ...@@ -109,15 +104,14 @@ class TestWebUIController : public WebUIController {
public: public:
TestWebUIController(WebUI* web_ui, base::RunLoop* run_loop) TestWebUIController(WebUI* web_ui, base::RunLoop* run_loop)
: WebUIController(web_ui), run_loop_(run_loop) { : WebUIController(web_ui), run_loop_(run_loop) {
content::WebUIDataSource* data_source = WebUIDataSource* data_source = WebUIDataSource::Create("mojo-web-ui");
WebUIDataSource::Create("mojo-web-ui");
data_source->SetRequestFilter(base::Bind(&GetResource)); data_source->SetRequestFilter(base::Bind(&GetResource));
content::WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(), WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(),
data_source); data_source);
} }
protected: protected:
base::RunLoop* run_loop_; base::RunLoop* const run_loop_;
std::unique_ptr<BrowserTargetImpl> browser_target_; std::unique_ptr<BrowserTargetImpl> browser_target_;
private: private:
...@@ -128,10 +122,9 @@ class TestWebUIController : public WebUIController { ...@@ -128,10 +122,9 @@ class TestWebUIController : public WebUIController {
// implementation at the right time. // implementation at the right time.
class PingTestWebUIController : public TestWebUIController { class PingTestWebUIController : public TestWebUIController {
public: public:
PingTestWebUIController(WebUI* web_ui, base::RunLoop* run_loop) PingTestWebUIController(WebUI* web_ui, base::RunLoop* run_loop)
: TestWebUIController(web_ui, run_loop) { : TestWebUIController(web_ui, run_loop) {}
} ~PingTestWebUIController() override {}
~PingTestWebUIController() override {}
// WebUIController overrides: // WebUIController overrides:
void RenderViewCreated(RenderViewHost* render_view_host) override { void RenderViewCreated(RenderViewHost* render_view_host) override {
...@@ -141,7 +134,8 @@ class PingTestWebUIController : public TestWebUIController { ...@@ -141,7 +134,8 @@ class PingTestWebUIController : public TestWebUIController {
} }
void CreateHandler(mojo::InterfaceRequest<mojom::BrowserTarget> request) { void CreateHandler(mojo::InterfaceRequest<mojom::BrowserTarget> request) {
browser_target_.reset(new BrowserTargetImpl(run_loop_, std::move(request))); browser_target_ =
base::MakeUnique<BrowserTargetImpl>(run_loop_, std::move(request));
} }
private: private:
...@@ -151,7 +145,7 @@ class PingTestWebUIController : public TestWebUIController { ...@@ -151,7 +145,7 @@ class PingTestWebUIController : public TestWebUIController {
// WebUIControllerFactory that creates TestWebUIController. // WebUIControllerFactory that creates TestWebUIController.
class TestWebUIControllerFactory : public WebUIControllerFactory { class TestWebUIControllerFactory : public WebUIControllerFactory {
public: public:
TestWebUIControllerFactory() : run_loop_(NULL) {} TestWebUIControllerFactory() : run_loop_(nullptr) {}
void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; }
...@@ -218,7 +212,7 @@ IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEndPing) { ...@@ -218,7 +212,7 @@ IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEndPing) {
"content/test/data/web_ui_test_mojo_bindings.mojom")) "content/test/data/web_ui_test_mojo_bindings.mojom"))
return; return;
got_message = false; g_got_message = false;
ASSERT_TRUE(embedded_test_server()->Start()); ASSERT_TRUE(embedded_test_server()->Start());
base::RunLoop run_loop; base::RunLoop run_loop;
factory()->set_run_loop(&run_loop); factory()->set_run_loop(&run_loop);
...@@ -226,18 +220,18 @@ IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEndPing) { ...@@ -226,18 +220,18 @@ IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEndPing) {
NavigateToURL(shell(), test_url); NavigateToURL(shell(), test_url);
// RunLoop is quit when message received from page. // RunLoop is quit when message received from page.
run_loop.Run(); run_loop.Run();
EXPECT_TRUE(got_message); EXPECT_TRUE(g_got_message);
// Check that a second render frame in the same renderer process works // Check that a second render frame in the same renderer process works
// correctly. // correctly.
Shell* other_shell = CreateBrowser(); Shell* other_shell = CreateBrowser();
got_message = false; g_got_message = false;
base::RunLoop other_run_loop; base::RunLoop other_run_loop;
factory()->set_run_loop(&other_run_loop); factory()->set_run_loop(&other_run_loop);
NavigateToURL(other_shell, test_url); NavigateToURL(other_shell, test_url);
// RunLoop is quit when message received from page. // RunLoop is quit when message received from page.
other_run_loop.Run(); other_run_loop.Run();
EXPECT_TRUE(got_message); EXPECT_TRUE(g_got_message);
EXPECT_EQ(shell()->web_contents()->GetRenderProcessHost(), EXPECT_EQ(shell()->web_contents()->GetRenderProcessHost(),
other_shell->web_contents()->GetRenderProcessHost()); other_shell->web_contents()->GetRenderProcessHost());
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment