| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/web_view/public/cpp/web_view.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/base_paths.h" | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/files/file_util.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/path_service.h" | |
| 16 #include "base/run_loop.h" | |
| 17 #include "components/mus/public/cpp/scoped_window_ptr.h" | |
| 18 #include "components/mus/public/cpp/tests/window_server_test_base.h" | |
| 19 #include "components/mus/public/cpp/window.h" | |
| 20 #include "components/mus/public/cpp/window_tree_connection.h" | |
| 21 #include "mojo/util/filename_util.h" | |
| 22 #include "url/gurl.h" | |
| 23 | |
| 24 namespace web_view { | |
| 25 | |
| 26 namespace { | |
| 27 const char kTestOneFile[] = "test_one.html"; | |
| 28 const char kTestOneTitle[] = "Test Title One"; | |
| 29 const char kTestTwoFile[] = "test_two.html"; | |
| 30 const char kTestTwoTitle[] = "Test Title Two"; | |
| 31 const char kTestThreeFile[] = "test_three.html"; | |
| 32 const char kTestThreeTitle[] = "Test Title Three"; | |
| 33 const char kTheWordGreenFiveTimes[] = "the_word_green_five_times.html"; | |
| 34 const char kTwoIframesWithGreen[] = "two_iframes_with_green.html"; | |
| 35 | |
| 36 GURL GetTestFileURL(const std::string& file) { | |
| 37 base::FilePath data_file; | |
| 38 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &data_file)); | |
| 39 data_file = data_file.AppendASCII("components/test/data/web_view") | |
| 40 .AppendASCII(file) | |
| 41 .NormalizePathSeparators(); | |
| 42 CHECK(base::PathExists(data_file)); | |
| 43 return mojo::util::FilePathToFileURL(data_file); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 class WebViewTest : public mus::WindowServerTestBase, | |
| 48 public mojom::WebViewClient { | |
| 49 public: | |
| 50 WebViewTest() | |
| 51 : web_view_(this), | |
| 52 quit_condition_(NO_QUIT), | |
| 53 active_find_match_(0), | |
| 54 find_count_(0) {} | |
| 55 ~WebViewTest() override {} | |
| 56 | |
| 57 mojom::WebView* web_view() { return web_view_.web_view(); } | |
| 58 | |
| 59 const std::string& navigation_url() const { return navigation_url_; } | |
| 60 const std::string& last_title() const { return last_title_; } | |
| 61 mojom::ButtonState last_back_button_state() { | |
| 62 return last_back_button_state_; | |
| 63 } | |
| 64 mojom::ButtonState last_forward_button_state() { | |
| 65 return last_forward_button_state_; | |
| 66 } | |
| 67 | |
| 68 int32_t active_find_match() const { return active_find_match_; } | |
| 69 int32_t find_count() const { return find_count_; } | |
| 70 | |
| 71 enum NestedLoopQuitCondition { | |
| 72 NO_QUIT, | |
| 73 LOADING_DONE, | |
| 74 FINAL_FIND_UPATE, | |
| 75 ACTIVE_FIND_UPDATE, | |
| 76 }; | |
| 77 | |
| 78 void StartNestedRunLoopUntil(NestedLoopQuitCondition quit_condition) { | |
| 79 quit_condition_ = quit_condition; | |
| 80 run_loop_.reset(new base::RunLoop); | |
| 81 run_loop_->Run(); | |
| 82 } | |
| 83 | |
| 84 void NavigateTo(const std::string& file) { | |
| 85 mojo::URLRequestPtr request(mojo::URLRequest::New()); | |
| 86 request->url = GetTestFileURL(file).spec(); | |
| 87 web_view()->LoadRequest(std::move(request)); | |
| 88 StartNestedRunLoopUntil(LOADING_DONE); | |
| 89 } | |
| 90 | |
| 91 private: | |
| 92 void QuitNestedRunLoop() { | |
| 93 if (run_loop_) { | |
| 94 quit_condition_ = NO_QUIT; | |
| 95 run_loop_->Quit(); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 // Overridden from mojo::ShellClient: | |
| 100 void Initialize(mojo::Shell* shell, const std::string& url, | |
| 101 uint32_t id) override { | |
| 102 WindowServerTestBase::Initialize(shell, url, id); | |
| 103 shell_ = shell; | |
| 104 } | |
| 105 | |
| 106 // Overridden from WindowTreeDelegate: | |
| 107 void OnEmbed(mus::Window* root) override { | |
| 108 content_ = root->connection()->NewWindow(); | |
| 109 content_->SetBounds(root->bounds()); | |
| 110 root->AddChild(content_); | |
| 111 content_->SetVisible(true); | |
| 112 | |
| 113 web_view_.Init(shell_, content_); | |
| 114 | |
| 115 WindowServerTestBase::OnEmbed(root); | |
| 116 } | |
| 117 | |
| 118 void TearDown() override { | |
| 119 ASSERT_EQ(1u, window_manager()->GetRoots().size()); | |
| 120 mus::ScopedWindowPtr::DeleteWindowOrWindowManager( | |
| 121 *window_manager()->GetRoots().begin()); | |
| 122 WindowServerTestBase::TearDown(); | |
| 123 } | |
| 124 | |
| 125 // Overridden from web_view::mojom::WebViewClient: | |
| 126 void TopLevelNavigateRequest(mojo::URLRequestPtr request) override {} | |
| 127 void TopLevelNavigationStarted(const mojo::String& url) override { | |
| 128 navigation_url_ = url.get(); | |
| 129 } | |
| 130 void LoadingStateChanged(bool is_loading, double progress) override { | |
| 131 if (is_loading == false && quit_condition_ == LOADING_DONE) | |
| 132 QuitNestedRunLoop(); | |
| 133 } | |
| 134 void BackForwardChanged(mojom::ButtonState back_button, | |
| 135 mojom::ButtonState forward_button) override { | |
| 136 last_back_button_state_ = back_button; | |
| 137 last_forward_button_state_ = forward_button; | |
| 138 } | |
| 139 void TitleChanged(const mojo::String& title) override { | |
| 140 last_title_ = title.get(); | |
| 141 } | |
| 142 void FindInPageMatchCountUpdated(int32_t request_id, | |
| 143 int32_t count, | |
| 144 bool final_update) override { | |
| 145 find_count_ = count; | |
| 146 if (final_update && quit_condition_ == FINAL_FIND_UPATE) | |
| 147 QuitNestedRunLoop(); | |
| 148 } | |
| 149 void FindInPageSelectionUpdated(int32_t request_id, | |
| 150 int32_t active_match_ordinal) override { | |
| 151 active_find_match_ = active_match_ordinal; | |
| 152 if (quit_condition_ == ACTIVE_FIND_UPDATE) | |
| 153 QuitNestedRunLoop(); | |
| 154 } | |
| 155 | |
| 156 mojo::Shell* shell_; | |
| 157 | |
| 158 mus::Window* content_; | |
| 159 | |
| 160 web_view::WebView web_view_; | |
| 161 | |
| 162 scoped_ptr<base::RunLoop> run_loop_; | |
| 163 | |
| 164 std::string navigation_url_; | |
| 165 std::string last_title_; | |
| 166 mojom::ButtonState last_back_button_state_; | |
| 167 mojom::ButtonState last_forward_button_state_; | |
| 168 | |
| 169 NestedLoopQuitCondition quit_condition_; | |
| 170 | |
| 171 int32_t active_find_match_; | |
| 172 int32_t find_count_; | |
| 173 | |
| 174 DISALLOW_COPY_AND_ASSIGN(WebViewTest); | |
| 175 }; | |
| 176 | |
| 177 TEST_F(WebViewTest, TestTitleChanged) { | |
| 178 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); | |
| 179 | |
| 180 // Our title should have been set on the navigation. | |
| 181 EXPECT_EQ(kTestOneTitle, last_title()); | |
| 182 } | |
| 183 | |
| 184 TEST_F(WebViewTest, CanGoBackAndForward) { | |
| 185 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); | |
| 186 | |
| 187 // We can't go back on first navigation since there's nothing previously on | |
| 188 // the stack. | |
| 189 EXPECT_EQ(GetTestFileURL(kTestOneFile).spec(), navigation_url()); | |
| 190 EXPECT_EQ(kTestOneTitle, last_title()); | |
| 191 EXPECT_EQ(mojom::ButtonState::DISABLED, last_back_button_state()); | |
| 192 EXPECT_EQ(mojom::ButtonState::DISABLED, last_forward_button_state()); | |
| 193 | |
| 194 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); | |
| 195 | |
| 196 EXPECT_EQ(kTestTwoTitle, last_title()); | |
| 197 EXPECT_EQ(mojom::ButtonState::ENABLED, last_back_button_state()); | |
| 198 EXPECT_EQ(mojom::ButtonState::DISABLED, last_forward_button_state()); | |
| 199 | |
| 200 web_view()->GoBack(); | |
| 201 StartNestedRunLoopUntil(LOADING_DONE); | |
| 202 | |
| 203 EXPECT_EQ(GetTestFileURL(kTestOneFile).spec(), navigation_url()); | |
| 204 EXPECT_EQ(kTestOneTitle, last_title()); | |
| 205 EXPECT_EQ(mojom::ButtonState::DISABLED, last_back_button_state()); | |
| 206 EXPECT_EQ(mojom::ButtonState::ENABLED, last_forward_button_state()); | |
| 207 | |
| 208 web_view()->GoForward(); | |
| 209 StartNestedRunLoopUntil(LOADING_DONE); | |
| 210 EXPECT_EQ(GetTestFileURL(kTestTwoFile).spec(), navigation_url()); | |
| 211 EXPECT_EQ(kTestTwoTitle, last_title()); | |
| 212 EXPECT_EQ(mojom::ButtonState::ENABLED, last_back_button_state()); | |
| 213 EXPECT_EQ(mojom::ButtonState::DISABLED, last_forward_button_state()); | |
| 214 } | |
| 215 | |
| 216 TEST_F(WebViewTest, NavigationClearsForward) { | |
| 217 // First navigate somewhere, navigate somewhere else, and go back so we have | |
| 218 // one item in the forward stack. | |
| 219 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); | |
| 220 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); | |
| 221 | |
| 222 web_view()->GoBack(); | |
| 223 StartNestedRunLoopUntil(LOADING_DONE); | |
| 224 | |
| 225 EXPECT_EQ(GetTestFileURL(kTestOneFile).spec(), navigation_url()); | |
| 226 EXPECT_EQ(kTestOneTitle, last_title()); | |
| 227 EXPECT_EQ(mojom::ButtonState::DISABLED, last_back_button_state()); | |
| 228 EXPECT_EQ(mojom::ButtonState::ENABLED, last_forward_button_state()); | |
| 229 | |
| 230 // Now navigate to a third file. This should clear the forward stack. | |
| 231 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestThreeFile)); | |
| 232 | |
| 233 EXPECT_EQ(GetTestFileURL(kTestThreeFile).spec(), navigation_url()); | |
| 234 EXPECT_EQ(kTestThreeTitle, last_title()); | |
| 235 EXPECT_EQ(mojom::ButtonState::ENABLED, last_back_button_state()); | |
| 236 EXPECT_EQ(mojom::ButtonState::DISABLED, last_forward_button_state()); | |
| 237 } | |
| 238 | |
| 239 TEST_F(WebViewTest, Find) { | |
| 240 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTheWordGreenFiveTimes)); | |
| 241 | |
| 242 web_view()->Find("Green", true); | |
| 243 StartNestedRunLoopUntil(FINAL_FIND_UPATE); | |
| 244 EXPECT_EQ(1, active_find_match()); | |
| 245 EXPECT_EQ(5, find_count()); | |
| 246 } | |
| 247 | |
| 248 TEST_F(WebViewTest, FindAcrossIframes) { | |
| 249 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTwoIframesWithGreen)); | |
| 250 | |
| 251 web_view()->Find("Green", true); | |
| 252 StartNestedRunLoopUntil(FINAL_FIND_UPATE); | |
| 253 EXPECT_EQ(13, find_count()); | |
| 254 } | |
| 255 | |
| 256 TEST_F(WebViewTest, FindWrapAroundOneFrame) { | |
| 257 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTheWordGreenFiveTimes)); | |
| 258 | |
| 259 web_view()->Find("Green", true); | |
| 260 StartNestedRunLoopUntil(FINAL_FIND_UPATE); | |
| 261 EXPECT_EQ(1, active_find_match()); | |
| 262 | |
| 263 // Searching times 2 through 5 should increment the active find. | |
| 264 for (int i = 2; i < 6; ++i) { | |
| 265 web_view()->Find("Green", true); | |
| 266 StartNestedRunLoopUntil(ACTIVE_FIND_UPDATE); | |
| 267 EXPECT_EQ(i, active_find_match()); | |
| 268 } | |
| 269 | |
| 270 // We should wrap around. | |
| 271 web_view()->Find("Green", true); | |
| 272 StartNestedRunLoopUntil(ACTIVE_FIND_UPDATE); | |
| 273 EXPECT_EQ(1, active_find_match()); | |
| 274 } | |
| 275 | |
| 276 TEST_F(WebViewTest, FindForwardsAndBackwards) { | |
| 277 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTheWordGreenFiveTimes)); | |
| 278 | |
| 279 web_view()->Find("Green", true); | |
| 280 StartNestedRunLoopUntil(FINAL_FIND_UPATE); | |
| 281 EXPECT_EQ(1, active_find_match()); | |
| 282 | |
| 283 // Navigate two forwards. | |
| 284 for (int i = 2; i < 4; ++i) { | |
| 285 web_view()->Find("Green", true); | |
| 286 StartNestedRunLoopUntil(ACTIVE_FIND_UPDATE); | |
| 287 EXPECT_EQ(i, active_find_match()); | |
| 288 } | |
| 289 | |
| 290 // Navigate two backwards. | |
| 291 for (int i = 2; i > 0; --i) { | |
| 292 web_view()->Find("Green", false); | |
| 293 StartNestedRunLoopUntil(ACTIVE_FIND_UPDATE); | |
| 294 EXPECT_EQ(i, active_find_match()); | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 TEST_F(WebViewTest, FindWrapAroundMultipleFrames) { | |
| 299 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTwoIframesWithGreen)); | |
| 300 | |
| 301 web_view()->Find("Green", true); | |
| 302 StartNestedRunLoopUntil(FINAL_FIND_UPATE); | |
| 303 EXPECT_EQ(1, active_find_match()); | |
| 304 | |
| 305 // Searching times 2 through 13 should increment the active find. | |
| 306 for (int i = 2; i < 14; ++i) { | |
| 307 web_view()->Find("Green", true); | |
| 308 StartNestedRunLoopUntil(ACTIVE_FIND_UPDATE); | |
| 309 EXPECT_EQ(i, active_find_match()); | |
| 310 } | |
| 311 | |
| 312 // We should wrap around. | |
| 313 web_view()->Find("Green", true); | |
| 314 StartNestedRunLoopUntil(ACTIVE_FIND_UPDATE); | |
| 315 EXPECT_EQ(1, active_find_match()); | |
| 316 } | |
| 317 | |
| 318 } // namespace web_view | |
| OLD | NEW |