1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/omnibox/browser/featured_search_provider.h"
#include <stddef.h>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/format_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "components/history_embeddings/history_embeddings_features.h"
#include "components/omnibox/browser/autocomplete_input.h"
#include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/autocomplete_match_type.h"
#include "components/omnibox/browser/autocomplete_result.h"
#include "components/omnibox/browser/fake_autocomplete_provider_client.h"
#include "components/omnibox/browser/omnibox_prefs.h"
#include "components/omnibox/browser/test_scheme_classifier.h"
#include "components/omnibox/common/omnibox_feature_configs.h"
#include "components/omnibox/common/omnibox_features.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/testing_pref_service.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_data.h"
#include "components/search_engines/template_url_service.h"
#include "components/search_engines/template_url_starter_pack_data.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/omnibox_event.pb.h"
#include "third_party/metrics_proto/omnibox_focus_type.pb.h"
#include "url/gurl.h"
namespace {
constexpr char16_t kBookmarksKeyword[] = u"@bookmarks";
constexpr char16_t kHistoryKeyword[] = u"@history";
constexpr char16_t kTabsKeyword[] = u"@tabs";
constexpr char16_t kGeminiKeyword[] = u"@gemini";
const std::string kBookmarksUrl =
template_url_starter_pack_data::bookmarks.destination_url;
const std::string kHistoryUrl =
template_url_starter_pack_data::history.destination_url;
const std::string kTabsUrl =
template_url_starter_pack_data::tabs.destination_url;
const std::string kGeminiUrl =
template_url_starter_pack_data::gemini.destination_url;
const std::string kPageUrl =
template_url_starter_pack_data::page.destination_url;
const std::string kAiModeUrl =
template_url_starter_pack_data::ai_mode.destination_url;
struct TestData {
const std::u16string input;
const std::vector<std::string> output;
};
struct IphData {
const IphType iph_type;
const std::u16string iph_contents;
const std::u16string iph_link_text;
const GURL iph_link_url;
};
std::u16string FeaturedKeywordN(int n) {
return base::UTF8ToUTF16("@featured" + base::NumberToString(n));
}
std::string FeaturedUrlN(int n) {
return "https://featured" + base::NumberToString(n) + ".com/q={searchTerms}";
}
} // namespace
class FeaturedSearchProviderTest : public testing::Test {
public:
FeaturedSearchProviderTest(const FeaturedSearchProviderTest&) = delete;
FeaturedSearchProviderTest& operator=(const FeaturedSearchProviderTest&) =
delete;
protected:
FeaturedSearchProviderTest() = default;
~FeaturedSearchProviderTest() override = default;
void SetUp() override {
client_ = std::make_unique<FakeAutocompleteProviderClient>();
provider_ = new FeaturedSearchProvider(client_.get());
omnibox::RegisterProfilePrefs(
static_cast<sync_preferences::TestingPrefServiceSyncable*>(
client_->GetPrefs())
->registry());
}
void TearDown() override { provider_ = nullptr; }
void RunTest(const std::vector<TestData> cases) {
ACMatches matches;
for (size_t i = 0; i < cases.size(); ++i) {
SCOPED_TRACE(base::StringPrintf(
"case %" PRIuS ": %s", i, base::UTF16ToUTF8(cases[i].input).c_str()));
AutocompleteInput input(cases[i].input, metrics::OmniboxEventProto::OTHER,
TestSchemeClassifier());
input.set_allow_exact_keyword_match(false);
provider_->Start(input, false);
EXPECT_TRUE(provider_->done());
matches = provider_->matches();
ASSERT_EQ(cases[i].output.size(), matches.size());
for (size_t j = 0; j < cases[i].output.size(); ++j) {
EXPECT_EQ(matches[j].destination_url, GURL(cases[i].output[j]));
}
}
}
void RunAndVerifyIph(const AutocompleteInput& input,
const std::vector<IphData> expected_iphs) {
provider_->Start(input, false);
EXPECT_TRUE(provider_->done());
ACMatches matches = provider_->matches();
if (matches.size() == expected_iphs.size()) {
for (size_t j = 0; j < expected_iphs.size(); ++j) {
EXPECT_EQ(matches[j].iph_type, expected_iphs[j].iph_type);
EXPECT_EQ(matches[j].contents, expected_iphs[j].iph_contents);
EXPECT_EQ(matches[j].iph_link_text, expected_iphs[j].iph_link_text);
EXPECT_EQ(matches[j].iph_link_url, expected_iphs[j].iph_link_url);
}
} else {
EXPECT_EQ(matches.size(), expected_iphs.size());
}
}
void RunAndVerifyIphTypes(const AutocompleteInput& input,
const std::vector<IphType> expected_iph_types) {
provider_->Start(input, false);
EXPECT_TRUE(provider_->done());
ACMatches matches = provider_->matches();
if (matches.size() == expected_iph_types.size()) {
for (size_t j = 0; j < expected_iph_types.size(); ++j)
EXPECT_EQ(matches[j].iph_type, expected_iph_types[j]);
} else {
EXPECT_EQ(matches.size(), expected_iph_types.size());
}
}
// Populate the TemplateURLService with starter pack entries.
void AddStarterPackEntriesToTemplateUrlService() {
std::vector<std::unique_ptr<TemplateURLData>> turls =
template_url_starter_pack_data::GetStarterPackEngines();
for (auto& turl : turls) {
client_->GetTemplateURLService()->Add(
std::make_unique<TemplateURL>(std::move(*turl)));
}
}
// Add a new featured search engine to the TemplateURLService.
void AddFeaturedEnterpriseSearchEngine(
const std::u16string& keyword,
const std::string& url,
const TemplateURLData::PolicyOrigin& policy_origin,
const TemplateURLData::ActiveStatus& is_active =
TemplateURLData::ActiveStatus::kTrue) {
TemplateURLData template_url_data;
template_url_data.SetKeyword(keyword);
template_url_data.SetShortName(keyword + u" Name");
template_url_data.SetURL(url);
template_url_data.policy_origin = policy_origin;
template_url_data.enforced_by_policy = true;
template_url_data.featured_by_policy = true;
template_url_data.safe_for_autoreplace = false;
template_url_data.is_active = is_active;
client_->GetTemplateURLService()->Add(
std::make_unique<TemplateURL>(template_url_data));
}
std::unique_ptr<MockAutocompleteProviderClient> client_;
scoped_refptr<FeaturedSearchProvider> provider_;
};
TEST_F(FeaturedSearchProviderTest, NonAtPrefix) {
std::vector<TestData> test_cases = {
// Typing text that doesn't start with "@" should give nothing.
{u"g@rb@g3", {}},
{u"www.google.com", {}},
{u"http:www.google.com", {}},
{u"http://www.google.com", {}},
{u"file:filename", {}},
{u"chrome:", {}},
{u"chrome://", {}},
{u"chrome://version", {}},
};
RunTest(test_cases);
}
TEST_F(FeaturedSearchProviderTest, DoesNotSupportMatchesOnFocus) {
history_embeddings::ScopedFeatureParametersForTesting feature_parameters(
base::BindOnce([](history_embeddings::FeatureParameters& parameters) {
parameters.omnibox_scoped = false;
}));
base::test::ScopedFeatureList features;
features.InitWithFeaturesAndParameters(
{{history_embeddings::kHistoryEmbeddings, {}}},
{omnibox::kStarterPackIPH});
AutocompleteInput input(u"@tabs", metrics::OmniboxEventProto::OTHER,
TestSchemeClassifier());
input.set_focus_type(metrics::OmniboxFocusType::INTERACTION_FOCUS);
provider_->Start(input, false);
EXPECT_TRUE(provider_->matches().empty());
}
TEST_F(FeaturedSearchProviderTest, StarterPack) {
base::test::ScopedFeatureList features;
features.InitAndDisableFeature(omnibox::kStarterPackExpansion);
AddStarterPackEntriesToTemplateUrlService();
std::vector<TestData> typing_scheme_cases = {
// Typing the keyword without '@' or past the keyword shouldn't produce
// results.
{u"b", {}},
{u"bookmarks", {}},
{u"his", {}},
{u"history", {}},
{u"@historyasdjflk", {}},
{u"@bookmarksasld", {}},
{u"tabs", {}},
// With the expansion flag disabled, typing the `@gemini` keyword should
// not provide the Gemini suggestion.
{u"@gemini", {}},
// Typing '@' should give all the starter pack suggestions.
{u"@", {kAiModeUrl, kBookmarksUrl, kHistoryUrl, kTabsUrl}},
// Typing a portion of "@bookmarks" should give the bookmarks suggestion.
{std::u16string(kBookmarksKeyword, 0, 3), {kBookmarksUrl}},
{kBookmarksKeyword, {kBookmarksUrl}},
// Typing a portion of "@history" should give the default urls.
{std::u16string(kHistoryKeyword, 0, 3), {kHistoryUrl}},
{kHistoryKeyword, {kHistoryUrl}},
// Typing a portion of "@tabs" should give the default urls.
{std::u16string(kTabsKeyword, 0, 3), {kTabsUrl}},
{kTabsKeyword, {kTabsUrl}},
};
RunTest(typing_scheme_cases);
}
TEST_F(FeaturedSearchProviderTest, StarterPackExpansion) {
base::test::ScopedFeatureList features;
features.InitAndEnableFeature(omnibox::kStarterPackExpansion);
AddStarterPackEntriesToTemplateUrlService();
std::vector<TestData> typing_scheme_cases = {
// Typing the keyword without '@' or past the keyword shouldn't produce
// results.
{u"b", {}},
{u"bookmarks", {}},
{u"his", {}},
{u"history", {}},
{u"@historyasdjflk", {}},
{u"@bookmarksasld", {}},
{u"tabs", {}},
{u"gemi", {}},
// Typing '@' should give all the starter pack suggestions.
{u"@", {kAiModeUrl, kBookmarksUrl, kGeminiUrl, kHistoryUrl, kTabsUrl}},
// Typing a portion of "@bookmarks" should give the bookmarks suggestion.
{std::u16string(kBookmarksKeyword, 0, 3), {kBookmarksUrl}},
{kBookmarksKeyword, {kBookmarksUrl}},
// Typing a portion of "@history" should give the default urls.
{std::u16string(kHistoryKeyword, 0, 3), {kHistoryUrl}},
{kHistoryKeyword, {kHistoryUrl}},
// Typing a portion of "@tabs" should give the default urls.
{std::u16string(kTabsKeyword, 0, 3), {kTabsUrl}},
{kTabsKeyword, {kTabsUrl}},
// Typing a portion of "@gemini" should give the default urls.
{std::u16string(kGeminiKeyword, 0, 3), {kGeminiUrl}},
{kGeminiKeyword, {kGeminiUrl}},
};
RunTest(typing_scheme_cases);
}
TEST_F(FeaturedSearchProviderTest, StarterPackExpansionRelevance) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({omnibox::kStarterPackExpansion}, {});
omnibox_feature_configs::ScopedConfigForTesting<
omnibox_feature_configs::ContextualSearch>
scoped_config;
scoped_config.Get().starter_pack_page = true;
AddStarterPackEntriesToTemplateUrlService();
AutocompleteInput input(u"@", metrics::OmniboxEventProto::OTHER,
TestSchemeClassifier());
input.set_prevent_inline_autocomplete(true);
provider_->Start(input, false);
EXPECT_TRUE(provider_->done());
ACMatches matches = provider_->matches();
ASSERT_EQ(template_url_starter_pack_data::GetStarterPackEngines().size(),
matches.size());
// Sort the matches according to relevances (in descending order), and make
// sure that the matches are in the expected order.
std::sort(matches.begin(), matches.end(), [](const auto& x, const auto& y) {
return x.relevance > y.relevance;
});
auto expected_match_order = std::vector<std::string>{
kAiModeUrl, kGeminiUrl, kHistoryUrl, kBookmarksUrl, kPageUrl, kTabsUrl,
};
ASSERT_EQ(matches.size(), expected_match_order.size());
for (size_t i = 0; i < matches.size(); i++) {
EXPECT_EQ(matches[i].destination_url, GURL(expected_match_order[i])) << i;
}
}
TEST_F(FeaturedSearchProviderTest, FeaturedEnterpriseSearch) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({omnibox::kStarterPackExpansion}, {});
AddStarterPackEntriesToTemplateUrlService();
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(2), FeaturedUrlN(2),
TemplateURLData::PolicyOrigin::kSiteSearch);
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(1), FeaturedUrlN(1),
TemplateURLData::PolicyOrigin::kSiteSearch);
// Inactive featured enterprise keywords should not be shown.
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(3), FeaturedUrlN(3),
TemplateURLData::PolicyOrigin::kSiteSearch,
TemplateURLData::ActiveStatus::kFalse);
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(5), FeaturedUrlN(5),
TemplateURLData::PolicyOrigin::kSiteSearch);
AddFeaturedEnterpriseSearchEngine(
FeaturedKeywordN(4), FeaturedUrlN(4),
TemplateURLData::PolicyOrigin::kSearchAggregator);
// At most 4 featured enterprise keywords should be shown.
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(6), FeaturedUrlN(6),
TemplateURLData::PolicyOrigin::kSiteSearch);
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(7), FeaturedUrlN(7),
TemplateURLData::PolicyOrigin::kSiteSearch);
std::vector<TestData> typing_scheme_cases = {
// Typing the keyword without '@' or past the keyword shouldn't produce
// results.
{u"f", {}},
{u"feat", {}},
{u"featured1", {}},
{u"featured2abs", {}},
{u"@featured1xxa", {}},
// Typing '@' should give all featured search engines and all the starter
// pack suggestions. The provider does not change the order given
// by TemplateURLService (which returns all keywords with the prefix in
// alphabetical order). Re-ordering by relevance will be made
// later on.
{u"@",
{kAiModeUrl, kBookmarksUrl, FeaturedUrlN(1), FeaturedUrlN(2),
FeaturedUrlN(4), FeaturedUrlN(5), kGeminiUrl, kHistoryUrl, kTabsUrl}},
// Typing a portion of "@featured" should give the featured engine
// suggestions.
{std::u16string(FeaturedKeywordN(1), 0, 3),
{FeaturedUrlN(1), FeaturedUrlN(2), FeaturedUrlN(4), FeaturedUrlN(5)}},
{FeaturedKeywordN(1), {FeaturedUrlN(1)}},
};
RunTest(typing_scheme_cases);
}
TEST_F(FeaturedSearchProviderTest, ZeroSuggestStarterPackIPHSuggestion) {
base::test::ScopedFeatureList features;
features.InitWithFeaturesAndParameters(
{{omnibox::kStarterPackExpansion, {}}, {omnibox::kStarterPackIPH, {}}},
{});
// "Focus" omnibox with zero input to put us in Zero suggest mode.
AutocompleteInput input;
input.set_focus_type(metrics::INTERACTION_FOCUS);
// Run the provider, there should be one match corresponding to IPH for
// Starter Pack.
provider_->Start(input, false);
ACMatches matches = provider_->matches();
EXPECT_EQ(matches.size(), 1u);
EXPECT_EQ(matches[0].type, AutocompleteMatchType::NULL_RESULT_MESSAGE);
EXPECT_EQ(matches[0].iph_type, IphType::kGemini);
// Not in ZPS, the IPH should not be provided.
input.set_focus_type(metrics::INTERACTION_DEFAULT);
provider_->Start(input, false);
matches = provider_->matches();
EXPECT_EQ(matches.size(), 0u);
// "@" state - Confirm expected starter pack is still shown but no ZPS.
AddStarterPackEntriesToTemplateUrlService();
std::vector<TestData> typing_scheme_cases = {
// Typing '@' should give all the starter pack suggestions, and no IPH.
{u"@", {kAiModeUrl, kBookmarksUrl, kGeminiUrl, kHistoryUrl, kTabsUrl}}};
RunTest(typing_scheme_cases);
}
TEST_F(FeaturedSearchProviderTest,
ZeroSuggestStarterPackIPHSuggestion_DeleteMatch) {
history_embeddings::ScopedFeatureParametersForTesting feature_parameters(
base::BindOnce([](history_embeddings::FeatureParameters& parameters) {
parameters.omnibox_scoped = false;
}));
base::test::ScopedFeatureList features;
features.InitWithFeaturesAndParameters(
{{history_embeddings::kHistoryEmbeddings, {}},
{omnibox::kStarterPackIPH, {}}},
{});
PrefService* prefs = client_->GetPrefs();
// "Focus" omnibox with zero input to put us in Zero suggest mode.
AutocompleteInput input;
input.set_focus_type(metrics::INTERACTION_FOCUS);
// Run the provider, there should be one match corresponding to IPH for
// Starter Pack.
EXPECT_FALSE(prefs->GetBoolean(omnibox::kDismissedGeminiIph));
provider_->Start(input, false);
ACMatches matches = provider_->matches();
EXPECT_EQ(matches.size(), 1u);
EXPECT_EQ(matches[0].type, AutocompleteMatchType::NULL_RESULT_MESSAGE);
EXPECT_EQ(matches[0].iph_type, IphType::kGemini);
// Call `DeleteMatch()`, match should be deleted from `matches_` and the pref
// should be set to false.
provider_->DeleteMatch(matches[0]);
matches = provider_->matches();
EXPECT_EQ(matches.size(), 0u);
EXPECT_TRUE(prefs->GetBoolean(omnibox::kDismissedGeminiIph));
// Run the provider again, IPH match should not be provided.
provider_->Start(input, false);
matches = provider_->matches();
EXPECT_EQ(matches.size(), 0u);
}
TEST_F(FeaturedSearchProviderTest,
ZeroSuggestFeaturedEnterpriseSiteSearchIPHSuggestion) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({omnibox::kStarterPackExpansion},
{omnibox::kStarterPackIPH});
AddStarterPackEntriesToTemplateUrlService();
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(2), FeaturedUrlN(2),
TemplateURLData::PolicyOrigin::kSiteSearch);
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(1), FeaturedUrlN(1),
TemplateURLData::PolicyOrigin::kSiteSearch);
// Inactive featured enterprise keywords should not be shown or included in
// IPH.
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(3), FeaturedUrlN(3),
TemplateURLData::PolicyOrigin::kSiteSearch,
TemplateURLData::ActiveStatus::kFalse);
// "Focus" omnibox with zero input to put us in Zero suggest mode.
AutocompleteInput input;
input.set_focus_type(metrics::INTERACTION_FOCUS);
// Run the provider, there should be one match corresponding to IPH for
// Enterprise search aggregator.
provider_->Start(input, false);
ACMatches matches = provider_->matches();
EXPECT_EQ(matches.size(), 1u);
EXPECT_EQ(matches[0].type, AutocompleteMatchType::NULL_RESULT_MESSAGE);
EXPECT_EQ(matches[0].iph_type, IphType::kFeaturedEnterpriseSiteSearch);
EXPECT_EQ(matches[0].contents,
u"Type @ to search across featured1.com, featured2.com");
// Not in ZPS, the IPH should not be provided.
input.set_focus_type(metrics::INTERACTION_DEFAULT);
provider_->Start(input, false);
matches = provider_->matches();
EXPECT_EQ(matches.size(), 0u);
// "@" state - Confirm expected starter pack is still shown but no ZPS.
std::vector<TestData> typing_scheme_cases = {
// Typing '@' should give all the starter pack suggestions, and no IPH.
{u"@",
{kAiModeUrl, kBookmarksUrl, FeaturedUrlN(1), FeaturedUrlN(2), kGeminiUrl,
kHistoryUrl, kTabsUrl}}};
RunTest(typing_scheme_cases);
}
TEST_F(FeaturedSearchProviderTest,
ZeroSuggestFeaturedSiteSearchIPHSuggestion_DeleteMatch) {
history_embeddings::ScopedFeatureParametersForTesting feature_parameters(
base::BindOnce([](history_embeddings::FeatureParameters& parameters) {
parameters.omnibox_scoped = false;
}));
base::test::ScopedFeatureList features;
features.InitWithFeaturesAndParameters(
{{history_embeddings::kHistoryEmbeddings, {}},
{omnibox::kStarterPackExpansion, {}}},
{omnibox::kStarterPackIPH});
AddStarterPackEntriesToTemplateUrlService();
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(2), FeaturedUrlN(2),
TemplateURLData::PolicyOrigin::kSiteSearch);
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(1), FeaturedUrlN(1),
TemplateURLData::PolicyOrigin::kSiteSearch);
// Inactive featured enterprise keywords should not be shown or included in
// IPH.
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(3), FeaturedUrlN(3),
TemplateURLData::PolicyOrigin::kSiteSearch,
TemplateURLData::ActiveStatus::kFalse);
// "Focus" omnibox with zero input to put us in Zero suggest mode.
AutocompleteInput input;
input.set_focus_type(metrics::INTERACTION_FOCUS);
// Run the provider, there should be one match corresponding to IPH for
// featured Enterprise site search.
PrefService* prefs = client_->GetPrefs();
EXPECT_FALSE(prefs->GetBoolean(
omnibox::kDismissedFeaturedEnterpriseSiteSearchIphPrefName));
provider_->Start(input, false);
ACMatches matches = provider_->matches();
EXPECT_EQ(matches.size(), 1u);
EXPECT_EQ(matches[0].type, AutocompleteMatchType::NULL_RESULT_MESSAGE);
EXPECT_EQ(matches[0].iph_type, IphType::kFeaturedEnterpriseSiteSearch);
EXPECT_EQ(matches[0].contents,
u"Type @ to search across featured1.com, featured2.com");
// Call `DeleteMatch()`, match should be deleted from `matches_` and the pref
// should be set to false.
provider_->DeleteMatch(matches[0]);
matches = provider_->matches();
EXPECT_EQ(matches.size(), 0u);
EXPECT_TRUE(prefs->GetBoolean(
omnibox::kDismissedFeaturedEnterpriseSiteSearchIphPrefName));
// Run the provider again, IPH match should not be provided.
provider_->Start(input, false);
matches = provider_->matches();
EXPECT_EQ(matches.size(), 0u);
}
TEST_F(FeaturedSearchProviderTest,
ZeroSuggestStarterPackIPHAfterFeaturedSiteSearchIPHDeleted) {
history_embeddings::ScopedFeatureParametersForTesting feature_parameters(
base::BindOnce([](history_embeddings::FeatureParameters& parameters) {
parameters.omnibox_scoped = false;
}));
base::test::ScopedFeatureList features;
features.InitWithFeaturesAndParameters(
{{history_embeddings::kHistoryEmbeddings, {}},
{omnibox::kStarterPackExpansion, {}},
{omnibox::kStarterPackIPH, {}}},
{});
AddStarterPackEntriesToTemplateUrlService();
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(2), FeaturedUrlN(2),
TemplateURLData::PolicyOrigin::kSiteSearch);
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(1), FeaturedUrlN(1),
TemplateURLData::PolicyOrigin::kSiteSearch);
// Inactive featured enterprise keywords should not be shown or included in
// IPH.
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(3), FeaturedUrlN(3),
TemplateURLData::PolicyOrigin::kSiteSearch,
TemplateURLData::ActiveStatus::kFalse);
// "Focus" omnibox with zero input to put us in Zero suggest mode.
AutocompleteInput input;
input.set_focus_type(metrics::INTERACTION_FOCUS);
// Run the provider, there should be one match corresponding to IPH for
// featured Enterprise site search.
PrefService* prefs = client_->GetPrefs();
EXPECT_FALSE(prefs->GetBoolean(
omnibox::kDismissedFeaturedEnterpriseSiteSearchIphPrefName));
EXPECT_FALSE(prefs->GetBoolean(omnibox::kDismissedGeminiIph));
provider_->Start(input, false);
ACMatches matches = provider_->matches();
EXPECT_EQ(matches.size(), 1u);
EXPECT_EQ(matches[0].type, AutocompleteMatchType::NULL_RESULT_MESSAGE);
EXPECT_EQ(matches[0].iph_type, IphType::kFeaturedEnterpriseSiteSearch);
// Call `DeleteMatch()`, match should be deleted from `matches_` and the pref
// should be set to false.
provider_->DeleteMatch(matches[0]);
matches = provider_->matches();
EXPECT_EQ(matches.size(), 0u);
EXPECT_TRUE(prefs->GetBoolean(
omnibox::kDismissedFeaturedEnterpriseSiteSearchIphPrefName));
EXPECT_FALSE(prefs->GetBoolean(omnibox::kDismissedGeminiIph));
// Run the provider again, there should be one match corresponding to IPH for
// Starter Pack.
EXPECT_FALSE(prefs->GetBoolean(omnibox::kDismissedGeminiIph));
provider_->Start(input, false);
matches = provider_->matches();
EXPECT_EQ(matches.size(), 1u);
EXPECT_EQ(matches[0].type, AutocompleteMatchType::NULL_RESULT_MESSAGE);
EXPECT_EQ(matches[0].iph_type, IphType::kGemini);
// Call `DeleteMatch()`, match should be deleted from `matches_` and the pref
// should be set to false.
provider_->DeleteMatch(matches[0]);
matches = provider_->matches();
EXPECT_EQ(matches.size(), 0u);
EXPECT_TRUE(prefs->GetBoolean(
omnibox::kDismissedFeaturedEnterpriseSiteSearchIphPrefName));
EXPECT_TRUE(prefs->GetBoolean(omnibox::kDismissedGeminiIph));
// Run the provider again, IPH match should not be provided.
provider_->Start(input, false);
matches = provider_->matches();
EXPECT_EQ(matches.size(), 0u);
}
TEST_F(FeaturedSearchProviderTest,
ZeroSuggestEnterpriseSearchAggregatorIPHSuggestion) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({omnibox::kStarterPackExpansion},
{omnibox::kStarterPackIPH});
AddStarterPackEntriesToTemplateUrlService();
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(1), FeaturedUrlN(1),
TemplateURLData::PolicyOrigin::kSiteSearch);
AddFeaturedEnterpriseSearchEngine(
FeaturedKeywordN(4), FeaturedUrlN(4),
TemplateURLData::PolicyOrigin::kSearchAggregator);
// "Focus" omnibox with zero input to put us in Zero suggest mode.
AutocompleteInput input;
input.set_focus_type(metrics::INTERACTION_FOCUS);
// Run the provider, there should be one match corresponding to IPH for
// Enterprise search aggregator.
provider_->Start(input, false);
ACMatches matches = provider_->matches();
EXPECT_EQ(matches.size(), 1u);
EXPECT_EQ(matches[0].type, AutocompleteMatchType::NULL_RESULT_MESSAGE);
EXPECT_EQ(matches[0].iph_type, IphType::kEnterpriseSearchAggregator);
// Not in ZPS, the IPH should not be provided.
input.set_focus_type(metrics::INTERACTION_DEFAULT);
provider_->Start(input, false);
matches = provider_->matches();
EXPECT_EQ(matches.size(), 0u);
// "@" state - Confirm expected starter pack is still shown but no ZPS.
std::vector<TestData> typing_scheme_cases = {
// Typing '@' should give all the starter pack suggestions, and no IPH.
{u"@",
{kAiModeUrl, kBookmarksUrl, FeaturedUrlN(1), FeaturedUrlN(4), kGeminiUrl,
kHistoryUrl, kTabsUrl}}};
RunTest(typing_scheme_cases);
}
TEST_F(FeaturedSearchProviderTest,
ZeroSuggestEnterpriseSearchAggregatorIPHSuggestion_DeleteMatch) {
history_embeddings::ScopedFeatureParametersForTesting feature_parameters(
base::BindOnce([](history_embeddings::FeatureParameters& parameters) {
parameters.omnibox_scoped = false;
}));
base::test::ScopedFeatureList features;
features.InitWithFeaturesAndParameters(
{{history_embeddings::kHistoryEmbeddings, {}},
{omnibox::kStarterPackExpansion, {}}},
{omnibox::kStarterPackIPH});
AddStarterPackEntriesToTemplateUrlService();
AddFeaturedEnterpriseSearchEngine(
FeaturedKeywordN(4), FeaturedUrlN(4),
TemplateURLData::PolicyOrigin::kSearchAggregator);
// "Focus" omnibox with zero input to put us in Zero suggest mode.
AutocompleteInput input;
input.set_focus_type(metrics::INTERACTION_FOCUS);
// Run the provider, there should be one match corresponding to IPH for
// Enterprise search aggregator.
PrefService* prefs = client_->GetPrefs();
EXPECT_FALSE(prefs->GetBoolean(
omnibox::kDismissedFeaturedEnterpriseSiteSearchIphPrefName));
provider_->Start(input, false);
ACMatches matches = provider_->matches();
EXPECT_EQ(matches.size(), 1u);
EXPECT_EQ(matches[0].type, AutocompleteMatchType::NULL_RESULT_MESSAGE);
EXPECT_EQ(matches[0].iph_type, IphType::kEnterpriseSearchAggregator);
// Call `DeleteMatch()`, match should be deleted from `matches_` and the pref
// should be set to false.
provider_->DeleteMatch(matches[0]);
matches = provider_->matches();
EXPECT_EQ(matches.size(), 0u);
EXPECT_TRUE(prefs->GetBoolean(
omnibox::kDismissedEnterpriseSearchAggregatorIphPrefName));
// Run the provider again, IPH match should not be provided.
provider_->Start(input, false);
matches = provider_->matches();
EXPECT_EQ(matches.size(), 0u);
}
TEST_F(
FeaturedSearchProviderTest,
ZeroSuggestFeaturedSiteSearchIPHAfterEnterpriseSearchAggregatorIPHDeleted) {
history_embeddings::ScopedFeatureParametersForTesting feature_parameters(
base::BindOnce([](history_embeddings::FeatureParameters& parameters) {
parameters.omnibox_scoped = false;
}));
base::test::ScopedFeatureList features;
features.InitWithFeaturesAndParameters(
{{history_embeddings::kHistoryEmbeddings, {}},
{omnibox::kStarterPackExpansion, {}},
{omnibox::kStarterPackIPH, {}}},
{});
AddStarterPackEntriesToTemplateUrlService();
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(1), FeaturedUrlN(1),
TemplateURLData::PolicyOrigin::kSiteSearch);
AddFeaturedEnterpriseSearchEngine(
FeaturedKeywordN(4), FeaturedUrlN(4),
TemplateURLData::PolicyOrigin::kSearchAggregator);
// "Focus" omnibox with zero input to put us in Zero suggest mode.
AutocompleteInput input;
input.set_focus_type(metrics::INTERACTION_FOCUS);
// Run the provider, there should be one match corresponding to IPH for
// Enterprise search aggregator.
PrefService* prefs = client_->GetPrefs();
EXPECT_FALSE(prefs->GetBoolean(
omnibox::kDismissedEnterpriseSearchAggregatorIphPrefName));
EXPECT_FALSE(prefs->GetBoolean(
omnibox::kDismissedFeaturedEnterpriseSiteSearchIphPrefName));
provider_->Start(input, false);
ACMatches matches = provider_->matches();
EXPECT_EQ(matches.size(), 1u);
EXPECT_EQ(matches[0].type, AutocompleteMatchType::NULL_RESULT_MESSAGE);
EXPECT_EQ(matches[0].iph_type, IphType::kEnterpriseSearchAggregator);
// Call `DeleteMatch()`, match should be deleted from `matches_` and the pref
// should be set to false.
provider_->DeleteMatch(matches[0]);
matches = provider_->matches();
EXPECT_EQ(matches.size(), 0u);
EXPECT_TRUE(prefs->GetBoolean(
omnibox::kDismissedEnterpriseSearchAggregatorIphPrefName));
EXPECT_FALSE(prefs->GetBoolean(
omnibox::kDismissedFeaturedEnterpriseSiteSearchIphPrefName));
// Run the provider again, there should be one match corresponding to IPH for
// featured Enterprise site search. The match should not include the search
// aggregator keyword.
EXPECT_FALSE(prefs->GetBoolean(
omnibox::kDismissedFeaturedEnterpriseSiteSearchIphPrefName));
provider_->Start(input, false);
matches = provider_->matches();
EXPECT_EQ(matches.size(), 1u);
EXPECT_EQ(matches[0].type, AutocompleteMatchType::NULL_RESULT_MESSAGE);
EXPECT_EQ(matches[0].iph_type, IphType::kFeaturedEnterpriseSiteSearch);
EXPECT_EQ(matches[0].contents, u"Type @ to search across featured1.com");
}
TEST_F(FeaturedSearchProviderTest, HistoryEmbedding_Iphs) {
// Setup.
AddStarterPackEntriesToTemplateUrlService();
AutocompleteInput zero_input(u"", metrics::OmniboxEventProto::OTHER,
TestSchemeClassifier());
zero_input.set_focus_type(metrics::OmniboxFocusType::INTERACTION_FOCUS);
AutocompleteInput non_zero_input(u"x", metrics::OmniboxEventProto::OTHER,
TestSchemeClassifier());
AutocompleteInput scope_input(u"@history", metrics::OmniboxEventProto::OTHER,
TestSchemeClassifier());
auto mock_setting = [&](bool setting_visible, bool setting_opted_in) {
CHECK(!setting_opted_in || setting_visible);
EXPECT_CALL(*client_, IsHistoryEmbeddingsSettingVisible())
.WillRepeatedly(testing::Return(setting_visible));
EXPECT_CALL(*client_, IsHistoryEmbeddingsEnabled())
.WillRepeatedly(testing::Return(setting_opted_in));
};
// No IPH is shown when the feature is disabled.
{
history_embeddings::ScopedFeatureParametersForTesting feature_parameters(
base::BindOnce([](history_embeddings::FeatureParameters& parameters) {
parameters.omnibox_scoped = false;
}));
base::test::ScopedFeatureList disabled_features;
disabled_features.InitAndEnableFeatureWithParameters(
history_embeddings::kHistoryEmbeddings, {});
{
SCOPED_TRACE("");
RunAndVerifyIph(zero_input, {});
}
{
SCOPED_TRACE("");
RunAndVerifyIph(non_zero_input, {});
}
{
SCOPED_TRACE("");
RunAndVerifyIph(scope_input, {});
}
}
// '@history' promo is shown when embeddings is not opted-in (even if the
// feature is enabled).
{
base::test::ScopedFeatureList features;
features.InitWithFeatures({{history_embeddings::kHistoryEmbeddings}}, {});
mock_setting(false, false);
{
SCOPED_TRACE("");
RunAndVerifyIph(zero_input,
{{IphType::kHistoryScopePromo,
u"Type @history to search your browsing history"}});
}
// Not shown for non-zero input.
{
SCOPED_TRACE("");
RunAndVerifyIph(non_zero_input, {});
}
// '@history' AI promo is shown when embeddings is opted-in.
mock_setting(true, true);
{
SCOPED_TRACE("");
RunAndVerifyIph(
zero_input,
{{IphType::kHistoryEmbeddingsScopePromo,
u"Type @history to search your browsing history, powered by AI"}});
}
// Not shown for non-zero input.
{
SCOPED_TRACE("");
RunAndVerifyIph(non_zero_input, {});
}
// chrome://settings/ai/historySearch promo shown when not opted-in and in
// @history scope.
mock_setting(true, false);
{
SCOPED_TRACE("");
RunAndVerifyIph(
scope_input,
{{IphType::kHistoryEmbeddingsSettingsPromo,
// Should end with whitespace since there's a link following it.
u"For a more powerful way to search your browsing history, turn "
u"on ",
u"History search, powered by AI",
GURL("chrome://settings/ai/historySearch")}});
}
// Not shown for unscoped inputs. Zero input will show the '@history' promo
// tested above, so just test `non_zero_input` here.
{
SCOPED_TRACE("");
RunAndVerifyIph(non_zero_input, {});
}
// Not shown if the setting isn't available.
mock_setting(false, false);
{
SCOPED_TRACE("");
RunAndVerifyIph(scope_input, {});
}
// Disclaimer shown when opted-in and in @history scope.
mock_setting(true, true);
{
SCOPED_TRACE("");
RunAndVerifyIph(
scope_input,
{{IphType::kHistoryEmbeddingsDisclaimer,
// Should end with whitespace since there's a link following it.
u"Your searches, best matches, and their page contents are sent to "
u"Google and may be seen by human reviewers to improve this "
u"feature. "
u"This is an experimental feature and won't always get it right. ",
u"Learn more", GURL("chrome://settings/ai/historySearch")}});
}
// Not shown for unscoped inputs. Zero input will show the '@history' AI
// promo tested above, so just test `non_zero_input` here.
{
SCOPED_TRACE("");
RunAndVerifyIph(non_zero_input, {});
}
}
// Not shown if omnibox entry is disabled, even if embeddings is overall
// enabled.
{
history_embeddings::ScopedFeatureParametersForTesting feature_parameters(
base::BindOnce([](history_embeddings::FeatureParameters& parameters) {
parameters.omnibox_scoped = false;
}));
base::test::ScopedFeatureList features_without_omnibox;
features_without_omnibox.InitAndEnableFeatureWithParameters(
history_embeddings::kHistoryEmbeddings, {});
{
SCOPED_TRACE("");
RunAndVerifyIph(zero_input, {});
}
{
SCOPED_TRACE("");
RunAndVerifyIph(non_zero_input, {});
}
{
SCOPED_TRACE("");
RunAndVerifyIph(scope_input, {});
}
}
}
TEST_F(FeaturedSearchProviderTest, IphShownLimit) {
base::test::ScopedFeatureList features;
features.InitWithFeatures(
{{omnibox::kStarterPackIPH}, {history_embeddings::kHistoryEmbeddings}},
{});
AddStarterPackEntriesToTemplateUrlService();
AutocompleteInput input;
input.set_focus_type(metrics::INTERACTION_FOCUS);
auto test = [&](const AutocompleteInput& input,
const std::vector<IphType> expected_iph_types) {
RunAndVerifyIphTypes(input, expected_iph_types);
// Notify the provider which matches were shown.
AutocompleteResult result;
result.AppendMatches(provider_->matches());
provider_->RegisterDisplayedMatches(result);
};
// Show up to 3 IPHs per session.
{
SCOPED_TRACE("");
test(input, {IphType::kGemini});
}
{
SCOPED_TRACE("");
test(input, {IphType::kGemini});
}
{
SCOPED_TRACE("");
test(input, {IphType::kGemini});
}
{
SCOPED_TRACE("");
test(input, {});
}
// Start a new session, should see an IPH 3 more times. But not the same IPH
// as before, since it already consumed its limit.
provider_ = new FeaturedSearchProvider(client_.get());
{
SCOPED_TRACE("");
test(input, {IphType::kHistoryScopePromo});
}
{
SCOPED_TRACE("");
test(input, {IphType::kHistoryScopePromo});
}
{
SCOPED_TRACE("");
test(input, {IphType::kHistoryScopePromo});
}
{
SCOPED_TRACE("");
test(input, {});
}
}
TEST_F(FeaturedSearchProviderTest, OffTheRecord_HistoryEmbeddings) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({history_embeddings::kHistoryEmbeddings},
{omnibox::kStarterPackIPH});
AddStarterPackEntriesToTemplateUrlService();
AutocompleteInput input;
input.set_focus_type(metrics::INTERACTION_FOCUS);
// By default, the @history promo should be shown.
RunAndVerifyIphTypes(input, {IphType::kHistoryScopePromo});
// The @history scope doesn't work in Incognito or guest mode, though, so it
// doesn't make sense to promote it in these windows.
EXPECT_CALL(*client_, IsOffTheRecord()).WillRepeatedly(testing::Return(true));
RunAndVerifyIphTypes(input, {});
}
TEST_F(FeaturedSearchProviderTest, OffTheRecord_FeaturedEnterpriseSearch) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({omnibox::kStarterPackExpansion},
{omnibox::kStarterPackIPH});
AddStarterPackEntriesToTemplateUrlService();
AddFeaturedEnterpriseSearchEngine(FeaturedKeywordN(1), FeaturedUrlN(1),
TemplateURLData::PolicyOrigin::kSiteSearch);
AddFeaturedEnterpriseSearchEngine(
FeaturedKeywordN(2), FeaturedUrlN(2),
TemplateURLData::PolicyOrigin::kSearchAggregator);
AutocompleteInput input;
input.set_focus_type(metrics::INTERACTION_FOCUS);
// The enterprise search aggregator scope doesn't work in Incognito or guest
// mode. However, the match and IPH for enterprise site search engine should
// still show.
EXPECT_CALL(*client_, IsOffTheRecord()).WillRepeatedly(testing::Return(true));
RunAndVerifyIph(input, {{IphType::kFeaturedEnterpriseSiteSearch,
u"Type @ to search across featured1.com"}});
// "@" state.
std::vector<TestData> typing_scheme_cases = {
// Typing '@' should give all the starter pack suggestions (excluding
// history), featured site search engine, and no IPH.
{u"@",
{kAiModeUrl, kBookmarksUrl, FeaturedUrlN(1), kGeminiUrl, kTabsUrl}}};
RunTest(typing_scheme_cases);
}
|