Index: components/autofill/core/common/autofill_util_unittest.cc |
diff --git a/components/autofill/core/common/autofill_util_unittest.cc b/components/autofill/core/common/autofill_util_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2e537cac326a2014b01dca9c706ae4058cefc547 |
--- /dev/null |
+++ b/components/autofill/core/common/autofill_util_unittest.cc |
@@ -0,0 +1,86 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/autofill/core/common/autofill_util.h" |
+ |
+#include "base/command_line.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "components/autofill/core/common/autofill_switches.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace autofill { |
+ |
+// Tests for ContainsTokenThatStartsWith(). |
+TEST(AutofillUtilTest, ContainsTokenThatStartsWith) { |
+ // ContainsTokenThatStartsWith should not work yet without a flag. |
+ EXPECT_FALSE(ContainsTokenThatStartsWith(base::ASCIIToUTF16("ab@cd.b"), |
+ base::ASCIIToUTF16("a"), false)); |
+ |
+ // Token matching is currently behind a flag. |
+ base::CommandLine::ForCurrentProcess()->AppendSwitch( |
+ autofill::switches::kEnableSuggestionsWithSubstringMatch); |
+ |
+ const struct { |
+ const char* const field_suggestion; |
+ const char* const field_contents; |
+ bool case_sensitive; |
+ bool expected_result; |
+ } test_cases[] = { |
+ {"ab@cd.b", "a", false, true}, |
+ {"ab@cd.b", "b", false, true}, |
+ {"ab@cd.b", "Ab", false, true}, |
+ {"ab@cd.b", "Ab", true, false}, |
+ {"ab@cd.b", "cd", true, true}, |
+ {"ab@cd.b", "d", false, false}, |
+ {"ab@cd.b", "b@", true, false}, |
+ {"ab@cd.b", "ab", false, true}, |
+ {"", "ab", false, false}, |
+ {"", "ab", true, false}, |
+ {"ab", "", false, true}, |
+ {"ab", "", true, true}, |
+ }; |
+ |
+ for (size_t i = 0; i < arraysize(test_cases); ++i) { |
+ SCOPED_TRACE(testing::Message() |
+ << "suggestion = " << test_cases[i].field_suggestion |
+ << ", contents = " << test_cases[i].field_contents); |
+ |
+ EXPECT_EQ(test_cases[i].expected_result, |
+ ContainsTokenThatStartsWith( |
+ base::ASCIIToUTF16(test_cases[i].field_suggestion), |
+ base::ASCIIToUTF16(test_cases[i].field_contents), |
+ test_cases[i].case_sensitive)); |
+ } |
+} |
+ |
+// Tests for GetTextSelectionStart(). |
+TEST(AutofillUtilTest, GetTextSelectionStart) { |
+ const struct { |
+ const char* const field_suggestion; |
+ const char* const field_contents; |
+ size_t expected_start; |
+ } test_cases[] = { |
+ {"ab@cd.b", "a", 1}, |
+ {"ab@cd.b", "Ab", 2}, |
+ {"ab@cd.b", "cd", 5}, |
+ {"ab@cd.b", "ab@c", 4}, |
+ {"ab@cd.b", "cd.b", 7}, |
+ {"ab@cd.b", "b", 7}, |
+ {"ba.a.ab", "a.a", 6}, |
+ {"texample@example.com", "example", 16}, |
+ }; |
+ |
+ for (size_t i = 0; i < arraysize(test_cases); ++i) { |
+ SCOPED_TRACE(testing::Message() |
+ << "suggestion = " << test_cases[i].field_suggestion |
+ << ", contents = " << test_cases[i].field_contents); |
+ |
+ EXPECT_EQ(test_cases[i].expected_start, |
+ GetTextSelectionStart( |
+ base::ASCIIToUTF16(test_cases[i].field_suggestion), |
+ base::ASCIIToUTF16(test_cases[i].field_contents))); |
+ } |
+} |
+ |
+} // autofill |