| 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 <memory> | 5 #include <memory> |
| 6 | 6 |
| 7 #include "content/public/test/browser_test.h" | 7 #include "content/public/test/browser_test.h" |
| 8 #include "headless/public/headless_browser.h" | 8 #include "headless/public/headless_browser.h" |
| 9 #include "headless/public/headless_web_contents.h" | 9 #include "headless/public/headless_web_contents.h" |
| 10 #include "headless/test/headless_browser_test.h" | 10 #include "headless/test/headless_browser_test.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "ui/gfx/geometry/size.h" | 12 #include "ui/gfx/geometry/size.h" |
| 13 #include "url/gurl.h" | 13 #include "url/gurl.h" |
| 14 | 14 |
| 15 namespace headless { | 15 namespace headless { |
| 16 | 16 |
| 17 class HeadlessWebContentsTest : public HeadlessBrowserTest {}; | 17 class HeadlessWebContentsTest : public HeadlessBrowserTest {}; |
| 18 | 18 |
| 19 class NavigationObserver : public HeadlessWebContents::Observer { | |
| 20 public: | |
| 21 NavigationObserver(HeadlessWebContentsTest* browser_test) | |
| 22 : browser_test_(browser_test), navigation_succeeded_(false) {} | |
| 23 ~NavigationObserver() override {} | |
| 24 | |
| 25 void DocumentOnLoadCompletedInMainFrame() override { | |
| 26 browser_test_->FinishAsynchronousTest(); | |
| 27 } | |
| 28 | |
| 29 void DidFinishNavigation(bool success) override { | |
| 30 navigation_succeeded_ = success; | |
| 31 } | |
| 32 | |
| 33 bool navigation_succeeded() const { return navigation_succeeded_; } | |
| 34 | |
| 35 private: | |
| 36 HeadlessWebContentsTest* browser_test_; // Not owned. | |
| 37 bool navigation_succeeded_; | |
| 38 }; | |
| 39 | |
| 40 IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, Navigation) { | 19 IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, Navigation) { |
| 41 EXPECT_TRUE(embedded_test_server()->Start()); | 20 EXPECT_TRUE(embedded_test_server()->Start()); |
| 42 HeadlessWebContents* web_contents = browser()->CreateWebContents( | 21 HeadlessWebContents* web_contents = browser()->CreateWebContents( |
| 43 embedded_test_server()->GetURL("/hello.html"), gfx::Size(800, 600)); | 22 embedded_test_server()->GetURL("/hello.html"), gfx::Size(800, 600)); |
| 44 NavigationObserver observer(this); | 23 EXPECT_TRUE(WaitForLoad(web_contents)); |
| 45 web_contents->AddObserver(&observer); | |
| 46 | |
| 47 RunAsynchronousTest(); | |
| 48 | |
| 49 EXPECT_TRUE(observer.navigation_succeeded()); | |
| 50 web_contents->RemoveObserver(&observer); | |
| 51 | 24 |
| 52 std::vector<HeadlessWebContents*> all_web_contents = | 25 std::vector<HeadlessWebContents*> all_web_contents = |
| 53 browser()->GetAllWebContents(); | 26 browser()->GetAllWebContents(); |
| 54 | 27 |
| 55 EXPECT_EQ(static_cast<size_t>(1), all_web_contents.size()); | 28 EXPECT_EQ(static_cast<size_t>(1), all_web_contents.size()); |
| 56 EXPECT_EQ(web_contents, all_web_contents[0]); | 29 EXPECT_EQ(web_contents, all_web_contents[0]); |
| 57 } | 30 } |
| 58 | 31 |
| 59 IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, WindowOpen) { | 32 IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, WindowOpen) { |
| 60 EXPECT_TRUE(embedded_test_server()->Start()); | 33 EXPECT_TRUE(embedded_test_server()->Start()); |
| 61 | 34 |
| 62 HeadlessWebContents* web_contents = browser()->CreateWebContents( | 35 HeadlessWebContents* web_contents = browser()->CreateWebContents( |
| 63 embedded_test_server()->GetURL("/window_open.html"), gfx::Size(800, 600)); | 36 embedded_test_server()->GetURL("/window_open.html"), gfx::Size(800, 600)); |
| 64 NavigationObserver observer(this); | 37 EXPECT_TRUE(WaitForLoad(web_contents)); |
| 65 web_contents->AddObserver(&observer); | |
| 66 | |
| 67 RunAsynchronousTest(); | |
| 68 | |
| 69 EXPECT_TRUE(observer.navigation_succeeded()); | |
| 70 web_contents->RemoveObserver(&observer); | |
| 71 | 38 |
| 72 std::vector<HeadlessWebContents*> all_web_contents = | 39 std::vector<HeadlessWebContents*> all_web_contents = |
| 73 browser()->GetAllWebContents(); | 40 browser()->GetAllWebContents(); |
| 74 | 41 |
| 75 EXPECT_EQ(static_cast<size_t>(2), all_web_contents.size()); | 42 EXPECT_EQ(static_cast<size_t>(2), all_web_contents.size()); |
| 76 } | 43 } |
| 77 | 44 |
| 78 } // namespace headless | 45 } // namespace headless |
| OLD | NEW |