Index: headless/test/headless_browser_test.cc |
diff --git a/headless/test/headless_browser_test.cc b/headless/test/headless_browser_test.cc |
index 555616a8af7072dfb5795203b9a7766f92a81b48..61d4551e5944fdda4320caf7de632d00e4f92bf7 100644 |
--- a/headless/test/headless_browser_test.cc |
+++ b/headless/test/headless_browser_test.cc |
@@ -6,12 +6,45 @@ |
#include "base/files/file_path.h" |
#include "base/message_loop/message_loop.h" |
+#include "base/run_loop.h" |
#include "content/public/browser/browser_thread.h" |
#include "content/public/browser/render_process_host.h" |
#include "headless/lib/browser/headless_browser_impl.h" |
#include "headless/lib/headless_content_main_delegate.h" |
+#include "headless/public/headless_web_contents.h" |
namespace headless { |
+namespace { |
+ |
+class WaitForNavigationObserver : public HeadlessWebContents::Observer { |
+ public: |
+ WaitForNavigationObserver(base::RunLoop* run_loop, |
+ HeadlessWebContents* web_contents) |
+ : run_loop_(run_loop), |
+ web_contents_(web_contents), |
+ navigation_succeeded_(false) { |
+ web_contents_->AddObserver(this); |
+ } |
+ |
+ ~WaitForNavigationObserver() override { web_contents_->RemoveObserver(this); } |
+ |
+ void DocumentOnLoadCompletedInMainFrame() override { run_loop_->Quit(); } |
+ void DidFinishNavigation(bool success) override { |
+ navigation_succeeded_ = success; |
+ } |
+ |
+ bool navigation_succeeded() const { return navigation_succeeded_; } |
+ |
+ private: |
+ base::RunLoop* run_loop_; // Not owned. |
+ HeadlessWebContents* web_contents_; // Not owned. |
+ |
+ bool navigation_succeeded_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(WaitForNavigationObserver); |
+}; |
+ |
+} // namespace |
HeadlessBrowserTest::HeadlessBrowserTest() { |
base::FilePath headless_test_data(FILE_PATH_LITERAL("headless/test/data")); |
@@ -43,8 +76,28 @@ void HeadlessBrowserTest::RunTestOnMainThreadLoop() { |
} |
} |
+void HeadlessBrowserTest::SetBrowserOptions( |
+ const HeadlessBrowser::Options& options) { |
+ HeadlessContentMainDelegate::GetInstance()->browser()->SetOptionsForTesting( |
+ options); |
+} |
+ |
HeadlessBrowser* HeadlessBrowserTest::browser() const { |
return HeadlessContentMainDelegate::GetInstance()->browser(); |
} |
+bool HeadlessBrowserTest::NavigateAndWaitForLoad( |
+ HeadlessWebContents* web_contents, |
+ const GURL& url) { |
+ base::RunLoop run_loop; |
+ base::MessageLoop::ScopedNestableTaskAllower nestable_allower( |
+ base::MessageLoop::current()); |
+ WaitForNavigationObserver observer(&run_loop, web_contents); |
+ |
+ if (!web_contents->OpenURL(url)) |
+ return false; |
+ run_loop.Run(); |
+ return observer.navigation_succeeded(); |
+} |
+ |
} // namespace headless |