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 { | 22 namespace { |
23 const char kTestOneFile[] = "test_one.html"; | 23 const char kTestOneFile[] = "test_one.html"; |
24 const char kTestOneTitle[] = "Test Title One"; | 24 const char kTestOneTitle[] = "Test Title One"; |
25 const char kTestTwoFile[] = "test_two.html"; | 25 const char kTestTwoFile[] = "test_two.html"; |
26 const char kTestTwoTitle[] = "Test Title Two"; | 26 const char kTestTwoTitle[] = "Test Title Two"; |
27 const char kTestThreeFile[] = "test_three.html"; | 27 const char kTestThreeFile[] = "test_three.html"; |
28 const char kTestThreeTitle[] = "Test Title Three"; | 28 const char kTestThreeTitle[] = "Test Title Three"; |
| 29 |
| 30 GURL GetTestFileURL(const std::string& file) { |
| 31 base::FilePath data_file; |
| 32 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &data_file)); |
| 33 data_file = data_file.AppendASCII("components/test/data/web_view") |
| 34 .AppendASCII(file) |
| 35 .NormalizePathSeparators(); |
| 36 CHECK(base::PathExists(data_file)); |
| 37 return mojo::util::FilePathToFileURL(data_file); |
| 38 } |
29 } | 39 } |
30 | 40 |
31 class WebViewTest : public mus::ViewManagerTestBase, | 41 class WebViewTest : public mus::ViewManagerTestBase, |
32 public mojom::WebViewClient { | 42 public mojom::WebViewClient { |
33 public: | 43 public: |
34 WebViewTest() : web_view_(this) {} | 44 WebViewTest() : web_view_(this) {} |
35 ~WebViewTest() override {} | 45 ~WebViewTest() override {} |
36 | 46 |
37 mojom::WebView* web_view() { return web_view_.web_view(); } | 47 mojom::WebView* web_view() { return web_view_.web_view(); } |
38 | 48 |
39 const std::string& last_title() { return last_title_; } | 49 const std::string& navigation_url() const { return navigation_url_; } |
| 50 const std::string& last_title() const { return last_title_; } |
40 mojom::ButtonState last_back_button_state() { | 51 mojom::ButtonState last_back_button_state() { |
41 return last_back_button_state_; | 52 return last_back_button_state_; |
42 } | 53 } |
43 mojom::ButtonState last_forward_button_state() { | 54 mojom::ButtonState last_forward_button_state() { |
44 return last_forward_button_state_; | 55 return last_forward_button_state_; |
45 } | 56 } |
46 | 57 |
47 void StartNestedRunLoopUntilLoadingDone() { | 58 void StartNestedRunLoopUntilLoadingDone() { |
48 run_loop_.reset(new base::RunLoop); | 59 run_loop_.reset(new base::RunLoop); |
49 run_loop_->Run(); | 60 run_loop_->Run(); |
50 } | 61 } |
51 | 62 |
52 void NavigateTo(const std::string& file) { | 63 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()); | 64 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
59 request->url = mojo::util::FilePathToFileURL(data_file).spec(); | 65 request->url = GetTestFileURL(file).spec(); |
60 web_view()->LoadRequest(request.Pass()); | 66 web_view()->LoadRequest(request.Pass()); |
61 StartNestedRunLoopUntilLoadingDone(); | 67 StartNestedRunLoopUntilLoadingDone(); |
62 } | 68 } |
63 | 69 |
64 private: | 70 private: |
65 void QuitNestedRunLoop() { | 71 void QuitNestedRunLoop() { |
66 if (run_loop_) { | 72 if (run_loop_) { |
67 run_loop_->Quit(); | 73 run_loop_->Quit(); |
68 } | 74 } |
69 } | 75 } |
(...skipping 14 matching lines...) Expand all Loading... |
84 | 90 |
85 ViewManagerTestBase::OnEmbed(root); | 91 ViewManagerTestBase::OnEmbed(root); |
86 } | 92 } |
87 | 93 |
88 void TearDown() override { | 94 void TearDown() override { |
89 mus::ScopedViewPtr::DeleteViewOrViewManager(window_manager()->GetRoot()); | 95 mus::ScopedViewPtr::DeleteViewOrViewManager(window_manager()->GetRoot()); |
90 ViewManagerTestBase::TearDown(); | 96 ViewManagerTestBase::TearDown(); |
91 } | 97 } |
92 | 98 |
93 // Overridden from web_view::mojom::WebViewClient: | 99 // Overridden from web_view::mojom::WebViewClient: |
94 void TopLevelNavigate(mojo::URLRequestPtr request) override {} | 100 void TopLevelNavigateRequest(mojo::URLRequestPtr request) override {} |
| 101 void TopLevelNavigationStarted(const mojo::String& url) override { |
| 102 navigation_url_ = url.get(); |
| 103 } |
95 void LoadingStateChanged(bool is_loading, double progress) override { | 104 void LoadingStateChanged(bool is_loading, double progress) override { |
96 if (is_loading == false) | 105 if (is_loading == false) |
97 QuitNestedRunLoop(); | 106 QuitNestedRunLoop(); |
98 } | 107 } |
99 void BackForwardChanged(mojom::ButtonState back_button, | 108 void BackForwardChanged(mojom::ButtonState back_button, |
100 mojom::ButtonState forward_button) override { | 109 mojom::ButtonState forward_button) override { |
101 last_back_button_state_ = back_button; | 110 last_back_button_state_ = back_button; |
102 last_forward_button_state_ = forward_button; | 111 last_forward_button_state_ = forward_button; |
103 } | 112 } |
104 void TitleChanged(const mojo::String& title) override { | 113 void TitleChanged(const mojo::String& title) override { |
105 last_title_ = title.get(); | 114 last_title_ = title.get(); |
106 } | 115 } |
107 | 116 |
108 mojo::ApplicationImpl* app_; | 117 mojo::ApplicationImpl* app_; |
109 | 118 |
110 mus::View* content_; | 119 mus::View* content_; |
111 | 120 |
112 web_view::WebView web_view_; | 121 web_view::WebView web_view_; |
113 | 122 |
114 scoped_ptr<base::RunLoop> run_loop_; | 123 scoped_ptr<base::RunLoop> run_loop_; |
115 | 124 |
| 125 std::string navigation_url_; |
116 std::string last_title_; | 126 std::string last_title_; |
117 mojom::ButtonState last_back_button_state_; | 127 mojom::ButtonState last_back_button_state_; |
118 mojom::ButtonState last_forward_button_state_; | 128 mojom::ButtonState last_forward_button_state_; |
119 | 129 |
120 DISALLOW_COPY_AND_ASSIGN(WebViewTest); | 130 DISALLOW_COPY_AND_ASSIGN(WebViewTest); |
121 }; | 131 }; |
122 | 132 |
123 TEST_F(WebViewTest, TestTitleChanged) { | 133 TEST_F(WebViewTest, TestTitleChanged) { |
124 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); | 134 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); |
125 | 135 |
126 // Our title should have been set on the navigation. | 136 // Our title should have been set on the navigation. |
127 EXPECT_EQ(kTestOneTitle, last_title()); | 137 EXPECT_EQ(kTestOneTitle, last_title()); |
128 } | 138 } |
129 | 139 |
130 TEST_F(WebViewTest, CanGoBackAndForward) { | 140 TEST_F(WebViewTest, CanGoBackAndForward) { |
131 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); | 141 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); |
132 | 142 |
133 // We can't go back on first navigation since there's nothing previously on | 143 // We can't go back on first navigation since there's nothing previously on |
134 // the stack. | 144 // the stack. |
| 145 EXPECT_EQ(GetTestFileURL(kTestOneFile).spec(), navigation_url()); |
135 EXPECT_EQ(kTestOneTitle, last_title()); | 146 EXPECT_EQ(kTestOneTitle, last_title()); |
136 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 147 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
137 last_back_button_state()); | 148 last_back_button_state()); |
138 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 149 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
139 last_forward_button_state()); | 150 last_forward_button_state()); |
140 | 151 |
141 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); | 152 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); |
142 | 153 |
143 EXPECT_EQ(kTestTwoTitle, last_title()); | 154 EXPECT_EQ(kTestTwoTitle, last_title()); |
144 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); | 155 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); |
145 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 156 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
146 last_forward_button_state()); | 157 last_forward_button_state()); |
147 | 158 |
148 web_view()->GoBack(); | 159 web_view()->GoBack(); |
149 StartNestedRunLoopUntilLoadingDone(); | 160 StartNestedRunLoopUntilLoadingDone(); |
150 | 161 |
| 162 EXPECT_EQ(GetTestFileURL(kTestOneFile).spec(), navigation_url()); |
151 EXPECT_EQ(kTestOneTitle, last_title()); | 163 EXPECT_EQ(kTestOneTitle, last_title()); |
152 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 164 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
153 last_back_button_state()); | 165 last_back_button_state()); |
154 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, | 166 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, |
155 last_forward_button_state()); | 167 last_forward_button_state()); |
156 | 168 |
157 web_view()->GoForward(); | 169 web_view()->GoForward(); |
158 StartNestedRunLoopUntilLoadingDone(); | 170 StartNestedRunLoopUntilLoadingDone(); |
| 171 EXPECT_EQ(GetTestFileURL(kTestTwoFile).spec(), navigation_url()); |
159 EXPECT_EQ(kTestTwoTitle, last_title()); | 172 EXPECT_EQ(kTestTwoTitle, last_title()); |
160 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); | 173 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); |
161 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 174 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
162 last_forward_button_state()); | 175 last_forward_button_state()); |
163 } | 176 } |
164 | 177 |
165 TEST_F(WebViewTest, NavigationClearsForward) { | 178 TEST_F(WebViewTest, NavigationClearsForward) { |
166 // First navigate somewhere, navigate somewhere else, and go back so we have | 179 // First navigate somewhere, navigate somewhere else, and go back so we have |
167 // one item in the forward stack. | 180 // one item in the forward stack. |
168 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); | 181 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); |
169 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); | 182 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); |
170 | 183 |
171 web_view()->GoBack(); | 184 web_view()->GoBack(); |
172 StartNestedRunLoopUntilLoadingDone(); | 185 StartNestedRunLoopUntilLoadingDone(); |
173 | 186 |
| 187 EXPECT_EQ(GetTestFileURL(kTestOneFile).spec(), navigation_url()); |
174 EXPECT_EQ(kTestOneTitle, last_title()); | 188 EXPECT_EQ(kTestOneTitle, last_title()); |
175 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 189 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
176 last_back_button_state()); | 190 last_back_button_state()); |
177 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, | 191 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, |
178 last_forward_button_state()); | 192 last_forward_button_state()); |
179 | 193 |
180 // Now navigate to a third file. This should clear the forward stack. | 194 // Now navigate to a third file. This should clear the forward stack. |
181 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestThreeFile)); | 195 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestThreeFile)); |
182 | 196 |
| 197 EXPECT_EQ(GetTestFileURL(kTestThreeFile).spec(), navigation_url()); |
183 EXPECT_EQ(kTestThreeTitle, last_title()); | 198 EXPECT_EQ(kTestThreeTitle, last_title()); |
184 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); | 199 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); |
185 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 200 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
186 last_forward_button_state()); | 201 last_forward_button_state()); |
187 } | 202 } |
188 | 203 |
189 } // namespace web_view | 204 } // namespace web_view |
OLD | NEW |