Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: content/common/android/address_parser_unittest.cc

Issue 10456007: [Android] Split the address parser from AddressDetector for WebView use. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: transforming AddressParser into a namespace. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/common/android/address_parser_internal.cc ('k') | content/content_common.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/renderer/android/address_detector.h"
6
7 #include "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
8 #include "base/string_util.h" 6 #include "base/string_util.h"
9 #include "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "content/common/android/address_parser.h"
9 #include "content/common/android/address_parser_internal.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 using content::AddressDetector; 12 using namespace content::address_parser;
13 using namespace content::address_parser::internal;
13 14
14 class AddressDetectorTest : public testing::Test { 15 class AddressParserTest : public testing::Test {
15 public: 16 public:
16 AddressDetectorTest() {} 17 AddressParserTest() {}
17 18
18 void TokenizeWords(const string16& content, 19 void TokenizeWords(const string16& content, WordList* words) const {
19 AddressDetector::WordList* words) const { 20 String16Tokenizer tokenizer(content.begin(), content.end(),
20 AddressDetector::String16Tokenizer tokenizer(content.begin(), 21 kWhitespaceUTF16);
21 content.end(), kWhitespaceUTF16);
22 while (tokenizer.GetNext()) { 22 while (tokenizer.GetNext()) {
23 words->push_back(AddressDetector::Word(tokenizer.token_begin(), 23 words->push_back(Word(tokenizer.token_begin(), tokenizer.token_end()));
24 tokenizer.token_end()));
25 } 24 }
26 } 25 }
27 26
28 std::string GetHouseNumber(const std::string& content) const { 27 std::string GetHouseNumber(const std::string& content) const {
29 string16 content_16 = UTF8ToUTF16(content); 28 string16 content_16 = UTF8ToUTF16(content);
30 string16 result; 29 string16 result;
31 30
32 AddressDetector::HouseNumberParser parser; 31 HouseNumberParser parser;
33 AddressDetector::Word word; 32 Word word;
34 if (parser.Parse(content_16.begin(), content_16.end(), &word)) 33 if (parser.Parse(content_16.begin(), content_16.end(), &word))
35 result = string16(word.begin, word.end); 34 result = string16(word.begin, word.end);
36 return UTF16ToUTF8(result); 35 return UTF16ToUTF8(result);
37 } 36 }
38 37
39 bool ContainsHouseNumber(const std::string& content) const { 38 bool ContainsHouseNumber(const std::string& content) const {
40 return !GetHouseNumber(content).empty(); 39 return !GetHouseNumber(content).empty();
41 } 40 }
42 41
43 bool GetState(const std::string& state, size_t* state_index) const { 42 bool GetState(const std::string& state, size_t* state_index) const {
44 string16 state_16 = UTF8ToUTF16(state); 43 string16 state_16 = UTF8ToUTF16(state);
45 AddressDetector::String16Tokenizer tokenizer(state_16.begin(), 44 String16Tokenizer tokenizer(state_16.begin(), state_16.end(),
46 state_16.end(), 45 kWhitespaceUTF16);
47 kWhitespaceUTF16);
48 if (!tokenizer.GetNext()) 46 if (!tokenizer.GetNext())
49 return false; 47 return false;
50 48
51 size_t state_last_word; 49 size_t state_last_word;
52 AddressDetector::WordList words; 50 WordList words;
53 words.push_back(AddressDetector::Word(tokenizer.token_begin(), 51 words.push_back(Word(tokenizer.token_begin(), tokenizer.token_end()));
54 tokenizer.token_end())); 52 return FindStateStartingInWord(&words, 0, &state_last_word, &tokenizer,
55 return AddressDetector::FindStateStartingInWord( 53 state_index);
56 &words, 0, &state_last_word, &tokenizer, state_index);
57 } 54 }
58 55
59 bool IsState(const std::string& state) const { 56 bool IsState(const std::string& state) const {
60 size_t state_index; 57 size_t state_index;
61 return GetState(state, &state_index); 58 return GetState(state, &state_index);
62 } 59 }
63 60
64 bool IsZipValid(const std::string& zip, const std::string& state) const { 61 bool IsZipValid(const std::string& zip, const std::string& state) const {
65 size_t state_index; 62 size_t state_index;
66 EXPECT_TRUE(GetState(state, &state_index)); 63 EXPECT_TRUE(GetState(state, &state_index));
67 64
68 string16 zip_16 = UTF8ToUTF16(zip); 65 string16 zip_16 = UTF8ToUTF16(zip);
69 AddressDetector::WordList words; 66 WordList words;
70 TokenizeWords(zip_16, &words); 67 TokenizeWords(zip_16, &words);
71 EXPECT_TRUE(words.size() == 1); 68 EXPECT_TRUE(words.size() == 1);
72 return AddressDetector::IsZipValid(words.front(), state_index); 69 return ::IsZipValid(words.front(), state_index);
73 } 70 }
74 71
75 bool IsLocationName(const std::string& street) const { 72 bool IsLocationName(const std::string& street) const {
76 string16 street_16 = UTF8ToUTF16(street); 73 string16 street_16 = UTF8ToUTF16(street);
77 AddressDetector::WordList words; 74 WordList words;
78 TokenizeWords(street_16, &words); 75 TokenizeWords(street_16, &words);
79 EXPECT_TRUE(words.size() == 1); 76 EXPECT_TRUE(words.size() == 1);
80 return AddressDetector::IsValidLocationName(words.front()); 77 return IsValidLocationName(words.front());
81 } 78 }
82 79
83 std::string FindAddress(const std::string& content) const { 80 std::string FindAddress(const std::string& content) const {
84 string16 content_16 = UTF8ToUTF16(content); 81 string16 content_16 = UTF8ToUTF16(content);
85 string16 result_16; 82 string16 result_16;
86 size_t start, end; 83 size_t start, end;
87 AddressDetector detector; 84 if (::FindAddress(content_16.begin(), content_16.end(), &start, &end))
88 std::string content_text;
89 if (detector.FindContent(content_16.begin(), content_16.end(),
90 &start, &end, &content_text))
91 result_16 = content_16.substr(start, end - start); 85 result_16 = content_16.substr(start, end - start);
92 return UTF16ToUTF8(result_16); 86 return UTF16ToUTF8(result_16);
93 } 87 }
94 88
95 bool ContainsAddress(const std::string& content) const { 89 bool ContainsAddress(const std::string& content) const {
96 return !FindAddress(content).empty(); 90 return !FindAddress(content).empty();
97 } 91 }
98 92
99 bool IsAddress(const std::string& content) const { 93 bool IsAddress(const std::string& content) const {
100 return FindAddress(content) == content; 94 return FindAddress(content) == content;
101 } 95 }
102 96
103 private: 97 private:
104 DISALLOW_COPY_AND_ASSIGN(AddressDetectorTest); 98 DISALLOW_COPY_AND_ASSIGN(AddressParserTest);
105 }; 99 };
106 100
107 TEST_F(AddressDetectorTest, HouseNumber) { 101 TEST_F(AddressParserTest, HouseNumber) {
108 // Tests cases with valid home numbers. 102 // Tests cases with valid home numbers.
109 EXPECT_EQ(GetHouseNumber("4 my house"), "4"); 103 EXPECT_EQ(GetHouseNumber("4 my house"), "4");
110 EXPECT_EQ(GetHouseNumber("Something 4 my house"), "4"); 104 EXPECT_EQ(GetHouseNumber("Something 4 my house"), "4");
111 EXPECT_EQ(GetHouseNumber("4"), "4"); 105 EXPECT_EQ(GetHouseNumber("4"), "4");
112 EXPECT_EQ(GetHouseNumber(" 4,5"), "4"); 106 EXPECT_EQ(GetHouseNumber(" 4,5"), "4");
113 EXPECT_EQ(GetHouseNumber("one"), "one"); 107 EXPECT_EQ(GetHouseNumber("one"), "one");
114 EXPECT_EQ(GetHouseNumber("Number One somewhere"), "One"); 108 EXPECT_EQ(GetHouseNumber("Number One somewhere"), "One");
115 EXPECT_EQ(GetHouseNumber("Testing \n4\n"), "4"); 109 EXPECT_EQ(GetHouseNumber("Testing \n4\n"), "4");
116 EXPECT_EQ(GetHouseNumber("Foo 1ST"), "1ST"); 110 EXPECT_EQ(GetHouseNumber("Foo 1ST"), "1ST");
117 EXPECT_EQ(GetHouseNumber("Bar 2nd"), "2nd"); 111 EXPECT_EQ(GetHouseNumber("Bar 2nd"), "2nd");
(...skipping 24 matching lines...) Expand all
142 EXPECT_FALSE(ContainsHouseNumber("more than five digits: 123456")); 136 EXPECT_FALSE(ContainsHouseNumber("more than five digits: 123456"));
143 EXPECT_FALSE(ContainsHouseNumber("kjhdfkajsdhf98uf93h")); 137 EXPECT_FALSE(ContainsHouseNumber("kjhdfkajsdhf98uf93h"));
144 EXPECT_FALSE(ContainsHouseNumber("これはテストです。")); 138 EXPECT_FALSE(ContainsHouseNumber("これはテストです。"));
145 EXPECT_FALSE(ContainsHouseNumber("Number On")); 139 EXPECT_FALSE(ContainsHouseNumber("Number On"));
146 EXPECT_FALSE(ContainsHouseNumber("2: foo")); 140 EXPECT_FALSE(ContainsHouseNumber("2: foo"));
147 EXPECT_FALSE(ContainsHouseNumber("12-")); 141 EXPECT_FALSE(ContainsHouseNumber("12-"));
148 EXPECT_FALSE(ContainsHouseNumber("\n\"' \t, ")); 142 EXPECT_FALSE(ContainsHouseNumber("\n\"' \t, "));
149 EXPECT_FALSE(ContainsHouseNumber("")); 143 EXPECT_FALSE(ContainsHouseNumber(""));
150 } 144 }
151 145
152 TEST_F(AddressDetectorTest, FindState) { 146 TEST_F(AddressParserTest, FindState) {
153 // The complete set of state codes and names is tested together with their 147 // The complete set of state codes and names is tested together with their
154 // returned state indices in the zip code test. 148 // returned state indices in the zip code test.
155 EXPECT_TRUE(IsState("CALIFORNIA")); 149 EXPECT_TRUE(IsState("CALIFORNIA"));
156 EXPECT_TRUE(IsState("ca")); 150 EXPECT_TRUE(IsState("ca"));
157 151
158 EXPECT_FALSE(IsState("californi")); 152 EXPECT_FALSE(IsState("californi"));
159 EXPECT_FALSE(IsState("northern mariana")); 153 EXPECT_FALSE(IsState("northern mariana"));
160 EXPECT_FALSE(IsState("northern mariana island")); 154 EXPECT_FALSE(IsState("northern mariana island"));
161 EXPECT_FALSE(IsState("zz")); 155 EXPECT_FALSE(IsState("zz"));
162 } 156 }
163 157
164 TEST_F(AddressDetectorTest, ZipCode) { 158 TEST_F(AddressParserTest, ZipCode) {
165 EXPECT_TRUE(IsZipValid("90000", "CA")); 159 EXPECT_TRUE(IsZipValid("90000", "CA"));
166 EXPECT_TRUE(IsZipValid("01234", "MA")); 160 EXPECT_TRUE(IsZipValid("01234", "MA"));
167 EXPECT_TRUE(IsZipValid("99999-9999", "Alaska")); 161 EXPECT_TRUE(IsZipValid("99999-9999", "Alaska"));
168 162
169 EXPECT_FALSE(IsZipValid("999999999", "Alaska")); 163 EXPECT_FALSE(IsZipValid("999999999", "Alaska"));
170 EXPECT_FALSE(IsZipValid("9999-99999", "Alaska")); 164 EXPECT_FALSE(IsZipValid("9999-99999", "Alaska"));
171 EXPECT_FALSE(IsZipValid("999999999-", "Alaska")); 165 EXPECT_FALSE(IsZipValid("999999999-", "Alaska"));
172 EXPECT_FALSE(IsZipValid("99999-999a", "Alaska")); 166 EXPECT_FALSE(IsZipValid("99999-999a", "Alaska"));
173 EXPECT_FALSE(IsZipValid("99999--9999", "Alaska")); 167 EXPECT_FALSE(IsZipValid("99999--9999", "Alaska"));
174 EXPECT_FALSE(IsZipValid("90000o", "CA")); 168 EXPECT_FALSE(IsZipValid("90000o", "CA"));
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 EXPECT_TRUE(IsZipValid("98000", "WA")); 283 EXPECT_TRUE(IsZipValid("98000", "WA"));
290 EXPECT_TRUE(IsZipValid("99000", "Washington")); 284 EXPECT_TRUE(IsZipValid("99000", "Washington"));
291 EXPECT_TRUE(IsZipValid("53000", "WI")); 285 EXPECT_TRUE(IsZipValid("53000", "WI"));
292 EXPECT_TRUE(IsZipValid("54000", "Wisconsin")); 286 EXPECT_TRUE(IsZipValid("54000", "Wisconsin"));
293 EXPECT_TRUE(IsZipValid("24000", "WV")); 287 EXPECT_TRUE(IsZipValid("24000", "WV"));
294 EXPECT_TRUE(IsZipValid("26000", "West Virginia")); 288 EXPECT_TRUE(IsZipValid("26000", "West Virginia"));
295 EXPECT_TRUE(IsZipValid("82000", "WY")); 289 EXPECT_TRUE(IsZipValid("82000", "WY"));
296 EXPECT_TRUE(IsZipValid("83000", "Wyoming")); 290 EXPECT_TRUE(IsZipValid("83000", "Wyoming"));
297 } 291 }
298 292
299 TEST_F(AddressDetectorTest, LocationName) { 293 TEST_F(AddressParserTest, LocationName) {
300 EXPECT_FALSE(IsLocationName("str-eet")); 294 EXPECT_FALSE(IsLocationName("str-eet"));
301 EXPECT_FALSE(IsLocationName("somewhere")); 295 EXPECT_FALSE(IsLocationName("somewhere"));
302 296
303 // Test all supported street names and expected plural cases. 297 // Test all supported street names and expected plural cases.
304 EXPECT_TRUE(IsLocationName("alley")); 298 EXPECT_TRUE(IsLocationName("alley"));
305 EXPECT_TRUE(IsLocationName("annex")); 299 EXPECT_TRUE(IsLocationName("annex"));
306 EXPECT_TRUE(IsLocationName("arcade")); 300 EXPECT_TRUE(IsLocationName("arcade"));
307 EXPECT_TRUE(IsLocationName("ave.")); 301 EXPECT_TRUE(IsLocationName("ave."));
308 EXPECT_TRUE(IsLocationName("avenue")); 302 EXPECT_TRUE(IsLocationName("avenue"));
309 EXPECT_TRUE(IsLocationName("alameda")); 303 EXPECT_TRUE(IsLocationName("alameda"));
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 EXPECT_TRUE(IsLocationName("walks")); 502 EXPECT_TRUE(IsLocationName("walks"));
509 EXPECT_TRUE(IsLocationName("wall")); 503 EXPECT_TRUE(IsLocationName("wall"));
510 EXPECT_TRUE(IsLocationName("way")); 504 EXPECT_TRUE(IsLocationName("way"));
511 EXPECT_TRUE(IsLocationName("ways")); 505 EXPECT_TRUE(IsLocationName("ways"));
512 EXPECT_TRUE(IsLocationName("well")); 506 EXPECT_TRUE(IsLocationName("well"));
513 EXPECT_TRUE(IsLocationName("wells")); 507 EXPECT_TRUE(IsLocationName("wells"));
514 EXPECT_TRUE(IsLocationName("xing")); 508 EXPECT_TRUE(IsLocationName("xing"));
515 EXPECT_TRUE(IsLocationName("xrd")); 509 EXPECT_TRUE(IsLocationName("xrd"));
516 } 510 }
517 511
518 TEST_F(AddressDetectorTest, NumberPrefixCases) { 512 TEST_F(AddressParserTest, NumberPrefixCases) {
519 EXPECT_EQ(FindAddress("Cafe 21\n750 Fifth Ave. San Diego, California 92101"), 513 EXPECT_EQ(FindAddress("Cafe 21\n750 Fifth Ave. San Diego, California 92101"),
520 "750 Fifth Ave. San Diego, California 92101"); 514 "750 Fifth Ave. San Diego, California 92101");
521 EXPECT_EQ(FindAddress( 515 EXPECT_EQ(FindAddress(
522 "Century City 15\n 10250 Santa Monica Boulevard Los Angeles, CA 90067"), 516 "Century City 15\n 10250 Santa Monica Boulevard Los Angeles, CA 90067"),
523 "10250 Santa Monica Boulevard Los Angeles, CA 90067"); 517 "10250 Santa Monica Boulevard Los Angeles, CA 90067");
524 EXPECT_EQ(FindAddress("123 45\n67 My Street, Somewhere, NY 10000"), 518 EXPECT_EQ(FindAddress("123 45\n67 My Street, Somewhere, NY 10000"),
525 "67 My Street, Somewhere, NY 10000"); 519 "67 My Street, Somewhere, NY 10000");
526 EXPECT_TRUE(IsAddress("123 4th Avenue, Somewhere in NY 10000")); 520 EXPECT_TRUE(IsAddress("123 4th Avenue, Somewhere in NY 10000"));
527 } 521 }
528 522
529 TEST_F(AddressDetectorTest, FullAddress) { 523 TEST_F(AddressParserTest, FullAddress) {
530 // Test US Google corporate addresses. Expects a full string match. 524 // Test US Google corporate addresses. Expects a full string match.
531 EXPECT_TRUE(IsAddress("1600 Amphitheatre Parkway Mountain View, CA 94043")); 525 EXPECT_TRUE(IsAddress("1600 Amphitheatre Parkway Mountain View, CA 94043"));
532 EXPECT_TRUE(IsAddress("201 S. Division St. Suite 500 Ann Arbor, MI 48104")); 526 EXPECT_TRUE(IsAddress("201 S. Division St. Suite 500 Ann Arbor, MI 48104"));
533 EXPECT_TRUE(ContainsAddress( 527 EXPECT_TRUE(ContainsAddress(
534 "Millennium at Midtown 10 10th Street NE Suite 600 Atlanta, GA 30309")); 528 "Millennium at Midtown 10 10th Street NE Suite 600 Atlanta, GA 30309"));
535 EXPECT_TRUE(IsAddress( 529 EXPECT_TRUE(IsAddress(
536 "9606 North MoPac Expressway Suite 400 Austin, TX 78759")); 530 "9606 North MoPac Expressway Suite 400 Austin, TX 78759"));
537 EXPECT_TRUE(IsAddress("2590 Pearl Street Suite 100 Boulder, CO 80302")); 531 EXPECT_TRUE(IsAddress("2590 Pearl Street Suite 100 Boulder, CO 80302"));
538 EXPECT_TRUE(IsAddress("5 Cambridge Center, Floors 3-6 Cambridge, MA 02142")); 532 EXPECT_TRUE(IsAddress("5 Cambridge Center, Floors 3-6 Cambridge, MA 02142"));
539 EXPECT_TRUE(IsAddress("410 Market St Suite 415 Chapel Hill, NC 27516")); 533 EXPECT_TRUE(IsAddress("410 Market St Suite 415 Chapel Hill, NC 27516"));
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 EXPECT_TRUE(IsAddress("79th Street 1st Floor New York City, NY 10024-5192")); 585 EXPECT_TRUE(IsAddress("79th Street 1st Floor New York City, NY 10024-5192"));
592 586
593 EXPECT_FALSE(ContainsAddress("123 Fake Street, Springfield, Springfield")); 587 EXPECT_FALSE(ContainsAddress("123 Fake Street, Springfield, Springfield"));
594 EXPECT_FALSE(ContainsAddress("999 Street Avenue, City, ZZ 98765")); 588 EXPECT_FALSE(ContainsAddress("999 Street Avenue, City, ZZ 98765"));
595 EXPECT_FALSE(ContainsAddress("76 Here be dragons, CA 94043")); 589 EXPECT_FALSE(ContainsAddress("76 Here be dragons, CA 94043"));
596 EXPECT_FALSE(ContainsAddress("1 This, has, too* many, lines, to, be* valid")); 590 EXPECT_FALSE(ContainsAddress("1 This, has, too* many, lines, to, be* valid"));
597 EXPECT_FALSE(ContainsAddress( 591 EXPECT_FALSE(ContainsAddress(
598 "1 Supercalifragilisticexpialidocious is too long, CA 90000")); 592 "1 Supercalifragilisticexpialidocious is too long, CA 90000"));
599 EXPECT_FALSE(ContainsAddress("")); 593 EXPECT_FALSE(ContainsAddress(""));
600 } 594 }
OLDNEW
« no previous file with comments | « content/common/android/address_parser_internal.cc ('k') | content/content_common.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698