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