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/download/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 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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, ClearBrowsingData_Downloads) { |
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 // Add a download, test that it was added, remove it, test that it was |
327 DownloadID in_progress; | 322 // removed. |
328 | 323 DownloadID handle; |
329 // Create one with a 0 time. | 324 EXPECT_NE(0, handle = AddDownload(DownloadItem::COMPLETE, Time())); |
330 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, Time())); | |
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 | |
341 // Test to see if inserts worked. | |
342 db_->QueryDownloads(&downloads); | |
343 EXPECT_EQ(8U, downloads.size()); | |
344 | |
345 // Try removing from current timestamp. This should delete the one in the | |
346 // future and one very recent one. | |
347 db_->RemoveDownloadsBetween(now, Time()); | |
348 db_->QueryDownloads(&downloads); | |
349 EXPECT_EQ(6U, downloads.size()); | |
350 | |
351 // Try removing from two months ago. This should not delete items that are | |
352 // 'in progress' or in 'removing' state. | |
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); | |
382 EXPECT_EQ(0U, downloads.size()); | |
383 | |
384 // Check removal of downloads stuck in IN_PROGRESS state. | |
385 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, month_ago)); | |
386 EXPECT_NE(0, AddDownload(DownloadItem::IN_PROGRESS, month_ago)); | |
387 db_->QueryDownloads(&downloads); | |
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); | 325 db_->QueryDownloads(&downloads); |
395 EXPECT_EQ(1U, downloads.size()); | 326 EXPECT_EQ(1U, downloads.size()); |
396 db_->RemoveDownloadsBetween(Time(), Time()); | 327 std::set<DownloadID> remove_set; |
| 328 remove_set.insert(handle); |
| 329 db_->RemoveDownloads(remove_set); |
397 db_->QueryDownloads(&downloads); | 330 db_->QueryDownloads(&downloads); |
398 EXPECT_EQ(0U, downloads.size()); | 331 EXPECT_EQ(0U, downloads.size()); |
399 } | 332 } |
400 | 333 |
401 TEST_F(HistoryTest, MigrateDownloadsState) { | 334 TEST_F(HistoryTest, MigrateDownloadsState) { |
402 // Create the db and close it so that we can reopen it directly. | 335 // Create the db and close it so that we can reopen it directly. |
403 CreateBackendAndDatabase(); | 336 CreateBackendAndDatabase(); |
404 DeleteBackend(); | 337 DeleteBackend(); |
405 { | 338 { |
406 // Re-open the db for manual manipulation. | 339 // Re-open the db for manual manipulation. |
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1023 history_service_ = history; | 956 history_service_ = history; |
1024 history->ScheduleDBTask(task.get(), &request_consumer); | 957 history->ScheduleDBTask(task.get(), &request_consumer); |
1025 request_consumer.CancelAllRequests(); | 958 request_consumer.CancelAllRequests(); |
1026 CleanupHistoryService(); | 959 CleanupHistoryService(); |
1027 // WARNING: history has now been deleted. | 960 // WARNING: history has now been deleted. |
1028 history = NULL; | 961 history = NULL; |
1029 ASSERT_FALSE(task->done_invoked); | 962 ASSERT_FALSE(task->done_invoked); |
1030 } | 963 } |
1031 | 964 |
1032 } // namespace history | 965 } // namespace history |
OLD | NEW |