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

Side by Side Diff: headless/lib/embedder_mojo_browsertest.cc

Issue 2049363003: Adds support for headless chrome embedder mojo services (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix MojoBindingsReinstalledAfterNavigation to use a browser initiated cross-origin navigation. Created 4 years, 6 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 | « headless/lib/browser/headless_web_contents_impl.cc ('k') | headless/lib/embedder_test.mojom » ('j') | no next file with comments »
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 <memory>
6 #include "base/path_service.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/threading/thread_restrictions.h"
9 #include "content/public/test/browser_test.h"
10 #include "headless/grit/headless_browsertest_resources.h"
11 #include "headless/lib/embedder_test.mojom.h"
12 #include "headless/public/domains/runtime.h"
13 #include "headless/public/domains/page.h"
14 #include "headless/public/headless_browser.h"
15 #include "headless/public/headless_devtools_client.h"
16 #include "headless/public/headless_devtools_target.h"
17 #include "headless/public/headless_web_contents.h"
18 #include "headless/test/headless_browser_test.h"
19 #include "mojo/public/cpp/bindings/binding_set.h"
20 #include "mojo/public/cpp/bindings/interface_ptr_set.h"
21 #include "mojo/public/cpp/bindings/interface_request.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "url/gurl.h"
25
26 namespace headless {
27
28 #define DEVTOOLS_CLIENT_TEST_F(TEST_FIXTURE_NAME) \
29 IN_PROC_BROWSER_TEST_F(TEST_FIXTURE_NAME, RunAsyncTest) { RunTest(); } \
30 class AsyncHeadlessBrowserTestNeedsSemicolon##TEST_FIXTURE_NAME {}
31
32 // A test fixture which attaches a devtools client before starting the test.
33 class EmbedderMojoTest : public HeadlessBrowserTest,
34 public HeadlessWebContents::Observer,
35 public embedder_test::TestEmbedderService {
36 public:
37 EmbedderMojoTest()
38 : web_contents_(nullptr),
39 devtools_client_(HeadlessDevToolsClient::Create()) {}
40 ~EmbedderMojoTest() override {}
41
42 void SetUpOnMainThread() override {
43 base::ThreadRestrictions::SetIOAllowed(true);
44 base::FilePath pak_path;
45 ASSERT_TRUE(PathService::Get(base::DIR_MODULE, &pak_path));
46 pak_path = pak_path.AppendASCII("headless_browser_tests.pak");
47 ResourceBundle::GetSharedInstance().AddDataPackFromPath(
48 pak_path, ui::SCALE_FACTOR_NONE);
49 }
50
51 // HeadlessWebContentsObserver implementation:
52 void DevToolsTargetReady() override {
53 EXPECT_TRUE(web_contents_->GetDevToolsTarget());
54 web_contents_->GetDevToolsTarget()->AttachClient(devtools_client_.get());
55
56 RunMojoTest();
57 }
58
59 virtual void RunMojoTest() = 0;
60
61 virtual GURL GetInitialUrl() const { return GURL("about:blank"); }
62
63 void OnEvalResult(std::unique_ptr<runtime::EvaluateResult> result) {
64 EXPECT_FALSE(result->HasExceptionDetails())
65 << "JS exception: " << result->GetExceptionDetails()->GetText();
66 if (result->HasExceptionDetails()) {
67 FinishAsynchronousTest();
68 }
69 }
70
71 protected:
72 void RunTest() {
73 // Using a pak file is idomatic chromium style, but most embedders probably
74 // wouln't load the javascript bindings file this way.
75 std::string embedder_test_mojom_js =
76 ResourceBundle::GetSharedInstance()
77 .GetRawDataResource(IDR_HEADLESS_EMBEDDER_TEST_MOJOM_JS)
78 .as_string();
79
80 web_contents_ =
81 browser()
82 ->CreateWebContentsBuilder()
83 .SetInitialURL(GURL(GetInitialUrl()))
84 .AddMojoService(base::Bind(&EmbedderMojoTest::CreateTestMojoService,
85 base::Unretained(this)))
86 .AddJsMojoBindings("headless/lib/embedder_test.mojom",
87 embedder_test_mojom_js)
88 .Build();
89
90 web_contents_->AddObserver(this);
91 RunAsynchronousTest();
92
93 web_contents_->GetDevToolsTarget()->DetachClient(devtools_client_.get());
94 web_contents_->RemoveObserver(this);
95 web_contents_->Close();
96 web_contents_ = nullptr;
97 }
98
99 void CreateTestMojoService(
100 mojo::InterfaceRequest<embedder_test::TestEmbedderService> request) {
101 test_embedder_mojo_bindings_.AddBinding(this, std::move(request));
102 }
103
104 HeadlessWebContents* web_contents_;
105 std::unique_ptr<HeadlessDevToolsClient> devtools_client_;
106
107 mojo::BindingSet<embedder_test::TestEmbedderService>
108 test_embedder_mojo_bindings_;
109 };
110
111 class EmbedderMojoBindingsTest : public EmbedderMojoTest {
112 public:
113 void RunMojoTest() override {
114 devtools_client_->GetRuntime()->Evaluate(
115 "mojo.services.embedder_test.TestEmbedderService.then( \n"
116 " function(service) { \n"
117 " service.returnTestResult('hello world'); \n"
118 " });",
119 base::Bind(&EmbedderMojoTest::OnEvalResult, base::Unretained(this)));
120 }
121
122 // embedder_test::TestEmbedderService:
123 void ReturnTestResult(const mojo::String& result) override {
124 EXPECT_EQ("hello world", result.get());
125 FinishAsynchronousTest();
126 }
127 };
128
129 DEVTOOLS_CLIENT_TEST_F(EmbedderMojoBindingsTest);
130
131 class RejectNonExistentBindingsTest : public EmbedderMojoTest {
132 public:
133 void RunMojoTest() override {
134 devtools_client_->GetRuntime()->Evaluate(
135 "mojo.services.embedder_test.TestEmbedderService.then( \n"
136 " function(service) { \n"
137 " mojo.services.nonExistent.MojoService.then(function() { \n"
138 " service.returnTestResult('Fail - promise not rejected'); \n"
139 " }).catch(function() { \n"
140 " service.returnTestResult('Pass - promise rejected'); \n"
141 " }); \n"
142 " });",
143 base::Bind(&EmbedderMojoTest::OnEvalResult, base::Unretained(this)));
144 }
145
146 // embedder_test::TestEmbedderService:
147 void ReturnTestResult(const mojo::String& result) override {
148 EXPECT_EQ("Pass - promise rejected", result.get());
149 FinishAsynchronousTest();
150 }
151 };
152
153 DEVTOOLS_CLIENT_TEST_F(RejectNonExistentBindingsTest);
154
155 // Test bindings that occur after the onload event, which is after the browser
156 // has sent us the bindings.
157 class DelayedRejectNonExistentBindingsTest : public EmbedderMojoTest {
158 public:
159 DelayedRejectNonExistentBindingsTest() {
160 EXPECT_TRUE(embedded_test_server()->Start());
161 }
162
163 GURL GetInitialUrl() const override {
164 return embedded_test_server()->GetURL(
165 "/late_nonexistent_mojo_binding.html");
166 }
167
168 void RunMojoTest() override {}
169
170 // embedder_test::TestEmbedderService:
171 void ReturnTestResult(const mojo::String& result) override {
172 EXPECT_EQ("Pass - promise rejected", result.get());
173 FinishAsynchronousTest();
174 }
175 };
176
177 DEVTOOLS_CLIENT_TEST_F(DelayedRejectNonExistentBindingsTest);
178
179 class HeadScriptEmbedderMojoBindingsTest : public EmbedderMojoTest {
180 public:
181 HeadScriptEmbedderMojoBindingsTest() {
182 EXPECT_TRUE(embedded_test_server()->Start());
183 }
184
185 void RunMojoTest() override {}
186
187 GURL GetInitialUrl() const override {
188 return embedded_test_server()->GetURL("/mojo_test.html");
189 }
190
191 // embedder_test::TestEmbedderService:
192 void ReturnTestResult(const mojo::String& result) override {
193 EXPECT_EQ("hello world", result.get());
194 FinishAsynchronousTest();
195 }
196 };
197
198 DEVTOOLS_CLIENT_TEST_F(HeadScriptEmbedderMojoBindingsTest);
199
200 class DefaultMojoStyleBindingsTest : public EmbedderMojoTest {
201 public:
202 void RunMojoTest() override {
203 devtools_client_->GetRuntime()->Evaluate(
204 "// Note define() defines a module in the mojo module dependency \n"
205 "// system. While we don't expose our module, the callback below only\n"
206 "// fires after the requested modules have been loaded. \n"
207 "define([ \n"
208 " 'headless/lib/embedder_test.mojom', \n"
209 " 'mojo/public/js/core', \n"
210 " 'mojo/public/js/router', \n"
211 " 'content/public/renderer/frame_service_registry', \n"
212 " ], function(embedderMojom, mojoCore, routerModule, \n"
213 " serviceProvider) { \n"
214 " var testEmbedderService = \n"
215 " new embedderMojom.TestEmbedderService.proxyClass( \n"
216 " new routerModule.Router( \n"
217 " serviceProvider.connectToService( \n"
218 " embedderMojom.TestEmbedderService.name))); \n"
219 " \n"
220 " // Send a message to the embedder! \n"
221 " testEmbedderService.returnTestResult('hello world'); \n"
222 "});",
223 base::Bind(&EmbedderMojoTest::OnEvalResult, base::Unretained(this)));
224 }
225
226 // embedder_test::TestEmbedderService:
227 void ReturnTestResult(const mojo::String& result) override {
228 EXPECT_EQ("hello world", result.get());
229 FinishAsynchronousTest();
230 }
231 };
232
233 DEVTOOLS_CLIENT_TEST_F(DefaultMojoStyleBindingsTest);
234
235 class AssignToMojoServicesProxyNotAllowed : public EmbedderMojoTest {
236 public:
237 void RunMojoTest() override {
238 devtools_client_->GetRuntime()->Evaluate(
239 "mojo.services.foo = 'bar';",
240 base::Bind(&AssignToMojoServicesProxyNotAllowed::OnEvalResult,
241 base::Unretained(this)));
242 }
243
244 void OnEvalResult(std::unique_ptr<runtime::EvaluateResult> result) {
245 ASSERT_TRUE(result->HasExceptionDetails());
246 EXPECT_EQ(
247 "Uncaught Error: Assignment to the mojo services proxy is not allowed",
248 result->GetExceptionDetails()->GetText());
249 FinishAsynchronousTest();
250 }
251
252 void ReturnTestResult(const mojo::String& result) override {}
253 };
254
255 DEVTOOLS_CLIENT_TEST_F(AssignToMojoServicesProxyNotAllowed);
256
257 class AssignToMojoServicesSecondaryProxyNotAllowed : public EmbedderMojoTest {
258 public:
259 void RunMojoTest() override {
260 devtools_client_->GetRuntime()->Evaluate(
261 "mojo.services.foo.bar = 'baz';",
262 base::Bind(&AssignToMojoServicesSecondaryProxyNotAllowed::OnEvalResult,
263 base::Unretained(this)));
264 }
265
266 void OnEvalResult(std::unique_ptr<runtime::EvaluateResult> result) {
267 ASSERT_TRUE(result->HasExceptionDetails());
268 EXPECT_EQ(
269 "Uncaught Error: Assignment to the mojo services proxy is not allowed",
270 result->GetExceptionDetails()->GetText());
271 FinishAsynchronousTest();
272 }
273
274 void ReturnTestResult(const mojo::String& result) override {}
275 };
276
277 DEVTOOLS_CLIENT_TEST_F(AssignToMojoServicesSecondaryProxyNotAllowed);
278
279 class MojoBindingsReinstalledAfterNavigation : public EmbedderMojoTest {
280 public:
281 MojoBindingsReinstalledAfterNavigation() : seen_page_one_(false) {
282 EXPECT_TRUE(embedded_test_server()->Start());
283 }
284
285 void SetUpOnMainThread() override {
286 // We want to make sure bindings work across browser initiaited cross-origin
Charlie Reis 2016/06/22 19:30:59 nit: initiated
alex clarke (OOO till 29th) 2016/06/22 20:54:22 Done.
287 // navigation, which is why we're setting up this fake tld.
288 HeadlessBrowser::Options::Builder builder;
289 builder.SetHostResolverRules(
290 base::StringPrintf("MAP not-an-actual-domain.tld 127.0.0.1:%d",
291 embedded_test_server()->host_port_pair().port()));
292 SetBrowserOptions(builder.Build());
293
294 EmbedderMojoTest::SetUpOnMainThread();
295 }
296
297 void RunMojoTest() override {}
298
299 GURL GetInitialUrl() const override {
300 return embedded_test_server()->GetURL("/page_one.html");
301 }
302
303 // embedder_test::TestEmbedderService:
304 void ReturnTestResult(const mojo::String& result) override {
305 if (result.get() == "page one") {
306 seen_page_one_ = true;
307 devtools_client_->GetPage()->Navigate(
308 "http://not-an-actual-domain.tld/page_two.html");
309 } else {
310 EXPECT_TRUE(seen_page_one_);
311 EXPECT_EQ("page two", result.get());
312 FinishAsynchronousTest();
313 }
314 }
315
316 private:
317 bool seen_page_one_;
318 };
319
320 DEVTOOLS_CLIENT_TEST_F(MojoBindingsReinstalledAfterNavigation);
321
322 } // namespace headless
OLDNEW
« no previous file with comments | « headless/lib/browser/headless_web_contents_impl.cc ('k') | headless/lib/embedder_test.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698