history_quick_provider_unittest.cc revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/autocomplete/history_quick_provider.h"
6
7#include <algorithm>
8#include <functional>
9#include <set>
10#include <string>
11#include <vector>
12
13#include "base/message_loop.h"
14#include "base/scoped_ptr.h"
15#include "base/utf_string_conversions.h"
16#include "chrome/browser/autocomplete/autocomplete_match.h"
17#include "chrome/browser/history/history.h"
18#include "chrome/browser/history/url_database.h"
19#include "chrome/browser/prefs/pref_service.h"
20#include "chrome/common/pref_names.h"
21#include "chrome/test/testing_browser_process.h"
22#include "chrome/test/testing_browser_process_test.h"
23#include "chrome/test/testing_profile.h"
24#include "content/browser/browser_thread.h"
25#include "testing/gtest/include/gtest/gtest.h"
26
27using base::Time;
28using base::TimeDelta;
29
30struct TestURLInfo {
31  std::string url;
32  std::string title;
33  int visit_count;
34  int typed_count;
35  int days_from_now;
36} quick_test_db[] = {
37  {"http://www.google.com/", "Google", 3, 3, 0},
38  {"http://slashdot.org/favorite_page.html", "Favorite page", 200, 100, 0},
39  {"http://kerneltrap.org/not_very_popular.html", "Less popular", 4, 0, 0},
40  {"http://freshmeat.net/unpopular.html", "Unpopular", 1, 1, 0},
41  {"http://news.google.com/?ned=us&topic=n", "Google News - U.S.", 2, 2, 0},
42  {"http://news.google.com/", "Google News", 1, 1, 0},
43  {"http://foo.com/", "Dir", 5, 5, 0},
44  {"http://foo.com/dir/", "Dir", 2, 2, 0},
45  {"http://foo.com/dir/another/", "Dir", 5, 1, 0},
46  {"http://foo.com/dir/another/again/", "Dir", 10, 0, 0},
47  {"http://foo.com/dir/another/again/myfile.html", "File", 10, 2, 0},
48  {"http://startest.com/y/a", "A", 5, 2, 0},
49  {"http://startest.com/y/b", "B", 1, 2, 0},
50  {"http://startest.com/x/c", "C", 1, 1, 1},
51  {"http://startest.com/x/d", "D", 1, 1, 1},
52  {"http://startest.com/y/e", "E", 1, 1, 2},
53  {"http://startest.com/y/f", "F", 1, 1, 2},
54  {"http://startest.com/y/g", "G", 1, 1, 4},
55  {"http://startest.com/y/h", "H", 1, 1, 4},
56  {"http://startest.com/y/i", "I", 1, 1, 5},
57  {"http://startest.com/y/j", "J", 1, 1, 5},
58  {"http://startest.com/y/k", "K", 1, 1, 6},
59  {"http://startest.com/y/l", "L", 1, 1, 6},
60  {"http://startest.com/y/m", "M", 1, 1, 6},
61  {"http://abcdefghixyzjklmnopqrstuvw.com/a", "An XYZ", 1, 1, 0},
62  {"http://spaces.com/path%20with%20spaces/foo.html", "Spaces", 2, 2, 0},
63  {"http://abcdefghijklxyzmnopqrstuvw.com/a", "An XYZ", 1, 1, 0},
64  {"http://abcdefxyzghijklmnopqrstuvw.com/a", "An XYZ", 1, 1, 0},
65  {"http://abcxyzdefghijklmnopqrstuvw.com/a", "An XYZ", 1, 1, 0},
66  {"http://xyzabcdefghijklmnopqrstuvw.com/a", "An XYZ", 1, 1, 0},
67};
68
69class HistoryQuickProviderTest : public TestingBrowserProcessTest,
70                                 public ACProviderListener {
71 public:
72  HistoryQuickProviderTest()
73      : ui_thread_(BrowserThread::UI, &message_loop_),
74        file_thread_(BrowserThread::FILE, &message_loop_) {}
75
76  // ACProviderListener
77  virtual void OnProviderUpdate(bool updated_matches);
78
79 protected:
80  void SetUp() {
81    profile_.reset(new TestingProfile());
82    profile_->CreateHistoryService(true, false);
83    profile_->CreateBookmarkModel(true);
84    profile_->BlockUntilBookmarkModelLoaded();
85    history_service_ = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
86    EXPECT_TRUE(history_service_);
87    provider_ = new HistoryQuickProvider(this, profile_.get());
88    FillData();
89  }
90
91  void TearDown() {
92    provider_ = NULL;
93  }
94
95  // Fills test data into the history system.
96  void FillData();
97
98  // Runs an autocomplete query on |text| and checks to see that the returned
99  // results' destination URLs match those provided. |expected_urls| does not
100  // need to be in sorted order.
101  void RunTest(const string16 text,
102               std::vector<std::string> expected_urls,
103               std::string expected_top_result);
104
105  MessageLoopForUI message_loop_;
106  BrowserThread ui_thread_;
107  BrowserThread file_thread_;
108
109  scoped_ptr<TestingProfile> profile_;
110  HistoryService* history_service_;
111
112 private:
113  scoped_refptr<HistoryQuickProvider> provider_;
114};
115
116void HistoryQuickProviderTest::OnProviderUpdate(bool updated_matches) {
117  MessageLoop::current()->Quit();
118}
119
120void HistoryQuickProviderTest::FillData() {
121  history::URLDatabase* db = history_service_->InMemoryDatabase();
122  ASSERT_TRUE(db != NULL);
123  for (size_t i = 0; i < arraysize(quick_test_db); ++i) {
124    const TestURLInfo& cur = quick_test_db[i];
125    const GURL current_url(cur.url);
126    Time visit_time = Time::Now() - TimeDelta::FromDays(cur.days_from_now);
127
128    history::URLRow url_info(current_url);
129    url_info.set_title(UTF8ToUTF16(cur.title));
130    url_info.set_visit_count(cur.visit_count);
131    url_info.set_typed_count(cur.typed_count);
132    url_info.set_last_visit(visit_time);
133    url_info.set_hidden(false);
134    EXPECT_TRUE(db->AddURL(url_info));
135
136    history_service_->AddPageWithDetails(current_url, UTF8ToUTF16(cur.title),
137                                         cur.visit_count, cur.typed_count,
138                                         visit_time, false,
139                                         history::SOURCE_BROWSED);
140  }
141
142  history::InMemoryURLIndex* index =
143      new history::InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy")));
144  PrefService* prefs = profile_->GetPrefs();
145  std::string languages(prefs->GetString(prefs::kAcceptLanguages));
146  index->Init(db, languages);
147  provider_->SetIndexForTesting(index);
148}
149
150class SetShouldContain : public std::unary_function<const std::string&,
151                                                    std::set<std::string> > {
152 public:
153  explicit SetShouldContain(const ACMatches& matched_urls) {
154    for (ACMatches::const_iterator iter = matched_urls.begin();
155         iter != matched_urls.end(); ++iter)
156      matches_.insert(iter->destination_url.spec());
157  }
158
159  void operator()(const std::string& expected) {
160    EXPECT_EQ(1U, matches_.erase(expected));
161  }
162
163  std::set<std::string> LeftOvers() const { return matches_; }
164
165 private:
166  std::set<std::string> matches_;
167};
168
169void HistoryQuickProviderTest::RunTest(const string16 text,
170                                       std::vector<std::string> expected_urls,
171                                       std::string expected_top_result) {
172  std::sort(expected_urls.begin(), expected_urls.end());
173
174  MessageLoop::current()->RunAllPending();
175  AutocompleteInput input(text, string16(), false, false, true, false);
176  provider_->Start(input, false);
177  EXPECT_TRUE(provider_->done());
178
179  ACMatches matches = provider_->matches();
180  // If the number of expected and actual matches aren't equal then we need
181  // test no further, but let's do anyway so that we know which URLs failed.
182  EXPECT_EQ(expected_urls.size(), matches.size());
183
184  // Verify that all expected URLs were found and that all found URLs
185  // were expected.
186  std::set<std::string> leftovers =
187      for_each(expected_urls.begin(), expected_urls.end(),
188               SetShouldContain(matches)).LeftOvers();
189  EXPECT_TRUE(leftovers.empty());
190
191  // See if we got the expected top scorer.
192  if (!matches.empty()) {
193    std::partial_sort(matches.begin(), matches.begin() + 1,
194                      matches.end(), AutocompleteMatch::MoreRelevant);
195    EXPECT_EQ(expected_top_result, matches[0].destination_url.spec());
196  }
197}
198
199TEST_F(HistoryQuickProviderTest, SimpleSingleMatch) {
200  string16 text(ASCIIToUTF16("slashdot"));
201  std::string expected_url("http://slashdot.org/favorite_page.html");
202  std::vector<std::string> expected_urls;
203  expected_urls.push_back(expected_url);
204  RunTest(text, expected_urls, expected_url);
205}
206
207TEST_F(HistoryQuickProviderTest, MultiMatch) {
208  string16 text(ASCIIToUTF16("foo"));
209  std::vector<std::string> expected_urls;
210  expected_urls.push_back("http://foo.com/");
211  expected_urls.push_back("http://foo.com/dir/");
212  expected_urls.push_back("http://foo.com/dir/another/");
213  expected_urls.push_back("http://foo.com/dir/another/again/");
214  expected_urls.push_back("http://foo.com/dir/another/again/myfile.html");
215  expected_urls.push_back("http://spaces.com/path%20with%20spaces/foo.html");
216  RunTest(text, expected_urls, "http://foo.com/");
217}
218
219TEST_F(HistoryQuickProviderTest, StartRelativeMatch) {
220  string16 text(ASCIIToUTF16("xyz"));
221  std::vector<std::string> expected_urls;
222  expected_urls.push_back("http://xyzabcdefghijklmnopqrstuvw.com/a");
223  expected_urls.push_back("http://abcxyzdefghijklmnopqrstuvw.com/a");
224  expected_urls.push_back("http://abcdefxyzghijklmnopqrstuvw.com/a");
225  expected_urls.push_back("http://abcdefghixyzjklmnopqrstuvw.com/a");
226  expected_urls.push_back("http://abcdefghijklxyzmnopqrstuvw.com/a");
227  RunTest(text, expected_urls, "http://xyzabcdefghijklmnopqrstuvw.com/a");
228}
229
230TEST_F(HistoryQuickProviderTest, RecencyMatch) {
231  string16 text(ASCIIToUTF16("startest"));
232  std::vector<std::string> expected_urls;
233  expected_urls.push_back("http://startest.com/y/a");
234  expected_urls.push_back("http://startest.com/y/b");
235  expected_urls.push_back("http://startest.com/x/c");
236  expected_urls.push_back("http://startest.com/x/d");
237  expected_urls.push_back("http://startest.com/y/e");
238  expected_urls.push_back("http://startest.com/y/f");
239  RunTest(text, expected_urls, "http://startest.com/y/a");
240}
241