blob: 0925146e63711ac66feb1b5ecb132066e8ad6ef5 [file] [log] [blame]
[email protected]377c3582012-06-14 01:56:181// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]35401022011-02-25 23:47:132// 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
[email protected]05871b32011-10-22 00:46:187#include "base/message_loop.h"
[email protected]35401022011-02-25 23:47:138#include "base/string16.h"
9#include "base/utf_string_conversions.h"
[email protected]e4b2fa32013-03-09 22:56:5610#include "components/autofill/browser/address.h"
11#include "components/autofill/browser/autofill_type.h"
[email protected]e97882f2012-06-04 02:23:1712#include "content/public/test/test_browser_thread.h"
[email protected]35401022011-02-25 23:47:1313#include "testing/gtest/include/gtest/gtest.h"
14
[email protected]631bb742011-11-02 11:29:3915using content::BrowserThread;
16
[email protected]377c3582012-06-14 01:56:1817class AddressTest : public testing::Test {
18 public:
19 // In order to access the application locale -- which the tested functions do
20 // internally -- this test must run on the UI thread.
21 AddressTest() : ui_thread_(BrowserThread::UI, &message_loop_) {}
22
23 private:
24 MessageLoopForUI message_loop_;
25 content::TestBrowserThread ui_thread_;
26
27 DISALLOW_COPY_AND_ASSIGN(AddressTest);
28};
29
[email protected]35401022011-02-25 23:47:1330// Test that the getters and setters for country code are working.
[email protected]377c3582012-06-14 01:56:1831TEST_F(AddressTest, CountryCode) {
[email protected]35401022011-02-25 23:47:1332 Address address;
33 EXPECT_EQ(std::string(), address.country_code());
34
35 address.set_country_code("US");
36 EXPECT_EQ("US", address.country_code());
37
38 address.set_country_code("CA");
39 EXPECT_EQ("CA", address.country_code());
40}
41
42// Test that country codes are properly decoded as country names.
[email protected]377c3582012-06-14 01:56:1843TEST_F(AddressTest, GetCountry) {
[email protected]35401022011-02-25 23:47:1344 Address address;
45 EXPECT_EQ(std::string(), address.country_code());
46
47 // Make sure that nothing breaks when the country code is missing.
[email protected]91b073d2013-01-05 01:30:2848 string16 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
[email protected]35401022011-02-25 23:47:1349 EXPECT_EQ(string16(), country);
50
51 address.set_country_code("US");
[email protected]91b073d2013-01-05 01:30:2852 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
[email protected]35401022011-02-25 23:47:1353 EXPECT_EQ(ASCIIToUTF16("United States"), country);
54
55 address.set_country_code("CA");
[email protected]91b073d2013-01-05 01:30:2856 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
[email protected]35401022011-02-25 23:47:1357 EXPECT_EQ(ASCIIToUTF16("Canada"), country);
58}
59
60// Test that we properly detect country codes appropriate for each country.
[email protected]377c3582012-06-14 01:56:1861TEST_F(AddressTest, SetCountry) {
[email protected]35401022011-02-25 23:47:1362 Address address;
63 EXPECT_EQ(std::string(), address.country_code());
64
65 // Test basic conversion.
[email protected]91b073d2013-01-05 01:30:2866 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("United States"), "en-US");
67 string16 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
[email protected]35401022011-02-25 23:47:1368 EXPECT_EQ("US", address.country_code());
69 EXPECT_EQ(ASCIIToUTF16("United States"), country);
70
71 // Test basic synonym detection.
[email protected]91b073d2013-01-05 01:30:2872 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("USA"), "en-US");
73 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
[email protected]35401022011-02-25 23:47:1374 EXPECT_EQ("US", address.country_code());
75 EXPECT_EQ(ASCIIToUTF16("United States"), country);
76
77 // Test case-insensitivity.
[email protected]91b073d2013-01-05 01:30:2878 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("canADA"), "en-US");
79 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
[email protected]35401022011-02-25 23:47:1380 EXPECT_EQ("CA", address.country_code());
81 EXPECT_EQ(ASCIIToUTF16("Canada"), country);
82
83 // Test country code detection.
[email protected]91b073d2013-01-05 01:30:2884 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("JP"), "en-US");
85 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
[email protected]35401022011-02-25 23:47:1386 EXPECT_EQ("JP", address.country_code());
87 EXPECT_EQ(ASCIIToUTF16("Japan"), country);
88
89 // Test that we ignore unknown countries.
[email protected]91b073d2013-01-05 01:30:2890 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("Unknown"), "en-US");
91 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
[email protected]35401022011-02-25 23:47:1392 EXPECT_EQ(std::string(), address.country_code());
93 EXPECT_EQ(string16(), country);
94}
95
96// Test that we properly match typed values to stored country data.
[email protected]377c3582012-06-14 01:56:1897TEST_F(AddressTest, IsCountry) {
[email protected]35401022011-02-25 23:47:1398 Address address;
99 address.set_country_code("US");
100
101 const char* const kValidMatches[] = {
102 "United States",
103 "USA",
104 "US",
105 "United states",
106 "us"
107 };
108 for (size_t i = 0; i < arraysize(kValidMatches); ++i) {
109 SCOPED_TRACE(kValidMatches[i]);
[email protected]1308c2832011-05-09 18:30:23110 FieldTypeSet matching_types;
[email protected]4d11fcd2012-12-05 05:34:48111 address.GetMatchingTypes(ASCIIToUTF16(kValidMatches[i]), "US",
112 &matching_types);
[email protected]1308c2832011-05-09 18:30:23113 ASSERT_EQ(1U, matching_types.size());
114 EXPECT_EQ(ADDRESS_HOME_COUNTRY, *matching_types.begin());
[email protected]35401022011-02-25 23:47:13115 }
116
117 const char* const kInvalidMatches[] = {
118 "United",
119 "Garbage"
120 };
121 for (size_t i = 0; i < arraysize(kInvalidMatches); ++i) {
[email protected]1308c2832011-05-09 18:30:23122 FieldTypeSet matching_types;
[email protected]4d11fcd2012-12-05 05:34:48123 address.GetMatchingTypes(ASCIIToUTF16(kInvalidMatches[i]), "US",
124 &matching_types);
[email protected]1308c2832011-05-09 18:30:23125 EXPECT_EQ(0U, matching_types.size());
[email protected]35401022011-02-25 23:47:13126 }
127
128 // Make sure that garbage values don't match when the country code is empty.
129 address.set_country_code("");
130 EXPECT_EQ(std::string(), address.country_code());
[email protected]1308c2832011-05-09 18:30:23131 FieldTypeSet matching_types;
[email protected]4d11fcd2012-12-05 05:34:48132 address.GetMatchingTypes(ASCIIToUTF16("Garbage"), "US", &matching_types);
[email protected]1308c2832011-05-09 18:30:23133 EXPECT_EQ(0U, matching_types.size());
[email protected]35401022011-02-25 23:47:13134}