Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1046)

Unified Diff: components/autofill/core/common/autofill_util_unittest.cc

Issue 962673004: [Autofill/Autocomplete Feature] Substring matching instead of prefix matching. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Incorporated Vaclav's review comments. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..384881f6247910781944862511a46dca25af4337
--- /dev/null
+++ b/components/autofill/core/common/autofill_util_unittest.cc
@@ -0,0 +1,99 @@
+// 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 IsContentsPrefixOfSuggestionToken().
+TEST(AutofillUtilTest, IsContentsPrefixOfSuggestionToken) {
+ // IsContentsPrefixOfSuggestionToken should not work yet without a flag.
+ EXPECT_FALSE(IsContentsPrefixOfSuggestionToken(
+ base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("a"), false));
+
+ // Token matching is currently behind a flag.
+ base::CommandLine::ForCurrentProcess()->AppendSwitch(
+ autofill::switches::kEnableSuggestionsWithSubstringMatch);
+
+ EXPECT_TRUE(IsContentsPrefixOfSuggestionToken(
+ base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("a"), false));
+ EXPECT_TRUE(IsContentsPrefixOfSuggestionToken(
+ base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("b"), false));
+ EXPECT_TRUE(IsContentsPrefixOfSuggestionToken(
+ base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("Ab"), false));
+ EXPECT_FALSE(IsContentsPrefixOfSuggestionToken(
+ base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("Ab"), true));
+ EXPECT_TRUE(IsContentsPrefixOfSuggestionToken(
+ base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("cd"), true));
+ EXPECT_FALSE(IsContentsPrefixOfSuggestionToken(
+ base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("d"), false));
+ EXPECT_FALSE(IsContentsPrefixOfSuggestionToken(
+ base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("b@"), true));
+ EXPECT_FALSE(IsContentsPrefixOfSuggestionToken(
+ base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("cd.b"), false));
+ EXPECT_FALSE(IsContentsPrefixOfSuggestionToken(
+ base::string16(), base::ASCIIToUTF16("ab"), false));
+ EXPECT_FALSE(IsContentsPrefixOfSuggestionToken(
+ base::string16(), base::ASCIIToUTF16("ab"), true));
+ EXPECT_TRUE(IsContentsPrefixOfSuggestionToken(base::ASCIIToUTF16("ab"),
+ base::string16(), false));
+ EXPECT_TRUE(IsContentsPrefixOfSuggestionToken(base::ASCIIToUTF16("ab"),
+ base::string16(), true));
+}
+
+// Tests for ComputeRange().
+TEST(AutofillUtilTest, ComputeRange) {
+ const struct {
+ base::string16 field_suggestion;
+ base::string16 field_contents;
+ size_t expected_offset;
+ size_t expected_start;
+ size_t expected_end;
+ } test_cases[] = {
+ {base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("a"), 0, 1, 7},
+ {base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("Ab"), 0, 2, 7},
+ {base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("cd"), 3, 5, 7},
+ {base::ASCIIToUTF16("ab@cd.b"),
+ base::ASCIIToUTF16("dc"),
+ base::string16::npos,
+ 0,
+ 0},
+ {base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("ab@c"), 0, 4, 7},
+ {base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("cd.b"), 3, 7, 7},
+ {base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("b"), 6, 7, 7},
+ {base::ASCIIToUTF16("ba.a.ab"), base::ASCIIToUTF16("a.a"), 3, 6, 7},
+ {base::ASCIIToUTF16("texample@example.com"),
+ base::ASCIIToUTF16("example"),
+ 9,
+ 16,
+ 20},
+ {base::ASCIIToUTF16("texample@exam.com"),
+ base::ASCIIToUTF16("example"),
+ base::string16::npos,
+ 0,
+ 0}};
+
+ 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);
+
+ size_t start = 0;
+ size_t end = 0;
+ size_t offset = ComputeRange(test_cases[i].field_suggestion,
+ test_cases[i].field_contents, &start, &end);
+ EXPECT_EQ(test_cases[i].expected_offset, offset);
+ if (base::string16::npos != offset) {
+ EXPECT_EQ(test_cases[i].expected_start, start);
+ EXPECT_EQ(test_cases[i].expected_end, end);
+ }
+ }
+}
+
+} // autofill

Powered by Google App Engine
This is Rietveld 408576698