Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: chrome/browser/history/history_unittest.cc

Issue 10915180: Make DownloadHistory observe manager, items (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r159248 Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 18 matching lines...) Expand all
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/time.h" 37 #include "base/time.h"
38 #include "base/utf_string_conversions.h" 38 #include "base/utf_string_conversions.h"
39 #include "chrome/browser/history/download_persistent_store_info.h"
39 #include "chrome/browser/history/history.h" 40 #include "chrome/browser/history/history.h"
40 #include "chrome/browser/history/history_backend.h" 41 #include "chrome/browser/history/history_backend.h"
41 #include "chrome/browser/history/history_database.h" 42 #include "chrome/browser/history/history_database.h"
42 #include "chrome/browser/history/history_notifications.h" 43 #include "chrome/browser/history/history_notifications.h"
43 #include "chrome/browser/history/in_memory_database.h" 44 #include "chrome/browser/history/in_memory_database.h"
44 #include "chrome/browser/history/in_memory_history_backend.h" 45 #include "chrome/browser/history/in_memory_history_backend.h"
45 #include "chrome/browser/history/page_usage_data.h" 46 #include "chrome/browser/history/page_usage_data.h"
46 #include "chrome/common/chrome_constants.h" 47 #include "chrome/common/chrome_constants.h"
47 #include "chrome/common/chrome_paths.h" 48 #include "chrome/common/chrome_paths.h"
48 #include "chrome/common/thumbnail_score.h" 49 #include "chrome/common/thumbnail_score.h"
49 #include "chrome/tools/profiles/thumbnail-inl.h" 50 #include "chrome/tools/profiles/thumbnail-inl.h"
50 #include "content/public/browser/download_item.h" 51 #include "content/public/browser/download_item.h"
51 #include "content/public/browser/download_persistent_store_info.h"
52 #include "content/public/browser/notification_details.h" 52 #include "content/public/browser/notification_details.h"
53 #include "content/public/browser/notification_source.h" 53 #include "content/public/browser/notification_source.h"
54 #include "sql/connection.h" 54 #include "sql/connection.h"
55 #include "sql/statement.h" 55 #include "sql/statement.h"
56 #include "testing/gtest/include/gtest/gtest.h" 56 #include "testing/gtest/include/gtest/gtest.h"
57 #include "third_party/skia/include/core/SkBitmap.h" 57 #include "third_party/skia/include/core/SkBitmap.h"
58 #include "ui/gfx/codec/jpeg_codec.h" 58 #include "ui/gfx/codec/jpeg_codec.h"
59 59
60 using base::Time; 60 using base::Time;
61 using base::TimeDelta; 61 using base::TimeDelta;
62 using content::DownloadItem; 62 using content::DownloadItem;
63 using content::DownloadPersistentStoreInfo;
64 63
65 namespace history { 64 namespace history {
66 class HistoryTest; 65 class HistoryTest;
67 } 66 }
68 67
69 namespace history { 68 namespace history {
70 69
71 namespace { 70 namespace {
72 71
73 // The tracker uses RenderProcessHost pointers for scoping but never 72 // The tracker uses RenderProcessHost pointers for scoping but never
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 history_test_->in_mem_backend_->Observe(type, 307 history_test_->in_mem_backend_->Observe(type,
309 content::Source<HistoryTest>(NULL), det); 308 content::Source<HistoryTest>(NULL), det);
310 309
311 // The backend passes ownership of the details pointer to us. 310 // The backend passes ownership of the details pointer to us.
312 delete details; 311 delete details;
313 } 312 }
314 313
315 TEST_F(HistoryTest, ClearBrowsingData_Downloads) { 314 TEST_F(HistoryTest, ClearBrowsingData_Downloads) {
316 CreateBackendAndDatabase(); 315 CreateBackendAndDatabase();
317 316
318 Time now = Time::Now();
319 TimeDelta one_day = TimeDelta::FromDays(1);
320 Time month_ago = now - TimeDelta::FromDays(30);
321
322 // Initially there should be nothing in the downloads database. 317 // Initially there should be nothing in the downloads database.
323 std::vector<DownloadPersistentStoreInfo> downloads; 318 std::vector<DownloadPersistentStoreInfo> downloads;
324 db_->QueryDownloads(&downloads); 319 db_->QueryDownloads(&downloads);
325 EXPECT_EQ(0U, downloads.size()); 320 EXPECT_EQ(0U, downloads.size());
326 321
327 // Keep track of these as we need to update them later during the test. 322 // Add a download, test that it was added, remove it, test that it was
328 DownloadID in_progress; 323 // removed.
329 324 DownloadID handle;
330 // Create one with a 0 time. 325 EXPECT_NE(0, handle = AddDownload(DownloadItem::COMPLETE, Time()));
331 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, Time()));
332 // Create one for now and +/- 1 day.
333 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now - one_day));
334 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now));
335 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now + one_day));
336 // Try the other four states.
337 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, month_ago));
338 EXPECT_NE(0, in_progress = AddDownload(DownloadItem::IN_PROGRESS, month_ago));
339 EXPECT_NE(0, AddDownload(DownloadItem::CANCELLED, month_ago));
340 EXPECT_NE(0, AddDownload(DownloadItem::INTERRUPTED, month_ago));
341
342 // Test to see if inserts worked.
343 db_->QueryDownloads(&downloads);
344 EXPECT_EQ(8U, downloads.size());
345
346 // Try removing from current timestamp. This should delete the one in the
347 // future and one very recent one.
348 db_->RemoveDownloadsBetween(now, Time());
349 db_->QueryDownloads(&downloads);
350 EXPECT_EQ(6U, downloads.size());
351
352 // Try removing from two months ago. This should not delete items that are
353 // 'in progress' or in 'removing' state.
354 db_->RemoveDownloadsBetween(now - TimeDelta::FromDays(60), Time());
355 db_->QueryDownloads(&downloads);
356 EXPECT_EQ(2U, downloads.size());
357
358 // Download manager converts to TimeT, which is lossy, so we do the same
359 // for comparison.
360 Time month_ago_lossy = Time::FromTimeT(month_ago.ToTimeT());
361
362 // Make sure the right values remain.
363 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0].state);
364 EXPECT_EQ(0, downloads[0].start_time.ToInternalValue());
365 EXPECT_EQ(DownloadItem::IN_PROGRESS, downloads[1].state);
366 EXPECT_EQ(month_ago_lossy.ToInternalValue(),
367 downloads[1].start_time.ToInternalValue());
368
369 // Change state so we can delete the downloads.
370 DownloadPersistentStoreInfo data;
371 data.received_bytes = 512;
372 data.state = DownloadItem::COMPLETE;
373 data.end_time = base::Time::Now();
374 data.opened = false;
375 data.db_handle = in_progress;
376 EXPECT_TRUE(db_->UpdateDownload(data));
377 data.state = DownloadItem::CANCELLED;
378 EXPECT_TRUE(db_->UpdateDownload(data));
379
380 // Try removing from Time=0. This should delete all.
381 db_->RemoveDownloadsBetween(Time(), Time());
382 db_->QueryDownloads(&downloads);
383 EXPECT_EQ(0U, downloads.size());
384
385 // Check removal of downloads stuck in IN_PROGRESS state.
386 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, month_ago));
387 EXPECT_NE(0, AddDownload(DownloadItem::IN_PROGRESS, month_ago));
388 db_->QueryDownloads(&downloads);
389 EXPECT_EQ(2U, downloads.size());
390 db_->RemoveDownloadsBetween(Time(), Time());
391 db_->QueryDownloads(&downloads);
392 // IN_PROGRESS download should remain. It it indicated as "Canceled"
393 EXPECT_EQ(1U, downloads.size());
394 db_->CleanUpInProgressEntries();
395 db_->QueryDownloads(&downloads); 326 db_->QueryDownloads(&downloads);
396 EXPECT_EQ(1U, downloads.size()); 327 EXPECT_EQ(1U, downloads.size());
397 db_->RemoveDownloadsBetween(Time(), Time()); 328 std::set<DownloadID> remove_set;
329 remove_set.insert(handle);
330 db_->RemoveDownloads(remove_set);
398 db_->QueryDownloads(&downloads); 331 db_->QueryDownloads(&downloads);
399 EXPECT_EQ(0U, downloads.size()); 332 EXPECT_EQ(0U, downloads.size());
400 } 333 }
401 334
402 TEST_F(HistoryTest, MigrateDownloadsState) { 335 TEST_F(HistoryTest, MigrateDownloadsState) {
403 // Create the db and close it so that we can reopen it directly. 336 // Create the db and close it so that we can reopen it directly.
404 CreateBackendAndDatabase(); 337 CreateBackendAndDatabase();
405 DeleteBackend(); 338 DeleteBackend();
406 { 339 {
407 // Re-open the db for manual manipulation. 340 // Re-open the db for manual manipulation.
(...skipping 620 matching lines...) Expand 10 before | Expand all | Expand 10 after
1028 history_service_ = history; 961 history_service_ = history;
1029 history->ScheduleDBTask(task.get(), &request_consumer); 962 history->ScheduleDBTask(task.get(), &request_consumer);
1030 request_consumer.CancelAllRequests(); 963 request_consumer.CancelAllRequests();
1031 CleanupHistoryService(); 964 CleanupHistoryService();
1032 // WARNING: history has now been deleted. 965 // WARNING: history has now been deleted.
1033 history = NULL; 966 history = NULL;
1034 ASSERT_FALSE(task->done_invoked); 967 ASSERT_FALSE(task->done_invoked);
1035 } 968 }
1036 969
1037 } // namespace history 970 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698