OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/omnibox/browser/shortcuts_provider.h" | 5 #include "components/omnibox/browser/shortcuts_provider.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <functional> | 11 #include <functional> |
12 #include <set> | 12 #include <set> |
13 #include <string> | 13 #include <string> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
| 16 #include "base/files/scoped_temp_dir.h" |
16 #include "base/macros.h" | 17 #include "base/macros.h" |
17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
18 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
19 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
20 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
21 #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h" | 22 #include "base/test/sequenced_worker_pool_owner.h" |
22 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" | 23 #include "base/threading/thread.h" |
23 #include "chrome/browser/autocomplete/shortcuts_backend_factory.h" | |
24 #include "chrome/test/base/testing_profile.h" | |
25 #include "components/history/core/browser/history_service.h" | 24 #include "components/history/core/browser/history_service.h" |
26 #include "components/history/core/browser/url_database.h" | 25 #include "components/history/core/browser/url_database.h" |
| 26 #include "components/history/core/test/history_service_test_util.h" |
27 #include "components/metrics/proto/omnibox_event.pb.h" | 27 #include "components/metrics/proto/omnibox_event.pb.h" |
28 #include "components/omnibox/browser/autocomplete_input.h" | 28 #include "components/omnibox/browser/autocomplete_input.h" |
29 #include "components/omnibox/browser/autocomplete_match.h" | 29 #include "components/omnibox/browser/autocomplete_match.h" |
30 #include "components/omnibox/browser/autocomplete_provider.h" | 30 #include "components/omnibox/browser/autocomplete_provider.h" |
31 #include "components/omnibox/browser/autocomplete_result.h" | 31 #include "components/omnibox/browser/autocomplete_result.h" |
32 #include "components/omnibox/browser/in_memory_url_index.h" | 32 #include "components/omnibox/browser/in_memory_url_index.h" |
| 33 #include "components/omnibox/browser/mock_autocomplete_provider_client.h" |
33 #include "components/omnibox/browser/shortcuts_backend.h" | 34 #include "components/omnibox/browser/shortcuts_backend.h" |
34 #include "components/prefs/pref_service.h" | 35 #include "components/omnibox/browser/shortcuts_provider_test_util.h" |
35 #include "content/public/browser/notification_service.h" | 36 #include "components/omnibox/browser/test_scheme_classifier.h" |
36 #include "content/public/test/test_browser_thread.h" | |
37 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
38 | 38 |
39 #if defined(ENABLE_EXTENSIONS) | |
40 #include "extensions/browser/notification_types.h" | |
41 #include "extensions/common/extension.h" | |
42 #include "extensions/common/extension_builder.h" | |
43 #include "extensions/common/value_builder.h" | |
44 #endif | |
45 | |
46 using base::ASCIIToUTF16; | 39 using base::ASCIIToUTF16; |
47 | 40 using ExpectedURLs = std::vector<ExpectedURLAndAllowedToBeDefault>; |
48 // TestShortcutInfo ----------------------------------------------------------- | |
49 | 41 |
50 namespace { | 42 namespace { |
51 | 43 |
52 struct TestShortcutInfo { | 44 struct TestShortcutData shortcut_test_db[] = { |
53 std::string guid; | |
54 std::string text; | |
55 std::string fill_into_edit; | |
56 std::string destination_url; | |
57 std::string contents; | |
58 std::string contents_class; | |
59 std::string description; | |
60 std::string description_class; | |
61 ui::PageTransition transition; | |
62 AutocompleteMatch::Type type; | |
63 std::string keyword; | |
64 int days_from_now; | |
65 int number_of_hits; | |
66 } shortcut_test_db[] = { | |
67 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", "goog", "www.google.com", | 45 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", "goog", "www.google.com", |
68 "http://www.google.com/", "Google", "0,1,4,0", "Google", "0,3,4,1", | 46 "http://www.google.com/", "Google", "0,1,4,0", "Google", "0,3,4,1", |
69 ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 1, | 47 ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 1, |
70 100 }, | 48 100 }, |
71 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", "slash", "slashdot.org", | 49 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", "slash", "slashdot.org", |
72 "http://slashdot.org/", "slashdot.org", "0,3,5,1", | 50 "http://slashdot.org/", "slashdot.org", "0,3,5,1", |
73 "Slashdot - News for nerds, stuff that matters", "0,2,5,0", | 51 "Slashdot - News for nerds, stuff that matters", "0,2,5,0", |
74 ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 0, | 52 ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 0, |
75 100 }, | 53 100 }, |
76 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E2", "news", "slashdot.org", | 54 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E2", "news", "slashdot.org", |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED, | 117 "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED, |
140 AutocompleteMatchType::HISTORY_URL, "", 2, 1 }, | 118 AutocompleteMatchType::HISTORY_URL, "", 2, 1 }, |
141 { "BD85DBA2-8C29-49F9-84AE-48E1E90880EF", "ago", "www.daysagotest.com/c.html", | 119 { "BD85DBA2-8C29-49F9-84AE-48E1E90880EF", "ago", "www.daysagotest.com/c.html", |
142 "http://www.daysagotest.com/c.html", "www.daysagotest.com/c.html", | 120 "http://www.daysagotest.com/c.html", "www.daysagotest.com/c.html", |
143 "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED, | 121 "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED, |
144 AutocompleteMatchType::HISTORY_URL, "", 3, 1 }, | 122 AutocompleteMatchType::HISTORY_URL, "", 3, 1 }, |
145 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F0", "ago", "www.daysagotest.com/d.html", | 123 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F0", "ago", "www.daysagotest.com/d.html", |
146 "http://www.daysagotest.com/d.html", "www.daysagotest.com/d.html", | 124 "http://www.daysagotest.com/d.html", "www.daysagotest.com/d.html", |
147 "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED, | 125 "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED, |
148 AutocompleteMatchType::HISTORY_URL, "", 4, 1 }, | 126 AutocompleteMatchType::HISTORY_URL, "", 4, 1 }, |
149 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F1", "echo echo", "echo echo", | |
150 "chrome-extension://cedabbhfglmiikkmdgcpjdkocfcmbkee/?q=echo", | |
151 "Run Echo command: echo", "0,0", "Echo", "0,4", | |
152 ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::EXTENSION_APP, | |
153 "echo", 1, 1 }, | |
154 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F2", "abcdef.com", "http://abcdef.com", | 127 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F2", "abcdef.com", "http://abcdef.com", |
155 "http://abcdef.com/", "Abcdef", "0,1,4,0", "Abcdef", "0,3,4,1", | 128 "http://abcdef.com/", "Abcdef", "0,1,4,0", "Abcdef", "0,3,4,1", |
156 ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 1, | 129 ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 1, |
157 100 }, | 130 100 }, |
158 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F3", "query", "query", | 131 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F3", "query", "query", |
159 "https://www.google.com/search?q=query", "query", "0,0", | 132 "https://www.google.com/search?q=query", "query", "0,0", |
160 "Google Search", "0,4", ui::PAGE_TRANSITION_GENERATED, | 133 "Google Search", "0,4", ui::PAGE_TRANSITION_GENERATED, |
161 AutocompleteMatchType::SEARCH_HISTORY, "", 1, 100 }, | 134 AutocompleteMatchType::SEARCH_HISTORY, "", 1, 100 }, |
162 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F4", "word", "www.word", | 135 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F4", "word", "www.word", |
163 "https://www.google.com/search?q=www.word", "www.word", "0,0", | 136 "https://www.google.com/search?q=www.word", "www.word", "0,0", |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 "Trailing1 - Space in Shortcut", "0,1", | 170 "Trailing1 - Space in Shortcut", "0,1", |
198 "Trailing1 - Space in Shortcut", "0,1", ui::PAGE_TRANSITION_TYPED, | 171 "Trailing1 - Space in Shortcut", "0,1", ui::PAGE_TRANSITION_TYPED, |
199 AutocompleteMatchType::HISTORY_URL, "", 1, 100 }, | 172 AutocompleteMatchType::HISTORY_URL, "", 1, 100 }, |
200 { "BD85DBA2-8C29-49F9-84AE-48E1E90880FC", "about:trailing2 ", | 173 { "BD85DBA2-8C29-49F9-84AE-48E1E90880FC", "about:trailing2 ", |
201 "chrome://trailing2blah", "chrome://trailing2blah/", | 174 "chrome://trailing2blah", "chrome://trailing2blah/", |
202 "Trailing2 - Space in Shortcut", "0,1", | 175 "Trailing2 - Space in Shortcut", "0,1", |
203 "Trailing2 - Space in Shortcut", "0,1", ui::PAGE_TRANSITION_TYPED, | 176 "Trailing2 - Space in Shortcut", "0,1", ui::PAGE_TRANSITION_TYPED, |
204 AutocompleteMatchType::HISTORY_URL, "", 1, 100 }, | 177 AutocompleteMatchType::HISTORY_URL, "", 1, 100 }, |
205 }; | 178 }; |
206 | 179 |
| 180 class FakeAutocompleteProviderClient |
| 181 : public testing::NiceMock<MockAutocompleteProviderClient> { |
| 182 public: |
| 183 FakeAutocompleteProviderClient() |
| 184 : db_thread_("Test DB thread"), pool_owner_(3, "Background Pool") { |
| 185 set_template_url_service( |
| 186 make_scoped_ptr(new TemplateURLService(nullptr, 0))); |
| 187 if (history_dir_.CreateUniqueTempDir()) { |
| 188 history_service_ = history::CreateHistoryService(history_dir_.path(), |
| 189 std::string(), true); |
| 190 } |
| 191 |
| 192 db_thread_.Start(); |
| 193 shortcuts_backend_ = new ShortcutsBackend( |
| 194 GetTemplateURLService(), make_scoped_ptr(new SearchTermsData()), |
| 195 history_service_.get(), db_thread_.task_runner(), base::FilePath(), |
| 196 true); |
| 197 shortcuts_backend_->Init(); |
| 198 } |
| 199 |
| 200 ~FakeAutocompleteProviderClient() override { db_thread_.Stop(); } |
| 201 |
| 202 history::HistoryService* GetHistoryService() override { |
| 203 return history_service_.get(); |
| 204 } |
| 205 |
| 206 scoped_refptr<ShortcutsBackend> GetShortcutsBackend() override { |
| 207 return shortcuts_backend_; |
| 208 } |
| 209 |
| 210 scoped_refptr<ShortcutsBackend> GetShortcutsBackendIfExists() override { |
| 211 return shortcuts_backend_; |
| 212 } |
| 213 |
| 214 private: |
| 215 base::Thread db_thread_; |
| 216 base::SequencedWorkerPoolOwner pool_owner_; |
| 217 base::ScopedTempDir history_dir_; |
| 218 scoped_ptr<history::HistoryService> history_service_; |
| 219 scoped_refptr<ShortcutsBackend> shortcuts_backend_; |
| 220 |
| 221 DISALLOW_COPY_AND_ASSIGN(FakeAutocompleteProviderClient); |
| 222 }; |
| 223 |
207 } // namespace | 224 } // namespace |
208 | 225 |
209 | |
210 // ClassifyTest --------------------------------------------------------------- | 226 // ClassifyTest --------------------------------------------------------------- |
211 | 227 |
212 // Helper class to make running tests of ClassifyAllMatchesInString() more | 228 // Helper class to make running tests of ClassifyAllMatchesInString() more |
213 // convenient. | 229 // convenient. |
214 class ClassifyTest { | 230 class ClassifyTest { |
215 public: | 231 public: |
216 ClassifyTest(const base::string16& text, ACMatchClassifications matches); | 232 ClassifyTest(const base::string16& text, ACMatchClassifications matches); |
217 ~ClassifyTest(); | 233 ~ClassifyTest(); |
218 | 234 |
219 ACMatchClassifications RunTest(const base::string16& find_text); | 235 ACMatchClassifications RunTest(const base::string16& find_text); |
220 | 236 |
221 private: | 237 private: |
222 const base::string16 text_; | 238 const base::string16 text_; |
223 const ACMatchClassifications matches_; | 239 const ACMatchClassifications matches_; |
224 }; | 240 }; |
225 | 241 |
226 ClassifyTest::ClassifyTest(const base::string16& text, | 242 ClassifyTest::ClassifyTest(const base::string16& text, |
227 ACMatchClassifications matches) | 243 ACMatchClassifications matches) |
228 : text_(text), | 244 : text_(text), matches_(matches) {} |
229 matches_(matches) { | |
230 } | |
231 | 245 |
232 ClassifyTest::~ClassifyTest() { | 246 ClassifyTest::~ClassifyTest() {} |
233 } | |
234 | 247 |
235 ACMatchClassifications ClassifyTest::RunTest(const base::string16& find_text) { | 248 ACMatchClassifications ClassifyTest::RunTest(const base::string16& find_text) { |
236 return ShortcutsProvider::ClassifyAllMatchesInString(find_text, | 249 return ShortcutsProvider::ClassifyAllMatchesInString( |
237 ShortcutsProvider::CreateWordMapForString(find_text), text_, matches_); | 250 find_text, ShortcutsProvider::CreateWordMapForString(find_text), text_, |
| 251 matches_); |
238 } | 252 } |
239 | 253 |
240 | |
241 // ShortcutsProviderTest ------------------------------------------------------ | 254 // ShortcutsProviderTest ------------------------------------------------------ |
242 | 255 |
243 class ShortcutsProviderTest : public testing::Test { | 256 class ShortcutsProviderTest : public testing::Test { |
244 public: | 257 public: |
245 ShortcutsProviderTest(); | 258 ShortcutsProviderTest(); |
246 | 259 |
247 protected: | 260 protected: |
248 typedef std::pair<std::string, bool> ExpectedURLAndAllowedToBeDefault; | |
249 typedef std::vector<ExpectedURLAndAllowedToBeDefault> ExpectedURLs; | |
250 | |
251 class SetShouldContain | |
252 : public std::unary_function<const ExpectedURLAndAllowedToBeDefault&, | |
253 std::set<std::string> > { | |
254 public: | |
255 explicit SetShouldContain(const ACMatches& matched_urls); | |
256 | |
257 void operator()(const ExpectedURLAndAllowedToBeDefault& expected); | |
258 std::set<ExpectedURLAndAllowedToBeDefault> Leftovers() const { | |
259 return matches_; | |
260 } | |
261 | |
262 private: | |
263 std::set<ExpectedURLAndAllowedToBeDefault> matches_; | |
264 }; | |
265 | |
266 void SetUp() override; | 261 void SetUp() override; |
267 void TearDown() override; | 262 void TearDown() override; |
268 | 263 |
269 // Fills test data into the provider. | |
270 void FillData(TestShortcutInfo* db, size_t db_size); | |
271 | |
272 // Runs an autocomplete query on |text| with the provided | |
273 // |prevent_inline_autocomplete| setting and checks to see that the returned | |
274 // results' destination URLs match those provided. |expected_urls| does not | |
275 // need to be in sorted order, but |expected_top_result| should be the top | |
276 // match, and it should have inline autocompletion | |
277 // |top_result_inline_autocompletion|. | |
278 void RunTest(const base::string16 text, | |
279 bool prevent_inline_autocomplete, | |
280 const ExpectedURLs& expected_urls, | |
281 std::string expected_top_result, | |
282 base::string16 top_result_inline_autocompletion); | |
283 | |
284 // Passthrough to the private function in provider_. | 264 // Passthrough to the private function in provider_. |
285 int CalculateScore(const std::string& terms, | 265 int CalculateScore(const std::string& terms, |
286 const ShortcutsDatabase::Shortcut& shortcut, | 266 const ShortcutsDatabase::Shortcut& shortcut, |
287 int max_relevance); | 267 int max_relevance); |
288 | 268 |
289 base::MessageLoopForUI message_loop_; | 269 base::MessageLoop message_loop_; |
290 content::TestBrowserThread ui_thread_; | 270 scoped_ptr<FakeAutocompleteProviderClient> client_; |
291 content::TestBrowserThread file_thread_; | |
292 | |
293 TestingProfile profile_; | |
294 ChromeAutocompleteProviderClient client_; | |
295 | |
296 ACMatches ac_matches_; // The resulting matches after running RunTest. | |
297 | |
298 scoped_refptr<ShortcutsBackend> backend_; | |
299 scoped_refptr<ShortcutsProvider> provider_; | 271 scoped_refptr<ShortcutsProvider> provider_; |
300 }; | 272 }; |
301 | 273 |
302 ShortcutsProviderTest::ShortcutsProviderTest() | 274 ShortcutsProviderTest::ShortcutsProviderTest() {} |
303 : ui_thread_(content::BrowserThread::UI, &message_loop_), | |
304 file_thread_(content::BrowserThread::FILE, &message_loop_), | |
305 client_(&profile_) { | |
306 } | |
307 | 275 |
308 void ShortcutsProviderTest::SetUp() { | 276 void ShortcutsProviderTest::SetUp() { |
309 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse( | 277 client_.reset(new FakeAutocompleteProviderClient()); |
310 &profile_, &ShortcutsBackendFactory::BuildProfileNoDatabaseForTesting); | 278 |
311 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_); | 279 ASSERT_TRUE(client_->GetShortcutsBackend()); |
312 ASSERT_TRUE(backend_.get()); | 280 provider_ = new ShortcutsProvider(client_.get()); |
313 ASSERT_TRUE(profile_.CreateHistoryService(true, false)); | 281 PopulateShortcutsBackendWithTestData(client_->GetShortcutsBackend(), |
314 provider_ = new ShortcutsProvider(&client_); | 282 shortcut_test_db, |
315 FillData(shortcut_test_db, arraysize(shortcut_test_db)); | 283 arraysize(shortcut_test_db)); |
316 } | 284 } |
317 | 285 |
318 void ShortcutsProviderTest::TearDown() { | 286 void ShortcutsProviderTest::TearDown() { |
319 // Run all pending tasks or else some threads hold on to the message loop | 287 // Run all pending tasks or else some threads hold on to the message loop |
320 // and prevent it from being deleted. | 288 // and prevent it from being deleted. |
321 message_loop_.RunUntilIdle(); | 289 message_loop_.RunUntilIdle(); |
322 profile_.DestroyHistoryService(); | |
323 provider_ = NULL; | 290 provider_ = NULL; |
324 } | 291 } |
325 | 292 |
326 void ShortcutsProviderTest::FillData(TestShortcutInfo* db, size_t db_size) { | |
327 DCHECK(provider_.get()); | |
328 size_t expected_size = backend_->shortcuts_map().size() + db_size; | |
329 for (size_t i = 0; i < db_size; ++i) { | |
330 const TestShortcutInfo& cur = db[i]; | |
331 ShortcutsDatabase::Shortcut shortcut( | |
332 cur.guid, ASCIIToUTF16(cur.text), | |
333 ShortcutsDatabase::Shortcut::MatchCore( | |
334 ASCIIToUTF16(cur.fill_into_edit), GURL(cur.destination_url), | |
335 ASCIIToUTF16(cur.contents), cur.contents_class, | |
336 ASCIIToUTF16(cur.description), cur.description_class, | |
337 cur.transition, cur.type, ASCIIToUTF16(cur.keyword)), | |
338 base::Time::Now() - base::TimeDelta::FromDays(cur.days_from_now), | |
339 cur.number_of_hits); | |
340 backend_->AddShortcut(shortcut); | |
341 } | |
342 EXPECT_EQ(expected_size, backend_->shortcuts_map().size()); | |
343 } | |
344 | |
345 ShortcutsProviderTest::SetShouldContain::SetShouldContain( | |
346 const ACMatches& matched_urls) { | |
347 for (ACMatches::const_iterator iter = matched_urls.begin(); | |
348 iter != matched_urls.end(); ++iter) | |
349 matches_.insert(ExpectedURLAndAllowedToBeDefault( | |
350 iter->destination_url.spec(), iter->allowed_to_be_default_match)); | |
351 } | |
352 | |
353 void ShortcutsProviderTest::SetShouldContain::operator()( | |
354 const ExpectedURLAndAllowedToBeDefault& expected) { | |
355 EXPECT_EQ(1U, matches_.erase(expected)); | |
356 } | |
357 | |
358 void ShortcutsProviderTest::RunTest( | |
359 const base::string16 text, | |
360 bool prevent_inline_autocomplete, | |
361 const ExpectedURLs& expected_urls, | |
362 std::string expected_top_result, | |
363 base::string16 top_result_inline_autocompletion) { | |
364 base::MessageLoop::current()->RunUntilIdle(); | |
365 AutocompleteInput input(text, base::string16::npos, std::string(), GURL(), | |
366 metrics::OmniboxEventProto::INVALID_SPEC, | |
367 prevent_inline_autocomplete, false, true, true, false, | |
368 ChromeAutocompleteSchemeClassifier(&profile_)); | |
369 provider_->Start(input, false); | |
370 EXPECT_TRUE(provider_->done()); | |
371 | |
372 ac_matches_ = provider_->matches(); | |
373 | |
374 // We should have gotten back at most AutocompleteProvider::kMaxMatches. | |
375 EXPECT_LE(ac_matches_.size(), AutocompleteProvider::kMaxMatches); | |
376 | |
377 // If the number of expected and actual matches aren't equal then we need | |
378 // test no further, but let's do anyway so that we know which URLs failed. | |
379 EXPECT_EQ(expected_urls.size(), ac_matches_.size()); | |
380 | |
381 // Verify that all expected URLs were found and that all found URLs | |
382 // were expected. | |
383 std::set<ExpectedURLAndAllowedToBeDefault> Leftovers = | |
384 for_each(expected_urls.begin(), expected_urls.end(), | |
385 SetShouldContain(ac_matches_)).Leftovers(); | |
386 EXPECT_EQ(0U, Leftovers.size()); | |
387 | |
388 // See if we got the expected top scorer. | |
389 if (!ac_matches_.empty()) { | |
390 std::partial_sort(ac_matches_.begin(), ac_matches_.begin() + 1, | |
391 ac_matches_.end(), AutocompleteMatch::MoreRelevant); | |
392 EXPECT_EQ(expected_top_result, ac_matches_[0].destination_url.spec()); | |
393 EXPECT_EQ(top_result_inline_autocompletion, | |
394 ac_matches_[0].inline_autocompletion); | |
395 } | |
396 } | |
397 | |
398 int ShortcutsProviderTest::CalculateScore( | 293 int ShortcutsProviderTest::CalculateScore( |
399 const std::string& terms, | 294 const std::string& terms, |
400 const ShortcutsDatabase::Shortcut& shortcut, | 295 const ShortcutsDatabase::Shortcut& shortcut, |
401 int max_relevance) { | 296 int max_relevance) { |
402 return provider_->CalculateScore(ASCIIToUTF16(terms), shortcut, | 297 return provider_->CalculateScore(ASCIIToUTF16(terms), shortcut, |
403 max_relevance); | 298 max_relevance); |
404 } | 299 } |
405 | 300 |
406 | |
407 // Actual tests --------------------------------------------------------------- | 301 // Actual tests --------------------------------------------------------------- |
408 | 302 |
409 TEST_F(ShortcutsProviderTest, SimpleSingleMatch) { | 303 TEST_F(ShortcutsProviderTest, SimpleSingleMatch) { |
410 base::string16 text(ASCIIToUTF16("go")); | 304 base::string16 text(ASCIIToUTF16("go")); |
411 std::string expected_url("http://www.google.com/"); | 305 std::string expected_url("http://www.google.com/"); |
412 ExpectedURLs expected_urls; | 306 ExpectedURLs expected_urls; |
413 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); | 307 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); |
414 RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16("ogle.com")); | 308 RunShortcutsProviderTest(provider_, text, false, expected_urls, expected_url, |
| 309 ASCIIToUTF16("ogle.com")); |
415 | 310 |
416 // Same test with prevent inline autocomplete. | 311 // Same test with prevent inline autocomplete. |
417 expected_urls.clear(); | 312 expected_urls.clear(); |
418 expected_urls.push_back( | 313 expected_urls.push_back( |
419 ExpectedURLAndAllowedToBeDefault(expected_url, false)); | 314 ExpectedURLAndAllowedToBeDefault(expected_url, false)); |
420 // The match will have an |inline_autocompletion| set, but the value will not | 315 // The match will have an |inline_autocompletion| set, but the value will not |
421 // be used because |allowed_to_be_default_match| will be false. | 316 // be used because |allowed_to_be_default_match| will be false. |
422 RunTest(text, true, expected_urls, expected_url, ASCIIToUTF16("ogle.com")); | 317 RunShortcutsProviderTest(provider_, text, true, expected_urls, expected_url, |
| 318 ASCIIToUTF16("ogle.com")); |
423 | 319 |
424 // A pair of analogous tests where the shortcut ends at the end of | 320 // A pair of analogous tests where the shortcut ends at the end of |
425 // |fill_into_edit|. This exercises the inline autocompletion and default | 321 // |fill_into_edit|. This exercises the inline autocompletion and default |
426 // match code. | 322 // match code. |
427 text = ASCIIToUTF16("abcdef.com"); | 323 text = ASCIIToUTF16("abcdef.com"); |
428 expected_url = "http://abcdef.com/"; | 324 expected_url = "http://abcdef.com/"; |
429 expected_urls.clear(); | 325 expected_urls.clear(); |
430 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); | 326 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); |
431 RunTest(text, false, expected_urls, expected_url, base::string16()); | 327 RunShortcutsProviderTest(provider_, text, false, expected_urls, expected_url, |
| 328 base::string16()); |
432 // With prevent inline autocomplete, the suggestion should be the same | 329 // With prevent inline autocomplete, the suggestion should be the same |
433 // (because there is no completion). | 330 // (because there is no completion). |
434 RunTest(text, true, expected_urls, expected_url, base::string16()); | 331 RunShortcutsProviderTest(provider_, text, true, expected_urls, expected_url, |
| 332 base::string16()); |
435 | 333 |
436 // Another test, simply for a query match type, not a navigation URL match | 334 // Another test, simply for a query match type, not a navigation URL match |
437 // type. | 335 // type. |
438 text = ASCIIToUTF16("que"); | 336 text = ASCIIToUTF16("que"); |
439 expected_url = "https://www.google.com/search?q=query"; | 337 expected_url = "https://www.google.com/search?q=query"; |
440 expected_urls.clear(); | 338 expected_urls.clear(); |
441 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); | 339 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); |
442 RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16("ry")); | 340 RunShortcutsProviderTest(provider_, text, false, expected_urls, expected_url, |
| 341 ASCIIToUTF16("ry")); |
443 | 342 |
444 // Same test with prevent inline autocomplete. | 343 // Same test with prevent inline autocomplete. |
445 expected_urls.clear(); | 344 expected_urls.clear(); |
446 expected_urls.push_back( | 345 expected_urls.push_back( |
447 ExpectedURLAndAllowedToBeDefault(expected_url, false)); | 346 ExpectedURLAndAllowedToBeDefault(expected_url, false)); |
448 // The match will have an |inline_autocompletion| set, but the value will not | 347 // The match will have an |inline_autocompletion| set, but the value will not |
449 // be used because |allowed_to_be_default_match| will be false. | 348 // be used because |allowed_to_be_default_match| will be false. |
450 RunTest(text, true, expected_urls, expected_url, ASCIIToUTF16("ry")); | 349 RunShortcutsProviderTest(provider_, text, true, expected_urls, expected_url, |
| 350 ASCIIToUTF16("ry")); |
451 | 351 |
452 // A pair of analogous tests where the shortcut ends at the end of | 352 // A pair of analogous tests where the shortcut ends at the end of |
453 // |fill_into_edit|. This exercises the inline autocompletion and default | 353 // |fill_into_edit|. This exercises the inline autocompletion and default |
454 // match code. | 354 // match code. |
455 text = ASCIIToUTF16("query"); | 355 text = ASCIIToUTF16("query"); |
456 expected_urls.clear(); | 356 expected_urls.clear(); |
457 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); | 357 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); |
458 RunTest(text, false, expected_urls, expected_url, base::string16()); | 358 RunShortcutsProviderTest(provider_, text, false, expected_urls, expected_url, |
| 359 base::string16()); |
459 // With prevent inline autocomplete, the suggestion should be the same | 360 // With prevent inline autocomplete, the suggestion should be the same |
460 // (because there is no completion). | 361 // (because there is no completion). |
461 RunTest(text, true, expected_urls, expected_url, base::string16()); | 362 RunShortcutsProviderTest(provider_, text, true, expected_urls, expected_url, |
| 363 base::string16()); |
462 | 364 |
463 // Now the shortcut ends at the end of |fill_into_edit| but has a | 365 // Now the shortcut ends at the end of |fill_into_edit| but has a |
464 // non-droppable prefix. ("www.", for instance, is not droppable for | 366 // non-droppable prefix. ("www.", for instance, is not droppable for |
465 // queries.) | 367 // queries.) |
466 text = ASCIIToUTF16("word"); | 368 text = ASCIIToUTF16("word"); |
467 expected_url = "https://www.google.com/search?q=www.word"; | 369 expected_url = "https://www.google.com/search?q=www.word"; |
468 expected_urls.clear(); | 370 expected_urls.clear(); |
469 expected_urls.push_back( | 371 expected_urls.push_back( |
470 ExpectedURLAndAllowedToBeDefault(expected_url, false)); | 372 ExpectedURLAndAllowedToBeDefault(expected_url, false)); |
471 RunTest(text, false, expected_urls, expected_url, base::string16()); | 373 RunShortcutsProviderTest(provider_, text, false, expected_urls, expected_url, |
| 374 base::string16()); |
472 } | 375 } |
473 | 376 |
474 // These tests are like those in SimpleSingleMatch but more complex, | 377 // These tests are like those in SimpleSingleMatch but more complex, |
475 // involving URLs that need to be fixed up to match properly. | 378 // involving URLs that need to be fixed up to match properly. |
476 TEST_F(ShortcutsProviderTest, TrickySingleMatch) { | 379 TEST_F(ShortcutsProviderTest, TrickySingleMatch) { |
477 // Test that about: URLs are fixed up/transformed to chrome:// URLs. | 380 // Test that about: URLs are fixed up/transformed to chrome:// URLs. |
478 base::string16 text(ASCIIToUTF16("about:o")); | 381 base::string16 text(ASCIIToUTF16("about:o")); |
479 std::string expected_url("chrome://omnibox/"); | 382 std::string expected_url("chrome://omnibox/"); |
480 ExpectedURLs expected_urls; | 383 ExpectedURLs expected_urls; |
481 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); | 384 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); |
482 RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16("mnibox")); | 385 RunShortcutsProviderTest(provider_, text, false, expected_urls, expected_url, |
| 386 ASCIIToUTF16("mnibox")); |
483 | 387 |
484 // Same test with prevent inline autocomplete. | 388 // Same test with prevent inline autocomplete. |
485 expected_urls.clear(); | 389 expected_urls.clear(); |
486 expected_urls.push_back( | 390 expected_urls.push_back( |
487 ExpectedURLAndAllowedToBeDefault(expected_url, false)); | 391 ExpectedURLAndAllowedToBeDefault(expected_url, false)); |
488 // The match will have an |inline_autocompletion| set, but the value will not | 392 // The match will have an |inline_autocompletion| set, but the value will not |
489 // be used because |allowed_to_be_default_match| will be false. | 393 // be used because |allowed_to_be_default_match| will be false. |
490 RunTest(text, true, expected_urls, expected_url, ASCIIToUTF16("mnibox")); | 394 RunShortcutsProviderTest(provider_, text, true, expected_urls, expected_url, |
| 395 ASCIIToUTF16("mnibox")); |
491 | 396 |
492 // Test that an input with a space can match URLs with a (escaped) space. | 397 // Test that an input with a space can match URLs with a (escaped) space. |
493 // This would fail if we didn't try to lookup the un-fixed-up string. | 398 // This would fail if we didn't try to lookup the un-fixed-up string. |
494 text = ASCIIToUTF16("www/real sp"); | 399 text = ASCIIToUTF16("www/real sp"); |
495 expected_url = "http://www/real%20space/long-url-with-space.html"; | 400 expected_url = "http://www/real%20space/long-url-with-space.html"; |
496 expected_urls.clear(); | 401 expected_urls.clear(); |
497 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); | 402 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); |
498 RunTest(text, false, expected_urls, expected_url, | 403 RunShortcutsProviderTest(provider_, text, false, expected_urls, expected_url, |
499 ASCIIToUTF16("ace/long-url-with-space.html")); | 404 ASCIIToUTF16("ace/long-url-with-space.html")); |
500 | 405 |
501 // Same test with prevent inline autocomplete. | 406 // Same test with prevent inline autocomplete. |
502 expected_urls.clear(); | 407 expected_urls.clear(); |
503 expected_urls.push_back( | 408 expected_urls.push_back( |
504 ExpectedURLAndAllowedToBeDefault(expected_url, false)); | 409 ExpectedURLAndAllowedToBeDefault(expected_url, false)); |
505 // The match will have an |inline_autocompletion| set, but the value will not | 410 // The match will have an |inline_autocompletion| set, but the value will not |
506 // be used because |allowed_to_be_default_match| will be false. | 411 // be used because |allowed_to_be_default_match| will be false. |
507 RunTest(text, true, expected_urls, expected_url, | 412 RunShortcutsProviderTest(provider_, text, true, expected_urls, expected_url, |
508 ASCIIToUTF16("ace/long-url-with-space.html")); | 413 ASCIIToUTF16("ace/long-url-with-space.html")); |
509 | 414 |
510 // Test when the user input has a trailing slash but fill_into_edit does | 415 // Test when the user input has a trailing slash but fill_into_edit does |
511 // not. This should still be allowed to be default. | 416 // not. This should still be allowed to be default. |
512 text = ASCIIToUTF16("notrailing.com/"); | 417 text = ASCIIToUTF16("notrailing.com/"); |
513 expected_url = "http://notrailing.com/"; | 418 expected_url = "http://notrailing.com/"; |
514 expected_urls.clear(); | 419 expected_urls.clear(); |
515 expected_urls.push_back( | 420 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); |
516 ExpectedURLAndAllowedToBeDefault(expected_url, true)); | 421 RunShortcutsProviderTest(provider_, text, true, expected_urls, expected_url, |
517 RunTest(text, true, expected_urls, expected_url, base::string16()); | 422 base::string16()); |
518 | 423 |
519 // Test when the user input has a typo that can be fixed up for matching | 424 // Test when the user input has a typo that can be fixed up for matching |
520 // fill_into_edit. This should still be allowed to be default. | 425 // fill_into_edit. This should still be allowed to be default. |
521 text = ASCIIToUTF16("http:///foo.com"); | 426 text = ASCIIToUTF16("http:///foo.com"); |
522 expected_url = "http://foo.com/"; | 427 expected_url = "http://foo.com/"; |
523 expected_urls.clear(); | 428 expected_urls.clear(); |
524 expected_urls.push_back( | 429 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); |
525 ExpectedURLAndAllowedToBeDefault(expected_url, true)); | 430 RunShortcutsProviderTest(provider_, text, true, expected_urls, expected_url, |
526 RunTest(text, true, expected_urls, expected_url, base::string16()); | 431 base::string16()); |
527 | 432 |
528 // A foursome of tests to verify that trailing spaces prevent the shortcut | 433 // A foursome of tests to verify that trailing spaces prevent the shortcut |
529 // from being allowed to be the default match. For each of two tests, we | 434 // from being allowed to be the default match. For each of two tests, we |
530 // first verify that the match is allowed to be default without the trailing | 435 // first verify that the match is allowed to be default without the trailing |
531 // space but is not allowed to be default with the trailing space. In both | 436 // space but is not allowed to be default with the trailing space. In both |
532 // of these with-trailing-space cases, we actually get an | 437 // of these with-trailing-space cases, we actually get an |
533 // inline_autocompletion, though it's never used because the match is | 438 // inline_autocompletion, though it's never used because the match is |
534 // prohibited from being default. | 439 // prohibited from being default. |
535 text = ASCIIToUTF16("trailing1"); | 440 text = ASCIIToUTF16("trailing1"); |
536 expected_url = "http://trailing1.com/"; | 441 expected_url = "http://trailing1.com/"; |
537 expected_urls.clear(); | 442 expected_urls.clear(); |
538 expected_urls.push_back( | 443 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); |
539 ExpectedURLAndAllowedToBeDefault(expected_url, true)); | 444 RunShortcutsProviderTest(provider_, text, false, expected_urls, expected_url, |
540 RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16(".com")); | 445 ASCIIToUTF16(".com")); |
541 text = ASCIIToUTF16("trailing1 "); | 446 text = ASCIIToUTF16("trailing1 "); |
542 expected_urls.clear(); | 447 expected_urls.clear(); |
543 expected_urls.push_back( | 448 expected_urls.push_back( |
544 ExpectedURLAndAllowedToBeDefault(expected_url, false)); | 449 ExpectedURLAndAllowedToBeDefault(expected_url, false)); |
545 RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16(".com")); | 450 RunShortcutsProviderTest(provider_, text, false, expected_urls, expected_url, |
| 451 ASCIIToUTF16(".com")); |
546 text = ASCIIToUTF16("about:trailing2"); | 452 text = ASCIIToUTF16("about:trailing2"); |
547 expected_url = "chrome://trailing2blah/"; | 453 expected_url = "chrome://trailing2blah/"; |
548 expected_urls.clear(); | 454 expected_urls.clear(); |
549 expected_urls.push_back( | 455 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); |
550 ExpectedURLAndAllowedToBeDefault(expected_url, true)); | 456 RunShortcutsProviderTest(provider_, text, false, expected_urls, expected_url, |
551 RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16("blah")); | 457 ASCIIToUTF16("blah")); |
552 text = ASCIIToUTF16("about:trailing2 "); | 458 text = ASCIIToUTF16("about:trailing2 "); |
553 expected_urls.clear(); | 459 expected_urls.clear(); |
554 expected_urls.push_back( | 460 expected_urls.push_back( |
555 ExpectedURLAndAllowedToBeDefault(expected_url, false)); | 461 ExpectedURLAndAllowedToBeDefault(expected_url, false)); |
556 RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16("blah")); | 462 RunShortcutsProviderTest(provider_, text, false, expected_urls, expected_url, |
| 463 ASCIIToUTF16("blah")); |
557 } | 464 } |
558 | 465 |
559 TEST_F(ShortcutsProviderTest, MultiMatch) { | 466 TEST_F(ShortcutsProviderTest, MultiMatch) { |
560 base::string16 text(ASCIIToUTF16("NEWS")); | 467 base::string16 text(ASCIIToUTF16("NEWS")); |
561 ExpectedURLs expected_urls; | 468 ExpectedURLs expected_urls; |
562 // Scores high because of completion length. | 469 // Scores high because of completion length. |
563 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( | 470 expected_urls.push_back( |
564 "http://slashdot.org/", false)); | 471 ExpectedURLAndAllowedToBeDefault("http://slashdot.org/", false)); |
565 // Scores high because of visit count. | 472 // Scores high because of visit count. |
566 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( | 473 expected_urls.push_back( |
567 "http://sports.yahoo.com/", false)); | 474 ExpectedURLAndAllowedToBeDefault("http://sports.yahoo.com/", false)); |
568 // Scores high because of visit count but less match span, | 475 // Scores high because of visit count but less match span, |
569 // which is more important. | 476 // which is more important. |
570 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( | 477 expected_urls.push_back( |
571 "http://www.cnn.com/index.html", false)); | 478 ExpectedURLAndAllowedToBeDefault("http://www.cnn.com/index.html", false)); |
572 RunTest(text, false, expected_urls, "http://slashdot.org/", base::string16()); | 479 RunShortcutsProviderTest(provider_, text, false, expected_urls, |
| 480 "http://slashdot.org/", base::string16()); |
573 } | 481 } |
574 | 482 |
575 TEST_F(ShortcutsProviderTest, RemoveDuplicates) { | 483 TEST_F(ShortcutsProviderTest, RemoveDuplicates) { |
576 base::string16 text(ASCIIToUTF16("dupl")); | 484 base::string16 text(ASCIIToUTF16("dupl")); |
577 ExpectedURLs expected_urls; | 485 ExpectedURLs expected_urls; |
578 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( | 486 expected_urls.push_back( |
579 "http://duplicate.com/", true)); | 487 ExpectedURLAndAllowedToBeDefault("http://duplicate.com/", true)); |
580 // Make sure the URL only appears once in the output list. | 488 // Make sure the URL only appears once in the output list. |
581 RunTest(text, false, expected_urls, "http://duplicate.com/", | 489 RunShortcutsProviderTest(provider_, text, false, expected_urls, |
582 ASCIIToUTF16("icate.com")); | 490 "http://duplicate.com/", ASCIIToUTF16("icate.com")); |
583 } | 491 } |
584 | 492 |
585 TEST_F(ShortcutsProviderTest, TypedCountMatches) { | 493 TEST_F(ShortcutsProviderTest, TypedCountMatches) { |
586 base::string16 text(ASCIIToUTF16("just")); | 494 base::string16 text(ASCIIToUTF16("just")); |
587 ExpectedURLs expected_urls; | 495 ExpectedURLs expected_urls; |
588 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( | 496 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( |
589 "http://www.testsite.com/b.html", false)); | 497 "http://www.testsite.com/b.html", false)); |
590 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( | 498 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( |
591 "http://www.testsite.com/a.html", false)); | 499 "http://www.testsite.com/a.html", false)); |
592 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( | 500 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( |
593 "http://www.testsite.com/c.html", false)); | 501 "http://www.testsite.com/c.html", false)); |
594 RunTest(text, false, expected_urls, "http://www.testsite.com/b.html", | 502 RunShortcutsProviderTest(provider_, text, false, expected_urls, |
595 base::string16()); | 503 "http://www.testsite.com/b.html", base::string16()); |
596 } | 504 } |
597 | 505 |
598 TEST_F(ShortcutsProviderTest, FragmentLengthMatches) { | 506 TEST_F(ShortcutsProviderTest, FragmentLengthMatches) { |
599 base::string16 text(ASCIIToUTF16("just a")); | 507 base::string16 text(ASCIIToUTF16("just a")); |
600 ExpectedURLs expected_urls; | 508 ExpectedURLs expected_urls; |
601 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( | 509 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( |
602 "http://www.testsite.com/d.html", false)); | 510 "http://www.testsite.com/d.html", false)); |
603 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( | 511 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( |
604 "http://www.testsite.com/e.html", false)); | 512 "http://www.testsite.com/e.html", false)); |
605 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( | 513 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( |
606 "http://www.testsite.com/f.html", false)); | 514 "http://www.testsite.com/f.html", false)); |
607 RunTest(text, false, expected_urls, "http://www.testsite.com/d.html", | 515 RunShortcutsProviderTest(provider_, text, false, expected_urls, |
608 base::string16()); | 516 "http://www.testsite.com/d.html", base::string16()); |
609 } | 517 } |
610 | 518 |
611 TEST_F(ShortcutsProviderTest, DaysAgoMatches) { | 519 TEST_F(ShortcutsProviderTest, DaysAgoMatches) { |
612 base::string16 text(ASCIIToUTF16("ago")); | 520 base::string16 text(ASCIIToUTF16("ago")); |
613 ExpectedURLs expected_urls; | 521 ExpectedURLs expected_urls; |
614 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( | 522 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( |
615 "http://www.daysagotest.com/a.html", false)); | 523 "http://www.daysagotest.com/a.html", false)); |
616 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( | 524 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( |
617 "http://www.daysagotest.com/b.html", false)); | 525 "http://www.daysagotest.com/b.html", false)); |
618 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( | 526 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( |
619 "http://www.daysagotest.com/c.html", false)); | 527 "http://www.daysagotest.com/c.html", false)); |
620 RunTest(text, false, expected_urls, "http://www.daysagotest.com/a.html", | 528 RunShortcutsProviderTest(provider_, text, false, expected_urls, |
621 base::string16()); | 529 "http://www.daysagotest.com/a.html", |
| 530 base::string16()); |
622 } | 531 } |
623 | 532 |
624 TEST_F(ShortcutsProviderTest, ClassifyAllMatchesInString) { | 533 TEST_F(ShortcutsProviderTest, ClassifyAllMatchesInString) { |
625 ACMatchClassifications matches = | 534 ACMatchClassifications matches = |
626 AutocompleteMatch::ClassificationsFromString("0,0"); | 535 AutocompleteMatch::ClassificationsFromString("0,0"); |
627 ClassifyTest classify_test(ASCIIToUTF16("A man, a plan, a canal Panama"), | 536 ClassifyTest classify_test(ASCIIToUTF16("A man, a plan, a canal Panama"), |
628 matches); | 537 matches); |
629 | 538 |
630 ACMatchClassifications spans_a = classify_test.RunTest(ASCIIToUTF16("man")); | 539 ACMatchClassifications spans_a = classify_test.RunTest(ASCIIToUTF16("man")); |
631 // ACMatch spans should be: '--MMM------------------------' | 540 // ACMatch spans should be: '--MMM------------------------' |
632 EXPECT_EQ("0,0,2,2,5,0", AutocompleteMatch::ClassificationsToString(spans_a)); | 541 EXPECT_EQ("0,0,2,2,5,0", AutocompleteMatch::ClassificationsToString(spans_a)); |
633 | 542 |
634 ACMatchClassifications spans_b = classify_test.RunTest(ASCIIToUTF16("man p")); | 543 ACMatchClassifications spans_b = classify_test.RunTest(ASCIIToUTF16("man p")); |
635 // ACMatch spans should be: '--MMM----M-------------M-----' | 544 // ACMatch spans should be: '--MMM----M-------------M-----' |
636 EXPECT_EQ("0,0,2,2,5,0,9,2,10,0,23,2,24,0", | 545 EXPECT_EQ("0,0,2,2,5,0,9,2,10,0,23,2,24,0", |
637 AutocompleteMatch::ClassificationsToString(spans_b)); | 546 AutocompleteMatch::ClassificationsToString(spans_b)); |
638 | 547 |
639 ACMatchClassifications spans_c = | 548 ACMatchClassifications spans_c = |
640 classify_test.RunTest(ASCIIToUTF16("man plan panama")); | 549 classify_test.RunTest(ASCIIToUTF16("man plan panama")); |
641 // ACMatch spans should be:'--MMM----MMMM----------MMMMMM' | 550 // ACMatch spans should be:'--MMM----MMMM----------MMMMMM' |
642 EXPECT_EQ("0,0,2,2,5,0,9,2,13,0,23,2", | 551 EXPECT_EQ("0,0,2,2,5,0,9,2,13,0,23,2", |
643 AutocompleteMatch::ClassificationsToString(spans_c)); | 552 AutocompleteMatch::ClassificationsToString(spans_c)); |
644 | 553 |
645 ClassifyTest classify_test2(ASCIIToUTF16("Yahoo! Sports - Sports News, " | 554 ClassifyTest classify_test2( |
646 "Scores, Rumors, Fantasy Games, and more"), matches); | 555 ASCIIToUTF16("Yahoo! Sports - Sports News, " |
| 556 "Scores, Rumors, Fantasy Games, and more"), |
| 557 matches); |
647 | 558 |
648 ACMatchClassifications spans_d = classify_test2.RunTest(ASCIIToUTF16("ne")); | 559 ACMatchClassifications spans_d = classify_test2.RunTest(ASCIIToUTF16("ne")); |
649 // ACMatch spans should match first two letters of the "news". | 560 // ACMatch spans should match first two letters of the "news". |
650 EXPECT_EQ("0,0,23,2,25,0", | 561 EXPECT_EQ("0,0,23,2,25,0", |
651 AutocompleteMatch::ClassificationsToString(spans_d)); | 562 AutocompleteMatch::ClassificationsToString(spans_d)); |
652 | 563 |
653 ACMatchClassifications spans_e = | 564 ACMatchClassifications spans_e = |
654 classify_test2.RunTest(ASCIIToUTF16("news r")); | 565 classify_test2.RunTest(ASCIIToUTF16("news r")); |
655 EXPECT_EQ("0,0,10,2,11,0,19,2,20,0,23,2,27,0,32,2,33,0,37,2,38,0,41,2,42,0," | 566 EXPECT_EQ( |
656 "66,2,67,0", AutocompleteMatch::ClassificationsToString(spans_e)); | 567 "0,0,10,2,11,0,19,2,20,0,23,2,27,0,32,2,33,0,37,2,38,0,41,2,42,0," |
| 568 "66,2,67,0", |
| 569 AutocompleteMatch::ClassificationsToString(spans_e)); |
657 | 570 |
658 matches = AutocompleteMatch::ClassificationsFromString("0,1"); | 571 matches = AutocompleteMatch::ClassificationsFromString("0,1"); |
659 ClassifyTest classify_test3(ASCIIToUTF16("livescore.goal.com"), matches); | 572 ClassifyTest classify_test3(ASCIIToUTF16("livescore.goal.com"), matches); |
660 | 573 |
661 ACMatchClassifications spans_f = classify_test3.RunTest(ASCIIToUTF16("go")); | 574 ACMatchClassifications spans_f = classify_test3.RunTest(ASCIIToUTF16("go")); |
662 // ACMatch spans should match first two letters of the "goal". | 575 // ACMatch spans should match first two letters of the "goal". |
663 EXPECT_EQ("0,1,10,3,12,1", | 576 EXPECT_EQ("0,1,10,3,12,1", |
664 AutocompleteMatch::ClassificationsToString(spans_f)); | 577 AutocompleteMatch::ClassificationsToString(spans_f)); |
665 | 578 |
666 matches = AutocompleteMatch::ClassificationsFromString("0,0,13,1"); | 579 matches = AutocompleteMatch::ClassificationsFromString("0,0,13,1"); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
714 classify_test7.RunTest(ASCIIToUTF16("ht co")); | 627 classify_test7.RunTest(ASCIIToUTF16("ht co")); |
715 EXPECT_EQ("0,3,2,1,9,3,11,0", | 628 EXPECT_EQ("0,3,2,1,9,3,11,0", |
716 AutocompleteMatch::ClassificationsToString(spans_l)); | 629 AutocompleteMatch::ClassificationsToString(spans_l)); |
717 } | 630 } |
718 | 631 |
719 TEST_F(ShortcutsProviderTest, CalculateScore) { | 632 TEST_F(ShortcutsProviderTest, CalculateScore) { |
720 ShortcutsDatabase::Shortcut shortcut( | 633 ShortcutsDatabase::Shortcut shortcut( |
721 std::string(), ASCIIToUTF16("test"), | 634 std::string(), ASCIIToUTF16("test"), |
722 ShortcutsDatabase::Shortcut::MatchCore( | 635 ShortcutsDatabase::Shortcut::MatchCore( |
723 ASCIIToUTF16("www.test.com"), GURL("http://www.test.com"), | 636 ASCIIToUTF16("www.test.com"), GURL("http://www.test.com"), |
724 ASCIIToUTF16("www.test.com"), "0,1,4,3,8,1", | 637 ASCIIToUTF16("www.test.com"), "0,1,4,3,8,1", ASCIIToUTF16("A test"), |
725 ASCIIToUTF16("A test"), "0,0,2,2", ui::PAGE_TRANSITION_TYPED, | 638 "0,0,2,2", ui::PAGE_TRANSITION_TYPED, |
726 AutocompleteMatchType::HISTORY_URL, base::string16()), | 639 AutocompleteMatchType::HISTORY_URL, base::string16()), |
727 base::Time::Now(), 1); | 640 base::Time::Now(), 1); |
728 | 641 |
729 // Maximal score. | 642 // Maximal score. |
730 const int max_relevance = | 643 const int max_relevance = |
731 ShortcutsProvider::kShortcutsProviderDefaultMaxRelevance; | 644 ShortcutsProvider::kShortcutsProviderDefaultMaxRelevance; |
732 const int kMaxScore = CalculateScore("test", shortcut, max_relevance); | 645 const int kMaxScore = CalculateScore("test", shortcut, max_relevance); |
733 | 646 |
734 // Score decreases as percent of the match is decreased. | 647 // Score decreases as percent of the match is decreased. |
735 int score_three_quarters = CalculateScore("tes", shortcut, max_relevance); | 648 int score_three_quarters = CalculateScore("tes", shortcut, max_relevance); |
(...skipping 27 matching lines...) Expand all Loading... |
763 shortcut.last_access_time = base::Time::Now() - base::TimeDelta::FromDays(14); | 676 shortcut.last_access_time = base::Time::Now() - base::TimeDelta::FromDays(14); |
764 int score_more_popular_two_weeks_old = | 677 int score_more_popular_two_weeks_old = |
765 CalculateScore("test", shortcut, max_relevance); | 678 CalculateScore("test", shortcut, max_relevance); |
766 EXPECT_LT(score_two_weeks_old, score_more_popular_two_weeks_old); | 679 EXPECT_LT(score_two_weeks_old, score_more_popular_two_weeks_old); |
767 EXPECT_LT(score_popular_two_weeks_old, score_more_popular_two_weeks_old); | 680 EXPECT_LT(score_popular_two_weeks_old, score_more_popular_two_weeks_old); |
768 // But still decayed. | 681 // But still decayed. |
769 EXPECT_LT(score_more_popular_two_weeks_old, kMaxScore); | 682 EXPECT_LT(score_more_popular_two_weeks_old, kMaxScore); |
770 } | 683 } |
771 | 684 |
772 TEST_F(ShortcutsProviderTest, DeleteMatch) { | 685 TEST_F(ShortcutsProviderTest, DeleteMatch) { |
773 TestShortcutInfo shortcuts_to_test_delete[] = { | 686 TestShortcutData shortcuts_to_test_delete[] = { |
774 { "BD85DBA2-8C29-49F9-84AE-48E1E90881F1", "delete", "www.deletetest.com/1", | 687 {"BD85DBA2-8C29-49F9-84AE-48E1E90881F1", "delete", "www.deletetest.com/1", |
775 "http://www.deletetest.com/1", "http://www.deletetest.com/1", "0,2", | 688 "http://www.deletetest.com/1", "http://www.deletetest.com/1", "0,2", |
776 "Erase this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED, | 689 "Erase this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED, |
777 AutocompleteMatchType::HISTORY_URL, "", 1, 1}, | 690 AutocompleteMatchType::HISTORY_URL, "", 1, 1}, |
778 { "BD85DBA2-8C29-49F9-84AE-48E1E90881F2", "erase", "www.deletetest.com/1", | 691 {"BD85DBA2-8C29-49F9-84AE-48E1E90881F2", "erase", "www.deletetest.com/1", |
779 "http://www.deletetest.com/1", "http://www.deletetest.com/1", "0,2", | 692 "http://www.deletetest.com/1", "http://www.deletetest.com/1", "0,2", |
780 "Erase this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED, | 693 "Erase this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED, |
781 AutocompleteMatchType::HISTORY_TITLE, "", 1, 1}, | 694 AutocompleteMatchType::HISTORY_TITLE, "", 1, 1}, |
782 { "BD85DBA2-8C29-49F9-84AE-48E1E90881F3", "keep", "www.deletetest.com/1/2", | 695 {"BD85DBA2-8C29-49F9-84AE-48E1E90881F3", "keep", "www.deletetest.com/1/2", |
783 "http://www.deletetest.com/1/2", "http://www.deletetest.com/1/2", "0,2", | 696 "http://www.deletetest.com/1/2", "http://www.deletetest.com/1/2", "0,2", |
784 "Keep this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED, | 697 "Keep this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED, |
785 AutocompleteMatchType::HISTORY_TITLE, "", 1, 1}, | 698 AutocompleteMatchType::HISTORY_TITLE, "", 1, 1}, |
786 { "BD85DBA2-8C29-49F9-84AE-48E1E90881F4", "delete", "www.deletetest.com/2", | 699 {"BD85DBA2-8C29-49F9-84AE-48E1E90881F4", "delete", "www.deletetest.com/2", |
787 "http://www.deletetest.com/2", "http://www.deletetest.com/2", "0,2", | 700 "http://www.deletetest.com/2", "http://www.deletetest.com/2", "0,2", |
788 "Erase this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED, | 701 "Erase this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED, |
789 AutocompleteMatchType::HISTORY_URL, "", 1, 1}, | 702 AutocompleteMatchType::HISTORY_URL, "", 1, 1}, |
790 }; | 703 }; |
791 | 704 |
792 size_t original_shortcuts_count = backend_->shortcuts_map().size(); | 705 scoped_refptr<ShortcutsBackend> backend = client_->GetShortcutsBackend(); |
| 706 size_t original_shortcuts_count = backend->shortcuts_map().size(); |
793 | 707 |
794 FillData(shortcuts_to_test_delete, arraysize(shortcuts_to_test_delete)); | 708 PopulateShortcutsBackendWithTestData(backend, shortcuts_to_test_delete, |
| 709 arraysize(shortcuts_to_test_delete)); |
795 | 710 |
796 EXPECT_EQ(original_shortcuts_count + 4, backend_->shortcuts_map().size()); | 711 EXPECT_EQ(original_shortcuts_count + 4, backend->shortcuts_map().size()); |
797 EXPECT_FALSE(backend_->shortcuts_map().end() == | 712 EXPECT_FALSE(backend->shortcuts_map().end() == |
798 backend_->shortcuts_map().find(ASCIIToUTF16("delete"))); | 713 backend->shortcuts_map().find(ASCIIToUTF16("delete"))); |
799 EXPECT_FALSE(backend_->shortcuts_map().end() == | 714 EXPECT_FALSE(backend->shortcuts_map().end() == |
800 backend_->shortcuts_map().find(ASCIIToUTF16("erase"))); | 715 backend->shortcuts_map().find(ASCIIToUTF16("erase"))); |
801 | 716 |
802 AutocompleteMatch match( | 717 AutocompleteMatch match(provider_.get(), 1200, true, |
803 provider_.get(), 1200, true, AutocompleteMatchType::HISTORY_TITLE); | 718 AutocompleteMatchType::HISTORY_TITLE); |
804 | 719 |
805 match.destination_url = GURL(shortcuts_to_test_delete[0].destination_url); | 720 match.destination_url = GURL(shortcuts_to_test_delete[0].destination_url); |
806 match.contents = ASCIIToUTF16(shortcuts_to_test_delete[0].contents); | 721 match.contents = ASCIIToUTF16(shortcuts_to_test_delete[0].contents); |
807 match.description = ASCIIToUTF16(shortcuts_to_test_delete[0].description); | 722 match.description = ASCIIToUTF16(shortcuts_to_test_delete[0].description); |
808 | 723 |
809 provider_->DeleteMatch(match); | 724 provider_->DeleteMatch(match); |
810 | 725 |
811 // shortcuts_to_test_delete[0] and shortcuts_to_test_delete[1] should be | 726 // shortcuts_to_test_delete[0] and shortcuts_to_test_delete[1] should be |
812 // deleted, but not shortcuts_to_test_delete[2] or | 727 // deleted, but not shortcuts_to_test_delete[2] or |
813 // shortcuts_to_test_delete[3], which have different URLs. | 728 // shortcuts_to_test_delete[3], which have different URLs. |
814 EXPECT_EQ(original_shortcuts_count + 2, backend_->shortcuts_map().size()); | 729 EXPECT_EQ(original_shortcuts_count + 2, backend->shortcuts_map().size()); |
815 EXPECT_FALSE(backend_->shortcuts_map().end() == | 730 EXPECT_FALSE(backend->shortcuts_map().end() == |
816 backend_->shortcuts_map().find(ASCIIToUTF16("delete"))); | 731 backend->shortcuts_map().find(ASCIIToUTF16("delete"))); |
817 EXPECT_TRUE(backend_->shortcuts_map().end() == | 732 EXPECT_TRUE(backend->shortcuts_map().end() == |
818 backend_->shortcuts_map().find(ASCIIToUTF16("erase"))); | 733 backend->shortcuts_map().find(ASCIIToUTF16("erase"))); |
819 | 734 |
820 match.destination_url = GURL(shortcuts_to_test_delete[3].destination_url); | 735 match.destination_url = GURL(shortcuts_to_test_delete[3].destination_url); |
821 match.contents = ASCIIToUTF16(shortcuts_to_test_delete[3].contents); | 736 match.contents = ASCIIToUTF16(shortcuts_to_test_delete[3].contents); |
822 match.description = ASCIIToUTF16(shortcuts_to_test_delete[3].description); | 737 match.description = ASCIIToUTF16(shortcuts_to_test_delete[3].description); |
823 | 738 |
824 provider_->DeleteMatch(match); | 739 provider_->DeleteMatch(match); |
825 EXPECT_EQ(original_shortcuts_count + 1, backend_->shortcuts_map().size()); | 740 EXPECT_EQ(original_shortcuts_count + 1, backend->shortcuts_map().size()); |
826 EXPECT_TRUE(backend_->shortcuts_map().end() == | 741 EXPECT_TRUE(backend->shortcuts_map().end() == |
827 backend_->shortcuts_map().find(ASCIIToUTF16("delete"))); | 742 backend->shortcuts_map().find(ASCIIToUTF16("delete"))); |
828 } | 743 } |
829 | 744 |
830 TEST_F(ShortcutsProviderTest, DoesNotProvideOnFocus) { | 745 TEST_F(ShortcutsProviderTest, DoesNotProvideOnFocus) { |
831 AutocompleteInput input( | 746 AutocompleteInput input(ASCIIToUTF16("about:o"), base::string16::npos, |
832 ASCIIToUTF16("about:o"), base::string16::npos, std::string(), GURL(), | 747 std::string(), GURL(), |
833 metrics::OmniboxEventProto::INVALID_SPEC, false, false, true, true, true, | 748 metrics::OmniboxEventProto::INVALID_SPEC, false, |
834 ChromeAutocompleteSchemeClassifier(&profile_)); | 749 false, true, true, true, TestSchemeClassifier()); |
835 provider_->Start(input, false); | 750 provider_->Start(input, false); |
836 EXPECT_TRUE(provider_->matches().empty()); | 751 EXPECT_TRUE(provider_->matches().empty()); |
837 } | 752 } |
838 | |
839 #if defined(ENABLE_EXTENSIONS) | |
840 TEST_F(ShortcutsProviderTest, Extension) { | |
841 // Try an input string that matches an extension URL. | |
842 base::string16 text(ASCIIToUTF16("echo")); | |
843 std::string expected_url( | |
844 "chrome-extension://cedabbhfglmiikkmdgcpjdkocfcmbkee/?q=echo"); | |
845 ExpectedURLs expected_urls; | |
846 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( | |
847 expected_url, true)); | |
848 RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16(" echo")); | |
849 | |
850 // Claim the extension has been unloaded. | |
851 scoped_refptr<const extensions::Extension> extension = | |
852 extensions::ExtensionBuilder() | |
853 .SetManifest(std::move(extensions::DictionaryBuilder() | |
854 .Set("name", "Echo") | |
855 .Set("version", "1.0"))) | |
856 .SetID("cedabbhfglmiikkmdgcpjdkocfcmbkee") | |
857 .Build(); | |
858 extensions::UnloadedExtensionInfo details( | |
859 extension.get(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL); | |
860 content::NotificationService::current()->Notify( | |
861 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
862 content::Source<Profile>(&profile_), | |
863 content::Details<extensions::UnloadedExtensionInfo>(&details)); | |
864 | |
865 // Now the URL should have disappeared. | |
866 RunTest(text, false, ExpectedURLs(), std::string(), base::string16()); | |
867 } | |
868 #endif | |
OLD | NEW |