address_unittest.cc revision 9ab5563a3196760eb381d102cbb2bc0f7abc6a50
1// Copyright 2013 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 <string>
6
7#include "base/message_loop/message_loop.h"
8#include "base/strings/string16.h"
9#include "base/strings/utf_string_conversions.h"
10#include "components/autofill/core/browser/address.h"
11#include "components/autofill/core/browser/autofill_type.h"
12#include "content/public/test/test_browser_thread.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15using content::BrowserThread;
16
17namespace autofill {
18
19class AddressTest : public testing::Test {
20 public:
21  // In order to access the application locale -- which the tested functions do
22  // internally -- this test must run on the UI thread.
23  AddressTest() : ui_thread_(BrowserThread::UI, &message_loop_) {}
24
25 private:
26  base::MessageLoopForUI message_loop_;
27  content::TestBrowserThread ui_thread_;
28
29  DISALLOW_COPY_AND_ASSIGN(AddressTest);
30};
31
32// Test that country codes are properly decoded as country names.
33TEST_F(AddressTest, GetCountry) {
34  Address address;
35  EXPECT_EQ(base::string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
36
37  // Make sure that nothing breaks when the country code is missing.
38  base::string16 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
39  EXPECT_EQ(base::string16(), country);
40
41  address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US");
42  country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
43  EXPECT_EQ(ASCIIToUTF16("United States"), country);
44
45  address.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("CA"));
46  country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
47  EXPECT_EQ(ASCIIToUTF16("Canada"), country);
48}
49
50// Test that we properly detect country codes appropriate for each country.
51TEST_F(AddressTest, SetCountry) {
52  Address address;
53  EXPECT_EQ(base::string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
54
55  // Test basic conversion.
56  address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("United States"), "en-US");
57  base::string16 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
58  EXPECT_EQ(ASCIIToUTF16("US"), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
59  EXPECT_EQ(ASCIIToUTF16("United States"), country);
60
61  // Test basic synonym detection.
62  address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("USA"), "en-US");
63  country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
64  EXPECT_EQ(ASCIIToUTF16("US"), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
65  EXPECT_EQ(ASCIIToUTF16("United States"), country);
66
67  // Test case-insensitivity.
68  address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("canADA"), "en-US");
69  country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
70  EXPECT_EQ(ASCIIToUTF16("CA"), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
71  EXPECT_EQ(ASCIIToUTF16("Canada"), country);
72
73  // Test country code detection.
74  address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("JP"), "en-US");
75  country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
76  EXPECT_EQ(ASCIIToUTF16("JP"), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
77  EXPECT_EQ(ASCIIToUTF16("Japan"), country);
78
79  // Test that we ignore unknown countries.
80  address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("Unknown"), "en-US");
81  country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
82  EXPECT_EQ(base::string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
83  EXPECT_EQ(base::string16(), country);
84}
85
86// Test that we properly match typed values to stored country data.
87TEST_F(AddressTest, IsCountry) {
88  Address address;
89  address.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
90
91  const char* const kValidMatches[] = {
92    "United States",
93    "USA",
94    "US",
95    "United states",
96    "us"
97  };
98  for (size_t i = 0; i < arraysize(kValidMatches); ++i) {
99    SCOPED_TRACE(kValidMatches[i]);
100    FieldTypeSet matching_types;
101    address.GetMatchingTypes(ASCIIToUTF16(kValidMatches[i]), "US",
102                             &matching_types);
103    ASSERT_EQ(1U, matching_types.size());
104    EXPECT_EQ(ADDRESS_HOME_COUNTRY, *matching_types.begin());
105  }
106
107  const char* const kInvalidMatches[] = {
108    "United",
109    "Garbage"
110  };
111  for (size_t i = 0; i < arraysize(kInvalidMatches); ++i) {
112    FieldTypeSet matching_types;
113    address.GetMatchingTypes(ASCIIToUTF16(kInvalidMatches[i]), "US",
114                             &matching_types);
115    EXPECT_EQ(0U, matching_types.size());
116  }
117
118  // Make sure that garbage values don't match when the country code is empty.
119  address.SetRawInfo(ADDRESS_HOME_COUNTRY, base::string16());
120  EXPECT_EQ(base::string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
121  FieldTypeSet matching_types;
122  address.GetMatchingTypes(ASCIIToUTF16("Garbage"), "US", &matching_types);
123  EXPECT_EQ(0U, matching_types.size());
124}
125
126}  // namespace autofill
127