1// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import "ios/chrome/browser/ui/autofill/manual_fill/manual_fill_address+AutofillProfile.h"
6
7#include "base/strings/sys_string_conversions.h"
8#include "base/strings/utf_string_conversions.h"
9#include "components/autofill/core/browser/data_model/autofill_profile.h"
10#include "components/autofill/core/browser/geo/country_names.h"
11#include "ios/chrome/browser/application_context.h"
12#include "testing/platform_test.h"
13#include "url/gurl.h"
14
15#if !defined(__has_feature) || !__has_feature(objc_arc)
16#error "This file requires ARC support."
17#endif
18
19using autofill::AutofillProfile;
20using ManualFillAddressFormAutofilliOSTest = PlatformTest;
21
22namespace {
23
24void SetProfileFieldTypeValue(AutofillProfile* profile,
25                              const autofill::ServerFieldType fieldType,
26                              NSString* value) {
27  const base::string16 v = base::SysNSStringToUTF16(value);
28  const std::string& app_locale =
29      GetApplicationContext()->GetApplicationLocale();
30  profile->SetInfo(fieldType, v, app_locale);
31}
32
33}  // namespace
34
35// Tests the creation of an address from an autofill::AutofillProfile.
36TEST_F(ManualFillAddressFormAutofilliOSTest, CreationWithMiddleName) {
37  NSString* firstName = @"First";
38  NSString* middleName = @"Middle";
39  NSString* lastName = @"Last";
40  NSString* company = @"Google";
41  NSString* line1 = @"10 Main Street";
42  NSString* line2 = @"Appt 16";
43  NSString* zip = @"12345";
44  NSString* city = @"Springfield";
45  NSString* state = @"State";
46  NSString* country = @"US";
47  NSString* phoneNumber = @"123-456-789";
48  NSString* emailAddress = @"john@doe";
49
50  autofill::CountryNames::SetLocaleString("en-US");
51
52  AutofillProfile* profile = new AutofillProfile();
53  SetProfileFieldTypeValue(profile, autofill::NAME_FIRST, firstName);
54  SetProfileFieldTypeValue(profile, autofill::NAME_MIDDLE, middleName);
55  SetProfileFieldTypeValue(profile, autofill::NAME_LAST, lastName);
56  SetProfileFieldTypeValue(profile, autofill::COMPANY_NAME, company);
57  SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_LINE1, line1);
58  SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_LINE2, line2);
59  SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_ZIP, zip);
60  SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_CITY, city);
61  SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_STATE, state);
62  SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_COUNTRY, country);
63  SetProfileFieldTypeValue(profile, autofill::PHONE_HOME_WHOLE_NUMBER,
64                           phoneNumber);
65  SetProfileFieldTypeValue(profile, autofill::EMAIL_ADDRESS, emailAddress);
66
67  ManualFillAddress* manualFillAddress =
68      [[ManualFillAddress alloc] initWithProfile:*profile];
69
70  EXPECT_TRUE(manualFillAddress);
71  EXPECT_TRUE([firstName isEqualToString:manualFillAddress.firstName]);
72  EXPECT_TRUE(
73      [middleName isEqualToString:manualFillAddress.middleNameOrInitial]);
74  EXPECT_TRUE([lastName isEqualToString:manualFillAddress.lastName]);
75  EXPECT_TRUE([company isEqualToString:manualFillAddress.company]);
76  EXPECT_TRUE([line1 isEqualToString:manualFillAddress.line1]);
77  EXPECT_TRUE([line2 isEqualToString:manualFillAddress.line2]);
78  EXPECT_TRUE([zip isEqualToString:manualFillAddress.zip]);
79  EXPECT_TRUE([city isEqualToString:manualFillAddress.city]);
80  EXPECT_TRUE([state isEqualToString:manualFillAddress.state]);
81  EXPECT_TRUE([@"United States" isEqualToString:manualFillAddress.country]);
82  EXPECT_TRUE([phoneNumber isEqualToString:manualFillAddress.phoneNumber]);
83  EXPECT_TRUE([emailAddress isEqualToString:manualFillAddress.emailAddress]);
84}
85
86// Tests the creation of an address from an autofill::AutofillProfile.
87TEST_F(ManualFillAddressFormAutofilliOSTest, CreationWithMiddleInitial) {
88  NSString* firstName = @"First";
89  NSString* middleInitial = @"M";
90  NSString* lastName = @"Last";
91  NSString* company = @"Google";
92  NSString* line1 = @"10 Main Street";
93  NSString* line2 = @"Appt 16";
94  NSString* zip = @"12345";
95  NSString* city = @"Springfield";
96  NSString* state = @"State";
97  NSString* country = @"US";
98  NSString* phoneNumber = @"123-456-789";
99  NSString* emailAddress = @"john@doe";
100
101  autofill::CountryNames::SetLocaleString("en-US");
102
103  AutofillProfile* profile = new AutofillProfile();
104  SetProfileFieldTypeValue(profile, autofill::NAME_FIRST, firstName);
105  SetProfileFieldTypeValue(profile, autofill::NAME_MIDDLE_INITIAL,
106                           middleInitial);
107  SetProfileFieldTypeValue(profile, autofill::NAME_LAST, lastName);
108  SetProfileFieldTypeValue(profile, autofill::COMPANY_NAME, company);
109  SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_LINE1, line1);
110  SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_LINE2, line2);
111  SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_ZIP, zip);
112  SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_CITY, city);
113  SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_STATE, state);
114  SetProfileFieldTypeValue(profile, autofill::ADDRESS_HOME_COUNTRY, country);
115  SetProfileFieldTypeValue(profile, autofill::PHONE_HOME_WHOLE_NUMBER,
116                           phoneNumber);
117  SetProfileFieldTypeValue(profile, autofill::EMAIL_ADDRESS, emailAddress);
118
119  ManualFillAddress* manualFillAddress =
120      [[ManualFillAddress alloc] initWithProfile:*profile];
121
122  EXPECT_TRUE(manualFillAddress);
123  EXPECT_TRUE([firstName isEqualToString:manualFillAddress.firstName]);
124  EXPECT_TRUE(
125      [middleInitial isEqualToString:manualFillAddress.middleNameOrInitial]);
126  EXPECT_TRUE([lastName isEqualToString:manualFillAddress.lastName]);
127  EXPECT_TRUE([company isEqualToString:manualFillAddress.company]);
128  EXPECT_TRUE([line1 isEqualToString:manualFillAddress.line1]);
129  EXPECT_TRUE([line2 isEqualToString:manualFillAddress.line2]);
130  EXPECT_TRUE([zip isEqualToString:manualFillAddress.zip]);
131  EXPECT_TRUE([city isEqualToString:manualFillAddress.city]);
132  EXPECT_TRUE([state isEqualToString:manualFillAddress.state]);
133  EXPECT_TRUE([@"United States" isEqualToString:manualFillAddress.country]);
134  EXPECT_TRUE([phoneNumber isEqualToString:manualFillAddress.phoneNumber]);
135  EXPECT_TRUE([emailAddress isEqualToString:manualFillAddress.emailAddress]);
136}
137