OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <memory> | 5 #include <memory> |
| 6 #include "base/optional.h" |
6 #include "base/path_service.h" | 7 #include "base/path_service.h" |
7 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
8 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
9 #include "content/public/test/browser_test.h" | 10 #include "content/public/test/browser_test.h" |
10 #include "headless/grit/headless_browsertest_resources.h" | 11 #include "headless/grit/headless_browsertest_resources.h" |
11 #include "headless/lib/embedder_test.mojom.h" | 12 #include "headless/lib/embedder_test.mojom.h" |
12 #include "headless/public/domains/network.h" | 13 #include "headless/public/domains/network.h" |
13 #include "headless/public/domains/page.h" | 14 #include "headless/public/domains/page.h" |
14 #include "headless/public/domains/runtime.h" | 15 #include "headless/public/domains/runtime.h" |
15 #include "headless/public/headless_browser.h" | 16 #include "headless/public/headless_browser.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 | 79 |
79 protected: | 80 protected: |
80 void RunTest() { | 81 void RunTest() { |
81 // Using a pak file is idiomatic chromium style, but most embedders probably | 82 // Using a pak file is idiomatic chromium style, but most embedders probably |
82 // wouln't load the javascript bindings file this way. | 83 // wouln't load the javascript bindings file this way. |
83 std::string embedder_test_mojom_js = | 84 std::string embedder_test_mojom_js = |
84 ResourceBundle::GetSharedInstance() | 85 ResourceBundle::GetSharedInstance() |
85 .GetRawDataResource(IDR_HEADLESS_EMBEDDER_TEST_MOJOM_JS) | 86 .GetRawDataResource(IDR_HEADLESS_EMBEDDER_TEST_MOJOM_JS) |
86 .as_string(); | 87 .as_string(); |
87 | 88 |
| 89 HeadlessBrowserContext::Builder builder = |
| 90 browser()->CreateBrowserContextBuilder(); |
| 91 builder.AddJsMojoBindings("headless/lib/embedder_test.mojom", |
| 92 embedder_test_mojom_js); |
88 if (http_policy_ == HttpPolicy::ENABLE_HTTP) { | 93 if (http_policy_ == HttpPolicy::ENABLE_HTTP) { |
89 browser_context_ = | 94 builder.EnableUnsafeNetworkAccessWithMojoBindings(true); |
90 browser() | 95 } |
91 ->CreateBrowserContextBuilder() | 96 if (host_resolver_rules_) { |
92 .AddJsMojoBindings("headless/lib/embedder_test.mojom", | 97 builder.SetHostResolverRules(*host_resolver_rules_); |
93 embedder_test_mojom_js) | |
94 .EnableUnsafeNetworkAccessWithMojoBindings(true) | |
95 .Build(); | |
96 } else { | |
97 browser_context_ = | |
98 browser() | |
99 ->CreateBrowserContextBuilder() | |
100 .AddJsMojoBindings("headless/lib/embedder_test.mojom", | |
101 embedder_test_mojom_js) | |
102 .Build(); | |
103 } | 98 } |
104 | 99 |
| 100 browser_context_ = builder.Build(); |
| 101 |
105 web_contents_ = | 102 web_contents_ = |
106 browser() | 103 browser_context_->CreateWebContentsBuilder() |
107 ->CreateWebContentsBuilder() | |
108 .SetInitialURL(GURL(GetInitialUrl())) | 104 .SetInitialURL(GURL(GetInitialUrl())) |
109 .SetBrowserContext(browser_context_.get()) | |
110 .AddMojoService(base::Bind(&EmbedderMojoTest::CreateTestMojoService, | 105 .AddMojoService(base::Bind(&EmbedderMojoTest::CreateTestMojoService, |
111 base::Unretained(this))) | 106 base::Unretained(this))) |
112 .Build(); | 107 .Build(); |
113 | 108 |
114 web_contents_->AddObserver(this); | 109 web_contents_->AddObserver(this); |
115 RunAsynchronousTest(); | 110 RunAsynchronousTest(); |
116 | 111 |
117 web_contents_->GetDevToolsTarget()->DetachClient(devtools_client_.get()); | 112 web_contents_->GetDevToolsTarget()->DetachClient(devtools_client_.get()); |
118 web_contents_->RemoveObserver(this); | 113 web_contents_->RemoveObserver(this); |
119 web_contents_->Close(); | 114 web_contents_->Close(); |
120 web_contents_ = nullptr; | 115 web_contents_ = nullptr; |
121 | 116 |
122 browser_context_.reset(); | 117 browser_context_.reset(); |
123 } | 118 } |
124 | 119 |
125 void CreateTestMojoService( | 120 void CreateTestMojoService( |
126 mojo::InterfaceRequest<embedder_test::TestEmbedderService> request) { | 121 mojo::InterfaceRequest<embedder_test::TestEmbedderService> request) { |
127 test_embedder_mojo_bindings_.AddBinding(this, std::move(request)); | 122 test_embedder_mojo_bindings_.AddBinding(this, std::move(request)); |
128 } | 123 } |
129 | 124 |
130 std::unique_ptr<HeadlessBrowserContext> browser_context_; | 125 std::unique_ptr<HeadlessBrowserContext> browser_context_; |
131 HeadlessWebContents* web_contents_; | 126 HeadlessWebContents* web_contents_; |
132 std::unique_ptr<HeadlessDevToolsClient> devtools_client_; | 127 std::unique_ptr<HeadlessDevToolsClient> devtools_client_; |
133 | 128 |
134 mojo::BindingSet<embedder_test::TestEmbedderService> | 129 mojo::BindingSet<embedder_test::TestEmbedderService> |
135 test_embedder_mojo_bindings_; | 130 test_embedder_mojo_bindings_; |
136 | 131 |
137 HttpPolicy http_policy_; | 132 HttpPolicy http_policy_; |
| 133 base::Optional<std::string> host_resolver_rules_; |
138 }; | 134 }; |
139 | 135 |
140 class MojoBindingsTest : public EmbedderMojoTest { | 136 class MojoBindingsTest : public EmbedderMojoTest { |
141 public: | 137 public: |
142 void RunMojoTest() override { | 138 void RunMojoTest() override { |
143 devtools_client_->GetRuntime()->Evaluate( | 139 devtools_client_->GetRuntime()->Evaluate( |
144 "// Note define() defines a module in the mojo module dependency \n" | 140 "// Note define() defines a module in the mojo module dependency \n" |
145 "// system. While we don't expose our module, the callback below only\n" | 141 "// system. While we don't expose our module, the callback below only\n" |
146 "// fires after the requested modules have been loaded. \n" | 142 "// fires after the requested modules have been loaded. \n" |
147 "define([ \n" | 143 "define([ \n" |
(...skipping 27 matching lines...) Expand all Loading... |
175 class MojoBindingsReinstalledAfterNavigation : public EmbedderMojoTest { | 171 class MojoBindingsReinstalledAfterNavigation : public EmbedderMojoTest { |
176 public: | 172 public: |
177 MojoBindingsReinstalledAfterNavigation() | 173 MojoBindingsReinstalledAfterNavigation() |
178 : EmbedderMojoTest(HttpPolicy::ENABLE_HTTP), seen_page_one_(false) { | 174 : EmbedderMojoTest(HttpPolicy::ENABLE_HTTP), seen_page_one_(false) { |
179 EXPECT_TRUE(embedded_test_server()->Start()); | 175 EXPECT_TRUE(embedded_test_server()->Start()); |
180 } | 176 } |
181 | 177 |
182 void SetUpOnMainThread() override { | 178 void SetUpOnMainThread() override { |
183 // We want to make sure bindings work across browser initiated cross-origin | 179 // We want to make sure bindings work across browser initiated cross-origin |
184 // navigation, which is why we're setting up this fake tld. | 180 // navigation, which is why we're setting up this fake tld. |
185 HeadlessBrowser::Options::Builder builder; | 181 host_resolver_rules_ = |
186 builder.SetHostResolverRules( | |
187 base::StringPrintf("MAP not-an-actual-domain.tld 127.0.0.1:%d", | 182 base::StringPrintf("MAP not-an-actual-domain.tld 127.0.0.1:%d", |
188 embedded_test_server()->host_port_pair().port())); | 183 embedded_test_server()->host_port_pair().port()); |
189 SetBrowserOptions(builder.Build()); | |
190 | 184 |
191 EmbedderMojoTest::SetUpOnMainThread(); | 185 EmbedderMojoTest::SetUpOnMainThread(); |
192 } | 186 } |
193 | 187 |
194 void RunMojoTest() override {} | 188 void RunMojoTest() override {} |
195 | 189 |
196 GURL GetInitialUrl() const override { | 190 GURL GetInitialUrl() const override { |
197 return embedded_test_server()->GetURL("/page_one.html"); | 191 return embedded_test_server()->GetURL("/page_one.html"); |
198 } | 192 } |
199 | 193 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 void OnLoadingFailed(const network::LoadingFailedParams& params) override { | 236 void OnLoadingFailed(const network::LoadingFailedParams& params) override { |
243 // The navigation should fail since HTTP requests are blackholed. | 237 // The navigation should fail since HTTP requests are blackholed. |
244 EXPECT_EQ(params.GetErrorText(), "net::ERR_FILE_NOT_FOUND"); | 238 EXPECT_EQ(params.GetErrorText(), "net::ERR_FILE_NOT_FOUND"); |
245 FinishAsynchronousTest(); | 239 FinishAsynchronousTest(); |
246 } | 240 } |
247 }; | 241 }; |
248 | 242 |
249 DEVTOOLS_CLIENT_TEST_F(HttpDisabledByDefaultWhenMojoBindingsUsed); | 243 DEVTOOLS_CLIENT_TEST_F(HttpDisabledByDefaultWhenMojoBindingsUsed); |
250 | 244 |
251 } // namespace headless | 245 } // namespace headless |
OLD | NEW |