Commit 4b8c7297 authored by eugenebut's avatar eugenebut Committed by Commit bot

[ios] Made TestWebStateDelegate and TestJavaScriptDialogPresenter reusable.

These fakes will be resued in CRWWebControllerPageDialogOpenPolicyTest
instead of CRWWebUserInterfaceDelegate mock.

BUG=661445
TRR=kkhorimoto@chromium.org

Review-Url: https://codereview.chromium.org/2615093004
Cr-Commit-Position: refs/heads/master@{#441991}
parent 5a126a5c
......@@ -383,6 +383,8 @@ source_set("test_support") {
"public/test/fakes/crw_test_js_injection_receiver.mm",
"public/test/fakes/test_browser_state.cc",
"public/test/fakes/test_browser_state.h",
"public/test/fakes/test_java_script_dialog_presenter.h",
"public/test/fakes/test_java_script_dialog_presenter.mm",
"public/test/fakes/test_native_content.h",
"public/test/fakes/test_native_content.mm",
"public/test/fakes/test_native_content_provider.h",
......@@ -393,6 +395,8 @@ source_set("test_support") {
"public/test/fakes/test_web_client.mm",
"public/test/fakes/test_web_state.h",
"public/test/fakes/test_web_state.mm",
"public/test/fakes/test_web_state_delegate.h",
"public/test/fakes/test_web_state_delegate.mm",
"public/test/fakes/test_web_view_content_view.h",
"public/test/fakes/test_web_view_content_view.mm",
"public/test/http_server.h",
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_WEB_PUBLIC_TEST_FAKES_TEST_JAVA_SCRIPT_DIALOG_PRESENTER_H_
#define IOS_WEB_PUBLIC_TEST_FAKES_TEST_JAVA_SCRIPT_DIALOG_PRESENTER_H_
#include <vector>
#import "ios/web/public/java_script_dialog_presenter.h"
#import "base/mac/scoped_nsobject.h"
namespace web {
struct TestJavaScriptDialog {
TestJavaScriptDialog();
TestJavaScriptDialog(const TestJavaScriptDialog&);
~TestJavaScriptDialog();
WebState* web_state = nullptr;
GURL origin_url;
JavaScriptDialogType java_script_dialog_type;
base::scoped_nsobject<NSString> message_text;
base::scoped_nsobject<NSString> default_prompt_text;
};
// Test presenter to check that the JavaScriptDialogPresenter methods are called
// as expected. |RunJavaScriptDialog| always calls |callback| with
// |callback_success_argument| and |callback_user_input_argument| values.
class TestJavaScriptDialogPresenter : public JavaScriptDialogPresenter {
public:
TestJavaScriptDialogPresenter();
~TestJavaScriptDialogPresenter();
// JavaScriptDialogPresenter overrides:
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;
void CancelDialogs(WebState* web_state) override;
// True if the JavaScriptDialogPresenter CancelDialogs method has been called.
bool cancel_dialogs_called() const { return cancel_dialogs_called_; }
// Returns a vector of requested dialogs.
const std::vector<TestJavaScriptDialog>& requested_dialogs() const {
return requested_dialogs_;
}
// Sets |success| argument to be used for RunJavaScriptDialog callback.
void set_callback_success_argument(bool success) {
callback_success_argument_ = success;
}
// Sets |user_input| argument to be used for RunJavaScriptDialog callback.
void set_callback_user_input_argument(NSString* user_input) {
callback_user_input_argument_.reset(user_input);
}
private:
bool cancel_dialogs_called_ = false;
std::vector<TestJavaScriptDialog> requested_dialogs_;
bool callback_success_argument_ = false;
base::scoped_nsobject<NSString> callback_user_input_argument_;
};
} // namespace web
#endif // IOS_WEB_PUBLIC_TEST_FAKES_TEST_JAVA_SCRIPT_DIALOG_PRESENTER_H_
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/web/public/test/fakes/test_java_script_dialog_presenter.h"
namespace web {
TestJavaScriptDialog::TestJavaScriptDialog() = default;
TestJavaScriptDialog::TestJavaScriptDialog(const TestJavaScriptDialog&) =
default;
TestJavaScriptDialog::~TestJavaScriptDialog() = default;
TestJavaScriptDialogPresenter::TestJavaScriptDialogPresenter() = default;
TestJavaScriptDialogPresenter::~TestJavaScriptDialogPresenter() = default;
void TestJavaScriptDialogPresenter::RunJavaScriptDialog(
WebState* web_state,
const GURL& origin_url,
JavaScriptDialogType java_script_dialog_type,
NSString* message_text,
NSString* default_prompt_text,
const DialogClosedCallback& callback) {
TestJavaScriptDialog dialog;
dialog.web_state = web_state;
dialog.origin_url = origin_url;
dialog.java_script_dialog_type = java_script_dialog_type;
dialog.message_text.reset(message_text);
dialog.default_prompt_text.reset(default_prompt_text);
requested_dialogs_.push_back(dialog);
callback.Run(callback_success_argument_, callback_user_input_argument_.get());
}
void TestJavaScriptDialogPresenter::CancelDialogs(WebState* web_state) {
cancel_dialogs_called_ = true;
}
} // namespace web
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_WEB_PUBLIC_TEST_FAKES_TEST_WEB_STATE_DELEGATE_H_
#define IOS_WEB_PUBLIC_TEST_FAKES_TEST_WEB_STATE_DELEGATE_H_
#include "ios/web/public/web_state/web_state_delegate.h"
#import "ios/web/public/test/fakes/test_java_script_dialog_presenter.h"
namespace web {
// Fake WebStateDelegate used for testing purposes.
class TestWebStateDelegate : public WebStateDelegate {
public:
// WebStateDelegate overrides:
JavaScriptDialogPresenter* GetJavaScriptDialogPresenter(WebState*) override;
void LoadProgressChanged(WebState* source, double progress) override;
bool HandleContextMenu(WebState* source,
const ContextMenuParams& params) override;
TestJavaScriptDialogPresenter* GetTestJavaScriptDialogPresenter();
// 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_;
}
private:
bool load_progress_changed_called_ = false;
bool handle_context_menu_called_ = false;
bool get_java_script_dialog_presenter_called_ = false;
TestJavaScriptDialogPresenter java_script_dialog_presenter_;
};
} // namespace web
#endif // IOS_WEB_PUBLIC_TEST_FAKES_TEST_WEB_STATE_DELEGATE_H_
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/web/public/test/fakes/test_web_state_delegate.h"
namespace web {
JavaScriptDialogPresenter* TestWebStateDelegate::GetJavaScriptDialogPresenter(
WebState*) {
get_java_script_dialog_presenter_called_ = true;
return &java_script_dialog_presenter_;
}
void TestWebStateDelegate::LoadProgressChanged(WebState*, double) {
load_progress_changed_called_ = true;
}
bool TestWebStateDelegate::HandleContextMenu(WebState*,
const ContextMenuParams&) {
handle_context_menu_called_ = true;
return NO;
}
TestJavaScriptDialogPresenter*
TestWebStateDelegate::GetTestJavaScriptDialogPresenter() {
return &java_script_dialog_presenter_;
}
} // namespace web
......@@ -18,6 +18,7 @@
#import "ios/web/public/java_script_dialog_presenter.h"
#include "ios/web/public/load_committed_details.h"
#include "ios/web/public/test/fakes/test_browser_state.h"
#import "ios/web/public/test/fakes/test_web_state_delegate.h"
#include "ios/web/public/test/web_test.h"
#import "ios/web/public/web_state/context_menu_params.h"
#include "ios/web/public/web_state/global_web_state_observer.h"
......@@ -114,95 +115,6 @@ class TestGlobalWebStateObserver : public GlobalWebStateObserver {
bool web_state_destroyed_called_;
};
// Test delegate to check that the WebStateDelegate methods are called as
// expected.
class TestWebStateDelegate : public WebStateDelegate {
public:
TestWebStateDelegate()
: load_progress_changed_called_(false),
handle_context_menu_called_(false),
get_java_script_dialog_presenter_called_(false) {}
// 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:
// 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_dialogs_called_(false), run_java_script_dialog_called_(false) {}
// True if the JavaScriptDialogPresenter CancelDialogs method has been called.
bool cancel_dialogs_called() const { return cancel_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 CancelDialogs(WebState* web_state) override {
cancel_dialogs_called_ = true;
}
bool cancel_dialogs_called_;
bool run_java_script_dialog_called_;
};
// Test observer to check that the WebStateObserver methods are called as
// expected.
class TestWebStateObserver : public WebStateObserver {
......@@ -506,12 +418,11 @@ TEST_F(WebStateTest, DelegateTest) {
EXPECT_TRUE(delegate.handle_context_menu_called());
// Test that GetJavaScriptDialogPresenter() is called.
TestJavaScriptDialogPresenter presenter;
delegate.SetJavaScriptDialogPresenter(&presenter);
TestJavaScriptDialogPresenter* presenter =
delegate.GetTestJavaScriptDialogPresenter();
EXPECT_FALSE(delegate.get_java_script_dialog_presenter_called());
EXPECT_FALSE(presenter.run_java_script_dialog_called());
EXPECT_FALSE(presenter.cancel_dialogs_called());
EXPECT_TRUE(presenter->requested_dialogs().empty());
EXPECT_FALSE(presenter->cancel_dialogs_called());
__block bool callback_called = false;
web_state_->RunJavaScriptDialog(GURL(), JAVASCRIPT_DIALOG_TYPE_ALERT, @"",
......@@ -520,12 +431,12 @@ TEST_F(WebStateTest, DelegateTest) {
}));
EXPECT_TRUE(delegate.get_java_script_dialog_presenter_called());
EXPECT_TRUE(presenter.run_java_script_dialog_called());
EXPECT_EQ(1U, presenter->requested_dialogs().size());
EXPECT_TRUE(callback_called);
EXPECT_FALSE(presenter.cancel_dialogs_called());
EXPECT_FALSE(presenter->cancel_dialogs_called());
web_state_->CancelDialogs();
EXPECT_TRUE(presenter.cancel_dialogs_called());
EXPECT_TRUE(presenter->cancel_dialogs_called());
}
// Verifies that GlobalWebStateObservers are called when expected.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment