1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
headless / test / headless_web_contents_browsertest.cc [blame]
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "headless/public/headless_web_contents.h"
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "base/base64.h"
#include "base/check_deref.h"
#include "base/check_op.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/json/json_writer.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "cc/base/switches.h"
#include "cc/test/pixel_test_utils.h"
#include "components/devtools/simple_devtools_protocol_client/simple_devtools_protocol_client.h"
#include "components/viz/common/switches.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "headless/lib/browser/headless_web_contents_impl.h"
#include "headless/public/headless_browser.h"
#include "headless/test/headless_browser_test.h"
#include "headless/test/headless_browser_test_utils.h"
#include "headless/test/headless_devtooled_browsertest.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/switches.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/size_f.h"
#include "url/gurl.h"
using testing::ElementsAre;
using testing::ElementsAreArray;
using testing::Not;
using testing::UnorderedElementsAre;
using testing::UnorderedElementsAreArray;
using simple_devtools_protocol_client::SimpleDevToolsProtocolClient;
namespace headless {
class HeadlessWebContentsTest : public HeadlessBrowserTest {};
IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, Navigation) {
EXPECT_TRUE(embedded_test_server()->Start());
HeadlessBrowserContext* browser_context =
browser()->CreateBrowserContextBuilder().Build();
HeadlessWebContents* web_contents =
browser_context->CreateWebContentsBuilder()
.SetInitialURL(embedded_test_server()->GetURL("/hello.html"))
.Build();
EXPECT_TRUE(WaitForLoad(web_contents));
EXPECT_THAT(browser_context->GetAllWebContents(),
UnorderedElementsAre(web_contents));
}
IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, WindowOpen) {
EXPECT_TRUE(embedded_test_server()->Start());
HeadlessBrowserContext* browser_context =
browser()->CreateBrowserContextBuilder().Build();
HeadlessWebContents* web_contents =
browser_context->CreateWebContentsBuilder()
.SetInitialURL(embedded_test_server()->GetURL("/window_open.html"))
.Build();
EXPECT_TRUE(WaitForLoad(web_contents));
EXPECT_EQ(2u, browser_context->GetAllWebContents().size());
HeadlessWebContentsImpl* child = nullptr;
HeadlessWebContentsImpl* parent = nullptr;
for (HeadlessWebContents* c : browser_context->GetAllWebContents()) {
HeadlessWebContentsImpl* impl = HeadlessWebContentsImpl::From(c);
if (impl->window_id() == 1)
parent = impl;
else if (impl->window_id() == 2)
child = impl;
}
EXPECT_NE(nullptr, parent);
EXPECT_NE(nullptr, child);
EXPECT_NE(parent, child);
// Mac doesn't have WindowTreeHosts.
if (parent && child && parent->window_tree_host())
EXPECT_NE(parent->window_tree_host(), child->window_tree_host());
gfx::Rect expected_bounds(0, 0, 200, 100);
#if !BUILDFLAG(IS_MAC)
EXPECT_EQ(expected_bounds, child->web_contents()->GetViewBounds());
EXPECT_EQ(expected_bounds, child->web_contents()->GetContainerBounds());
#else // !BUILDFLAG(IS_MAC)
// Mac does not support GetViewBounds() and view positions are random.
EXPECT_EQ(expected_bounds.size(),
child->web_contents()->GetContainerBounds().size());
#endif // !BUILDFLAG(IS_MAC)
}
IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest,
FocusOfHeadlessWebContents_IsIndependent) {
EXPECT_TRUE(embedded_test_server()->Start());
HeadlessBrowserContext* browser_context =
browser()->CreateBrowserContextBuilder().Build();
HeadlessWebContents* web_contents =
browser_context->CreateWebContentsBuilder()
.SetInitialURL(embedded_test_server()->GetURL("/hello.html"))
.Build();
WaitForLoadAndGainFocus(web_contents);
EXPECT_THAT(EvaluateScript(web_contents, "document.hasFocus()"),
DictHasValue("result.result.value", true));
HeadlessWebContents* web_contents2 =
browser_context->CreateWebContentsBuilder()
.SetInitialURL(embedded_test_server()->GetURL("/hello.html"))
.Build();
WaitForLoadAndGainFocus(web_contents2);
// Focus of different WebContents is independent.
EXPECT_THAT(EvaluateScript(web_contents, "document.hasFocus()"),
DictHasValue("result.result.value", true));
EXPECT_THAT(EvaluateScript(web_contents2, "document.hasFocus()"),
DictHasValue("result.result.value", true));
}
IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, HandleSSLError) {
net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS);
https_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED);
ASSERT_TRUE(https_server.Start());
HeadlessBrowserContext* browser_context =
browser()->CreateBrowserContextBuilder().Build();
HeadlessWebContents* web_contents =
browser_context->CreateWebContentsBuilder()
.SetInitialURL(https_server.GetURL("/hello.html"))
.Build();
EXPECT_FALSE(WaitForLoad(web_contents));
}
// Parameter specifies whether --disable-gpu should be used.
class HeadlessWebContentsScreenshotTest
: public HeadlessDevTooledBrowserTest,
public ::testing::WithParamInterface<bool> {
public:
void SetUp() override {
EnablePixelOutput();
if (GetParam()) {
UseSoftwareCompositing();
SetUpWithoutGPU();
} else {
HeadlessDevTooledBrowserTest::SetUp();
}
}
void RunDevTooledTest() override {
devtools_client_.SendCommand(
"Runtime.evaluate",
Param("expression", "document.body.style.background = '#0000ff'"),
base::BindOnce(&HeadlessWebContentsScreenshotTest::OnPageSetupCompleted,
base::Unretained(this)));
}
void OnPageSetupCompleted(base::Value::Dict) {
devtools_client_.SendCommand(
"Page.captureScreenshot",
base::BindOnce(&HeadlessWebContentsScreenshotTest::OnScreenshotCaptured,
base::Unretained(this)));
}
void OnScreenshotCaptured(base::Value::Dict result) {
std::string png_data_base64 = DictString(result, "result.data");
ASSERT_FALSE(png_data_base64.empty());
std::optional<std::vector<uint8_t>> png_data =
base::Base64Decode(png_data_base64);
EXPECT_GT(png_data.value().size(), 0U);
SkBitmap result_bitmap = gfx::PNGCodec::Decode(png_data.value());
EXPECT_FALSE(result_bitmap.isNull());
EXPECT_EQ(800, result_bitmap.width());
EXPECT_EQ(600, result_bitmap.height());
SkColor actual_color = result_bitmap.getColor(400, 300);
SkColor expected_color = SkColorSetRGB(0x00, 0x00, 0xff);
EXPECT_EQ(expected_color, actual_color);
FinishAsynchronousTest();
}
};
HEADLESS_DEVTOOLED_TEST_P(HeadlessWebContentsScreenshotTest);
// Instantiate test case for both software and gpu compositing modes.
INSTANTIATE_TEST_SUITE_P(HeadlessWebContentsScreenshotTests,
HeadlessWebContentsScreenshotTest,
::testing::Bool());
// Regression test for crbug.com/832138.
class HeadlessWebContentsScreenshotWindowPositionTest
: public HeadlessWebContentsScreenshotTest {
public:
void RunDevTooledTest() override {
base::Value::Dict params;
params.Set("windowId",
HeadlessWebContentsImpl::From(web_contents_)->window_id());
params.SetByDottedPath("bounds.left", 600);
params.SetByDottedPath("bounds.top", 100);
params.SetByDottedPath("bounds.width", 800);
params.SetByDottedPath("bounds.height", 600);
browser_devtools_client_.SendCommand(
"Browser.setWindowBounds", std::move(params),
base::BindOnce(
&HeadlessWebContentsScreenshotWindowPositionTest::OnWindowBoundsSet,
base::Unretained(this)));
}
void OnWindowBoundsSet(base::Value::Dict result) {
EXPECT_NE(result.FindDict("result"), nullptr);
HeadlessWebContentsScreenshotTest::RunDevTooledTest();
}
};
HEADLESS_DEVTOOLED_TEST_P(HeadlessWebContentsScreenshotWindowPositionTest);
// Instantiate test case for both software and gpu compositing modes.
INSTANTIATE_TEST_SUITE_P(/* no prefix */,
HeadlessWebContentsScreenshotWindowPositionTest,
::testing::Bool());
// Regression test for https://crbug.com/733569.
class HeadlessWebContentsRequestStorageQuotaTest
: public HeadlessDevTooledBrowserTest {
public:
void RunDevTooledTest() override {
EXPECT_TRUE(embedded_test_server()->Start());
devtools_client_.AddEventHandler(
"Runtime.consoleAPICalled",
base::BindRepeating(
&HeadlessWebContentsRequestStorageQuotaTest::OnConsoleAPICalled,
base::Unretained(this)));
SendCommandSync(devtools_client_, "Runtime.enable");
// Should not crash and call console.log() if quota request succeeds.
devtools_client_.SendCommand(
"Page.navigate",
Param("url", embedded_test_server()
->GetURL("/request_storage_quota.html")
.spec()));
}
void OnConsoleAPICalled(const base::Value::Dict& params) {
const base::Value::List* args = params.FindListByDottedPath("params.args");
ASSERT_NE(args, nullptr);
ASSERT_GT(args->size(), 0ul);
const base::Value* value = args->front().GetDict().Find("value");
ASSERT_NE(value, nullptr);
EXPECT_EQ(value->GetString(), "success");
FinishAsynchronousTest();
}
};
HEADLESS_DEVTOOLED_TEST_F(HeadlessWebContentsRequestStorageQuotaTest);
IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, BrowserTabChangeContent) {
EXPECT_TRUE(embedded_test_server()->Start());
HeadlessBrowserContext* browser_context =
browser()->CreateBrowserContextBuilder().Build();
HeadlessWebContents* web_contents =
browser_context->CreateWebContentsBuilder().Build();
EXPECT_TRUE(WaitForLoad(web_contents));
std::string script = "window.location = '" +
embedded_test_server()->GetURL("/hello.html").spec() +
"';";
EXPECT_THAT(EvaluateScript(web_contents, script),
Not(DictHasKey("exceptionDetails")));
// This will time out if the previous script did not work.
EXPECT_TRUE(WaitForLoad(web_contents));
}
IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, BrowserOpenInTab) {
EXPECT_TRUE(embedded_test_server()->Start());
HeadlessBrowserContext* browser_context =
browser()->CreateBrowserContextBuilder().Build();
HeadlessWebContents* web_contents =
browser_context->CreateWebContentsBuilder()
.SetInitialURL(embedded_test_server()->GetURL("/link.html"))
.Build();
EXPECT_TRUE(WaitForLoad(web_contents));
EXPECT_EQ(1u, browser_context->GetAllWebContents().size());
// Simulates a middle-button click on a link to ensure that the
// link is opened in a new tab by the browser and not by the renderer.
std::string script =
"var event = new MouseEvent('click', {'button': 1});"
"document.getElementsByTagName('a')[0].dispatchEvent(event);";
EXPECT_THAT(EvaluateScript(web_contents, script),
Not(DictHasKey("exceptionDetails")));
// Check that we have a new tab.
EXPECT_EQ(2u, browser_context->GetAllWebContents().size());
}
// BeginFrameControl is not supported on MacOS.
#if !BUILDFLAG(IS_MAC)
// TODO(kvitekp): Check to see if this could be trimmed down by using
// Pre/PostRunAsynchronousTest().
class HeadlessWebContentsBeginFrameControlTest : public HeadlessBrowserTest {
public:
HeadlessWebContentsBeginFrameControlTest() {}
void SetUp() override {
EnablePixelOutput();
HeadlessBrowserTest::SetUp();
}
protected:
virtual std::string GetTestHtmlFile() = 0;
virtual void StartFrames() {}
virtual void OnFrameFinished(base::Value::Dict result) {}
void SetUpCommandLine(base::CommandLine* command_line) override {
HeadlessBrowserTest::SetUpCommandLine(command_line);
// See bit.ly/headless-rendering for why we use these flags.
command_line->AppendSwitch(::switches::kRunAllCompositorStagesBeforeDraw);
command_line->AppendSwitch(::switches::kDisableNewContentRenderingTimeout);
command_line->AppendSwitch(switches::kDisableCheckerImaging);
command_line->AppendSwitch(switches::kDisableThreadedAnimation);
}
void RunTest() {
browser()->SetDefaultBrowserContext(
browser()->CreateBrowserContextBuilder().Build());
SimpleDevToolsProtocolClient browser_devtools_client;
browser_devtools_client.AttachToBrowser();
EXPECT_TRUE(embedded_test_server()->Start());
base::Value::Dict params;
params.Set("url", "about:blank");
params.Set("width", 200);
params.Set("height", 200);
params.Set("enableBeginFrameControl", true);
browser_devtools_client.SendCommand(
"Target.createTarget", std::move(params),
base::BindOnce(
&HeadlessWebContentsBeginFrameControlTest::OnTargetCreated,
base::Unretained(this)));
RunAsynchronousTest();
browser_devtools_client.DetachClient();
}
void OnTargetCreated(base::Value::Dict result) {
const std::string targetId = DictString(result, "result.targetId");
ASSERT_FALSE(targetId.empty());
web_contents_ = HeadlessWebContentsImpl::From(
content::DevToolsAgentHost::GetForId(targetId)->GetWebContents());
devtools_client_.AttachToWebContents(web_contents_->web_contents());
devtools_client_.AddEventHandler("Page.loadEventFired",
on_load_event_fired_handler_);
devtools_client_.SendCommand(
"Page.enable",
base::BindOnce(
&HeadlessWebContentsBeginFrameControlTest::OnPageDomainEnabled,
base::Unretained(this)));
}
void OnPageDomainEnabled(base::Value::Dict) {
devtools_client_.SendCommand(
"Page.navigate",
Param("url", embedded_test_server()->GetURL(GetTestHtmlFile()).spec()));
}
void OnLoadEventFired(const base::Value::Dict& params) {
TRACE_EVENT0("headless",
"HeadlessWebContentsBeginFrameControlTest::OnLoadEventFired");
devtools_client_.SendCommand("Page.disable");
devtools_client_.RemoveEventHandler("Page.loadEventFired",
on_load_event_fired_handler_);
StartFrames();
}
void BeginFrame(bool screenshot) {
num_begin_frames_++;
base::Value::Dict params;
if (screenshot)
params.Set("screenshot", base::Value::Dict());
devtools_client_.SendCommand(
"HeadlessExperimental.beginFrame", std::move(params),
base::BindOnce(&HeadlessWebContentsBeginFrameControlTest::FrameFinished,
base::Unretained(this)));
}
void FrameFinished(base::Value::Dict result) {
TRACE_EVENT2(
"headless", "HeadlessWebContentsBeginFrameControlTest::FrameFinished",
"has_damage", DictBool(result, "result.hasDamage"),
"has_screenshot_data", DictString(result, "result.screenshotData"));
OnFrameFinished(std::move(result));
}
void PostFinishAsynchronousTest() {
browser()->BrowserMainThread()->PostTask(
FROM_HERE,
base::BindOnce(
&HeadlessWebContentsBeginFrameControlTest::FinishAsynchronousTest,
base::Unretained(this)));
}
raw_ptr<HeadlessWebContentsImpl, AcrossTasksDanglingUntriaged> web_contents_ =
nullptr; // Not owned.
int num_begin_frames_ = 0;
SimpleDevToolsProtocolClient devtools_client_;
SimpleDevToolsProtocolClient::EventCallback on_load_event_fired_handler_ =
base::BindRepeating(
&HeadlessWebContentsBeginFrameControlTest::OnLoadEventFired,
base::Unretained(this));
};
class HeadlessWebContentsBeginFrameControlBasicTest
: public HeadlessWebContentsBeginFrameControlTest {
public:
HeadlessWebContentsBeginFrameControlBasicTest() = default;
protected:
std::string GetTestHtmlFile() override {
// Blue background.
return "/blue_page.html";
}
void StartFrames() override { BeginFrame(true); }
void OnFrameFinished(base::Value::Dict result) override {
if (num_begin_frames_ == 1) {
// First BeginFrame should have caused damage and have a screenshot.
EXPECT_TRUE(DictBool(result, "result.hasDamage"));
std::string png_data_base64 = DictString(result, "result.screenshotData");
ASSERT_FALSE(png_data_base64.empty());
std::optional<std::vector<uint8_t>> png_data =
base::Base64Decode(png_data_base64);
EXPECT_GT(png_data.value().size(), 0U);
SkBitmap result_bitmap = gfx::PNGCodec::Decode(png_data.value());
EXPECT_FALSE(result_bitmap.isNull());
EXPECT_EQ(200, result_bitmap.width());
EXPECT_EQ(200, result_bitmap.height());
SkColor expected_color = SkColorSetRGB(0x00, 0x00, 0xff);
SkColor actual_color = result_bitmap.getColor(100, 100);
EXPECT_EQ(expected_color, actual_color);
} else {
DCHECK_EQ(2, num_begin_frames_);
// Can't guarantee that the second BeginFrame didn't have damage, but it
// should not have a screenshot.
EXPECT_FALSE(result.FindStringByDottedPath("result.screenshotData"));
}
if (num_begin_frames_ < 2) {
// Don't capture a screenshot in the second BeginFrame.
BeginFrame(false);
} else {
// Post completion to avoid deleting the WebContents on the same callstack
// as frame finished callback.
PostFinishAsynchronousTest();
}
}
};
HEADLESS_DEVTOOLED_TEST_F(HeadlessWebContentsBeginFrameControlBasicTest);
class HeadlessWebContentsBeginFrameControlViewportTest
: public HeadlessWebContentsBeginFrameControlTest {
public:
HeadlessWebContentsBeginFrameControlViewportTest() = default;
protected:
std::string GetTestHtmlFile() override {
// Draws a 100x100px blue box at 200x200px.
return "/blue_box.html";
}
void StartFrames() override {
// Send a first BeginFrame to initialize the surface.
BeginFrame(false);
}
void SetUpViewport() {
base::Value::Dict params;
params.Set("width", 0);
params.Set("height", 0);
params.Set("deviceScaleFactor", 0);
params.Set("mobile", false);
params.SetByDottedPath("viewport.x", 200);
params.SetByDottedPath("viewport.y", 200);
params.SetByDottedPath("viewport.width", 100);
params.SetByDottedPath("viewport.height", 100);
params.SetByDottedPath("viewport.scale", 3);
devtools_client_.SendCommand(
"Emulation.setDeviceMetricsOverride", std::move(params),
base::BindOnce(&HeadlessWebContentsBeginFrameControlViewportTest::
OnSetDeviceMetricsOverrideDone,
base::Unretained(this)));
}
void OnSetDeviceMetricsOverrideDone(base::Value::Dict result) {
EXPECT_THAT(result, DictHasKey("result"));
// Take a screenshot in the second BeginFrame.
BeginFrame(true);
}
void OnFrameFinished(base::Value::Dict result) override {
if (num_begin_frames_ == 1) {
SetUpViewport();
return;
}
DCHECK_EQ(2, num_begin_frames_);
// Second BeginFrame should have a screenshot of the configured viewport and
// of the correct size.
EXPECT_TRUE(*result.FindBoolByDottedPath("result.hasDamage"));
std::string png_data_base64 = DictString(result, "result.screenshotData");
ASSERT_FALSE(png_data_base64.empty());
std::optional<std::vector<uint8_t>> png_data =
base::Base64Decode(png_data_base64);
ASSERT_GT(png_data.value().size(), 0ul);
SkBitmap result_bitmap = gfx::PNGCodec::Decode(png_data.value());
EXPECT_FALSE(result_bitmap.isNull());
// Expect a 300x300 bitmap that is all blue.
SkBitmap expected_bitmap;
SkImageInfo info;
expected_bitmap.allocPixels(
SkImageInfo::MakeN32(300, 300, kOpaque_SkAlphaType), /*rowBytes=*/0);
expected_bitmap.eraseColor(SkColorSetRGB(0x00, 0x00, 0xff));
EXPECT_TRUE(cc::MatchesBitmap(result_bitmap, expected_bitmap,
cc::ExactPixelComparator()));
// Post completion to avoid deleting the WebContents on the same callstack
// as frame finished callback.
PostFinishAsynchronousTest();
}
};
// TODO(crbug.com/40274291): Turning this off since it's flaking regularly.
DISABLED_HEADLESS_DEVTOOLED_TEST_F(
HeadlessWebContentsBeginFrameControlViewportTest);
#endif // !BUILDFLAG(IS_MAC)
class CookiesEnabled : public HeadlessDevTooledBrowserTest {
public:
void RunDevTooledTest() override {
EXPECT_TRUE(embedded_test_server()->Start());
devtools_client_.AddEventHandler(
"Page.loadEventFired",
base::BindRepeating(&CookiesEnabled::OnLoadEventFired,
base::Unretained(this)));
devtools_client_.SendCommand("Page.enable");
devtools_client_.SendCommand(
"Page.navigate",
Param("url", embedded_test_server()->GetURL("/cookie.html").spec()));
}
void OnLoadEventFired(const base::Value::Dict& params) {
devtools_client_.SendCommand(
"Runtime.evaluate", Param("expression", "window.test_result"),
base::BindOnce(&CookiesEnabled::OnEvaluateResult,
base::Unretained(this)));
}
void OnEvaluateResult(base::Value::Dict result) {
EXPECT_EQ(DictString(result, "result.result.value"), "0");
FinishAsynchronousTest();
}
};
HEADLESS_DEVTOOLED_TEST_F(CookiesEnabled);
// Regression test for crbug.com/1385982.
class BlockDevToolsEmbedding : public HeadlessDevTooledBrowserTest {
protected:
void SetUpCommandLine(base::CommandLine* command_line) override {
HeadlessDevTooledBrowserTest::SetUpCommandLine(command_line);
command_line->AppendSwitchASCII(switches::kRemoteDebuggingPort,
base::NumberToString(port_));
}
void RunDevTooledTest() override {
std::stringstream url;
url << "data:text/html,<iframe src='http://localhost:" << port_
<< "/json/version'></iframe>";
devtools_client_.AddEventHandler(
"Page.loadEventFired",
base::BindRepeating(&BlockDevToolsEmbedding::OnLoadEventFired,
base::Unretained(this)));
devtools_client_.SendCommand("Page.enable");
devtools_client_.SendCommand("Page.navigate", Param("url", url.str()));
}
void OnLoadEventFired(const base::Value::Dict& params) {
devtools_client_.SendCommand(
"Page.getFrameTree",
base::BindOnce(&BlockDevToolsEmbedding::OnFrameTreeResult,
base::Unretained(this)));
}
void OnFrameTreeResult(base::Value::Dict result) {
// Make sure the iframe did not load successfully.
const auto& child_frames = CHECK_DEREF(
result.FindListByDottedPath("result.frameTree.childFrames"));
EXPECT_EQ(DictString(child_frames[0].GetDict(), "frame.url"),
"chrome-error://chromewebdata/");
FinishAsynchronousTest();
}
bool ShouldEnableSitePerProcess() override {
// Headless browser tests by default run with OOPIF enabled. This results in
// the child frame to appear in a separate target. For simplicity we disable
// OOPIF for this test and expect child frame in the same target.
return false;
}
private:
int port_ = 10000 + (rand() % 60000);
};
HEADLESS_DEVTOOLED_TEST_F(BlockDevToolsEmbedding);
} // namespace headless