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