#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "components/search_engines/template_url.h"
#include <stddef.h>
#include <string>
#include "base/base64.h"
#include "base/base_paths.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/i18n/case_conversion.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/with_feature_override.h"
#include "components/google/core/common/google_util.h"
#include "components/omnibox/common/omnibox_features.h"
#include "components/search_engines/regulatory_extension_type.h"
#include "components/search_engines/search_engines_switches.h"
#include "components/search_engines/search_terms_data.h"
#include "components/search_engines/template_url_data.h"
#include "components/search_engines/testing_search_terms_data.h"
#include "net/base/url_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/omnibox_event.pb.h"
#include "third_party/metrics_proto/omnibox_input_type.pb.h"
#include "third_party/omnibox_proto/chrome_searchbox_stats.pb.h"
#include "ui/base/device_form_factor.h"
ASCIIToUTF16;
CreatedByPolicy;
RequestSource;
namespace {
bool IsLowerCase(const std::u16string& str) { … }
}
class TemplateURLTest : public testing::Test { … };
void TemplateURLTest::CheckSuggestBaseURL(
const std::string& base_url,
const std::string& base_suggest_url) const { … }
void TemplateURLTest::ExpectPostParamIs(const TemplateURLRef::PostParam& param,
const std::string& name,
const std::string& value,
const std::string& content_type) { … }
void TemplateURLTest::ExpectContainsPostParam(
const TemplateURLRef::PostParams& params,
const std::string& name,
const std::string& value,
const std::string& content_type) { … }
TEST_F(TemplateURLTest, Defaults) { … }
TEST_F(TemplateURLTest, TestValidWithComplete) { … }
TEST_F(TemplateURLTest, URLRefTestSearchTerms) { … }
TEST_F(TemplateURLTest, URLRefTestCount) { … }
TEST_F(TemplateURLTest, URLRefTestCount2) { … }
TEST_F(TemplateURLTest, URLRefTestIndices) { … }
TEST_F(TemplateURLTest, URLRefTestIndices2) { … }
TEST_F(TemplateURLTest, URLRefTestEncoding) { … }
TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) { … }
TEST_F(TemplateURLTest, ImageThumbnailContentTypePostParams) { … }
TEST_F(TemplateURLTest, ImageURLWithGetShouldNotCrash) { … }
TEST_F(TemplateURLTest, ParsePlayStoreDefinitions) { … }
TEST_F(TemplateURLTest, SetPrepopulatedAndParse) { … }
TEST_F(TemplateURLTest, SetPrepopulatedAndReplace) { … }
TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) { … }
TEST_F(TemplateURLTest, URLRefTestEncoding2) { … }
TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) { … }
TEST_F(TemplateURLTest, URLRefTermToWide) { … }
TEST_F(TemplateURLTest, DisplayURLToURLRef) { … }
TEST_F(TemplateURLTest, ReplaceSearchTerms) { … }
TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) { … }
TEST_F(TemplateURLTest, ReplaceSearchTermsMultipleEncodings) { … }
void TemplateURLTest::TestReplaceSearcboxStats(
bool is_prefetch,
const std::string& expected_prefetch_param,
const std::string& expected_gs_lcrp_param) { … }
TEST_F(TemplateURLTest, ReplaceSearchboxStats) { … }
TEST_F(TemplateURLTest, ReplaceCursorPosition) { … }
TEST_F(TemplateURLTest, ReplaceInputType) { … }
TEST_F(TemplateURLTest, ReplaceOmniboxFocusType) { … }
TEST_F(TemplateURLTest, GetRegulatoryExtensionType) { … }
TEST_F(TemplateURLTest, GetRegulatoryExtension_NoExtension) { … }
TEST_F(TemplateURLTest, GetRegulatoryExtension_OnlyDefaultExtension) { … }
TEST_F(TemplateURLTest, GetRegulatoryExtension_WithDefaultAndEEAExtensions) { … }
class TemplateURLOnePrefetchSourceTest : public base::test::WithFeatureOverride,
public TemplateURLTest { … };
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(…);
TEST_P(TemplateURLOnePrefetchSourceTest, ReplaceIsPrefetch) { … }
TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) { … }
#if BUILDFLAG(IS_ANDROID)
TEST_F(TemplateURLTest, PlayAPIAttributionEnabled) {
base::test::ScopedFeatureList feature_list{
switches::kSearchEngineChoiceAttribution};
const struct TestData {
const char* url;
std::u16string terms;
bool created_from_play_api;
const char* output;
} test_data[] = {
{"http://foo/?q={searchTerms}", u"bar", false, "http://foo/?q=bar"},
{"http://foo/?q={searchTerms}", u"bar", true,
"http://foo/?q=bar&chrome_dse_attribution=1"}};
TemplateURLData data;
for (const auto& entry : test_data) {
data.SetURL(entry.url);
data.created_from_play_api = entry.created_from_play_api;
TemplateURL url(data);
EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
GURL result(url.url_ref().ReplaceSearchTerms(
TemplateURLRef::SearchTermsArgs(entry.terms), search_terms_data_));
ASSERT_TRUE(result.is_valid());
EXPECT_EQ(entry.output, result.spec());
}
}
TEST_F(TemplateURLTest, PlayAPIAttributionDisabled) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(switches::kSearchEngineChoiceAttribution);
const struct TestData {
const char* url;
std::u16string terms;
bool created_from_play_api;
const char* output;
} test_data[] = {
{"http://foo/?q={searchTerms}", u"bar", false, "http://foo/?q=bar"},
{"http://foo/?q={searchTerms}", u"bar", true, "http://foo/?q=bar"}};
TemplateURLData data;
for (const auto& entry : test_data) {
data.SetURL(entry.url);
data.created_from_play_api = entry.created_from_play_api;
TemplateURL url(data);
EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
GURL result(url.url_ref().ReplaceSearchTerms(
TemplateURLRef::SearchTermsArgs(entry.terms), search_terms_data_));
ASSERT_TRUE(result.is_valid());
EXPECT_EQ(entry.output, result.spec());
}
}
#endif
TEST_F(TemplateURLTest, Suggestions) { … }
TEST_F(TemplateURLTest, RLZ) { … }
TEST_F(TemplateURLTest, RLZFromAppList) { … }
TEST_F(TemplateURLTest, HostAndSearchTermKey) { … }
TEST_F(TemplateURLTest, SearchTermKeyLocation) { … }
TEST_F(TemplateURLTest, GoogleBaseSuggestURL) { … }
TEST_F(TemplateURLTest, ParseParameterKnown) { … }
TEST_F(TemplateURLTest, ParseParameterUnknown) { … }
TEST_F(TemplateURLTest, ParseURLEmpty) { … }
TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) { … }
TEST_F(TemplateURLTest, ParseURLNoKnownParameters) { … }
TEST_F(TemplateURLTest, ParseURLTwoParameters) { … }
TEST_F(TemplateURLTest, ParseURLNestedParameter) { … }
TEST_F(TemplateURLTest, SearchSourceId) { … }
TEST_F(TemplateURLTest, SearchClient) { … }
TEST_F(TemplateURLTest, SuggestClient) { … }
TEST_F(TemplateURLTest, SuggestRequestIdentifier) { … }
TEST_F(TemplateURLTest, ZeroSuggestCacheDuration) { … }
TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) { … }
TEST_F(TemplateURLTest, GetURLOnlyOneURL) { … }
TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) { … }
TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) { … }
TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) { … }
TEST_F(TemplateURLTest, ExtractSearchTermsFromNonUTF8URL) { … }
TEST_F(TemplateURLTest, ExtractSearchTermsWithPrefixAndSuffix) { … }
TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) { … }
TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) { … }
TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) { … }
TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) { … }
TEST_F(TemplateURLTest, SuggestQueryParams) { … }
TEST_F(TemplateURLTest, ExtraQueryParams) { … }
TEST_F(TemplateURLTest, ReplacePageClassification) { … }
TEST_F(TemplateURLTest, IsSearchURL) { … }
TEST_F(TemplateURLTest, SearchboxVersionIncludedForAnswers) { … }
TEST_F(TemplateURLTest, SessionToken) { … }
TEST_F(TemplateURLTest, ContextualSearchParameters) { … }
TEST_F(TemplateURLTest, GenerateKeyword) { … }
TEST_F(TemplateURLTest, KeepSearchTermsInURL) { … }
TEST_F(TemplateURLTest, GenerateSearchURL) { … }
TEST_F(TemplateURLTest, GenerateURL_NoRegulatoryExtensions) { … }
TEST_F(TemplateURLTest, GenerateURL_WithEmptyRegulatoryExtensions) { … }
TEST_F(TemplateURLTest, GenerateURL_WithFullRegulatoryExtensions) { … }
TEST_F(TemplateURLTest, GenerateURL_RegulatoryExtensionVariantHistograms) { … }
TEST_F(TemplateURLTest, GenerateSuggestionURL) { … }
TEST_F(TemplateURLTest, PrefetchQueryParameters) { … }
TEST_F(TemplateURLTest, InvalidateCachedValues) { … }
TEST_F(TemplateURLTest, PathWildcard) { … }
TEST_F(TemplateURLTest, SideImageSearchParams) { … }
TEST_F(TemplateURLTest, ImageTranslate) { … }
TEST_F(TemplateURLTest, ImageSearchBrandingLabel) { … }
struct IsBetterThanEngineTestEngine { … };
TemplateURL CreateEngineFromTestEngine(
const IsBetterThanEngineTestEngine& engine) { … }
struct IsBetterThanEngineTestCase { … } kTestCases[] = …;
class TemplateURLIsBetterThanEngineTest
: public TemplateURLTest,
public testing::WithParamInterface<IsBetterThanEngineTestCase> { … };
std::string ParamToTestSuffix(
const ::testing::TestParamInfo<IsBetterThanEngineTestCase>& info) { … }
INSTANTIATE_TEST_SUITE_P(…);
TEST_P(TemplateURLIsBetterThanEngineTest, Compare) { … }