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

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

Issue 2043603004: headless: Introduce a browser context (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address nits 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
« no previous file with comments | « headless/test/headless_browser_test.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
17 #include "headless/public/domains/page.h"
18 #include "headless/public/domains/runtime.h" 16 #include "headless/public/domains/runtime.h"
19 #include "headless/public/headless_devtools_client.h" 17 #include "headless/public/headless_devtools_client.h"
20 #include "headless/public/headless_devtools_target.h" 18 #include "headless/public/headless_devtools_target.h"
21 #include "headless/public/headless_web_contents.h" 19 #include "headless/public/headless_web_contents.h"
22 #include "ui/gfx/geometry/size.h" 20 #include "ui/gfx/geometry/size.h"
23 #include "url/gurl.h" 21 #include "url/gurl.h"
24 22
25 namespace headless { 23 namespace headless {
26 namespace { 24 namespace {
27 25
28 class WaitForLoadObserver : public page::Observer, public network::Observer { 26 class SynchronousLoadObserver {
29 public: 27 public:
30 WaitForLoadObserver(HeadlessBrowserTest* browser_test, 28 SynchronousLoadObserver(HeadlessBrowserTest* browser_test,
31 HeadlessWebContents* web_contents) 29 HeadlessWebContents* web_contents)
32 : browser_test_(browser_test), 30 : web_contents_(web_contents),
33 web_contents_(web_contents), 31 devtools_client_(HeadlessDevToolsClient::Create()) {
34 devtools_client_(HeadlessDevToolsClient::Create()),
35 navigation_succeeded_(true) {
36 web_contents_->GetDevToolsTarget()->AttachClient(devtools_client_.get()); 32 web_contents_->GetDevToolsTarget()->AttachClient(devtools_client_.get());
37 devtools_client_->GetNetwork()->AddObserver(this); 33 load_observer_.reset(new LoadObserver(
38 devtools_client_->GetNetwork()->Enable(); 34 devtools_client_.get(),
39 devtools_client_->GetPage()->AddObserver(this); 35 base::Bind(&HeadlessBrowserTest::FinishAsynchronousTest,
40 devtools_client_->GetPage()->Enable(); 36 base::Unretained(browser_test))));
41 } 37 }
42 38
43 ~WaitForLoadObserver() override { 39 ~SynchronousLoadObserver() {
44 devtools_client_->GetNetwork()->RemoveObserver(this);
45 devtools_client_->GetPage()->RemoveObserver(this);
46 web_contents_->GetDevToolsTarget()->DetachClient(devtools_client_.get()); 40 web_contents_->GetDevToolsTarget()->DetachClient(devtools_client_.get());
47 } 41 }
48 42
49 void OnLoadEventFired(const page::LoadEventFiredParams& params) override { 43 bool navigation_succeeded() const {
50 browser_test_->FinishAsynchronousTest(); 44 return load_observer_->navigation_succeeded();
51 } 45 }
52 46
53 void OnResponseReceived(
54 const network::ResponseReceivedParams& params) override {
55 if (params.GetResponse()->GetStatus() != 200 ||
56 params.GetResponse()->GetUrl() == content::kUnreachableWebDataURL) {
57 navigation_succeeded_ = false;
58 }
59 }
60
61 bool navigation_succeeded() const { return navigation_succeeded_; }
62
63 private: 47 private:
64 HeadlessBrowserTest* browser_test_; // Not owned.
65 HeadlessWebContents* web_contents_; // Not owned. 48 HeadlessWebContents* web_contents_; // Not owned.
66 std::unique_ptr<HeadlessDevToolsClient> devtools_client_; 49 std::unique_ptr<HeadlessDevToolsClient> devtools_client_;
67 50 std::unique_ptr<LoadObserver> load_observer_;
68 bool navigation_succeeded_;
69
70 DISALLOW_COPY_AND_ASSIGN(WaitForLoadObserver);
71 }; 51 };
72 52
73 class EvaluateHelper { 53 class EvaluateHelper {
74 public: 54 public:
75 EvaluateHelper(HeadlessBrowserTest* browser_test, 55 EvaluateHelper(HeadlessBrowserTest* browser_test,
76 HeadlessWebContents* web_contents, 56 HeadlessWebContents* web_contents,
77 const std::string& script_to_eval) 57 const std::string& script_to_eval)
78 : browser_test_(browser_test), 58 : browser_test_(browser_test),
79 web_contents_(web_contents), 59 web_contents_(web_contents),
80 devtools_client_(HeadlessDevToolsClient::Create()) { 60 devtools_client_(HeadlessDevToolsClient::Create()) {
(...skipping 21 matching lines...) Expand all
102 HeadlessWebContents* web_contents_; // Not owned. 82 HeadlessWebContents* web_contents_; // Not owned.
103 std::unique_ptr<HeadlessDevToolsClient> devtools_client_; 83 std::unique_ptr<HeadlessDevToolsClient> devtools_client_;
104 84
105 std::unique_ptr<runtime::EvaluateResult> result_; 85 std::unique_ptr<runtime::EvaluateResult> result_;
106 86
107 DISALLOW_COPY_AND_ASSIGN(EvaluateHelper); 87 DISALLOW_COPY_AND_ASSIGN(EvaluateHelper);
108 }; 88 };
109 89
110 } // namespace 90 } // namespace
111 91
92 LoadObserver::LoadObserver(HeadlessDevToolsClient* devtools_client,
93 base::Closure callback)
94 : callback_(std::move(callback)),
95 devtools_client_(devtools_client),
96 navigation_succeeded_(true) {
97 devtools_client_->GetNetwork()->AddObserver(this);
98 devtools_client_->GetNetwork()->Enable();
99 devtools_client_->GetPage()->AddObserver(this);
100 devtools_client_->GetPage()->Enable();
101 }
102
103 LoadObserver::~LoadObserver() {
104 devtools_client_->GetNetwork()->RemoveObserver(this);
105 devtools_client_->GetPage()->RemoveObserver(this);
106 }
107
108 void LoadObserver::OnLoadEventFired(const page::LoadEventFiredParams& params) {
109 callback_.Run();
110 }
111
112 void LoadObserver::OnResponseReceived(
113 const network::ResponseReceivedParams& params) {
114 if (params.GetResponse()->GetStatus() != 200 ||
115 params.GetResponse()->GetUrl() == content::kUnreachableWebDataURL) {
116 navigation_succeeded_ = false;
117 }
118 }
119
112 HeadlessBrowserTest::HeadlessBrowserTest() { 120 HeadlessBrowserTest::HeadlessBrowserTest() {
113 base::FilePath headless_test_data(FILE_PATH_LITERAL("headless/test/data")); 121 base::FilePath headless_test_data(FILE_PATH_LITERAL("headless/test/data"));
114 CreateTestServer(headless_test_data); 122 CreateTestServer(headless_test_data);
115 } 123 }
116 124
117 HeadlessBrowserTest::~HeadlessBrowserTest() {} 125 HeadlessBrowserTest::~HeadlessBrowserTest() {}
118 126
119 void HeadlessBrowserTest::SetUpOnMainThread() {} 127 void HeadlessBrowserTest::SetUpOnMainThread() {}
120 128
121 void HeadlessBrowserTest::TearDownOnMainThread() { 129 void HeadlessBrowserTest::TearDownOnMainThread() {
(...skipping 20 matching lines...) Expand all
142 void HeadlessBrowserTest::SetBrowserOptions(HeadlessBrowser::Options options) { 150 void HeadlessBrowserTest::SetBrowserOptions(HeadlessBrowser::Options options) {
143 HeadlessContentMainDelegate::GetInstance()->browser()->SetOptionsForTesting( 151 HeadlessContentMainDelegate::GetInstance()->browser()->SetOptionsForTesting(
144 std::move(options)); 152 std::move(options));
145 } 153 }
146 154
147 HeadlessBrowser* HeadlessBrowserTest::browser() const { 155 HeadlessBrowser* HeadlessBrowserTest::browser() const {
148 return HeadlessContentMainDelegate::GetInstance()->browser(); 156 return HeadlessContentMainDelegate::GetInstance()->browser();
149 } 157 }
150 158
151 bool HeadlessBrowserTest::WaitForLoad(HeadlessWebContents* web_contents) { 159 bool HeadlessBrowserTest::WaitForLoad(HeadlessWebContents* web_contents) {
152 WaitForLoadObserver observer(this, web_contents); 160 SynchronousLoadObserver load_observer(this, web_contents);
153 RunAsynchronousTest(); 161 RunAsynchronousTest();
154 return observer.navigation_succeeded(); 162 return load_observer.navigation_succeeded();
155 } 163 }
156 164
157 std::unique_ptr<runtime::EvaluateResult> HeadlessBrowserTest::EvaluateScript( 165 std::unique_ptr<runtime::EvaluateResult> HeadlessBrowserTest::EvaluateScript(
158 HeadlessWebContents* web_contents, 166 HeadlessWebContents* web_contents,
159 const std::string& script) { 167 const std::string& script) {
160 EvaluateHelper helper(this, web_contents, script); 168 EvaluateHelper helper(this, web_contents, script);
161 RunAsynchronousTest(); 169 RunAsynchronousTest();
162 return helper.TakeResult(); 170 return helper.TakeResult();
163 } 171 }
164 172
(...skipping 16 matching lines...) Expand all
181 189
182 HeadlessAsyncDevTooledBrowserTest::~HeadlessAsyncDevTooledBrowserTest() {} 190 HeadlessAsyncDevTooledBrowserTest::~HeadlessAsyncDevTooledBrowserTest() {}
183 191
184 void HeadlessAsyncDevTooledBrowserTest::DevToolsTargetReady() { 192 void HeadlessAsyncDevTooledBrowserTest::DevToolsTargetReady() {
185 EXPECT_TRUE(web_contents_->GetDevToolsTarget()); 193 EXPECT_TRUE(web_contents_->GetDevToolsTarget());
186 web_contents_->GetDevToolsTarget()->AttachClient(devtools_client_.get()); 194 web_contents_->GetDevToolsTarget()->AttachClient(devtools_client_.get());
187 RunDevTooledTest(); 195 RunDevTooledTest();
188 } 196 }
189 197
190 void HeadlessAsyncDevTooledBrowserTest::RunTest() { 198 void HeadlessAsyncDevTooledBrowserTest::RunTest() {
191 web_contents_ = 199 web_contents_ = browser()->CreateWebContentsBuilder().Build();
192 browser()->CreateWebContents(GURL("about:blank"), gfx::Size(800, 600));
193 web_contents_->AddObserver(this); 200 web_contents_->AddObserver(this);
194 201
195 RunAsynchronousTest(); 202 RunAsynchronousTest();
196 203
197 web_contents_->GetDevToolsTarget()->DetachClient(devtools_client_.get()); 204 web_contents_->GetDevToolsTarget()->DetachClient(devtools_client_.get());
198 web_contents_->RemoveObserver(this); 205 web_contents_->RemoveObserver(this);
199 web_contents_->Close(); 206 web_contents_->Close();
200 web_contents_ = nullptr; 207 web_contents_ = nullptr;
201 } 208 }
202 209
203 } // namespace headless 210 } // namespace headless
OLDNEW
« no previous file with comments | « headless/test/headless_browser_test.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698