OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // History unit tests come in two flavors: | 5 // History unit tests come in two flavors: |
6 // | 6 // |
7 // 1. The more complicated style is that the unit test creates a full history | 7 // 1. The more complicated style is that the unit test creates a full history |
8 // service. This spawns a background thread for the history backend, and | 8 // service. This spawns a background thread for the history backend, and |
9 // all communication is asynchronous. This is useful for testing more | 9 // all communication is asynchronous. This is useful for testing more |
10 // complicated things or end-to-end behavior. | 10 // complicated things or end-to-end behavior. |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "base/callback.h" | 28 #include "base/callback.h" |
29 #include "base/command_line.h" | 29 #include "base/command_line.h" |
30 #include "base/file_path.h" | 30 #include "base/file_path.h" |
31 #include "base/file_util.h" | 31 #include "base/file_util.h" |
32 #include "base/memory/scoped_vector.h" | 32 #include "base/memory/scoped_vector.h" |
33 #include "base/message_loop.h" | 33 #include "base/message_loop.h" |
34 #include "base/path_service.h" | 34 #include "base/path_service.h" |
35 #include "base/scoped_temp_dir.h" | 35 #include "base/scoped_temp_dir.h" |
36 #include "base/string_util.h" | 36 #include "base/string_util.h" |
37 #include "base/utf_string_conversions.h" | 37 #include "base/utf_string_conversions.h" |
| 38 #include "chrome/browser/history/download_persistent_store_info.h" |
38 #include "chrome/browser/history/history.h" | 39 #include "chrome/browser/history/history.h" |
39 #include "chrome/browser/history/history_backend.h" | 40 #include "chrome/browser/history/history_backend.h" |
40 #include "chrome/browser/history/history_database.h" | 41 #include "chrome/browser/history/history_database.h" |
41 #include "chrome/browser/history/history_notifications.h" | 42 #include "chrome/browser/history/history_notifications.h" |
42 #include "chrome/browser/history/in_memory_database.h" | 43 #include "chrome/browser/history/in_memory_database.h" |
43 #include "chrome/browser/history/in_memory_history_backend.h" | 44 #include "chrome/browser/history/in_memory_history_backend.h" |
44 #include "chrome/browser/history/page_usage_data.h" | 45 #include "chrome/browser/history/page_usage_data.h" |
45 #include "chrome/common/chrome_constants.h" | 46 #include "chrome/common/chrome_constants.h" |
46 #include "chrome/common/chrome_paths.h" | 47 #include "chrome/common/chrome_paths.h" |
47 #include "chrome/common/thumbnail_score.h" | 48 #include "chrome/common/thumbnail_score.h" |
48 #include "chrome/tools/profiles/thumbnail-inl.h" | 49 #include "chrome/tools/profiles/thumbnail-inl.h" |
49 #include "content/public/browser/download_item.h" | 50 #include "content/public/browser/download_item.h" |
50 #include "content/public/browser/download_persistent_store_info.h" | |
51 #include "content/public/browser/notification_details.h" | 51 #include "content/public/browser/notification_details.h" |
52 #include "content/public/browser/notification_source.h" | 52 #include "content/public/browser/notification_source.h" |
53 #include "sql/connection.h" | 53 #include "sql/connection.h" |
54 #include "sql/statement.h" | 54 #include "sql/statement.h" |
55 #include "testing/gtest/include/gtest/gtest.h" | 55 #include "testing/gtest/include/gtest/gtest.h" |
56 #include "third_party/skia/include/core/SkBitmap.h" | 56 #include "third_party/skia/include/core/SkBitmap.h" |
57 #include "ui/gfx/codec/jpeg_codec.h" | 57 #include "ui/gfx/codec/jpeg_codec.h" |
58 | 58 |
59 using base::Time; | 59 using base::Time; |
60 using base::TimeDelta; | 60 using base::TimeDelta; |
61 using content::DownloadItem; | 61 using content::DownloadItem; |
62 using content::DownloadPersistentStoreInfo; | |
63 | 62 |
64 namespace history { | 63 namespace history { |
65 class HistoryTest; | 64 class HistoryTest; |
66 } | 65 } |
67 | 66 |
68 namespace history { | 67 namespace history { |
69 | 68 |
70 namespace { | 69 namespace { |
71 | 70 |
72 // The tracker uses RenderProcessHost pointers for scoping but never | 71 // The tracker uses RenderProcessHost pointers for scoping but never |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 // Currently, just send the notifications directly to the in-memory database. | 303 // Currently, just send the notifications directly to the in-memory database. |
305 // We may want do do something more fancy in the future. | 304 // We may want do do something more fancy in the future. |
306 content::Details<HistoryDetails> det(details); | 305 content::Details<HistoryDetails> det(details); |
307 history_test_->in_mem_backend_->Observe(type, | 306 history_test_->in_mem_backend_->Observe(type, |
308 content::Source<HistoryTest>(NULL), det); | 307 content::Source<HistoryTest>(NULL), det); |
309 | 308 |
310 // The backend passes ownership of the details pointer to us. | 309 // The backend passes ownership of the details pointer to us. |
311 delete details; | 310 delete details; |
312 } | 311 } |
313 | 312 |
314 TEST_F(HistoryTest, ClearBrowsingData_Downloads) { | 313 TEST_F(HistoryTest, TimeRemoveDownloads) { |
315 CreateBackendAndDatabase(); | 314 CreateBackendAndDatabase(); |
316 | 315 |
317 Time now = Time::Now(); | |
318 TimeDelta one_day = TimeDelta::FromDays(1); | |
319 Time month_ago = now - TimeDelta::FromDays(30); | |
320 | |
321 // Initially there should be nothing in the downloads database. | 316 // Initially there should be nothing in the downloads database. |
322 std::vector<DownloadPersistentStoreInfo> downloads; | 317 std::vector<DownloadPersistentStoreInfo> downloads; |
323 db_->QueryDownloads(&downloads); | 318 db_->QueryDownloads(&downloads); |
324 EXPECT_EQ(0U, downloads.size()); | 319 EXPECT_EQ(0U, downloads.size()); |
325 | 320 |
326 // Keep track of these as we need to update them later during the test. | 321 std::set<DownloadID> handles; |
327 DownloadID in_progress; | 322 base::Time start_adding = base::Time::Now(); |
| 323 static const int kNumDownloads = 100000; |
| 324 for (int i = 0; i < kNumDownloads; ++i) { |
| 325 handles.insert(AddDownload(DownloadItem::COMPLETE, |
| 326 start_adding - base::TimeDelta::FromSeconds(i))); |
| 327 } |
| 328 LOG(INFO) << "occam time to add " << kNumDownloads << " records: " |
| 329 << (base::Time::Now() - start_adding).InMillisecondsF() << " ms"; |
328 | 330 |
329 // Create one with a 0 time. | 331 db_->QueryDownloads(&downloads); |
330 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, Time())); | 332 EXPECT_EQ(static_cast<size_t>(kNumDownloads), downloads.size()); |
331 // Create one for now and +/- 1 day. | |
332 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now - one_day)); | |
333 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now)); | |
334 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now + one_day)); | |
335 // Try the other four states. | |
336 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, month_ago)); | |
337 EXPECT_NE(0, in_progress = AddDownload(DownloadItem::IN_PROGRESS, month_ago)); | |
338 EXPECT_NE(0, AddDownload(DownloadItem::CANCELLED, month_ago)); | |
339 EXPECT_NE(0, AddDownload(DownloadItem::INTERRUPTED, month_ago)); | |
340 | 333 |
341 // Test to see if inserts worked. | 334 base::Time start_removing = base::Time::Now(); |
| 335 db_->RemoveDownloads(handles); |
| 336 LOG(INFO) << "occam time to remove " << kNumDownloads << " records: " |
| 337 << (base::Time::Now() - start_removing).InMillisecondsF() << " ms"; |
| 338 |
342 db_->QueryDownloads(&downloads); | 339 db_->QueryDownloads(&downloads); |
343 EXPECT_EQ(8U, downloads.size()); | 340 EXPECT_EQ(0U, downloads.size()); |
| 341 } |
344 | 342 |
345 // Try removing from current timestamp. This should delete the one in the | 343 TEST_F(HistoryTest, ClearBrowsingData_Downloads) { |
346 // future and one very recent one. | 344 CreateBackendAndDatabase(); |
347 db_->RemoveDownloadsBetween(now, Time()); | |
348 db_->QueryDownloads(&downloads); | |
349 EXPECT_EQ(6U, downloads.size()); | |
350 | 345 |
351 // Try removing from two months ago. This should not delete items that are | 346 // Initially there should be nothing in the downloads database. |
352 // 'in progress' or in 'removing' state. | 347 std::vector<DownloadPersistentStoreInfo> downloads; |
353 db_->RemoveDownloadsBetween(now - TimeDelta::FromDays(60), Time()); | |
354 db_->QueryDownloads(&downloads); | |
355 EXPECT_EQ(2U, downloads.size()); | |
356 | |
357 // Download manager converts to TimeT, which is lossy, so we do the same | |
358 // for comparison. | |
359 Time month_ago_lossy = Time::FromTimeT(month_ago.ToTimeT()); | |
360 | |
361 // Make sure the right values remain. | |
362 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0].state); | |
363 EXPECT_EQ(0, downloads[0].start_time.ToInternalValue()); | |
364 EXPECT_EQ(DownloadItem::IN_PROGRESS, downloads[1].state); | |
365 EXPECT_EQ(month_ago_lossy.ToInternalValue(), | |
366 downloads[1].start_time.ToInternalValue()); | |
367 | |
368 // Change state so we can delete the downloads. | |
369 DownloadPersistentStoreInfo data; | |
370 data.received_bytes = 512; | |
371 data.state = DownloadItem::COMPLETE; | |
372 data.end_time = base::Time::Now(); | |
373 data.opened = false; | |
374 data.db_handle = in_progress; | |
375 EXPECT_TRUE(db_->UpdateDownload(data)); | |
376 data.state = DownloadItem::CANCELLED; | |
377 EXPECT_TRUE(db_->UpdateDownload(data)); | |
378 | |
379 // Try removing from Time=0. This should delete all. | |
380 db_->RemoveDownloadsBetween(Time(), Time()); | |
381 db_->QueryDownloads(&downloads); | 348 db_->QueryDownloads(&downloads); |
382 EXPECT_EQ(0U, downloads.size()); | 349 EXPECT_EQ(0U, downloads.size()); |
383 | 350 |
384 // Check removal of downloads stuck in IN_PROGRESS state. | 351 // Add a download, test that it was added, remove it, test that it was |
385 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, month_ago)); | 352 // removed. |
386 EXPECT_NE(0, AddDownload(DownloadItem::IN_PROGRESS, month_ago)); | 353 DownloadID handle; |
387 db_->QueryDownloads(&downloads); | 354 EXPECT_NE(0, handle = AddDownload(DownloadItem::COMPLETE, Time())); |
388 EXPECT_EQ(2U, downloads.size()); | |
389 db_->RemoveDownloadsBetween(Time(), Time()); | |
390 db_->QueryDownloads(&downloads); | |
391 // IN_PROGRESS download should remain. It it indicated as "Canceled" | |
392 EXPECT_EQ(1U, downloads.size()); | |
393 db_->CleanUpInProgressEntries(); | |
394 db_->QueryDownloads(&downloads); | 355 db_->QueryDownloads(&downloads); |
395 EXPECT_EQ(1U, downloads.size()); | 356 EXPECT_EQ(1U, downloads.size()); |
396 db_->RemoveDownloadsBetween(Time(), Time()); | 357 std::set<DownloadID> remove_set; |
| 358 remove_set.insert(handle); |
| 359 db_->RemoveDownloads(remove_set); |
397 db_->QueryDownloads(&downloads); | 360 db_->QueryDownloads(&downloads); |
398 EXPECT_EQ(0U, downloads.size()); | 361 EXPECT_EQ(0U, downloads.size()); |
399 } | 362 } |
400 | 363 |
401 TEST_F(HistoryTest, MigrateDownloadsState) { | 364 TEST_F(HistoryTest, MigrateDownloadsState) { |
402 // Create the db and close it so that we can reopen it directly. | 365 // Create the db and close it so that we can reopen it directly. |
403 CreateBackendAndDatabase(); | 366 CreateBackendAndDatabase(); |
404 DeleteBackend(); | 367 DeleteBackend(); |
405 { | 368 { |
406 // Re-open the db for manual manipulation. | 369 // Re-open the db for manual manipulation. |
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1023 history_service_ = history; | 986 history_service_ = history; |
1024 history->ScheduleDBTask(task.get(), &request_consumer); | 987 history->ScheduleDBTask(task.get(), &request_consumer); |
1025 request_consumer.CancelAllRequests(); | 988 request_consumer.CancelAllRequests(); |
1026 CleanupHistoryService(); | 989 CleanupHistoryService(); |
1027 // WARNING: history has now been deleted. | 990 // WARNING: history has now been deleted. |
1028 history = NULL; | 991 history = NULL; |
1029 ASSERT_FALSE(task->done_invoked); | 992 ASSERT_FALSE(task->done_invoked); |
1030 } | 993 } |
1031 | 994 |
1032 } // namespace history | 995 } // namespace history |
OLD | NEW |