Home | History | Annotate | Download | only in browser
      1 // Copyright 2014 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 #include "components/autofill/core/browser/address_i18n.h"
      6 
      7 #include <string>
      8 #include <vector>
      9 
     10 #include "base/guid.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "components/autofill/core/browser/autofill_profile.h"
     13 #include "components/autofill/core/browser/autofill_test_utils.h"
     14 #include "components/autofill/core/browser/field_types.h"
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
     17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_field.h"
     18 
     19 namespace autofill {
     20 namespace i18n {
     21 
     22 using ::i18n::addressinput::AddressData;
     23 using ::i18n::addressinput::AddressField;
     24 
     25 using ::i18n::addressinput::ADMIN_AREA;
     26 using ::i18n::addressinput::COUNTRY;
     27 using ::i18n::addressinput::DEPENDENT_LOCALITY;
     28 using ::i18n::addressinput::LOCALITY;
     29 using ::i18n::addressinput::ORGANIZATION;
     30 using ::i18n::addressinput::POSTAL_CODE;
     31 using ::i18n::addressinput::RECIPIENT;
     32 using ::i18n::addressinput::SORTING_CODE;
     33 using ::i18n::addressinput::STREET_ADDRESS;
     34 
     35 TEST(AddressI18nTest, FieldTypeMirrorConversions) {
     36   static const struct {
     37     bool billing;
     38     ServerFieldType server_field;
     39     AddressField address_field;
     40   } kTestData[] = {
     41     {true, ADDRESS_BILLING_COUNTRY, COUNTRY},
     42     {true, ADDRESS_BILLING_STATE, ADMIN_AREA},
     43     {true, ADDRESS_BILLING_CITY, LOCALITY},
     44     {true, ADDRESS_BILLING_DEPENDENT_LOCALITY, DEPENDENT_LOCALITY},
     45     {true, ADDRESS_BILLING_SORTING_CODE, SORTING_CODE},
     46     {true, ADDRESS_BILLING_ZIP, POSTAL_CODE},
     47     {true, ADDRESS_BILLING_STREET_ADDRESS, STREET_ADDRESS},
     48     {true, COMPANY_NAME, ORGANIZATION},
     49     {true, NAME_BILLING_FULL, RECIPIENT},
     50     {false, ADDRESS_HOME_COUNTRY, COUNTRY},
     51     {false, ADDRESS_HOME_STATE, ADMIN_AREA},
     52     {false, ADDRESS_HOME_CITY, LOCALITY},
     53     {false, ADDRESS_HOME_DEPENDENT_LOCALITY, DEPENDENT_LOCALITY},
     54     {false, ADDRESS_HOME_SORTING_CODE, SORTING_CODE},
     55     {false, ADDRESS_HOME_ZIP, POSTAL_CODE},
     56     {false, ADDRESS_HOME_STREET_ADDRESS, STREET_ADDRESS},
     57     {false, COMPANY_NAME, ORGANIZATION},
     58     {false, NAME_FULL, RECIPIENT},
     59   };
     60 
     61   for (size_t i = 0; i < arraysize(kTestData); ++i) {
     62     AddressField address_field;
     63     EXPECT_TRUE(FieldForType(kTestData[i].server_field, &address_field));
     64     EXPECT_EQ(kTestData[i].address_field, address_field);
     65 
     66     ServerFieldType server_field =
     67         TypeForField(kTestData[i].address_field, kTestData[i].billing);
     68     EXPECT_EQ(kTestData[i].server_field, server_field);
     69   }
     70 }
     71 
     72 TEST(AddressI18nTest, FieldTypeUnidirectionalConversions) {
     73   static const struct {
     74     ServerFieldType server_field;
     75     AddressField expected_address_field;
     76   } kTestData[] = {
     77     {ADDRESS_BILLING_LINE1, STREET_ADDRESS},
     78     {ADDRESS_BILLING_LINE2, STREET_ADDRESS},
     79     {ADDRESS_HOME_LINE1, STREET_ADDRESS},
     80     {ADDRESS_HOME_LINE2, STREET_ADDRESS},
     81   };
     82 
     83   for (size_t i = 0; i < arraysize(kTestData); ++i) {
     84     AddressField actual_address_field;
     85     FieldForType(kTestData[i].server_field, &actual_address_field);
     86     EXPECT_EQ(kTestData[i].expected_address_field, actual_address_field);
     87   }
     88 }
     89 
     90 TEST(AddressI18nTest, UnconvertableServerFields) {
     91   EXPECT_FALSE(FieldForType(PHONE_HOME_NUMBER, NULL));
     92   EXPECT_FALSE(FieldForType(EMAIL_ADDRESS, NULL));
     93 }
     94 
     95 TEST(AddressI18nTest, CreateAddressDataFromAutofillProfile) {
     96   AutofillProfile profile(base::GenerateGUID(), "http://www.example.com/");
     97   test::SetProfileInfo(&profile,
     98                        "John",
     99                        "H.",
    100                        "Doe",
    101                        "johndoe (at) hades.com",
    102                        "Underworld",
    103                        "666 Erebus St.",
    104                        "Apt 8",
    105                        "Elysium", "CA",
    106                        "91111",
    107                        "US",
    108                        "16502111111");
    109   profile.set_language_code("en");
    110   scoped_ptr<AddressData> actual =
    111       CreateAddressDataFromAutofillProfile(profile, "en_US");
    112 
    113   AddressData expected;
    114   expected.region_code = "US";
    115   expected.address_line.push_back("666 Erebus St.");
    116   expected.address_line.push_back("Apt 8");
    117   expected.administrative_area = "CA";
    118   expected.locality = "Elysium";
    119   expected.postal_code = "91111";
    120   expected.language_code = "en";
    121   expected.organization = "Underworld";
    122   expected.recipient = "John H. Doe";
    123 
    124   EXPECT_EQ(expected, *actual);
    125 }
    126 
    127 }  // namespace i18n
    128 }  // namespace autofill
    129