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 |
19 IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, Navigation) { | 40 IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, Navigation) { |
20 EXPECT_TRUE(embedded_test_server()->Start()); | 41 EXPECT_TRUE(embedded_test_server()->Start()); |
21 std::unique_ptr<HeadlessWebContents> web_contents = | 42 std::unique_ptr<HeadlessWebContents> web_contents = |
22 browser()->CreateWebContents(gfx::Size(800, 600)); | 43 browser()->CreateWebContents( |
23 EXPECT_TRUE(NavigateAndWaitForLoad( | 44 embedded_test_server()->GetURL("/hello.html"), gfx::Size(800, 600)); |
24 web_contents.get(), embedded_test_server()->GetURL("/hello.html"))); | 45 NavigationObserver observer(this); |
25 } | 46 web_contents->AddObserver(&observer); |
26 | 47 |
27 IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, NavigationWithBadURL) { | 48 RunAsynchronousTest(); |
28 std::unique_ptr<HeadlessWebContents> web_contents = | 49 |
29 browser()->CreateWebContents(gfx::Size(800, 600)); | 50 EXPECT_TRUE(observer.navigation_succeeded()); |
30 GURL bad_url("not_valid"); | 51 web_contents->RemoveObserver(&observer); |
31 EXPECT_FALSE(web_contents->OpenURL(bad_url)); | |
32 } | 52 } |
33 | 53 |
34 } // namespace headless | 54 } // namespace headless |
OLD | NEW |