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