[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 | |
[email protected] | 05871b3 | 2011-10-22 00:46:18 | [diff] [blame] | 7 | #include "base/message_loop.h" |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 8 | #include "base/string16.h" |
| 9 | #include "base/utf_string_conversions.h" |
| 10 | #include "chrome/browser/autofill/address.h" |
| 11 | #include "chrome/browser/autofill/autofill_type.h" |
[email protected] | e97882f | 2012-06-04 02:23:17 | [diff] [blame^] | 12 | #include "content/public/test/test_browser_thread.h" |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 15 | using content::BrowserThread; |
| 16 | |
[email protected] | 05871b3 | 2011-10-22 00:46:18 | [diff] [blame] | 17 | class 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_; |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 25 | content::TestBrowserThread ui_thread_; |
[email protected] | 05871b3 | 2011-10-22 00:46:18 | [diff] [blame] | 26 | |
| 27 | DISALLOW_COPY_AND_ASSIGN(AddressTest); |
| 28 | }; |
[email protected] | a5f9f37 | 2011-08-03 23:02:21 | [diff] [blame] | 29 | |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 30 | // Test that the getters and setters for country code are working. |
[email protected] | a5f9f37 | 2011-08-03 23:02:21 | [diff] [blame] | 31 | TEST_F(AddressTest, CountryCode) { |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 32 | 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] | a5f9f37 | 2011-08-03 23:02:21 | [diff] [blame] | 43 | TEST_F(AddressTest, GetCountry) { |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 44 | 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] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 48 | string16 country = address.GetInfo(ADDRESS_HOME_COUNTRY); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 49 | EXPECT_EQ(string16(), country); |
| 50 | |
| 51 | address.set_country_code("US"); |
[email protected] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 52 | country = address.GetInfo(ADDRESS_HOME_COUNTRY); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 53 | EXPECT_EQ(ASCIIToUTF16("United States"), country); |
| 54 | |
| 55 | address.set_country_code("CA"); |
[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(ASCIIToUTF16("Canada"), country); |
| 58 | } |
| 59 | |
| 60 | // Test that we properly detect country codes appropriate for each country. |
[email protected] | a5f9f37 | 2011-08-03 23:02:21 | [diff] [blame] | 61 | TEST_F(AddressTest, SetCountry) { |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 62 | Address address; |
| 63 | EXPECT_EQ(std::string(), address.country_code()); |
| 64 | |
| 65 | // Test basic conversion. |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 66 | address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("United States")); |
[email protected] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 67 | string16 country = address.GetInfo(ADDRESS_HOME_COUNTRY); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 68 | EXPECT_EQ("US", address.country_code()); |
| 69 | EXPECT_EQ(ASCIIToUTF16("United States"), country); |
| 70 | |
| 71 | // Test basic synonym detection. |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 72 | address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("USA")); |
[email protected] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 73 | country = address.GetInfo(ADDRESS_HOME_COUNTRY); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 74 | EXPECT_EQ("US", address.country_code()); |
| 75 | EXPECT_EQ(ASCIIToUTF16("United States"), country); |
| 76 | |
| 77 | // Test case-insensitivity. |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 78 | address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("canADA")); |
[email protected] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 79 | country = address.GetInfo(ADDRESS_HOME_COUNTRY); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 80 | EXPECT_EQ("CA", address.country_code()); |
| 81 | EXPECT_EQ(ASCIIToUTF16("Canada"), country); |
| 82 | |
| 83 | // Test country code detection. |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 84 | address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("JP")); |
[email protected] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 85 | country = address.GetInfo(ADDRESS_HOME_COUNTRY); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 86 | EXPECT_EQ("JP", address.country_code()); |
| 87 | EXPECT_EQ(ASCIIToUTF16("Japan"), country); |
| 88 | |
| 89 | // Test that we ignore unknown countries. |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 90 | address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("Unknown")); |
[email protected] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 91 | country = address.GetInfo(ADDRESS_HOME_COUNTRY); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 92 | 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] | a5f9f37 | 2011-08-03 23:02:21 | [diff] [blame] | 97 | TEST_F(AddressTest, IsCountry) { |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 98 | 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] | 1308c283 | 2011-05-09 18:30:23 | [diff] [blame] | 110 | FieldTypeSet matching_types; |
| 111 | address.GetMatchingTypes(ASCIIToUTF16(kValidMatches[i]), &matching_types); |
| 112 | ASSERT_EQ(1U, matching_types.size()); |
| 113 | EXPECT_EQ(ADDRESS_HOME_COUNTRY, *matching_types.begin()); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | const char* const kInvalidMatches[] = { |
| 117 | "United", |
| 118 | "Garbage" |
| 119 | }; |
| 120 | for (size_t i = 0; i < arraysize(kInvalidMatches); ++i) { |
[email protected] | 1308c283 | 2011-05-09 18:30:23 | [diff] [blame] | 121 | FieldTypeSet matching_types; |
| 122 | address.GetMatchingTypes(ASCIIToUTF16(kInvalidMatches[i]), &matching_types); |
| 123 | EXPECT_EQ(0U, matching_types.size()); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Make sure that garbage values don't match when the country code is empty. |
| 127 | address.set_country_code(""); |
| 128 | EXPECT_EQ(std::string(), address.country_code()); |
[email protected] | 1308c283 | 2011-05-09 18:30:23 | [diff] [blame] | 129 | FieldTypeSet matching_types; |
| 130 | address.GetMatchingTypes(ASCIIToUTF16("Garbage"), &matching_types); |
| 131 | EXPECT_EQ(0U, matching_types.size()); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 132 | } |