shortcuts_provider_unittest.cc revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
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
3// found in the LICENSE file.
4
5#include "chrome/browser/autocomplete/shortcuts_provider.h"
6
7#include <math.h>
8
9#include <algorithm>
10#include <functional>
11#include <set>
12#include <string>
13#include <vector>
14
15#include "base/memory/ref_counted.h"
16#include "base/message_loop/message_loop.h"
17#include "base/prefs/pref_service.h"
18#include "base/strings/stringprintf.h"
19#include "base/strings/utf_string_conversions.h"
20#include "chrome/browser/autocomplete/autocomplete_input.h"
21#include "chrome/browser/autocomplete/autocomplete_match.h"
22#include "chrome/browser/autocomplete/autocomplete_provider.h"
23#include "chrome/browser/autocomplete/autocomplete_provider_listener.h"
24#include "chrome/browser/autocomplete/autocomplete_result.h"
25#include "chrome/browser/autocomplete/shortcuts_backend.h"
26#include "chrome/browser/autocomplete/shortcuts_backend_factory.h"
27#include "chrome/browser/chrome_notification_types.h"
28#include "chrome/browser/history/history_service.h"
29#include "chrome/browser/history/in_memory_url_index.h"
30#include "chrome/browser/history/url_database.h"
31#include "chrome/common/pref_names.h"
32#include "chrome/test/base/testing_profile.h"
33#include "content/public/browser/notification_service.h"
34#include "content/public/test/test_browser_thread.h"
35#include "extensions/common/extension.h"
36#include "extensions/common/extension_builder.h"
37#include "extensions/common/value_builder.h"
38#include "testing/gtest/include/gtest/gtest.h"
39
40using base::ASCIIToUTF16;
41
42// TestShortcutInfo -----------------------------------------------------------
43
44namespace {
45
46struct TestShortcutInfo {
47  std::string guid;
48  std::string text;
49  std::string fill_into_edit;
50  std::string destination_url;
51  std::string contents;
52  std::string contents_class;
53  std::string description;
54  std::string description_class;
55  content::PageTransition transition;
56  AutocompleteMatch::Type type;
57  std::string keyword;
58  int days_from_now;
59  int number_of_hits;
60} shortcut_test_db[] = {
61  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", "goog", "www.google.com",
62    "http://www.google.com/", "Google", "0,1,4,0", "Google", "0,3,4,1",
63    content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 1,
64    100 },
65  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", "slash", "slashdot.org",
66    "http://slashdot.org/", "slashdot.org", "0,3,5,1",
67    "Slashdot - News for nerds, stuff that matters", "0,2,5,0",
68    content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 0,
69    100 },
70  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E2", "news", "slashdot.org",
71    "http://slashdot.org/", "slashdot.org", "0,1",
72    "Slashdot - News for nerds, stuff that matters", "0,0,11,2,15,0",
73    content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_TITLE, "", 0,
74    5 },
75  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E3", "news", "sports.yahoo.com",
76    "http://sports.yahoo.com/", "sports.yahoo.com", "0,1",
77    "Yahoo! Sports - Sports News, Scores, Rumors, Fantasy Games, and more",
78    "0,0,23,2,27,0", content::PAGE_TRANSITION_TYPED,
79    AutocompleteMatchType::HISTORY_TITLE, "", 2, 5 },
80  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E4", "news weather",
81    "www.cnn.com/index.html", "http://www.cnn.com/index.html",
82    "www.cnn.com/index.html", "0,1",
83    "CNN.com - Breaking News, U.S., World, Weather, Entertainment & Video",
84    "0,0,19,2,23,0,38,2,45,0", content::PAGE_TRANSITION_TYPED,
85    AutocompleteMatchType::HISTORY_TITLE, "", 1, 10 },
86  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E5", "nhl scores", "sports.yahoo.com",
87    "http://sports.yahoo.com/", "sports.yahoo.com", "0,1",
88    "Yahoo! Sports - Sports News, Scores, Rumors, Fantasy Games, and more",
89    "0,0,29,2,35,0", content::PAGE_TRANSITION_TYPED,
90    AutocompleteMatchType::HISTORY_BODY, "", 1, 10 },
91  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E6", "nhl scores",
92    "www.nhl.com/scores/index.html", "http://www.nhl.com/scores/index.html",
93    "www.nhl.com/scores/index.html", "0,1,4,3,7,1",
94    "January 13, 2010 - NHL.com - Scores", "0,0,19,2,22,0,29,2,35,0",
95    content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 5,
96    1 },
97  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E7", "just", "www.testsite.com/a.html",
98    "http://www.testsite.com/a.html", "www.testsite.com/a.html", "0,1",
99    "Test - site - just a test", "0,0,14,2,18,0",
100    content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_TITLE, "", 5,
101    1 },
102  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E8", "just", "www.testsite.com/b.html",
103    "http://www.testsite.com/b.html", "www.testsite.com/b.html", "0,1",
104    "Test - site - just a test", "0,0,14,2,18,0",
105    content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_TITLE, "", 5,
106    2 },
107  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E9", "just", "www.testsite.com/c.html",
108    "http://www.testsite.com/c.html", "www.testsite.com/c.html", "0,1",
109    "Test - site - just a test", "0,0,14,2,18,0",
110    content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_TITLE, "", 8,
111    1 },
112  { "BD85DBA2-8C29-49F9-84AE-48E1E90880EA", "just a", "www.testsite.com/d.html",
113    "http://www.testsite.com/d.html", "www.testsite.com/d.html", "0,1",
114    "Test - site - just a test", "0,0,14,2,18,0",
115    content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_TITLE, "",
116    12, 1 },
117  { "BD85DBA2-8C29-49F9-84AE-48E1E90880EB", "just a t",
118    "www.testsite.com/e.html", "http://www.testsite.com/e.html",
119    "www.testsite.com/e.html", "0,1", "Test - site - just a test",
120    "0,0,14,2,18,0", content::PAGE_TRANSITION_TYPED,
121    AutocompleteMatchType::HISTORY_TITLE, "", 12, 1 },
122  { "BD85DBA2-8C29-49F9-84AE-48E1E90880EC", "just a te",
123    "www.testsite.com/f.html", "http://www.testsite.com/f.html",
124    "www.testsite.com/f.html", "0,1", "Test - site - just a test",
125    "0,0,14,2,18,0", content::PAGE_TRANSITION_TYPED,
126    AutocompleteMatchType::HISTORY_TITLE, "", 12, 1 },
127  { "BD85DBA2-8C29-49F9-84AE-48E1E90880ED", "ago", "www.daysagotest.com/a.html",
128    "http://www.daysagotest.com/a.html", "www.daysagotest.com/a.html",
129    "0,1,8,3,11,1", "Test - site", "0,0", content::PAGE_TRANSITION_TYPED,
130    AutocompleteMatchType::HISTORY_URL, "", 1, 1 },
131  { "BD85DBA2-8C29-49F9-84AE-48E1E90880EE", "ago", "www.daysagotest.com/b.html",
132    "http://www.daysagotest.com/b.html", "www.daysagotest.com/b.html",
133    "0,1,8,3,11,1", "Test - site", "0,0", content::PAGE_TRANSITION_TYPED,
134    AutocompleteMatchType::HISTORY_URL, "", 2, 1 },
135  { "BD85DBA2-8C29-49F9-84AE-48E1E90880EF", "ago", "www.daysagotest.com/c.html",
136    "http://www.daysagotest.com/c.html", "www.daysagotest.com/c.html",
137    "0,1,8,3,11,1", "Test - site", "0,0", content::PAGE_TRANSITION_TYPED,
138    AutocompleteMatchType::HISTORY_URL, "", 3, 1 },
139  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F0", "ago", "www.daysagotest.com/d.html",
140    "http://www.daysagotest.com/d.html", "www.daysagotest.com/d.html",
141    "0,1,8,3,11,1", "Test - site", "0,0", content::PAGE_TRANSITION_TYPED,
142    AutocompleteMatchType::HISTORY_URL, "", 4, 1 },
143  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F1", "echo echo", "echo echo",
144    "chrome-extension://cedabbhfglmiikkmdgcpjdkocfcmbkee/?q=echo",
145    "Run Echo command: echo", "0,0", "Echo", "0,4",
146    content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::EXTENSION_APP,
147    "echo", 1, 1 },
148  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F2", "abcdef.com", "http://abcdef.com",
149    "http://abcdef.com/", "Abcdef", "0,1,4,0", "Abcdef", "0,3,4,1",
150    content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 1,
151    100 },
152  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F3", "query", "query",
153    "https://www.google.com/search?q=query", "query", "0,0",
154    "Google Search", "0,4", content::PAGE_TRANSITION_GENERATED,
155    AutocompleteMatchType::SEARCH_HISTORY, "", 1, 100 },
156  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F4", "word", "www.word",
157    "https://www.google.com/search?q=www.word", "www.word", "0,0",
158    "Google Search", "0,4", content::PAGE_TRANSITION_GENERATED,
159    AutocompleteMatchType::SEARCH_HISTORY, "", 1, 100 },
160  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F5", "about:o", "chrome://omnibox",
161    "chrome://omnibox/", "about:omnibox", "0,3,10,1", "", "",
162    content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::NAVSUGGEST, "",
163    1, 100 },
164  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F6", "www/real sp",
165    "http://www/real space/long-url-with-space.html",
166    "http://www/real%20space/long-url-with-space.html",
167    "www/real space/long-url-with-space.html", "0,3,11,1",
168    "Page With Space; Input with Space", "0,0",
169    content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "",
170    1, 100 },
171  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F7", "duplicate", "http://duplicate.com",
172    "http://duplicate.com/", "Duplicate", "0,1", "Duplicate", "0,1",
173    content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 1,
174    100 },
175  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F8", "dupl", "http://duplicate.com",
176    "http://duplicate.com/", "Duplicate", "0,1", "Duplicate", "0,1",
177    content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 1,
178    100 },
179};
180
181}  // namespace
182
183
184// ClassifyTest ---------------------------------------------------------------
185
186// Helper class to make running tests of ClassifyAllMatchesInString() more
187// convenient.
188class ClassifyTest {
189 public:
190  ClassifyTest(const base::string16& text, ACMatchClassifications matches);
191  ~ClassifyTest();
192
193  ACMatchClassifications RunTest(const base::string16& find_text);
194
195 private:
196  const base::string16 text_;
197  const ACMatchClassifications matches_;
198};
199
200ClassifyTest::ClassifyTest(const base::string16& text,
201                           ACMatchClassifications matches)
202    : text_(text),
203      matches_(matches) {
204}
205
206ClassifyTest::~ClassifyTest() {
207}
208
209ACMatchClassifications ClassifyTest::RunTest(const base::string16& find_text) {
210  return ShortcutsProvider::ClassifyAllMatchesInString(find_text,
211      ShortcutsProvider::CreateWordMapForString(find_text), text_, matches_);
212}
213
214
215// ShortcutsProviderTest ------------------------------------------------------
216
217class ShortcutsProviderTest : public testing::Test,
218                              public AutocompleteProviderListener {
219 public:
220  ShortcutsProviderTest();
221
222  // AutocompleteProviderListener:
223  virtual void OnProviderUpdate(bool updated_matches) OVERRIDE;
224
225 protected:
226  typedef std::pair<std::string, bool> ExpectedURLAndAllowedToBeDefault;
227  typedef std::vector<ExpectedURLAndAllowedToBeDefault> ExpectedURLs;
228
229  class SetShouldContain
230      : public std::unary_function<const ExpectedURLAndAllowedToBeDefault&,
231                                   std::set<std::string> > {
232   public:
233    explicit SetShouldContain(const ACMatches& matched_urls);
234
235    void operator()(const ExpectedURLAndAllowedToBeDefault& expected);
236    std::set<ExpectedURLAndAllowedToBeDefault> Leftovers() const {
237        return matches_;
238    }
239
240   private:
241    std::set<ExpectedURLAndAllowedToBeDefault> matches_;
242  };
243
244  virtual void SetUp();
245  virtual void TearDown();
246
247  // Fills test data into the provider.
248  void FillData(TestShortcutInfo* db, size_t db_size);
249
250  // Runs an autocomplete query on |text| with the provided
251  // |prevent_inline_autocomplete| setting and checks to see that the returned
252  // results' destination URLs match those provided. |expected_urls| does not
253  // need to be in sorted order, but |expected_top_result| should be the top
254  // match, and it should have inline autocompletion
255  // |top_result_inline_autocompletion|.
256  void RunTest(const base::string16 text,
257               bool prevent_inline_autocomplete,
258               const ExpectedURLs& expected_urls,
259               std::string expected_top_result,
260               base::string16 top_result_inline_autocompletion);
261
262  // Passthrough to the private function in provider_.
263  int CalculateScore(const std::string& terms,
264                     const history::ShortcutsDatabase::Shortcut& shortcut,
265                     int max_relevance);
266
267  base::MessageLoopForUI message_loop_;
268  content::TestBrowserThread ui_thread_;
269  content::TestBrowserThread file_thread_;
270
271  TestingProfile profile_;
272
273  ACMatches ac_matches_;  // The resulting matches after running RunTest.
274
275  scoped_refptr<ShortcutsBackend> backend_;
276  scoped_refptr<ShortcutsProvider> provider_;
277};
278
279ShortcutsProviderTest::ShortcutsProviderTest()
280    : ui_thread_(content::BrowserThread::UI, &message_loop_),
281      file_thread_(content::BrowserThread::FILE, &message_loop_) {
282}
283
284void ShortcutsProviderTest::OnProviderUpdate(bool updated_matches) {}
285
286void ShortcutsProviderTest::SetUp() {
287  ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse(
288      &profile_, &ShortcutsBackendFactory::BuildProfileNoDatabaseForTesting);
289  backend_ = ShortcutsBackendFactory::GetForProfile(&profile_);
290  ASSERT_TRUE(backend_.get());
291  ASSERT_TRUE(profile_.CreateHistoryService(true, false));
292  provider_ = new ShortcutsProvider(this, &profile_);
293  FillData(shortcut_test_db, arraysize(shortcut_test_db));
294}
295
296void ShortcutsProviderTest::TearDown() {
297  // Run all pending tasks or else some threads hold on to the message loop
298  // and prevent it from being deleted.
299  message_loop_.RunUntilIdle();
300  provider_ = NULL;
301}
302
303void ShortcutsProviderTest::FillData(TestShortcutInfo* db, size_t db_size) {
304  DCHECK(provider_.get());
305  size_t expected_size = backend_->shortcuts_map().size() + db_size;
306  for (size_t i = 0; i < db_size; ++i) {
307    const TestShortcutInfo& cur = db[i];
308    history::ShortcutsDatabase::Shortcut shortcut(
309        cur.guid, ASCIIToUTF16(cur.text),
310        history::ShortcutsDatabase::Shortcut::MatchCore(
311            ASCIIToUTF16(cur.fill_into_edit), GURL(cur.destination_url),
312            ASCIIToUTF16(cur.contents), cur.contents_class,
313            ASCIIToUTF16(cur.description), cur.description_class,
314            cur.transition, cur.type, ASCIIToUTF16(cur.keyword)),
315        base::Time::Now() - base::TimeDelta::FromDays(cur.days_from_now),
316        cur.number_of_hits);
317    backend_->AddShortcut(shortcut);
318  }
319  EXPECT_EQ(expected_size, backend_->shortcuts_map().size());
320}
321
322ShortcutsProviderTest::SetShouldContain::SetShouldContain(
323    const ACMatches& matched_urls) {
324  for (ACMatches::const_iterator iter = matched_urls.begin();
325       iter != matched_urls.end(); ++iter)
326    matches_.insert(ExpectedURLAndAllowedToBeDefault(
327        iter->destination_url.spec(), iter->allowed_to_be_default_match));
328}
329
330void ShortcutsProviderTest::SetShouldContain::operator()(
331    const ExpectedURLAndAllowedToBeDefault& expected) {
332  EXPECT_EQ(1U, matches_.erase(expected));
333}
334
335void ShortcutsProviderTest::RunTest(
336    const base::string16 text,
337    bool prevent_inline_autocomplete,
338    const ExpectedURLs& expected_urls,
339    std::string expected_top_result,
340    base::string16 top_result_inline_autocompletion) {
341  base::MessageLoop::current()->RunUntilIdle();
342  AutocompleteInput input(text, base::string16::npos, base::string16(), GURL(),
343                          AutocompleteInput::INVALID_SPEC,
344                          prevent_inline_autocomplete, false, true,
345                          AutocompleteInput::ALL_MATCHES);
346  provider_->Start(input, false);
347  EXPECT_TRUE(provider_->done());
348
349  ac_matches_ = provider_->matches();
350
351  // We should have gotten back at most AutocompleteProvider::kMaxMatches.
352  EXPECT_LE(ac_matches_.size(), AutocompleteProvider::kMaxMatches);
353
354  // If the number of expected and actual matches aren't equal then we need
355  // test no further, but let's do anyway so that we know which URLs failed.
356  EXPECT_EQ(expected_urls.size(), ac_matches_.size());
357
358  // Verify that all expected URLs were found and that all found URLs
359  // were expected.
360  std::set<ExpectedURLAndAllowedToBeDefault> Leftovers =
361      for_each(expected_urls.begin(), expected_urls.end(),
362               SetShouldContain(ac_matches_)).Leftovers();
363  EXPECT_EQ(0U, Leftovers.size());
364
365  // See if we got the expected top scorer.
366  if (!ac_matches_.empty()) {
367    std::partial_sort(ac_matches_.begin(), ac_matches_.begin() + 1,
368                      ac_matches_.end(), AutocompleteMatch::MoreRelevant);
369    EXPECT_EQ(expected_top_result, ac_matches_[0].destination_url.spec());
370    EXPECT_EQ(top_result_inline_autocompletion,
371              ac_matches_[0].inline_autocompletion);
372  }
373}
374
375int ShortcutsProviderTest::CalculateScore(
376    const std::string& terms,
377    const history::ShortcutsDatabase::Shortcut& shortcut,
378    int max_relevance) {
379  return provider_->CalculateScore(ASCIIToUTF16(terms), shortcut,
380                                   max_relevance);
381}
382
383
384// Actual tests ---------------------------------------------------------------
385
386TEST_F(ShortcutsProviderTest, SimpleSingleMatch) {
387  base::string16 text(ASCIIToUTF16("go"));
388  std::string expected_url("http://www.google.com/");
389  ExpectedURLs expected_urls;
390  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true));
391  RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16("ogle.com"));
392
393  // Same test with prevent inline autocomplete.
394  expected_urls.clear();
395  expected_urls.push_back(
396      ExpectedURLAndAllowedToBeDefault(expected_url, false));
397  // The match will have an |inline_autocompletion| set, but the value will not
398  // be used because |allowed_to_be_default_match| will be false.
399  RunTest(text, true, expected_urls, expected_url, ASCIIToUTF16("ogle.com"));
400
401  // A pair of analogous tests where the shortcut ends at the end of
402  // |fill_into_edit|.  This exercises the inline autocompletion and default
403  // match code.
404  text = ASCIIToUTF16("abcdef.com");
405  expected_url = "http://abcdef.com/";
406  expected_urls.clear();
407  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true));
408  RunTest(text, false, expected_urls, expected_url, base::string16());
409  // With prevent inline autocomplete, the suggestion should be the same
410  // (because there is no completion).
411  RunTest(text, true, expected_urls, expected_url, base::string16());
412
413  // Another test, simply for a query match type, not a navigation URL match
414  // type.
415  text = ASCIIToUTF16("que");
416  expected_url = "https://www.google.com/search?q=query";
417  expected_urls.clear();
418  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true));
419  RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16("ry"));
420
421  // Same test with prevent inline autocomplete.
422  expected_urls.clear();
423  expected_urls.push_back(
424      ExpectedURLAndAllowedToBeDefault(expected_url, false));
425  // The match will have an |inline_autocompletion| set, but the value will not
426  // be used because |allowed_to_be_default_match| will be false.
427  RunTest(text, true, expected_urls, expected_url, ASCIIToUTF16("ry"));
428
429  // A pair of analogous tests where the shortcut ends at the end of
430  // |fill_into_edit|.  This exercises the inline autocompletion and default
431  // match code.
432  text = ASCIIToUTF16("query");
433  expected_urls.clear();
434  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true));
435  RunTest(text, false, expected_urls, expected_url, base::string16());
436  // With prevent inline autocomplete, the suggestion should be the same
437  // (because there is no completion).
438  RunTest(text, true, expected_urls, expected_url, base::string16());
439
440  // Now the shortcut ends at the end of |fill_into_edit| but has a
441  // non-droppable prefix.  ("www.", for instance, is not droppable for
442  // queries.)
443  text = ASCIIToUTF16("word");
444  expected_url = "https://www.google.com/search?q=www.word";
445  expected_urls.clear();
446  expected_urls.push_back(
447      ExpectedURLAndAllowedToBeDefault(expected_url, false));
448  RunTest(text, false, expected_urls, expected_url, base::string16());
449}
450
451// These tests are like those in SimpleSingleMatch but more complex,
452// involving URLs that need to be fixed up to match properly.
453TEST_F(ShortcutsProviderTest, TrickySingleMatch) {
454  // Test that about: URLs are fixed up/transformed to chrome:// URLs.
455  base::string16 text(ASCIIToUTF16("about:o"));
456  std::string expected_url("chrome://omnibox/");
457  ExpectedURLs expected_urls;
458  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true));
459  RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16("mnibox"));
460
461  // Same test with prevent inline autocomplete.
462  expected_urls.clear();
463  expected_urls.push_back(
464      ExpectedURLAndAllowedToBeDefault(expected_url, false));
465  // The match will have an |inline_autocompletion| set, but the value will not
466  // be used because |allowed_to_be_default_match| will be false.
467  RunTest(text, true, expected_urls, expected_url, ASCIIToUTF16("mnibox"));
468
469  // Test that an input with a space can match URLs with a (escaped) space.
470  // This would fail if we didn't try to lookup the un-fixed-up string.
471  text = ASCIIToUTF16("www/real sp");
472  expected_url = "http://www/real%20space/long-url-with-space.html";
473  expected_urls.clear();
474  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true));
475  RunTest(text, false, expected_urls, expected_url,
476          ASCIIToUTF16("ace/long-url-with-space.html"));
477
478  // Same test with prevent inline autocomplete.
479  expected_urls.clear();
480  expected_urls.push_back(
481      ExpectedURLAndAllowedToBeDefault(expected_url, false));
482  // The match will have an |inline_autocompletion| set, but the value will not
483  // be used because |allowed_to_be_default_match| will be false.
484  RunTest(text, true, expected_urls, expected_url,
485          ASCIIToUTF16("ace/long-url-with-space.html"));
486}
487
488TEST_F(ShortcutsProviderTest, MultiMatch) {
489  base::string16 text(ASCIIToUTF16("NEWS"));
490  ExpectedURLs expected_urls;
491  // Scores high because of completion length.
492  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
493      "http://slashdot.org/", false));
494  // Scores high because of visit count.
495  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
496      "http://sports.yahoo.com/", false));
497  // Scores high because of visit count but less match span,
498  // which is more important.
499  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
500      "http://www.cnn.com/index.html", false));
501  RunTest(text, false, expected_urls, "http://slashdot.org/", base::string16());
502}
503
504TEST_F(ShortcutsProviderTest, RemoveDuplicates) {
505  base::string16 text(ASCIIToUTF16("dupl"));
506  ExpectedURLs expected_urls;
507  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
508      "http://duplicate.com/", true));
509  // Make sure the URL only appears once in the output list.
510  RunTest(text, false, expected_urls, "http://duplicate.com/",
511          ASCIIToUTF16("icate.com"));
512}
513
514TEST_F(ShortcutsProviderTest, TypedCountMatches) {
515  base::string16 text(ASCIIToUTF16("just"));
516  ExpectedURLs expected_urls;
517  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
518      "http://www.testsite.com/b.html", false));
519  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
520      "http://www.testsite.com/a.html", false));
521  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
522      "http://www.testsite.com/c.html", false));
523  RunTest(text, false, expected_urls, "http://www.testsite.com/b.html",
524          base::string16());
525}
526
527TEST_F(ShortcutsProviderTest, FragmentLengthMatches) {
528  base::string16 text(ASCIIToUTF16("just a"));
529  ExpectedURLs expected_urls;
530  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
531      "http://www.testsite.com/d.html", false));
532  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
533      "http://www.testsite.com/e.html", false));
534  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
535      "http://www.testsite.com/f.html", false));
536  RunTest(text, false, expected_urls, "http://www.testsite.com/d.html",
537          base::string16());
538}
539
540TEST_F(ShortcutsProviderTest, DaysAgoMatches) {
541  base::string16 text(ASCIIToUTF16("ago"));
542  ExpectedURLs expected_urls;
543  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
544      "http://www.daysagotest.com/a.html", false));
545  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
546      "http://www.daysagotest.com/b.html", false));
547  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
548      "http://www.daysagotest.com/c.html", false));
549  RunTest(text, false, expected_urls, "http://www.daysagotest.com/a.html",
550          base::string16());
551}
552
553TEST_F(ShortcutsProviderTest, ClassifyAllMatchesInString) {
554  ACMatchClassifications matches =
555      AutocompleteMatch::ClassificationsFromString("0,0");
556  ClassifyTest classify_test(ASCIIToUTF16("A man, a plan, a canal Panama"),
557                             matches);
558
559  ACMatchClassifications spans_a = classify_test.RunTest(ASCIIToUTF16("man"));
560  // ACMatch spans should be: '--MMM------------------------'
561  EXPECT_EQ("0,0,2,2,5,0", AutocompleteMatch::ClassificationsToString(spans_a));
562
563  ACMatchClassifications spans_b = classify_test.RunTest(ASCIIToUTF16("man p"));
564  // ACMatch spans should be: '--MMM----M-------------M-----'
565  EXPECT_EQ("0,0,2,2,5,0,9,2,10,0,23,2,24,0",
566            AutocompleteMatch::ClassificationsToString(spans_b));
567
568  ACMatchClassifications spans_c =
569      classify_test.RunTest(ASCIIToUTF16("man plan panama"));
570  // ACMatch spans should be:'--MMM----MMMM----------MMMMMM'
571  EXPECT_EQ("0,0,2,2,5,0,9,2,13,0,23,2",
572            AutocompleteMatch::ClassificationsToString(spans_c));
573
574  ClassifyTest classify_test2(ASCIIToUTF16("Yahoo! Sports - Sports News, "
575      "Scores, Rumors, Fantasy Games, and more"), matches);
576
577  ACMatchClassifications spans_d = classify_test2.RunTest(ASCIIToUTF16("ne"));
578  // ACMatch spans should match first two letters of the "news".
579  EXPECT_EQ("0,0,23,2,25,0",
580            AutocompleteMatch::ClassificationsToString(spans_d));
581
582  ACMatchClassifications spans_e =
583      classify_test2.RunTest(ASCIIToUTF16("news r"));
584  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,"
585            "66,2,67,0", AutocompleteMatch::ClassificationsToString(spans_e));
586
587  matches = AutocompleteMatch::ClassificationsFromString("0,1");
588  ClassifyTest classify_test3(ASCIIToUTF16("livescore.goal.com"), matches);
589
590  ACMatchClassifications spans_f = classify_test3.RunTest(ASCIIToUTF16("go"));
591  // ACMatch spans should match first two letters of the "goal".
592  EXPECT_EQ("0,1,10,3,12,1",
593            AutocompleteMatch::ClassificationsToString(spans_f));
594
595  matches = AutocompleteMatch::ClassificationsFromString("0,0,13,1");
596  ClassifyTest classify_test4(ASCIIToUTF16("Email login: mail.somecorp.com"),
597                              matches);
598
599  ACMatchClassifications spans_g = classify_test4.RunTest(ASCIIToUTF16("ail"));
600  EXPECT_EQ("0,0,2,2,5,0,13,1,14,3,17,1",
601            AutocompleteMatch::ClassificationsToString(spans_g));
602
603  ACMatchClassifications spans_h =
604      classify_test4.RunTest(ASCIIToUTF16("lo log"));
605  EXPECT_EQ("0,0,6,2,9,0,13,1",
606            AutocompleteMatch::ClassificationsToString(spans_h));
607
608  ACMatchClassifications spans_i =
609      classify_test4.RunTest(ASCIIToUTF16("ail em"));
610  // 'Email' and 'ail' should be matched.
611  EXPECT_EQ("0,2,5,0,13,1,14,3,17,1",
612            AutocompleteMatch::ClassificationsToString(spans_i));
613
614  // Some web sites do not have a description.  If the string being searched is
615  // empty, the classifications must also be empty: http://crbug.com/148647
616  // Extra parens in the next line hack around C++03's "most vexing parse".
617  class ClassifyTest classify_test5((base::string16()),
618                                    ACMatchClassifications());
619  ACMatchClassifications spans_j = classify_test5.RunTest(ASCIIToUTF16("man"));
620  ASSERT_EQ(0U, spans_j.size());
621
622  // Matches which end at beginning of classification merge properly.
623  matches = AutocompleteMatch::ClassificationsFromString("0,4,9,0");
624  ClassifyTest classify_test6(ASCIIToUTF16("html password example"), matches);
625
626  // Extra space in the next string avoids having the string be a prefix of the
627  // text above, which would allow for two different valid classification sets,
628  // one of which uses two spans (the first of which would mark all of "html
629  // pass" as a match) and one which uses four (which marks the individual words
630  // as matches but not the space between them).  This way only the latter is
631  // valid.
632  ACMatchClassifications spans_k =
633      classify_test6.RunTest(ASCIIToUTF16("html  pass"));
634  EXPECT_EQ("0,6,4,4,5,6,9,0",
635            AutocompleteMatch::ClassificationsToString(spans_k));
636
637  // Multiple matches with both beginning and end at beginning of
638  // classifications merge properly.
639  matches = AutocompleteMatch::ClassificationsFromString("0,1,11,0");
640  ClassifyTest classify_test7(ASCIIToUTF16("http://a.co is great"), matches);
641
642  ACMatchClassifications spans_l =
643      classify_test7.RunTest(ASCIIToUTF16("ht co"));
644  EXPECT_EQ("0,3,2,1,9,3,11,0",
645            AutocompleteMatch::ClassificationsToString(spans_l));
646}
647
648TEST_F(ShortcutsProviderTest, CalculateScore) {
649  history::ShortcutsDatabase::Shortcut shortcut(
650      std::string(), ASCIIToUTF16("test"),
651      history::ShortcutsDatabase::Shortcut::MatchCore(
652          ASCIIToUTF16("www.test.com"), GURL("http://www.test.com"),
653          ASCIIToUTF16("www.test.com"), "0,1,4,3,8,1",
654          ASCIIToUTF16("A test"), "0,0,2,2", content::PAGE_TRANSITION_TYPED,
655          AutocompleteMatchType::HISTORY_URL, base::string16()),
656      base::Time::Now(), 1);
657
658  // Maximal score.
659  const int max_relevance = AutocompleteResult::kLowestDefaultScore - 1;
660  const int kMaxScore = CalculateScore("test", shortcut, max_relevance);
661
662  // Score decreases as percent of the match is decreased.
663  int score_three_quarters = CalculateScore("tes", shortcut, max_relevance);
664  EXPECT_LT(score_three_quarters, kMaxScore);
665  int score_one_half = CalculateScore("te", shortcut, max_relevance);
666  EXPECT_LT(score_one_half, score_three_quarters);
667  int score_one_quarter = CalculateScore("t", shortcut, max_relevance);
668  EXPECT_LT(score_one_quarter, score_one_half);
669
670  // Should decay with time - one week.
671  shortcut.last_access_time = base::Time::Now() - base::TimeDelta::FromDays(7);
672  int score_week_old = CalculateScore("test", shortcut, max_relevance);
673  EXPECT_LT(score_week_old, kMaxScore);
674
675  // Should decay more in two weeks.
676  shortcut.last_access_time = base::Time::Now() - base::TimeDelta::FromDays(14);
677  int score_two_weeks_old = CalculateScore("test", shortcut, max_relevance);
678  EXPECT_LT(score_two_weeks_old, score_week_old);
679
680  // But not if it was activly clicked on. 2 hits slow decaying power.
681  shortcut.number_of_hits = 2;
682  shortcut.last_access_time = base::Time::Now() - base::TimeDelta::FromDays(14);
683  int score_popular_two_weeks_old =
684      CalculateScore("test", shortcut, max_relevance);
685  EXPECT_LT(score_two_weeks_old, score_popular_two_weeks_old);
686  // But still decayed.
687  EXPECT_LT(score_popular_two_weeks_old, kMaxScore);
688
689  // 3 hits slow decaying power even more.
690  shortcut.number_of_hits = 3;
691  shortcut.last_access_time = base::Time::Now() - base::TimeDelta::FromDays(14);
692  int score_more_popular_two_weeks_old =
693      CalculateScore("test", shortcut, max_relevance);
694  EXPECT_LT(score_two_weeks_old, score_more_popular_two_weeks_old);
695  EXPECT_LT(score_popular_two_weeks_old, score_more_popular_two_weeks_old);
696  // But still decayed.
697  EXPECT_LT(score_more_popular_two_weeks_old, kMaxScore);
698}
699
700TEST_F(ShortcutsProviderTest, DeleteMatch) {
701  TestShortcutInfo shortcuts_to_test_delete[] = {
702    { "BD85DBA2-8C29-49F9-84AE-48E1E90881F1", "delete", "www.deletetest.com/1",
703      "http://www.deletetest.com/1", "http://www.deletetest.com/1", "0,2",
704      "Erase this shortcut!", "0,0", content::PAGE_TRANSITION_TYPED,
705      AutocompleteMatchType::HISTORY_URL, "", 1, 1},
706    { "BD85DBA2-8C29-49F9-84AE-48E1E90881F2", "erase", "www.deletetest.com/1",
707      "http://www.deletetest.com/1", "http://www.deletetest.com/1", "0,2",
708      "Erase this shortcut!", "0,0", content::PAGE_TRANSITION_TYPED,
709      AutocompleteMatchType::HISTORY_TITLE, "", 1, 1},
710    { "BD85DBA2-8C29-49F9-84AE-48E1E90881F3", "keep", "www.deletetest.com/1/2",
711      "http://www.deletetest.com/1/2", "http://www.deletetest.com/1/2", "0,2",
712      "Keep this shortcut!", "0,0", content::PAGE_TRANSITION_TYPED,
713      AutocompleteMatchType::HISTORY_TITLE, "", 1, 1},
714    { "BD85DBA2-8C29-49F9-84AE-48E1E90881F4", "delete", "www.deletetest.com/2",
715      "http://www.deletetest.com/2", "http://www.deletetest.com/2", "0,2",
716      "Erase this shortcut!", "0,0", content::PAGE_TRANSITION_TYPED,
717      AutocompleteMatchType::HISTORY_URL, "", 1, 1},
718  };
719
720  size_t original_shortcuts_count = backend_->shortcuts_map().size();
721
722  FillData(shortcuts_to_test_delete, arraysize(shortcuts_to_test_delete));
723
724  EXPECT_EQ(original_shortcuts_count + 4, backend_->shortcuts_map().size());
725  EXPECT_FALSE(backend_->shortcuts_map().end() ==
726               backend_->shortcuts_map().find(ASCIIToUTF16("delete")));
727  EXPECT_FALSE(backend_->shortcuts_map().end() ==
728               backend_->shortcuts_map().find(ASCIIToUTF16("erase")));
729
730  AutocompleteMatch match(
731      provider_.get(), 1200, true, AutocompleteMatchType::HISTORY_TITLE);
732
733  match.destination_url = GURL(shortcuts_to_test_delete[0].destination_url);
734  match.contents = ASCIIToUTF16(shortcuts_to_test_delete[0].contents);
735  match.description = ASCIIToUTF16(shortcuts_to_test_delete[0].description);
736
737  provider_->DeleteMatch(match);
738
739  // shortcuts_to_test_delete[0] and shortcuts_to_test_delete[1] should be
740  // deleted, but not shortcuts_to_test_delete[2] or
741  // shortcuts_to_test_delete[3], which have different URLs.
742  EXPECT_EQ(original_shortcuts_count + 2, backend_->shortcuts_map().size());
743  EXPECT_FALSE(backend_->shortcuts_map().end() ==
744               backend_->shortcuts_map().find(ASCIIToUTF16("delete")));
745  EXPECT_TRUE(backend_->shortcuts_map().end() ==
746              backend_->shortcuts_map().find(ASCIIToUTF16("erase")));
747
748  match.destination_url = GURL(shortcuts_to_test_delete[3].destination_url);
749  match.contents = ASCIIToUTF16(shortcuts_to_test_delete[3].contents);
750  match.description = ASCIIToUTF16(shortcuts_to_test_delete[3].description);
751
752  provider_->DeleteMatch(match);
753  EXPECT_EQ(original_shortcuts_count + 1, backend_->shortcuts_map().size());
754  EXPECT_TRUE(backend_->shortcuts_map().end() ==
755              backend_->shortcuts_map().find(ASCIIToUTF16("delete")));
756}
757
758TEST_F(ShortcutsProviderTest, Extension) {
759  // Try an input string that matches an extension URL.
760  base::string16 text(ASCIIToUTF16("echo"));
761  std::string expected_url(
762      "chrome-extension://cedabbhfglmiikkmdgcpjdkocfcmbkee/?q=echo");
763  ExpectedURLs expected_urls;
764  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
765      expected_url, true));
766  RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16(" echo"));
767
768  // Claim the extension has been unloaded.
769  scoped_refptr<const extensions::Extension> extension =
770      extensions::ExtensionBuilder()
771          .SetManifest(extensions::DictionaryBuilder()
772              .Set("name", "Echo")
773              .Set("version", "1.0"))
774          .SetID("cedabbhfglmiikkmdgcpjdkocfcmbkee")
775          .Build();
776  extensions::UnloadedExtensionInfo details(
777      extension.get(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL);
778  content::NotificationService::current()->Notify(
779      chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
780      content::Source<Profile>(&profile_),
781      content::Details<extensions::UnloadedExtensionInfo>(&details));
782
783  // Now the URL should have disappeared.
784  RunTest(text, false, ExpectedURLs(), std::string(), base::string16());
785}
786