1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/omnibox/browser/shortcuts_provider_test_util.h"
6
7 #include "base/run_loop.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "components/omnibox/browser/autocomplete_match.h"
11 #include "components/omnibox/browser/shortcuts_backend.h"
12 #include "components/omnibox/browser/shortcuts_provider.h"
13 #include "components/omnibox/browser/test_scheme_classifier.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
TestShortcutData(std::string guid,std::string text,std::string fill_into_edit,std::string destination_url,AutocompleteMatch::DocumentType document_type,std::string contents,std::string contents_class,std::string description,std::string description_class,ui::PageTransition transition,AutocompleteMatch::Type type,std::string keyword,int days_from_now,int number_of_hits)16 TestShortcutData::TestShortcutData(
17 std::string guid,
18 std::string text,
19 std::string fill_into_edit,
20 std::string destination_url,
21 AutocompleteMatch::DocumentType document_type,
22 std::string contents,
23 std::string contents_class,
24 std::string description,
25 std::string description_class,
26 ui::PageTransition transition,
27 AutocompleteMatch::Type type,
28 std::string keyword,
29 int days_from_now,
30 int number_of_hits) {
31 this->guid = guid;
32 this->text = text;
33 this->fill_into_edit = fill_into_edit;
34 this->destination_url = destination_url;
35 this->document_type = document_type;
36 this->contents = contents;
37 this->contents_class = contents_class;
38 this->description = description;
39 this->description_class = description_class;
40 this->transition = transition;
41 this->type = type;
42 this->keyword = keyword;
43 this->days_from_now = days_from_now;
44 this->number_of_hits = number_of_hits;
45 }
46
~TestShortcutData()47 TestShortcutData::~TestShortcutData() {}
48
PopulateShortcutsBackendWithTestData(scoped_refptr<ShortcutsBackend> backend,TestShortcutData * db,size_t db_size)49 void PopulateShortcutsBackendWithTestData(
50 scoped_refptr<ShortcutsBackend> backend,
51 TestShortcutData* db,
52 size_t db_size) {
53 size_t expected_size = backend->shortcuts_map().size() + db_size;
54 for (size_t i = 0; i < db_size; ++i) {
55 const TestShortcutData& cur = db[i];
56 ShortcutsDatabase::Shortcut shortcut(
57 cur.guid, base::ASCIIToUTF16(cur.text),
58 ShortcutsDatabase::Shortcut::MatchCore(
59 base::ASCIIToUTF16(cur.fill_into_edit), GURL(cur.destination_url),
60 static_cast<int>(cur.document_type),
61 base::ASCIIToUTF16(cur.contents), cur.contents_class,
62 base::ASCIIToUTF16(cur.description), cur.description_class,
63 cur.transition, cur.type, base::ASCIIToUTF16(cur.keyword)),
64 base::Time::Now() - base::TimeDelta::FromDays(cur.days_from_now),
65 cur.number_of_hits);
66 backend->AddShortcut(shortcut);
67 }
68 EXPECT_EQ(expected_size, backend->shortcuts_map().size());
69 }
70
RunShortcutsProviderTest(scoped_refptr<ShortcutsProvider> provider,const base::string16 text,bool prevent_inline_autocomplete,const std::vector<ExpectedURLAndAllowedToBeDefault> & expected_urls,std::string expected_top_result,base::string16 top_result_inline_autocompletion)71 void RunShortcutsProviderTest(
72 scoped_refptr<ShortcutsProvider> provider,
73 const base::string16 text,
74 bool prevent_inline_autocomplete,
75 const std::vector<ExpectedURLAndAllowedToBeDefault>& expected_urls,
76 std::string expected_top_result,
77 base::string16 top_result_inline_autocompletion) {
78 base::RunLoop().RunUntilIdle();
79 AutocompleteInput input(text, metrics::OmniboxEventProto::OTHER,
80 TestSchemeClassifier());
81 input.set_prevent_inline_autocomplete(prevent_inline_autocomplete);
82 provider->Start(input, false);
83 EXPECT_TRUE(provider->done());
84
85 ACMatches ac_matches = provider->matches();
86
87 std::string debug = base::StringPrintf(
88 "Input [%s], prevent inline [%d], matches:\n",
89 base::UTF16ToUTF8(text).c_str(), prevent_inline_autocomplete);
90 for (auto match : ac_matches) {
91 debug += base::StringPrintf(" URL [%s], default [%d]\n",
92 match.destination_url.spec().c_str(),
93 match.allowed_to_be_default_match);
94 }
95
96 // We should have gotten back at most
97 // AutocompleteProvider::provider_max_matches().
98 EXPECT_LE(ac_matches.size(), provider->provider_max_matches()) << debug;
99
100 // If the number of expected and actual matches aren't equal then we need
101 // test no further, but let's do anyway so that we know which URLs failed.
102 EXPECT_EQ(expected_urls.size(), ac_matches.size()) << debug;
103
104 for (const auto& expected_url : expected_urls) {
105 auto iter = std::find_if(
106 ac_matches.begin(), ac_matches.end(),
107 [&expected_url](const AutocompleteMatch& match) {
108 return expected_url.first == match.destination_url.spec() &&
109 expected_url.second == match.allowed_to_be_default_match;
110 });
111 EXPECT_TRUE(iter != ac_matches.end())
112 << debug
113 << base::StringPrintf("Expected URL [%s], default [%d]\n",
114 expected_url.first.c_str(), expected_url.second);
115 }
116
117 // See if we got the expected top scorer.
118 if (!ac_matches.empty()) {
119 std::partial_sort(ac_matches.begin(), ac_matches.begin() + 1,
120 ac_matches.end(), AutocompleteMatch::MoreRelevant);
121 EXPECT_EQ(expected_top_result, ac_matches[0].destination_url.spec())
122 << debug;
123 EXPECT_EQ(top_result_inline_autocompletion,
124 ac_matches[0].inline_autocompletion)
125 << debug;
126 }
127 }
128