OLD | NEW |
---|---|
(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. | |
Sami
2016/06/27 11:10:42
and registers mojo interfaces?
alex clarke (OOO till 29th)
2016/06/27 11:35:52
Done.
| |
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 idiomatic 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 browser_context_ = | |
81 browser() | |
82 ->CreateBrowserContextBuilder() | |
83 .AddJsMojoBindings("headless/lib/embedder_test.mojom", | |
84 embedder_test_mojom_js) | |
85 .Build(); | |
86 | |
87 web_contents_ = | |
88 browser() | |
89 ->CreateWebContentsBuilder() | |
90 .SetInitialURL(GURL(GetInitialUrl())) | |
91 .SetBrowserContext(browser_context_.get()) | |
92 .AddMojoService(base::Bind(&EmbedderMojoTest::CreateTestMojoService, | |
93 base::Unretained(this))) | |
94 .Build(); | |
95 | |
96 web_contents_->AddObserver(this); | |
97 RunAsynchronousTest(); | |
98 | |
99 web_contents_->GetDevToolsTarget()->DetachClient(devtools_client_.get()); | |
100 web_contents_->RemoveObserver(this); | |
101 web_contents_->Close(); | |
102 web_contents_ = nullptr; | |
103 | |
104 browser_context_.reset(); | |
105 } | |
106 | |
107 void CreateTestMojoService( | |
108 mojo::InterfaceRequest<embedder_test::TestEmbedderService> request) { | |
109 test_embedder_mojo_bindings_.AddBinding(this, std::move(request)); | |
110 } | |
111 | |
112 std::unique_ptr<HeadlessBrowserContext> browser_context_; | |
113 HeadlessWebContents* web_contents_; | |
114 std::unique_ptr<HeadlessDevToolsClient> devtools_client_; | |
115 | |
116 mojo::BindingSet<embedder_test::TestEmbedderService> | |
117 test_embedder_mojo_bindings_; | |
118 }; | |
119 | |
120 class MojoBindingsTest : public EmbedderMojoTest { | |
121 public: | |
122 void RunMojoTest() override { | |
123 devtools_client_->GetRuntime()->Evaluate( | |
124 "// Note define() defines a module in the mojo module dependency \n" | |
125 "// system. While we don't expose our module, the callback below only\n" | |
126 "// fires after the requested modules have been loaded. \n" | |
127 "define([ \n" | |
128 " 'headless/lib/embedder_test.mojom', \n" | |
129 " 'mojo/public/js/core', \n" | |
130 " 'mojo/public/js/router', \n" | |
131 " 'content/public/renderer/frame_service_registry', \n" | |
132 " ], function(embedderMojom, mojoCore, routerModule, \n" | |
133 " serviceProvider) { \n" | |
134 " var testEmbedderService = \n" | |
135 " new embedderMojom.TestEmbedderService.proxyClass( \n" | |
136 " new routerModule.Router( \n" | |
137 " serviceProvider.connectToService( \n" | |
138 " embedderMojom.TestEmbedderService.name))); \n" | |
139 " \n" | |
140 " // Send a message to the embedder! \n" | |
141 " testEmbedderService.returnTestResult('hello world'); \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("hello world", result.get()); | |
149 FinishAsynchronousTest(); | |
150 } | |
151 }; | |
152 | |
153 DEVTOOLS_CLIENT_TEST_F(MojoBindingsTest); | |
154 | |
155 class MojoBindingsReinstalledAfterNavigation : public EmbedderMojoTest { | |
156 public: | |
157 MojoBindingsReinstalledAfterNavigation() : seen_page_one_(false) { | |
158 EXPECT_TRUE(embedded_test_server()->Start()); | |
159 } | |
160 | |
161 void SetUpOnMainThread() override { | |
162 // We want to make sure bindings work across browser initiated cross-origin | |
163 // navigation, which is why we're setting up this fake tld. | |
164 HeadlessBrowser::Options::Builder builder; | |
165 builder.SetHostResolverRules( | |
166 base::StringPrintf("MAP not-an-actual-domain.tld 127.0.0.1:%d", | |
167 embedded_test_server()->host_port_pair().port())); | |
168 SetBrowserOptions(builder.Build()); | |
169 | |
170 EmbedderMojoTest::SetUpOnMainThread(); | |
171 } | |
172 | |
173 void RunMojoTest() override {} | |
174 | |
175 GURL GetInitialUrl() const override { | |
176 return embedded_test_server()->GetURL("/page_one.html"); | |
177 } | |
178 | |
179 // embedder_test::TestEmbedderService: | |
180 void ReturnTestResult(const mojo::String& result) override { | |
181 if (result.get() == "page one") { | |
182 seen_page_one_ = true; | |
183 devtools_client_->GetPage()->Navigate( | |
184 "http://not-an-actual-domain.tld/page_two.html"); | |
185 } else { | |
186 EXPECT_TRUE(seen_page_one_); | |
187 EXPECT_EQ("page two", result.get()); | |
188 FinishAsynchronousTest(); | |
189 } | |
190 } | |
191 | |
192 private: | |
193 bool seen_page_one_; | |
194 }; | |
195 | |
196 DEVTOOLS_CLIENT_TEST_F(MojoBindingsReinstalledAfterNavigation); | |
197 | |
198 } // namespace headless | |
OLD | NEW |