1 // Copyright 2018 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
7 #include "base/bind.h"
8 #include "build/build_config.h"
9 #include "content/public/test/browser_test.h"
10 #include "headless/public/devtools/domains/runtime.h"
11 #include "headless/public/devtools/domains/target.h"
12 #include "headless/public/headless_devtools_client.h"
13 #include "headless/test/headless_browser_test.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace headless {
17
18 class HeadlessClientBrowserTest : public HeadlessAsyncDevTooledBrowserTest,
19 public target::ExperimentalObserver {
20 public:
21 HeadlessClientBrowserTest() = default;
22
23 private:
24 // HeadlessWebContentsObserver implementation.
RunDevTooledTest()25 void RunDevTooledTest() override {
26 browser_devtools_client_->GetTarget()->GetExperimental()->AddObserver(this);
27 browser_devtools_client_->GetTarget()->CreateTarget(
28 target::CreateTargetParams::Builder().SetUrl("about:blank").Build(),
29 base::BindOnce(&HeadlessClientBrowserTest::AttachToTarget,
30 base::Unretained(this)));
31 }
32
AttachToTarget(std::unique_ptr<target::CreateTargetResult> result)33 void AttachToTarget(std::unique_ptr<target::CreateTargetResult> result) {
34 browser_devtools_client_->GetTarget()->AttachToTarget(
35 target::AttachToTargetParams::Builder()
36 .SetTargetId(result->GetTargetId())
37 .SetFlatten(true)
38 .Build(),
39 base::BindOnce(&HeadlessClientBrowserTest::CreateSession,
40 base::Unretained(this)));
41 }
42
CreateSession(std::unique_ptr<target::AttachToTargetResult> result)43 void CreateSession(std::unique_ptr<target::AttachToTargetResult> result) {
44 session_client_ =
45 browser_devtools_client_->CreateSession(result->GetSessionId());
46 session_client_->GetRuntime()->Evaluate(
47 "window.location.href",
48 base::BindOnce(&HeadlessClientBrowserTest::FinishTest,
49 base::Unretained(this)));
50 }
51
FinishTest(std::unique_ptr<runtime::EvaluateResult> result)52 void FinishTest(std::unique_ptr<runtime::EvaluateResult> result) {
53 const base::Value* value = result->GetResult()->GetValue();
54 std::string str;
55 EXPECT_TRUE(value->GetAsString(&str));
56 EXPECT_EQ("about:blank", str);
57 session_client_.reset();
58 FinishAsynchronousTest();
59 }
60
61 private:
62 std::unique_ptr<HeadlessDevToolsClient> session_client_;
63 };
64
IN_PROC_BROWSER_TEST_F(HeadlessClientBrowserTest,FlatProtocolAccess)65 IN_PROC_BROWSER_TEST_F(HeadlessClientBrowserTest, FlatProtocolAccess) {
66 RunTest();
67 }
68
69 } // namespace headless
70