Commit 35b0f446 authored by Olivier Robin's avatar Olivier Robin Committed by Commit Bot

Add unittests for [AutofillAgent onFormDataFilled:]

onFormDataFilled can have some non trivial behavior particularly when
there are multiple fields with the same name.

Bug: None
Change-Id: I89e5eacf658cf7c82b0c6dd01725f497b1c856e1
Reviewed-on: https://chromium-review.googlesource.com/822073
Commit-Queue: Olivier Robin <olivierrobin@chromium.org>
Reviewed-by: default avatarRobert Sesek <rsesek@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarRoger McFarlane <rogerm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#524381}
parent 0b5cc4e2
...@@ -166,6 +166,7 @@ test("components_unittests") { ...@@ -166,6 +166,7 @@ test("components_unittests") {
if (is_ios) { if (is_ios) {
deps += [ deps += [
"//components/autofill/ios/browser:unit_tests",
"//components/image_fetcher/ios:unit_tests", "//components/image_fetcher/ios:unit_tests",
"//components/signin/ios/browser:unit_tests", "//components/signin/ios/browser:unit_tests",
"//components/translate/ios/browser:unit_tests", "//components/translate/ios/browser:unit_tests",
......
...@@ -65,3 +65,22 @@ js_compile_checked("injected_js") { ...@@ -65,3 +65,22 @@ js_compile_checked("injected_js") {
"resources/suggestion_controller.js", "resources/suggestion_controller.js",
] ]
} }
source_set("unit_tests") {
testonly = true
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
"autofill_agent_unittests.mm",
]
deps = [
":browser",
"//base",
"//components/autofill/core/browser:test_support",
"//components/prefs",
"//ios/web/public",
"//ios/web/public/test/fakes",
"//testing/gmock",
"//testing/gtest",
"//third_party/ocmock",
]
}
include_rules = [
"+ios/web/public",
"+third_party/ocmock",
]
// 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 "components/autofill/ios/browser/autofill_agent.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/common/form_data.h"
#import "components/autofill/ios/browser/js_autofill_manager.h"
#include "components/prefs/pref_service.h"
#import "ios/web/public/test/fakes/test_web_state.h"
#import "ios/web/public/web_state/js/crw_js_injection_receiver.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#include "third_party/ocmock/gtest_support.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
class MockWebState : public web::TestWebState {
public:
MockWebState() : js_injection_receiver_(nullptr) {}
~MockWebState() override {}
CRWJSInjectionReceiver* GetJSInjectionReceiver() const override {
return js_injection_receiver_;
}
void SetJSInjectionReceiver(CRWJSInjectionReceiver* receiver) {
js_injection_receiver_ = receiver;
}
private:
CRWJSInjectionReceiver* js_injection_receiver_;
};
// Test fixture for AutofillAgent testing.
class AutofillAgentTests : public PlatformTest {
public:
AutofillAgentTests() {}
void SetUp() override {
PlatformTest::SetUp();
// Mock out the JsAutofillManager.
mock_js_autofill_manager_ =
[OCMockObject mockForClass:[JsAutofillManager class]];
mock_web_state_ = std::make_unique<MockWebState>();
id mock_js_injection_receiver =
[OCMockObject mockForClass:[CRWJSInjectionReceiver class]];
mock_web_state_->SetJSInjectionReceiver(mock_js_injection_receiver);
// Set expectations for setting the JsCastSenderManager instance.
[[[mock_js_autofill_manager_ stub] andReturnValue:@YES]
isKindOfClass:[JsAutofillManager class]];
[[[mock_js_injection_receiver expect] andReturn:mock_js_autofill_manager_]
instanceOfClass:[JsAutofillManager class]];
prefs_ = autofill::test::PrefServiceForTesting();
autofill_agent_ =
[[AutofillAgent alloc] initWithPrefService:prefs_.get()
webState:mock_web_state_.get()];
}
std::unique_ptr<MockWebState> mock_web_state_;
AutofillAgent* autofill_agent_;
std::unique_ptr<PrefService> prefs_;
// Mock JsSuggestionManager for verifying interactions.
id mock_js_autofill_manager_;
DISALLOW_COPY_AND_ASSIGN(AutofillAgentTests);
};
TEST_F(AutofillAgentTests, OnFormDataFilledTest) {
autofill::FormData form;
form.origin = GURL("https://myform.com");
form.action = GURL("https://myform.com/submit");
autofill::FormFieldData field;
field.form_control_type = "text";
field.label = base::ASCIIToUTF16("Card number");
field.name = base::ASCIIToUTF16("number");
field.value = base::ASCIIToUTF16("number_value");
form.fields.push_back(field);
field.label = base::ASCIIToUTF16("Name on Card");
field.name = base::ASCIIToUTF16("name");
field.value = base::ASCIIToUTF16("name_value");
form.fields.push_back(field);
field.label = base::ASCIIToUTF16("Unknown field");
field.name = base::ASCIIToUTF16("unknown");
field.value = base::ASCIIToUTF16("");
form.fields.push_back(field);
// Fields are in alphabetical order.
[[mock_js_autofill_manager_ expect] fillForm:
@"{\"fields\":{"
"\"name\":\"name_value\","
"\"number\":\"number_value\","
"\"unknown\":\"\""
"},\"formName\":\"\"}"
forceFillFieldName:@""
completionHandler:[OCMArg any]];
[autofill_agent_ onFormDataFilled:form];
mock_web_state_->WasShown();
EXPECT_OCMOCK_VERIFY(mock_js_autofill_manager_);
}
TEST_F(AutofillAgentTests, OnFormDataFilledWithNameCollisionTest) {
autofill::FormData form;
form.origin = GURL("https://myform.com");
form.action = GURL("https://myform.com/submit");
autofill::FormFieldData field;
// Check that in case of conflict, the last value of a given field is used.
field.form_control_type = "text";
field.label = base::ASCIIToUTF16("State");
field.name = base::ASCIIToUTF16("region");
field.value = base::ASCIIToUTF16("California");
form.fields.push_back(field);
field.label = base::ASCIIToUTF16("Province");
field.name = base::ASCIIToUTF16("region");
field.value = base::ASCIIToUTF16("");
form.fields.push_back(field);
field.label = base::ASCIIToUTF16("Other field");
field.name = base::ASCIIToUTF16("field1");
field.value = base::ASCIIToUTF16("value 1");
form.fields.push_back(field);
field.label = base::ASCIIToUTF16("Other field");
field.name = base::ASCIIToUTF16("field1");
field.value = base::ASCIIToUTF16("value 2");
form.fields.push_back(field);
// Fields are in alphabetical order.
[[mock_js_autofill_manager_ expect] fillForm:
@"{\"fields\":{"
"\"field1\":\"value 2\","
"\"region\":\"\""
"},\"formName\":\"\"}"
forceFillFieldName:@""
completionHandler:[OCMArg any]];
[autofill_agent_ onFormDataFilled:form];
mock_web_state_->WasShown();
EXPECT_OCMOCK_VERIFY(mock_js_autofill_manager_);
}
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