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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/strings/string_util.h" | 6 #include "base/strings/string_util.h" |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "content/common/android/address_parser.h" | 8 #include "content/common/android/address_parser.h" |
9 #include "content/common/android/address_parser_internal.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 namespace content::address_parser; | 12 using namespace content::address_parser; |
13 using namespace content::address_parser::internal; | 13 using namespace content::address_parser::internal; |
14 | 14 |
15 class AddressParserTest : public testing::Test { | 15 class AddressParserTest : public testing::Test { |
16 public: | 16 public: |
17 AddressParserTest() {} | 17 AddressParserTest() {} |
18 | 18 |
19 void TokenizeWords(const base::string16& content, WordList* words) const { | 19 void TokenizeWords(const base::string16& content, WordList* words) const { |
20 String16Tokenizer tokenizer(content.begin(), content.end(), | 20 String16Tokenizer tokenizer(content.begin(), content.end(), |
21 base::kWhitespaceUTF16); | 21 base::kWhitespaceUTF16); |
22 while (tokenizer.GetNext()) { | 22 while (tokenizer.GetNext()) { |
23 words->push_back(Word(tokenizer.token_begin(), tokenizer.token_end())); | 23 words->push_back(Word(tokenizer.token_begin(), tokenizer.token_end())); |
24 } | 24 } |
25 } | 25 } |
26 | 26 |
27 std::string GetHouseNumber(const std::string& content) const { | 27 std::string GetHouseNumber(const std::string& content) const { |
28 string16 content_16 = UTF8ToUTF16(content); | 28 base::string16 content_16 = UTF8ToUTF16(content); |
29 string16 result; | 29 base::string16 result; |
30 | 30 |
31 HouseNumberParser parser; | 31 HouseNumberParser parser; |
32 Word word; | 32 Word word; |
33 if (parser.Parse(content_16.begin(), content_16.end(), &word)) | 33 if (parser.Parse(content_16.begin(), content_16.end(), &word)) |
34 result = string16(word.begin, word.end); | 34 result = base::string16(word.begin, word.end); |
35 return UTF16ToUTF8(result); | 35 return UTF16ToUTF8(result); |
36 } | 36 } |
37 | 37 |
38 bool ContainsHouseNumber(const std::string& content) const { | 38 bool ContainsHouseNumber(const std::string& content) const { |
39 return !GetHouseNumber(content).empty(); | 39 return !GetHouseNumber(content).empty(); |
40 } | 40 } |
41 | 41 |
42 bool GetState(const std::string& state, size_t* state_index) const { | 42 bool GetState(const std::string& state, size_t* state_index) const { |
43 base::string16 state_16 = UTF8ToUTF16(state); | 43 base::string16 state_16 = UTF8ToUTF16(state); |
44 String16Tokenizer tokenizer(state_16.begin(), state_16.end(), | 44 String16Tokenizer tokenizer(state_16.begin(), state_16.end(), |
(...skipping 10 matching lines...) Expand all Loading... |
55 | 55 |
56 bool IsState(const std::string& state) const { | 56 bool IsState(const std::string& state) const { |
57 size_t state_index; | 57 size_t state_index; |
58 return GetState(state, &state_index); | 58 return GetState(state, &state_index); |
59 } | 59 } |
60 | 60 |
61 bool IsZipValid(const std::string& zip, const std::string& state) const { | 61 bool IsZipValid(const std::string& zip, const std::string& state) const { |
62 size_t state_index; | 62 size_t state_index; |
63 EXPECT_TRUE(GetState(state, &state_index)); | 63 EXPECT_TRUE(GetState(state, &state_index)); |
64 | 64 |
65 string16 zip_16 = UTF8ToUTF16(zip); | 65 base::string16 zip_16 = UTF8ToUTF16(zip); |
66 WordList words; | 66 WordList words; |
67 TokenizeWords(zip_16, &words); | 67 TokenizeWords(zip_16, &words); |
68 EXPECT_TRUE(words.size() == 1); | 68 EXPECT_TRUE(words.size() == 1); |
69 return ::IsZipValid(words.front(), state_index); | 69 return ::IsZipValid(words.front(), state_index); |
70 } | 70 } |
71 | 71 |
72 bool IsLocationName(const std::string& street) const { | 72 bool IsLocationName(const std::string& street) const { |
73 string16 street_16 = UTF8ToUTF16(street); | 73 base::string16 street_16 = UTF8ToUTF16(street); |
74 WordList words; | 74 WordList words; |
75 TokenizeWords(street_16, &words); | 75 TokenizeWords(street_16, &words); |
76 EXPECT_TRUE(words.size() == 1); | 76 EXPECT_TRUE(words.size() == 1); |
77 return IsValidLocationName(words.front()); | 77 return IsValidLocationName(words.front()); |
78 } | 78 } |
79 | 79 |
80 std::string FindAddress(const std::string& content) const { | 80 std::string FindAddress(const std::string& content) const { |
81 string16 content_16 = UTF8ToUTF16(content); | 81 base::string16 content_16 = UTF8ToUTF16(content); |
82 string16 result_16; | 82 base::string16 result_16; |
83 size_t start, end; | 83 size_t start, end; |
84 if (::FindAddress(content_16.begin(), content_16.end(), &start, &end)) | 84 if (::FindAddress(content_16.begin(), content_16.end(), &start, &end)) |
85 result_16 = content_16.substr(start, end - start); | 85 result_16 = content_16.substr(start, end - start); |
86 return UTF16ToUTF8(result_16); | 86 return UTF16ToUTF8(result_16); |
87 } | 87 } |
88 | 88 |
89 bool ContainsAddress(const std::string& content) const { | 89 bool ContainsAddress(const std::string& content) const { |
90 return !FindAddress(content).empty(); | 90 return !FindAddress(content).empty(); |
91 } | 91 } |
92 | 92 |
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 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")); |
586 | 586 |
587 EXPECT_FALSE(ContainsAddress("123 Fake Street, Springfield, Springfield")); | 587 EXPECT_FALSE(ContainsAddress("123 Fake Street, Springfield, Springfield")); |
588 EXPECT_FALSE(ContainsAddress("999 Street Avenue, City, ZZ 98765")); | 588 EXPECT_FALSE(ContainsAddress("999 Street Avenue, City, ZZ 98765")); |
589 EXPECT_FALSE(ContainsAddress("76 Here be dragons, CA 94043")); | 589 EXPECT_FALSE(ContainsAddress("76 Here be dragons, CA 94043")); |
590 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")); |
591 EXPECT_FALSE(ContainsAddress( | 591 EXPECT_FALSE(ContainsAddress( |
592 "1 Supercalifragilisticexpialidocious is too long, CA 90000")); | 592 "1 Supercalifragilisticexpialidocious is too long, CA 90000")); |
593 EXPECT_FALSE(ContainsAddress("")); | 593 EXPECT_FALSE(ContainsAddress("")); |
594 } | 594 } |
OLD | NEW |