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