#include "components/omnibox/browser/zero_suggest_verbatim_match_provider.h"
#include <memory>
#include <string>
#include "base/strings/utf_string_conversions.h"
#include "base/task/cancelable_task_tracker.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/history_types.h"
#include "components/omnibox/browser/fake_autocomplete_provider_client.h"
#include "components/omnibox/browser/mock_autocomplete_provider_client.h"
#include "components/omnibox/browser/test_scheme_classifier.h"
#include "components/omnibox/common/omnibox_features.h"
#include "components/search_engines/search_engines_test_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/omnibox_event.pb.h"
namespace {
#if BUILDFLAG(IS_ANDROID)
std::unique_ptr<TemplateURLData> GenerateSimpleTemplateURLData(
const std::string& keyword) {
auto data = std::make_unique<TemplateURLData>();
data->SetShortName(base::UTF8ToUTF16(keyword));
data->SetKeyword(base::UTF8ToUTF16(keyword));
data->SetURL(std::string("https://") + keyword + "/q={searchTerms}");
return data;
}
#endif
_;
class MockHistoryService : public history::HistoryService { … };
}
class ZeroSuggestVerbatimMatchProviderTest
: public testing::TestWithParam<
metrics::OmniboxEventProto::PageClassification> { … };
bool ZeroSuggestVerbatimMatchProviderTest::IsVerbatimMatchEligible() const { … }
void ZeroSuggestVerbatimMatchProviderTest::SetUp() { … }
TEST_P(ZeroSuggestVerbatimMatchProviderTest,
NoVerbatimMatchWithUserTextInOmnibox) { … }
TEST_P(ZeroSuggestVerbatimMatchProviderTest,
NoVerbatimMatchWithUserTextInOmniboxInIncognito) { … }
TEST_P(ZeroSuggestVerbatimMatchProviderTest, OffersVerbatimMatchOnFocus) { … }
TEST_P(ZeroSuggestVerbatimMatchProviderTest,
OffersVerbatimMatchOnFocusInIncognito) { … }
TEST_P(ZeroSuggestVerbatimMatchProviderTest, NoVerbatimMatchWithEmptyInput) { … }
TEST_P(ZeroSuggestVerbatimMatchProviderTest,
NoVerbatimMatchWithEmptyInputInIncognito) { … }
TEST_P(ZeroSuggestVerbatimMatchProviderTest, OffersVerbatimMatchOnClobber) { … }
TEST_P(ZeroSuggestVerbatimMatchProviderTest,
OffersVerbatimMatchOnClobberInIncognito) { … }
#if !BUILDFLAG(IS_ANDROID)
TEST_P(ZeroSuggestVerbatimMatchProviderTest,
DoesNotAttemptToPopulateFillIntoEditWithFeatureDisabled) { … }
#endif
#if BUILDFLAG(IS_ANDROID)
TEST_P(ZeroSuggestVerbatimMatchProviderTest,
NoFillIntoEditResolutionWithNoSearchEngines) {
base::test::ScopedFeatureList features;
std::string url("https://www.search.com/q=abc");
AutocompleteInput input(std::u16string(),
GetParam(), TestSchemeClassifier());
input.set_current_title(u"title");
input.set_current_url(GURL(url));
input.set_focus_type(metrics::OmniboxFocusType::INTERACTION_CLOBBER);
provider_->Start(input, false);
if (IsVerbatimMatchEligible()) {
ASSERT_FALSE(provider_->matches().empty());
ASSERT_EQ(u"https://www.search.com/q=abc",
provider_->matches()[0].fill_into_edit);
ASSERT_EQ(u"title", provider_->matches()[0].description);
}
}
TEST_P(ZeroSuggestVerbatimMatchProviderTest,
UpdateFillIntoEditWhenUrlMatchesSearchResultsPage) {
base::test::ScopedFeatureList features;
std::unique_ptr<TemplateURLData> engine =
GenerateSimpleTemplateURLData("www.search.com");
mock_client_.GetTemplateURLService()->ApplyDefaultSearchChangeForTesting(
engine.get(), DefaultSearchManager::FROM_USER);
std::string url("https://www.search.com/q=abc");
AutocompleteInput input(std::u16string(),
GetParam(), TestSchemeClassifier());
input.set_current_title(u"title");
input.set_current_url(GURL(url));
input.set_focus_type(metrics::OmniboxFocusType::INTERACTION_CLOBBER);
provider_->Start(input, false);
if (IsVerbatimMatchEligible()) {
ASSERT_FALSE(provider_->matches().empty());
ASSERT_EQ(u"abc", provider_->matches()[0].fill_into_edit);
ASSERT_EQ(u"title", provider_->matches()[0].description);
}
}
TEST_P(ZeroSuggestVerbatimMatchProviderTest,
DontUpdateFillIntoEditWhenUrlMatchesNonDefaultSearchEngine) {
base::test::ScopedFeatureList features;
std::unique_ptr<TemplateURLData> engine =
GenerateSimpleTemplateURLData("www.search.com");
TemplateURLService::Initializer other_engines[] = {
{"non-default", "https://www.non-default.com/q=abc", "non-default"}};
search_engines::SearchEnginesTestEnvironment test_environment(
{.template_url_service_initializer = other_engines});
mock_client_.set_template_url_service(
test_environment.template_url_service());
mock_client_.GetTemplateURLService()->ApplyDefaultSearchChangeForTesting(
engine.get(), DefaultSearchManager::FROM_USER);
std::string url("https://www.non-default.com/q=abc");
AutocompleteInput input(std::u16string(),
GetParam(), TestSchemeClassifier());
input.set_current_title(u"title");
input.set_current_url(GURL(url));
input.set_focus_type(metrics::OmniboxFocusType::INTERACTION_CLOBBER);
provider_->Start(input, false);
if (IsVerbatimMatchEligible()) {
ASSERT_FALSE(provider_->matches().empty());
ASSERT_EQ(u"https://www.non-default.com/q=abc",
provider_->matches()[0].fill_into_edit);
ASSERT_EQ(u"title", provider_->matches()[0].description);
}
mock_client_.set_template_url_service(nullptr);
}
#endif
TEST_P(ZeroSuggestVerbatimMatchProviderTest,
MissingPageTitle_NoHistoryService) { … }
TEST_P(ZeroSuggestVerbatimMatchProviderTest,
MissingPageTitle_WithHistoryService) { … }
TEST_P(ZeroSuggestVerbatimMatchProviderTest,
MissingPageTitle_WithHistoryService_Synchronous) { … }
TEST_P(ZeroSuggestVerbatimMatchProviderTest,
MissingPageTitle_CallbackCanceled) { … }
INSTANTIATE_TEST_SUITE_P(
ZeroSuggestVerbatimMatchProviderNonIncognitoTests,
ZeroSuggestVerbatimMatchProviderTest,
::testing::Values(
metrics::OmniboxEventProto::OTHER,
metrics::OmniboxEventProto::
SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT,
metrics::OmniboxEventProto::
SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT,
metrics::OmniboxEventProto::NTP,
metrics::OmniboxEventProto::BLANK,
metrics::OmniboxEventProto::HOME_PAGE,
metrics::OmniboxEventProto::OTHER_ON_CCT,
metrics::OmniboxEventProto::SEARCH_RESULT_PAGE_ON_CCT,
metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS),
+[](const ::testing::TestParamInfo<
metrics::OmniboxEventProto::PageClassification> context)
-> std::string { … };