[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 1 | // Copyright (c) 2011 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/string16.h" |
| 8 | #include "base/utf_string_conversions.h" |
| 9 | #include "chrome/browser/autofill/address.h" |
| 10 | #include "chrome/browser/autofill/autofill_type.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | // Test that the getters and setters for country code are working. |
| 14 | TEST(AddressTest, CountryCode) { |
| 15 | Address address; |
| 16 | EXPECT_EQ(std::string(), address.country_code()); |
| 17 | |
| 18 | address.set_country_code("US"); |
| 19 | EXPECT_EQ("US", address.country_code()); |
| 20 | |
| 21 | address.set_country_code("CA"); |
| 22 | EXPECT_EQ("CA", address.country_code()); |
| 23 | } |
| 24 | |
| 25 | // Test that country codes are properly decoded as country names. |
| 26 | TEST(AddressTest, GetCountry) { |
| 27 | Address address; |
| 28 | EXPECT_EQ(std::string(), address.country_code()); |
| 29 | |
| 30 | // Make sure that nothing breaks when the country code is missing. |
[email protected] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 31 | string16 country = address.GetInfo(ADDRESS_HOME_COUNTRY); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 32 | EXPECT_EQ(string16(), country); |
| 33 | |
| 34 | address.set_country_code("US"); |
[email protected] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 35 | country = address.GetInfo(ADDRESS_HOME_COUNTRY); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 36 | EXPECT_EQ(ASCIIToUTF16("United States"), country); |
| 37 | |
| 38 | address.set_country_code("CA"); |
[email protected] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 39 | country = address.GetInfo(ADDRESS_HOME_COUNTRY); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 40 | EXPECT_EQ(ASCIIToUTF16("Canada"), country); |
| 41 | } |
| 42 | |
| 43 | // Test that we properly detect country codes appropriate for each country. |
| 44 | TEST(AddressTest, SetCountry) { |
| 45 | Address address; |
| 46 | EXPECT_EQ(std::string(), address.country_code()); |
| 47 | |
| 48 | // Test basic conversion. |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 49 | address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("United States")); |
[email protected] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 50 | string16 country = address.GetInfo(ADDRESS_HOME_COUNTRY); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 51 | EXPECT_EQ("US", address.country_code()); |
| 52 | EXPECT_EQ(ASCIIToUTF16("United States"), country); |
| 53 | |
| 54 | // Test basic synonym detection. |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 55 | address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("USA")); |
[email protected] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 56 | country = address.GetInfo(ADDRESS_HOME_COUNTRY); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 57 | EXPECT_EQ("US", address.country_code()); |
| 58 | EXPECT_EQ(ASCIIToUTF16("United States"), country); |
| 59 | |
| 60 | // Test case-insensitivity. |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 61 | address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("canADA")); |
[email protected] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 62 | country = address.GetInfo(ADDRESS_HOME_COUNTRY); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 63 | EXPECT_EQ("CA", address.country_code()); |
| 64 | EXPECT_EQ(ASCIIToUTF16("Canada"), country); |
| 65 | |
| 66 | // Test country code detection. |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 67 | address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("JP")); |
[email protected] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 68 | country = address.GetInfo(ADDRESS_HOME_COUNTRY); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 69 | EXPECT_EQ("JP", address.country_code()); |
| 70 | EXPECT_EQ(ASCIIToUTF16("Japan"), country); |
| 71 | |
| 72 | // Test that we ignore unknown countries. |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 73 | address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("Unknown")); |
[email protected] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 74 | country = address.GetInfo(ADDRESS_HOME_COUNTRY); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 75 | EXPECT_EQ(std::string(), address.country_code()); |
| 76 | EXPECT_EQ(string16(), country); |
| 77 | } |
| 78 | |
| 79 | // Test that we properly match typed values to stored country data. |
| 80 | TEST(AddressTest, IsCountry) { |
| 81 | Address address; |
| 82 | address.set_country_code("US"); |
| 83 | |
| 84 | const char* const kValidMatches[] = { |
| 85 | "United States", |
| 86 | "USA", |
| 87 | "US", |
| 88 | "United states", |
| 89 | "us" |
| 90 | }; |
| 91 | for (size_t i = 0; i < arraysize(kValidMatches); ++i) { |
| 92 | SCOPED_TRACE(kValidMatches[i]); |
[email protected] | 1308c283 | 2011-05-09 18:30:23 | [diff] [blame^] | 93 | FieldTypeSet matching_types; |
| 94 | address.GetMatchingTypes(ASCIIToUTF16(kValidMatches[i]), &matching_types); |
| 95 | ASSERT_EQ(1U, matching_types.size()); |
| 96 | EXPECT_EQ(ADDRESS_HOME_COUNTRY, *matching_types.begin()); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | const char* const kInvalidMatches[] = { |
| 100 | "United", |
| 101 | "Garbage" |
| 102 | }; |
| 103 | for (size_t i = 0; i < arraysize(kInvalidMatches); ++i) { |
[email protected] | 1308c283 | 2011-05-09 18:30:23 | [diff] [blame^] | 104 | FieldTypeSet matching_types; |
| 105 | address.GetMatchingTypes(ASCIIToUTF16(kInvalidMatches[i]), &matching_types); |
| 106 | EXPECT_EQ(0U, matching_types.size()); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Make sure that garbage values don't match when the country code is empty. |
| 110 | address.set_country_code(""); |
| 111 | EXPECT_EQ(std::string(), address.country_code()); |
[email protected] | 1308c283 | 2011-05-09 18:30:23 | [diff] [blame^] | 112 | FieldTypeSet matching_types; |
| 113 | address.GetMatchingTypes(ASCIIToUTF16("Garbage"), &matching_types); |
| 114 | EXPECT_EQ(0U, matching_types.size()); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 115 | } |