| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/renderer/android/phone_number_detector.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace content { | |
| 13 | |
| 14 class PhoneNumberDetectorTest : public testing::Test { | |
| 15 public: | |
| 16 static std::string FindNumber(const std::string& content, | |
| 17 const std::string& region) { | |
| 18 base::string16 content_16 = base::UTF8ToUTF16(content); | |
| 19 base::string16 result_16; | |
| 20 size_t start, end; | |
| 21 PhoneNumberDetector detector(region); | |
| 22 std::string content_text; | |
| 23 if (detector.FindContent(content_16.begin(), content_16.end(), | |
| 24 &start, &end, &content_text)) | |
| 25 result_16 = content_16.substr(start, end - start); | |
| 26 return base::UTF16ToUTF8(result_16); | |
| 27 } | |
| 28 | |
| 29 static std::string FindAndFormatNumber(const std::string& content, | |
| 30 const std::string& region) { | |
| 31 base::string16 content_16 = base::UTF8ToUTF16(content); | |
| 32 base::string16 result_16; | |
| 33 size_t start, end; | |
| 34 PhoneNumberDetector detector(region); | |
| 35 std::string content_text; | |
| 36 detector.FindContent(content_16.begin(), content_16.end(), | |
| 37 &start, &end, &content_text); | |
| 38 return content_text; | |
| 39 } | |
| 40 }; | |
| 41 | |
| 42 TEST_F(PhoneNumberDetectorTest, FindNumber) { | |
| 43 // Tests cases with valid home numbers. | |
| 44 EXPECT_EQ("617-426-3000", FindNumber("hello 617-426-3000 blah", "us")); | |
| 45 EXPECT_EQ("", FindNumber("hello 617-426-3000 blah", "gb")); | |
| 46 EXPECT_EQ("020-7617-4426", FindNumber("<div>020-7617-4426</div>", "gb")); | |
| 47 EXPECT_EQ("", FindNumber("<div>020-7617-4426</div>", "fr")); | |
| 48 EXPECT_EQ("02.38.96.68.88", FindNumber("Tel:02.38.96.68.88", "fr")); | |
| 49 EXPECT_EQ("", FindNumber("Tel:02.38.96.68.88", "gb")); | |
| 50 EXPECT_EQ("1-800-866-2453", | |
| 51 FindNumber("You can call this number:1-800-866-2453 for more " | |
| 52 "information", "us")); | |
| 53 EXPECT_EQ("+1 203-925-4602", FindNumber("+1 203-925-4602", "us")); | |
| 54 } | |
| 55 | |
| 56 TEST_F(PhoneNumberDetectorTest, FindAndFormatNumber) { | |
| 57 EXPECT_EQ("+16174263000", | |
| 58 FindAndFormatNumber("hello 617-426-3000 blah", "us")); | |
| 59 EXPECT_EQ("", FindAndFormatNumber("hello 617-426-3000 blah", "gb")); | |
| 60 EXPECT_EQ("02076174426", | |
| 61 FindAndFormatNumber("<div>020-7617-4426</div>", "gb")); | |
| 62 EXPECT_EQ("", FindAndFormatNumber("<div>020-7617-4426</div>", "fr")); | |
| 63 EXPECT_EQ("0238966888", FindAndFormatNumber("Tel:02.38.96.68.88", "fr")); | |
| 64 EXPECT_EQ("+18008662453", | |
| 65 FindAndFormatNumber("You can call this number:1-800-866-2453 for" | |
| 66 "more information", "us")); | |
| 67 EXPECT_EQ("+12039254602", FindAndFormatNumber("+1 203-925-4602", "us")); | |
| 68 | |
| 69 // "+1 (650) 333-6000" using fullwidth UTF-8 characters. | |
| 70 EXPECT_EQ("+16503336000", FindAndFormatNumber( | |
| 71 "\xEF\xBC\x8B\xEF\xBC\x91\xE3\x80\x80\xEF\xBC\x88" | |
| 72 "\xEF\xBC\x96\xEF\xBC\x95\xEF\xBC\x90\xEF\xBC\x89" | |
| 73 "\xE3\x80\x80\xEF\xBC\x93\xEF\xBC\x93\xEF\xBC\x93" | |
| 74 "\xE3\x83\xBC\xEF\xBC\x96\xEF\xBC\x90\xEF\xBC\x90" | |
| 75 "\xEF\xBC\x90", "us")); | |
| 76 } | |
| 77 | |
| 78 TEST_F(PhoneNumberDetectorTest, FindAndFormatAlphaNumber) { | |
| 79 // Tests cases with valid alpha numbers. | |
| 80 EXPECT_EQ("+18002378289", FindAndFormatNumber("1 800-BestBuy", "us")); | |
| 81 EXPECT_EQ("+18002378289", FindAndFormatNumber("1-800-BEST-BUY", "us")); | |
| 82 EXPECT_EQ("+18002378289", FindAndFormatNumber("1-800-BEST BUY", "us")); | |
| 83 // TODO(qinmin): support alpha number detection when there are characters on | |
| 84 // either side of the string. | |
| 85 EXPECT_EQ("", FindAndFormatNumber("hello 1-800-BEST-BUY", "us")); | |
| 86 EXPECT_EQ("", FindAndFormatNumber("BestBuy", "us")); | |
| 87 EXPECT_EQ("", FindAndFormatNumber("1-BestBuy", "us")); | |
| 88 EXPECT_EQ("", FindAndFormatNumber("1 BestBuy", "us")); | |
| 89 } | |
| 90 | |
| 91 } // namespace content | |
| OLD | NEW |