Commit 5a122b34 authored by John Z Wu's avatar John Z Wu Committed by Commit Bot

Cleanup CWVAutofillControllerTest

- Use more mocks
- Remove old autoreleasepool work around

Bug: 1070657
Change-Id: Ic234ddb2e9be28048a39c365ec8c2e09eaf38d74
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2147882
Commit-Queue: John Wu <jzw@chromium.org>
Reviewed-by: default avatarHiroshi Ichikawa <ichikawa@chromium.org>
Cr-Commit-Position: refs/heads/master@{#759111}
parent 43511d1f
......@@ -124,7 +124,8 @@ fetchNonPasswordSuggestionsForFormWithName:(NSString*)formName
autofillAgent:(AutofillAgent*)autofillAgent
JSAutofillManager:(JsAutofillManager*)JSAutofillManager
JSSuggestionManager:(JsSuggestionManager*)JSSuggestionManager
passwordController:(CWVPasswordController*)passwordController {
passwordController:(CWVPasswordController*)passwordController
applicationLocale:(const std::string&)applicationLocale {
self = [super init];
if (self) {
DCHECK(webState);
......@@ -143,8 +144,7 @@ fetchNonPasswordSuggestionsForFormWithName:(NSString*)formName
_autofillClient->set_bridge(self);
autofill::AutofillDriverIOS::PrepareForWebStateWebFrameAndDelegate(
_webState, _autofillClient.get(), self,
ios_web_view::ApplicationContext::GetInstance()->GetApplicationLocale(),
_webState, _autofillClient.get(), self, applicationLocale,
autofill::AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER);
_JSAutofillManager = JSAutofillManager;
......
......@@ -6,6 +6,7 @@
#define IOS_WEB_VIEW_INTERNAL_AUTOFILL_CWV_AUTOFILL_CONTROLLER_INTERNAL_H_
#include <memory>
#include <string>
#import "ios/web_view/public/cwv_autofill_controller.h"
......@@ -34,6 +35,7 @@ class WebState;
JSAutofillManager:(JsAutofillManager*)JSAutofillManager
JSSuggestionManager:(JsSuggestionManager*)JSSuggestionManager
passwordController:(CWVPasswordController*)passwordController
applicationLocale:(const std::string&)applicationLocale
NS_DESIGNATED_INITIALIZER;
@end
......
......@@ -12,6 +12,10 @@
#include "base/strings/sys_string_conversions.h"
#import "base/test/ios/wait_util.h"
#include "components/autofill/core/browser/autofill_manager.h"
#include "components/autofill/core/browser/logging/stub_log_manager.h"
#include "components/autofill/core/browser/payments/test_strike_database.h"
#include "components/autofill/core/browser/test_personal_data_manager.h"
#include "components/autofill/core/common/autofill_prefs.h"
#import "components/autofill/ios/browser/fake_autofill_agent.h"
#import "components/autofill/ios/browser/fake_js_autofill_manager.h"
#import "components/autofill/ios/browser/form_suggestion.h"
......@@ -20,14 +24,17 @@
#import "components/autofill/ios/form_util/form_activity_tab_helper.h"
#import "components/autofill/ios/form_util/test_form_activity_tab_helper.h"
#include "components/password_manager/core/browser/password_manager.h"
#include "components/password_manager/core/common/password_manager_pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/sync/driver/test_sync_service.h"
#import "ios/web/public/deprecated/crw_test_js_injection_receiver.h"
#include "ios/web/public/js_messaging/web_frames_manager.h"
#import "ios/web/public/test/fakes/fake_web_frame.h"
#import "ios/web/public/test/fakes/fake_web_frames_manager.h"
#include "ios/web/public/test/fakes/test_browser_state.h"
#import "ios/web/public/test/fakes/test_web_state.h"
#include "ios/web/public/test/scoped_testing_web_client.h"
#include "ios/web/public/test/web_task_environment.h"
#include "ios/web/public/web_client.h"
#import "ios/web_view/internal/autofill/cwv_autofill_suggestion_internal.h"
#import "ios/web_view/internal/autofill/web_view_autofill_client_ios.h"
#import "ios/web_view/internal/passwords/cwv_password_controller_fake.h"
......@@ -35,7 +42,6 @@
#import "ios/web_view/internal/passwords/web_view_password_manager_driver.h"
#include "ios/web_view/internal/web_view_browser_state.h"
#import "ios/web_view/public/cwv_autofill_controller_delegate.h"
#include "ios/web_view/test/test_with_locale_and_resources.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
......@@ -52,6 +58,7 @@ namespace ios_web_view {
namespace {
const char kApplicationLocale[] = "en-US";
NSString* const kTestFormName = @"FormName";
NSString* const kTestFieldIdentifier = @"FieldIdentifier";
NSString* const kTestFrameId = @"FrameID";
......@@ -60,76 +67,85 @@ NSString* const kTestDisplayDescription = @"DisplayDescription";
} // namespace
class CWVAutofillControllerTest : public TestWithLocaleAndResources {
class CWVAutofillControllerTest : public PlatformTest {
protected:
CWVAutofillControllerTest()
: web_client_(std::make_unique<web::WebClient>()),
task_environment_(web::WebTaskEnvironment::IO_MAINLOOP),
browser_state_(/*off_the_record=*/false) {
test_web_state_.SetBrowserState(&browser_state_);
CWVAutofillControllerTest() {
pref_service_.registry()->RegisterBooleanPref(
password_manager::prefs::kCredentialsEnableService, true);
pref_service_.registry()->RegisterBooleanPref(
autofill::prefs::kAutofillProfileEnabled, true);
web_state_.SetBrowserState(&browser_state_);
CRWTestJSInjectionReceiver* injectionReceiver =
[[CRWTestJSInjectionReceiver alloc] init];
test_web_state_.SetJSInjectionReceiver(injectionReceiver);
web_state_.SetJSInjectionReceiver(injectionReceiver);
js_autofill_manager_ =
[[FakeJSAutofillManager alloc] initWithReceiver:injectionReceiver];
js_suggestion_manager_ = OCMClassMock([JsSuggestionManager class]);
autofill_agent_ =
[[FakeAutofillAgent alloc] initWithPrefService:browser_state_.GetPrefs()
webState:&test_web_state_];
[[FakeAutofillAgent alloc] initWithPrefService:&pref_service_
webState:&web_state_];
auto frames_manager = std::make_unique<web::FakeWebFramesManager>();
fake_web_frames_manager_ = frames_manager.get();
test_web_state_.SetWebFramesManager(std::move(frames_manager));
web_frames_manager_ = frames_manager.get();
web_state_.SetWebFramesManager(std::move(frames_manager));
// TODO(crbug.com/1070468): Redo CWVPasswordController so it is easier to
// fake in unit tests.
auto passwordManagerClient = std::make_unique<WebViewPasswordManagerClient>(
&test_web_state_, /*sync_service=*/nullptr, browser_state_.GetPrefs(),
/*identity_manager=*/nullptr, /*log_manager=*/nullptr,
/*profile_store=*/nullptr, /*account_store=*/nullptr);
auto passwordManager = std::make_unique<password_manager::PasswordManager>(
passwordManagerClient.get());
auto passwordManagerDriver =
auto password_manager_client =
std::make_unique<WebViewPasswordManagerClient>(
&web_state_, /*sync_service=*/nullptr, &pref_service_,
/*identity_manager=*/nullptr, /*log_manager=*/nullptr,
/*profile_store=*/nullptr, /*account_store=*/nullptr);
auto password_manager = std::make_unique<password_manager::PasswordManager>(
password_manager_client.get());
auto password_manager_driver =
std::make_unique<WebViewPasswordManagerDriver>();
password_controller_ = [[CWVPasswordControllerFake alloc]
initWithWebState:&test_web_state_
passwordManager:std::move(passwordManager)
passwordManagerClient:std::move(passwordManagerClient)
passwordManagerDriver:std::move(passwordManagerDriver)];
// TODO(crbug.com/1070657): Fake the dependencies of the autofill client.
auto autofill_client = autofill::WebViewAutofillClientIOS::Create(
&test_web_state_, &browser_state_);
initWithWebState:&web_state_
passwordManager:std::move(password_manager)
passwordManagerClient:std::move(password_manager_client)
passwordManagerDriver:std::move(password_manager_driver)];
auto autofill_client = std::make_unique<autofill::WebViewAutofillClientIOS>(
kApplicationLocale, &pref_service_, &personal_data_manager_,
/*autocomplete_history_manager=*/nullptr, &web_state_,
/*identity_manager=*/nullptr, &strike_database_, &sync_service_,
std::make_unique<autofill::StubLogManager>());
autofill_controller_ = [[CWVAutofillController alloc]
initWithWebState:&test_web_state_
initWithWebState:&web_state_
autofillClient:std::move(autofill_client)
autofillAgent:autofill_agent_
JSAutofillManager:js_autofill_manager_
JSSuggestionManager:js_suggestion_manager_
passwordController:password_controller_];
test_form_activity_tab_helper_ =
std::make_unique<autofill::TestFormActivityTabHelper>(&test_web_state_);
passwordController:password_controller_
applicationLocale:kApplicationLocale];
form_activity_tab_helper_ =
std::make_unique<autofill::TestFormActivityTabHelper>(&web_state_);
}
void AddWebFrame(std::unique_ptr<web::WebFrame> frame) {
web::WebFrame* frame_ptr = frame.get();
fake_web_frames_manager_->AddWebFrame(std::move(frame));
test_web_state_.OnWebFrameDidBecomeAvailable(frame_ptr);
web_frames_manager_->AddWebFrame(std::move(frame));
web_state_.OnWebFrameDidBecomeAvailable(frame_ptr);
}
web::ScopedTestingWebClient web_client_;
web::WebTaskEnvironment task_environment_;
ios_web_view::WebViewBrowserState browser_state_;
web::TestWebState test_web_state_;
web::FakeWebFramesManager* fake_web_frames_manager_;
TestingPrefServiceSimple pref_service_;
web::TestBrowserState browser_state_;
web::TestWebState web_state_;
autofill::TestPersonalDataManager personal_data_manager_;
autofill::TestStrikeDatabase strike_database_;
syncer::TestSyncService sync_service_;
web::FakeWebFramesManager* web_frames_manager_;
CWVAutofillController* autofill_controller_;
FakeAutofillAgent* autofill_agent_;
FakeJSAutofillManager* js_autofill_manager_;
CWVPasswordControllerFake* password_controller_;
std::unique_ptr<autofill::TestFormActivityTabHelper>
test_form_activity_tab_helper_;
form_activity_tab_helper_;
id js_suggestion_manager_;
};
......@@ -297,30 +313,25 @@ TEST_F(CWVAutofillControllerTest, FocusCallback) {
id delegate = OCMProtocolMock(@protocol(CWVAutofillControllerDelegate));
autofill_controller_.delegate = delegate;
// [delegate expect] returns an autoreleased object, but it must be
// destroyed before this test exits to avoid holding on to
// |autofill_controller_|.
@autoreleasepool {
[[delegate expect] autofillController:autofill_controller_
didFocusOnFieldWithIdentifier:kTestFieldIdentifier
fieldType:@""
formName:kTestFormName
frameID:kTestFrameId
value:kTestFieldValue
userInitiated:YES];
autofill::FormActivityParams params;
params.form_name = base::SysNSStringToUTF8(kTestFormName);
params.field_identifier = base::SysNSStringToUTF8(kTestFieldIdentifier);
params.value = base::SysNSStringToUTF8(kTestFieldValue);
params.frame_id = base::SysNSStringToUTF8(kTestFrameId);
params.has_user_gesture = true;
params.type = "focus";
web::FakeWebFrame frame(base::SysNSStringToUTF8(kTestFrameId), true,
GURL::EmptyGURL());
test_form_activity_tab_helper_->FormActivityRegistered(&frame, params);
[delegate verify];
}
[[delegate expect] autofillController:autofill_controller_
didFocusOnFieldWithIdentifier:kTestFieldIdentifier
fieldType:@""
formName:kTestFormName
frameID:kTestFrameId
value:kTestFieldValue
userInitiated:YES];
autofill::FormActivityParams params;
params.form_name = base::SysNSStringToUTF8(kTestFormName);
params.field_identifier = base::SysNSStringToUTF8(kTestFieldIdentifier);
params.value = base::SysNSStringToUTF8(kTestFieldValue);
params.frame_id = base::SysNSStringToUTF8(kTestFrameId);
params.has_user_gesture = true;
params.type = "focus";
web::FakeWebFrame frame(base::SysNSStringToUTF8(kTestFrameId), true,
GURL::EmptyGURL());
form_activity_tab_helper_->FormActivityRegistered(&frame, params);
[delegate verify];
}
// Tests CWVAutofillController delegate input callback is invoked.
......@@ -328,30 +339,25 @@ TEST_F(CWVAutofillControllerTest, InputCallback) {
id delegate = OCMProtocolMock(@protocol(CWVAutofillControllerDelegate));
autofill_controller_.delegate = delegate;
// [delegate expect] returns an autoreleased object, but it must be
// destroyed before this test exits to avoid holding on to
// |autofill_controller_|.
@autoreleasepool {
[[delegate expect] autofillController:autofill_controller_
didInputInFieldWithIdentifier:kTestFieldIdentifier
fieldType:@""
formName:kTestFormName
frameID:kTestFrameId
value:kTestFieldValue
userInitiated:YES];
autofill::FormActivityParams params;
params.form_name = base::SysNSStringToUTF8(kTestFormName);
params.field_identifier = base::SysNSStringToUTF8(kTestFieldIdentifier);
params.value = base::SysNSStringToUTF8(kTestFieldValue);
params.frame_id = base::SysNSStringToUTF8(kTestFrameId);
params.type = "input";
params.has_user_gesture = true;
web::FakeWebFrame frame(base::SysNSStringToUTF8(kTestFrameId), true,
GURL::EmptyGURL());
test_form_activity_tab_helper_->FormActivityRegistered(&frame, params);
[delegate verify];
}
[[delegate expect] autofillController:autofill_controller_
didInputInFieldWithIdentifier:kTestFieldIdentifier
fieldType:@""
formName:kTestFormName
frameID:kTestFrameId
value:kTestFieldValue
userInitiated:YES];
autofill::FormActivityParams params;
params.form_name = base::SysNSStringToUTF8(kTestFormName);
params.field_identifier = base::SysNSStringToUTF8(kTestFieldIdentifier);
params.value = base::SysNSStringToUTF8(kTestFieldValue);
params.frame_id = base::SysNSStringToUTF8(kTestFrameId);
params.type = "input";
params.has_user_gesture = true;
web::FakeWebFrame frame(base::SysNSStringToUTF8(kTestFrameId), true,
GURL::EmptyGURL());
form_activity_tab_helper_->FormActivityRegistered(&frame, params);
[delegate verify];
}
// Tests CWVAutofillController delegate blur callback is invoked.
......@@ -359,9 +365,6 @@ TEST_F(CWVAutofillControllerTest, BlurCallback) {
id delegate = OCMProtocolMock(@protocol(CWVAutofillControllerDelegate));
autofill_controller_.delegate = delegate;
// [delegate expect] returns an autoreleased object, but it must be destroyed
// before this test exits to avoid holding on to |autofill_controller_|.
@autoreleasepool {
[[delegate expect] autofillController:autofill_controller_
didBlurOnFieldWithIdentifier:kTestFieldIdentifier
fieldType:@""
......@@ -379,10 +382,9 @@ TEST_F(CWVAutofillControllerTest, BlurCallback) {
params.has_user_gesture = true;
web::FakeWebFrame frame(base::SysNSStringToUTF8(kTestFrameId), true,
GURL::EmptyGURL());
test_form_activity_tab_helper_->FormActivityRegistered(&frame, params);
form_activity_tab_helper_->FormActivityRegistered(&frame, params);
[delegate verify];
}
}
// Tests CWVAutofillController delegate submit callback is invoked.
......@@ -390,16 +392,13 @@ TEST_F(CWVAutofillControllerTest, SubmitCallback) {
id delegate = OCMProtocolMock(@protocol(CWVAutofillControllerDelegate));
autofill_controller_.delegate = delegate;
// [delegate expect] returns an autoreleased object, but it must be destroyed
// before this test exits to avoid holding on to |autofill_controller_|.
@autoreleasepool {
[[delegate expect] autofillController:autofill_controller_
didSubmitFormWithName:kTestFormName
frameID:kTestFrameId
userInitiated:YES];
web::FakeWebFrame frame(base::SysNSStringToUTF8(kTestFrameId), true,
GURL::EmptyGURL());
test_form_activity_tab_helper_->DocumentSubmitted(
form_activity_tab_helper_->DocumentSubmitted(
/*sender_frame*/ &frame, base::SysNSStringToUTF8(kTestFormName),
/*form_data=*/"",
/*user_initiated=*/true,
......@@ -410,14 +409,13 @@ TEST_F(CWVAutofillControllerTest, SubmitCallback) {
frameID:kTestFrameId
userInitiated:NO];
test_form_activity_tab_helper_->DocumentSubmitted(
form_activity_tab_helper_->DocumentSubmitted(
/*sender_frame*/ &frame, base::SysNSStringToUTF8(kTestFormName),
/*form_data=*/"",
/*user_initiated=*/false,
/*is_main_frame=*/true);
[delegate verify];
}
}
} // namespace ios_web_view
......@@ -38,6 +38,7 @@
#import "ios/web/public/web_state_delegate_bridge.h"
#import "ios/web/public/web_state_observer_bridge.h"
#import "ios/web/public/web_view_only/wk_web_view_configuration_util.h"
#include "ios/web_view/internal/app/application_context.h"
#import "ios/web_view/internal/autofill/cwv_autofill_controller_internal.h"
#import "ios/web_view/internal/autofill/web_view_autofill_client_ios.h"
#import "ios/web_view/internal/cwv_back_forward_list_internal.h"
......@@ -663,13 +664,15 @@ BOOL gChromeLongPressAndForceTouchHandlingEnabled = YES;
passwordManagerClient:std::move(passwordManagerClient)
passwordManagerDriver:std::move(passwordManagerDriver)];
return
[[CWVAutofillController alloc] initWithWebState:_webState.get()
autofillClient:std::move(autofillClient)
autofillAgent:autofillAgent
JSAutofillManager:JSAutofillManager
JSSuggestionManager:JSSuggestionManager
passwordController:passwordController];
return [[CWVAutofillController alloc]
initWithWebState:_webState.get()
autofillClient:std::move(autofillClient)
autofillAgent:autofillAgent
JSAutofillManager:JSAutofillManager
JSSuggestionManager:JSSuggestionManager
passwordController:passwordController
applicationLocale:ios_web_view::ApplicationContext::GetInstance()
->GetApplicationLocale()];
}
#pragma mark - Preserving and Restoring State
......
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