Index: ios/web/web_state/web_state_impl_unittest.mm |
diff --git a/ios/web/web_state/web_state_impl_unittest.mm b/ios/web/web_state/web_state_impl_unittest.mm |
index 58a1b114d7eec5303319386f34db90baa55cb360..521540d7b2225b549e142a5c42baaf58033338ea 100644 |
--- a/ios/web/web_state/web_state_impl_unittest.mm |
+++ b/ios/web/web_state/web_state_impl_unittest.mm |
@@ -15,9 +15,11 @@ |
#include "base/strings/utf_string_conversions.h" |
#include "base/test/ios/wait_util.h" |
#include "base/values.h" |
+#import "ios/web/public/java_script_dialog_presenter.h" |
#include "ios/web/public/load_committed_details.h" |
#include "ios/web/public/test/test_browser_state.h" |
#include "ios/web/public/test/web_test.h" |
+#include "ios/web/public/web_state/context_menu_params.h" |
#include "ios/web/public/web_state/global_web_state_observer.h" |
#include "ios/web/public/web_state/web_state_delegate.h" |
#include "ios/web/public/web_state/web_state_observer.h" |
@@ -116,21 +118,93 @@ class TestGlobalWebStateObserver : public GlobalWebStateObserver { |
// expected. |
class TestWebStateDelegate : public WebStateDelegate { |
public: |
- TestWebStateDelegate() : load_progress_changed_called_(false) {} |
+ TestWebStateDelegate() |
+ : load_progress_changed_called_(false), |
+ handle_context_menu_called_(false), |
+ get_java_script_dialog_presenter_called_(false) {} |
- // Methods returning true if the corresponding WebStateObserver method has |
- // been called. |
+ // True if the WebStateDelegate LoadProgressChanged method has been called. |
bool load_progress_changed_called() const { |
return load_progress_changed_called_; |
} |
+ // True if the WebStateDelegate HandleContextMenu method has been called. |
+ bool handle_context_menu_called() const { |
+ return handle_context_menu_called_; |
+ } |
+ |
+ // True if the WebStateDelegate GetJavaScriptDialogPresenter method has been |
+ // called. |
+ bool get_java_script_dialog_presenter_called() const { |
+ return get_java_script_dialog_presenter_called_; |
+ } |
+ |
+ void SetJavaScriptDialogPresenter(JavaScriptDialogPresenter* presenter) { |
+ presenter_ = presenter; |
+ } |
+ |
private: |
- // WebStateObserver implementation: |
+ // WebStateDelegate implementation: |
void LoadProgressChanged(WebState* source, double progress) override { |
load_progress_changed_called_ = true; |
} |
+ bool HandleContextMenu(WebState* source, |
+ const ContextMenuParams& params) override { |
+ handle_context_menu_called_ = true; |
+ return NO; |
+ } |
+ |
+ JavaScriptDialogPresenter* GetJavaScriptDialogPresenter( |
+ WebState* source) override { |
+ get_java_script_dialog_presenter_called_ = true; |
+ return presenter_; |
+ } |
+ |
bool load_progress_changed_called_; |
+ bool handle_context_menu_called_; |
+ bool get_java_script_dialog_presenter_called_; |
+ JavaScriptDialogPresenter* presenter_; |
+}; |
+ |
+// Test presenter to check that the JavaScriptDialogPresenter methods are called |
+// as expected. |
+class TestJavaScriptDialogPresenter : public JavaScriptDialogPresenter { |
+ public: |
+ TestJavaScriptDialogPresenter() |
+ : cancel_active_and_pending_dialogs_called_(false), |
+ run_java_script_dialog_called_(false) {} |
+ |
+ // True if the JavaScriptDialogPresenter CancelActiveAndPendingDialogs method |
+ // has been called. |
+ bool cancel_active_and_pending_dialogs_called() const { |
+ return cancel_active_and_pending_dialogs_called_; |
+ } |
+ |
+ // True if the JavaScriptDialogPresenter RunJavaScriptDialog method has been |
+ // called. |
+ bool run_java_script_dialog_called() const { |
+ return run_java_script_dialog_called_; |
+ } |
+ |
+ private: |
+ // JavaScriptDialogPresenter implementation: |
+ void RunJavaScriptDialog(WebState* web_state, |
+ const GURL& origin_url, |
+ JavaScriptDialogType java_script_dialog_type, |
+ NSString* message_text, |
+ NSString* default_prompt_text, |
+ const DialogClosedCallback& callback) override { |
+ run_java_script_dialog_called_ = true; |
+ callback.Run(false, nil); |
+ } |
+ |
+ void CancelActiveAndPendingDialogs(WebState* web_state) override { |
+ cancel_active_and_pending_dialogs_called_ = true; |
+ } |
+ |
+ bool cancel_active_and_pending_dialogs_called_; |
+ bool run_java_script_dialog_called_; |
}; |
// Test observer to check that the WebStateObserver methods are called as |
@@ -415,6 +489,34 @@ TEST_F(WebStateTest, DelegateTest) { |
EXPECT_FALSE(delegate.load_progress_changed_called()); |
web_state_->SendChangeLoadProgress(0.0); |
EXPECT_TRUE(delegate.load_progress_changed_called()); |
+ |
+ // Test that HandleContextMenu() is called. |
+ EXPECT_FALSE(delegate.handle_context_menu_called()); |
+ web::ContextMenuParams context_menu_params; |
+ web_state_->HandleContextMenu(context_menu_params); |
+ EXPECT_TRUE(delegate.handle_context_menu_called()); |
+ |
+ // Test that GetJavaScriptDialogPresenter() is called. |
+ TestJavaScriptDialogPresenter presenter; |
+ delegate.SetJavaScriptDialogPresenter(&presenter); |
+ |
+ EXPECT_FALSE(delegate.get_java_script_dialog_presenter_called()); |
+ EXPECT_FALSE(presenter.run_java_script_dialog_called()); |
+ EXPECT_FALSE(presenter.cancel_active_and_pending_dialogs_called()); |
+ |
+ __block bool callback_called = false; |
+ web_state_->RunJavaScriptDialog(GURL(), JAVASCRIPT_DIALOG_TYPE_ALERT, @"", |
+ nil, base::BindBlock(^(bool, NSString*) { |
+ callback_called = true; |
+ })); |
+ |
+ EXPECT_TRUE(delegate.get_java_script_dialog_presenter_called()); |
+ EXPECT_TRUE(presenter.run_java_script_dialog_called()); |
+ EXPECT_TRUE(callback_called); |
+ |
+ EXPECT_FALSE(presenter.cancel_active_and_pending_dialogs_called()); |
+ web_state_->CancelActiveAndPendingDialogs(); |
+ EXPECT_TRUE(presenter.cancel_active_and_pending_dialogs_called()); |
} |
// Verifies that GlobalWebStateObservers are called when expected. |