Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / webui / web_ui_mojo_browsertest.cc
blob847e39e759897e3d9d301b3c035fd3fce27c29d0
1 // Copyright 2014 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.
5 #include <limits>
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/run_loop.h"
11 #include "base/strings/string_util.h"
12 #include "content/browser/webui/web_ui_controller_factory_registry.h"
13 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_ui_controller.h"
19 #include "content/public/browser/web_ui_data_source.h"
20 #include "content/public/common/content_paths.h"
21 #include "content/public/common/content_switches.h"
22 #include "content/public/common/service_registry.h"
23 #include "content/public/common/url_utils.h"
24 #include "content/public/test/content_browser_test.h"
25 #include "content/public/test/content_browser_test_utils.h"
26 #include "content/shell/browser/shell.h"
27 #include "content/test/data/web_ui_test_mojo_bindings.mojom.h"
28 #include "third_party/mojo/src/mojo/edk/test/test_utils.h"
29 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_impl.h"
30 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
31 #include "third_party/mojo/src/mojo/public/js/constants.h"
33 namespace content {
34 namespace {
36 bool got_message = false;
38 // The bindings for the page are generated from a .mojom file. This code looks
39 // up the generated file from disk and returns it.
40 bool GetResource(const std::string& id,
41 const WebUIDataSource::GotDataCallback& callback) {
42 // These are handled by the WebUIDataSource that AddMojoDataSource() creates.
43 if (id == mojo::kBindingsModuleName ||
44 id == mojo::kBufferModuleName ||
45 id == mojo::kCodecModuleName ||
46 id == mojo::kConnectionModuleName ||
47 id == mojo::kConnectorModuleName ||
48 id == mojo::kUnicodeModuleName ||
49 id == mojo::kRouterModuleName ||
50 id == mojo::kValidatorModuleName)
51 return false;
53 std::string contents;
54 CHECK(base::ReadFileToString(mojo::test::GetFilePathForJSResource(id),
55 &contents,
56 std::string::npos)) << id;
57 base::RefCountedString* ref_contents = new base::RefCountedString;
58 ref_contents->data() = contents;
59 callback.Run(ref_contents);
60 return true;
63 class BrowserTargetImpl : public mojo::InterfaceImpl<BrowserTarget> {
64 public:
65 explicit BrowserTargetImpl(base::RunLoop* run_loop) : run_loop_(run_loop) {}
67 ~BrowserTargetImpl() override {}
69 // mojo::InterfaceImpl<BrowserTarget> overrides:
70 void Start(const mojo::Closure& closure) override {
71 closure.Run();
73 void Stop() override {
74 got_message = true;
75 run_loop_->Quit();
78 protected:
79 base::RunLoop* run_loop_;
81 private:
82 DISALLOW_COPY_AND_ASSIGN(BrowserTargetImpl);
85 // WebUIController that sets up mojo bindings.
86 class TestWebUIController : public WebUIController {
87 public:
88 TestWebUIController(WebUI* web_ui, base::RunLoop* run_loop)
89 : WebUIController(web_ui),
90 run_loop_(run_loop) {
91 content::WebUIDataSource* data_source =
92 WebUIDataSource::AddMojoDataSource(
93 web_ui->GetWebContents()->GetBrowserContext());
94 data_source->SetRequestFilter(base::Bind(&GetResource));
97 protected:
98 base::RunLoop* run_loop_;
99 scoped_ptr<BrowserTargetImpl> browser_target_;
101 private:
102 DISALLOW_COPY_AND_ASSIGN(TestWebUIController);
105 // TestWebUIController that additionally creates the ping test BrowserTarget
106 // implementation at the right time.
107 class PingTestWebUIController : public TestWebUIController {
108 public:
109 PingTestWebUIController(WebUI* web_ui, base::RunLoop* run_loop)
110 : TestWebUIController(web_ui, run_loop) {
112 ~PingTestWebUIController() override {}
114 // WebUIController overrides:
115 void RenderViewCreated(RenderViewHost* render_view_host) override {
116 render_view_host->GetMainFrame()->GetServiceRegistry()->
117 AddService<BrowserTarget>(base::Bind(
118 &PingTestWebUIController::CreateHandler, base::Unretained(this)));
121 void CreateHandler(mojo::InterfaceRequest<BrowserTarget> request) {
122 browser_target_.reset(mojo::WeakBindToRequest(
123 new BrowserTargetImpl(run_loop_), &request));
126 private:
127 DISALLOW_COPY_AND_ASSIGN(PingTestWebUIController);
130 // WebUIControllerFactory that creates TestWebUIController.
131 class TestWebUIControllerFactory : public WebUIControllerFactory {
132 public:
133 TestWebUIControllerFactory() : run_loop_(NULL) {}
135 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; }
137 WebUIController* CreateWebUIControllerForURL(WebUI* web_ui,
138 const GURL& url) const override {
139 if (url.query() == "ping")
140 return new PingTestWebUIController(web_ui, run_loop_);
141 return NULL;
143 WebUI::TypeID GetWebUIType(BrowserContext* browser_context,
144 const GURL& url) const override {
145 return reinterpret_cast<WebUI::TypeID>(1);
147 bool UseWebUIForURL(BrowserContext* browser_context,
148 const GURL& url) const override {
149 return true;
151 bool UseWebUIBindingsForURL(BrowserContext* browser_context,
152 const GURL& url) const override {
153 return true;
156 private:
157 base::RunLoop* run_loop_;
159 DISALLOW_COPY_AND_ASSIGN(TestWebUIControllerFactory);
162 class WebUIMojoTest : public ContentBrowserTest {
163 public:
164 WebUIMojoTest() {
165 WebUIControllerFactory::RegisterFactory(&factory_);
168 ~WebUIMojoTest() override {
169 WebUIControllerFactory::UnregisterFactoryForTesting(&factory_);
172 TestWebUIControllerFactory* factory() { return &factory_; }
174 private:
175 TestWebUIControllerFactory factory_;
177 DISALLOW_COPY_AND_ASSIGN(WebUIMojoTest);
180 // Loads a webui page that contains mojo bindings and verifies a message makes
181 // it from the browser to the page and back.
182 IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEndPing) {
183 // Currently there is no way to have a generated file included in the isolate
184 // files. If the bindings file doesn't exist assume we're on such a bot and
185 // pass.
186 // TODO(sky): remove this conditional when isolates support copying from gen.
187 const base::FilePath test_file_path(
188 mojo::test::GetFilePathForJSResource(
189 "content/test/data/web_ui_test_mojo_bindings.mojom"));
190 if (!base::PathExists(test_file_path)) {
191 LOG(WARNING) << " mojom binding file doesn't exist, assuming on isolate";
192 return;
195 got_message = false;
196 ASSERT_TRUE(test_server()->Start());
197 base::RunLoop run_loop;
198 factory()->set_run_loop(&run_loop);
199 GURL test_url(test_server()->GetURL("files/web_ui_mojo.html?ping"));
200 NavigateToURL(shell(), test_url);
201 // RunLoop is quit when message received from page.
202 run_loop.Run();
203 EXPECT_TRUE(got_message);
205 // Check that a second render frame in the same renderer process works
206 // correctly.
207 Shell* other_shell = CreateBrowser();
208 got_message = false;
209 base::RunLoop other_run_loop;
210 factory()->set_run_loop(&other_run_loop);
211 NavigateToURL(other_shell, test_url);
212 // RunLoop is quit when message received from page.
213 other_run_loop.Run();
214 EXPECT_TRUE(got_message);
215 EXPECT_EQ(shell()->web_contents()->GetRenderProcessHost(),
216 other_shell->web_contents()->GetRenderProcessHost());
219 } // namespace
220 } // namespace content