summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autocomplete/history_url_provider_unittest.cc
blob: 05a696d19bac328ab6082d61ad393fcfb8fe0d06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "chrome/browser/autocomplete/history_url_provider.h"
#include "chrome/browser/bookmark_bar_model.h"
#include "chrome/browser/history/history.h"
#include "chrome/test/testing_profile.h"
#include "testing/gtest/include/gtest/gtest.h"

struct TestURLInfo {
  std::wstring url;
  std::wstring title;
  int visit_count;
  int typed_count;
  bool starred;
};

// Contents of the test database.
static TestURLInfo test_db[] = {
  {L"http://www.google.com/", L"Google", 3, 3, false},

  // High-quality pages should get a host synthesized as a lower-quality match.
  {L"http://slashdot.org/favorite_page.html", L"Favorite page", 200, 100,
   false},

  // Less popular pages should have hosts synthesized as higher-quality
  // matches.
  {L"http://kerneltrap.org/not_very_popular.html", L"Less popular", 4, 0,
   false},

  // Unpopular pages should not appear in the results at all.
  {L"http://freshmeat.net/unpopular.html", L"Unpopular", 1, 1, false},

  // If a host has a match, we should pick it up during host synthesis.
  {L"http://news.google.com/?ned=us&topic=n", L"Google News - U.S.", 2, 2,
   false},
  {L"http://news.google.com/", L"Google News", 1, 1, false},

  // Suggested short URLs must be "good enough" and must match user input.
  {L"http://foo.com/", L"Dir", 5, 5, false},
  {L"http://foo.com/dir/", L"Dir", 2, 2, false},
  {L"http://foo.com/dir/another/", L"Dir", 5, 1, false},
  {L"http://foo.com/dir/another/again/", L"Dir", 10, 0, false},
  {L"http://foo.com/dir/another/again/myfile.html", L"File", 10, 2, false},

  // Starred state is more important than visit count (but less important than
  // typed count) when sorting URLs.  The order in which the URLs were starred
  // shouldn't matter.
  // We throw in a lot of extra URLs here to make sure we're testing the
  // history database's query, not just the autocomplete provider.
  {L"http://startest.com/y/a", L"A", 2, 2, true},
  {L"http://startest.com/y/b", L"B", 5, 2, false},
  {L"http://startest.com/x/c", L"C", 5, 2, true},
  {L"http://startest.com/x/d", L"D", 5, 5, false},
  {L"http://startest.com/y/e", L"E", 4, 2, false},
  {L"http://startest.com/y/f", L"F", 3, 2, false},
  {L"http://startest.com/y/g", L"G", 3, 2, false},
  {L"http://startest.com/y/h", L"H", 3, 2, false},
  {L"http://startest.com/y/i", L"I", 3, 2, false},
  {L"http://startest.com/y/j", L"J", 3, 2, false},
  {L"http://startest.com/y/k", L"K", 3, 2, false},
  {L"http://startest.com/y/l", L"L", 3, 2, false},
  {L"http://startest.com/y/m", L"M", 3, 2, false},

  // A file: URL is useful for testing that fixup does the right thing w.r.t.
  // the number of trailing slashes on the user's input.
  {L"file:///C:/foo.txt", L"", 2, 2, false},

  // Results with absurdly high typed_counts so that very generic queries like
  // "http" will give consistent results even if more data is added above.
  {L"http://bogussite.com/a", L"Bogus A", 10002, 10000, false},
  {L"http://bogussite.com/b", L"Bogus B", 10001, 10000, false},
  {L"http://bogussite.com/c", L"Bogus C", 10000, 10000, false},
};

class HistoryURLProviderTest : public testing::Test,
                               public ACProviderListener {
  // ACProviderListener
  virtual void OnProviderUpdate(bool updated_matches);

 protected:
  // testing::Test
  virtual void SetUp();
  virtual void TearDown();

  // Fills test data into the history system
  void FillData();

  // Runs an autocomplete query on |text| and checks to see that the returned
  // results' destination URLs match those provided.
  void RunTest(const std::wstring text,
               const std::wstring& desired_tld,
               bool prevent_inline_autocomplete,
               const std::wstring* expected_urls,
               int num_results);

  MessageLoopForUI message_loop_;
  ACMatches matches_;
  scoped_ptr<TestingProfile> profile_;
  HistoryService* history_service_;

 private:
  scoped_refptr<HistoryURLProvider> autocomplete_;
};

void HistoryURLProviderTest::OnProviderUpdate(bool updated_matches) {
  if (autocomplete_->done())
    MessageLoop::current()->Quit();
}

void HistoryURLProviderTest::SetUp() {
  profile_.reset(new TestingProfile());
  profile_->CreateBookmarkBarModel();
  profile_->CreateHistoryService(true);
  history_service_ = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);

  autocomplete_ = new HistoryURLProvider(this, profile_.get());

  FillData();
}

void HistoryURLProviderTest::TearDown() {
  autocomplete_ = NULL;
}

void HistoryURLProviderTest::FillData() {
  // All visits are a long time ago (some tests require this since we do some
  // special logic for things visited very recently). Note that this time must
  // be more recent than the "archived history" threshold for the data to go
  // into the main database.
  //
  // TODO(brettw) It would be nice if we could test this behavior, in which
  // case the time would be specifed in the test_db structure.
  Time visit_time = Time::Now() - TimeDelta::FromDays(80);

  for (int i = 0; i < arraysize(test_db); ++i) {
    const TestURLInfo& cur = test_db[i];
    const GURL current_url(cur.url);
    history_service_->AddPageWithDetails(current_url, cur.title,
                                         cur.visit_count, cur.typed_count,
                                         visit_time, false);
    if (cur.starred) {
      profile_->GetBookmarkBarModel()->SetURLStarred(
          current_url, std::wstring(), true);
    }
  }
}

void HistoryURLProviderTest::RunTest(const std::wstring text,
                                     const std::wstring& desired_tld,
                                     bool prevent_inline_autocomplete,
                                     const std::wstring* expected_urls,
                                     int num_results) {
  AutocompleteInput input(text, desired_tld, prevent_inline_autocomplete);
  autocomplete_->Start(input, false, false);
  if (!autocomplete_->done())
    MessageLoop::current()->Run();

  matches_ = autocomplete_->matches();
  ASSERT_EQ(num_results, matches_.size());
  for (int i = 0; i < num_results; ++i)
    EXPECT_EQ(expected_urls[i], matches_[i].destination_url);
}

TEST_F(HistoryURLProviderTest, PromoteShorterURLs) {
  // Test that hosts get synthesized below popular pages.
  const std::wstring expected_nonsynth[] = {
    L"http://slash/",
    L"http://slashdot.org/favorite_page.html",
    L"http://slashdot.org/",
  };
  RunTest(L"slash", std::wstring(), true, expected_nonsynth,
          arraysize(expected_nonsynth));

  // Test that hosts get synthesized above less popular pages.
  const std::wstring expected_synth[] = {
    L"http://kernel/",
    L"http://kerneltrap.org/",
    L"http://kerneltrap.org/not_very_popular.html",
  };
  RunTest(L"kernel", std::wstring(), true, expected_synth,
          arraysize(expected_synth));

  // Test that unpopular pages are ignored completely.
  const std::wstring expected_what_you_typed_only[] = {
    L"http://fresh/",
  };
  RunTest(L"fresh", std::wstring(), true, expected_what_you_typed_only,
          arraysize(expected_what_you_typed_only));

  // Test that if we have a synthesized host that matches a suggestion, they
  // get combined into one.
  const std::wstring expected_combine[] = {
    L"http://news/",
    L"http://news.google.com/",
    L"http://news.google.com/?ned=us&topic=n",
  };
  RunTest(L"news", std::wstring(), true, expected_combine,
          arraysize(expected_combine));
  // The title should also have gotten set properly on the host for the
  // synthesized one, since it was also in the results.
  EXPECT_EQ(std::wstring(L"Google News"), matches_[1].description);

  // Test that short URL matching works correctly as the user types more
  // (several tests):
  // The entry for foo.com is the best of all five foo.com* entries.
  const std::wstring short_1[] = {
    L"http://foo/",
    L"http://foo.com/",
    L"http://foo.com/dir/another/again/myfile.html",
    L"http://foo.com/dir/",
  };
  RunTest(L"foo", std::wstring(), true, short_1, arraysize(short_1));

  // When the user types the whole host, make sure we don't get two results for
  // it.
  const std::wstring short_2[] = {
    L"http://foo.com/",
    L"http://foo.com/dir/another/again/myfile.html",
    L"http://foo.com/dir/",
    L"http://foo.com/dir/another/",
  };
  RunTest(L"foo.com", std::wstring(), true, short_2, arraysize(short_2));
  RunTest(L"foo.com/", std::wstring(), true, short_2, arraysize(short_2));

  // The filename is the second best of the foo.com* entries, but there is a
  // shorter URL that's "good enough".  The host doesn't match the user input
  // and so should not appear.
  const std::wstring short_3[] = {
    L"http://foo.com/d",
    L"http://foo.com/dir/another/",
    L"http://foo.com/dir/another/again/myfile.html",
    L"http://foo.com/dir/",
  };
  RunTest(L"foo.com/d", std::wstring(), true, short_3, arraysize(short_3));

  // We shouldn't promote shorter URLs than the best if they're not good
  // enough.
  const std::wstring short_4[] = {
    L"http://foo.com/dir/another/a",
    L"http://foo.com/dir/another/again/myfile.html",
    L"http://foo.com/dir/another/again/",
  };
  RunTest(L"foo.com/dir/another/a", std::wstring(), true, short_4,
          arraysize(short_4));
}

// Bookmarks have been moved out of the history db, resulting in this no longer
// working. See TODO in URLDatabase::AutocompleteForPrefix.
TEST_F(HistoryURLProviderTest, DISABLED_Starred) {
  // Test that starred pages sort properly.
  const std::wstring star_1[] = {
    L"http://startest/",
    L"http://startest.com/x/d",
    L"http://startest.com/x/c",
    L"http://startest.com/y/a",
  };
  RunTest(L"startest", std::wstring(), true, star_1, arraysize(star_1));
  const std::wstring star_2[] = {
    L"http://startest.com/y",
    L"http://startest.com/y/a",
    L"http://startest.com/y/b",
    L"http://startest.com/y/e",
  };
  RunTest(L"startest.com/y", std::wstring(), true, star_2, arraysize(star_2));
}

TEST_F(HistoryURLProviderTest, CullRedirects) {
  // URLs we will be using, plus the visit counts they will initially get
  // (the redirect set below will also increment the visit counts). We want
  // the results to be in A,B,C order. Note also that our visit counts are
  // all high enough so that domain synthesizing won't get triggered.
  struct RedirectCase {
    const wchar_t* url;
    int count;
  };
  static const RedirectCase redirect[] = {
    {L"http://redirects/A", 30},
    {L"http://redirects/B", 20},
    {L"http://redirects/C", 10}
  };
  for (int i = 0; i < arraysize(redirect); i++) {
    history_service_->AddPageWithDetails(GURL(redirect[i].url), L"Title",
                                         redirect[i].count, redirect[i].count,
                                         Time::Now(), false);
  }

  // Create a B->C->A redirect chain, but set the visit counts such that they
  // will appear in A,B,C order in the results. The autocomplete query will
  // search for the most recent visit when looking for redirects, so this will
  // be found even though the previous visits had no redirects.
  HistoryService::RedirectList redirects_to_a;
  redirects_to_a.push_back(GURL(redirect[1].url));
  redirects_to_a.push_back(GURL(redirect[2].url));
  redirects_to_a.push_back(GURL(redirect[0].url));
  history_service_->AddPage(GURL(redirect[0].url), NULL, 0, GURL(),
                            PageTransition::TYPED, redirects_to_a);

  // Because all the results are part of a redirect chain with other results,
  // all but the first one (A) should be culled. We should get the default
  // "what you typed" result, plus this one.
  const std::wstring typing(L"http://redirects/");
  const std::wstring expected_results[] = {
    typing,
    redirect[0].url};
  RunTest(typing, std::wstring(), true, expected_results,
          arraysize(expected_results));
}

TEST_F(HistoryURLProviderTest, Fixup) {
  // Test for various past crashes we've had.
  RunTest(L"\\", std::wstring(), false, NULL, 0);

  RunTest(L"#", std::wstring(), false, NULL, 0);

  const std::wstring crash_1[] = {L"http://%20/"};
  RunTest(L"%20", std::wstring(), false, crash_1, arraysize(crash_1));

  // Fixing up "file:" should result in an inline autocomplete offset of just
  // after "file:", not just after "file://".
  const std::wstring input_1(L"file:");
  const std::wstring fixup_1[] = {L"file:///", L"file:///C:/foo.txt"};
  RunTest(input_1, std::wstring(), false, fixup_1, arraysize(fixup_1));
  EXPECT_EQ(input_1.length(), matches_[1].inline_autocomplete_offset);

  // Fixing up "http:/" should result in an inline autocomplete offset of just
  // after "http:/", not just after "http:".
  const std::wstring input_2(L"http:/");
  const std::wstring fixup_2[] = {
    L"http://bogussite.com/a",
    L"http://bogussite.com/b",
    L"http://bogussite.com/c",
  };
  RunTest(input_2, std::wstring(), false, fixup_2, arraysize(fixup_2));
  EXPECT_EQ(input_2.length(), matches_[0].inline_autocomplete_offset);

  // Adding a TLD to a small number like "56" should result in "www.56.com"
  // rather than "0.0.0.56.com".
  std::wstring fixup_3[] = {L"http://www.56.com/"};
  RunTest(L"56", L"com", true, fixup_3, arraysize(fixup_3));
}