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