Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / content / browser / webui / web_ui_mojo_browsertest.cc
blob598dbe16568f149ea555e6ccf9f177163f3d884d
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/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/path_service.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string_util.h"
13 #include "content/browser/webui/web_ui_controller_factory_registry.h"
14 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/browser/web_ui_controller.h"
18 #include "content/public/browser/web_ui_data_source.h"
19 #include "content/public/common/content_paths.h"
20 #include "content/public/common/content_switches.h"
21 #include "content/public/common/url_utils.h"
22 #include "content/public/test/content_browser_test.h"
23 #include "content/public/test/content_browser_test_utils.h"
24 #include "content/test/data/web_ui_test_mojo_bindings.mojom.h"
25 #include "grit/content_resources.h"
26 #include "mojo/common/test/test_utils.h"
27 #include "mojo/public/cpp/bindings/allocation_scope.h"
28 #include "mojo/public/cpp/bindings/remote_ptr.h"
29 #include "mojo/public/js/bindings/constants.h"
31 namespace content {
32 namespace {
34 bool got_message = false;
36 // The bindings for the page are generated from a .mojom file. This code looks
37 // up the generated file from disk and returns it.
38 bool GetResource(const std::string& id,
39 const WebUIDataSource::GotDataCallback& callback) {
40 // These are handled by the WebUIDataSource that AddMojoDataSource() creates.
41 if (id == mojo::kCodecModuleName ||
42 id == mojo::kConnectionModuleName ||
43 id == mojo::kConnectorModuleName ||
44 id == mojo::kRouterModuleName)
45 return false;
47 std::string contents;
48 CHECK(base::ReadFileToString(mojo::test::GetFilePathForJSResource(id),
49 &contents,
50 std::string::npos)) << id;
51 base::RefCountedString* ref_contents = new base::RefCountedString;
52 ref_contents->data() = contents;
53 callback.Run(ref_contents);
54 return true;
57 class BrowserTargetImpl : public mojo::BrowserTarget {
58 public:
59 BrowserTargetImpl(mojo::ScopedRendererTargetHandle& handle,
60 base::RunLoop* run_loop)
61 : client_(handle.Pass(), this),
62 run_loop_(run_loop) {
65 virtual ~BrowserTargetImpl() {}
67 // mojo::BrowserTarget overrides:
68 virtual void PingResponse() OVERRIDE {
69 NOTREACHED();
72 protected:
73 mojo::RemotePtr<mojo::RendererTarget> client_;
74 base::RunLoop* run_loop_;
76 private:
77 DISALLOW_COPY_AND_ASSIGN(BrowserTargetImpl);
80 class PingBrowserTargetImpl : public BrowserTargetImpl {
81 public:
82 PingBrowserTargetImpl(mojo::ScopedRendererTargetHandle handle,
83 base::RunLoop* run_loop)
84 : BrowserTargetImpl(handle, run_loop) {
85 client_->Ping();
88 virtual ~PingBrowserTargetImpl() {}
90 // mojo::BrowserTarget overrides:
91 // Quit the RunLoop when called.
92 virtual void PingResponse() OVERRIDE {
93 got_message = true;
94 run_loop_->Quit();
97 private:
98 DISALLOW_COPY_AND_ASSIGN(PingBrowserTargetImpl);
101 // WebUIController that sets up mojo bindings.
102 class TestWebUIController : public WebUIController {
103 public:
104 TestWebUIController(WebUI* web_ui, base::RunLoop* run_loop)
105 : WebUIController(web_ui),
106 run_loop_(run_loop) {
107 content::WebUIDataSource* data_source =
108 WebUIDataSource::AddMojoDataSource(
109 web_ui->GetWebContents()->GetBrowserContext());
110 data_source->SetRequestFilter(base::Bind(&GetResource));
113 protected:
114 base::RunLoop* run_loop_;
115 scoped_ptr<BrowserTargetImpl> browser_target_;
117 private:
118 DISALLOW_COPY_AND_ASSIGN(TestWebUIController);
121 // TestWebUIController that additionally creates the ping test BrowserTarget
122 // implementation at the right time.
123 class PingTestWebUIController : public TestWebUIController {
124 public:
125 PingTestWebUIController(WebUI* web_ui, base::RunLoop* run_loop)
126 : TestWebUIController(web_ui, run_loop) {
129 // WebUIController overrides:
130 virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE {
131 mojo::InterfacePipe<mojo::BrowserTarget, mojo::RendererTarget> pipe;
132 browser_target_.reset(new PingBrowserTargetImpl(
133 pipe.handle_to_peer.Pass(), run_loop_));
134 render_view_host->SetWebUIHandle(
135 mojo::ScopedMessagePipeHandle(pipe.handle_to_self.release()));
138 private:
139 DISALLOW_COPY_AND_ASSIGN(PingTestWebUIController);
142 // WebUIControllerFactory that creates TestWebUIController.
143 class TestWebUIControllerFactory : public WebUIControllerFactory {
144 public:
145 TestWebUIControllerFactory() : run_loop_(NULL) {}
147 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; }
149 virtual WebUIController* CreateWebUIControllerForURL(
150 WebUI* web_ui, const GURL& url) const OVERRIDE {
151 if (url.query() == "ping")
152 return new PingTestWebUIController(web_ui, run_loop_);
153 return NULL;
155 virtual WebUI::TypeID GetWebUIType(BrowserContext* browser_context,
156 const GURL& url) const OVERRIDE {
157 return reinterpret_cast<WebUI::TypeID>(1);
159 virtual bool UseWebUIForURL(BrowserContext* browser_context,
160 const GURL& url) const OVERRIDE {
161 return true;
163 virtual bool UseWebUIBindingsForURL(BrowserContext* browser_context,
164 const GURL& url) const OVERRIDE {
165 return true;
168 private:
169 base::RunLoop* run_loop_;
171 DISALLOW_COPY_AND_ASSIGN(TestWebUIControllerFactory);
174 class WebUIMojoTest : public ContentBrowserTest {
175 public:
176 WebUIMojoTest() {
177 WebUIControllerFactory::RegisterFactory(&factory_);
180 virtual ~WebUIMojoTest() {
181 WebUIControllerFactory::UnregisterFactoryForTesting(&factory_);
184 TestWebUIControllerFactory* factory() { return &factory_; }
186 private:
187 TestWebUIControllerFactory factory_;
189 DISALLOW_COPY_AND_ASSIGN(WebUIMojoTest);
192 // Loads a webui page that contains mojo bindings and verifies a message makes
193 // it from the browser to the page and back.
194 IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEndPing) {
195 // Currently there is no way to have a generated file included in the isolate
196 // files. If the bindings file doesn't exist assume we're on such a bot and
197 // pass.
198 // TODO(sky): remove this conditional when isolates support copying from gen.
199 const base::FilePath test_file_path(
200 mojo::test::GetFilePathForJSResource(
201 "content/test/data/web_ui_test_mojo_bindings.mojom"));
202 if (!base::PathExists(test_file_path)) {
203 LOG(WARNING) << " mojom binding file doesn't exist, assuming on isolate";
204 return;
207 got_message = false;
208 ASSERT_TRUE(test_server()->Start());
209 base::RunLoop run_loop;
210 factory()->set_run_loop(&run_loop);
211 GURL test_url(test_server()->GetURL("files/web_ui_mojo.html?ping"));
212 NavigateToURL(shell(), test_url);
213 // RunLoop is quit when message received from page.
214 run_loop.Run();
215 EXPECT_TRUE(got_message);
218 } // namespace
219 } // namespace content