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