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