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

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

Issue 2181413002: [headless] Remove default browser context. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More fixes Created 4 years, 4 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
OLDNEW
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/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "content/public/test/browser_test.h" 9 #include "content/public/test/browser_test.h"
10 #include "headless/public/domains/page.h" 10 #include "headless/public/domains/page.h"
11 #include "headless/public/headless_browser.h" 11 #include "headless/public/headless_browser.h"
12 #include "headless/public/headless_devtools_client.h" 12 #include "headless/public/headless_devtools_client.h"
13 #include "headless/public/headless_devtools_target.h" 13 #include "headless/public/headless_devtools_target.h"
14 #include "headless/public/headless_web_contents.h" 14 #include "headless/public/headless_web_contents.h"
15 #include "headless/test/headless_browser_test.h" 15 #include "headless/test/headless_browser_test.h"
16 #include "headless/test/test_protocol_handler.h" 16 #include "headless/test/test_protocol_handler.h"
17 #include "headless/test/test_url_request_job.h" 17 #include "headless/test/test_url_request_job.h"
18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" 18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
19 #include "net/cookies/cookie_store.h" 19 #include "net/cookies/cookie_store.h"
20 #include "net/test/spawned_test_server/spawned_test_server.h" 20 #include "net/test/spawned_test_server/spawned_test_server.h"
21 #include "net/url_request/url_request_context.h" 21 #include "net/url_request/url_request_context.h"
22 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/gfx/geometry/size.h" 24 #include "ui/gfx/geometry/size.h"
24 25
26 using testing::UnorderedElementsAre;
27
25 namespace headless { 28 namespace headless {
26 29
27 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, CreateAndDestroyWebContents) { 30 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, CreateAndDestroyWebContents) {
31 std::unique_ptr<HeadlessBrowserContext> browser_context =
32 browser()->CreateBrowserContextBuilder().Build();
28 HeadlessWebContents* web_contents = 33 HeadlessWebContents* web_contents =
29 browser()->CreateWebContentsBuilder().Build(); 34 browser_context->CreateWebContentsBuilder().Build();
30 EXPECT_TRUE(web_contents); 35 EXPECT_TRUE(web_contents);
31 36
32 EXPECT_EQ(static_cast<size_t>(1), browser()->GetAllWebContents().size()); 37 EXPECT_THAT(browser()->GetAllWebContents(),
33 EXPECT_EQ(web_contents, browser()->GetAllWebContents()[0]); 38 UnorderedElementsAre(web_contents));
39 EXPECT_THAT(browser_context->GetAllWebContents(),
40 UnorderedElementsAre(web_contents));
34 // TODO(skyostil): Verify viewport dimensions once we can. 41 // TODO(skyostil): Verify viewport dimensions once we can.
42
35 web_contents->Close(); 43 web_contents->Close();
36 44
37 EXPECT_TRUE(browser()->GetAllWebContents().empty()); 45 EXPECT_TRUE(browser()->GetAllWebContents().empty());
46 EXPECT_TRUE(browser_context->GetAllWebContents().empty());
47 }
48
49 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest,
50 WebContentsAreDestroyedWithContext) {
51 std::unique_ptr<HeadlessBrowserContext> browser_context =
52 browser()->CreateBrowserContextBuilder().Build();
53 HeadlessWebContents* web_contents =
54 browser_context->CreateWebContentsBuilder().Build();
55 EXPECT_TRUE(web_contents);
56
57 EXPECT_THAT(browser()->GetAllWebContents(),
58 UnorderedElementsAre(web_contents));
59 EXPECT_THAT(browser_context->GetAllWebContents(),
60 UnorderedElementsAre(web_contents));
61
62 browser_context.reset();
63
64 EXPECT_TRUE(browser()->GetAllWebContents().empty());
65 }
66
67 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, DestroyAndCreateTwoWebContents) {
68 std::unique_ptr<HeadlessBrowserContext> browser_context1 =
69 browser()->CreateBrowserContextBuilder().Build();
70 HeadlessWebContents* web_contents1 =
71 browser_context1->CreateWebContentsBuilder().Build();
72
73 EXPECT_THAT(browser()->GetAllWebContents(),
74 UnorderedElementsAre(web_contents1));
75 EXPECT_THAT(browser_context1->GetAllWebContents(),
76 UnorderedElementsAre(web_contents1));
77
78 std::unique_ptr<HeadlessBrowserContext> browser_context2 =
79 browser()->CreateBrowserContextBuilder().Build();
80 HeadlessWebContents* web_contents2 =
81 browser_context2->CreateWebContentsBuilder().Build();
82
83 EXPECT_THAT(browser()->GetAllWebContents(),
84 UnorderedElementsAre(web_contents1, web_contents2));
85 EXPECT_THAT(browser_context1->GetAllWebContents(),
86 UnorderedElementsAre(web_contents1));
87 EXPECT_THAT(browser_context2->GetAllWebContents(),
88 UnorderedElementsAre(web_contents2));
89
90 browser_context1.reset();
91
92 EXPECT_THAT(browser()->GetAllWebContents(),
93 UnorderedElementsAre(web_contents2));
94 EXPECT_THAT(browser_context2->GetAllWebContents(),
95 UnorderedElementsAre(web_contents2));
96
97 browser_context2.reset();
98
99 EXPECT_TRUE(browser()->GetAllWebContents().empty());
38 } 100 }
39 101
40 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, CreateWithBadURL) { 102 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, CreateWithBadURL) {
41 GURL bad_url("not_valid"); 103 GURL bad_url("not_valid");
104
105 std::unique_ptr<HeadlessBrowserContext> browser_context =
106 browser()->CreateBrowserContextBuilder().Build();
42 HeadlessWebContents* web_contents = 107 HeadlessWebContents* web_contents =
43 browser()->CreateWebContentsBuilder().SetInitialURL(bad_url).Build(); 108 browser_context->CreateWebContentsBuilder()
109 .SetInitialURL(bad_url)
110 .Build();
111
44 EXPECT_FALSE(web_contents); 112 EXPECT_FALSE(web_contents);
45 EXPECT_TRUE(browser()->GetAllWebContents().empty()); 113 EXPECT_TRUE(browser()->GetAllWebContents().empty());
46 } 114 }
47 115
48 class HeadlessBrowserTestWithProxy : public HeadlessBrowserTest { 116 class HeadlessBrowserTestWithProxy : public HeadlessBrowserTest {
49 public: 117 public:
50 HeadlessBrowserTestWithProxy() 118 HeadlessBrowserTestWithProxy()
51 : proxy_server_(net::SpawnedTestServer::TYPE_HTTP, 119 : proxy_server_(net::SpawnedTestServer::TYPE_HTTP,
52 net::SpawnedTestServer::kLocalhost, 120 net::SpawnedTestServer::kLocalhost,
53 base::FilePath(FILE_PATH_LITERAL("headless/test/data"))) { 121 base::FilePath(FILE_PATH_LITERAL("headless/test/data"))) {
54 } 122 }
55 123
56 void SetUp() override { 124 void SetUp() override {
57 ASSERT_TRUE(proxy_server_.Start()); 125 ASSERT_TRUE(proxy_server_.Start());
58 HeadlessBrowserTest::SetUp(); 126 HeadlessBrowserTest::SetUp();
59 } 127 }
60 128
61 void TearDown() override { 129 void TearDown() override {
62 proxy_server_.Stop(); 130 proxy_server_.Stop();
63 HeadlessBrowserTest::TearDown(); 131 HeadlessBrowserTest::TearDown();
64 } 132 }
65 133
66 net::SpawnedTestServer* proxy_server() { return &proxy_server_; } 134 net::SpawnedTestServer* proxy_server() { return &proxy_server_; }
67 135
68 private: 136 private:
69 net::SpawnedTestServer proxy_server_; 137 net::SpawnedTestServer proxy_server_;
70 }; 138 };
71 139
72 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTestWithProxy, SetProxyServer) { 140 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTestWithProxy, SetProxyServer) {
73 HeadlessBrowser::Options::Builder builder; 141 std::unique_ptr<HeadlessBrowserContext> browser_context =
74 builder.SetProxyServer(proxy_server()->host_port_pair()); 142 browser()
75 SetBrowserOptions(builder.Build()); 143 ->CreateBrowserContextBuilder()
144 .SetProxyServer(proxy_server()->host_port_pair())
145 .Build();
76 146
77 // Load a page which doesn't actually exist, but for which the our proxy 147 // Load a page which doesn't actually exist, but for which the our proxy
78 // returns valid content anyway. 148 // returns valid content anyway.
79 // 149 //
80 // TODO(altimin): Currently this construction does not serve hello.html 150 // TODO(altimin): Currently this construction does not serve hello.html
81 // from headless/test/data as expected. We should fix this. 151 // from headless/test/data as expected. We should fix this.
82 HeadlessWebContents* web_contents = 152 HeadlessWebContents* web_contents =
83 browser() 153 browser_context->CreateWebContentsBuilder()
84 ->CreateWebContentsBuilder()
85 .SetInitialURL(GURL("http://not-an-actual-domain.tld/hello.html")) 154 .SetInitialURL(GURL("http://not-an-actual-domain.tld/hello.html"))
86 .Build(); 155 .Build();
87 EXPECT_TRUE(WaitForLoad(web_contents)); 156 EXPECT_TRUE(WaitForLoad(web_contents));
88 EXPECT_EQ(static_cast<size_t>(1), browser()->GetAllWebContents().size()); 157 EXPECT_THAT(browser()->GetAllWebContents(),
89 EXPECT_EQ(web_contents, browser()->GetAllWebContents()[0]); 158 UnorderedElementsAre(web_contents));
90 web_contents->Close(); 159 web_contents->Close();
91 EXPECT_TRUE(browser()->GetAllWebContents().empty()); 160 EXPECT_TRUE(browser()->GetAllWebContents().empty());
92 } 161 }
93 162
94 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, SetHostResolverRules) { 163 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, SetHostResolverRules) {
95 EXPECT_TRUE(embedded_test_server()->Start()); 164 EXPECT_TRUE(embedded_test_server()->Start());
96 HeadlessBrowser::Options::Builder builder; 165
97 builder.SetHostResolverRules( 166 std::string host_resolver_rules =
98 base::StringPrintf("MAP not-an-actual-domain.tld 127.0.0.1:%d", 167 base::StringPrintf("MAP not-an-actual-domain.tld 127.0.0.1:%d",
99 embedded_test_server()->host_port_pair().port())); 168 embedded_test_server()->host_port_pair().port());
100 SetBrowserOptions(builder.Build()); 169
170 std::unique_ptr<HeadlessBrowserContext> browser_context =
171 browser()
172 ->CreateBrowserContextBuilder()
173 .SetHostResolverRules(host_resolver_rules)
174 .Build();
101 175
102 // Load a page which doesn't actually exist, but which is turned into a valid 176 // Load a page which doesn't actually exist, but which is turned into a valid
103 // address by our host resolver rules. 177 // address by our host resolver rules.
104 HeadlessWebContents* web_contents = 178 HeadlessWebContents* web_contents =
105 browser() 179 browser_context->CreateWebContentsBuilder()
106 ->CreateWebContentsBuilder()
107 .SetInitialURL(GURL("http://not-an-actual-domain.tld/hello.html")) 180 .SetInitialURL(GURL("http://not-an-actual-domain.tld/hello.html"))
108 .Build(); 181 .Build();
182
109 EXPECT_TRUE(WaitForLoad(web_contents)); 183 EXPECT_TRUE(WaitForLoad(web_contents));
110 } 184 }
111 185
112 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, HttpProtocolHandler) { 186 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, HttpProtocolHandler) {
113 const std::string kResponseBody = "<p>HTTP response body</p>"; 187 const std::string kResponseBody = "<p>HTTP response body</p>";
114 ProtocolHandlerMap protocol_handlers; 188 ProtocolHandlerMap protocol_handlers;
115 protocol_handlers[url::kHttpScheme] = 189 protocol_handlers[url::kHttpScheme] =
116 base::WrapUnique(new TestProtocolHandler(kResponseBody)); 190 base::WrapUnique(new TestProtocolHandler(kResponseBody));
117 191
118 HeadlessBrowser::Options::Builder builder; 192 std::unique_ptr<HeadlessBrowserContext> browser_context =
119 builder.SetProtocolHandlers(std::move(protocol_handlers)); 193 browser()
120 SetBrowserOptions(builder.Build()); 194 ->CreateBrowserContextBuilder()
195 .SetProtocolHandlers(std::move(protocol_handlers))
196 .Build();
121 197
122 // Load a page which doesn't actually exist, but which is fetched by our 198 // Load a page which doesn't actually exist, but which is fetched by our
123 // custom protocol handler. 199 // custom protocol handler.
124 HeadlessWebContents* web_contents = 200 HeadlessWebContents* web_contents =
125 browser() 201 browser_context->CreateWebContentsBuilder()
126 ->CreateWebContentsBuilder()
127 .SetInitialURL(GURL("http://not-an-actual-domain.tld/hello.html")) 202 .SetInitialURL(GURL("http://not-an-actual-domain.tld/hello.html"))
128 .Build(); 203 .Build();
129 EXPECT_TRUE(WaitForLoad(web_contents)); 204 EXPECT_TRUE(WaitForLoad(web_contents));
130 205
131 std::string inner_html; 206 std::string inner_html;
132 EXPECT_TRUE(EvaluateScript(web_contents, "document.body.innerHTML") 207 EXPECT_TRUE(EvaluateScript(web_contents, "document.body.innerHTML")
133 ->GetResult() 208 ->GetResult()
134 ->GetValue() 209 ->GetValue()
135 ->GetAsString(&inner_html)); 210 ->GetAsString(&inner_html));
136 EXPECT_EQ(kResponseBody, inner_html); 211 EXPECT_EQ(kResponseBody, inner_html);
137 } 212 }
138 213
139 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, HttpsProtocolHandler) { 214 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, HttpsProtocolHandler) {
140 const std::string kResponseBody = "<p>HTTPS response body</p>"; 215 const std::string kResponseBody = "<p>HTTPS response body</p>";
141 ProtocolHandlerMap protocol_handlers; 216 ProtocolHandlerMap protocol_handlers;
142 protocol_handlers[url::kHttpsScheme] = 217 protocol_handlers[url::kHttpsScheme] =
143 base::WrapUnique(new TestProtocolHandler(kResponseBody)); 218 base::WrapUnique(new TestProtocolHandler(kResponseBody));
144 219
145 HeadlessBrowser::Options::Builder builder; 220 std::unique_ptr<HeadlessBrowserContext> browser_context =
146 builder.SetProtocolHandlers(std::move(protocol_handlers)); 221 browser()
147 SetBrowserOptions(builder.Build()); 222 ->CreateBrowserContextBuilder()
223 .SetProtocolHandlers(std::move(protocol_handlers))
224 .Build();
148 225
149 // Load a page which doesn't actually exist, but which is fetched by our 226 // Load a page which doesn't actually exist, but which is fetched by our
150 // custom protocol handler. 227 // custom protocol handler.
151 HeadlessWebContents* web_contents = 228 HeadlessWebContents* web_contents =
152 browser() 229 browser_context->CreateWebContentsBuilder()
153 ->CreateWebContentsBuilder()
154 .SetInitialURL(GURL("https://not-an-actual-domain.tld/hello.html")) 230 .SetInitialURL(GURL("https://not-an-actual-domain.tld/hello.html"))
155 .Build(); 231 .Build();
156 EXPECT_TRUE(WaitForLoad(web_contents)); 232 EXPECT_TRUE(WaitForLoad(web_contents));
157 233
158 std::string inner_html; 234 std::string inner_html;
159 EXPECT_TRUE(EvaluateScript(web_contents, "document.body.innerHTML") 235 EXPECT_TRUE(EvaluateScript(web_contents, "document.body.innerHTML")
160 ->GetResult() 236 ->GetResult()
161 ->GetValue() 237 ->GetValue()
162 ->GetAsString(&inner_html)); 238 ->GetAsString(&inner_html));
163 EXPECT_EQ(kResponseBody, inner_html); 239 EXPECT_EQ(kResponseBody, inner_html);
164 } 240 }
165 241
166 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, WebGLSupported) { 242 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, WebGLSupported) {
243 std::unique_ptr<HeadlessBrowserContext> browser_context =
244 browser()->CreateBrowserContextBuilder().Build();
245
167 HeadlessWebContents* web_contents = 246 HeadlessWebContents* web_contents =
168 browser()->CreateWebContentsBuilder().Build(); 247 browser_context->CreateWebContentsBuilder().Build();
169 248
170 bool webgl_supported; 249 bool webgl_supported;
171 EXPECT_TRUE( 250 EXPECT_TRUE(
172 EvaluateScript(web_contents, 251 EvaluateScript(web_contents,
173 "(document.createElement('canvas').getContext('webgl')" 252 "(document.createElement('canvas').getContext('webgl')"
174 " instanceof WebGLRenderingContext)") 253 " instanceof WebGLRenderingContext)")
175 ->GetResult() 254 ->GetResult()
176 ->GetValue() 255 ->GetValue()
177 ->GetAsBoolean(&webgl_supported)); 256 ->GetAsBoolean(&webgl_supported));
178 EXPECT_TRUE(webgl_supported); 257 EXPECT_TRUE(webgl_supported);
179 } 258 }
180 259
181 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, DefaultSizes) { 260 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, DefaultSizes) {
261 std::unique_ptr<HeadlessBrowserContext> browser_context =
262 browser()->CreateBrowserContextBuilder().Build();
263
182 HeadlessWebContents* web_contents = 264 HeadlessWebContents* web_contents =
183 browser()->CreateWebContentsBuilder().Build(); 265 browser_context->CreateWebContentsBuilder().Build();
184 266
185 HeadlessBrowser::Options::Builder builder; 267 HeadlessBrowser::Options::Builder builder;
186 const HeadlessBrowser::Options kDefaultOptions = builder.Build(); 268 const HeadlessBrowser::Options kDefaultOptions = builder.Build();
187 269
188 int screen_width; 270 int screen_width;
189 int screen_height; 271 int screen_height;
190 int window_width; 272 int window_width;
191 int window_height; 273 int window_height;
192 274
193 EXPECT_TRUE(EvaluateScript(web_contents, "screen.width") 275 EXPECT_TRUE(EvaluateScript(web_contents, "screen.width")
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 } 391 }
310 392
311 } // namespace 393 } // namespace
312 394
313 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, ReadCookiesInProtocolHandler) { 395 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, ReadCookiesInProtocolHandler) {
314 net::CookieList sent_cookies; 396 net::CookieList sent_cookies;
315 ProtocolHandlerMap protocol_handlers; 397 ProtocolHandlerMap protocol_handlers;
316 protocol_handlers[url::kHttpsScheme] = 398 protocol_handlers[url::kHttpsScheme] =
317 base::WrapUnique(new ProtocolHandlerWithCookies(&sent_cookies)); 399 base::WrapUnique(new ProtocolHandlerWithCookies(&sent_cookies));
318 400
319 HeadlessBrowser::Options::Builder builder; 401 std::unique_ptr<HeadlessBrowserContext> browser_context =
320 builder.SetProtocolHandlers(std::move(protocol_handlers)); 402 browser()
321 SetBrowserOptions(builder.Build()); 403 ->CreateBrowserContextBuilder()
404 .SetProtocolHandlers(std::move(protocol_handlers))
405 .Build();
322 406
323 HeadlessWebContents* web_contents = 407 HeadlessWebContents* web_contents =
324 browser() 408 browser_context->CreateWebContentsBuilder()
325 ->CreateWebContentsBuilder()
326 .SetInitialURL(GURL("https://example.com/cookie.html")) 409 .SetInitialURL(GURL("https://example.com/cookie.html"))
327 .Build(); 410 .Build();
328 EXPECT_TRUE(WaitForLoad(web_contents)); 411 EXPECT_TRUE(WaitForLoad(web_contents));
329 412
330 // The first load has no cookies. 413 // The first load has no cookies.
331 EXPECT_EQ(0u, sent_cookies.size()); 414 EXPECT_EQ(0u, sent_cookies.size());
332 415
333 // Set a cookie and reload the page. 416 // Set a cookie and reload the page.
334 EXPECT_FALSE(EvaluateScript( 417 EXPECT_FALSE(EvaluateScript(
335 web_contents, 418 web_contents,
336 "document.cookie = 'shape=oblong', window.location.reload()") 419 "document.cookie = 'shape=oblong', window.location.reload()")
337 ->HasExceptionDetails()); 420 ->HasExceptionDetails());
338 EXPECT_TRUE(WaitForLoad(web_contents)); 421 EXPECT_TRUE(WaitForLoad(web_contents));
339 422
340 // We should have sent the cookie this time. 423 // We should have sent the cookie this time.
341 EXPECT_EQ(1u, sent_cookies.size()); 424 EXPECT_EQ(1u, sent_cookies.size());
342 EXPECT_EQ("shape", sent_cookies[0].Name()); 425 EXPECT_EQ("shape", sent_cookies[0].Name());
343 EXPECT_EQ("oblong", sent_cookies[0].Value()); 426 EXPECT_EQ("oblong", sent_cookies[0].Value());
344 } 427 }
345 428
346 } // namespace headless 429 } // namespace headless
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698