| 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 "headless/lib/browser/headless_browser_impl.h" | 14 #include "headless/lib/browser/headless_browser_impl.h" |
| 14 #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/headless_devtools_client.h" |
| 19 #include "headless/public/headless_devtools_target.h" |
| 15 #include "headless/public/headless_web_contents.h" | 20 #include "headless/public/headless_web_contents.h" |
| 16 | 21 |
| 17 namespace headless { | 22 namespace headless { |
| 18 namespace { | 23 namespace { |
| 19 | 24 |
| 20 class WaitForNavigationObserver : public HeadlessWebContents::Observer { | 25 class WaitForLoadObserver : public page::Observer, public network::Observer { |
| 21 public: | 26 public: |
| 22 WaitForNavigationObserver(HeadlessBrowserTest* browser_test, | 27 WaitForLoadObserver(HeadlessBrowserTest* browser_test, |
| 23 HeadlessWebContents* web_contents) | 28 HeadlessWebContents* web_contents) |
| 24 : browser_test_(browser_test), | 29 : browser_test_(browser_test), |
| 25 web_contents_(web_contents), | 30 web_contents_(web_contents), |
| 26 navigation_succeeded_(false) { | 31 devtools_client_(HeadlessDevToolsClient::Create()), |
| 27 web_contents_->AddObserver(this); | 32 navigation_succeeded_(true) { |
| 33 web_contents_->GetDevToolsTarget()->AttachClient(devtools_client_.get()); |
| 34 devtools_client_->GetNetwork()->AddObserver(this); |
| 35 devtools_client_->GetNetwork()->Enable(); |
| 36 devtools_client_->GetPage()->AddObserver(this); |
| 37 devtools_client_->GetPage()->Enable(); |
| 28 } | 38 } |
| 29 | 39 |
| 30 ~WaitForNavigationObserver() override { web_contents_->RemoveObserver(this); } | 40 ~WaitForLoadObserver() override { |
| 41 devtools_client_->GetNetwork()->RemoveObserver(this); |
| 42 devtools_client_->GetPage()->RemoveObserver(this); |
| 43 web_contents_->GetDevToolsTarget()->DetachClient(devtools_client_.get()); |
| 44 } |
| 31 | 45 |
| 32 void DocumentOnLoadCompletedInMainFrame() override { | 46 void OnLoadEventFired(const page::LoadEventFiredParams& params) override { |
| 33 browser_test_->FinishAsynchronousTest(); | 47 browser_test_->FinishAsynchronousTest(); |
| 34 } | 48 } |
| 35 void DidFinishNavigation(bool success) override { | 49 |
| 36 navigation_succeeded_ = success; | 50 void OnResponseReceived( |
| 51 const network::ResponseReceivedParams& params) override { |
| 52 if (params.GetResponse()->GetStatus() != 200 || |
| 53 params.GetResponse()->GetUrl() == content::kUnreachableWebDataURL) { |
| 54 navigation_succeeded_ = false; |
| 55 } |
| 37 } | 56 } |
| 38 | 57 |
| 39 bool navigation_succeeded() const { return navigation_succeeded_; } | 58 bool navigation_succeeded() const { return navigation_succeeded_; } |
| 40 | 59 |
| 41 private: | 60 private: |
| 42 HeadlessBrowserTest* browser_test_; // Not owned. | 61 HeadlessBrowserTest* browser_test_; // Not owned. |
| 43 HeadlessWebContents* web_contents_; // Not owned. | 62 HeadlessWebContents* web_contents_; // Not owned. |
| 63 std::unique_ptr<HeadlessDevToolsClient> devtools_client_; |
| 44 | 64 |
| 45 bool navigation_succeeded_; | 65 bool navigation_succeeded_; |
| 46 | 66 |
| 47 DISALLOW_COPY_AND_ASSIGN(WaitForNavigationObserver); | 67 DISALLOW_COPY_AND_ASSIGN(WaitForLoadObserver); |
| 48 }; | 68 }; |
| 49 | 69 |
| 50 } // namespace | 70 } // namespace |
| 51 | 71 |
| 52 HeadlessBrowserTest::HeadlessBrowserTest() { | 72 HeadlessBrowserTest::HeadlessBrowserTest() { |
| 53 base::FilePath headless_test_data(FILE_PATH_LITERAL("headless/test/data")); | 73 base::FilePath headless_test_data(FILE_PATH_LITERAL("headless/test/data")); |
| 54 CreateTestServer(headless_test_data); | 74 CreateTestServer(headless_test_data); |
| 55 } | 75 } |
| 56 | 76 |
| 57 HeadlessBrowserTest::~HeadlessBrowserTest() {} | 77 HeadlessBrowserTest::~HeadlessBrowserTest() {} |
| (...skipping 25 matching lines...) Expand all Loading... |
| 83 const HeadlessBrowser::Options& options) { | 103 const HeadlessBrowser::Options& options) { |
| 84 HeadlessContentMainDelegate::GetInstance()->browser()->SetOptionsForTesting( | 104 HeadlessContentMainDelegate::GetInstance()->browser()->SetOptionsForTesting( |
| 85 options); | 105 options); |
| 86 } | 106 } |
| 87 | 107 |
| 88 HeadlessBrowser* HeadlessBrowserTest::browser() const { | 108 HeadlessBrowser* HeadlessBrowserTest::browser() const { |
| 89 return HeadlessContentMainDelegate::GetInstance()->browser(); | 109 return HeadlessContentMainDelegate::GetInstance()->browser(); |
| 90 } | 110 } |
| 91 | 111 |
| 92 bool HeadlessBrowserTest::WaitForLoad(HeadlessWebContents* web_contents) { | 112 bool HeadlessBrowserTest::WaitForLoad(HeadlessWebContents* web_contents) { |
| 93 WaitForNavigationObserver observer(this, web_contents); | 113 WaitForLoadObserver observer(this, web_contents); |
| 94 RunAsynchronousTest(); | 114 RunAsynchronousTest(); |
| 95 return observer.navigation_succeeded(); | 115 return observer.navigation_succeeded(); |
| 96 } | 116 } |
| 97 | 117 |
| 98 void HeadlessBrowserTest::RunAsynchronousTest() { | 118 void HeadlessBrowserTest::RunAsynchronousTest() { |
| 99 base::MessageLoop::ScopedNestableTaskAllower nestable_allower( | 119 base::MessageLoop::ScopedNestableTaskAllower nestable_allower( |
| 100 base::MessageLoop::current()); | 120 base::MessageLoop::current()); |
| 101 EXPECT_FALSE(run_loop_); | 121 EXPECT_FALSE(run_loop_); |
| 102 run_loop_ = base::WrapUnique(new base::RunLoop()); | 122 run_loop_ = base::WrapUnique(new base::RunLoop()); |
| 103 run_loop_->Run(); | 123 run_loop_->Run(); |
| 104 run_loop_ = nullptr; | 124 run_loop_ = nullptr; |
| 105 } | 125 } |
| 106 | 126 |
| 107 void HeadlessBrowserTest::FinishAsynchronousTest() { | 127 void HeadlessBrowserTest::FinishAsynchronousTest() { |
| 108 run_loop_->Quit(); | 128 run_loop_->Quit(); |
| 109 } | 129 } |
| 110 | 130 |
| 111 } // namespace headless | 131 } // namespace headless |
| OLD | NEW |