OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/string16.h" | 8 #include "base/string16.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "components/autofill/browser/address.h" | 10 #include "components/autofill/browser/address.h" |
11 #include "components/autofill/browser/autofill_type.h" | 11 #include "components/autofill/browser/autofill_type.h" |
12 #include "content/public/test/test_browser_thread.h" | 12 #include "content/public/test/test_browser_thread.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 using content::BrowserThread; | 15 using content::BrowserThread; |
16 | 16 |
17 class AddressTest : public testing::Test { | 17 class AddressTest : public testing::Test { |
18 public: | 18 public: |
19 // In order to access the application locale -- which the tested functions do | 19 // In order to access the application locale -- which the tested functions do |
20 // internally -- this test must run on the UI thread. | 20 // internally -- this test must run on the UI thread. |
21 AddressTest() : ui_thread_(BrowserThread::UI, &message_loop_) {} | 21 AddressTest() : ui_thread_(BrowserThread::UI, &message_loop_) {} |
22 | 22 |
23 private: | 23 private: |
24 MessageLoopForUI message_loop_; | 24 MessageLoopForUI message_loop_; |
25 content::TestBrowserThread ui_thread_; | 25 content::TestBrowserThread ui_thread_; |
26 | 26 |
27 DISALLOW_COPY_AND_ASSIGN(AddressTest); | 27 DISALLOW_COPY_AND_ASSIGN(AddressTest); |
28 }; | 28 }; |
29 | 29 |
30 // Test that the getters and setters for country code are working. | |
31 TEST_F(AddressTest, CountryCode) { | |
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. | 30 // Test that country codes are properly decoded as country names. |
43 TEST_F(AddressTest, GetCountry) { | 31 TEST_F(AddressTest, GetCountry) { |
44 Address address; | 32 Address address; |
45 EXPECT_EQ(std::string(), address.country_code()); | 33 EXPECT_EQ(string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY)); |
46 | 34 |
47 // Make sure that nothing breaks when the country code is missing. | 35 // Make sure that nothing breaks when the country code is missing. |
48 string16 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); | 36 string16 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); |
49 EXPECT_EQ(string16(), country); | 37 EXPECT_EQ(string16(), country); |
50 | 38 |
51 address.set_country_code("US"); | 39 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US"); |
52 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); | 40 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); |
53 EXPECT_EQ(ASCIIToUTF16("United States"), country); | 41 EXPECT_EQ(ASCIIToUTF16("United States"), country); |
54 | 42 |
55 address.set_country_code("CA"); | 43 address.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("CA")); |
56 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); | 44 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); |
57 EXPECT_EQ(ASCIIToUTF16("Canada"), country); | 45 EXPECT_EQ(ASCIIToUTF16("Canada"), country); |
58 } | 46 } |
59 | 47 |
60 // Test that we properly detect country codes appropriate for each country. | 48 // Test that we properly detect country codes appropriate for each country. |
61 TEST_F(AddressTest, SetCountry) { | 49 TEST_F(AddressTest, SetCountry) { |
62 Address address; | 50 Address address; |
63 EXPECT_EQ(std::string(), address.country_code()); | 51 EXPECT_EQ(string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY)); |
64 | 52 |
65 // Test basic conversion. | 53 // Test basic conversion. |
66 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("United States"), "en-US"); | 54 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("United States"), "en-US"); |
67 string16 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); | 55 string16 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); |
68 EXPECT_EQ("US", address.country_code()); | 56 EXPECT_EQ(ASCIIToUTF16("US"), address.GetRawInfo(ADDRESS_HOME_COUNTRY)); |
69 EXPECT_EQ(ASCIIToUTF16("United States"), country); | 57 EXPECT_EQ(ASCIIToUTF16("United States"), country); |
70 | 58 |
71 // Test basic synonym detection. | 59 // Test basic synonym detection. |
72 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("USA"), "en-US"); | 60 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("USA"), "en-US"); |
73 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); | 61 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); |
74 EXPECT_EQ("US", address.country_code()); | 62 EXPECT_EQ(ASCIIToUTF16("US"), address.GetRawInfo(ADDRESS_HOME_COUNTRY)); |
75 EXPECT_EQ(ASCIIToUTF16("United States"), country); | 63 EXPECT_EQ(ASCIIToUTF16("United States"), country); |
76 | 64 |
77 // Test case-insensitivity. | 65 // Test case-insensitivity. |
78 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("canADA"), "en-US"); | 66 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("canADA"), "en-US"); |
79 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); | 67 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); |
80 EXPECT_EQ("CA", address.country_code()); | 68 EXPECT_EQ(ASCIIToUTF16("CA"), address.GetRawInfo(ADDRESS_HOME_COUNTRY)); |
81 EXPECT_EQ(ASCIIToUTF16("Canada"), country); | 69 EXPECT_EQ(ASCIIToUTF16("Canada"), country); |
82 | 70 |
83 // Test country code detection. | 71 // Test country code detection. |
84 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("JP"), "en-US"); | 72 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("JP"), "en-US"); |
85 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); | 73 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); |
86 EXPECT_EQ("JP", address.country_code()); | 74 EXPECT_EQ(ASCIIToUTF16("JP"), address.GetRawInfo(ADDRESS_HOME_COUNTRY)); |
87 EXPECT_EQ(ASCIIToUTF16("Japan"), country); | 75 EXPECT_EQ(ASCIIToUTF16("Japan"), country); |
88 | 76 |
89 // Test that we ignore unknown countries. | 77 // Test that we ignore unknown countries. |
90 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("Unknown"), "en-US"); | 78 address.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("Unknown"), "en-US"); |
91 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); | 79 country = address.GetInfo(ADDRESS_HOME_COUNTRY, "en-US"); |
92 EXPECT_EQ(std::string(), address.country_code()); | 80 EXPECT_EQ(string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY)); |
93 EXPECT_EQ(string16(), country); | 81 EXPECT_EQ(string16(), country); |
94 } | 82 } |
95 | 83 |
96 // Test that we properly match typed values to stored country data. | 84 // Test that we properly match typed values to stored country data. |
97 TEST_F(AddressTest, IsCountry) { | 85 TEST_F(AddressTest, IsCountry) { |
98 Address address; | 86 Address address; |
99 address.set_country_code("US"); | 87 address.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("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]); | |
110 FieldTypeSet matching_types; | |
111 address.GetMatchingTypes(ASCIIToUTF16(kValidMatches[i]), "US", | |
112 &matching_types); | |
113 ASSERT_EQ(1U, matching_types.size()); | |
114 EXPECT_EQ(ADDRESS_HOME_COUNTRY, *matching_types.begin()); | |
115 } | |
Ilya Sherman
2013/04/05 05:18:01
Hmm, why did you remove this test coverage?
jam
2013/04/05 06:45:54
it seemed that that test coverate was for Address:
Ilya Sherman
2013/04/05 07:18:41
Ah, I missed that you'd removed that code. I'm pr
jam
2013/04/05 07:35:35
Done.
| |
116 | 88 |
117 const char* const kInvalidMatches[] = { | 89 const char* const kInvalidMatches[] = { |
118 "United", | 90 "United", |
119 "Garbage" | 91 "Garbage" |
120 }; | 92 }; |
121 for (size_t i = 0; i < arraysize(kInvalidMatches); ++i) { | 93 for (size_t i = 0; i < arraysize(kInvalidMatches); ++i) { |
122 FieldTypeSet matching_types; | 94 FieldTypeSet matching_types; |
123 address.GetMatchingTypes(ASCIIToUTF16(kInvalidMatches[i]), "US", | 95 address.GetMatchingTypes(ASCIIToUTF16(kInvalidMatches[i]), "US", |
124 &matching_types); | 96 &matching_types); |
125 EXPECT_EQ(0U, matching_types.size()); | 97 EXPECT_EQ(0U, matching_types.size()); |
126 } | 98 } |
127 | 99 |
128 // Make sure that garbage values don't match when the country code is empty. | 100 // Make sure that garbage values don't match when the country code is empty. |
129 address.set_country_code(""); | 101 address.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("")); |
Ilya Sherman
2013/04/05 05:18:01
nit: string16() rather than ASCIIToUTF16("")
jam
2013/04/05 06:45:54
Done.
| |
130 EXPECT_EQ(std::string(), address.country_code()); | 102 EXPECT_EQ(string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY)); |
131 FieldTypeSet matching_types; | 103 FieldTypeSet matching_types; |
132 address.GetMatchingTypes(ASCIIToUTF16("Garbage"), "US", &matching_types); | 104 address.GetMatchingTypes(ASCIIToUTF16("Garbage"), "US", &matching_types); |
133 EXPECT_EQ(0U, matching_types.size()); | 105 EXPECT_EQ(0U, matching_types.size()); |
134 } | 106 } |
OLD | NEW |