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 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 history_test_->in_mem_backend_->Observe(type, | 306 history_test_->in_mem_backend_->Observe(type, |
307 content::Source<HistoryTest>(NULL), det); | 307 content::Source<HistoryTest>(NULL), det); |
308 | 308 |
309 // The backend passes ownership of the details pointer to us. | 309 // The backend passes ownership of the details pointer to us. |
310 delete details; | 310 delete details; |
311 } | 311 } |
312 | 312 |
313 TEST_F(HistoryTest, ClearBrowsingData_Downloads) { | 313 TEST_F(HistoryTest, ClearBrowsingData_Downloads) { |
314 CreateBackendAndDatabase(); | 314 CreateBackendAndDatabase(); |
315 | 315 |
316 Time now = Time::Now(); | |
317 TimeDelta one_day = TimeDelta::FromDays(1); | |
318 Time month_ago = now - TimeDelta::FromDays(30); | |
319 | |
320 // Initially there should be nothing in the downloads database. | 316 // Initially there should be nothing in the downloads database. |
321 std::vector<DownloadPersistentStoreInfo> downloads; | 317 std::vector<DownloadPersistentStoreInfo> downloads; |
322 db_->QueryDownloads(&downloads); | 318 db_->QueryDownloads(&downloads); |
323 EXPECT_EQ(0U, downloads.size()); | 319 EXPECT_EQ(0U, downloads.size()); |
324 | 320 |
325 // 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 |
326 DownloadID in_progress, removing; | 322 // removed. |
327 | 323 DownloadID handle; |
328 // Create one with a 0 time. | 324 EXPECT_NE(0, handle = AddDownload(DownloadItem::COMPLETE, Time())); |
329 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, Time())); | |
330 // Create one for now and +/- 1 day. | |
331 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now - one_day)); | |
332 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now)); | |
333 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now + one_day)); | |
334 // Try the other four states. | |
335 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, month_ago)); | |
336 EXPECT_NE(0, in_progress = AddDownload(DownloadItem::IN_PROGRESS, month_ago)); | |
337 EXPECT_NE(0, AddDownload(DownloadItem::CANCELLED, month_ago)); | |
338 EXPECT_NE(0, AddDownload(DownloadItem::INTERRUPTED, month_ago)); | |
339 EXPECT_NE(0, removing = AddDownload(DownloadItem::REMOVING, month_ago)); | |
340 | |
341 // Test to see if inserts worked. | |
342 db_->QueryDownloads(&downloads); | |
343 EXPECT_EQ(9U, 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(7U, 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(3U, 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 EXPECT_EQ(DownloadItem::REMOVING, downloads[2].state); | |
368 EXPECT_EQ(month_ago_lossy.ToInternalValue(), | |
369 downloads[2].start_time.ToInternalValue()); | |
370 | |
371 // Change state so we can delete the downloads. | |
372 DownloadPersistentStoreInfo data; | |
373 data.received_bytes = 512; | |
374 data.state = DownloadItem::COMPLETE; | |
375 data.end_time = base::Time::Now(); | |
376 data.opened = false; | |
377 data.db_handle = in_progress; | |
378 EXPECT_TRUE(db_->UpdateDownload(data)); | |
379 data.state = DownloadItem::CANCELLED; | |
380 data.db_handle = removing; | |
381 EXPECT_TRUE(db_->UpdateDownload(data)); | |
382 | |
383 // Try removing from Time=0. This should delete all. | |
384 db_->RemoveDownloadsBetween(Time(), Time()); | |
385 db_->QueryDownloads(&downloads); | |
386 EXPECT_EQ(0U, downloads.size()); | |
387 | |
388 // Check removal of downloads stuck in IN_PROGRESS state. | |
389 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, month_ago)); | |
390 EXPECT_NE(0, AddDownload(DownloadItem::IN_PROGRESS, month_ago)); | |
391 db_->QueryDownloads(&downloads); | |
392 EXPECT_EQ(2U, downloads.size()); | |
393 db_->RemoveDownloadsBetween(Time(), Time()); | |
394 db_->QueryDownloads(&downloads); | |
395 // IN_PROGRESS download should remain. It it indicated as "Canceled" | |
396 EXPECT_EQ(1U, downloads.size()); | |
397 db_->CleanUpInProgressEntries(); | |
398 db_->QueryDownloads(&downloads); | 325 db_->QueryDownloads(&downloads); |
399 EXPECT_EQ(1U, downloads.size()); | 326 EXPECT_EQ(1U, downloads.size()); |
400 db_->RemoveDownloadsBetween(Time(), Time()); | 327 std::set<DownloadID> remove_set; |
| 328 remove_set.insert(handle); |
| 329 db_->RemoveDownloads(remove_set); |
401 db_->QueryDownloads(&downloads); | 330 db_->QueryDownloads(&downloads); |
402 EXPECT_EQ(0U, downloads.size()); | 331 EXPECT_EQ(0U, downloads.size()); |
403 } | 332 } |
404 | 333 |
405 TEST_F(HistoryTest, AddPage) { | 334 TEST_F(HistoryTest, AddPage) { |
406 scoped_refptr<HistoryService> history(new HistoryService); | 335 scoped_refptr<HistoryService> history(new HistoryService); |
407 history_service_ = history; | 336 history_service_ = history; |
408 ASSERT_TRUE(history->Init(history_dir_, NULL)); | 337 ASSERT_TRUE(history->Init(history_dir_, NULL)); |
409 | 338 |
410 // Add the page once from a child frame. | 339 // Add the page once from a child frame. |
(...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
956 history_service_ = history; | 885 history_service_ = history; |
957 history->ScheduleDBTask(task.get(), &request_consumer); | 886 history->ScheduleDBTask(task.get(), &request_consumer); |
958 request_consumer.CancelAllRequests(); | 887 request_consumer.CancelAllRequests(); |
959 CleanupHistoryService(); | 888 CleanupHistoryService(); |
960 // WARNING: history has now been deleted. | 889 // WARNING: history has now been deleted. |
961 history = NULL; | 890 history = NULL; |
962 ASSERT_FALSE(task->done_invoked); | 891 ASSERT_FALSE(task->done_invoked); |
963 } | 892 } |
964 | 893 |
965 } // namespace history | 894 } // namespace history |
OLD | NEW |