[email protected] | 377c358 | 2012-06-14 01:56:18 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 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" |
[email protected] | e4b2fa3 | 2013-03-09 22:56:56 | [diff] [blame] | 10 | #include "components/autofill/browser/address.h" |
| 11 | #include "components/autofill/browser/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] | 377c358 | 2012-06-14 01:56: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_; |
| 25 | content::TestBrowserThread ui_thread_; |
| 26 | |
| 27 | DISALLOW_COPY_AND_ASSIGN(AddressTest); |
| 28 | }; |
| 29 | |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 30 | // Test that country codes are properly decoded as country names. |
[email protected] | 377c358 | 2012-06-14 01:56:18 | [diff] [blame] | 31 | TEST_F(AddressTest, GetCountry) { |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 32 | Address address; |
[email protected] | ed005f6f | 2013-04-05 17:03:56 | [diff] [blame^] | 33 | EXPECT_EQ(string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY)); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 34 | |
| 35 | // Make sure that nothing breaks when the country code is missing. |
[email protected] | 91b073d | 2013-01-05 01:30:28 | [diff] [blame] | 36 | string16 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 37 | EXPECT_EQ(string16(), country); |
| 38 | |
[email protected] | ed005f6f | 2013-04-05 17:03:56 | [diff] [blame^] | 39 | address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US"); |
[email protected] | 91b073d | 2013-01-05 01:30:28 | [diff] [blame] | 40 | country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 41 | EXPECT_EQ(ASCIIToUTF16("United States"), country); |
| 42 | |
[email protected] | ed005f6f | 2013-04-05 17:03:56 | [diff] [blame^] | 43 | address.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("CA")); |
[email protected] | 91b073d | 2013-01-05 01:30:28 | [diff] [blame] | 44 | country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 45 | EXPECT_EQ(ASCIIToUTF16("Canada"), country); |
| 46 | } |
| 47 | |
| 48 | // Test that we properly detect country codes appropriate for each country. |
[email protected] | 377c358 | 2012-06-14 01:56:18 | [diff] [blame] | 49 | TEST_F(AddressTest, SetCountry) { |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 50 | Address address; |
[email protected] | ed005f6f | 2013-04-05 17:03:56 | [diff] [blame^] | 51 | EXPECT_EQ(string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY)); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 52 | |
| 53 | // Test basic conversion. |
[email protected] | 91b073d | 2013-01-05 01:30:28 | [diff] [blame] | 54 | address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("United States"), "en-US"); |
| 55 | string16 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); |
[email protected] | ed005f6f | 2013-04-05 17:03:56 | [diff] [blame^] | 56 | EXPECT_EQ(ASCIIToUTF16("US"), address.GetRawInfo(ADDRESS_HOME_COUNTRY)); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 57 | EXPECT_EQ(ASCIIToUTF16("United States"), country); |
| 58 | |
| 59 | // Test basic synonym detection. |
[email protected] | 91b073d | 2013-01-05 01:30:28 | [diff] [blame] | 60 | address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("USA"), "en-US"); |
| 61 | country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); |
[email protected] | ed005f6f | 2013-04-05 17:03:56 | [diff] [blame^] | 62 | EXPECT_EQ(ASCIIToUTF16("US"), address.GetRawInfo(ADDRESS_HOME_COUNTRY)); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 63 | EXPECT_EQ(ASCIIToUTF16("United States"), country); |
| 64 | |
| 65 | // Test case-insensitivity. |
[email protected] | 91b073d | 2013-01-05 01:30:28 | [diff] [blame] | 66 | address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("canADA"), "en-US"); |
| 67 | country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); |
[email protected] | ed005f6f | 2013-04-05 17:03:56 | [diff] [blame^] | 68 | EXPECT_EQ(ASCIIToUTF16("CA"), address.GetRawInfo(ADDRESS_HOME_COUNTRY)); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 69 | EXPECT_EQ(ASCIIToUTF16("Canada"), country); |
| 70 | |
| 71 | // Test country code detection. |
[email protected] | 91b073d | 2013-01-05 01:30:28 | [diff] [blame] | 72 | address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("JP"), "en-US"); |
| 73 | country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); |
[email protected] | ed005f6f | 2013-04-05 17:03:56 | [diff] [blame^] | 74 | EXPECT_EQ(ASCIIToUTF16("JP"), address.GetRawInfo(ADDRESS_HOME_COUNTRY)); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 75 | EXPECT_EQ(ASCIIToUTF16("Japan"), country); |
| 76 | |
| 77 | // Test that we ignore unknown countries. |
[email protected] | 91b073d | 2013-01-05 01:30:28 | [diff] [blame] | 78 | address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("Unknown"), "en-US"); |
| 79 | country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); |
[email protected] | ed005f6f | 2013-04-05 17:03:56 | [diff] [blame^] | 80 | EXPECT_EQ(string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY)); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 81 | EXPECT_EQ(string16(), country); |
| 82 | } |
| 83 | |
| 84 | // Test that we properly match typed values to stored country data. |
[email protected] | 377c358 | 2012-06-14 01:56:18 | [diff] [blame] | 85 | TEST_F(AddressTest, IsCountry) { |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 86 | Address address; |
[email protected] | ed005f6f | 2013-04-05 17:03:56 | [diff] [blame^] | 87 | address.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 88 | |
| 89 | const char* const kValidMatches[] = { |
| 90 | "United States", |
| 91 | "USA", |
| 92 | "US", |
| 93 | "United states", |
| 94 | "us" |
| 95 | }; |
| 96 | for (size_t i = 0; i < arraysize(kValidMatches); ++i) { |
| 97 | SCOPED_TRACE(kValidMatches[i]); |
[email protected] | 1308c283 | 2011-05-09 18:30:23 | [diff] [blame] | 98 | FieldTypeSet matching_types; |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 99 | address.GetMatchingTypes(ASCIIToUTF16(kValidMatches[i]), "US", |
| 100 | &matching_types); |
[email protected] | 1308c283 | 2011-05-09 18:30:23 | [diff] [blame] | 101 | ASSERT_EQ(1U, matching_types.size()); |
| 102 | EXPECT_EQ(ADDRESS_HOME_COUNTRY, *matching_types.begin()); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | const char* const kInvalidMatches[] = { |
| 106 | "United", |
| 107 | "Garbage" |
| 108 | }; |
| 109 | for (size_t i = 0; i < arraysize(kInvalidMatches); ++i) { |
[email protected] | 1308c283 | 2011-05-09 18:30:23 | [diff] [blame] | 110 | FieldTypeSet matching_types; |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 111 | address.GetMatchingTypes(ASCIIToUTF16(kInvalidMatches[i]), "US", |
| 112 | &matching_types); |
[email protected] | 1308c283 | 2011-05-09 18:30:23 | [diff] [blame] | 113 | EXPECT_EQ(0U, matching_types.size()); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // Make sure that garbage values don't match when the country code is empty. |
[email protected] | ed005f6f | 2013-04-05 17:03:56 | [diff] [blame^] | 117 | address.SetRawInfo(ADDRESS_HOME_COUNTRY, string16()); |
| 118 | EXPECT_EQ(string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY)); |
[email protected] | 1308c283 | 2011-05-09 18:30:23 | [diff] [blame] | 119 | FieldTypeSet matching_types; |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 120 | address.GetMatchingTypes(ASCIIToUTF16("Garbage"), "US", &matching_types); |
[email protected] | 1308c283 | 2011-05-09 18:30:23 | [diff] [blame] | 121 | EXPECT_EQ(0U, matching_types.size()); |
[email protected] | 3540102 | 2011-02-25 23:47:13 | [diff] [blame] | 122 | } |