OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/path_service.h" | 6 #include "base/path_service.h" |
7 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
8 #include "chrome/browser/bookmarks/bookmark_model.h" | 8 #include "chrome/browser/bookmarks/bookmark_model.h" |
9 #include "chrome/browser/history/history_backend.h" | 9 #include "chrome/browser/history/history_backend.h" |
10 #include "chrome/browser/history/in_memory_history_backend.h" | 10 #include "chrome/browser/history/in_memory_history_backend.h" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 class HistoryBackendTest : public testing::Test { | 50 class HistoryBackendTest : public testing::Test { |
51 public: | 51 public: |
52 HistoryBackendTest() : bookmark_model_(NULL), loaded_(false) {} | 52 HistoryBackendTest() : bookmark_model_(NULL), loaded_(false) {} |
53 virtual ~HistoryBackendTest() { | 53 virtual ~HistoryBackendTest() { |
54 } | 54 } |
55 | 55 |
56 protected: | 56 protected: |
57 scoped_refptr<HistoryBackend> backend_; // Will be NULL on init failure. | 57 scoped_refptr<HistoryBackend> backend_; // Will be NULL on init failure. |
58 scoped_ptr<InMemoryHistoryBackend> mem_backend_; | 58 scoped_ptr<InMemoryHistoryBackend> mem_backend_; |
59 | 59 |
60 void AddRedirectChain(const char* sequence[], int page_id) { | 60 void AddRedirectChain(const wchar_t* sequence[], int page_id) { |
61 HistoryService::RedirectList redirects; | 61 HistoryService::RedirectList redirects; |
62 for (int i = 0; sequence[i] != NULL; ++i) | 62 for (int i = 0; sequence[i] != NULL; ++i) |
63 redirects.push_back(GURL(sequence[i])); | 63 redirects.push_back(GURL(sequence[i])); |
64 | 64 |
65 int int_scope = 1; | 65 int int_scope = 1; |
66 void* scope = 0; | 66 void* scope = 0; |
67 memcpy(&scope, &int_scope, sizeof(int_scope)); | 67 memcpy(&scope, &int_scope, sizeof(int_scope)); |
68 scoped_refptr<history::HistoryAddPageArgs> request( | 68 scoped_refptr<history::HistoryAddPageArgs> request( |
69 new history::HistoryAddPageArgs( | 69 new history::HistoryAddPageArgs( |
70 redirects.back(), Time::Now(), scope, page_id, GURL(), | 70 redirects.back(), Time::Now(), scope, page_id, GURL(), |
71 redirects, PageTransition::LINK)); | 71 redirects, PageTransition::LINK)); |
72 backend_->AddPage(request); | 72 backend_->AddPage(request); |
73 } | 73 } |
74 | 74 |
75 BookmarkModel bookmark_model_; | 75 BookmarkModel bookmark_model_; |
76 | 76 |
77 protected: | 77 protected: |
78 bool loaded_; | 78 bool loaded_; |
79 | 79 |
80 private: | 80 private: |
81 friend class HistoryBackendTestDelegate; | 81 friend HistoryBackendTestDelegate; |
82 | 82 |
83 // testing::Test | 83 // testing::Test |
84 virtual void SetUp() { | 84 virtual void SetUp() { |
85 if (!file_util::CreateNewTempDirectory(L"BackendTest", &test_dir_)) | 85 if (!file_util::CreateNewTempDirectory(L"BackendTest", &test_dir_)) |
86 return; | 86 return; |
87 backend_ = new HistoryBackend(test_dir_, | 87 backend_ = new HistoryBackend(test_dir_, |
88 new HistoryBackendTestDelegate(this), | 88 new HistoryBackendTestDelegate(this), |
89 &bookmark_model_); | 89 &bookmark_model_); |
90 backend_->Init(); | 90 backend_->Init(); |
91 } | 91 } |
92 virtual void TearDown() { | 92 virtual void TearDown() { |
93 backend_->Closing(); | 93 backend_->Closing(); |
94 backend_ = NULL; | 94 backend_ = NULL; |
95 mem_backend_.reset(); | 95 mem_backend_.reset(); |
96 file_util::Delete(test_dir_, true); | 96 file_util::Delete(test_dir_, true); |
97 } | 97 } |
98 | 98 |
99 void SetInMemoryBackend(InMemoryHistoryBackend* backend) { | 99 void SetInMemoryBackend(InMemoryHistoryBackend* backend) { |
100 mem_backend_.reset(backend); | 100 mem_backend_.reset(backend); |
101 } | 101 } |
102 | 102 |
103 void BroadcastNotifications(NotificationType type, | 103 void BroadcastNotifications(NotificationType type, |
104 HistoryDetails* details) { | 104 HistoryDetails* details) { |
105 // Send the notifications directly to the in-memory database. | 105 // Send the notifications directly to the in-memory database. |
106 Details<HistoryDetails> det(details); | 106 Details<HistoryDetails> det(details); |
107 mem_backend_->Observe(type, Source<HistoryBackendTest>(NULL), det); | 107 mem_backend_->Observe(type, Source<HistoryTest>(NULL), det); |
108 | 108 |
109 // The backend passes ownership of the details pointer to us. | 109 // The backend passes ownership of the details pointer to us. |
110 delete details; | 110 delete details; |
111 } | 111 } |
112 | 112 |
113 MessageLoop message_loop_; | 113 MessageLoop message_loop_; |
114 std::wstring test_dir_; | 114 std::wstring test_dir_; |
115 }; | 115 }; |
116 | 116 |
117 void HistoryBackendTestDelegate::SetInMemoryBackend( | 117 void HistoryBackendTestDelegate::SetInMemoryBackend( |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 rows.push_back(row2); // Reversed order for the same reason as favicons. | 171 rows.push_back(row2); // Reversed order for the same reason as favicons. |
172 rows.push_back(row1); | 172 rows.push_back(row1); |
173 backend_->AddPagesWithDetails(rows); | 173 backend_->AddPagesWithDetails(rows); |
174 | 174 |
175 URLID row1_id = backend_->db_->GetRowForURL(row1.url(), NULL); | 175 URLID row1_id = backend_->db_->GetRowForURL(row1.url(), NULL); |
176 URLID row2_id = backend_->db_->GetRowForURL(row2.url(), NULL); | 176 URLID row2_id = backend_->db_->GetRowForURL(row2.url(), NULL); |
177 | 177 |
178 // Get the two visits for the URLs we just added. | 178 // Get the two visits for the URLs we just added. |
179 VisitVector visits; | 179 VisitVector visits; |
180 backend_->db_->GetVisitsForURL(row1_id, &visits); | 180 backend_->db_->GetVisitsForURL(row1_id, &visits); |
181 ASSERT_EQ(1U, visits.size()); | 181 ASSERT_EQ(1, visits.size()); |
182 VisitID visit1_id = visits[0].visit_id; | 182 VisitID visit1_id = visits[0].visit_id; |
183 | 183 |
184 visits.clear(); | 184 visits.clear(); |
185 backend_->db_->GetVisitsForURL(row2_id, &visits); | 185 backend_->db_->GetVisitsForURL(row2_id, &visits); |
186 ASSERT_EQ(1U, visits.size()); | 186 ASSERT_EQ(1, visits.size()); |
187 VisitID visit2_id = visits[0].visit_id; | 187 VisitID visit2_id = visits[0].visit_id; |
188 | 188 |
189 // The in-memory backend should have been set and it should have gotten the | 189 // The in-memory backend should have been set and it should have gotten the |
190 // typed URL. | 190 // typed URL. |
191 ASSERT_TRUE(mem_backend_.get()); | 191 ASSERT_TRUE(mem_backend_.get()); |
192 URLRow outrow1; | 192 URLRow outrow1; |
193 EXPECT_TRUE(mem_backend_->db_->GetRowForURL(row1.url(), NULL)); | 193 EXPECT_TRUE(mem_backend_->db_->GetRowForURL(row1.url(), NULL)); |
194 | 194 |
195 // Add thumbnails for each page. | 195 // Add thumbnails for each page. |
196 ThumbnailScore score(0.25, true, true); | 196 ThumbnailScore score(0.25, true, true); |
(...skipping 30 matching lines...) Expand all Loading... |
227 EXPECT_EQ(0, outrow1.typed_count()); | 227 EXPECT_EQ(0, outrow1.typed_count()); |
228 EXPECT_TRUE(Time() == outrow1.last_visit()); | 228 EXPECT_TRUE(Time() == outrow1.last_visit()); |
229 | 229 |
230 // The second row should be deleted. | 230 // The second row should be deleted. |
231 URLRow outrow2; | 231 URLRow outrow2; |
232 EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &outrow2)); | 232 EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &outrow2)); |
233 | 233 |
234 // All visits should be deleted for both URLs. | 234 // All visits should be deleted for both URLs. |
235 VisitVector all_visits; | 235 VisitVector all_visits; |
236 backend_->db_->GetAllVisitsInRange(Time(), Time(), 0, &all_visits); | 236 backend_->db_->GetAllVisitsInRange(Time(), Time(), 0, &all_visits); |
237 ASSERT_EQ(0U, all_visits.size()); | 237 ASSERT_EQ(0, all_visits.size()); |
238 | 238 |
239 // All thumbnails should be deleted. | 239 // All thumbnails should be deleted. |
240 std::vector<unsigned char> out_data; | 240 std::vector<unsigned char> out_data; |
241 EXPECT_FALSE(backend_->thumbnail_db_->GetPageThumbnail(outrow1.id(), | 241 EXPECT_FALSE(backend_->thumbnail_db_->GetPageThumbnail(outrow1.id(), |
242 &out_data)); | 242 &out_data)); |
243 EXPECT_FALSE(backend_->thumbnail_db_->GetPageThumbnail(row2_id, &out_data)); | 243 EXPECT_FALSE(backend_->thumbnail_db_->GetPageThumbnail(row2_id, &out_data)); |
244 | 244 |
245 // We should have a favicon for the first URL only. We look them up by favicon | 245 // We should have a favicon for the first URL only. We look them up by favicon |
246 // URL since the IDs may hav changed. | 246 // URL since the IDs may hav changed. |
247 FavIconID out_favicon1 = backend_->thumbnail_db_-> | 247 FavIconID out_favicon1 = backend_->thumbnail_db_-> |
248 GetFavIconIDForFavIconURL(favicon_url1); | 248 GetFavIconIDForFavIconURL(favicon_url1); |
249 EXPECT_TRUE(out_favicon1); | 249 EXPECT_TRUE(out_favicon1); |
250 FavIconID out_favicon2 = backend_->thumbnail_db_-> | 250 FavIconID out_favicon2 = backend_->thumbnail_db_-> |
251 GetFavIconIDForFavIconURL(favicon_url2); | 251 GetFavIconIDForFavIconURL(favicon_url2); |
252 EXPECT_FALSE(out_favicon2) << "Favicon not deleted"; | 252 EXPECT_FALSE(out_favicon2) << "Favicon not deleted"; |
253 | 253 |
254 // The remaining URL should still reference the same favicon, even if its | 254 // The remaining URL should still reference the same favicon, even if its |
255 // ID has changed. | 255 // ID has changed. |
256 EXPECT_EQ(out_favicon1, outrow1.favicon_id()); | 256 EXPECT_EQ(out_favicon1, outrow1.favicon_id()); |
257 | 257 |
258 // The first URL should still be bookmarked. | 258 // The first URL should still be bookmarked. |
259 EXPECT_TRUE(bookmark_model_.IsBookmarked(row1.url())); | 259 EXPECT_TRUE(bookmark_model_.IsBookmarked(row1.url())); |
260 | 260 |
261 // The full text database should have no data. | 261 // The full text database should have no data. |
262 std::vector<TextDatabase::Match> text_matches; | 262 std::vector<TextDatabase::Match> text_matches; |
263 Time first_time_searched; | 263 Time first_time_searched; |
264 backend_->text_database_->GetTextMatches(L"Body", QueryOptions(), | 264 backend_->text_database_->GetTextMatches(L"Body", QueryOptions(), |
265 &text_matches, | 265 &text_matches, |
266 &first_time_searched); | 266 &first_time_searched); |
267 EXPECT_EQ(0U, text_matches.size()); | 267 EXPECT_EQ(0, text_matches.size()); |
268 } | 268 } |
269 | 269 |
270 TEST_F(HistoryBackendTest, URLsNoLongerBookmarked) { | 270 TEST_F(HistoryBackendTest, URLsNoLongerBookmarked) { |
271 GURL favicon_url1("http://www.google.com/favicon.ico"); | 271 GURL favicon_url1("http://www.google.com/favicon.ico"); |
272 GURL favicon_url2("http://news.google.com/favicon.ico"); | 272 GURL favicon_url2("http://news.google.com/favicon.ico"); |
273 FavIconID favicon2 = backend_->thumbnail_db_->AddFavIcon(favicon_url2); | 273 FavIconID favicon2 = backend_->thumbnail_db_->AddFavIcon(favicon_url2); |
274 FavIconID favicon1 = backend_->thumbnail_db_->AddFavIcon(favicon_url1); | 274 FavIconID favicon1 = backend_->thumbnail_db_->AddFavIcon(favicon_url1); |
275 | 275 |
276 std::vector<unsigned char> data; | 276 std::vector<unsigned char> data; |
277 data.push_back('1'); | 277 data.push_back('1'); |
(...skipping 30 matching lines...) Expand all Loading... |
308 | 308 |
309 // Delete url 2. Because url 2 is starred this won't delete the URL, only | 309 // Delete url 2. Because url 2 is starred this won't delete the URL, only |
310 // the visits. | 310 // the visits. |
311 backend_->expirer_.DeleteURL(row2.url()); | 311 backend_->expirer_.DeleteURL(row2.url()); |
312 | 312 |
313 // Make sure url 2 is still valid, but has no visits. | 313 // Make sure url 2 is still valid, but has no visits. |
314 URLRow tmp_url_row; | 314 URLRow tmp_url_row; |
315 EXPECT_EQ(row2_id, backend_->db_->GetRowForURL(row2.url(), NULL)); | 315 EXPECT_EQ(row2_id, backend_->db_->GetRowForURL(row2.url(), NULL)); |
316 VisitVector visits; | 316 VisitVector visits; |
317 backend_->db_->GetVisitsForURL(row2_id, &visits); | 317 backend_->db_->GetVisitsForURL(row2_id, &visits); |
318 EXPECT_EQ(0U, visits.size()); | 318 EXPECT_EQ(0, visits.size()); |
319 // The favicon should still be valid. | 319 // The favicon should still be valid. |
320 EXPECT_EQ(favicon2, | 320 EXPECT_EQ(favicon2, |
321 backend_->thumbnail_db_->GetFavIconIDForFavIconURL(favicon_url2)); | 321 backend_->thumbnail_db_->GetFavIconIDForFavIconURL(favicon_url2)); |
322 | 322 |
323 // Unstar row2. | 323 // Unstar row2. |
324 bookmark_model_.SetURLStarred(row2.url(), std::wstring(), false); | 324 bookmark_model_.SetURLStarred(row2.url(), std::wstring(), false); |
325 // Tell the backend it was unstarred. We have to explicitly do this as | 325 // Tell the backend it was unstarred. We have to explicitly do this as |
326 // BookmarkModel isn't wired up to the backend during testing. | 326 // BookmarkModel isn't wired up to the backend during testing. |
327 std::set<GURL> unstarred_urls; | 327 std::set<GURL> unstarred_urls; |
328 unstarred_urls.insert(row2.url()); | 328 unstarred_urls.insert(row2.url()); |
(...skipping 12 matching lines...) Expand all Loading... |
341 unstarred_urls.clear(); | 341 unstarred_urls.clear(); |
342 unstarred_urls.insert(row1.url()); | 342 unstarred_urls.insert(row1.url()); |
343 backend_->URLsNoLongerBookmarked(unstarred_urls); | 343 backend_->URLsNoLongerBookmarked(unstarred_urls); |
344 | 344 |
345 // The URL should still exist (because there were visits). | 345 // The URL should still exist (because there were visits). |
346 EXPECT_EQ(row1_id, backend_->db_->GetRowForURL(row1.url(), NULL)); | 346 EXPECT_EQ(row1_id, backend_->db_->GetRowForURL(row1.url(), NULL)); |
347 | 347 |
348 // There should still be visits. | 348 // There should still be visits. |
349 visits.clear(); | 349 visits.clear(); |
350 backend_->db_->GetVisitsForURL(row1_id, &visits); | 350 backend_->db_->GetVisitsForURL(row1_id, &visits); |
351 EXPECT_EQ(1U, visits.size()); | 351 EXPECT_EQ(1, visits.size()); |
352 | 352 |
353 // The favicon should still be valid. | 353 // The favicon should still be valid. |
354 EXPECT_EQ(favicon1, | 354 EXPECT_EQ(favicon1, |
355 backend_->thumbnail_db_->GetFavIconIDForFavIconURL(favicon_url1)); | 355 backend_->thumbnail_db_->GetFavIconIDForFavIconURL(favicon_url1)); |
356 } | 356 } |
357 | 357 |
358 TEST_F(HistoryBackendTest, GetPageThumbnailAfterRedirects) { | 358 TEST_F(HistoryBackendTest, GetPageThumbnailAfterRedirects) { |
359 ASSERT_TRUE(backend_.get()); | 359 ASSERT_TRUE(backend_.get()); |
360 | 360 |
361 const char* base_url = "http://mail"; | 361 const wchar_t* base_url = L"http://mail"; |
362 const char* thumbnail_url = "http://mail.google.com"; | 362 const wchar_t* thumbnail_url = L"http://mail.google.com"; |
363 const char* first_chain[] = { | 363 const wchar_t* first_chain[] = { |
364 base_url, | 364 base_url, |
365 thumbnail_url, | 365 thumbnail_url, |
366 NULL | 366 NULL |
367 }; | 367 }; |
368 AddRedirectChain(first_chain, 0); | 368 AddRedirectChain(first_chain, 0); |
369 | 369 |
370 // Add a thumbnail for the end of that redirect chain. | 370 // Add a thumbnail for the end of that redirect chain. |
371 scoped_ptr<SkBitmap> thumbnail( | 371 scoped_ptr<SkBitmap> thumbnail( |
372 JPEGCodec::Decode(kGoogleThumbnail, sizeof(kGoogleThumbnail))); | 372 JPEGCodec::Decode(kGoogleThumbnail, sizeof(kGoogleThumbnail))); |
373 backend_->SetPageThumbnail(GURL(thumbnail_url), *thumbnail, | 373 backend_->SetPageThumbnail(GURL(thumbnail_url), *thumbnail, |
374 ThumbnailScore(0.25, true, true)); | 374 ThumbnailScore(0.25, true, true)); |
375 | 375 |
376 // Write a second URL chain so that if you were to simply check what | 376 // Write a second URL chain so that if you were to simply check what |
377 // "http://mail" redirects to, you wouldn't see the URL that has | 377 // "http://mail" redirects to, you wouldn't see the URL that has |
378 // contains the thumbnail. | 378 // contains the thumbnail. |
379 const char* second_chain[] = { | 379 const wchar_t* second_chain[] = { |
380 base_url, | 380 base_url, |
381 "http://mail.google.com/somewhere/else", | 381 L"http://mail.google.com/somewhere/else", |
382 NULL | 382 NULL |
383 }; | 383 }; |
384 AddRedirectChain(second_chain, 1); | 384 AddRedirectChain(second_chain, 1); |
385 | 385 |
386 // Now try to get the thumbnail for the base url. It shouldn't be | 386 // Now try to get the thumbnail for the base url. It shouldn't be |
387 // distracted by the second chain and should return the thumbnail | 387 // distracted by the second chain and should return the thumbnail |
388 // attached to thumbnail_url_. | 388 // attached to thumbnail_url_. |
389 scoped_refptr<RefCountedBytes> data; | 389 scoped_refptr<RefCountedBytes> data; |
390 backend_->GetPageThumbnailDirectly(GURL(base_url), &data); | 390 backend_->GetPageThumbnailDirectly(GURL(base_url), &data); |
391 | 391 |
392 EXPECT_TRUE(data.get()); | 392 EXPECT_TRUE(data.get()); |
393 } | 393 } |
394 | 394 |
395 } // namespace history | 395 } // namespace history |
OLD | NEW |