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

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: @r168573 Created 8 years, 1 month 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/files/scoped_temp_dir.h" 32 #include "base/files/scoped_temp_dir.h"
33 #include "base/memory/scoped_vector.h" 33 #include "base/memory/scoped_vector.h"
34 #include "base/message_loop.h" 34 #include "base/message_loop.h"
35 #include "base/path_service.h" 35 #include "base/path_service.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_row.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 HistoryBackendDBTest; 65 class HistoryBackendDBTest;
67 66
68 // Delegate class for when we create a backend without a HistoryService. 67 // Delegate class for when we create a backend without a HistoryService.
69 // 68 //
70 // This must be outside the anonymous namespace for the friend statement in 69 // This must be outside the anonymous namespace for the friend statement in
71 // HistoryBackendDBTest to work. 70 // HistoryBackendDBTest to work.
72 class BackendDelegate : public HistoryBackend::Delegate { 71 class BackendDelegate : public HistoryBackend::Delegate {
73 public: 72 public:
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 virtual void TearDown() { 129 virtual void TearDown() {
131 DeleteBackend(); 130 DeleteBackend();
132 131
133 // Make sure we don't have any event pending that could disrupt the next 132 // Make sure we don't have any event pending that could disrupt the next
134 // test. 133 // test.
135 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 134 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
136 MessageLoop::current()->Run(); 135 MessageLoop::current()->Run();
137 } 136 }
138 137
139 int64 AddDownload(DownloadItem::DownloadState state, const Time& time) { 138 int64 AddDownload(DownloadItem::DownloadState state, const Time& time) {
140 DownloadPersistentStoreInfo download( 139 DownloadRow download(
141 FilePath(FILE_PATH_LITERAL("foo-path")), 140 FilePath(FILE_PATH_LITERAL("foo-path")),
142 GURL("foo-url"), 141 GURL("foo-url"),
143 GURL(""), 142 GURL(""),
144 time, 143 time,
145 time, 144 time,
146 0, 145 0,
147 512, 146 512,
148 state, 147 state,
149 0, 148 0,
150 0); 149 0);
(...skipping 30 matching lines...) Expand all
181 180
182 // The backend passes ownership of the details pointer to us. 181 // The backend passes ownership of the details pointer to us.
183 delete details; 182 delete details;
184 } 183 }
185 184
186 namespace { 185 namespace {
187 186
188 TEST_F(HistoryBackendDBTest, ClearBrowsingData_Downloads) { 187 TEST_F(HistoryBackendDBTest, ClearBrowsingData_Downloads) {
189 CreateBackendAndDatabase(); 188 CreateBackendAndDatabase();
190 189
191 Time now = Time::Now();
192 TimeDelta one_day = TimeDelta::FromDays(1);
193 Time month_ago = now - TimeDelta::FromDays(30);
194
195 // Initially there should be nothing in the downloads database. 190 // Initially there should be nothing in the downloads database.
196 std::vector<DownloadPersistentStoreInfo> downloads; 191 std::vector<DownloadRow> downloads;
197 db_->QueryDownloads(&downloads); 192 db_->QueryDownloads(&downloads);
198 EXPECT_EQ(0U, downloads.size()); 193 EXPECT_EQ(0U, downloads.size());
199 194
200 // Keep track of these as we need to update them later during the test. 195 // Add a download, test that it was added, remove it, test that it was
201 DownloadID in_progress; 196 // removed.
202 197 DownloadID handle;
203 // Create one with a 0 time. 198 EXPECT_NE(0, handle = AddDownload(DownloadItem::COMPLETE, Time()));
204 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, Time()));
205 // Create one for now and +/- 1 day.
206 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now - one_day));
207 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now));
208 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now + one_day));
209 // Try the other four states.
210 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, month_ago));
211 EXPECT_NE(0, in_progress = AddDownload(DownloadItem::IN_PROGRESS, month_ago));
212 EXPECT_NE(0, AddDownload(DownloadItem::CANCELLED, month_ago));
213 EXPECT_NE(0, AddDownload(DownloadItem::INTERRUPTED, month_ago));
214
215 // Test to see if inserts worked.
216 db_->QueryDownloads(&downloads);
217 EXPECT_EQ(8U, downloads.size());
218
219 // Try removing from current timestamp. This should delete the one in the
220 // future and one very recent one.
221 db_->RemoveDownloadsBetween(now, Time());
222 db_->QueryDownloads(&downloads);
223 EXPECT_EQ(6U, downloads.size());
224
225 // Try removing from two months ago. This should not delete items that are
226 // 'in progress' or in 'removing' state.
227 db_->RemoveDownloadsBetween(now - TimeDelta::FromDays(60), Time());
228 db_->QueryDownloads(&downloads);
229 EXPECT_EQ(2U, downloads.size());
230
231 // Download manager converts to TimeT, which is lossy, so we do the same
232 // for comparison.
233 Time month_ago_lossy = Time::FromTimeT(month_ago.ToTimeT());
234
235 // Make sure the right values remain.
236 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0].state);
237 EXPECT_EQ(0, downloads[0].start_time.ToInternalValue());
238 EXPECT_EQ(DownloadItem::IN_PROGRESS, downloads[1].state);
239 EXPECT_EQ(month_ago_lossy.ToInternalValue(),
240 downloads[1].start_time.ToInternalValue());
241
242 // Change state so we can delete the downloads.
243 DownloadPersistentStoreInfo data;
244 data.received_bytes = 512;
245 data.state = DownloadItem::COMPLETE;
246 data.end_time = base::Time::Now();
247 data.opened = false;
248 data.db_handle = in_progress;
249 EXPECT_TRUE(db_->UpdateDownload(data));
250 data.state = DownloadItem::CANCELLED;
251 EXPECT_TRUE(db_->UpdateDownload(data));
252
253 // Try removing from Time=0. This should delete all.
254 db_->RemoveDownloadsBetween(Time(), Time());
255 db_->QueryDownloads(&downloads);
256 EXPECT_EQ(0U, downloads.size());
257
258 // Check removal of downloads stuck in IN_PROGRESS state.
259 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, month_ago));
260 EXPECT_NE(0, AddDownload(DownloadItem::IN_PROGRESS, month_ago));
261 db_->QueryDownloads(&downloads);
262 EXPECT_EQ(2U, downloads.size());
263 db_->RemoveDownloadsBetween(Time(), Time());
264 db_->QueryDownloads(&downloads);
265 // IN_PROGRESS download should remain. It it indicated as "Canceled"
266 EXPECT_EQ(1U, downloads.size());
267 db_->CleanUpInProgressEntries();
268 db_->QueryDownloads(&downloads); 199 db_->QueryDownloads(&downloads);
269 EXPECT_EQ(1U, downloads.size()); 200 EXPECT_EQ(1U, downloads.size());
270 db_->RemoveDownloadsBetween(Time(), Time()); 201 db_->RemoveDownload(handle);
271 db_->QueryDownloads(&downloads); 202 db_->QueryDownloads(&downloads);
272 EXPECT_EQ(0U, downloads.size()); 203 EXPECT_EQ(0U, downloads.size());
273 } 204 }
274 205
275 TEST_F(HistoryBackendDBTest, MigrateDownloadsState) { 206 TEST_F(HistoryBackendDBTest, MigrateDownloadsState) {
276 // Create the db and close it so that we can reopen it directly. 207 // Create the db and close it so that we can reopen it directly.
277 CreateBackendAndDatabase(); 208 CreateBackendAndDatabase();
278 DeleteBackend(); 209 DeleteBackend();
279 { 210 {
280 // Re-open the db for manual manipulation. 211 // Re-open the db for manual manipulation.
(...skipping 751 matching lines...) Expand 10 before | Expand all | Expand 10 after
1032 request_consumer.CancelAllRequests(); 963 request_consumer.CancelAllRequests();
1033 CleanupHistoryService(); 964 CleanupHistoryService();
1034 // WARNING: history has now been deleted. 965 // WARNING: history has now been deleted.
1035 history_service_.reset(); 966 history_service_.reset();
1036 ASSERT_FALSE(task->done_invoked); 967 ASSERT_FALSE(task->done_invoked);
1037 } 968 }
1038 969
1039 } // namespace 970 } // namespace
1040 971
1041 } // namespace history 972 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698