Commit 24d8d0c2 authored by David Jean's avatar David Jean Committed by Commit Bot

[ios] Add pure obj-c address for manual fallback

Bug: 845472
Change-Id: Icb239377ea417d68c46d4a8bf4bc38ddf11b1b42
Reviewed-on: https://chromium-review.googlesource.com/c/1333810
Commit-Queue: David Jean <djean@chromium.org>
Reviewed-by: default avatarJavier Ernesto Flores Robles <javierrobles@chromium.org>
Cr-Commit-Position: refs/heads/master@{#607953}
parent e5dd3494
......@@ -8,6 +8,8 @@ source_set("manual_fill") {
sources = [
"address_coordinator.h",
"address_coordinator.mm",
"address_form.h",
"address_form.mm",
"address_mediator.h",
"address_mediator.mm",
"card_coordinator.h",
......@@ -62,6 +64,8 @@ source_set("manual_fill_ui") {
sources = [
"action_cell.h",
"action_cell.mm",
"address.h",
"address.mm",
"address_consumer.h",
"address_list_delegate.h",
"address_view_controller.h",
......@@ -127,6 +131,8 @@ source_set("unit_tests") {
configs += [ "//build/config/compiler:enable_arc" ]
testonly = true
sources = [
"address_form_unittest.mm",
"address_unittest.mm",
"credential_password_form_unittest.mm",
"credential_unittest.mm",
"credit_card_form_unittest.mm",
......@@ -141,8 +147,9 @@ source_set("unit_tests") {
"//components/autofill/core/common",
"//components/autofill/ios/form_util:form_util",
"//components/autofill/ios/form_util:test_support",
"//ios/chrome/browser/web_state_list",
"//ios/chrome/browser",
"//ios/chrome/browser/web_state_list:test_support",
"//ios/chrome/browser/web_state_list:web_state_list",
"//ios/web/public/test/fakes",
"//testing/gtest:gtest",
"//third_party/ocmock:ocmock",
......
// Copyright 2018 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_CHROME_BROWSER_UI_AUTOFILL_MANUAL_FILL_ADDRESS_H_
#define IOS_CHROME_BROWSER_UI_AUTOFILL_MANUAL_FILL_ADDRESS_H_
#import <Foundation/Foundation.h>
// This represents an address to use with manual fill.
@interface ManualFillAddress : NSObject
// The addressee's first name.
@property(nonatomic, readonly) NSString* firstName;
// The addressee's middle name or middle initial, or empty.
@property(nonatomic, readonly) NSString* middleNameOrInitial;
// The addressee's last name.
@property(nonatomic, readonly) NSString* lastName;
// The first line of this address.
@property(nonatomic, readonly) NSString* line1;
// The second, optional, line of this address.
@property(nonatomic, readonly) NSString* line2;
// The zip code of the address.
@property(nonatomic, readonly) NSString* zip;
// The city of the address.
@property(nonatomic, readonly) NSString* city;
// The state or province of the address.
@property(nonatomic, readonly) NSString* state;
// The country of the address.
@property(nonatomic, readonly) NSString* country;
// Default init.
- (instancetype)initWithFirstName:(NSString*)firstName
middleNameOrInitial:(NSString*)middleNameOrInitial
lastName:(NSString*)lastName
line1:(NSString*)line1
line2:(NSString*)line2
zip:(NSString*)zip
city:(NSString*)city
state:(NSString*)state
country:(NSString*)country NS_DESIGNATED_INITIALIZER;
// Unavailable. Please use |initWithFirstName:middleNameOrInitial:lastName:
// line1:line2:zip:city:state:country:|.
- (instancetype)init NS_UNAVAILABLE;
@end
#endif // IOS_CHROME_BROWSER_UI_AUTOFILL_MANUAL_FILL_ADDRESS_H_
// Copyright 2018 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/chrome/browser/ui/autofill/manual_fill/address.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@implementation ManualFillAddress
- (instancetype)initWithFirstName:(NSString*)firstName
middleNameOrInitial:(NSString*)middleNameOrInitial
lastName:(NSString*)lastName
line1:(NSString*)line1
line2:(NSString*)line2
zip:(NSString*)zip
city:(NSString*)city
state:(NSString*)state
country:(NSString*)country {
self = [super init];
if (self) {
_firstName = [firstName copy];
_middleNameOrInitial = [middleNameOrInitial copy];
_lastName = [lastName copy];
_line1 = [line1 copy];
_line2 = [line2 copy];
_zip = [zip copy];
_city = [city copy];
_state = [state copy];
_country = [country copy];
}
return self;
}
- (BOOL)isEqual:(id)object {
if (!object) {
return NO;
}
if (self == object) {
return YES;
}
if (![object isMemberOfClass:[ManualFillAddress class]]) {
return NO;
}
ManualFillAddress* otherObject = (ManualFillAddress*)object;
if (![otherObject.firstName isEqual:self.firstName]) {
return NO;
}
if (![otherObject.middleNameOrInitial isEqual:self.middleNameOrInitial]) {
return NO;
}
if (![otherObject.lastName isEqual:self.lastName]) {
return NO;
}
if (![otherObject.line1 isEqual:self.line1]) {
return NO;
}
if (![otherObject.line2 isEqual:self.line2]) {
return NO;
}
if (![otherObject.zip isEqual:self.zip]) {
return NO;
}
if (![otherObject.city isEqual:self.city]) {
return NO;
}
if (![otherObject.state isEqual:self.state]) {
return NO;
}
if (![otherObject.country isEqual:self.country]) {
return NO;
}
return YES;
}
- (NSUInteger)hash {
return [self.firstName hash] ^ [self.middleNameOrInitial hash] ^
[self.lastName hash] ^ [self.line1 hash] ^ [self.line2 hash] ^
[self.zip hash] ^ [self.city hash] ^ [self.state hash] ^
[self.country hash];
}
- (NSString*)description {
return
[NSString stringWithFormat:
@"<%@ (%p): firstName: %@, middleNameOrInitial: %@, "
@"lastName: %@, line1: %@, "
@"line2: %@, zip: %@, city: %@, state: %@, country: %@>",
NSStringFromClass([self class]), self, self.firstName,
self.middleNameOrInitial, self.lastName, self.line1,
self.line2, self.zip, self.city, self.state, self.country];
}
@end
// Copyright 2018 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_CHROME_BROWSER_UI_AUTOFILL_MANUAL_FILL_ADDRESS_FORM_H_
#define IOS_CHROME_BROWSER_UI_AUTOFILL_MANUAL_FILL_ADDRESS_FORM_H_
#import "ios/chrome/browser/ui/autofill/manual_fill/address.h"
namespace autofill {
class AutofillProfile;
}
// Extends |ManualFillAddress| with a convenience initializer from c++
// |autofill::AutofillProfile|.
@interface ManualFillAddress (AutofillProfile)
// Convenience initializer from an autofill::AutofillProfile.
- (instancetype)initWithProfile:(const autofill::AutofillProfile&)profile;
@end
#endif // IOS_CHROME_BROWSER_UI_AUTOFILL_MANUAL_FILL_ADDRESS_FORM_H_
// Copyright 2018 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/chrome/browser/ui/autofill/manual_fill/address_form.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "ios/chrome/browser/application_context.h"
#include "url/gurl.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
// Takes in an autofill profile and an autofill field type and returns the
// corresponding field value.
NSString* FieldValueOfTypeOnProfile(const autofill::AutofillProfile& profile,
autofill::ServerFieldType fieldType) {
return base::SysUTF16ToNSString(
profile.GetInfo(autofill::AutofillType(fieldType),
GetApplicationContext()->GetApplicationLocale()));
}
} // namespace
@implementation ManualFillAddress (AutofillProfile)
- (instancetype)initWithProfile:(const autofill::AutofillProfile&)profile {
NSString* firstName =
FieldValueOfTypeOnProfile(profile, autofill::NAME_FIRST);
NSString* middleNameOrInitial =
FieldValueOfTypeOnProfile(profile, autofill::NAME_MIDDLE);
if (!middleNameOrInitial || middleNameOrInitial.length == 0) {
middleNameOrInitial =
FieldValueOfTypeOnProfile(profile, autofill::NAME_MIDDLE_INITIAL);
}
NSString* lastName = FieldValueOfTypeOnProfile(profile, autofill::NAME_LAST);
NSString* line1 =
FieldValueOfTypeOnProfile(profile, autofill::ADDRESS_HOME_LINE1);
NSString* line2 =
FieldValueOfTypeOnProfile(profile, autofill::ADDRESS_HOME_LINE2);
NSString* zip =
FieldValueOfTypeOnProfile(profile, autofill::ADDRESS_HOME_ZIP);
NSString* city =
FieldValueOfTypeOnProfile(profile, autofill::ADDRESS_HOME_CITY);
NSString* state =
FieldValueOfTypeOnProfile(profile, autofill::ADDRESS_HOME_STATE);
NSString* country =
FieldValueOfTypeOnProfile(profile, autofill::ADDRESS_HOME_COUNTRY);
return [self initWithFirstName:firstName
middleNameOrInitial:middleNameOrInitial
lastName:lastName
line1:line1
line2:line2
zip:zip
city:city
state:state
country:country];
}
@end
// Copyright 2018 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/chrome/browser/ui/autofill/manual_fill/address_form.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/country_names.h"
#include "ios/chrome/browser/application_context.h"
#include "testing/platform_test.h"
#include "url/gurl.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
using autofill::AutofillProfile;
using ManualFillAddressFormAutofilliOSTest = PlatformTest;
namespace {
void SetProfileFieldTypeValue(AutofillProfile* profile,
const autofill::ServerFieldType fieldType,
NSString* value) {
const base::string16 v = base::SysNSStringToUTF16(value);
const std::string& app_locale =
GetApplicationContext()->GetApplicationLocale();
profile->SetInfo(fieldType, v, app_locale);
}
} // namespace
// Tests the creation of an address from an autofill::AutofillProfile.
TEST_F(ManualFillAddressFormAutofilliOSTest, CreationWithMiddleName) {
NSString* firstName = @"First";
NSString* middleName = @"Middle";
NSString* lastName = @"Last";
NSString* line1 = @"10 Main Street";
NSString* line2 = @"Appt 16";
NSString* zip = @"12345";
NSString* city = @"Springfield";
NSString* state = @"State";
NSString* country = @"US";
autofill::CountryNames::SetLocaleString("en-US");
AutofillProfile* profile = new AutofillProfile();
SetProfileFieldTypeValue(profile, autofill::NAME_FIRST, firstName);
SetProfileFieldTypeValue(profile, autofill::NAME_MIDDLE, middleName);
SetProfileFieldTypeValue(profile, autofill::NAME_LAST, lastName);
SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_LINE1, line1);
SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_LINE2, line2);
SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_ZIP, zip);
SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_CITY, city);
SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_STATE, state);
SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_COUNTRY, country);
ManualFillAddress* manualFillAddress =
[[ManualFillAddress alloc] initWithProfile:*profile];
EXPECT_TRUE(manualFillAddress);
EXPECT_TRUE([firstName isEqualToString:manualFillAddress.firstName]);
EXPECT_TRUE(
[middleName isEqualToString:manualFillAddress.middleNameOrInitial]);
EXPECT_TRUE([lastName isEqualToString:manualFillAddress.lastName]);
EXPECT_TRUE([line1 isEqualToString:manualFillAddress.line1]);
EXPECT_TRUE([line2 isEqualToString:manualFillAddress.line2]);
EXPECT_TRUE([zip isEqualToString:manualFillAddress.zip]);
EXPECT_TRUE([city isEqualToString:manualFillAddress.city]);
EXPECT_TRUE([state isEqualToString:manualFillAddress.state]);
EXPECT_TRUE([@"United States" isEqualToString:manualFillAddress.country]);
}
// Tests the creation of an address from an autofill::AutofillProfile.
TEST_F(ManualFillAddressFormAutofilliOSTest, CreationWithMiddleInitial) {
NSString* firstName = @"First";
NSString* middleInitial = @"M";
NSString* lastName = @"Last";
NSString* line1 = @"10 Main Street";
NSString* line2 = @"Appt 16";
NSString* zip = @"12345";
NSString* city = @"Springfield";
NSString* state = @"State";
NSString* country = @"US";
autofill::CountryNames::SetLocaleString("en-US");
AutofillProfile* profile = new AutofillProfile();
SetProfileFieldTypeValue(profile, autofill::NAME_FIRST, firstName);
SetProfileFieldTypeValue(profile, autofill::NAME_MIDDLE_INITIAL,
middleInitial);
SetProfileFieldTypeValue(profile, autofill::NAME_LAST, lastName);
SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_LINE1, line1);
SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_LINE2, line2);
SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_ZIP, zip);
SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_CITY, city);
SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_STATE, state);
SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_COUNTRY, country);
ManualFillAddress* manualFillAddress =
[[ManualFillAddress alloc] initWithProfile:*profile];
EXPECT_TRUE(manualFillAddress);
EXPECT_TRUE([firstName isEqualToString:manualFillAddress.firstName]);
EXPECT_TRUE(
[middleInitial isEqualToString:manualFillAddress.middleNameOrInitial]);
EXPECT_TRUE([lastName isEqualToString:manualFillAddress.lastName]);
EXPECT_TRUE([line1 isEqualToString:manualFillAddress.line1]);
EXPECT_TRUE([line2 isEqualToString:manualFillAddress.line2]);
EXPECT_TRUE([zip isEqualToString:manualFillAddress.zip]);
EXPECT_TRUE([city isEqualToString:manualFillAddress.city]);
EXPECT_TRUE([state isEqualToString:manualFillAddress.state]);
EXPECT_TRUE([@"United States" isEqualToString:manualFillAddress.country]);
}
// Copyright 2018 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/chrome/browser/ui/autofill/manual_fill/address.h"
#include "testing/platform_test.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
using ManualFillAddressiOSTest = PlatformTest;
// Tests that a credential is correctly created.
TEST_F(ManualFillAddressiOSTest, Creation) {
NSString* firstName = @"First";
NSString* middleNameOrInitial = @"M";
NSString* lastName = @"Last";
NSString* line1 = @"10 Main Street";
NSString* line2 = @"Appt 16";
NSString* zip = @"12345";
NSString* city = @"Springfield";
NSString* state = @"State";
NSString* country = @"Country";
ManualFillAddress* address =
[[ManualFillAddress alloc] initWithFirstName:firstName
middleNameOrInitial:middleNameOrInitial
lastName:lastName
line1:line1
line2:line2
zip:zip
city:city
state:state
country:country];
EXPECT_TRUE(address);
EXPECT_TRUE([firstName isEqualToString:address.firstName]);
EXPECT_TRUE(
[middleNameOrInitial isEqualToString:address.middleNameOrInitial]);
EXPECT_TRUE([lastName isEqualToString:address.lastName]);
EXPECT_TRUE([line1 isEqualToString:address.line1]);
EXPECT_TRUE([line2 isEqualToString:address.line2]);
EXPECT_TRUE([zip isEqualToString:address.zip]);
EXPECT_TRUE([city isEqualToString:address.city]);
EXPECT_TRUE([state isEqualToString:address.state]);
EXPECT_TRUE([country isEqualToString:address.country]);
}
// Test equality between addresses (lexicographically).
TEST_F(ManualFillAddressiOSTest, Equality) {
NSString* firstName = @"First";
NSString* middleNameOrInitial = @"M";
NSString* lastName = @"Last";
NSString* line1 = @"10 Main Street";
NSString* line2 = @"Appt 16";
NSString* zip = @"12345";
NSString* city = @"Springfield";
NSString* state = @"State";
NSString* country = @"Country";
ManualFillAddress* address =
[[ManualFillAddress alloc] initWithFirstName:firstName
middleNameOrInitial:middleNameOrInitial
lastName:lastName
line1:line1
line2:line2
zip:zip
city:city
state:state
country:country];
ManualFillAddress* equalAddress =
[[ManualFillAddress alloc] initWithFirstName:firstName
middleNameOrInitial:middleNameOrInitial
lastName:lastName
line1:line1
line2:line2
zip:zip
city:city
state:state
country:country];
EXPECT_TRUE([address isEqual:equalAddress]);
ManualFillAddress* differentAddressFirstName =
[[ManualFillAddress alloc] initWithFirstName:@"Bilbo"
middleNameOrInitial:middleNameOrInitial
lastName:lastName
line1:line1
line2:line2
zip:zip
city:city
state:state
country:country];
EXPECT_FALSE([address isEqual:differentAddressFirstName]);
ManualFillAddress* differentAddressMiddleNameOrInitial =
[[ManualFillAddress alloc] initWithFirstName:firstName
middleNameOrInitial:@"R"
lastName:lastName
line1:line1
line2:line2
zip:zip
city:city
state:state
country:country];
EXPECT_FALSE([address isEqual:differentAddressMiddleNameOrInitial]);
ManualFillAddress* differentAddressLastName =
[[ManualFillAddress alloc] initWithFirstName:firstName
middleNameOrInitial:middleNameOrInitial
lastName:@"Hobbit"
line1:line1
line2:line2
zip:zip
city:city
state:state
country:country];
EXPECT_FALSE([address isEqual:differentAddressLastName]);
ManualFillAddress* differentAddressLine1 =
[[ManualFillAddress alloc] initWithFirstName:firstName
middleNameOrInitial:middleNameOrInitial
lastName:lastName
line1:@"A House"
line2:line2
zip:zip
city:city
state:state
country:country];
EXPECT_FALSE([address isEqual:differentAddressLine1]);
ManualFillAddress* differentAddressLine2 =
[[ManualFillAddress alloc] initWithFirstName:firstName
middleNameOrInitial:middleNameOrInitial
lastName:lastName
line1:line1
line2:@""
zip:zip
city:city
state:state
country:country];
EXPECT_FALSE([address isEqual:differentAddressLine2]);
ManualFillAddress* differentAddressZip =
[[ManualFillAddress alloc] initWithFirstName:firstName
middleNameOrInitial:middleNameOrInitial
lastName:lastName
line1:line1
line2:line2
zip:@"1937"
city:city
state:state
country:country];
EXPECT_FALSE([address isEqual:differentAddressZip]);
ManualFillAddress* differentAddressCity =
[[ManualFillAddress alloc] initWithFirstName:firstName
middleNameOrInitial:middleNameOrInitial
lastName:lastName
line1:line1
line2:line2
zip:zip
city:@"Shire"
state:state
country:country];
EXPECT_FALSE([address isEqual:differentAddressCity]);
ManualFillAddress* differentAddressState =
[[ManualFillAddress alloc] initWithFirstName:firstName
middleNameOrInitial:middleNameOrInitial
lastName:lastName
line1:line1
line2:line2
zip:zip
city:city
state:@"Eriador"
country:country];
EXPECT_FALSE([address isEqual:differentAddressState]);
ManualFillAddress* differentAddressCountry =
[[ManualFillAddress alloc] initWithFirstName:firstName
middleNameOrInitial:middleNameOrInitial
lastName:lastName
line1:line1
line2:line2
zip:zip
city:city
state:state
country:@"Arnor"];
EXPECT_FALSE([address isEqual:differentAddressCountry]);
}
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