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/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
11 #include "content/public/browser/render_process_host.h" | 12 #include "content/public/browser/render_process_host.h" |
12 #include "headless/lib/browser/headless_browser_impl.h" | 13 #include "headless/lib/browser/headless_browser_impl.h" |
13 #include "headless/lib/headless_content_main_delegate.h" | 14 #include "headless/lib/headless_content_main_delegate.h" |
14 #include "headless/public/headless_web_contents.h" | 15 #include "headless/public/headless_web_contents.h" |
15 | 16 |
16 namespace headless { | 17 namespace headless { |
17 namespace { | 18 namespace { |
18 | 19 |
19 class WaitForNavigationObserver : public HeadlessWebContents::Observer { | 20 class WaitForNavigationObserver : public HeadlessWebContents::Observer { |
20 public: | 21 public: |
21 WaitForNavigationObserver(base::RunLoop* run_loop, | 22 WaitForNavigationObserver(HeadlessBrowserTest* browser_test, |
22 HeadlessWebContents* web_contents) | 23 HeadlessWebContents* web_contents) |
23 : run_loop_(run_loop), | 24 : browser_test_(browser_test), |
24 web_contents_(web_contents), | 25 web_contents_(web_contents), |
25 navigation_succeeded_(false) { | 26 navigation_succeeded_(false) { |
26 web_contents_->AddObserver(this); | 27 web_contents_->AddObserver(this); |
27 } | 28 } |
28 | 29 |
29 ~WaitForNavigationObserver() override { web_contents_->RemoveObserver(this); } | 30 ~WaitForNavigationObserver() override { web_contents_->RemoveObserver(this); } |
30 | 31 |
31 void DocumentOnLoadCompletedInMainFrame() override { run_loop_->Quit(); } | 32 void DocumentOnLoadCompletedInMainFrame() override { |
| 33 browser_test_->FinishAsynchronousTest(); |
| 34 } |
32 void DidFinishNavigation(bool success) override { | 35 void DidFinishNavigation(bool success) override { |
33 navigation_succeeded_ = success; | 36 navigation_succeeded_ = success; |
34 } | 37 } |
35 | 38 |
36 bool navigation_succeeded() const { return navigation_succeeded_; } | 39 bool navigation_succeeded() const { return navigation_succeeded_; } |
37 | 40 |
38 private: | 41 private: |
39 base::RunLoop* run_loop_; // Not owned. | 42 HeadlessBrowserTest* browser_test_; // Not owned. |
40 HeadlessWebContents* web_contents_; // Not owned. | 43 HeadlessWebContents* web_contents_; // Not owned. |
41 | 44 |
42 bool navigation_succeeded_; | 45 bool navigation_succeeded_; |
43 | 46 |
44 DISALLOW_COPY_AND_ASSIGN(WaitForNavigationObserver); | 47 DISALLOW_COPY_AND_ASSIGN(WaitForNavigationObserver); |
45 }; | 48 }; |
46 | 49 |
47 } // namespace | 50 } // namespace |
48 | 51 |
49 HeadlessBrowserTest::HeadlessBrowserTest() { | 52 HeadlessBrowserTest::HeadlessBrowserTest() { |
(...skipping 29 matching lines...) Expand all Loading... |
79 void HeadlessBrowserTest::SetBrowserOptions( | 82 void HeadlessBrowserTest::SetBrowserOptions( |
80 const HeadlessBrowser::Options& options) { | 83 const HeadlessBrowser::Options& options) { |
81 HeadlessContentMainDelegate::GetInstance()->browser()->SetOptionsForTesting( | 84 HeadlessContentMainDelegate::GetInstance()->browser()->SetOptionsForTesting( |
82 options); | 85 options); |
83 } | 86 } |
84 | 87 |
85 HeadlessBrowser* HeadlessBrowserTest::browser() const { | 88 HeadlessBrowser* HeadlessBrowserTest::browser() const { |
86 return HeadlessContentMainDelegate::GetInstance()->browser(); | 89 return HeadlessContentMainDelegate::GetInstance()->browser(); |
87 } | 90 } |
88 | 91 |
89 bool HeadlessBrowserTest::NavigateAndWaitForLoad( | 92 bool HeadlessBrowserTest::WaitForLoad(HeadlessWebContents* web_contents) { |
90 HeadlessWebContents* web_contents, | 93 WaitForNavigationObserver observer(this, web_contents); |
91 const GURL& url) { | 94 RunAsynchronousTest(); |
92 base::RunLoop run_loop; | |
93 base::MessageLoop::ScopedNestableTaskAllower nestable_allower( | |
94 base::MessageLoop::current()); | |
95 WaitForNavigationObserver observer(&run_loop, web_contents); | |
96 | |
97 if (!web_contents->OpenURL(url)) | |
98 return false; | |
99 run_loop.Run(); | |
100 return observer.navigation_succeeded(); | 95 return observer.navigation_succeeded(); |
101 } | 96 } |
102 | 97 |
| 98 void HeadlessBrowserTest::RunAsynchronousTest() { |
| 99 base::MessageLoop::ScopedNestableTaskAllower nestable_allower( |
| 100 base::MessageLoop::current()); |
| 101 run_loop_ = base::WrapUnique(new base::RunLoop()); |
| 102 run_loop_->Run(); |
| 103 run_loop_ = nullptr; |
| 104 } |
| 105 |
| 106 void HeadlessBrowserTest::FinishAsynchronousTest() { |
| 107 run_loop_->Quit(); |
| 108 } |
| 109 |
103 } // namespace headless | 110 } // namespace headless |
OLD | NEW |