15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/autocomplete/shortcuts_provider.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <math.h>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <functional>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <set>
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
169ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h"
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
2123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "chrome/browser/autocomplete/shortcuts_backend.h"
2223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "chrome/browser/autocomplete/shortcuts_backend_factory.h"
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/history/history_service.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/in_memory_url_index.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/testing_profile.h"
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/history/core/browser/url_database.h"
286d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "components/metrics/proto/omnibox_event.pb.h"
295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/omnibox/autocomplete_input.h"
305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/omnibox/autocomplete_match.h"
315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/omnibox/autocomplete_provider.h"
326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "components/omnibox/autocomplete_result.h"
334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "content/public/browser/notification_service.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/test_browser_thread.h"
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "testing/gtest/include/gtest/gtest.h"
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(ENABLE_EXTENSIONS)
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "extensions/browser/notification_types.h"
39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/extension.h"
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/extension_builder.h"
41f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/value_builder.h"
421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using base::ASCIIToUTF16;
454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// TestShortcutInfo -----------------------------------------------------------
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct TestShortcutInfo {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string guid;
524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::string text;
534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::string fill_into_edit;
544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::string destination_url;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string contents;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string contents_class;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string description;
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string description_class;
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ui::PageTransition transition;
604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  AutocompleteMatch::Type type;
614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::string keyword;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int days_from_now;
634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int number_of_hits;
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} shortcut_test_db[] = {
654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", "goog", "www.google.com",
664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "http://www.google.com/", "Google", "0,1,4,0", "Google", "0,3,4,1",
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 1,
684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    100 },
694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", "slash", "slashdot.org",
704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "http://slashdot.org/", "slashdot.org", "0,3,5,1",
714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "Slashdot - News for nerds, stuff that matters", "0,2,5,0",
721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 0,
734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    100 },
744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E2", "news", "slashdot.org",
754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "http://slashdot.org/", "slashdot.org", "0,1",
764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "Slashdot - News for nerds, stuff that matters", "0,0,11,2,15,0",
771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_TITLE, "", 0,
784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    5 },
794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E3", "news", "sports.yahoo.com",
804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "http://sports.yahoo.com/", "sports.yahoo.com", "0,1",
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Yahoo! Sports - Sports News, Scores, Rumors, Fantasy Games, and more",
821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    "0,0,23,2,27,0", ui::PAGE_TRANSITION_TYPED,
834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    AutocompleteMatchType::HISTORY_TITLE, "", 2, 5 },
844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E4", "news weather",
854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "www.cnn.com/index.html", "http://www.cnn.com/index.html",
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "www.cnn.com/index.html", "0,1",
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "CNN.com - Breaking News, U.S., World, Weather, Entertainment & Video",
881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    "0,0,19,2,23,0,38,2,45,0", ui::PAGE_TRANSITION_TYPED,
894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    AutocompleteMatchType::HISTORY_TITLE, "", 1, 10 },
904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E5", "nhl scores", "sports.yahoo.com",
914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "http://sports.yahoo.com/", "sports.yahoo.com", "0,1",
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Yahoo! Sports - Sports News, Scores, Rumors, Fantasy Games, and more",
931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    "0,0,29,2,35,0", ui::PAGE_TRANSITION_TYPED,
944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    AutocompleteMatchType::HISTORY_BODY, "", 1, 10 },
954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E6", "nhl scores",
964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "www.nhl.com/scores/index.html", "http://www.nhl.com/scores/index.html",
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "www.nhl.com/scores/index.html", "0,1,4,3,7,1",
984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "January 13, 2010 - NHL.com - Scores", "0,0,19,2,22,0,29,2,35,0",
991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 5,
1004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    1 },
1014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E7", "just", "www.testsite.com/a.html",
1024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "http://www.testsite.com/a.html", "www.testsite.com/a.html", "0,1",
1034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "Test - site - just a test", "0,0,14,2,18,0",
1041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_TITLE, "", 5,
1054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    1 },
1064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E8", "just", "www.testsite.com/b.html",
1074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "http://www.testsite.com/b.html", "www.testsite.com/b.html", "0,1",
1084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "Test - site - just a test", "0,0,14,2,18,0",
1091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_TITLE, "", 5,
1104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    2 },
1114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880E9", "just", "www.testsite.com/c.html",
1124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "http://www.testsite.com/c.html", "www.testsite.com/c.html", "0,1",
1134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "Test - site - just a test", "0,0,14,2,18,0",
1141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_TITLE, "", 8,
1154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    1 },
1164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880EA", "just a", "www.testsite.com/d.html",
1174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "http://www.testsite.com/d.html", "www.testsite.com/d.html", "0,1",
1184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "Test - site - just a test", "0,0,14,2,18,0",
1191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_TITLE, "",
1204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    12, 1 },
1214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880EB", "just a t",
1224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "www.testsite.com/e.html", "http://www.testsite.com/e.html",
1234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "www.testsite.com/e.html", "0,1", "Test - site - just a test",
1241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    "0,0,14,2,18,0", ui::PAGE_TRANSITION_TYPED,
1254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    AutocompleteMatchType::HISTORY_TITLE, "", 12, 1 },
1264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880EC", "just a te",
1274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "www.testsite.com/f.html", "http://www.testsite.com/f.html",
1284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "www.testsite.com/f.html", "0,1", "Test - site - just a test",
1291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    "0,0,14,2,18,0", ui::PAGE_TRANSITION_TYPED,
1304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    AutocompleteMatchType::HISTORY_TITLE, "", 12, 1 },
1314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880ED", "ago", "www.daysagotest.com/a.html",
1324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "http://www.daysagotest.com/a.html", "www.daysagotest.com/a.html",
1331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED,
1344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    AutocompleteMatchType::HISTORY_URL, "", 1, 1 },
1354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880EE", "ago", "www.daysagotest.com/b.html",
1364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "http://www.daysagotest.com/b.html", "www.daysagotest.com/b.html",
1371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED,
1384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    AutocompleteMatchType::HISTORY_URL, "", 2, 1 },
1394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880EF", "ago", "www.daysagotest.com/c.html",
1404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "http://www.daysagotest.com/c.html", "www.daysagotest.com/c.html",
1411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED,
1424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    AutocompleteMatchType::HISTORY_URL, "", 3, 1 },
1434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F0", "ago", "www.daysagotest.com/d.html",
1444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "http://www.daysagotest.com/d.html", "www.daysagotest.com/d.html",
1451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED,
1464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    AutocompleteMatchType::HISTORY_URL, "", 4, 1 },
1474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F1", "echo echo", "echo echo",
1484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "chrome-extension://cedabbhfglmiikkmdgcpjdkocfcmbkee/?q=echo",
1494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "Run Echo command: echo", "0,0", "Echo", "0,4",
1501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::EXTENSION_APP,
1514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    "echo", 1, 1 },
152a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F2", "abcdef.com", "http://abcdef.com",
153a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    "http://abcdef.com/", "Abcdef", "0,1,4,0", "Abcdef", "0,3,4,1",
1541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 1,
155a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    100 },
156a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F3", "query", "query",
157a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    "https://www.google.com/search?q=query", "query", "0,0",
1581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    "Google Search", "0,4", ui::PAGE_TRANSITION_GENERATED,
159a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    AutocompleteMatchType::SEARCH_HISTORY, "", 1, 100 },
160a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F4", "word", "www.word",
161a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    "https://www.google.com/search?q=www.word", "www.word", "0,0",
1621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    "Google Search", "0,4", ui::PAGE_TRANSITION_GENERATED,
163a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    AutocompleteMatchType::SEARCH_HISTORY, "", 1, 100 },
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F5", "about:o", "chrome://omnibox",
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "chrome://omnibox/", "about:omnibox", "0,3,10,1", "", "",
1661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::NAVSUGGEST, "",
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    1, 100 },
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F6", "www/real sp",
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "http://www/real space/long-url-with-space.html",
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "http://www/real%20space/long-url-with-space.html",
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "www/real space/long-url-with-space.html", "0,3,11,1",
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "Page With Space; Input with Space", "0,0",
1731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "",
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    1, 100 },
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F7", "duplicate", "http://duplicate.com",
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "http://duplicate.com/", "Duplicate", "0,1", "Duplicate", "0,1",
1771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 1,
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    100 },
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F8", "dupl", "http://duplicate.com",
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "http://duplicate.com/", "Duplicate", "0,1", "Duplicate", "0,1",
1811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 1,
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    100 },
183c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  { "BD85DBA2-8C29-49F9-84AE-48E1E90880F9", "notrailing.com/",
184c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    "http://notrailing.com", "http://notrailing.com/", "No Trailing Slash",
185c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    "0,1", "No Trailing Slash on fill_into_edit", "0,1",
1861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "",
187c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    1, 100 },
188c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  { "BD85DBA2-8C29-49F9-84AE-48E1E90880FA", "http:///foo.com",
189c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    "http://foo.com", "http://foo.com/", "Foo - Typo in Input",
190c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    "0,1", "Foo - Typo in Input Corrected in fill_into_edit", "0,1",
1911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "",
192c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    1, 100 },
193a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  { "BD85DBA2-8C29-49F9-84AE-48E1E90880FB", "trailing1 ",
194a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    "http://trailing1.com", "http://trailing1.com/",
195a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    "Trailing1 - Space in Shortcut", "0,1",
1961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    "Trailing1 - Space in Shortcut", "0,1", ui::PAGE_TRANSITION_TYPED,
197a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    AutocompleteMatchType::HISTORY_URL, "", 1, 100 },
198a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  { "BD85DBA2-8C29-49F9-84AE-48E1E90880FC", "about:trailing2 ",
199a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    "chrome://trailing2blah", "chrome://trailing2blah/",
200a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    "Trailing2 - Space in Shortcut", "0,1",
2011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    "Trailing2 - Space in Shortcut", "0,1", ui::PAGE_TRANSITION_TYPED,
202a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    AutocompleteMatchType::HISTORY_URL, "", 1, 100 },
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// ClassifyTest ---------------------------------------------------------------
2094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Helper class to make running tests of ClassifyAllMatchesInString() more
2114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// convenient.
2124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class ClassifyTest {
2134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) public:
214a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ClassifyTest(const base::string16& text, ACMatchClassifications matches);
2154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ~ClassifyTest();
2164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
217a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ACMatchClassifications RunTest(const base::string16& find_text);
2184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) private:
220a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const base::string16 text_;
2214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const ACMatchClassifications matches_;
2224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)};
2234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
224a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)ClassifyTest::ClassifyTest(const base::string16& text,
225a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                           ACMatchClassifications matches)
2264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    : text_(text),
2274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      matches_(matches) {
2284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
2294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)ClassifyTest::~ClassifyTest() {
2314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
2324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
233a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)ACMatchClassifications ClassifyTest::RunTest(const base::string16& find_text) {
2344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return ShortcutsProvider::ClassifyAllMatchesInString(find_text,
2354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      ShortcutsProvider::CreateWordMapForString(find_text), text_, matches_);
2364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
2374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// ShortcutsProviderTest ------------------------------------------------------
2404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
241116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass ShortcutsProviderTest : public testing::Test {
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShortcutsProviderTest();
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
246a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  typedef std::pair<std::string, bool> ExpectedURLAndAllowedToBeDefault;
247a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  typedef std::vector<ExpectedURLAndAllowedToBeDefault> ExpectedURLs;
248a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class SetShouldContain
250a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      : public std::unary_function<const ExpectedURLAndAllowedToBeDefault&,
251a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                   std::set<std::string> > {
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    explicit SetShouldContain(const ACMatches& matched_urls);
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    void operator()(const ExpectedURLAndAllowedToBeDefault& expected);
256a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    std::set<ExpectedURLAndAllowedToBeDefault> Leftovers() const {
257a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        return matches_;
258a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   private:
261a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    std::set<ExpectedURLAndAllowedToBeDefault> matches_;
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetUp();
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void TearDown();
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Fills test data into the provider.
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void FillData(TestShortcutInfo* db, size_t db_size);
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
270a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Runs an autocomplete query on |text| with the provided
271a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // |prevent_inline_autocomplete| setting and checks to see that the returned
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // results' destination URLs match those provided. |expected_urls| does not
273a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // need to be in sorted order, but |expected_top_result| should be the top
274a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // match, and it should have inline autocompletion
275a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // |top_result_inline_autocompletion|.
276a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void RunTest(const base::string16 text,
277a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)               bool prevent_inline_autocomplete,
278a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)               const ExpectedURLs& expected_urls,
279a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)               std::string expected_top_result,
280a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)               base::string16 top_result_inline_autocompletion);
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Passthrough to the private function in provider_.
2834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int CalculateScore(const std::string& terms,
28423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)                     const history::ShortcutsDatabase::Shortcut& shortcut,
2854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                     int max_relevance);
2864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
28790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::MessageLoopForUI message_loop_;
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::TestBrowserThread ui_thread_;
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::TestBrowserThread file_thread_;
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestingProfile profile_;
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ACMatches ac_matches_;  // The resulting matches after running RunTest.
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<ShortcutsBackend> backend_;
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<ShortcutsProvider> provider_;
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ShortcutsProviderTest::ShortcutsProviderTest()
3004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    : ui_thread_(content::BrowserThread::UI, &message_loop_),
3014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      file_thread_(content::BrowserThread::FILE, &message_loop_) {
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ShortcutsProviderTest::SetUp() {
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse(
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &profile_, &ShortcutsBackendFactory::BuildProfileNoDatabaseForTesting);
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  backend_ = ShortcutsBackendFactory::GetForProfile(&profile_);
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(backend_.get());
309bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  ASSERT_TRUE(profile_.CreateHistoryService(true, false));
310116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  provider_ = new ShortcutsProvider(&profile_);
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FillData(shortcut_test_db, arraysize(shortcut_test_db));
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ShortcutsProviderTest::TearDown() {
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Run all pending tasks or else some threads hold on to the message loop
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and prevent it from being deleted.
3172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  message_loop_.RunUntilIdle();
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  provider_ = NULL;
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ShortcutsProviderTest::FillData(TestShortcutInfo* db, size_t db_size) {
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(provider_.get());
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t expected_size = backend_->shortcuts_map().size() + db_size;
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < db_size; ++i) {
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const TestShortcutInfo& cur = db[i];
32623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    history::ShortcutsDatabase::Shortcut shortcut(
3274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        cur.guid, ASCIIToUTF16(cur.text),
32823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)        history::ShortcutsDatabase::Shortcut::MatchCore(
3294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            ASCIIToUTF16(cur.fill_into_edit), GURL(cur.destination_url),
33023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)            ASCIIToUTF16(cur.contents), cur.contents_class,
33123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)            ASCIIToUTF16(cur.description), cur.description_class,
3324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            cur.transition, cur.type, ASCIIToUTF16(cur.keyword)),
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::Time::Now() - base::TimeDelta::FromDays(cur.days_from_now),
3344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        cur.number_of_hits);
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    backend_->AddShortcut(shortcut);
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(expected_size, backend_->shortcuts_map().size());
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ShortcutsProviderTest::SetShouldContain::SetShouldContain(
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ACMatches& matched_urls) {
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (ACMatches::const_iterator iter = matched_urls.begin();
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       iter != matched_urls.end(); ++iter)
344a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    matches_.insert(ExpectedURLAndAllowedToBeDefault(
345a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        iter->destination_url.spec(), iter->allowed_to_be_default_match));
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ShortcutsProviderTest::SetShouldContain::operator()(
349a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const ExpectedURLAndAllowedToBeDefault& expected) {
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(1U, matches_.erase(expected));
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
353a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void ShortcutsProviderTest::RunTest(
354a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16 text,
355a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    bool prevent_inline_autocomplete,
356a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const ExpectedURLs& expected_urls,
357a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    std::string expected_top_result,
358a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::string16 top_result_inline_autocompletion) {
35990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::MessageLoop::current()->RunUntilIdle();
360a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  AutocompleteInput input(text, base::string16::npos, base::string16(), GURL(),
3616d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                          metrics::OmniboxEventProto::INVALID_SPEC,
362116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                          prevent_inline_autocomplete, false, true, true,
363116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                          ChromeAutocompleteSchemeClassifier(&profile_));
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  provider_->Start(input, false);
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(provider_->done());
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ac_matches_ = provider_->matches();
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We should have gotten back at most AutocompleteProvider::kMaxMatches.
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_LE(ac_matches_.size(), AutocompleteProvider::kMaxMatches);
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the number of expected and actual matches aren't equal then we need
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // test no further, but let's do anyway so that we know which URLs failed.
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(expected_urls.size(), ac_matches_.size());
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Verify that all expected URLs were found and that all found URLs
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // were expected.
378a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  std::set<ExpectedURLAndAllowedToBeDefault> Leftovers =
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      for_each(expected_urls.begin(), expected_urls.end(),
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               SetShouldContain(ac_matches_)).Leftovers();
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0U, Leftovers.size());
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // See if we got the expected top scorer.
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!ac_matches_.empty()) {
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::partial_sort(ac_matches_.begin(), ac_matches_.begin() + 1,
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      ac_matches_.end(), AutocompleteMatch::MoreRelevant);
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(expected_top_result, ac_matches_[0].destination_url.spec());
388a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    EXPECT_EQ(top_result_inline_autocompletion,
389a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)              ac_matches_[0].inline_autocompletion);
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)int ShortcutsProviderTest::CalculateScore(
3944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const std::string& terms,
39523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    const history::ShortcutsDatabase::Shortcut& shortcut,
3964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    int max_relevance) {
3974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return provider_->CalculateScore(ASCIIToUTF16(terms), shortcut,
3984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                   max_relevance);
3994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
4004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Actual tests ---------------------------------------------------------------
4034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ShortcutsProviderTest, SimpleSingleMatch) {
405a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 text(ASCIIToUTF16("go"));
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string expected_url("http://www.google.com/");
407a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ExpectedURLs expected_urls;
408a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true));
409a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16("ogle.com"));
410a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
411a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Same test with prevent inline autocomplete.
412a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.clear();
413a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(
414a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ExpectedURLAndAllowedToBeDefault(expected_url, false));
415a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // The match will have an |inline_autocompletion| set, but the value will not
416a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // be used because |allowed_to_be_default_match| will be false.
417a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RunTest(text, true, expected_urls, expected_url, ASCIIToUTF16("ogle.com"));
418a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
419a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // A pair of analogous tests where the shortcut ends at the end of
420a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // |fill_into_edit|.  This exercises the inline autocompletion and default
421a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // match code.
422a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  text = ASCIIToUTF16("abcdef.com");
423a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_url = "http://abcdef.com/";
424a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.clear();
425a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true));
426a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RunTest(text, false, expected_urls, expected_url, base::string16());
427a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // With prevent inline autocomplete, the suggestion should be the same
428a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // (because there is no completion).
429a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RunTest(text, true, expected_urls, expected_url, base::string16());
430a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
431a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Another test, simply for a query match type, not a navigation URL match
432a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // type.
433a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  text = ASCIIToUTF16("que");
434a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_url = "https://www.google.com/search?q=query";
435a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.clear();
436a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true));
437a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16("ry"));
438a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
439a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Same test with prevent inline autocomplete.
440a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.clear();
441a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(
442a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ExpectedURLAndAllowedToBeDefault(expected_url, false));
443a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // The match will have an |inline_autocompletion| set, but the value will not
444a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // be used because |allowed_to_be_default_match| will be false.
445a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RunTest(text, true, expected_urls, expected_url, ASCIIToUTF16("ry"));
446a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
447a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // A pair of analogous tests where the shortcut ends at the end of
448a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // |fill_into_edit|.  This exercises the inline autocompletion and default
449a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // match code.
450a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  text = ASCIIToUTF16("query");
451a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.clear();
452a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true));
453a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RunTest(text, false, expected_urls, expected_url, base::string16());
454a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // With prevent inline autocomplete, the suggestion should be the same
455a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // (because there is no completion).
456a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RunTest(text, true, expected_urls, expected_url, base::string16());
457a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
458a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Now the shortcut ends at the end of |fill_into_edit| but has a
459a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // non-droppable prefix.  ("www.", for instance, is not droppable for
460a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // queries.)
461a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  text = ASCIIToUTF16("word");
462a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_url = "https://www.google.com/search?q=www.word";
463a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.clear();
464a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(
465a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ExpectedURLAndAllowedToBeDefault(expected_url, false));
466a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RunTest(text, false, expected_urls, expected_url, base::string16());
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// These tests are like those in SimpleSingleMatch but more complex,
4705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// involving URLs that need to be fixed up to match properly.
4715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(ShortcutsProviderTest, TrickySingleMatch) {
4725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Test that about: URLs are fixed up/transformed to chrome:// URLs.
4735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::string16 text(ASCIIToUTF16("about:o"));
4745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string expected_url("chrome://omnibox/");
4755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ExpectedURLs expected_urls;
4765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true));
4775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16("mnibox"));
4785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Same test with prevent inline autocomplete.
4805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  expected_urls.clear();
4815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  expected_urls.push_back(
4825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ExpectedURLAndAllowedToBeDefault(expected_url, false));
4835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The match will have an |inline_autocompletion| set, but the value will not
4845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // be used because |allowed_to_be_default_match| will be false.
4855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RunTest(text, true, expected_urls, expected_url, ASCIIToUTF16("mnibox"));
4865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Test that an input with a space can match URLs with a (escaped) space.
4885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This would fail if we didn't try to lookup the un-fixed-up string.
4895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  text = ASCIIToUTF16("www/real sp");
4905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  expected_url = "http://www/real%20space/long-url-with-space.html";
4915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  expected_urls.clear();
4925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true));
4935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RunTest(text, false, expected_urls, expected_url,
4945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          ASCIIToUTF16("ace/long-url-with-space.html"));
4955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Same test with prevent inline autocomplete.
4975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  expected_urls.clear();
4985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  expected_urls.push_back(
4995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ExpectedURLAndAllowedToBeDefault(expected_url, false));
5005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The match will have an |inline_autocompletion| set, but the value will not
5015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // be used because |allowed_to_be_default_match| will be false.
5025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RunTest(text, true, expected_urls, expected_url,
5035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          ASCIIToUTF16("ace/long-url-with-space.html"));
504c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
505c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Test when the user input has a trailing slash but fill_into_edit does
506c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // not.  This should still be allowed to be default.
507c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  text = ASCIIToUTF16("notrailing.com/");
508c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  expected_url = "http://notrailing.com/";
509c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  expected_urls.clear();
510c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  expected_urls.push_back(
511c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      ExpectedURLAndAllowedToBeDefault(expected_url, true));
512c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  RunTest(text, true, expected_urls, expected_url, base::string16());
513c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
514c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Test when the user input has a typo that can be fixed up for matching
515c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // fill_into_edit.  This should still be allowed to be default.
516c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  text = ASCIIToUTF16("http:///foo.com");
517c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  expected_url = "http://foo.com/";
518c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  expected_urls.clear();
519c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  expected_urls.push_back(
520c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      ExpectedURLAndAllowedToBeDefault(expected_url, true));
521c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  RunTest(text, true, expected_urls, expected_url, base::string16());
522a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
523a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // A foursome of tests to verify that trailing spaces prevent the shortcut
524a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // from being allowed to be the default match.  For each of two tests, we
525a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // first verify that the match is allowed to be default without the trailing
526a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // space but is not allowed to be default with the trailing space.  In both
527a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // of these with-trailing-space cases, we actually get an
528a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // inline_autocompletion, though it's never used because the match is
529a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // prohibited from being default.
530a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  text = ASCIIToUTF16("trailing1");
531a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  expected_url = "http://trailing1.com/";
532a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  expected_urls.clear();
533a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  expected_urls.push_back(
534a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      ExpectedURLAndAllowedToBeDefault(expected_url, true));
535a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16(".com"));
536a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  text = ASCIIToUTF16("trailing1 ");
537a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  expected_urls.clear();
538a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  expected_urls.push_back(
539a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      ExpectedURLAndAllowedToBeDefault(expected_url, false));
540a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16(".com"));
541a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  text = ASCIIToUTF16("about:trailing2");
542a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  expected_url = "chrome://trailing2blah/";
543a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  expected_urls.clear();
544a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  expected_urls.push_back(
545a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      ExpectedURLAndAllowedToBeDefault(expected_url, true));
546a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16("blah"));
547a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  text = ASCIIToUTF16("about:trailing2 ");
548a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  expected_urls.clear();
549a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  expected_urls.push_back(
550a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      ExpectedURLAndAllowedToBeDefault(expected_url, false));
551a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16("blah"));
5525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ShortcutsProviderTest, MultiMatch) {
555a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 text(ASCIIToUTF16("NEWS"));
556a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ExpectedURLs expected_urls;
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Scores high because of completion length.
558a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
559a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://slashdot.org/", false));
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Scores high because of visit count.
561a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
562a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://sports.yahoo.com/", false));
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Scores high because of visit count but less match span,
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // which is more important.
565a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
566a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://www.cnn.com/index.html", false));
567a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RunTest(text, false, expected_urls, "http://slashdot.org/", base::string16());
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(ShortcutsProviderTest, RemoveDuplicates) {
5715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::string16 text(ASCIIToUTF16("dupl"));
5725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ExpectedURLs expected_urls;
5735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
5745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "http://duplicate.com/", true));
5755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Make sure the URL only appears once in the output list.
5765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RunTest(text, false, expected_urls, "http://duplicate.com/",
5775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          ASCIIToUTF16("icate.com"));
5785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ShortcutsProviderTest, TypedCountMatches) {
581a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 text(ASCIIToUTF16("just"));
582a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ExpectedURLs expected_urls;
583a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
584a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://www.testsite.com/b.html", false));
585a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
586a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://www.testsite.com/a.html", false));
587a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
588a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://www.testsite.com/c.html", false));
589a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RunTest(text, false, expected_urls, "http://www.testsite.com/b.html",
590a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          base::string16());
5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ShortcutsProviderTest, FragmentLengthMatches) {
594a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 text(ASCIIToUTF16("just a"));
595a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ExpectedURLs expected_urls;
596a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
597a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://www.testsite.com/d.html", false));
598a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
599a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://www.testsite.com/e.html", false));
600a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
601a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://www.testsite.com/f.html", false));
602a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RunTest(text, false, expected_urls, "http://www.testsite.com/d.html",
603a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          base::string16());
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ShortcutsProviderTest, DaysAgoMatches) {
607a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 text(ASCIIToUTF16("ago"));
608a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ExpectedURLs expected_urls;
609a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
610a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://www.daysagotest.com/a.html", false));
611a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
612a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://www.daysagotest.com/b.html", false));
613a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
614a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://www.daysagotest.com/c.html", false));
615a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RunTest(text, false, expected_urls, "http://www.daysagotest.com/a.html",
616a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          base::string16());
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ShortcutsProviderTest, ClassifyAllMatchesInString) {
6204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ACMatchClassifications matches =
6214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      AutocompleteMatch::ClassificationsFromString("0,0");
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ClassifyTest classify_test(ASCIIToUTF16("A man, a plan, a canal Panama"),
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             matches);
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ACMatchClassifications spans_a = classify_test.RunTest(ASCIIToUTF16("man"));
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ACMatch spans should be: '--MMM------------------------'
6274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ("0,0,2,2,5,0", AutocompleteMatch::ClassificationsToString(spans_a));
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ACMatchClassifications spans_b = classify_test.RunTest(ASCIIToUTF16("man p"));
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ACMatch spans should be: '--MMM----M-------------M-----'
6314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ("0,0,2,2,5,0,9,2,10,0,23,2,24,0",
6324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            AutocompleteMatch::ClassificationsToString(spans_b));
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ACMatchClassifications spans_c =
6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      classify_test.RunTest(ASCIIToUTF16("man plan panama"));
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ACMatch spans should be:'--MMM----MMMM----------MMMMMM'
6374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ("0,0,2,2,5,0,9,2,13,0,23,2",
6384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            AutocompleteMatch::ClassificationsToString(spans_c));
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ClassifyTest classify_test2(ASCIIToUTF16("Yahoo! Sports - Sports News, "
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "Scores, Rumors, Fantasy Games, and more"), matches);
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ACMatchClassifications spans_d = classify_test2.RunTest(ASCIIToUTF16("ne"));
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ACMatch spans should match first two letters of the "news".
6454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ("0,0,23,2,25,0",
6464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            AutocompleteMatch::ClassificationsToString(spans_d));
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ACMatchClassifications spans_e =
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      classify_test2.RunTest(ASCIIToUTF16("news r"));
6504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  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,"
6514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            "66,2,67,0", AutocompleteMatch::ClassificationsToString(spans_e));
6524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
6534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  matches = AutocompleteMatch::ClassificationsFromString("0,1");
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ClassifyTest classify_test3(ASCIIToUTF16("livescore.goal.com"), matches);
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ACMatchClassifications spans_f = classify_test3.RunTest(ASCIIToUTF16("go"));
6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ACMatch spans should match first two letters of the "goal".
6584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ("0,1,10,3,12,1",
6594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            AutocompleteMatch::ClassificationsToString(spans_f));
6604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
6614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  matches = AutocompleteMatch::ClassificationsFromString("0,0,13,1");
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ClassifyTest classify_test4(ASCIIToUTF16("Email login: mail.somecorp.com"),
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              matches);
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ACMatchClassifications spans_g = classify_test4.RunTest(ASCIIToUTF16("ail"));
6664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ("0,0,2,2,5,0,13,1,14,3,17,1",
6674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            AutocompleteMatch::ClassificationsToString(spans_g));
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ACMatchClassifications spans_h =
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      classify_test4.RunTest(ASCIIToUTF16("lo log"));
6714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ("0,0,6,2,9,0,13,1",
6724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            AutocompleteMatch::ClassificationsToString(spans_h));
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ACMatchClassifications spans_i =
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      classify_test4.RunTest(ASCIIToUTF16("ail em"));
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 'Email' and 'ail' should be matched.
6774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ("0,2,5,0,13,1,14,3,17,1",
6784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            AutocompleteMatch::ClassificationsToString(spans_i));
6795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Some web sites do not have a description.  If the string being searched is
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // empty, the classifications must also be empty: http://crbug.com/148647
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Extra parens in the next line hack around C++03's "most vexing parse".
6835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class ClassifyTest classify_test5((base::string16()),
6845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    ACMatchClassifications());
6855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ACMatchClassifications spans_j = classify_test5.RunTest(ASCIIToUTF16("man"));
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(0U, spans_j.size());
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Matches which end at beginning of classification merge properly.
6894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  matches = AutocompleteMatch::ClassificationsFromString("0,4,9,0");
6905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ClassifyTest classify_test6(ASCIIToUTF16("html password example"), matches);
6915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Extra space in the next string avoids having the string be a prefix of the
6935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // text above, which would allow for two different valid classification sets,
6945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // one of which uses two spans (the first of which would mark all of "html
6955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // pass" as a match) and one which uses four (which marks the individual words
6965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // as matches but not the space between them).  This way only the latter is
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // valid.
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ACMatchClassifications spans_k =
6995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      classify_test6.RunTest(ASCIIToUTF16("html  pass"));
7004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ("0,6,4,4,5,6,9,0",
7014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            AutocompleteMatch::ClassificationsToString(spans_k));
7025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Multiple matches with both beginning and end at beginning of
7045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // classifications merge properly.
7054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  matches = AutocompleteMatch::ClassificationsFromString("0,1,11,0");
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ClassifyTest classify_test7(ASCIIToUTF16("http://a.co is great"), matches);
7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ACMatchClassifications spans_l =
7095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      classify_test7.RunTest(ASCIIToUTF16("ht co"));
7104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ("0,3,2,1,9,3,11,0",
7114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            AutocompleteMatch::ClassificationsToString(spans_l));
7125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ShortcutsProviderTest, CalculateScore) {
71523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  history::ShortcutsDatabase::Shortcut shortcut(
7164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      std::string(), ASCIIToUTF16("test"),
71723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      history::ShortcutsDatabase::Shortcut::MatchCore(
7184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          ASCIIToUTF16("www.test.com"), GURL("http://www.test.com"),
71923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)          ASCIIToUTF16("www.test.com"), "0,1,4,3,8,1",
7201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          ASCIIToUTF16("A test"), "0,0,2,2", ui::PAGE_TRANSITION_TYPED,
72123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)          AutocompleteMatchType::HISTORY_URL, base::string16()),
7224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      base::Time::Now(), 1);
7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Maximal score.
7250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  const int max_relevance =
7260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      ShortcutsProvider::kShortcutsProviderDefaultMaxRelevance;
7274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const int kMaxScore = CalculateScore("test", shortcut, max_relevance);
7285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Score decreases as percent of the match is decreased.
7304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int score_three_quarters = CalculateScore("tes", shortcut, max_relevance);
7315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_LT(score_three_quarters, kMaxScore);
7324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int score_one_half = CalculateScore("te", shortcut, max_relevance);
7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_LT(score_one_half, score_three_quarters);
7344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int score_one_quarter = CalculateScore("t", shortcut, max_relevance);
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_LT(score_one_quarter, score_one_half);
7365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should decay with time - one week.
7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  shortcut.last_access_time = base::Time::Now() - base::TimeDelta::FromDays(7);
7394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int score_week_old = CalculateScore("test", shortcut, max_relevance);
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_LT(score_week_old, kMaxScore);
7415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should decay more in two weeks.
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  shortcut.last_access_time = base::Time::Now() - base::TimeDelta::FromDays(14);
7444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int score_two_weeks_old = CalculateScore("test", shortcut, max_relevance);
7455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_LT(score_two_weeks_old, score_week_old);
7465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // But not if it was activly clicked on. 2 hits slow decaying power.
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  shortcut.number_of_hits = 2;
7495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  shortcut.last_access_time = base::Time::Now() - base::TimeDelta::FromDays(14);
7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int score_popular_two_weeks_old =
7514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      CalculateScore("test", shortcut, max_relevance);
7525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_LT(score_two_weeks_old, score_popular_two_weeks_old);
7535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // But still decayed.
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_LT(score_popular_two_weeks_old, kMaxScore);
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 3 hits slow decaying power even more.
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  shortcut.number_of_hits = 3;
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  shortcut.last_access_time = base::Time::Now() - base::TimeDelta::FromDays(14);
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int score_more_popular_two_weeks_old =
7604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      CalculateScore("test", shortcut, max_relevance);
7615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_LT(score_two_weeks_old, score_more_popular_two_weeks_old);
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_LT(score_popular_two_weeks_old, score_more_popular_two_weeks_old);
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // But still decayed.
7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_LT(score_more_popular_two_weeks_old, kMaxScore);
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ShortcutsProviderTest, DeleteMatch) {
7684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  TestShortcutInfo shortcuts_to_test_delete[] = {
7694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    { "BD85DBA2-8C29-49F9-84AE-48E1E90881F1", "delete", "www.deletetest.com/1",
7704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      "http://www.deletetest.com/1", "http://www.deletetest.com/1", "0,2",
7711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Erase this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED,
7724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      AutocompleteMatchType::HISTORY_URL, "", 1, 1},
7734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    { "BD85DBA2-8C29-49F9-84AE-48E1E90881F2", "erase", "www.deletetest.com/1",
7744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      "http://www.deletetest.com/1", "http://www.deletetest.com/1", "0,2",
7751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Erase this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED,
7764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      AutocompleteMatchType::HISTORY_TITLE, "", 1, 1},
7774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    { "BD85DBA2-8C29-49F9-84AE-48E1E90881F3", "keep", "www.deletetest.com/1/2",
7784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      "http://www.deletetest.com/1/2", "http://www.deletetest.com/1/2", "0,2",
7791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Keep this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED,
7804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      AutocompleteMatchType::HISTORY_TITLE, "", 1, 1},
7814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    { "BD85DBA2-8C29-49F9-84AE-48E1E90881F4", "delete", "www.deletetest.com/2",
7824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      "http://www.deletetest.com/2", "http://www.deletetest.com/2", "0,2",
7831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Erase this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED,
7844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      AutocompleteMatchType::HISTORY_URL, "", 1, 1},
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t original_shortcuts_count = backend_->shortcuts_map().size();
7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FillData(shortcuts_to_test_delete, arraysize(shortcuts_to_test_delete));
7905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ(original_shortcuts_count + 4, backend_->shortcuts_map().size());
7925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(backend_->shortcuts_map().end() ==
7935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               backend_->shortcuts_map().find(ASCIIToUTF16("delete")));
7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(backend_->shortcuts_map().end() ==
7955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               backend_->shortcuts_map().find(ASCIIToUTF16("erase")));
7965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
797868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  AutocompleteMatch match(
798868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      provider_.get(), 1200, true, AutocompleteMatchType::HISTORY_TITLE);
7995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  match.destination_url = GURL(shortcuts_to_test_delete[0].destination_url);
8015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.contents = ASCIIToUTF16(shortcuts_to_test_delete[0].contents);
8025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.description = ASCIIToUTF16(shortcuts_to_test_delete[0].description);
8035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  provider_->DeleteMatch(match);
8055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // shortcuts_to_test_delete[0] and shortcuts_to_test_delete[1] should be
8074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // deleted, but not shortcuts_to_test_delete[2] or
8084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // shortcuts_to_test_delete[3], which have different URLs.
8094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ(original_shortcuts_count + 2, backend_->shortcuts_map().size());
8105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(backend_->shortcuts_map().end() ==
8115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               backend_->shortcuts_map().find(ASCIIToUTF16("delete")));
8125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(backend_->shortcuts_map().end() ==
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              backend_->shortcuts_map().find(ASCIIToUTF16("erase")));
8145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  match.destination_url = GURL(shortcuts_to_test_delete[3].destination_url);
8164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  match.contents = ASCIIToUTF16(shortcuts_to_test_delete[3].contents);
8174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  match.description = ASCIIToUTF16(shortcuts_to_test_delete[3].description);
8185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  provider_->DeleteMatch(match);
8204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ(original_shortcuts_count + 1, backend_->shortcuts_map().size());
8215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(backend_->shortcuts_map().end() ==
8225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              backend_->shortcuts_map().find(ASCIIToUTF16("delete")));
8235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
8251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(ENABLE_EXTENSIONS)
8264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)TEST_F(ShortcutsProviderTest, Extension) {
8274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Try an input string that matches an extension URL.
828a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 text(ASCIIToUTF16("echo"));
8294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::string expected_url(
8304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      "chrome-extension://cedabbhfglmiikkmdgcpjdkocfcmbkee/?q=echo");
831a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ExpectedURLs expected_urls;
832a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(
833a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      expected_url, true));
834a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16(" echo"));
8354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
8364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Claim the extension has been unloaded.
8374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  scoped_refptr<const extensions::Extension> extension =
8384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      extensions::ExtensionBuilder()
8394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          .SetManifest(extensions::DictionaryBuilder()
8404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)              .Set("name", "Echo")
8414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)              .Set("version", "1.0"))
8424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          .SetID("cedabbhfglmiikkmdgcpjdkocfcmbkee")
8434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          .Build();
8444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  extensions::UnloadedExtensionInfo details(
8451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      extension.get(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL);
8464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  content::NotificationService::current()->Notify(
8475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
8484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      content::Source<Profile>(&profile_),
8494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      content::Details<extensions::UnloadedExtensionInfo>(&details));
8504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
8514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Now the URL should have disappeared.
852a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RunTest(text, false, ExpectedURLs(), std::string(), base::string16());
8534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
8541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
855