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

Side by Side Diff: headless/test/headless_browser_test.cc

Issue 2024973002: headless: Allow protocol handler customization (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review comments Created 4 years, 6 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 "headless/test/headless_browser_test.h" 5 #include "headless/test/headless_browser_test.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/render_process_host.h" 12 #include "content/public/browser/render_process_host.h"
13 #include "content/public/common/url_constants.h" 13 #include "content/public/common/url_constants.h"
14 #include "headless/lib/browser/headless_browser_impl.h" 14 #include "headless/lib/browser/headless_browser_impl.h"
15 #include "headless/lib/headless_content_main_delegate.h" 15 #include "headless/lib/headless_content_main_delegate.h"
16 #include "headless/public/domains/network.h" 16 #include "headless/public/domains/network.h"
17 #include "headless/public/domains/page.h" 17 #include "headless/public/domains/page.h"
18 #include "headless/public/domains/runtime.h"
18 #include "headless/public/headless_devtools_client.h" 19 #include "headless/public/headless_devtools_client.h"
19 #include "headless/public/headless_devtools_target.h" 20 #include "headless/public/headless_devtools_target.h"
20 #include "headless/public/headless_web_contents.h" 21 #include "headless/public/headless_web_contents.h"
21 22
22 namespace headless { 23 namespace headless {
23 namespace { 24 namespace {
24 25
25 class WaitForLoadObserver : public page::Observer, public network::Observer { 26 class WaitForLoadObserver : public page::Observer, public network::Observer {
26 public: 27 public:
27 WaitForLoadObserver(HeadlessBrowserTest* browser_test, 28 WaitForLoadObserver(HeadlessBrowserTest* browser_test,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 private: 61 private:
61 HeadlessBrowserTest* browser_test_; // Not owned. 62 HeadlessBrowserTest* browser_test_; // Not owned.
62 HeadlessWebContents* web_contents_; // Not owned. 63 HeadlessWebContents* web_contents_; // Not owned.
63 std::unique_ptr<HeadlessDevToolsClient> devtools_client_; 64 std::unique_ptr<HeadlessDevToolsClient> devtools_client_;
64 65
65 bool navigation_succeeded_; 66 bool navigation_succeeded_;
66 67
67 DISALLOW_COPY_AND_ASSIGN(WaitForLoadObserver); 68 DISALLOW_COPY_AND_ASSIGN(WaitForLoadObserver);
68 }; 69 };
69 70
71 class EvaluateHelper {
72 public:
73 EvaluateHelper(HeadlessBrowserTest* browser_test,
74 HeadlessWebContents* web_contents,
75 const std::string& script_to_eval)
76 : browser_test_(browser_test),
77 web_contents_(web_contents),
78 devtools_client_(HeadlessDevToolsClient::Create()) {
79 web_contents_->GetDevToolsTarget()->AttachClient(devtools_client_.get());
80 devtools_client_->GetRuntime()->Evaluate(
81 script_to_eval,
82 base::Bind(&EvaluateHelper::OnEvaluateResult, base::Unretained(this)));
83 }
84
85 ~EvaluateHelper() {
86 web_contents_->GetDevToolsTarget()->DetachClient(devtools_client_.get());
87 }
88
89 void OnEvaluateResult(std::unique_ptr<runtime::EvaluateResult> result) {
90 result_ = std::move(result);
91 browser_test_->FinishAsynchronousTest();
92 }
93
94 std::unique_ptr<runtime::EvaluateResult> TakeResult() {
95 return std::move(result_);
96 }
97
98 private:
99 HeadlessBrowserTest* browser_test_; // Not owned.
100 HeadlessWebContents* web_contents_; // Not owned.
101 std::unique_ptr<HeadlessDevToolsClient> devtools_client_;
102
103 std::unique_ptr<runtime::EvaluateResult> result_;
104
105 DISALLOW_COPY_AND_ASSIGN(EvaluateHelper);
106 };
107
70 } // namespace 108 } // namespace
71 109
72 HeadlessBrowserTest::HeadlessBrowserTest() { 110 HeadlessBrowserTest::HeadlessBrowserTest() {
73 base::FilePath headless_test_data(FILE_PATH_LITERAL("headless/test/data")); 111 base::FilePath headless_test_data(FILE_PATH_LITERAL("headless/test/data"));
74 CreateTestServer(headless_test_data); 112 CreateTestServer(headless_test_data);
75 } 113 }
76 114
77 HeadlessBrowserTest::~HeadlessBrowserTest() {} 115 HeadlessBrowserTest::~HeadlessBrowserTest() {}
78 116
79 void HeadlessBrowserTest::SetUpOnMainThread() {} 117 void HeadlessBrowserTest::SetUpOnMainThread() {}
(...skipping 27 matching lines...) Expand all
107 HeadlessBrowser* HeadlessBrowserTest::browser() const { 145 HeadlessBrowser* HeadlessBrowserTest::browser() const {
108 return HeadlessContentMainDelegate::GetInstance()->browser(); 146 return HeadlessContentMainDelegate::GetInstance()->browser();
109 } 147 }
110 148
111 bool HeadlessBrowserTest::WaitForLoad(HeadlessWebContents* web_contents) { 149 bool HeadlessBrowserTest::WaitForLoad(HeadlessWebContents* web_contents) {
112 WaitForLoadObserver observer(this, web_contents); 150 WaitForLoadObserver observer(this, web_contents);
113 RunAsynchronousTest(); 151 RunAsynchronousTest();
114 return observer.navigation_succeeded(); 152 return observer.navigation_succeeded();
115 } 153 }
116 154
155 std::unique_ptr<runtime::EvaluateResult> HeadlessBrowserTest::EvaluateScript(
156 HeadlessWebContents* web_contents,
157 const std::string& script) {
158 EvaluateHelper helper(this, web_contents, script);
159 RunAsynchronousTest();
160 return helper.TakeResult();
161 }
162
117 void HeadlessBrowserTest::RunAsynchronousTest() { 163 void HeadlessBrowserTest::RunAsynchronousTest() {
118 base::MessageLoop::ScopedNestableTaskAllower nestable_allower( 164 base::MessageLoop::ScopedNestableTaskAllower nestable_allower(
119 base::MessageLoop::current()); 165 base::MessageLoop::current());
120 EXPECT_FALSE(run_loop_); 166 EXPECT_FALSE(run_loop_);
121 run_loop_ = base::WrapUnique(new base::RunLoop()); 167 run_loop_ = base::WrapUnique(new base::RunLoop());
122 run_loop_->Run(); 168 run_loop_->Run();
123 run_loop_ = nullptr; 169 run_loop_ = nullptr;
124 } 170 }
125 171
126 void HeadlessBrowserTest::FinishAsynchronousTest() { 172 void HeadlessBrowserTest::FinishAsynchronousTest() {
127 run_loop_->Quit(); 173 run_loop_->Quit();
128 } 174 }
129 175
130 } // namespace headless 176 } // namespace headless
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698