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/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
16 #import "base/test/ios/wait_util.h" | 16 #import "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 #import "ios/web/public/java_script_dialog_presenter.h" |
19 #include "ios/web/public/load_committed_details.h" | 19 #include "ios/web/public/load_committed_details.h" |
20 #include "ios/web/public/test/fakes/test_browser_state.h" | 20 #include "ios/web/public/test/fakes/test_browser_state.h" |
| 21 #import "ios/web/public/test/fakes/test_web_state_delegate.h" |
21 #include "ios/web/public/test/web_test.h" | 22 #include "ios/web/public/test/web_test.h" |
22 #import "ios/web/public/web_state/context_menu_params.h" | 23 #import "ios/web/public/web_state/context_menu_params.h" |
23 #include "ios/web/public/web_state/global_web_state_observer.h" | 24 #include "ios/web/public/web_state/global_web_state_observer.h" |
24 #include "ios/web/public/web_state/web_state_delegate.h" | 25 #include "ios/web/public/web_state/web_state_delegate.h" |
25 #include "ios/web/public/web_state/web_state_observer.h" | 26 #include "ios/web/public/web_state/web_state_observer.h" |
26 #import "ios/web/public/web_state/web_state_policy_decider.h" | 27 #import "ios/web/public/web_state/web_state_policy_decider.h" |
27 #include "ios/web/web_state/global_web_state_event_tracker.h" | 28 #include "ios/web/web_state/global_web_state_event_tracker.h" |
28 #import "ios/web/web_state/ui/crw_web_controller.h" | 29 #import "ios/web/web_state/ui/crw_web_controller.h" |
29 #include "net/http/http_response_headers.h" | 30 #include "net/http/http_response_headers.h" |
30 #include "net/http/http_util.h" | 31 #include "net/http/http_util.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 108 |
108 bool navigation_items_pruned_called_; | 109 bool navigation_items_pruned_called_; |
109 bool navigation_item_changed_called_; | 110 bool navigation_item_changed_called_; |
110 bool navigation_item_committed_called_; | 111 bool navigation_item_committed_called_; |
111 bool did_start_loading_called_; | 112 bool did_start_loading_called_; |
112 bool did_stop_loading_called_; | 113 bool did_stop_loading_called_; |
113 bool page_loaded_called_with_success_; | 114 bool page_loaded_called_with_success_; |
114 bool web_state_destroyed_called_; | 115 bool web_state_destroyed_called_; |
115 }; | 116 }; |
116 | 117 |
117 // Test delegate to check that the WebStateDelegate methods are called as | |
118 // expected. | |
119 class TestWebStateDelegate : public WebStateDelegate { | |
120 public: | |
121 TestWebStateDelegate() | |
122 : load_progress_changed_called_(false), | |
123 handle_context_menu_called_(false), | |
124 get_java_script_dialog_presenter_called_(false) {} | |
125 | |
126 // True if the WebStateDelegate LoadProgressChanged method has been called. | |
127 bool load_progress_changed_called() const { | |
128 return load_progress_changed_called_; | |
129 } | |
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 | |
146 private: | |
147 // WebStateDelegate implementation: | |
148 void LoadProgressChanged(WebState* source, double progress) override { | |
149 load_progress_changed_called_ = true; | |
150 } | |
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 | |
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_dialogs_called_(false), run_java_script_dialog_called_(false) {} | |
176 | |
177 // True if the JavaScriptDialogPresenter CancelDialogs method has been called. | |
178 bool cancel_dialogs_called() const { return cancel_dialogs_called_; } | |
179 | |
180 // True if the JavaScriptDialogPresenter RunJavaScriptDialog method has been | |
181 // called. | |
182 bool run_java_script_dialog_called() const { | |
183 return run_java_script_dialog_called_; | |
184 } | |
185 | |
186 private: | |
187 // JavaScriptDialogPresenter implementation: | |
188 void RunJavaScriptDialog(WebState* web_state, | |
189 const GURL& origin_url, | |
190 JavaScriptDialogType java_script_dialog_type, | |
191 NSString* message_text, | |
192 NSString* default_prompt_text, | |
193 const DialogClosedCallback& callback) override { | |
194 run_java_script_dialog_called_ = true; | |
195 callback.Run(false, nil); | |
196 } | |
197 | |
198 void CancelDialogs(WebState* web_state) override { | |
199 cancel_dialogs_called_ = true; | |
200 } | |
201 | |
202 bool cancel_dialogs_called_; | |
203 bool run_java_script_dialog_called_; | |
204 }; | |
205 | |
206 // Test observer to check that the WebStateObserver methods are called as | 118 // Test observer to check that the WebStateObserver methods are called as |
207 // expected. | 119 // expected. |
208 class TestWebStateObserver : public WebStateObserver { | 120 class TestWebStateObserver : public WebStateObserver { |
209 public: | 121 public: |
210 TestWebStateObserver(WebState* web_state) | 122 TestWebStateObserver(WebState* web_state) |
211 : WebStateObserver(web_state), | 123 : WebStateObserver(web_state), |
212 provisional_navigation_started_called_(false), | 124 provisional_navigation_started_called_(false), |
213 navigation_items_pruned_called_(false), | 125 navigation_items_pruned_called_(false), |
214 navigation_item_changed_called_(false), | 126 navigation_item_changed_called_(false), |
215 navigation_item_committed_called_(false), | 127 navigation_item_committed_called_(false), |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 web_state_->SendChangeLoadProgress(0.0); | 411 web_state_->SendChangeLoadProgress(0.0); |
500 EXPECT_TRUE(delegate.load_progress_changed_called()); | 412 EXPECT_TRUE(delegate.load_progress_changed_called()); |
501 | 413 |
502 // Test that HandleContextMenu() is called. | 414 // Test that HandleContextMenu() is called. |
503 EXPECT_FALSE(delegate.handle_context_menu_called()); | 415 EXPECT_FALSE(delegate.handle_context_menu_called()); |
504 web::ContextMenuParams context_menu_params; | 416 web::ContextMenuParams context_menu_params; |
505 web_state_->HandleContextMenu(context_menu_params); | 417 web_state_->HandleContextMenu(context_menu_params); |
506 EXPECT_TRUE(delegate.handle_context_menu_called()); | 418 EXPECT_TRUE(delegate.handle_context_menu_called()); |
507 | 419 |
508 // Test that GetJavaScriptDialogPresenter() is called. | 420 // Test that GetJavaScriptDialogPresenter() is called. |
509 TestJavaScriptDialogPresenter presenter; | 421 TestJavaScriptDialogPresenter* presenter = |
510 delegate.SetJavaScriptDialogPresenter(&presenter); | 422 delegate.GetTestJavaScriptDialogPresenter(); |
511 | |
512 EXPECT_FALSE(delegate.get_java_script_dialog_presenter_called()); | 423 EXPECT_FALSE(delegate.get_java_script_dialog_presenter_called()); |
513 EXPECT_FALSE(presenter.run_java_script_dialog_called()); | 424 EXPECT_TRUE(presenter->requested_dialogs().empty()); |
514 EXPECT_FALSE(presenter.cancel_dialogs_called()); | 425 EXPECT_FALSE(presenter->cancel_dialogs_called()); |
515 | 426 |
516 __block bool callback_called = false; | 427 __block bool callback_called = false; |
517 web_state_->RunJavaScriptDialog(GURL(), JAVASCRIPT_DIALOG_TYPE_ALERT, @"", | 428 web_state_->RunJavaScriptDialog(GURL(), JAVASCRIPT_DIALOG_TYPE_ALERT, @"", |
518 nil, base::BindBlock(^(bool, NSString*) { | 429 nil, base::BindBlock(^(bool, NSString*) { |
519 callback_called = true; | 430 callback_called = true; |
520 })); | 431 })); |
521 | 432 |
522 EXPECT_TRUE(delegate.get_java_script_dialog_presenter_called()); | 433 EXPECT_TRUE(delegate.get_java_script_dialog_presenter_called()); |
523 EXPECT_TRUE(presenter.run_java_script_dialog_called()); | 434 EXPECT_EQ(1U, presenter->requested_dialogs().size()); |
524 EXPECT_TRUE(callback_called); | 435 EXPECT_TRUE(callback_called); |
525 | 436 |
526 EXPECT_FALSE(presenter.cancel_dialogs_called()); | 437 EXPECT_FALSE(presenter->cancel_dialogs_called()); |
527 web_state_->CancelDialogs(); | 438 web_state_->CancelDialogs(); |
528 EXPECT_TRUE(presenter.cancel_dialogs_called()); | 439 EXPECT_TRUE(presenter->cancel_dialogs_called()); |
529 } | 440 } |
530 | 441 |
531 // Verifies that GlobalWebStateObservers are called when expected. | 442 // Verifies that GlobalWebStateObservers are called when expected. |
532 TEST_F(WebStateTest, GlobalObserverTest) { | 443 TEST_F(WebStateTest, GlobalObserverTest) { |
533 std::unique_ptr<TestGlobalWebStateObserver> observer( | 444 std::unique_ptr<TestGlobalWebStateObserver> observer( |
534 new TestGlobalWebStateObserver()); | 445 new TestGlobalWebStateObserver()); |
535 | 446 |
536 // Test that NavigationItemsPruned() is called. | 447 // Test that NavigationItemsPruned() is called. |
537 EXPECT_FALSE(observer->navigation_items_pruned_called()); | 448 EXPECT_FALSE(observer->navigation_items_pruned_called()); |
538 web_state_->OnNavigationItemsPruned(1); | 449 web_state_->OnNavigationItemsPruned(1); |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
736 "</script>"); | 647 "</script>"); |
737 | 648 |
738 base::test::ios::WaitUntilCondition(^{ | 649 base::test::ios::WaitUntilCondition(^{ |
739 return message_received; | 650 return message_received; |
740 }); | 651 }); |
741 web_state_->RemoveScriptCommandCallback("test"); | 652 web_state_->RemoveScriptCommandCallback("test"); |
742 } | 653 } |
743 | 654 |
744 } // namespace | 655 } // namespace |
745 } // namespace web | 656 } // namespace web |
OLD | NEW |