Home | History | Annotate | Download | only in android
      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/email_detector.h"
      6 
      7 #include "base/strings/utf_string_conversions.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 
     10 namespace content {
     11 
     12 class EmailDetectorTest : public testing::Test {
     13  public:
     14   static void FindAndCheckEmail(const std::string& content,
     15                                 const std::string& expected) {
     16     base::string16 content_16 = UTF8ToUTF16(content);
     17     base::string16 result_16;
     18     size_t start, end;
     19     EmailDetector detector;
     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     }
     25     EXPECT_EQ(expected, UTF16ToUTF8(result_16));
     26     EXPECT_EQ(expected, content_text);
     27   }
     28 };
     29 
     30 TEST_F(EmailDetectorTest, FindEmail) {
     31   FindAndCheckEmail("please email test (at) testing.com", "test (at) testing.com");
     32   FindAndCheckEmail("please email test (at) 123.456.co.uk.", "test (at) 123.456.co.uk");
     33   FindAndCheckEmail("My email is 'a (at) b.org'.", "a (at) b.org");
     34   FindAndCheckEmail("123 (at) bcd.org", "123 (at) bcd.org");
     35   FindAndCheckEmail("[quitelongwelllongemailaddress (at) somequitelongdomain.org]",
     36                     "quitelongwelllongemailaddress (at) somequitelongdomain.org");
     37   FindAndCheckEmail("Should find the first (at) email.org not the second (at) email.org",
     38                     "first (at) email.org");
     39   FindAndCheckEmail("Email:HELLO (at) SOMETHING.COM", "HELLO (at) SOMETHING.COM");
     40   FindAndCheckEmail("Email SOMEONE (at) GOOGLE.COM for details.",
     41                     "SOMEONE (at) GOOGLE.COM");
     42   FindAndCheckEmail("It's \"testadd (at) company.fr\"", "testadd (at) company.fr");
     43   FindAndCheckEmail("This is not an @emailaddress.com", "");
     44   FindAndCheckEmail("Apples @2.50 each", "");
     45   FindAndCheckEmail("Log on to google.com", "");
     46   FindAndCheckEmail("Try someone@, they might know.", "");
     47   FindAndCheckEmail("No, bob@com is not an email address.", "");
     48   FindAndCheckEmail("@", "");
     49   FindAndCheckEmail("Just bob @google.com", "");
     50   FindAndCheckEmail("Why not call larry@google and ask him.", "");
     51 }
     52 
     53 }  // namespace content
     54