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 18 matching lines...) Expand all Loading... |
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 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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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<DownloadPersistentStoreInfo> 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 std::set<DownloadID> remove_set; |
| 202 remove_set.insert(handle); |
| 203 db_->RemoveDownloads(remove_set); |
271 db_->QueryDownloads(&downloads); | 204 db_->QueryDownloads(&downloads); |
272 EXPECT_EQ(0U, downloads.size()); | 205 EXPECT_EQ(0U, downloads.size()); |
273 } | 206 } |
274 | 207 |
275 TEST_F(HistoryBackendDBTest, MigrateDownloadsState) { | 208 TEST_F(HistoryBackendDBTest, MigrateDownloadsState) { |
276 // Create the db and close it so that we can reopen it directly. | 209 // Create the db and close it so that we can reopen it directly. |
277 CreateBackendAndDatabase(); | 210 CreateBackendAndDatabase(); |
278 DeleteBackend(); | 211 DeleteBackend(); |
279 { | 212 { |
280 // Re-open the db for manual manipulation. | 213 // Re-open the db for manual manipulation. |
(...skipping 751 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1032 request_consumer.CancelAllRequests(); | 965 request_consumer.CancelAllRequests(); |
1033 CleanupHistoryService(); | 966 CleanupHistoryService(); |
1034 // WARNING: history has now been deleted. | 967 // WARNING: history has now been deleted. |
1035 history_service_.reset(); | 968 history_service_.reset(); |
1036 ASSERT_FALSE(task->done_invoked); | 969 ASSERT_FALSE(task->done_invoked); |
1037 } | 970 } |
1038 | 971 |
1039 } // namespace | 972 } // namespace |
1040 | 973 |
1041 } // namespace history | 974 } // namespace history |
OLD | NEW |