OLD | NEW |
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" | |
19 #include "headless/public/headless_devtools_client.h" | 18 #include "headless/public/headless_devtools_client.h" |
20 #include "headless/public/headless_devtools_target.h" | 19 #include "headless/public/headless_devtools_target.h" |
21 #include "headless/public/headless_web_contents.h" | 20 #include "headless/public/headless_web_contents.h" |
22 | 21 |
23 namespace headless { | 22 namespace headless { |
24 namespace { | 23 namespace { |
25 | 24 |
26 class WaitForLoadObserver : public page::Observer, public network::Observer { | 25 class WaitForLoadObserver : public page::Observer, public network::Observer { |
27 public: | 26 public: |
28 WaitForLoadObserver(HeadlessBrowserTest* browser_test, | 27 WaitForLoadObserver(HeadlessBrowserTest* browser_test, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 private: | 60 private: |
62 HeadlessBrowserTest* browser_test_; // Not owned. | 61 HeadlessBrowserTest* browser_test_; // Not owned. |
63 HeadlessWebContents* web_contents_; // Not owned. | 62 HeadlessWebContents* web_contents_; // Not owned. |
64 std::unique_ptr<HeadlessDevToolsClient> devtools_client_; | 63 std::unique_ptr<HeadlessDevToolsClient> devtools_client_; |
65 | 64 |
66 bool navigation_succeeded_; | 65 bool navigation_succeeded_; |
67 | 66 |
68 DISALLOW_COPY_AND_ASSIGN(WaitForLoadObserver); | 67 DISALLOW_COPY_AND_ASSIGN(WaitForLoadObserver); |
69 }; | 68 }; |
70 | 69 |
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 | |
108 } // namespace | 70 } // namespace |
109 | 71 |
110 HeadlessBrowserTest::HeadlessBrowserTest() { | 72 HeadlessBrowserTest::HeadlessBrowserTest() { |
111 base::FilePath headless_test_data(FILE_PATH_LITERAL("headless/test/data")); | 73 base::FilePath headless_test_data(FILE_PATH_LITERAL("headless/test/data")); |
112 CreateTestServer(headless_test_data); | 74 CreateTestServer(headless_test_data); |
113 } | 75 } |
114 | 76 |
115 HeadlessBrowserTest::~HeadlessBrowserTest() {} | 77 HeadlessBrowserTest::~HeadlessBrowserTest() {} |
116 | 78 |
117 void HeadlessBrowserTest::SetUpOnMainThread() {} | 79 void HeadlessBrowserTest::SetUpOnMainThread() {} |
(...skipping 27 matching lines...) Expand all Loading... |
145 HeadlessBrowser* HeadlessBrowserTest::browser() const { | 107 HeadlessBrowser* HeadlessBrowserTest::browser() const { |
146 return HeadlessContentMainDelegate::GetInstance()->browser(); | 108 return HeadlessContentMainDelegate::GetInstance()->browser(); |
147 } | 109 } |
148 | 110 |
149 bool HeadlessBrowserTest::WaitForLoad(HeadlessWebContents* web_contents) { | 111 bool HeadlessBrowserTest::WaitForLoad(HeadlessWebContents* web_contents) { |
150 WaitForLoadObserver observer(this, web_contents); | 112 WaitForLoadObserver observer(this, web_contents); |
151 RunAsynchronousTest(); | 113 RunAsynchronousTest(); |
152 return observer.navigation_succeeded(); | 114 return observer.navigation_succeeded(); |
153 } | 115 } |
154 | 116 |
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 | |
163 void HeadlessBrowserTest::RunAsynchronousTest() { | 117 void HeadlessBrowserTest::RunAsynchronousTest() { |
164 base::MessageLoop::ScopedNestableTaskAllower nestable_allower( | 118 base::MessageLoop::ScopedNestableTaskAllower nestable_allower( |
165 base::MessageLoop::current()); | 119 base::MessageLoop::current()); |
166 EXPECT_FALSE(run_loop_); | 120 EXPECT_FALSE(run_loop_); |
167 run_loop_ = base::WrapUnique(new base::RunLoop()); | 121 run_loop_ = base::WrapUnique(new base::RunLoop()); |
168 run_loop_->Run(); | 122 run_loop_->Run(); |
169 run_loop_ = nullptr; | 123 run_loop_ = nullptr; |
170 } | 124 } |
171 | 125 |
172 void HeadlessBrowserTest::FinishAsynchronousTest() { | 126 void HeadlessBrowserTest::FinishAsynchronousTest() { |
173 run_loop_->Quit(); | 127 run_loop_->Quit(); |
174 } | 128 } |
175 | 129 |
176 } // namespace headless | 130 } // namespace headless |
OLD | NEW |