| 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 #include "ios/web/web_state/web_state_impl.h" | 5 #include "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 #include "base/mac/bind_objc_block.h" | 14 #include "base/mac/bind_objc_block.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/test/ios/wait_util.h" | 16 #include "base/test/ios/wait_util.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #import "ios/web/public/java_script_dialog_presenter.h" |
| 18 #include "ios/web/public/load_committed_details.h" | 19 #include "ios/web/public/load_committed_details.h" |
| 19 #include "ios/web/public/test/test_browser_state.h" | 20 #include "ios/web/public/test/test_browser_state.h" |
| 20 #include "ios/web/public/test/web_test.h" | 21 #include "ios/web/public/test/web_test.h" |
| 22 #include "ios/web/public/web_state/context_menu_params.h" |
| 21 #include "ios/web/public/web_state/global_web_state_observer.h" | 23 #include "ios/web/public/web_state/global_web_state_observer.h" |
| 22 #include "ios/web/public/web_state/web_state_delegate.h" | 24 #include "ios/web/public/web_state/web_state_delegate.h" |
| 23 #include "ios/web/public/web_state/web_state_observer.h" | 25 #include "ios/web/public/web_state/web_state_observer.h" |
| 24 #include "ios/web/public/web_state/web_state_policy_decider.h" | 26 #include "ios/web/public/web_state/web_state_policy_decider.h" |
| 25 #include "ios/web/web_state/global_web_state_event_tracker.h" | 27 #include "ios/web/web_state/global_web_state_event_tracker.h" |
| 26 #import "ios/web/web_state/ui/crw_web_controller.h" | 28 #import "ios/web/web_state/ui/crw_web_controller.h" |
| 27 #include "net/http/http_response_headers.h" | 29 #include "net/http/http_response_headers.h" |
| 28 #include "net/http/http_util.h" | 30 #include "net/http/http_util.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 bool did_start_loading_called_; | 111 bool did_start_loading_called_; |
| 110 bool did_stop_loading_called_; | 112 bool did_stop_loading_called_; |
| 111 bool page_loaded_called_with_success_; | 113 bool page_loaded_called_with_success_; |
| 112 bool web_state_destroyed_called_; | 114 bool web_state_destroyed_called_; |
| 113 }; | 115 }; |
| 114 | 116 |
| 115 // Test delegate to check that the WebStateDelegate methods are called as | 117 // Test delegate to check that the WebStateDelegate methods are called as |
| 116 // expected. | 118 // expected. |
| 117 class TestWebStateDelegate : public WebStateDelegate { | 119 class TestWebStateDelegate : public WebStateDelegate { |
| 118 public: | 120 public: |
| 119 TestWebStateDelegate() : load_progress_changed_called_(false) {} | 121 TestWebStateDelegate() |
| 122 : load_progress_changed_called_(false), |
| 123 handle_context_menu_called_(false), |
| 124 get_java_script_dialog_presenter_called_(false) {} |
| 120 | 125 |
| 121 // Methods returning true if the corresponding WebStateObserver method has | 126 // True if the WebStateDelegate LoadProgressChanged method has been called. |
| 122 // been called. | |
| 123 bool load_progress_changed_called() const { | 127 bool load_progress_changed_called() const { |
| 124 return load_progress_changed_called_; | 128 return load_progress_changed_called_; |
| 125 } | 129 } |
| 126 | 130 |
| 131 // True if the WebStateDelegate HandleContextMenu method has been called. |
| 132 bool handle_context_menu_called() const { |
| 133 return handle_context_menu_called_; |
| 134 } |
| 135 |
| 136 // True if the WebStateDelegate GetJavaScriptDialogPresenter method has been |
| 137 // called. |
| 138 bool get_java_script_dialog_presenter_called() const { |
| 139 return get_java_script_dialog_presenter_called_; |
| 140 } |
| 141 |
| 142 void SetJavaScriptDialogPresenter(JavaScriptDialogPresenter* presenter) { |
| 143 presenter_ = presenter; |
| 144 } |
| 145 |
| 127 private: | 146 private: |
| 128 // WebStateObserver implementation: | 147 // WebStateDelegate implementation: |
| 129 void LoadProgressChanged(WebState* source, double progress) override { | 148 void LoadProgressChanged(WebState* source, double progress) override { |
| 130 load_progress_changed_called_ = true; | 149 load_progress_changed_called_ = true; |
| 131 } | 150 } |
| 132 | 151 |
| 152 bool HandleContextMenu(WebState* source, |
| 153 const ContextMenuParams& params) override { |
| 154 handle_context_menu_called_ = true; |
| 155 return NO; |
| 156 } |
| 157 |
| 158 JavaScriptDialogPresenter* GetJavaScriptDialogPresenter( |
| 159 WebState* source) override { |
| 160 get_java_script_dialog_presenter_called_ = true; |
| 161 return presenter_; |
| 162 } |
| 163 |
| 133 bool load_progress_changed_called_; | 164 bool load_progress_changed_called_; |
| 165 bool handle_context_menu_called_; |
| 166 bool get_java_script_dialog_presenter_called_; |
| 167 JavaScriptDialogPresenter* presenter_; |
| 168 }; |
| 169 |
| 170 // Test presenter to check that the JavaScriptDialogPresenter methods are called |
| 171 // as expected. |
| 172 class TestJavaScriptDialogPresenter : public JavaScriptDialogPresenter { |
| 173 public: |
| 174 TestJavaScriptDialogPresenter() |
| 175 : cancel_active_and_pending_dialogs_called_(false), |
| 176 run_java_script_dialog_called_(false) {} |
| 177 |
| 178 // True if the JavaScriptDialogPresenter CancelActiveAndPendingDialogs method |
| 179 // has been called. |
| 180 bool cancel_active_and_pending_dialogs_called() const { |
| 181 return cancel_active_and_pending_dialogs_called_; |
| 182 } |
| 183 |
| 184 // True if the JavaScriptDialogPresenter RunJavaScriptDialog method has been |
| 185 // called. |
| 186 bool run_java_script_dialog_called() const { |
| 187 return run_java_script_dialog_called_; |
| 188 } |
| 189 |
| 190 private: |
| 191 // JavaScriptDialogPresenter implementation: |
| 192 void RunJavaScriptDialog(WebState* web_state, |
| 193 const GURL& origin_url, |
| 194 JavaScriptDialogType java_script_dialog_type, |
| 195 NSString* message_text, |
| 196 NSString* default_prompt_text, |
| 197 const DialogClosedCallback& callback) override { |
| 198 run_java_script_dialog_called_ = true; |
| 199 callback.Run(false, nil); |
| 200 } |
| 201 |
| 202 void CancelActiveAndPendingDialogs(WebState* web_state) override { |
| 203 cancel_active_and_pending_dialogs_called_ = true; |
| 204 } |
| 205 |
| 206 bool cancel_active_and_pending_dialogs_called_; |
| 207 bool run_java_script_dialog_called_; |
| 134 }; | 208 }; |
| 135 | 209 |
| 136 // Test observer to check that the WebStateObserver methods are called as | 210 // Test observer to check that the WebStateObserver methods are called as |
| 137 // expected. | 211 // expected. |
| 138 class TestWebStateObserver : public WebStateObserver { | 212 class TestWebStateObserver : public WebStateObserver { |
| 139 public: | 213 public: |
| 140 TestWebStateObserver(WebState* web_state) | 214 TestWebStateObserver(WebState* web_state) |
| 141 : WebStateObserver(web_state), | 215 : WebStateObserver(web_state), |
| 142 provisional_navigation_started_called_(false), | 216 provisional_navigation_started_called_(false), |
| 143 navigation_items_pruned_called_(false), | 217 navigation_items_pruned_called_(false), |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 | 482 |
| 409 // Tests that WebStateDelegate methods appropriately called. | 483 // Tests that WebStateDelegate methods appropriately called. |
| 410 TEST_F(WebStateTest, DelegateTest) { | 484 TEST_F(WebStateTest, DelegateTest) { |
| 411 TestWebStateDelegate delegate; | 485 TestWebStateDelegate delegate; |
| 412 web_state_->SetDelegate(&delegate); | 486 web_state_->SetDelegate(&delegate); |
| 413 | 487 |
| 414 // Test that LoadProgressChanged() is called. | 488 // Test that LoadProgressChanged() is called. |
| 415 EXPECT_FALSE(delegate.load_progress_changed_called()); | 489 EXPECT_FALSE(delegate.load_progress_changed_called()); |
| 416 web_state_->SendChangeLoadProgress(0.0); | 490 web_state_->SendChangeLoadProgress(0.0); |
| 417 EXPECT_TRUE(delegate.load_progress_changed_called()); | 491 EXPECT_TRUE(delegate.load_progress_changed_called()); |
| 492 |
| 493 // Test that HandleContextMenu() is called. |
| 494 EXPECT_FALSE(delegate.handle_context_menu_called()); |
| 495 web::ContextMenuParams context_menu_params; |
| 496 web_state_->HandleContextMenu(context_menu_params); |
| 497 EXPECT_TRUE(delegate.handle_context_menu_called()); |
| 498 |
| 499 // Test that GetJavaScriptDialogPresenter() is called. |
| 500 TestJavaScriptDialogPresenter presenter; |
| 501 delegate.SetJavaScriptDialogPresenter(&presenter); |
| 502 |
| 503 EXPECT_FALSE(delegate.get_java_script_dialog_presenter_called()); |
| 504 EXPECT_FALSE(presenter.run_java_script_dialog_called()); |
| 505 EXPECT_FALSE(presenter.cancel_active_and_pending_dialogs_called()); |
| 506 |
| 507 __block bool callback_called = false; |
| 508 web_state_->RunJavaScriptDialog(GURL(), JAVASCRIPT_DIALOG_TYPE_ALERT, @"", |
| 509 nil, base::BindBlock(^(bool, NSString*) { |
| 510 callback_called = true; |
| 511 })); |
| 512 |
| 513 EXPECT_TRUE(delegate.get_java_script_dialog_presenter_called()); |
| 514 EXPECT_TRUE(presenter.run_java_script_dialog_called()); |
| 515 EXPECT_TRUE(callback_called); |
| 516 |
| 517 EXPECT_FALSE(presenter.cancel_active_and_pending_dialogs_called()); |
| 518 web_state_->CancelActiveAndPendingDialogs(); |
| 519 EXPECT_TRUE(presenter.cancel_active_and_pending_dialogs_called()); |
| 418 } | 520 } |
| 419 | 521 |
| 420 // Verifies that GlobalWebStateObservers are called when expected. | 522 // Verifies that GlobalWebStateObservers are called when expected. |
| 421 TEST_F(WebStateTest, GlobalObserverTest) { | 523 TEST_F(WebStateTest, GlobalObserverTest) { |
| 422 std::unique_ptr<TestGlobalWebStateObserver> observer( | 524 std::unique_ptr<TestGlobalWebStateObserver> observer( |
| 423 new TestGlobalWebStateObserver()); | 525 new TestGlobalWebStateObserver()); |
| 424 | 526 |
| 425 // Test that NavigationItemsPruned() is called. | 527 // Test that NavigationItemsPruned() is called. |
| 426 EXPECT_FALSE(observer->navigation_items_pruned_called()); | 528 EXPECT_FALSE(observer->navigation_items_pruned_called()); |
| 427 web_state_->OnNavigationItemsPruned(1); | 529 web_state_->OnNavigationItemsPruned(1); |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 625 "</script>"); | 727 "</script>"); |
| 626 | 728 |
| 627 base::test::ios::WaitUntilCondition(^{ | 729 base::test::ios::WaitUntilCondition(^{ |
| 628 return message_received; | 730 return message_received; |
| 629 }); | 731 }); |
| 630 web_state_->RemoveScriptCommandCallback("test"); | 732 web_state_->RemoveScriptCommandCallback("test"); |
| 631 } | 733 } |
| 632 | 734 |
| 633 } // namespace | 735 } // namespace |
| 634 } // namespace web | 736 } // namespace web |
| OLD | NEW |