blob: 2b74f40a2a498f90554e19738fd586d3c9347dec [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]e217c5632013-04-12 19:11:4817namespace autofill {
18
[email protected]377c3582012-06-14 01:56:1819class 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 MessageLoopForUI message_loop_;
27 content::TestBrowserThread ui_thread_;
28
29 DISALLOW_COPY_AND_ASSIGN(AddressTest);
30};
31
[email protected]35401022011-02-25 23:47:1332// Test that country codes are properly decoded as country names.
[email protected]377c3582012-06-14 01:56:1833TEST_F(AddressTest, GetCountry) {
[email protected]35401022011-02-25 23:47:1334 Address address;
[email protected]d5ca8fb2013-04-11 17:54:3135 EXPECT_EQ(base::string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
[email protected]35401022011-02-25 23:47:1336
37 // Make sure that nothing breaks when the country code is missing.
[email protected]d5ca8fb2013-04-11 17:54:3138 base::string16 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
39 EXPECT_EQ(base::string16(), country);
[email protected]35401022011-02-25 23:47:1340
[email protected]ed005f6f2013-04-05 17:03:5641 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US");
[email protected]91b073d2013-01-05 01:30:2842 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
[email protected]35401022011-02-25 23:47:1343 EXPECT_EQ(ASCIIToUTF16("United States"), country);
44
[email protected]ed005f6f2013-04-05 17:03:5645 address.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("CA"));
[email protected]91b073d2013-01-05 01:30:2846 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
[email protected]35401022011-02-25 23:47:1347 EXPECT_EQ(ASCIIToUTF16("Canada"), country);
48}
49
50// Test that we properly detect country codes appropriate for each country.
[email protected]377c3582012-06-14 01:56:1851TEST_F(AddressTest, SetCountry) {
[email protected]35401022011-02-25 23:47:1352 Address address;
[email protected]d5ca8fb2013-04-11 17:54:3153 EXPECT_EQ(base::string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
[email protected]35401022011-02-25 23:47:1354
55 // Test basic conversion.
[email protected]91b073d2013-01-05 01:30:2856 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("United States"), "en-US");
[email protected]d5ca8fb2013-04-11 17:54:3157 base::string16 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
[email protected]ed005f6f2013-04-05 17:03:5658 EXPECT_EQ(ASCIIToUTF16("US"), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
[email protected]35401022011-02-25 23:47:1359 EXPECT_EQ(ASCIIToUTF16("United States"), country);
60
61 // Test basic synonym detection.
[email protected]91b073d2013-01-05 01:30:2862 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("USA"), "en-US");
63 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
[email protected]ed005f6f2013-04-05 17:03:5664 EXPECT_EQ(ASCIIToUTF16("US"), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
[email protected]35401022011-02-25 23:47:1365 EXPECT_EQ(ASCIIToUTF16("United States"), country);
66
67 // Test case-insensitivity.
[email protected]91b073d2013-01-05 01:30:2868 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("canADA"), "en-US");
69 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
[email protected]ed005f6f2013-04-05 17:03:5670 EXPECT_EQ(ASCIIToUTF16("CA"), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
[email protected]35401022011-02-25 23:47:1371 EXPECT_EQ(ASCIIToUTF16("Canada"), country);
72
73 // Test country code detection.
[email protected]91b073d2013-01-05 01:30:2874 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("JP"), "en-US");
75 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
[email protected]ed005f6f2013-04-05 17:03:5676 EXPECT_EQ(ASCIIToUTF16("JP"), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
[email protected]35401022011-02-25 23:47:1377 EXPECT_EQ(ASCIIToUTF16("Japan"), country);
78
79 // Test that we ignore unknown countries.
[email protected]91b073d2013-01-05 01:30:2880 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("Unknown"), "en-US");
81 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US");
[email protected]d5ca8fb2013-04-11 17:54:3182 EXPECT_EQ(base::string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
83 EXPECT_EQ(base::string16(), country);
[email protected]35401022011-02-25 23:47:1384}
85
86// Test that we properly match typed values to stored country data.
[email protected]377c3582012-06-14 01:56:1887TEST_F(AddressTest, IsCountry) {
[email protected]35401022011-02-25 23:47:1388 Address address;
[email protected]ed005f6f2013-04-05 17:03:5689 address.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
[email protected]35401022011-02-25 23:47:1390
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]);
[email protected]1308c2832011-05-09 18:30:23100 FieldTypeSet matching_types;
[email protected]4d11fcd2012-12-05 05:34:48101 address.GetMatchingTypes(ASCIIToUTF16(kValidMatches[i]), "US",
102 &matching_types);
[email protected]1308c2832011-05-09 18:30:23103 ASSERT_EQ(1U, matching_types.size());
104 EXPECT_EQ(ADDRESS_HOME_COUNTRY, *matching_types.begin());
[email protected]35401022011-02-25 23:47:13105 }
106
107 const char* const kInvalidMatches[] = {
108 "United",
109 "Garbage"
110 };
111 for (size_t i = 0; i < arraysize(kInvalidMatches); ++i) {
[email protected]1308c2832011-05-09 18:30:23112 FieldTypeSet matching_types;
[email protected]4d11fcd2012-12-05 05:34:48113 address.GetMatchingTypes(ASCIIToUTF16(kInvalidMatches[i]), "US",
114 &matching_types);
[email protected]1308c2832011-05-09 18:30:23115 EXPECT_EQ(0U, matching_types.size());
[email protected]35401022011-02-25 23:47:13116 }
117
118 // Make sure that garbage values don't match when the country code is empty.
[email protected]d5ca8fb2013-04-11 17:54:31119 address.SetRawInfo(ADDRESS_HOME_COUNTRY, base::string16());
120 EXPECT_EQ(base::string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
[email protected]1308c2832011-05-09 18:30:23121 FieldTypeSet matching_types;
[email protected]4d11fcd2012-12-05 05:34:48122 address.GetMatchingTypes(ASCIIToUTF16("Garbage"), "US", &matching_types);
[email protected]1308c2832011-05-09 18:30:23123 EXPECT_EQ(0U, matching_types.size());
[email protected]35401022011-02-25 23:47:13124}
[email protected]e217c5632013-04-12 19:11:48125
126} // namespace autofill