| 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 const char kTheWordGreenFiveTimes[] = "the_word_green_five_times.html"; |
| 30 const char kTwoIframesWithGreen[] = "two_iframes_with_green.html"; |
| 29 | 31 |
| 30 GURL GetTestFileURL(const std::string& file) { | 32 GURL GetTestFileURL(const std::string& file) { |
| 31 base::FilePath data_file; | 33 base::FilePath data_file; |
| 32 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &data_file)); | 34 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &data_file)); |
| 33 data_file = data_file.AppendASCII("components/test/data/web_view") | 35 data_file = data_file.AppendASCII("components/test/data/web_view") |
| 34 .AppendASCII(file) | 36 .AppendASCII(file) |
| 35 .NormalizePathSeparators(); | 37 .NormalizePathSeparators(); |
| 36 CHECK(base::PathExists(data_file)); | 38 CHECK(base::PathExists(data_file)); |
| 37 return mojo::util::FilePathToFileURL(data_file); | 39 return mojo::util::FilePathToFileURL(data_file); |
| 38 } | 40 } |
| 39 } | 41 } |
| 40 | 42 |
| 41 class WebViewTest : public mus::ViewManagerTestBase, | 43 class WebViewTest : public mus::ViewManagerTestBase, |
| 42 public mojom::WebViewClient { | 44 public mojom::WebViewClient { |
| 43 public: | 45 public: |
| 44 WebViewTest() : web_view_(this) {} | 46 WebViewTest() |
| 47 : web_view_(this), |
| 48 quit_condition_(NO_QUIT), |
| 49 active_find_match_(0), |
| 50 find_count_(0) {} |
| 45 ~WebViewTest() override {} | 51 ~WebViewTest() override {} |
| 46 | 52 |
| 47 mojom::WebView* web_view() { return web_view_.web_view(); } | 53 mojom::WebView* web_view() { return web_view_.web_view(); } |
| 48 | 54 |
| 49 const std::string& navigation_url() const { return navigation_url_; } | 55 const std::string& navigation_url() const { return navigation_url_; } |
| 50 const std::string& last_title() const { return last_title_; } | 56 const std::string& last_title() const { return last_title_; } |
| 51 mojom::ButtonState last_back_button_state() { | 57 mojom::ButtonState last_back_button_state() { |
| 52 return last_back_button_state_; | 58 return last_back_button_state_; |
| 53 } | 59 } |
| 54 mojom::ButtonState last_forward_button_state() { | 60 mojom::ButtonState last_forward_button_state() { |
| 55 return last_forward_button_state_; | 61 return last_forward_button_state_; |
| 56 } | 62 } |
| 57 | 63 |
| 58 void StartNestedRunLoopUntilLoadingDone() { | 64 int32_t active_find_match() const { return active_find_match_; } |
| 65 int32_t find_count() const { return find_count_; } |
| 66 |
| 67 enum NestedLoopQuitCondition { |
| 68 NO_QUIT, |
| 69 LOADING_DONE, |
| 70 FINAL_FIND_UPATE, |
| 71 }; |
| 72 |
| 73 void StartNestedRunLoopUntil(NestedLoopQuitCondition quit_condition) { |
| 74 quit_condition_ = quit_condition; |
| 59 run_loop_.reset(new base::RunLoop); | 75 run_loop_.reset(new base::RunLoop); |
| 60 run_loop_->Run(); | 76 run_loop_->Run(); |
| 61 } | 77 } |
| 62 | 78 |
| 63 void NavigateTo(const std::string& file) { | 79 void NavigateTo(const std::string& file) { |
| 64 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 80 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 65 request->url = GetTestFileURL(file).spec(); | 81 request->url = GetTestFileURL(file).spec(); |
| 66 web_view()->LoadRequest(request.Pass()); | 82 web_view()->LoadRequest(request.Pass()); |
| 67 StartNestedRunLoopUntilLoadingDone(); | 83 StartNestedRunLoopUntil(LOADING_DONE); |
| 68 } | 84 } |
| 69 | 85 |
| 70 private: | 86 private: |
| 71 void QuitNestedRunLoop() { | 87 void QuitNestedRunLoop() { |
| 72 if (run_loop_) { | 88 if (run_loop_) { |
| 89 quit_condition_ = NO_QUIT; |
| 73 run_loop_->Quit(); | 90 run_loop_->Quit(); |
| 74 } | 91 } |
| 75 } | 92 } |
| 76 | 93 |
| 77 // Overridden from ApplicationDelegate: | 94 // Overridden from ApplicationDelegate: |
| 78 void Initialize(mojo::ApplicationImpl* app) override { | 95 void Initialize(mojo::ApplicationImpl* app) override { |
| 79 ViewManagerTestBase::Initialize(app); | 96 ViewManagerTestBase::Initialize(app); |
| 80 app_ = app; | 97 app_ = app; |
| 81 } | 98 } |
| 82 | 99 |
| 83 // Overridden from ViewTreeDelegate: | 100 // Overridden from ViewTreeDelegate: |
| 84 void OnEmbed(mus::View* root) override { | 101 void OnEmbed(mus::View* root) override { |
| 85 content_ = root->connection()->CreateView(); | 102 content_ = root->connection()->CreateView(); |
| 103 content_->SetBounds(root->bounds()); |
| 86 root->AddChild(content_); | 104 root->AddChild(content_); |
| 87 content_->SetVisible(true); | 105 content_->SetVisible(true); |
| 88 | 106 |
| 89 web_view_.Init(app_, content_); | 107 web_view_.Init(app_, content_); |
| 90 | 108 |
| 91 ViewManagerTestBase::OnEmbed(root); | 109 ViewManagerTestBase::OnEmbed(root); |
| 92 } | 110 } |
| 93 | 111 |
| 94 void TearDown() override { | 112 void TearDown() override { |
| 95 mus::ScopedViewPtr::DeleteViewOrViewManager(window_manager()->GetRoot()); | 113 mus::ScopedViewPtr::DeleteViewOrViewManager(window_manager()->GetRoot()); |
| 96 ViewManagerTestBase::TearDown(); | 114 ViewManagerTestBase::TearDown(); |
| 97 } | 115 } |
| 98 | 116 |
| 99 // Overridden from web_view::mojom::WebViewClient: | 117 // Overridden from web_view::mojom::WebViewClient: |
| 100 void TopLevelNavigateRequest(mojo::URLRequestPtr request) override {} | 118 void TopLevelNavigateRequest(mojo::URLRequestPtr request) override {} |
| 101 void TopLevelNavigationStarted(const mojo::String& url) override { | 119 void TopLevelNavigationStarted(const mojo::String& url) override { |
| 102 navigation_url_ = url.get(); | 120 navigation_url_ = url.get(); |
| 103 } | 121 } |
| 104 void LoadingStateChanged(bool is_loading, double progress) override { | 122 void LoadingStateChanged(bool is_loading, double progress) override { |
| 105 if (is_loading == false) | 123 if (is_loading == false && quit_condition_ == LOADING_DONE) |
| 106 QuitNestedRunLoop(); | 124 QuitNestedRunLoop(); |
| 107 } | 125 } |
| 108 void BackForwardChanged(mojom::ButtonState back_button, | 126 void BackForwardChanged(mojom::ButtonState back_button, |
| 109 mojom::ButtonState forward_button) override { | 127 mojom::ButtonState forward_button) override { |
| 110 last_back_button_state_ = back_button; | 128 last_back_button_state_ = back_button; |
| 111 last_forward_button_state_ = forward_button; | 129 last_forward_button_state_ = forward_button; |
| 112 } | 130 } |
| 113 void TitleChanged(const mojo::String& title) override { | 131 void TitleChanged(const mojo::String& title) override { |
| 114 last_title_ = title.get(); | 132 last_title_ = title.get(); |
| 115 } | 133 } |
| 134 void FindInPageMatchCountUpdated(int32_t request_id, |
| 135 int32_t count, |
| 136 bool final_update) override { |
| 137 find_count_ = count; |
| 138 if (final_update && quit_condition_ == FINAL_FIND_UPATE) |
| 139 QuitNestedRunLoop(); |
| 140 } |
| 141 void FindInPageSelectionUpdated(int32_t request_id, |
| 142 int32_t active_match_ordinal) override { |
| 143 active_find_match_ = active_match_ordinal; |
| 144 } |
| 116 | 145 |
| 117 mojo::ApplicationImpl* app_; | 146 mojo::ApplicationImpl* app_; |
| 118 | 147 |
| 119 mus::View* content_; | 148 mus::View* content_; |
| 120 | 149 |
| 121 web_view::WebView web_view_; | 150 web_view::WebView web_view_; |
| 122 | 151 |
| 123 scoped_ptr<base::RunLoop> run_loop_; | 152 scoped_ptr<base::RunLoop> run_loop_; |
| 124 | 153 |
| 125 std::string navigation_url_; | 154 std::string navigation_url_; |
| 126 std::string last_title_; | 155 std::string last_title_; |
| 127 mojom::ButtonState last_back_button_state_; | 156 mojom::ButtonState last_back_button_state_; |
| 128 mojom::ButtonState last_forward_button_state_; | 157 mojom::ButtonState last_forward_button_state_; |
| 129 | 158 |
| 159 NestedLoopQuitCondition quit_condition_; |
| 160 |
| 161 int32_t active_find_match_; |
| 162 int32_t find_count_; |
| 163 |
| 130 DISALLOW_COPY_AND_ASSIGN(WebViewTest); | 164 DISALLOW_COPY_AND_ASSIGN(WebViewTest); |
| 131 }; | 165 }; |
| 132 | 166 |
| 133 TEST_F(WebViewTest, TestTitleChanged) { | 167 TEST_F(WebViewTest, TestTitleChanged) { |
| 134 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); | 168 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); |
| 135 | 169 |
| 136 // Our title should have been set on the navigation. | 170 // Our title should have been set on the navigation. |
| 137 EXPECT_EQ(kTestOneTitle, last_title()); | 171 EXPECT_EQ(kTestOneTitle, last_title()); |
| 138 } | 172 } |
| 139 | 173 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 150 last_forward_button_state()); | 184 last_forward_button_state()); |
| 151 | 185 |
| 152 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); | 186 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); |
| 153 | 187 |
| 154 EXPECT_EQ(kTestTwoTitle, last_title()); | 188 EXPECT_EQ(kTestTwoTitle, last_title()); |
| 155 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); | 189 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); |
| 156 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 190 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
| 157 last_forward_button_state()); | 191 last_forward_button_state()); |
| 158 | 192 |
| 159 web_view()->GoBack(); | 193 web_view()->GoBack(); |
| 160 StartNestedRunLoopUntilLoadingDone(); | 194 StartNestedRunLoopUntil(LOADING_DONE); |
| 161 | 195 |
| 162 EXPECT_EQ(GetTestFileURL(kTestOneFile).spec(), navigation_url()); | 196 EXPECT_EQ(GetTestFileURL(kTestOneFile).spec(), navigation_url()); |
| 163 EXPECT_EQ(kTestOneTitle, last_title()); | 197 EXPECT_EQ(kTestOneTitle, last_title()); |
| 164 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 198 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
| 165 last_back_button_state()); | 199 last_back_button_state()); |
| 166 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, | 200 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, |
| 167 last_forward_button_state()); | 201 last_forward_button_state()); |
| 168 | 202 |
| 169 web_view()->GoForward(); | 203 web_view()->GoForward(); |
| 170 StartNestedRunLoopUntilLoadingDone(); | 204 StartNestedRunLoopUntil(LOADING_DONE); |
| 171 EXPECT_EQ(GetTestFileURL(kTestTwoFile).spec(), navigation_url()); | 205 EXPECT_EQ(GetTestFileURL(kTestTwoFile).spec(), navigation_url()); |
| 172 EXPECT_EQ(kTestTwoTitle, last_title()); | 206 EXPECT_EQ(kTestTwoTitle, last_title()); |
| 173 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); | 207 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); |
| 174 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 208 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
| 175 last_forward_button_state()); | 209 last_forward_button_state()); |
| 176 } | 210 } |
| 177 | 211 |
| 178 TEST_F(WebViewTest, NavigationClearsForward) { | 212 TEST_F(WebViewTest, NavigationClearsForward) { |
| 179 // First navigate somewhere, navigate somewhere else, and go back so we have | 213 // First navigate somewhere, navigate somewhere else, and go back so we have |
| 180 // one item in the forward stack. | 214 // one item in the forward stack. |
| 181 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); | 215 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); |
| 182 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); | 216 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); |
| 183 | 217 |
| 184 web_view()->GoBack(); | 218 web_view()->GoBack(); |
| 185 StartNestedRunLoopUntilLoadingDone(); | 219 StartNestedRunLoopUntil(LOADING_DONE); |
| 186 | 220 |
| 187 EXPECT_EQ(GetTestFileURL(kTestOneFile).spec(), navigation_url()); | 221 EXPECT_EQ(GetTestFileURL(kTestOneFile).spec(), navigation_url()); |
| 188 EXPECT_EQ(kTestOneTitle, last_title()); | 222 EXPECT_EQ(kTestOneTitle, last_title()); |
| 189 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 223 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
| 190 last_back_button_state()); | 224 last_back_button_state()); |
| 191 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, | 225 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, |
| 192 last_forward_button_state()); | 226 last_forward_button_state()); |
| 193 | 227 |
| 194 // Now navigate to a third file. This should clear the forward stack. | 228 // Now navigate to a third file. This should clear the forward stack. |
| 195 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestThreeFile)); | 229 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestThreeFile)); |
| 196 | 230 |
| 197 EXPECT_EQ(GetTestFileURL(kTestThreeFile).spec(), navigation_url()); | 231 EXPECT_EQ(GetTestFileURL(kTestThreeFile).spec(), navigation_url()); |
| 198 EXPECT_EQ(kTestThreeTitle, last_title()); | 232 EXPECT_EQ(kTestThreeTitle, last_title()); |
| 199 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); | 233 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); |
| 200 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 234 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
| 201 last_forward_button_state()); | 235 last_forward_button_state()); |
| 202 } | 236 } |
| 203 | 237 |
| 238 TEST_F(WebViewTest, Find) { |
| 239 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTheWordGreenFiveTimes)); |
| 240 |
| 241 web_view()->Find(1, "Green"); |
| 242 StartNestedRunLoopUntil(FINAL_FIND_UPATE); |
| 243 EXPECT_EQ(1, active_find_match()); |
| 244 EXPECT_EQ(5, find_count()); |
| 245 } |
| 246 |
| 247 TEST_F(WebViewTest, FindAcrossIframes) { |
| 248 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTwoIframesWithGreen)); |
| 249 |
| 250 web_view()->Find(1, "Green"); |
| 251 StartNestedRunLoopUntil(FINAL_FIND_UPATE); |
| 252 EXPECT_EQ(13, find_count()); |
| 253 } |
| 254 |
| 204 } // namespace web_view | 255 } // namespace web_view |
| OLD | NEW |