OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "chrome/browser/autocomplete/history_url_provider.h" | 9 #include "chrome/browser/autocomplete/history_url_provider.h" |
10 #include "chrome/browser/bookmarks/bookmark_model.h" | |
11 #include "chrome/browser/chrome_thread.h" | 10 #include "chrome/browser/chrome_thread.h" |
12 #include "chrome/browser/history/history.h" | 11 #include "chrome/browser/history/history.h" |
13 #include "chrome/test/testing_profile.h" | 12 #include "chrome/test/testing_profile.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
15 #if defined(OS_MACOSX) | 14 #if defined(OS_MACOSX) |
16 #include "base/mac_util.h" | 15 #include "base/mac_util.h" |
17 #endif | 16 #endif |
18 | 17 |
19 using base::Time; | 18 using base::Time; |
20 using base::TimeDelta; | 19 using base::TimeDelta; |
21 | 20 |
22 struct TestURLInfo { | 21 struct TestURLInfo { |
23 std::string url; | 22 std::string url; |
24 std::wstring title; | 23 std::wstring title; |
25 int visit_count; | 24 int visit_count; |
26 int typed_count; | 25 int typed_count; |
27 bool starred; | |
28 }; | 26 }; |
29 | 27 |
30 // Contents of the test database. | 28 // Contents of the test database. |
31 static TestURLInfo test_db[] = { | 29 static TestURLInfo test_db[] = { |
32 {"http://www.google.com/", L"Google", 3, 3, false}, | 30 {"http://www.google.com/", L"Google", 3, 3}, |
33 | 31 |
34 // High-quality pages should get a host synthesized as a lower-quality match. | 32 // High-quality pages should get a host synthesized as a lower-quality match. |
35 {"http://slashdot.org/favorite_page.html", L"Favorite page", 200, 100, | 33 {"http://slashdot.org/favorite_page.html", L"Favorite page", 200, 100}, |
36 false}, | |
37 | 34 |
38 // Less popular pages should have hosts synthesized as higher-quality | 35 // Less popular pages should have hosts synthesized as higher-quality |
39 // matches. | 36 // matches. |
40 {"http://kerneltrap.org/not_very_popular.html", L"Less popular", 4, 0, | 37 {"http://kerneltrap.org/not_very_popular.html", L"Less popular", 4, 0}, |
41 false}, | |
42 | 38 |
43 // Unpopular pages should not appear in the results at all. | 39 // Unpopular pages should not appear in the results at all. |
44 {"http://freshmeat.net/unpopular.html", L"Unpopular", 1, 1, false}, | 40 {"http://freshmeat.net/unpopular.html", L"Unpopular", 1, 1}, |
45 | 41 |
46 // If a host has a match, we should pick it up during host synthesis. | 42 // If a host has a match, we should pick it up during host synthesis. |
47 {"http://news.google.com/?ned=us&topic=n", L"Google News - U.S.", 2, 2, | 43 {"http://news.google.com/?ned=us&topic=n", L"Google News - U.S.", 2, 2}, |
48 false}, | 44 {"http://news.google.com/", L"Google News", 1, 1}, |
49 {"http://news.google.com/", L"Google News", 1, 1, false}, | |
50 | 45 |
51 // Suggested short URLs must be "good enough" and must match user input. | 46 // Suggested short URLs must be "good enough" and must match user input. |
52 {"http://foo.com/", L"Dir", 5, 5, false}, | 47 {"http://foo.com/", L"Dir", 5, 5}, |
53 {"http://foo.com/dir/", L"Dir", 2, 2, false}, | 48 {"http://foo.com/dir/", L"Dir", 2, 2}, |
54 {"http://foo.com/dir/another/", L"Dir", 5, 1, false}, | 49 {"http://foo.com/dir/another/", L"Dir", 5, 1}, |
55 {"http://foo.com/dir/another/again/", L"Dir", 10, 0, false}, | 50 {"http://foo.com/dir/another/again/", L"Dir", 10, 0}, |
56 {"http://foo.com/dir/another/again/myfile.html", L"File", 10, 2, false}, | 51 {"http://foo.com/dir/another/again/myfile.html", L"File", 10, 2}, |
57 | 52 |
58 // Starred state is more important than visit count (but less important than | |
59 // typed count) when sorting URLs. The order in which the URLs were starred | |
60 // shouldn't matter. | |
61 // We throw in a lot of extra URLs here to make sure we're testing the | 53 // We throw in a lot of extra URLs here to make sure we're testing the |
62 // history database's query, not just the autocomplete provider. | 54 // history database's query, not just the autocomplete provider. |
63 {"http://startest.com/y/a", L"A", 2, 2, true}, | 55 {"http://startest.com/y/a", L"A", 2, 2}, |
64 {"http://startest.com/y/b", L"B", 5, 2, false}, | 56 {"http://startest.com/y/b", L"B", 5, 2}, |
65 {"http://startest.com/x/c", L"C", 5, 2, true}, | 57 {"http://startest.com/x/c", L"C", 5, 2}, |
66 {"http://startest.com/x/d", L"D", 5, 5, false}, | 58 {"http://startest.com/x/d", L"D", 5, 5}, |
67 {"http://startest.com/y/e", L"E", 4, 2, false}, | 59 {"http://startest.com/y/e", L"E", 4, 2}, |
68 {"http://startest.com/y/f", L"F", 3, 2, false}, | 60 {"http://startest.com/y/f", L"F", 3, 2}, |
69 {"http://startest.com/y/g", L"G", 3, 2, false}, | 61 {"http://startest.com/y/g", L"G", 3, 2}, |
70 {"http://startest.com/y/h", L"H", 3, 2, false}, | 62 {"http://startest.com/y/h", L"H", 3, 2}, |
71 {"http://startest.com/y/i", L"I", 3, 2, false}, | 63 {"http://startest.com/y/i", L"I", 3, 2}, |
72 {"http://startest.com/y/j", L"J", 3, 2, false}, | 64 {"http://startest.com/y/j", L"J", 3, 2}, |
73 {"http://startest.com/y/k", L"K", 3, 2, false}, | 65 {"http://startest.com/y/k", L"K", 3, 2}, |
74 {"http://startest.com/y/l", L"L", 3, 2, false}, | 66 {"http://startest.com/y/l", L"L", 3, 2}, |
75 {"http://startest.com/y/m", L"M", 3, 2, false}, | 67 {"http://startest.com/y/m", L"M", 3, 2}, |
76 | 68 |
77 // A file: URL is useful for testing that fixup does the right thing w.r.t. | 69 // A file: URL is useful for testing that fixup does the right thing w.r.t. |
78 // the number of trailing slashes on the user's input. | 70 // the number of trailing slashes on the user's input. |
79 {"file:///C:/foo.txt", L"", 2, 2, false}, | 71 {"file:///C:/foo.txt", L"", 2, 2}, |
80 | 72 |
81 // Results with absurdly high typed_counts so that very generic queries like | 73 // Results with absurdly high typed_counts so that very generic queries like |
82 // "http" will give consistent results even if more data is added above. | 74 // "http" will give consistent results even if more data is added above. |
83 {"http://bogussite.com/a", L"Bogus A", 10002, 10000, false}, | 75 {"http://bogussite.com/a", L"Bogus A", 10002, 10000}, |
84 {"http://bogussite.com/b", L"Bogus B", 10001, 10000, false}, | 76 {"http://bogussite.com/b", L"Bogus B", 10001, 10000}, |
85 {"http://bogussite.com/c", L"Bogus C", 10000, 10000, false}, | 77 {"http://bogussite.com/c", L"Bogus C", 10000, 10000}, |
86 | 78 |
87 // Domain name with number. | 79 // Domain name with number. |
88 {"http://www.17173.com/", L"Domain with number", 3, 3, false}, | 80 {"http://www.17173.com/", L"Domain with number", 3, 3}, |
89 | 81 |
90 // URLs to test exact-matching behavior. | 82 // URLs to test exact-matching behavior. |
91 {"http://go/", L"Intranet URL", 1, 1, false}, | 83 {"http://go/", L"Intranet URL", 1, 1}, |
92 {"http://gooey/", L"Intranet URL 2", 5, 5, false}, | 84 {"http://gooey/", L"Intranet URL 2", 5, 5}, |
93 | 85 |
94 }; | 86 }; |
95 | 87 |
96 class HistoryURLProviderTest : public testing::Test, | 88 class HistoryURLProviderTest : public testing::Test, |
97 public ACProviderListener { | 89 public ACProviderListener { |
98 public: | 90 public: |
99 HistoryURLProviderTest() | 91 HistoryURLProviderTest() |
100 : ui_thread_(ChromeThread::UI, &message_loop_), | 92 : ui_thread_(ChromeThread::UI, &message_loop_), |
101 file_thread_(ChromeThread::FILE, &message_loop_) {} | 93 file_thread_(ChromeThread::FILE, &message_loop_) {} |
102 | 94 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 } | 134 } |
143 }; | 135 }; |
144 | 136 |
145 void HistoryURLProviderTest::OnProviderUpdate(bool updated_matches) { | 137 void HistoryURLProviderTest::OnProviderUpdate(bool updated_matches) { |
146 if (autocomplete_->done()) | 138 if (autocomplete_->done()) |
147 MessageLoop::current()->Quit(); | 139 MessageLoop::current()->Quit(); |
148 } | 140 } |
149 | 141 |
150 void HistoryURLProviderTest::SetUpImpl(bool no_db) { | 142 void HistoryURLProviderTest::SetUpImpl(bool no_db) { |
151 profile_.reset(new TestingProfile()); | 143 profile_.reset(new TestingProfile()); |
152 profile_->CreateBookmarkModel(true); | |
153 profile_->BlockUntilBookmarkModelLoaded(); | |
154 profile_->CreateHistoryService(true, no_db); | 144 profile_->CreateHistoryService(true, no_db); |
155 history_service_ = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 145 history_service_ = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
156 | 146 |
157 autocomplete_ = new HistoryURLProvider(this, profile_.get()); | 147 autocomplete_ = new HistoryURLProvider(this, profile_.get()); |
158 | 148 |
159 FillData(); | 149 FillData(); |
160 } | 150 } |
161 | 151 |
162 void HistoryURLProviderTest::TearDown() { | 152 void HistoryURLProviderTest::TearDown() { |
163 autocomplete_ = NULL; | 153 autocomplete_ = NULL; |
164 } | 154 } |
165 | 155 |
166 void HistoryURLProviderTest::FillData() { | 156 void HistoryURLProviderTest::FillData() { |
167 // All visits are a long time ago (some tests require this since we do some | 157 // All visits are a long time ago (some tests require this since we do some |
168 // special logic for things visited very recently). Note that this time must | 158 // special logic for things visited very recently). Note that this time must |
169 // be more recent than the "archived history" threshold for the data to go | 159 // be more recent than the "archived history" threshold for the data to go |
170 // into the main database. | 160 // into the main database. |
171 // | 161 // |
172 // TODO(brettw) It would be nice if we could test this behavior, in which | 162 // TODO(brettw) It would be nice if we could test this behavior, in which |
173 // case the time would be specifed in the test_db structure. | 163 // case the time would be specifed in the test_db structure. |
174 Time visit_time = Time::Now() - TimeDelta::FromDays(80); | 164 Time visit_time = Time::Now() - TimeDelta::FromDays(80); |
175 | 165 |
176 for (size_t i = 0; i < arraysize(test_db); ++i) { | 166 for (size_t i = 0; i < arraysize(test_db); ++i) { |
177 const TestURLInfo& cur = test_db[i]; | 167 const TestURLInfo& cur = test_db[i]; |
178 const GURL current_url(cur.url); | 168 const GURL current_url(cur.url); |
179 history_service_->AddPageWithDetails(current_url, cur.title, | 169 history_service_->AddPageWithDetails(current_url, cur.title, |
180 cur.visit_count, cur.typed_count, | 170 cur.visit_count, cur.typed_count, |
181 visit_time, false); | 171 visit_time, false); |
182 if (cur.starred) { | |
183 profile_->GetBookmarkModel()->SetURLStarred( | |
184 current_url, std::wstring(), true); | |
185 } | |
186 } | 172 } |
187 } | 173 } |
188 | 174 |
189 void HistoryURLProviderTest::RunTest(const std::wstring text, | 175 void HistoryURLProviderTest::RunTest(const std::wstring text, |
190 const std::wstring& desired_tld, | 176 const std::wstring& desired_tld, |
191 bool prevent_inline_autocomplete, | 177 bool prevent_inline_autocomplete, |
192 const std::string* expected_urls, | 178 const std::string* expected_urls, |
193 size_t num_results) { | 179 size_t num_results) { |
194 AutocompleteInput input(text, desired_tld, prevent_inline_autocomplete, | 180 AutocompleteInput input(text, desired_tld, prevent_inline_autocomplete, |
195 false, false); | 181 false, false); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 }; | 271 }; |
286 const std::string short_5b[] = { | 272 const std::string short_5b[] = { |
287 "http://go/", | 273 "http://go/", |
288 "http://gooey/", | 274 "http://gooey/", |
289 "http://www.google.com/", | 275 "http://www.google.com/", |
290 }; | 276 }; |
291 RunTest(L"g", std::wstring(), false, short_5a, arraysize(short_5a)); | 277 RunTest(L"g", std::wstring(), false, short_5a, arraysize(short_5a)); |
292 RunTest(L"go", std::wstring(), false, short_5b, arraysize(short_5b)); | 278 RunTest(L"go", std::wstring(), false, short_5b, arraysize(short_5b)); |
293 } | 279 } |
294 | 280 |
295 // Bookmarks have been moved out of the history db, resulting in this no longer | |
296 // working. See TODO in URLDatabase::AutocompleteForPrefix. | |
297 TEST_F(HistoryURLProviderTest, DISABLED_Starred) { | |
298 // Test that starred pages sort properly. | |
299 const std::string star_1[] = { | |
300 "http://startest/", | |
301 "http://startest.com/x/d", | |
302 "http://startest.com/x/c", | |
303 "http://startest.com/y/a", | |
304 }; | |
305 RunTest(L"startest", std::wstring(), true, star_1, arraysize(star_1)); | |
306 const std::string star_2[] = { | |
307 "http://startest.com/y", | |
308 "http://startest.com/y/a", | |
309 "http://startest.com/y/b", | |
310 "http://startest.com/y/e", | |
311 }; | |
312 RunTest(L"startest.com/y", std::wstring(), true, star_2, arraysize(star_2)); | |
313 } | |
314 | |
315 TEST_F(HistoryURLProviderTest, CullRedirects) { | 281 TEST_F(HistoryURLProviderTest, CullRedirects) { |
316 // URLs we will be using, plus the visit counts they will initially get | 282 // URLs we will be using, plus the visit counts they will initially get |
317 // (the redirect set below will also increment the visit counts). We want | 283 // (the redirect set below will also increment the visit counts). We want |
318 // the results to be in A,B,C order. Note also that our visit counts are | 284 // the results to be in A,B,C order. Note also that our visit counts are |
319 // all high enough so that domain synthesizing won't get triggered. | 285 // all high enough so that domain synthesizing won't get triggered. |
320 struct RedirectCase { | 286 struct RedirectCase { |
321 const char* url; | 287 const char* url; |
322 int count; | 288 int count; |
323 }; | 289 }; |
324 static const RedirectCase redirect[] = { | 290 static const RedirectCase redirect[] = { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 std::string navigation_1[] = {"http://test.com/"}; | 369 std::string navigation_1[] = {"http://test.com/"}; |
404 RunTest(L"test.com", std::wstring(), false, navigation_1, | 370 RunTest(L"test.com", std::wstring(), false, navigation_1, |
405 arraysize(navigation_1)); | 371 arraysize(navigation_1)); |
406 | 372 |
407 std::string navigation_2[] = {"http://slash/"}; | 373 std::string navigation_2[] = {"http://slash/"}; |
408 RunTest(L"slash", std::wstring(), false, navigation_2, | 374 RunTest(L"slash", std::wstring(), false, navigation_2, |
409 arraysize(navigation_2)); | 375 arraysize(navigation_2)); |
410 | 376 |
411 RunTest(L"this is a query", std::wstring(), false, NULL, 0); | 377 RunTest(L"this is a query", std::wstring(), false, NULL, 0); |
412 } | 378 } |
OLD | NEW |