Commit 26a9f582 authored by Tommy Li's avatar Tommy Li Committed by Commit Bot

[omnibox] ZeroSuggestProvider::TypeOfResultToRun w/ AutocompleteInput

This CL refactors ZeroSuggestProvider::TypeOfResultToRun to take an
AutocompleteInput as a parameter instead of just the current page URL.

We need this because differentiating the on-focus vs. on-clobber
ZeroSuggest modes requires access to AutocompleteInput::focus_type().

This CL also makes the method 'static', for three reasons:

 1. The function is more easily testable when it only depends on its
    parameters, rather than any state in the member variables.

 2. The member variables store data that's also in the |input|
    parameter, and I didn't want to introduce ambiguity as to which
    is used.

 3. We plan to split MostVisited into its own provider, and we will
    want to share some logic from TypeOfResultToRun. Making this
    method static is a step towards that future.

For reviewability, this CL actually has no functional changes, and
just moves things around.

The followup CL will actually add the on-clobber vs. on-focus logic to
TypeOfResultToRun.

Bug: 1106096
Change-Id: I69c8f2f08a182796be9ce1c66611fe6788a6158a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2360562Reviewed-by: default avatarTomasz Wiszkowski <ender@google.com>
Commit-Queue: Tommy Li <tommycli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799394}
parent 1c0816a5
...@@ -241,7 +241,7 @@ void ZeroSuggestProvider::Start(const AutocompleteInput& input, ...@@ -241,7 +241,7 @@ void ZeroSuggestProvider::Start(const AutocompleteInput& input,
if (!suggest_url.is_valid()) if (!suggest_url.is_valid())
return; return;
result_type_running_ = TypeOfResultToRun(input.current_url(), suggest_url); result_type_running_ = TypeOfResultToRun(client(), input, suggest_url);
if (result_type_running_ == NONE) if (result_type_running_ == NONE)
return; return;
...@@ -674,26 +674,34 @@ void ZeroSuggestProvider::MaybeUseCachedSuggestions() { ...@@ -674,26 +674,34 @@ void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
} }
} }
// static
ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun( ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun(
const GURL& current_url, AutocompleteProviderClient* client,
const AutocompleteInput& input,
const GURL& suggest_url) { const GURL& suggest_url) {
DCHECK(client);
// Check if the URL can be sent in any suggest request. // Check if the URL can be sent in any suggest request.
const TemplateURLService* template_url_service = const TemplateURLService* template_url_service =
client()->GetTemplateURLService(); client->GetTemplateURLService();
DCHECK(template_url_service); DCHECK(template_url_service);
const TemplateURL* default_provider = const TemplateURL* default_provider =
template_url_service->GetDefaultSearchProvider(); template_url_service->GetDefaultSearchProvider();
GURL current_url = input.current_url();
metrics::OmniboxEventProto::PageClassification current_page_classification =
input.current_page_classification();
const bool can_send_current_url = CanSendURL( const bool can_send_current_url = CanSendURL(
current_url, suggest_url, default_provider, current_page_classification_, current_url, suggest_url, default_provider, current_page_classification,
template_url_service->search_terms_data(), client(), false); template_url_service->search_terms_data(), client, false);
// Collect metrics on eligibility. // Collect metrics on eligibility.
GURL arbitrary_insecure_url(kArbitraryInsecureUrlString); GURL arbitrary_insecure_url(kArbitraryInsecureUrlString);
ZeroSuggestEligibility eligibility = ZeroSuggestEligibility::ELIGIBLE; ZeroSuggestEligibility eligibility = ZeroSuggestEligibility::ELIGIBLE;
if (!can_send_current_url) { if (!can_send_current_url) {
const bool can_send_ordinary_url = const bool can_send_ordinary_url =
CanSendURL(arbitrary_insecure_url, suggest_url, default_provider, CanSendURL(arbitrary_insecure_url, suggest_url, default_provider,
current_page_classification_, current_page_classification,
template_url_service->search_terms_data(), client(), false); template_url_service->search_terms_data(), client, false);
eligibility = can_send_ordinary_url eligibility = can_send_ordinary_url
? ZeroSuggestEligibility::URL_INELIGIBLE ? ZeroSuggestEligibility::URL_INELIGIBLE
: ZeroSuggestEligibility::GENERALLY_INELIGIBLE; : ZeroSuggestEligibility::GENERALLY_INELIGIBLE;
...@@ -703,12 +711,12 @@ ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun( ...@@ -703,12 +711,12 @@ ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun(
static_cast<int>(ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE)); static_cast<int>(ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE));
const auto field_trial_variants = const auto field_trial_variants =
OmniboxFieldTrial::GetZeroSuggestVariants(current_page_classification_); OmniboxFieldTrial::GetZeroSuggestVariants(current_page_classification);
if (base::Contains(field_trial_variants, kNoneVariant)) if (base::Contains(field_trial_variants, kNoneVariant))
return NONE; return NONE;
if (current_page_classification_ == OmniboxEventProto::CHROMEOS_APP_LIST) if (current_page_classification == OmniboxEventProto::CHROMEOS_APP_LIST)
return REMOTE_NO_URL; return REMOTE_NO_URL;
// Contextual Open Web - (same client side behavior for multiple variants). // Contextual Open Web - (same client side behavior for multiple variants).
...@@ -716,25 +724,25 @@ ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun( ...@@ -716,25 +724,25 @@ ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun(
base::FeatureList::IsEnabled(omnibox::kOnFocusSuggestionsContextualWeb) || base::FeatureList::IsEnabled(omnibox::kOnFocusSuggestionsContextualWeb) ||
base::FeatureList::IsEnabled( base::FeatureList::IsEnabled(
omnibox::kOnFocusSuggestionsContextualWebOnContent); omnibox::kOnFocusSuggestionsContextualWebOnContent);
if (current_page_classification_ == OmniboxEventProto::OTHER && if (current_page_classification == OmniboxEventProto::OTHER &&
can_send_current_url && contextual_web_suggestions_enabled) { can_send_current_url && contextual_web_suggestions_enabled) {
return REMOTE_SEND_URL; return REMOTE_SEND_URL;
} }
// Reactive Zero-Prefix Suggestions (rZPS) on NTP cases. // Reactive Zero-Prefix Suggestions (rZPS) on NTP cases.
bool remote_no_url_allowed = bool remote_no_url_allowed =
RemoteNoUrlSuggestionsAreAllowed(client(), template_url_service); RemoteNoUrlSuggestionsAreAllowed(client, template_url_service);
if (remote_no_url_allowed) { if (remote_no_url_allowed) {
// NTP Omnibox. // NTP Omnibox.
if ((current_page_classification_ == OmniboxEventProto::NTP || if ((current_page_classification == OmniboxEventProto::NTP ||
current_page_classification_ == current_page_classification ==
OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS) && OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS) &&
base::FeatureList::IsEnabled( base::FeatureList::IsEnabled(
omnibox::kReactiveZeroSuggestionsOnNTPOmnibox)) { omnibox::kReactiveZeroSuggestionsOnNTPOmnibox)) {
return REMOTE_NO_URL; return REMOTE_NO_URL;
} }
// NTP Realbox. // NTP Realbox.
if (current_page_classification_ == OmniboxEventProto::NTP_REALBOX && if (current_page_classification == OmniboxEventProto::NTP_REALBOX &&
base::FeatureList::IsEnabled( base::FeatureList::IsEnabled(
omnibox::kReactiveZeroSuggestionsOnNTPRealbox)) { omnibox::kReactiveZeroSuggestionsOnNTPRealbox)) {
return REMOTE_NO_URL; return REMOTE_NO_URL;
...@@ -755,13 +763,13 @@ ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun( ...@@ -755,13 +763,13 @@ ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun(
#if !defined(OS_IOS) #if !defined(OS_IOS)
// For Desktop and Android, default to REMOTE_NO_URL on the NTP, if allowed. // For Desktop and Android, default to REMOTE_NO_URL on the NTP, if allowed.
if (IsNTPPage(current_page_classification_) && remote_no_url_allowed) if (IsNTPPage(current_page_classification) && remote_no_url_allowed)
return REMOTE_NO_URL; return REMOTE_NO_URL;
#endif #endif
#if defined(OS_ANDROID) || defined(OS_IOS) #if defined(OS_ANDROID) || defined(OS_IOS)
// For Android and iOS, default to MOST_VISITED everywhere except on the SERP. // For Android and iOS, default to MOST_VISITED everywhere except on the SERP.
if (!IsSearchResultsPage(current_page_classification_)) { if (!IsSearchResultsPage(current_page_classification)) {
return MOST_VISITED; return MOST_VISITED;
} }
#endif #endif
......
...@@ -66,13 +66,6 @@ class ZeroSuggestProvider : public BaseSearchProvider { ...@@ -66,13 +66,6 @@ class ZeroSuggestProvider : public BaseSearchProvider {
// Sets |field_trial_triggered_| to false. // Sets |field_trial_triggered_| to false.
void ResetSession() override; void ResetSession() override;
// Calling |Start()| will reset the page classification. This is mainly
// intended for unit testing TypeOfResultToRun().
void SetPageClassificationForTesting(
metrics::OmniboxEventProto::PageClassification classification) {
current_page_classification_ = classification;
}
// Returns the list of experiment stats corresponding to the latest |results_| // Returns the list of experiment stats corresponding to the latest |results_|
// to be logged to SearchboxStats as part of a GWS experiment, if any. // to be logged to SearchboxStats as part of a GWS experiment, if any.
const SearchSuggestionParser::ExperimentStats& experiment_stats() const { const SearchSuggestionParser::ExperimentStats& experiment_stats() const {
...@@ -187,8 +180,12 @@ class ZeroSuggestProvider : public BaseSearchProvider { ...@@ -187,8 +180,12 @@ class ZeroSuggestProvider : public BaseSearchProvider {
// context. // context.
// Logs UMA metrics. Should be called exactly once, on Start(), otherwise the // Logs UMA metrics. Should be called exactly once, on Start(), otherwise the
// meaning of the data logged would change. // meaning of the data logged would change.
ResultType TypeOfResultToRun(const GURL& current_url, //
const GURL& suggest_url); // This method is static for testability and to avoid depending on the
// provider state.
static ResultType TypeOfResultToRun(AutocompleteProviderClient* client,
const AutocompleteInput& input,
const GURL& suggest_url);
AutocompleteProviderListener* listener_; AutocompleteProviderListener* listener_;
......
...@@ -210,7 +210,6 @@ void ZeroSuggestProviderTest::SetZeroSuggestVariantForAllContexts( ...@@ -210,7 +210,6 @@ void ZeroSuggestProviderTest::SetZeroSuggestVariantForAllContexts(
} }
TEST_F(ZeroSuggestProviderTest, AllowZeroSuggestSuggestions) { TEST_F(ZeroSuggestProviderTest, AllowZeroSuggestSuggestions) {
provider_->SetPageClassificationForTesting(metrics::OmniboxEventProto::OTHER);
std::string input_url = "https://example.com/"; std::string input_url = "https://example.com/";
AutocompleteInput prefix_input(base::ASCIIToUTF16(input_url), AutocompleteInput prefix_input(base::ASCIIToUTF16(input_url),
...@@ -262,17 +261,17 @@ TEST_F(ZeroSuggestProviderTest, AllowZeroSuggestSuggestions) { ...@@ -262,17 +261,17 @@ TEST_F(ZeroSuggestProviderTest, AllowZeroSuggestSuggestions) {
} }
TEST_F(ZeroSuggestProviderTest, TypeOfResultToRun) { TEST_F(ZeroSuggestProviderTest, TypeOfResultToRun) {
provider_->SetPageClassificationForTesting(metrics::OmniboxEventProto::OTHER);
GURL current_url = GURL("https://example.com/");
GURL suggest_url = GetSuggestURL(metrics::OmniboxEventProto::OTHER);
// Verifies the unconfigured state. Returns platorm-specific defaults. // Verifies the unconfigured state. Returns platorm-specific defaults.
// TODO(tommycli): The remote_no_url_allowed idiom seems kind of confusing,
// its true meaning seems closer to "expect_remote_no_url". Ideally we can
// simplify the test or make this much more obvious.
auto ExpectPlatformSpecificDefaultZeroSuggestBehavior = auto ExpectPlatformSpecificDefaultZeroSuggestBehavior =
[&](const bool remote_no_url_allowed) { [&](AutocompleteInput& input, const bool remote_no_url_allowed) {
const auto current_page_classification = const auto current_page_classification =
provider_->current_page_classification_; input.current_page_classification();
const auto result_type = GURL suggest_url = GetSuggestURL(current_page_classification);
provider_->TypeOfResultToRun(current_url, suggest_url); const auto result_type = ZeroSuggestProvider::TypeOfResultToRun(
client_.get(), input, suggest_url);
#if !defined(OS_ANDROID) && !defined(OS_IOS) // Desktop #if !defined(OS_ANDROID) && !defined(OS_IOS) // Desktop
EXPECT_EQ(BaseSearchProvider::IsNTPPage(current_page_classification) && EXPECT_EQ(BaseSearchProvider::IsNTPPage(current_page_classification) &&
remote_no_url_allowed remote_no_url_allowed
...@@ -296,37 +295,42 @@ TEST_F(ZeroSuggestProviderTest, TypeOfResultToRun) { ...@@ -296,37 +295,42 @@ TEST_F(ZeroSuggestProviderTest, TypeOfResultToRun) {
result_type); result_type);
#endif #endif
}; };
// Verify OTHER defaults (contextual web).
std::string url("https://www.example.com/");
AutocompleteInput other_input(base::ASCIIToUTF16(url),
metrics::OmniboxEventProto::OTHER,
TestSchemeClassifier());
other_input.set_current_url(GURL(url));
ExpectPlatformSpecificDefaultZeroSuggestBehavior( ExpectPlatformSpecificDefaultZeroSuggestBehavior(
other_input,
/*remote_no_url_allowed=*/false); /*remote_no_url_allowed=*/false);
// Verify the platorm-specific defaults for the NTP. // Verify the platorm-specific defaults for the NTP.
provider_->SetPageClassificationForTesting(metrics::OmniboxEventProto::NTP); AutocompleteInput ntp_input(base::string16(), metrics::OmniboxEventProto::NTP,
suggest_url = GetSuggestURL(metrics::OmniboxEventProto::NTP); TestSchemeClassifier());
ExpectPlatformSpecificDefaultZeroSuggestBehavior( ExpectPlatformSpecificDefaultZeroSuggestBehavior(
ntp_input,
/*remote_no_url_allowed=*/false); /*remote_no_url_allowed=*/false);
// Verify RemoteNoUrl works when the user is signed in. // Verify RemoteNoUrl works when the user is signed in.
EXPECT_CALL(*client_, IsAuthenticated()) EXPECT_CALL(*client_, IsAuthenticated())
.WillRepeatedly(testing::Return(true)); .WillRepeatedly(testing::Return(true));
// Verify the platorm-specific defaults for the NTP.
provider_->SetPageClassificationForTesting(metrics::OmniboxEventProto::NTP);
suggest_url = GetSuggestURL(metrics::OmniboxEventProto::NTP);
ExpectPlatformSpecificDefaultZeroSuggestBehavior( ExpectPlatformSpecificDefaultZeroSuggestBehavior(
ntp_input,
/*remote_no_url_allowed=*/true); /*remote_no_url_allowed=*/true);
// Restore to non-NTP page classification.
provider_->SetPageClassificationForTesting(metrics::OmniboxEventProto::OTHER);
suggest_url = GetSuggestURL(metrics::OmniboxEventProto::OTHER);
CreateRemoteNoUrlFieldTrial(); CreateRemoteNoUrlFieldTrial();
GURL other_suggest_url = GetSuggestURL(metrics::OmniboxEventProto::OTHER);
EXPECT_EQ(ZeroSuggestProvider::ResultType::REMOTE_NO_URL, EXPECT_EQ(ZeroSuggestProvider::ResultType::REMOTE_NO_URL,
provider_->TypeOfResultToRun(current_url, suggest_url)); ZeroSuggestProvider::TypeOfResultToRun(client_.get(), other_input,
other_suggest_url));
// But if the user has signed out, fall back to platform-specific defaults. // But if the user has signed out, fall back to platform-specific defaults.
EXPECT_CALL(*client_, IsAuthenticated()) EXPECT_CALL(*client_, IsAuthenticated())
.WillRepeatedly(testing::Return(false)); .WillRepeatedly(testing::Return(false));
ExpectPlatformSpecificDefaultZeroSuggestBehavior( ExpectPlatformSpecificDefaultZeroSuggestBehavior(
other_input,
/*remote_no_url_allowed=*/false); /*remote_no_url_allowed=*/false);
// Restore authentication state, but now set a non-Google default search // Restore authentication state, but now set a non-Google default search
...@@ -342,6 +346,7 @@ TEST_F(ZeroSuggestProviderTest, TypeOfResultToRun) { ...@@ -342,6 +346,7 @@ TEST_F(ZeroSuggestProviderTest, TypeOfResultToRun) {
turl_model->Add(std::make_unique<TemplateURL>(data)); turl_model->Add(std::make_unique<TemplateURL>(data));
turl_model->SetUserSelectedDefaultSearchProvider(other_search_provider); turl_model->SetUserSelectedDefaultSearchProvider(other_search_provider);
ExpectPlatformSpecificDefaultZeroSuggestBehavior( ExpectPlatformSpecificDefaultZeroSuggestBehavior(
other_input,
/*remote_no_url_allowed=*/false); /*remote_no_url_allowed=*/false);
// Restore Google as the default search provider. // Restore Google as the default search provider.
...@@ -352,13 +357,16 @@ TEST_F(ZeroSuggestProviderTest, TypeOfResultToRun) { ...@@ -352,13 +357,16 @@ TEST_F(ZeroSuggestProviderTest, TypeOfResultToRun) {
SetZeroSuggestVariantForAllContexts( SetZeroSuggestVariantForAllContexts(
ZeroSuggestProvider::kRemoteSendUrlVariant); ZeroSuggestProvider::kRemoteSendUrlVariant);
EXPECT_EQ(ZeroSuggestProvider::ResultType::REMOTE_SEND_URL, EXPECT_EQ(ZeroSuggestProvider::ResultType::REMOTE_SEND_URL,
provider_->TypeOfResultToRun(current_url, suggest_url)); ZeroSuggestProvider::TypeOfResultToRun(client_.get(), other_input,
other_suggest_url));
CreateRemoteNoUrlFieldTrial(); CreateRemoteNoUrlFieldTrial();
EXPECT_EQ(ZeroSuggestProvider::ResultType::REMOTE_NO_URL, EXPECT_EQ(ZeroSuggestProvider::ResultType::REMOTE_NO_URL,
provider_->TypeOfResultToRun(current_url, suggest_url)); ZeroSuggestProvider::TypeOfResultToRun(client_.get(), other_input,
other_suggest_url));
CreateMostVisitedFieldTrial(); CreateMostVisitedFieldTrial();
EXPECT_EQ(ZeroSuggestProvider::ResultType::MOST_VISITED, EXPECT_EQ(ZeroSuggestProvider::ResultType::MOST_VISITED,
provider_->TypeOfResultToRun(current_url, suggest_url)); ZeroSuggestProvider::TypeOfResultToRun(client_.get(), other_input,
other_suggest_url));
// Verify that a wildcard rule works in conjunction with a // Verify that a wildcard rule works in conjunction with a
// page-classification-specific rule. // page-classification-specific rule.
...@@ -373,13 +381,22 @@ TEST_F(ZeroSuggestProviderTest, TypeOfResultToRun) { ...@@ -373,13 +381,22 @@ TEST_F(ZeroSuggestProviderTest, TypeOfResultToRun) {
metrics::OmniboxEventProto::BLANK), metrics::OmniboxEventProto::BLANK),
ZeroSuggestProvider::kNoneVariant}, ZeroSuggestProvider::kNoneVariant},
}); });
provider_->SetPageClassificationForTesting(metrics::OmniboxEventProto::OTHER);
EXPECT_EQ(ZeroSuggestProvider::ResultType::MOST_VISITED, EXPECT_EQ(ZeroSuggestProvider::ResultType::MOST_VISITED,
provider_->TypeOfResultToRun(current_url, suggest_url)); ZeroSuggestProvider::TypeOfResultToRun(client_.get(), other_input,
provider_->SetPageClassificationForTesting(metrics::OmniboxEventProto::BLANK); other_suggest_url));
EXPECT_EQ(
ZeroSuggestProvider::ResultType::NONE, // Test the BLANK page classification to verify the wildcard rule works.
provider_->TypeOfResultToRun(GURL("chrome://newtab/"), suggest_url)); {
std::string url("chrome://newtab/");
AutocompleteInput blank_input(base::ASCIIToUTF16(url),
metrics::OmniboxEventProto::BLANK,
TestSchemeClassifier());
blank_input.set_current_url(GURL(url));
EXPECT_EQ(ZeroSuggestProvider::ResultType::NONE,
ZeroSuggestProvider::TypeOfResultToRun(
client_.get(), blank_input,
GetSuggestURL(metrics::OmniboxEventProto::BLANK)));
}
} }
TEST_F(ZeroSuggestProviderTest, TestDoesNotReturnMatchesForPrefix) { TEST_F(ZeroSuggestProviderTest, TestDoesNotReturnMatchesForPrefix) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment