blob: 8a03f5c75aaf23d9ffe046b0c0c7c1acb3196a2f [file] [log] [blame]
// Copyright 2018 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 "chrome/browser/ui/omnibox/lookalike_url_navigation_observer.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
TEST(LookalikeUrlNavigationObserverTest, IsEditDistanceAtMostOne) {
const struct TestCase {
const wchar_t* domain;
const wchar_t* top_domain;
bool expected;
} kTestCases[] = {
{L"", L"", true},
{L"a", L"a", true},
{L"a", L"", true},
{L"", L"a", true},
{L"", L"ab", false},
{L"ab", L"", false},
{L"ab", L"a", true},
{L"a", L"ab", true},
{L"ab", L"b", true},
{L"b", L"ab", true},
{L"ab", L"ab", true},
{L"", L"ab", false},
{L"ab", L"", false},
{L"a", L"abc", false},
{L"abc", L"a", false},
{L"aba", L"ab", true},
{L"ba", L"aba", true},
{L"abc", L"ac", true},
{L"ac", L"abc", true},
// Same length.
{L"xbc", L"ybc", true},
{L"axc", L"ayc", true},
{L"abx", L"aby", true},
// Should also work for non-ASCII.
{L"é", L"", true},
{L"", L"é", true},
{L"tést", L"test", true},
{L"test", L"tést", true},
{L"tés", L"test", false},
{L"test", L"tés", false},
// Real world test cases.
{L"google.com", L"gooogle.com", true},
{L"gogle.com", L"google.com", true},
{L"googlé.com", L"google.com", true},
{L"google.com", L"googlé.com", true},
// Different by two characters.
{L"google.com", L"goooglé.com", false},
};
for (const TestCase& test_case : kTestCases) {
bool result = LookalikeUrlNavigationObserver::IsEditDistanceAtMostOne(
base::WideToUTF16(test_case.domain),
base::WideToUTF16(test_case.top_domain));
EXPECT_EQ(test_case.expected, result);
}
}