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 #include "base/file_path.h" | 5 #include "base/file_path.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 : ui_thread_(BrowserThread::UI, &message_loop_), | 100 : ui_thread_(BrowserThread::UI, &message_loop_), |
101 file_thread_(BrowserThread::FILE, &message_loop_) { | 101 file_thread_(BrowserThread::FILE, &message_loop_) { |
102 } | 102 } |
103 | 103 |
104 void DownloadPathReservationTrackerTest::SetUp() { | 104 void DownloadPathReservationTrackerTest::SetUp() { |
105 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir()); | 105 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir()); |
106 set_default_download_path(test_download_dir_.path()); | 106 set_default_download_path(test_download_dir_.path()); |
107 } | 107 } |
108 | 108 |
109 void DownloadPathReservationTrackerTest::TearDown() { | 109 void DownloadPathReservationTrackerTest::TearDown() { |
110 message_loop_.RunAllPending(); | 110 message_loop_.RunUntilIdle(); |
111 } | 111 } |
112 | 112 |
113 FakeDownloadItem* DownloadPathReservationTrackerTest::CreateDownloadItem( | 113 FakeDownloadItem* DownloadPathReservationTrackerTest::CreateDownloadItem( |
114 int32 id) { | 114 int32 id) { |
115 FakeDownloadItem* item = new ::testing::StrictMock<FakeDownloadItem>; | 115 FakeDownloadItem* item = new ::testing::StrictMock<FakeDownloadItem>; |
116 DownloadId download_id(reinterpret_cast<void*>(this), id); | 116 DownloadId download_id(reinterpret_cast<void*>(this), id); |
117 EXPECT_CALL(*item, GetGlobalId()) | 117 EXPECT_CALL(*item, GetGlobalId()) |
118 .WillRepeatedly(Return(download_id)); | 118 .WillRepeatedly(Return(download_id)); |
119 EXPECT_CALL(*item, GetTargetFilePath()) | 119 EXPECT_CALL(*item, GetTargetFilePath()) |
120 .WillRepeatedly(ReturnRefOfCopy(FilePath())); | 120 .WillRepeatedly(ReturnRefOfCopy(FilePath())); |
(...skipping 18 matching lines...) Expand all Loading... |
139 // Weak pointer factory to prevent the callback from running after this | 139 // Weak pointer factory to prevent the callback from running after this |
140 // function has returned. | 140 // function has returned. |
141 base::WeakPtrFactory<DownloadPathReservationTrackerTest> weak_ptr_factory( | 141 base::WeakPtrFactory<DownloadPathReservationTrackerTest> weak_ptr_factory( |
142 this); | 142 this); |
143 bool did_run_callback = false; | 143 bool did_run_callback = false; |
144 DownloadPathReservationTracker::GetReservedPath( | 144 DownloadPathReservationTracker::GetReservedPath( |
145 download_item, target_path, default_download_path(), uniquify_path, | 145 download_item, target_path, default_download_path(), uniquify_path, |
146 base::Bind(&DownloadPathReservationTrackerTest::TestReservedPathCallback, | 146 base::Bind(&DownloadPathReservationTrackerTest::TestReservedPathCallback, |
147 weak_ptr_factory.GetWeakPtr(), return_path, return_verified, | 147 weak_ptr_factory.GetWeakPtr(), return_path, return_verified, |
148 &did_run_callback)); | 148 &did_run_callback)); |
149 message_loop_.RunAllPending(); | 149 message_loop_.RunUntilIdle(); |
150 EXPECT_TRUE(did_run_callback); | 150 EXPECT_TRUE(did_run_callback); |
151 } | 151 } |
152 | 152 |
153 void DownloadPathReservationTrackerTest::TestReservedPathCallback( | 153 void DownloadPathReservationTrackerTest::TestReservedPathCallback( |
154 FilePath* return_path, bool* return_verified, bool* did_run_callback, | 154 FilePath* return_path, bool* return_verified, bool* did_run_callback, |
155 const FilePath& path, bool verified) { | 155 const FilePath& path, bool verified) { |
156 *did_run_callback = true; | 156 *did_run_callback = true; |
157 *return_path = path; | 157 *return_path = path; |
158 *return_verified = verified; | 158 *return_verified = verified; |
159 } | 159 } |
160 | 160 |
161 } // namespace | 161 } // namespace |
162 | 162 |
163 // A basic reservation is acquired and committed. | 163 // A basic reservation is acquired and committed. |
164 TEST_F(DownloadPathReservationTrackerTest, BasicReservation) { | 164 TEST_F(DownloadPathReservationTrackerTest, BasicReservation) { |
165 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); | 165 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); |
166 FilePath path(GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); | 166 FilePath path(GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); |
167 ASSERT_FALSE(IsPathInUse(path)); | 167 ASSERT_FALSE(IsPathInUse(path)); |
168 | 168 |
169 FilePath reserved_path; | 169 FilePath reserved_path; |
170 bool verified = false; | 170 bool verified = false; |
171 CallGetReservedPath(*item, path, false, &reserved_path, &verified); | 171 CallGetReservedPath(*item, path, false, &reserved_path, &verified); |
172 EXPECT_TRUE(IsPathInUse(path)); | 172 EXPECT_TRUE(IsPathInUse(path)); |
173 EXPECT_TRUE(verified); | 173 EXPECT_TRUE(verified); |
174 EXPECT_EQ(path.value(), reserved_path.value()); | 174 EXPECT_EQ(path.value(), reserved_path.value()); |
175 | 175 |
176 // Destroying the item should release the reservation. | 176 // Destroying the item should release the reservation. |
177 item.reset(); | 177 item.reset(); |
178 message_loop_.RunAllPending(); | 178 message_loop_.RunUntilIdle(); |
179 EXPECT_FALSE(IsPathInUse(path)); | 179 EXPECT_FALSE(IsPathInUse(path)); |
180 } | 180 } |
181 | 181 |
182 // A download that is interrupted should lose its reservation. | 182 // A download that is interrupted should lose its reservation. |
183 TEST_F(DownloadPathReservationTrackerTest, InterruptedDownload) { | 183 TEST_F(DownloadPathReservationTrackerTest, InterruptedDownload) { |
184 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); | 184 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); |
185 FilePath path(GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); | 185 FilePath path(GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); |
186 ASSERT_FALSE(IsPathInUse(path)); | 186 ASSERT_FALSE(IsPathInUse(path)); |
187 | 187 |
188 FilePath reserved_path; | 188 FilePath reserved_path; |
189 bool verified = false; | 189 bool verified = false; |
190 CallGetReservedPath(*item, path, false, &reserved_path, &verified); | 190 CallGetReservedPath(*item, path, false, &reserved_path, &verified); |
191 EXPECT_TRUE(IsPathInUse(path)); | 191 EXPECT_TRUE(IsPathInUse(path)); |
192 EXPECT_TRUE(verified); | 192 EXPECT_TRUE(verified); |
193 EXPECT_EQ(path.value(), reserved_path.value()); | 193 EXPECT_EQ(path.value(), reserved_path.value()); |
194 | 194 |
195 // Once the download is interrupted, the path should become available again. | 195 // Once the download is interrupted, the path should become available again. |
196 item->SetState(DownloadItem::INTERRUPTED); | 196 item->SetState(DownloadItem::INTERRUPTED); |
197 message_loop_.RunAllPending(); | 197 message_loop_.RunUntilIdle(); |
198 EXPECT_FALSE(IsPathInUse(path)); | 198 EXPECT_FALSE(IsPathInUse(path)); |
199 } | 199 } |
200 | 200 |
201 // A completed download should also lose its reservation. | 201 // A completed download should also lose its reservation. |
202 TEST_F(DownloadPathReservationTrackerTest, CompleteDownload) { | 202 TEST_F(DownloadPathReservationTrackerTest, CompleteDownload) { |
203 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); | 203 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); |
204 FilePath path(GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); | 204 FilePath path(GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); |
205 ASSERT_FALSE(IsPathInUse(path)); | 205 ASSERT_FALSE(IsPathInUse(path)); |
206 | 206 |
207 FilePath reserved_path; | 207 FilePath reserved_path; |
208 bool verified = false; | 208 bool verified = false; |
209 CallGetReservedPath(*item, path, false, &reserved_path, &verified); | 209 CallGetReservedPath(*item, path, false, &reserved_path, &verified); |
210 EXPECT_TRUE(IsPathInUse(path)); | 210 EXPECT_TRUE(IsPathInUse(path)); |
211 EXPECT_TRUE(verified); | 211 EXPECT_TRUE(verified); |
212 EXPECT_EQ(path.value(), reserved_path.value()); | 212 EXPECT_EQ(path.value(), reserved_path.value()); |
213 | 213 |
214 // Once the download completes, the path should become available again. For a | 214 // Once the download completes, the path should become available again. For a |
215 // real download, at this point only the path reservation will be released. | 215 // real download, at this point only the path reservation will be released. |
216 // The path wouldn't be available since it is occupied on disk by the | 216 // The path wouldn't be available since it is occupied on disk by the |
217 // completed download. | 217 // completed download. |
218 item->SetState(DownloadItem::COMPLETE); | 218 item->SetState(DownloadItem::COMPLETE); |
219 message_loop_.RunAllPending(); | 219 message_loop_.RunUntilIdle(); |
220 EXPECT_FALSE(IsPathInUse(path)); | 220 EXPECT_FALSE(IsPathInUse(path)); |
221 } | 221 } |
222 | 222 |
223 // If there are files on the file system, a unique reservation should uniquify | 223 // If there are files on the file system, a unique reservation should uniquify |
224 // around it. | 224 // around it. |
225 TEST_F(DownloadPathReservationTrackerTest, ConflictingFiles) { | 225 TEST_F(DownloadPathReservationTrackerTest, ConflictingFiles) { |
226 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); | 226 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); |
227 FilePath path(GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); | 227 FilePath path(GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); |
228 FilePath path1(GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt"))); | 228 FilePath path1(GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt"))); |
229 // Create a file at |path|, and a .crdownload file at |path1|. | 229 // Create a file at |path|, and a .crdownload file at |path1|. |
230 ASSERT_EQ(0, file_util::WriteFile(path, "", 0)); | 230 ASSERT_EQ(0, file_util::WriteFile(path, "", 0)); |
231 ASSERT_EQ(0, file_util::WriteFile(download_util::GetCrDownloadPath(path1), | 231 ASSERT_EQ(0, file_util::WriteFile(download_util::GetCrDownloadPath(path1), |
232 "", 0)); | 232 "", 0)); |
233 ASSERT_TRUE(IsPathInUse(path)); | 233 ASSERT_TRUE(IsPathInUse(path)); |
234 | 234 |
235 FilePath reserved_path; | 235 FilePath reserved_path; |
236 bool verified = false; | 236 bool verified = false; |
237 CallGetReservedPath(*item, path, true, &reserved_path, &verified); | 237 CallGetReservedPath(*item, path, true, &reserved_path, &verified); |
238 EXPECT_TRUE(IsPathInUse(path)); | 238 EXPECT_TRUE(IsPathInUse(path)); |
239 EXPECT_TRUE(IsPathInUse(reserved_path)); | 239 EXPECT_TRUE(IsPathInUse(reserved_path)); |
240 EXPECT_TRUE(verified); | 240 EXPECT_TRUE(verified); |
241 // The path should be uniquified, skipping over foo.txt but not over | 241 // The path should be uniquified, skipping over foo.txt but not over |
242 // "foo (1).txt.crdownload" | 242 // "foo (1).txt.crdownload" |
243 EXPECT_EQ( | 243 EXPECT_EQ( |
244 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt")).value(), | 244 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt")).value(), |
245 reserved_path.value()); | 245 reserved_path.value()); |
246 | 246 |
247 item.reset(); | 247 item.reset(); |
248 message_loop_.RunAllPending(); | 248 message_loop_.RunUntilIdle(); |
249 EXPECT_TRUE(IsPathInUse(path)); | 249 EXPECT_TRUE(IsPathInUse(path)); |
250 EXPECT_FALSE(IsPathInUse(reserved_path)); | 250 EXPECT_FALSE(IsPathInUse(reserved_path)); |
251 } | 251 } |
252 | 252 |
253 // Multiple reservations for the same path should uniquify around each other. | 253 // Multiple reservations for the same path should uniquify around each other. |
254 TEST_F(DownloadPathReservationTrackerTest, ConflictingReservations) { | 254 TEST_F(DownloadPathReservationTrackerTest, ConflictingReservations) { |
255 scoped_ptr<FakeDownloadItem> item1(CreateDownloadItem(1)); | 255 scoped_ptr<FakeDownloadItem> item1(CreateDownloadItem(1)); |
256 FilePath path(GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); | 256 FilePath path(GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); |
257 FilePath uniquified_path( | 257 FilePath uniquified_path( |
258 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt"))); | 258 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt"))); |
(...skipping 10 matching lines...) Expand all Loading... |
269 { | 269 { |
270 // Requesting a reservation for the same path with uniquification results in | 270 // Requesting a reservation for the same path with uniquification results in |
271 // a uniquified path. | 271 // a uniquified path. |
272 scoped_ptr<FakeDownloadItem> item2(CreateDownloadItem(2)); | 272 scoped_ptr<FakeDownloadItem> item2(CreateDownloadItem(2)); |
273 FilePath reserved_path2; | 273 FilePath reserved_path2; |
274 CallGetReservedPath(*item2, path, true, &reserved_path2, &verified); | 274 CallGetReservedPath(*item2, path, true, &reserved_path2, &verified); |
275 EXPECT_TRUE(IsPathInUse(path)); | 275 EXPECT_TRUE(IsPathInUse(path)); |
276 EXPECT_TRUE(IsPathInUse(uniquified_path)); | 276 EXPECT_TRUE(IsPathInUse(uniquified_path)); |
277 EXPECT_EQ(uniquified_path.value(), reserved_path2.value()); | 277 EXPECT_EQ(uniquified_path.value(), reserved_path2.value()); |
278 } | 278 } |
279 message_loop_.RunAllPending(); | 279 message_loop_.RunUntilIdle(); |
280 EXPECT_TRUE(IsPathInUse(path)); | 280 EXPECT_TRUE(IsPathInUse(path)); |
281 EXPECT_FALSE(IsPathInUse(uniquified_path)); | 281 EXPECT_FALSE(IsPathInUse(uniquified_path)); |
282 | 282 |
283 { | 283 { |
284 // Since the previous download item was removed, requesting a reservation | 284 // Since the previous download item was removed, requesting a reservation |
285 // for the same path should result in the same uniquified path. | 285 // for the same path should result in the same uniquified path. |
286 scoped_ptr<FakeDownloadItem> item2(CreateDownloadItem(2)); | 286 scoped_ptr<FakeDownloadItem> item2(CreateDownloadItem(2)); |
287 FilePath reserved_path2; | 287 FilePath reserved_path2; |
288 CallGetReservedPath(*item2, path, true, &reserved_path2, &verified); | 288 CallGetReservedPath(*item2, path, true, &reserved_path2, &verified); |
289 EXPECT_TRUE(IsPathInUse(path)); | 289 EXPECT_TRUE(IsPathInUse(path)); |
290 EXPECT_TRUE(IsPathInUse(uniquified_path)); | 290 EXPECT_TRUE(IsPathInUse(uniquified_path)); |
291 EXPECT_EQ(uniquified_path.value(), reserved_path2.value()); | 291 EXPECT_EQ(uniquified_path.value(), reserved_path2.value()); |
292 } | 292 } |
293 message_loop_.RunAllPending(); | 293 message_loop_.RunUntilIdle(); |
294 | 294 |
295 // Now acquire an overwriting reservation. We should end up with the same | 295 // Now acquire an overwriting reservation. We should end up with the same |
296 // non-uniquified path for both reservations. | 296 // non-uniquified path for both reservations. |
297 scoped_ptr<FakeDownloadItem> item3(CreateDownloadItem(2)); | 297 scoped_ptr<FakeDownloadItem> item3(CreateDownloadItem(2)); |
298 FilePath reserved_path3; | 298 FilePath reserved_path3; |
299 CallGetReservedPath(*item3, path, false, &reserved_path3, &verified); | 299 CallGetReservedPath(*item3, path, false, &reserved_path3, &verified); |
300 EXPECT_TRUE(IsPathInUse(path)); | 300 EXPECT_TRUE(IsPathInUse(path)); |
301 EXPECT_FALSE(IsPathInUse(uniquified_path)); | 301 EXPECT_FALSE(IsPathInUse(uniquified_path)); |
302 | 302 |
303 EXPECT_EQ(path.value(), reserved_path1.value()); | 303 EXPECT_EQ(path.value(), reserved_path1.value()); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 bool verified = false; | 398 bool verified = false; |
399 CallGetReservedPath(*item, path, false, &reserved_path, &verified); | 399 CallGetReservedPath(*item, path, false, &reserved_path, &verified); |
400 EXPECT_TRUE(IsPathInUse(path)); | 400 EXPECT_TRUE(IsPathInUse(path)); |
401 EXPECT_TRUE(verified); | 401 EXPECT_TRUE(verified); |
402 EXPECT_EQ(path.value(), reserved_path.value()); | 402 EXPECT_EQ(path.value(), reserved_path.value()); |
403 | 403 |
404 // The target path is initially empty. If an OnDownloadUpdated() is issued in | 404 // The target path is initially empty. If an OnDownloadUpdated() is issued in |
405 // this state, we shouldn't lose the reservation. | 405 // this state, we shouldn't lose the reservation. |
406 ASSERT_EQ(FilePath::StringType(), item->GetTargetFilePath().value()); | 406 ASSERT_EQ(FilePath::StringType(), item->GetTargetFilePath().value()); |
407 item->UpdateObservers(); | 407 item->UpdateObservers(); |
408 message_loop_.RunAllPending(); | 408 message_loop_.RunUntilIdle(); |
409 EXPECT_TRUE(IsPathInUse(path)); | 409 EXPECT_TRUE(IsPathInUse(path)); |
410 | 410 |
411 // If the target path changes, we should update the reservation to match. | 411 // If the target path changes, we should update the reservation to match. |
412 FilePath new_target_path( | 412 FilePath new_target_path( |
413 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("bar.txt"))); | 413 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("bar.txt"))); |
414 ASSERT_FALSE(IsPathInUse(new_target_path)); | 414 ASSERT_FALSE(IsPathInUse(new_target_path)); |
415 EXPECT_CALL(*item, GetTargetFilePath()) | 415 EXPECT_CALL(*item, GetTargetFilePath()) |
416 .WillRepeatedly(ReturnRef(new_target_path)); | 416 .WillRepeatedly(ReturnRef(new_target_path)); |
417 item->UpdateObservers(); | 417 item->UpdateObservers(); |
418 message_loop_.RunAllPending(); | 418 message_loop_.RunUntilIdle(); |
419 EXPECT_FALSE(IsPathInUse(path)); | 419 EXPECT_FALSE(IsPathInUse(path)); |
420 EXPECT_TRUE(IsPathInUse(new_target_path)); | 420 EXPECT_TRUE(IsPathInUse(new_target_path)); |
421 | 421 |
422 // Destroying the item should release the reservation. | 422 // Destroying the item should release the reservation. |
423 item.reset(); | 423 item.reset(); |
424 message_loop_.RunAllPending(); | 424 message_loop_.RunUntilIdle(); |
425 EXPECT_FALSE(IsPathInUse(new_target_path)); | 425 EXPECT_FALSE(IsPathInUse(new_target_path)); |
426 } | 426 } |
OLD | NEW |