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/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" |
9 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
10 #include "content/public/browser/render_process_host.h" | 11 #include "content/public/browser/render_process_host.h" |
11 #include "headless/lib/browser/headless_browser_impl.h" | 12 #include "headless/lib/browser/headless_browser_impl.h" |
12 #include "headless/lib/headless_content_main_delegate.h" | 13 #include "headless/lib/headless_content_main_delegate.h" |
| 14 #include "headless/public/headless_web_contents.h" |
13 | 15 |
14 namespace headless { | 16 namespace headless { |
| 17 namespace { |
| 18 |
| 19 class WaitForNavigationObserver : public HeadlessWebContents::Observer { |
| 20 public: |
| 21 WaitForNavigationObserver(base::RunLoop* run_loop, |
| 22 HeadlessWebContents* web_contents) |
| 23 : run_loop_(run_loop), |
| 24 web_contents_(web_contents), |
| 25 navigation_succeeded_(false) { |
| 26 web_contents_->AddObserver(this); |
| 27 } |
| 28 |
| 29 ~WaitForNavigationObserver() override { web_contents_->RemoveObserver(this); } |
| 30 |
| 31 void DocumentOnLoadCompletedInMainFrame() override { run_loop_->Quit(); } |
| 32 void DidFinishNavigation(bool success) override { |
| 33 navigation_succeeded_ = success; |
| 34 } |
| 35 |
| 36 bool navigation_succeeded() const { return navigation_succeeded_; } |
| 37 |
| 38 private: |
| 39 base::RunLoop* run_loop_; // Not owned. |
| 40 HeadlessWebContents* web_contents_; // Not owned. |
| 41 |
| 42 bool navigation_succeeded_; |
| 43 |
| 44 DISALLOW_COPY_AND_ASSIGN(WaitForNavigationObserver); |
| 45 }; |
| 46 |
| 47 } // namespace |
15 | 48 |
16 HeadlessBrowserTest::HeadlessBrowserTest() { | 49 HeadlessBrowserTest::HeadlessBrowserTest() { |
17 base::FilePath headless_test_data(FILE_PATH_LITERAL("headless/test/data")); | 50 base::FilePath headless_test_data(FILE_PATH_LITERAL("headless/test/data")); |
18 CreateTestServer(headless_test_data); | 51 CreateTestServer(headless_test_data); |
19 } | 52 } |
20 | 53 |
21 HeadlessBrowserTest::~HeadlessBrowserTest() {} | 54 HeadlessBrowserTest::~HeadlessBrowserTest() {} |
22 | 55 |
23 void HeadlessBrowserTest::SetUpOnMainThread() {} | 56 void HeadlessBrowserTest::SetUpOnMainThread() {} |
24 | 57 |
(...skipping 11 matching lines...) Expand all Loading... |
36 RunTestOnMainThread(); | 69 RunTestOnMainThread(); |
37 TearDownOnMainThread(); | 70 TearDownOnMainThread(); |
38 | 71 |
39 for (content::RenderProcessHost::iterator i( | 72 for (content::RenderProcessHost::iterator i( |
40 content::RenderProcessHost::AllHostsIterator()); | 73 content::RenderProcessHost::AllHostsIterator()); |
41 !i.IsAtEnd(); i.Advance()) { | 74 !i.IsAtEnd(); i.Advance()) { |
42 i.GetCurrentValue()->FastShutdownIfPossible(); | 75 i.GetCurrentValue()->FastShutdownIfPossible(); |
43 } | 76 } |
44 } | 77 } |
45 | 78 |
| 79 void HeadlessBrowserTest::SetBrowserOptions( |
| 80 const HeadlessBrowser::Options& options) { |
| 81 HeadlessContentMainDelegate::GetInstance()->browser()->SetOptionsForTesting( |
| 82 options); |
| 83 } |
| 84 |
46 HeadlessBrowser* HeadlessBrowserTest::browser() const { | 85 HeadlessBrowser* HeadlessBrowserTest::browser() const { |
47 return HeadlessContentMainDelegate::GetInstance()->browser(); | 86 return HeadlessContentMainDelegate::GetInstance()->browser(); |
48 } | 87 } |
49 | 88 |
| 89 bool HeadlessBrowserTest::NavigateAndWaitForLoad( |
| 90 HeadlessWebContents* web_contents, |
| 91 const GURL& url) { |
| 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(); |
| 101 } |
| 102 |
50 } // namespace headless | 103 } // namespace headless |
OLD | NEW |