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 | 6 |
| 7 #include "base/files/scoped_temp_dir.h" |
7 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
8 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/threading/thread_restrictions.h" |
9 #include "content/public/test/browser_test.h" | 11 #include "content/public/test/browser_test.h" |
10 #include "headless/public/domains/runtime.h" | 12 #include "headless/public/domains/runtime.h" |
11 #include "headless/public/headless_browser.h" | 13 #include "headless/public/headless_browser.h" |
12 #include "headless/public/headless_browser_context.h" | 14 #include "headless/public/headless_browser_context.h" |
13 #include "headless/public/headless_devtools_client.h" | 15 #include "headless/public/headless_devtools_client.h" |
14 #include "headless/public/headless_devtools_target.h" | 16 #include "headless/public/headless_devtools_target.h" |
15 #include "headless/public/headless_web_contents.h" | 17 #include "headless/public/headless_web_contents.h" |
16 #include "headless/test/headless_browser_test.h" | 18 #include "headless/test/headless_browser_test.h" |
17 #include "headless/test/test_protocol_handler.h" | 19 #include "headless/test/test_protocol_handler.h" |
18 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
(...skipping 29 matching lines...) Expand all Loading... |
48 HeadlessBrowserContextIsolationTest() | 50 HeadlessBrowserContextIsolationTest() |
49 : web_contents2_(nullptr), | 51 : web_contents2_(nullptr), |
50 devtools_client2_(HeadlessDevToolsClient::Create()) { | 52 devtools_client2_(HeadlessDevToolsClient::Create()) { |
51 EXPECT_TRUE(embedded_test_server()->Start()); | 53 EXPECT_TRUE(embedded_test_server()->Start()); |
52 } | 54 } |
53 | 55 |
54 // HeadlessWebContentsObserver implementation: | 56 // HeadlessWebContentsObserver implementation: |
55 void DevToolsTargetReady() override { | 57 void DevToolsTargetReady() override { |
56 if (!web_contents2_) { | 58 if (!web_contents2_) { |
57 browser_context_ = browser()->CreateBrowserContextBuilder().Build(); | 59 browser_context_ = browser()->CreateBrowserContextBuilder().Build(); |
58 web_contents2_ = browser() | 60 web_contents2_ = browser_context_->CreateWebContentsBuilder().Build(); |
59 ->CreateWebContentsBuilder() | |
60 .SetBrowserContext(browser_context_.get()) | |
61 .Build(); | |
62 web_contents2_->AddObserver(this); | 61 web_contents2_->AddObserver(this); |
63 return; | 62 return; |
64 } | 63 } |
65 | 64 |
66 web_contents2_->GetDevToolsTarget()->AttachClient(devtools_client2_.get()); | 65 web_contents2_->GetDevToolsTarget()->AttachClient(devtools_client2_.get()); |
67 HeadlessAsyncDevTooledBrowserTest::DevToolsTargetReady(); | 66 HeadlessAsyncDevTooledBrowserTest::DevToolsTargetReady(); |
68 } | 67 } |
69 | 68 |
70 void RunDevTooledTest() override { | 69 void RunDevTooledTest() override { |
71 load_observer_.reset(new LoadObserver( | 70 load_observer_.reset(new LoadObserver( |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 base::WrapUnique(new TestProtocolHandler(kResponseBody)); | 162 base::WrapUnique(new TestProtocolHandler(kResponseBody)); |
164 | 163 |
165 // Load a page which doesn't actually exist, but which is fetched by our | 164 // Load a page which doesn't actually exist, but which is fetched by our |
166 // custom protocol handler. | 165 // custom protocol handler. |
167 std::unique_ptr<HeadlessBrowserContext> browser_context = | 166 std::unique_ptr<HeadlessBrowserContext> browser_context = |
168 browser() | 167 browser() |
169 ->CreateBrowserContextBuilder() | 168 ->CreateBrowserContextBuilder() |
170 .SetProtocolHandlers(std::move(protocol_handlers)) | 169 .SetProtocolHandlers(std::move(protocol_handlers)) |
171 .Build(); | 170 .Build(); |
172 HeadlessWebContents* web_contents = | 171 HeadlessWebContents* web_contents = |
173 browser() | 172 browser_context->CreateWebContentsBuilder() |
174 ->CreateWebContentsBuilder() | |
175 .SetInitialURL(GURL("http://not-an-actual-domain.tld/hello.html")) | 173 .SetInitialURL(GURL("http://not-an-actual-domain.tld/hello.html")) |
176 .SetBrowserContext(browser_context.get()) | |
177 .Build(); | 174 .Build(); |
178 EXPECT_TRUE(WaitForLoad(web_contents)); | 175 EXPECT_TRUE(WaitForLoad(web_contents)); |
179 | 176 |
180 std::string inner_html; | 177 std::string inner_html; |
181 EXPECT_TRUE(EvaluateScript(web_contents, "document.body.innerHTML") | 178 EXPECT_TRUE(EvaluateScript(web_contents, "document.body.innerHTML") |
182 ->GetResult() | 179 ->GetResult() |
183 ->GetValue() | 180 ->GetValue() |
184 ->GetAsString(&inner_html)); | 181 ->GetAsString(&inner_html)); |
185 EXPECT_EQ(kResponseBody, inner_html); | 182 EXPECT_EQ(kResponseBody, inner_html); |
186 web_contents->Close(); | 183 web_contents->Close(); |
187 | 184 |
188 // Loading the same non-existent page using a tab with the default context | 185 std::unique_ptr<HeadlessBrowserContext> another_browser_context = |
| 186 browser()->CreateBrowserContextBuilder().Build(); |
| 187 |
| 188 // Loading the same non-existent page using a tab with a different context |
189 // should not work since the protocol handler only exists on the custom | 189 // should not work since the protocol handler only exists on the custom |
190 // context. | 190 // context. |
191 web_contents = | 191 web_contents = |
192 browser() | 192 another_browser_context->CreateWebContentsBuilder() |
193 ->CreateWebContentsBuilder() | |
194 .SetInitialURL(GURL("http://not-an-actual-domain.tld/hello.html")) | 193 .SetInitialURL(GURL("http://not-an-actual-domain.tld/hello.html")) |
195 .Build(); | 194 .Build(); |
196 EXPECT_TRUE(WaitForLoad(web_contents)); | 195 EXPECT_TRUE(WaitForLoad(web_contents)); |
197 EXPECT_TRUE(EvaluateScript(web_contents, "document.body.innerHTML") | 196 EXPECT_TRUE(EvaluateScript(web_contents, "document.body.innerHTML") |
198 ->GetResult() | 197 ->GetResult() |
199 ->GetValue() | 198 ->GetValue() |
200 ->GetAsString(&inner_html)); | 199 ->GetAsString(&inner_html)); |
201 EXPECT_EQ("", inner_html); | 200 EXPECT_EQ("", inner_html); |
202 web_contents->Close(); | 201 web_contents->Close(); |
203 } | 202 } |
204 | 203 |
| 204 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, UserDataDir) { |
| 205 // We do not want to bother with posting tasks to create a temp dir. |
| 206 // Just allow IO from main thread for now. |
| 207 base::ThreadRestrictions::SetIOAllowed(true); |
| 208 |
| 209 EXPECT_TRUE(embedded_test_server()->Start()); |
| 210 |
| 211 base::ScopedTempDir user_data_dir; |
| 212 ASSERT_TRUE(user_data_dir.CreateUniqueTempDir()); |
| 213 |
| 214 // Newly created temp directory should be empty. |
| 215 EXPECT_TRUE(base::IsDirectoryEmpty(user_data_dir.path())); |
| 216 |
| 217 std::unique_ptr<HeadlessBrowserContext> browser_context = |
| 218 browser() |
| 219 ->CreateBrowserContextBuilder() |
| 220 .SetUserDataDir(user_data_dir.path()) |
| 221 .Build(); |
| 222 |
| 223 HeadlessWebContents* web_contents = |
| 224 browser_context->CreateWebContentsBuilder() |
| 225 .SetInitialURL(embedded_test_server()->GetURL("/hello.html")) |
| 226 .Build(); |
| 227 |
| 228 EXPECT_TRUE(WaitForLoad(web_contents)); |
| 229 |
| 230 // Something should be written to this directory. |
| 231 // If it is not the case, more complex page may be needed. |
| 232 // ServiceWorkers may be a good option. |
| 233 EXPECT_FALSE(base::IsDirectoryEmpty(user_data_dir.path())); |
| 234 } |
| 235 |
205 } // namespace headless | 236 } // namespace headless |
OLD | NEW |