OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/web_view/public/cpp/web_view.h" | 5 #include "components/web_view/public/cpp/web_view.h" |
6 | 6 |
7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "components/mus/public/cpp/scoped_view_ptr.h" | 13 #include "components/mus/public/cpp/scoped_view_ptr.h" |
14 #include "components/mus/public/cpp/tests/view_manager_test_base.h" | 14 #include "components/mus/public/cpp/tests/view_manager_test_base.h" |
15 #include "components/mus/public/cpp/view.h" | 15 #include "components/mus/public/cpp/view.h" |
16 #include "components/mus/public/cpp/view_tree_connection.h" | 16 #include "components/mus/public/cpp/view_tree_connection.h" |
17 #include "mojo/util/filename_util.h" | 17 #include "mojo/util/filename_util.h" |
18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
19 | 19 |
20 namespace web_view { | 20 namespace web_view { |
21 | 21 |
| 22 namespace { |
| 23 const char kTestOneFile[] = "test_one.html"; |
| 24 const char kTestOneTitle[] = "Test Title One"; |
| 25 const char kTestTwoFile[] = "test_two.html"; |
| 26 const char kTestTwoTitle[] = "Test Title Two"; |
| 27 const char kTestThreeFile[] = "test_three.html"; |
| 28 const char kTestThreeTitle[] = "Test Title Three"; |
| 29 } |
| 30 |
22 class WebViewTest : public mojo::ViewManagerTestBase, | 31 class WebViewTest : public mojo::ViewManagerTestBase, |
23 public mojom::WebViewClient { | 32 public mojom::WebViewClient { |
24 public: | 33 public: |
25 WebViewTest() : web_view_(this) {} | 34 WebViewTest() : web_view_(this) {} |
26 ~WebViewTest() override {} | 35 ~WebViewTest() override {} |
27 | 36 |
28 mojom::WebView* web_view() { return web_view_.web_view(); } | 37 mojom::WebView* web_view() { return web_view_.web_view(); } |
29 | 38 |
30 const std::string& last_title() { return last_title_; } | 39 const std::string& last_title() { return last_title_; } |
| 40 mojom::ButtonState last_back_button_state() { |
| 41 return last_back_button_state_; |
| 42 } |
| 43 mojom::ButtonState last_forward_button_state() { |
| 44 return last_forward_button_state_; |
| 45 } |
31 | 46 |
32 void StartNestedRunLoopUntilLoadingDone() { | 47 void StartNestedRunLoopUntilLoadingDone() { |
33 run_loop_.reset(new base::RunLoop); | 48 run_loop_.reset(new base::RunLoop); |
34 run_loop_->Run(); | 49 run_loop_->Run(); |
35 } | 50 } |
36 | 51 |
| 52 void NavigateTo(const std::string& file) { |
| 53 base::FilePath data_file; |
| 54 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_file)); |
| 55 data_file = data_file.AppendASCII("components/test/data/web_view") |
| 56 .AppendASCII(file).NormalizePathSeparators(); |
| 57 ASSERT_TRUE(base::PathExists(data_file)); |
| 58 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 59 request->url = mojo::util::FilePathToFileURL(data_file).spec(); |
| 60 web_view()->LoadRequest(request.Pass()); |
| 61 StartNestedRunLoopUntilLoadingDone(); |
| 62 } |
| 63 |
37 private: | 64 private: |
38 void QuitNestedRunLoop() { | 65 void QuitNestedRunLoop() { |
39 if (run_loop_) { | 66 if (run_loop_) { |
40 run_loop_->Quit(); | 67 run_loop_->Quit(); |
41 } | 68 } |
42 } | 69 } |
43 | 70 |
44 // Overridden from ApplicationDelegate: | 71 // Overridden from ApplicationDelegate: |
45 void Initialize(mojo::ApplicationImpl* app) override { | 72 void Initialize(mojo::ApplicationImpl* app) override { |
46 ViewManagerTestBase::Initialize(app); | 73 ViewManagerTestBase::Initialize(app); |
(...skipping 16 matching lines...) Expand all Loading... |
63 ViewManagerTestBase::TearDown(); | 90 ViewManagerTestBase::TearDown(); |
64 } | 91 } |
65 | 92 |
66 // Overridden from web_view::mojom::WebViewClient: | 93 // Overridden from web_view::mojom::WebViewClient: |
67 void TopLevelNavigate(mojo::URLRequestPtr request) override {} | 94 void TopLevelNavigate(mojo::URLRequestPtr request) override {} |
68 void LoadingStateChanged(bool is_loading) override { | 95 void LoadingStateChanged(bool is_loading) override { |
69 if (is_loading == false) | 96 if (is_loading == false) |
70 QuitNestedRunLoop(); | 97 QuitNestedRunLoop(); |
71 } | 98 } |
72 void ProgressChanged(double progress) override {} | 99 void ProgressChanged(double progress) override {} |
| 100 void BackForwardChanged(mojom::ButtonState back_button, |
| 101 mojom::ButtonState forward_button) override { |
| 102 last_back_button_state_ = back_button; |
| 103 last_forward_button_state_ = forward_button; |
| 104 } |
73 void TitleChanged(const mojo::String& title) override { | 105 void TitleChanged(const mojo::String& title) override { |
74 last_title_ = title.get(); | 106 last_title_ = title.get(); |
75 } | 107 } |
76 | 108 |
77 mojo::ApplicationImpl* app_; | 109 mojo::ApplicationImpl* app_; |
78 | 110 |
79 mojo::View* content_; | 111 mojo::View* content_; |
80 | 112 |
81 web_view::WebView web_view_; | 113 web_view::WebView web_view_; |
82 | 114 |
83 scoped_ptr<base::RunLoop> run_loop_; | 115 scoped_ptr<base::RunLoop> run_loop_; |
84 | 116 |
85 std::string last_title_; | 117 std::string last_title_; |
| 118 mojom::ButtonState last_back_button_state_; |
| 119 mojom::ButtonState last_forward_button_state_; |
86 | 120 |
87 DISALLOW_COPY_AND_ASSIGN(WebViewTest); | 121 DISALLOW_COPY_AND_ASSIGN(WebViewTest); |
88 }; | 122 }; |
89 | 123 |
90 TEST_F(WebViewTest, TestTitleChanged) { | 124 TEST_F(WebViewTest, TestTitleChanged) { |
91 base::FilePath data_file; | 125 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); |
92 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_file)); | |
93 data_file = data_file.AppendASCII("components"). | |
94 AppendASCII("test"). | |
95 AppendASCII("data"). | |
96 AppendASCII("web_view"). | |
97 AppendASCII("test_title_changed.html"); | |
98 ASSERT_TRUE(base::PathExists(data_file)); | |
99 | 126 |
100 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 127 // Our title should have been set on the navigation. |
101 request->url = mojo::util::FilePathToFileURL(data_file).spec(); | 128 EXPECT_EQ(kTestOneTitle, last_title()); |
102 web_view()->LoadRequest(request.Pass()); | 129 } |
103 | 130 |
104 // Build a nested run loop. | 131 TEST_F(WebViewTest, CanGoBackAndForward) { |
| 132 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); |
| 133 |
| 134 // We can't go back on first navigation since there's nothing previously on |
| 135 // the stack. |
| 136 EXPECT_EQ(kTestOneTitle, last_title()); |
| 137 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
| 138 last_back_button_state()); |
| 139 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
| 140 last_forward_button_state()); |
| 141 |
| 142 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); |
| 143 |
| 144 EXPECT_EQ(kTestTwoTitle, last_title()); |
| 145 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); |
| 146 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
| 147 last_forward_button_state()); |
| 148 |
| 149 web_view()->GoBack(); |
105 StartNestedRunLoopUntilLoadingDone(); | 150 StartNestedRunLoopUntilLoadingDone(); |
106 | 151 |
107 // Our title should have been set on the final. | 152 EXPECT_EQ(kTestOneTitle, last_title()); |
108 EXPECT_EQ("Test Title Changed", last_title()); | 153 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
| 154 last_back_button_state()); |
| 155 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, |
| 156 last_forward_button_state()); |
| 157 |
| 158 web_view()->GoForward(); |
| 159 StartNestedRunLoopUntilLoadingDone(); |
| 160 EXPECT_EQ(kTestTwoTitle, last_title()); |
| 161 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); |
| 162 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
| 163 last_forward_button_state()); |
| 164 } |
| 165 |
| 166 TEST_F(WebViewTest, NavigationClearsForward) { |
| 167 // First navigate somewhere, navigate somewhere else, and go back so we have |
| 168 // one item in the forward stack. |
| 169 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); |
| 170 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); |
| 171 |
| 172 web_view()->GoBack(); |
| 173 StartNestedRunLoopUntilLoadingDone(); |
| 174 |
| 175 EXPECT_EQ(kTestOneTitle, last_title()); |
| 176 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
| 177 last_back_button_state()); |
| 178 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, |
| 179 last_forward_button_state()); |
| 180 |
| 181 // Now navigate to a third file. This should clear the forward stack. |
| 182 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestThreeFile)); |
| 183 |
| 184 EXPECT_EQ(kTestThreeTitle, last_title()); |
| 185 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); |
| 186 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
| 187 last_forward_button_state()); |
109 } | 188 } |
110 | 189 |
111 } // namespace web_view | 190 } // namespace web_view |
OLD | NEW |