| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #import "ios/web/web_state/web_state_impl.h" | 5 #import "ios/web/web_state/web_state_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/base64.h" | 11 #include "base/base64.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #import "base/mac/bind_objc_block.h" | 14 #import "base/mac/bind_objc_block.h" |
| 15 #include "base/memory/ptr_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 16 #import "base/test/ios/wait_util.h" | 17 #import "base/test/ios/wait_util.h" |
| 17 #include "base/values.h" | 18 #include "base/values.h" |
| 18 #import "ios/web/public/java_script_dialog_presenter.h" | 19 #import "ios/web/public/java_script_dialog_presenter.h" |
| 19 #include "ios/web/public/load_committed_details.h" | 20 #include "ios/web/public/load_committed_details.h" |
| 20 #include "ios/web/public/test/fakes/test_browser_state.h" | 21 #include "ios/web/public/test/fakes/test_browser_state.h" |
| 21 #import "ios/web/public/test/fakes/test_web_state_delegate.h" | 22 #import "ios/web/public/test/fakes/test_web_state_delegate.h" |
| 22 #include "ios/web/public/test/web_test.h" | 23 #include "ios/web/public/test/web_test.h" |
| 23 #import "ios/web/public/web_state/context_menu_params.h" | 24 #import "ios/web/public/web_state/context_menu_params.h" |
| 24 #include "ios/web/public/web_state/global_web_state_observer.h" | 25 #include "ios/web/public/web_state/global_web_state_observer.h" |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 public: | 122 public: |
| 122 TestWebStateObserver(WebState* web_state) | 123 TestWebStateObserver(WebState* web_state) |
| 123 : WebStateObserver(web_state), | 124 : WebStateObserver(web_state), |
| 124 provisional_navigation_started_called_(false), | 125 provisional_navigation_started_called_(false), |
| 125 navigation_items_pruned_called_(false), | 126 navigation_items_pruned_called_(false), |
| 126 navigation_item_changed_called_(false), | 127 navigation_item_changed_called_(false), |
| 127 navigation_item_committed_called_(false), | 128 navigation_item_committed_called_(false), |
| 128 page_loaded_called_with_success_(false), | 129 page_loaded_called_with_success_(false), |
| 129 url_hash_changed_called_(false), | 130 url_hash_changed_called_(false), |
| 130 history_state_changed_called_(false), | 131 history_state_changed_called_(false), |
| 132 did_finish_navigation_called_(false), |
| 131 web_state_destroyed_called_(false) {} | 133 web_state_destroyed_called_(false) {} |
| 132 | 134 |
| 133 // Methods returning true if the corresponding WebStateObserver method has | 135 // Methods returning true if the corresponding WebStateObserver method has |
| 134 // been called. | 136 // been called. |
| 135 bool provisional_navigation_started_called() const { | 137 bool provisional_navigation_started_called() const { |
| 136 return provisional_navigation_started_called_; | 138 return provisional_navigation_started_called_; |
| 137 }; | 139 }; |
| 138 bool navigation_items_pruned_called() const { | 140 bool navigation_items_pruned_called() const { |
| 139 return navigation_items_pruned_called_; | 141 return navigation_items_pruned_called_; |
| 140 } | 142 } |
| 141 bool navigation_item_changed_called() const { | 143 bool navigation_item_changed_called() const { |
| 142 return navigation_item_changed_called_; | 144 return navigation_item_changed_called_; |
| 143 } | 145 } |
| 144 bool navigation_item_committed_called() const { | 146 bool navigation_item_committed_called() const { |
| 145 return navigation_item_committed_called_; | 147 return navigation_item_committed_called_; |
| 146 } | 148 } |
| 147 bool page_loaded_called_with_success() const { | 149 bool page_loaded_called_with_success() const { |
| 148 return page_loaded_called_with_success_; | 150 return page_loaded_called_with_success_; |
| 149 } | 151 } |
| 150 bool url_hash_changed_called() const { return url_hash_changed_called_; } | 152 bool url_hash_changed_called() const { return url_hash_changed_called_; } |
| 151 bool history_state_changed_called() const { | 153 bool history_state_changed_called() const { |
| 152 return history_state_changed_called_; | 154 return history_state_changed_called_; |
| 153 } | 155 } |
| 156 bool did_finish_navigation_called() const { |
| 157 return did_finish_navigation_called_; |
| 158 } |
| 154 bool web_state_destroyed_called() const { | 159 bool web_state_destroyed_called() const { |
| 155 return web_state_destroyed_called_; | 160 return web_state_destroyed_called_; |
| 156 } | 161 } |
| 157 | 162 |
| 158 private: | 163 private: |
| 159 // WebStateObserver implementation: | 164 // WebStateObserver implementation: |
| 160 void ProvisionalNavigationStarted(const GURL& url) override { | 165 void ProvisionalNavigationStarted(const GURL& url) override { |
| 161 provisional_navigation_started_called_ = true; | 166 provisional_navigation_started_called_ = true; |
| 162 } | 167 } |
| 163 void NavigationItemsPruned(size_t pruned_item_count) override { | 168 void NavigationItemsPruned(size_t pruned_item_count) override { |
| 164 navigation_items_pruned_called_ = true; | 169 navigation_items_pruned_called_ = true; |
| 165 } | 170 } |
| 166 void NavigationItemChanged() override { | 171 void NavigationItemChanged() override { |
| 167 navigation_item_changed_called_ = true; | 172 navigation_item_changed_called_ = true; |
| 168 } | 173 } |
| 169 void NavigationItemCommitted( | 174 void NavigationItemCommitted( |
| 170 const LoadCommittedDetails& load_details) override { | 175 const LoadCommittedDetails& load_details) override { |
| 171 navigation_item_committed_called_ = true; | 176 navigation_item_committed_called_ = true; |
| 172 } | 177 } |
| 178 void DidFinishNavigation(NavigationHandle* navigation_handle) override { |
| 179 did_finish_navigation_called_ = true; |
| 180 } |
| 173 void PageLoaded(PageLoadCompletionStatus load_completion_status) override { | 181 void PageLoaded(PageLoadCompletionStatus load_completion_status) override { |
| 174 page_loaded_called_with_success_ = | 182 page_loaded_called_with_success_ = |
| 175 load_completion_status == PageLoadCompletionStatus::SUCCESS; | 183 load_completion_status == PageLoadCompletionStatus::SUCCESS; |
| 176 } | 184 } |
| 177 void UrlHashChanged() override { url_hash_changed_called_ = true; } | 185 void UrlHashChanged() override { url_hash_changed_called_ = true; } |
| 178 void HistoryStateChanged() override { history_state_changed_called_ = true; } | 186 void HistoryStateChanged() override { history_state_changed_called_ = true; } |
| 179 void WebStateDestroyed() override { | 187 void WebStateDestroyed() override { |
| 180 EXPECT_TRUE(web_state()->IsBeingDestroyed()); | 188 EXPECT_TRUE(web_state()->IsBeingDestroyed()); |
| 181 web_state_destroyed_called_ = true; | 189 web_state_destroyed_called_ = true; |
| 182 Observe(nullptr); | 190 Observe(nullptr); |
| 183 } | 191 } |
| 184 | 192 |
| 185 bool provisional_navigation_started_called_; | 193 bool provisional_navigation_started_called_; |
| 186 bool navigation_items_pruned_called_; | 194 bool navigation_items_pruned_called_; |
| 187 bool navigation_item_changed_called_; | 195 bool navigation_item_changed_called_; |
| 188 bool navigation_item_committed_called_; | 196 bool navigation_item_committed_called_; |
| 189 bool page_loaded_called_with_success_; | 197 bool page_loaded_called_with_success_; |
| 190 bool url_hash_changed_called_; | 198 bool url_hash_changed_called_; |
| 191 bool history_state_changed_called_; | 199 bool history_state_changed_called_; |
| 200 bool did_finish_navigation_called_; |
| 192 bool web_state_destroyed_called_; | 201 bool web_state_destroyed_called_; |
| 193 }; | 202 }; |
| 194 | 203 |
| 195 // Test decider to check that the WebStatePolicyDecider methods are called as | 204 // Test decider to check that the WebStatePolicyDecider methods are called as |
| 196 // expected. | 205 // expected. |
| 197 class MockWebStatePolicyDecider : public WebStatePolicyDecider { | 206 class MockWebStatePolicyDecider : public WebStatePolicyDecider { |
| 198 public: | 207 public: |
| 199 explicit MockWebStatePolicyDecider(WebState* web_state) | 208 explicit MockWebStatePolicyDecider(WebState* web_state) |
| 200 : WebStatePolicyDecider(web_state) {} | 209 : WebStatePolicyDecider(web_state) {} |
| 201 virtual ~MockWebStatePolicyDecider() {} | 210 virtual ~MockWebStatePolicyDecider() {} |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 // Test that UrlHashChanged() is called. | 395 // Test that UrlHashChanged() is called. |
| 387 EXPECT_FALSE(observer->url_hash_changed_called()); | 396 EXPECT_FALSE(observer->url_hash_changed_called()); |
| 388 web_state_->OnUrlHashChanged(); | 397 web_state_->OnUrlHashChanged(); |
| 389 EXPECT_TRUE(observer->url_hash_changed_called()); | 398 EXPECT_TRUE(observer->url_hash_changed_called()); |
| 390 | 399 |
| 391 // Test that HistoryStateChanged() is called. | 400 // Test that HistoryStateChanged() is called. |
| 392 EXPECT_FALSE(observer->history_state_changed_called()); | 401 EXPECT_FALSE(observer->history_state_changed_called()); |
| 393 web_state_->OnHistoryStateChanged(); | 402 web_state_->OnHistoryStateChanged(); |
| 394 EXPECT_TRUE(observer->history_state_changed_called()); | 403 EXPECT_TRUE(observer->history_state_changed_called()); |
| 395 | 404 |
| 405 // Test that DidFinishNavigation() is called for same page navigations. |
| 406 EXPECT_FALSE(observer->did_finish_navigation_called()); |
| 407 web_state_->OnSamePageNavigation(GURL("http://test")); |
| 408 EXPECT_TRUE(observer->did_finish_navigation_called()); |
| 409 |
| 410 // Reset the observer and test that DidFinishNavigation() is called |
| 411 // for error navigations. |
| 412 observer = base::MakeUnique<TestWebStateObserver>(web_state_.get()); |
| 413 EXPECT_FALSE(observer->did_finish_navigation_called()); |
| 414 web_state_->OnErrorPageNavigation(GURL("http://test")); |
| 415 EXPECT_TRUE(observer->did_finish_navigation_called()); |
| 416 |
| 396 // Test that WebStateDestroyed() is called. | 417 // Test that WebStateDestroyed() is called. |
| 397 EXPECT_FALSE(observer->web_state_destroyed_called()); | 418 EXPECT_FALSE(observer->web_state_destroyed_called()); |
| 398 web_state_.reset(); | 419 web_state_.reset(); |
| 399 EXPECT_TRUE(observer->web_state_destroyed_called()); | 420 EXPECT_TRUE(observer->web_state_destroyed_called()); |
| 400 | 421 |
| 401 EXPECT_EQ(nullptr, observer->web_state()); | 422 EXPECT_EQ(nullptr, observer->web_state()); |
| 402 } | 423 } |
| 403 | 424 |
| 404 // Tests that WebStateDelegate methods appropriately called. | 425 // Tests that WebStateDelegate methods appropriately called. |
| 405 TEST_F(WebStateTest, DelegateTest) { | 426 TEST_F(WebStateTest, DelegateTest) { |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 668 "</script>"); | 689 "</script>"); |
| 669 | 690 |
| 670 base::test::ios::WaitUntilCondition(^{ | 691 base::test::ios::WaitUntilCondition(^{ |
| 671 return message_received; | 692 return message_received; |
| 672 }); | 693 }); |
| 673 web_state_->RemoveScriptCommandCallback("test"); | 694 web_state_->RemoveScriptCommandCallback("test"); |
| 674 } | 695 } |
| 675 | 696 |
| 676 } // namespace | 697 } // namespace |
| 677 } // namespace web | 698 } // namespace web |
| OLD | NEW |