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

Side by Side Diff: chrome/browser/download/download_path_reservation_tracker_unittest.cc

Issue 248713004: [Downloads] Add real observers to MockDownloadItem. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 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 #include "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/files/file_path.h" 6 #include "base/files/file_path.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/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/observer_list.h" 10 #include "base/observer_list.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "base/test/test_file_util.h" 12 #include "base/test/test_file_util.h"
13 #include "chrome/browser/download/download_path_reservation_tracker.h" 13 #include "chrome/browser/download/download_path_reservation_tracker.h"
14 #include "chrome/browser/download/download_target_determiner.h" 14 #include "chrome/browser/download/download_target_determiner.h"
15 #include "content/public/test/mock_download_item.h" 15 #include "content/public/test/mock_download_item.h"
16 #include "content/public/test/test_browser_thread.h" 16 #include "content/public/test/test_browser_thread.h"
17 #include "testing/gmock/include/gmock/gmock.h" 17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 19
20 using content::BrowserThread; 20 using content::BrowserThread;
21 using content::DownloadItem; 21 using content::DownloadItem;
22 using content::MockDownloadItem; 22 using content::MockDownloadItem;
23 using testing::AnyNumber; 23 using testing::AnyNumber;
24 using testing::Return; 24 using testing::Return;
25 using testing::ReturnRef; 25 using testing::ReturnRef;
26 using testing::ReturnRefOfCopy; 26 using testing::ReturnRefOfCopy;
27 27
28 namespace { 28 namespace {
29 29
30 // MockDownloadItem with real observers and state.
31 class FakeDownloadItem : public MockDownloadItem {
32 public:
33 explicit FakeDownloadItem()
34 : state_(IN_PROGRESS) {
35 }
36 virtual ~FakeDownloadItem() {
37 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadDestroyed(this));
38 EXPECT_FALSE(observers_.might_have_observers());
39 }
40 virtual void AddObserver(Observer* observer) OVERRIDE {
41 observers_.AddObserver(observer);
42 }
43 virtual void RemoveObserver(Observer* observer) OVERRIDE {
44 observers_.RemoveObserver(observer);
45 }
46 virtual void UpdateObservers() OVERRIDE {
47 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this));
48 }
49
50 virtual DownloadState GetState() const OVERRIDE {
51 return state_;
52 }
53
54 void SetState(DownloadState state) {
55 state_ = state;
56 UpdateObservers();
57 }
58
59 private:
60 DownloadState state_;
61 ObserverList<Observer> observers_;
62 };
63
64 class DownloadPathReservationTrackerTest : public testing::Test { 30 class DownloadPathReservationTrackerTest : public testing::Test {
65 public: 31 public:
66 DownloadPathReservationTrackerTest(); 32 DownloadPathReservationTrackerTest();
67 33
68 // testing::Test 34 // testing::Test
69 virtual void SetUp() OVERRIDE; 35 virtual void SetUp() OVERRIDE;
70 virtual void TearDown() OVERRIDE; 36 virtual void TearDown() OVERRIDE;
71 37
72 FakeDownloadItem* CreateDownloadItem(int32 id); 38 MockDownloadItem* CreateDownloadItem(int32 id);
73 base::FilePath GetPathInDownloadsDirectory( 39 base::FilePath GetPathInDownloadsDirectory(
74 const base::FilePath::CharType* suffix); 40 const base::FilePath::CharType* suffix);
75 bool IsPathInUse(const base::FilePath& path); 41 bool IsPathInUse(const base::FilePath& path);
76 void CallGetReservedPath( 42 void CallGetReservedPath(
77 DownloadItem* download_item, 43 DownloadItem* download_item,
78 const base::FilePath& target_path, 44 const base::FilePath& target_path,
79 bool create_directory, 45 bool create_directory,
80 DownloadPathReservationTracker::FilenameConflictAction conflict_action, 46 DownloadPathReservationTracker::FilenameConflictAction conflict_action,
81 base::FilePath* return_path, 47 base::FilePath* return_path,
82 bool* return_verified); 48 bool* return_verified);
(...skipping 28 matching lines...) Expand all
111 77
112 void DownloadPathReservationTrackerTest::SetUp() { 78 void DownloadPathReservationTrackerTest::SetUp() {
113 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir()); 79 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir());
114 set_default_download_path(test_download_dir_.path()); 80 set_default_download_path(test_download_dir_.path());
115 } 81 }
116 82
117 void DownloadPathReservationTrackerTest::TearDown() { 83 void DownloadPathReservationTrackerTest::TearDown() {
118 message_loop_.RunUntilIdle(); 84 message_loop_.RunUntilIdle();
119 } 85 }
120 86
121 FakeDownloadItem* DownloadPathReservationTrackerTest::CreateDownloadItem( 87 MockDownloadItem* DownloadPathReservationTrackerTest::CreateDownloadItem(
122 int32 id) { 88 int32 id) {
123 FakeDownloadItem* item = new ::testing::StrictMock<FakeDownloadItem>; 89 MockDownloadItem* item = new ::testing::StrictMock<MockDownloadItem>;
124 EXPECT_CALL(*item, GetId()) 90 EXPECT_CALL(*item, GetId())
125 .WillRepeatedly(Return(id)); 91 .WillRepeatedly(Return(id));
126 EXPECT_CALL(*item, GetTargetFilePath()) 92 EXPECT_CALL(*item, GetTargetFilePath())
127 .WillRepeatedly(ReturnRefOfCopy(base::FilePath())); 93 .WillRepeatedly(ReturnRefOfCopy(base::FilePath()));
128 return item; 94 return item;
129 } 95 }
130 96
131 base::FilePath DownloadPathReservationTrackerTest::GetPathInDownloadsDirectory( 97 base::FilePath DownloadPathReservationTrackerTest::GetPathInDownloadsDirectory(
132 const base::FilePath::CharType* suffix) { 98 const base::FilePath::CharType* suffix) {
133 return default_download_path().Append(suffix).NormalizePathSeparators(); 99 return default_download_path().Append(suffix).NormalizePathSeparators();
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 size_t repeat, const base::FilePath::CharType* suffix) { 142 size_t repeat, const base::FilePath::CharType* suffix) {
177 return GetPathInDownloadsDirectory( 143 return GetPathInDownloadsDirectory(
178 (base::FilePath::StringType(repeat, FILE_PATH_LITERAL('a')) 144 (base::FilePath::StringType(repeat, FILE_PATH_LITERAL('a'))
179 + suffix).c_str()); 145 + suffix).c_str());
180 } 146 }
181 147
182 } // namespace 148 } // namespace
183 149
184 // A basic reservation is acquired and committed. 150 // A basic reservation is acquired and committed.
185 TEST_F(DownloadPathReservationTrackerTest, BasicReservation) { 151 TEST_F(DownloadPathReservationTrackerTest, BasicReservation) {
186 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); 152 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
187 base::FilePath path( 153 base::FilePath path(
188 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); 154 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
189 ASSERT_FALSE(IsPathInUse(path)); 155 ASSERT_FALSE(IsPathInUse(path));
190 156
191 base::FilePath reserved_path; 157 base::FilePath reserved_path;
192 bool verified = false; 158 bool verified = false;
193 DownloadPathReservationTracker::FilenameConflictAction conflict_action = 159 DownloadPathReservationTracker::FilenameConflictAction conflict_action =
194 DownloadPathReservationTracker::OVERWRITE; 160 DownloadPathReservationTracker::OVERWRITE;
195 bool create_directory = false; 161 bool create_directory = false;
196 CallGetReservedPath( 162 CallGetReservedPath(
197 item.get(), 163 item.get(),
198 path, 164 path,
199 create_directory, 165 create_directory,
200 conflict_action, 166 conflict_action,
201 &reserved_path, 167 &reserved_path,
202 &verified); 168 &verified);
203 EXPECT_TRUE(IsPathInUse(path)); 169 EXPECT_TRUE(IsPathInUse(path));
204 EXPECT_TRUE(verified); 170 EXPECT_TRUE(verified);
205 EXPECT_EQ(path.value(), reserved_path.value()); 171 EXPECT_EQ(path.value(), reserved_path.value());
206 172
207 // Destroying the item should release the reservation. 173 // Destroying the item should release the reservation.
208 item->SetState(DownloadItem::COMPLETE); 174 EXPECT_CALL(*item, GetState()).WillRepeatedly(Return(DownloadItem::COMPLETE));
175 item->UpdateObservers();
209 item.reset(); 176 item.reset();
210 message_loop_.RunUntilIdle(); 177 message_loop_.RunUntilIdle();
211 EXPECT_FALSE(IsPathInUse(path)); 178 EXPECT_FALSE(IsPathInUse(path));
212 } 179 }
213 180
214 // A download that is interrupted should lose its reservation. 181 // A download that is interrupted should lose its reservation.
215 TEST_F(DownloadPathReservationTrackerTest, InterruptedDownload) { 182 TEST_F(DownloadPathReservationTrackerTest, InterruptedDownload) {
216 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); 183 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
217 base::FilePath path( 184 base::FilePath path(
218 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); 185 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
219 ASSERT_FALSE(IsPathInUse(path)); 186 ASSERT_FALSE(IsPathInUse(path));
220 187
221 base::FilePath reserved_path; 188 base::FilePath reserved_path;
222 bool verified = false; 189 bool verified = false;
223 DownloadPathReservationTracker::FilenameConflictAction conflict_action = 190 DownloadPathReservationTracker::FilenameConflictAction conflict_action =
224 DownloadPathReservationTracker::OVERWRITE; 191 DownloadPathReservationTracker::OVERWRITE;
225 bool create_directory = false; 192 bool create_directory = false;
226 CallGetReservedPath( 193 CallGetReservedPath(
227 item.get(), 194 item.get(),
228 path, 195 path,
229 create_directory, 196 create_directory,
230 conflict_action, 197 conflict_action,
231 &reserved_path, 198 &reserved_path,
232 &verified); 199 &verified);
233 EXPECT_TRUE(IsPathInUse(path)); 200 EXPECT_TRUE(IsPathInUse(path));
234 EXPECT_TRUE(verified); 201 EXPECT_TRUE(verified);
235 EXPECT_EQ(path.value(), reserved_path.value()); 202 EXPECT_EQ(path.value(), reserved_path.value());
236 203
237 // Once the download is interrupted, the path should become available again. 204 // Once the download is interrupted, the path should become available again.
238 item->SetState(DownloadItem::INTERRUPTED); 205 EXPECT_CALL(*item, GetState())
206 .WillRepeatedly(Return(DownloadItem::INTERRUPTED));
207 item->UpdateObservers();
239 message_loop_.RunUntilIdle(); 208 message_loop_.RunUntilIdle();
240 EXPECT_FALSE(IsPathInUse(path)); 209 EXPECT_FALSE(IsPathInUse(path));
241 } 210 }
242 211
243 // A completed download should also lose its reservation. 212 // A completed download should also lose its reservation.
244 TEST_F(DownloadPathReservationTrackerTest, CompleteDownload) { 213 TEST_F(DownloadPathReservationTrackerTest, CompleteDownload) {
245 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); 214 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
246 base::FilePath path( 215 base::FilePath path(
247 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); 216 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
248 ASSERT_FALSE(IsPathInUse(path)); 217 ASSERT_FALSE(IsPathInUse(path));
249 218
250 base::FilePath reserved_path; 219 base::FilePath reserved_path;
251 bool verified = false; 220 bool verified = false;
252 DownloadPathReservationTracker::FilenameConflictAction conflict_action = 221 DownloadPathReservationTracker::FilenameConflictAction conflict_action =
253 DownloadPathReservationTracker::OVERWRITE; 222 DownloadPathReservationTracker::OVERWRITE;
254 bool create_directory = false; 223 bool create_directory = false;
255 CallGetReservedPath( 224 CallGetReservedPath(
256 item.get(), 225 item.get(),
257 path, 226 path,
258 create_directory, 227 create_directory,
259 conflict_action, 228 conflict_action,
260 &reserved_path, 229 &reserved_path,
261 &verified); 230 &verified);
262 EXPECT_TRUE(IsPathInUse(path)); 231 EXPECT_TRUE(IsPathInUse(path));
263 EXPECT_TRUE(verified); 232 EXPECT_TRUE(verified);
264 EXPECT_EQ(path.value(), reserved_path.value()); 233 EXPECT_EQ(path.value(), reserved_path.value());
265 234
266 // Once the download completes, the path should become available again. For a 235 // Once the download completes, the path should become available again. For a
267 // real download, at this point only the path reservation will be released. 236 // real download, at this point only the path reservation will be released.
268 // The path wouldn't be available since it is occupied on disk by the 237 // The path wouldn't be available since it is occupied on disk by the
269 // completed download. 238 // completed download.
270 item->SetState(DownloadItem::COMPLETE); 239 EXPECT_CALL(*item, GetState()).WillRepeatedly(Return(DownloadItem::COMPLETE));
240 item->UpdateObservers();
271 message_loop_.RunUntilIdle(); 241 message_loop_.RunUntilIdle();
272 EXPECT_FALSE(IsPathInUse(path)); 242 EXPECT_FALSE(IsPathInUse(path));
273 } 243 }
274 244
275 // If there are files on the file system, a unique reservation should uniquify 245 // If there are files on the file system, a unique reservation should uniquify
276 // around it. 246 // around it.
277 TEST_F(DownloadPathReservationTrackerTest, ConflictingFiles) { 247 TEST_F(DownloadPathReservationTrackerTest, ConflictingFiles) {
278 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); 248 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
279 base::FilePath path( 249 base::FilePath path(
280 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); 250 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
281 base::FilePath path1( 251 base::FilePath path1(
282 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt"))); 252 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt")));
283 // Create a file at |path|, and a .crdownload file at |path1|. 253 // Create a file at |path|, and a .crdownload file at |path1|.
284 ASSERT_EQ(0, base::WriteFile(path, "", 0)); 254 ASSERT_EQ(0, base::WriteFile(path, "", 0));
285 ASSERT_EQ(0, 255 ASSERT_EQ(0,
286 base::WriteFile( 256 base::WriteFile(
287 DownloadTargetDeterminer::GetCrDownloadPath(path1), "", 0)); 257 DownloadTargetDeterminer::GetCrDownloadPath(path1), "", 0));
288 ASSERT_TRUE(IsPathInUse(path)); 258 ASSERT_TRUE(IsPathInUse(path));
(...skipping 12 matching lines...) Expand all
301 &verified); 271 &verified);
302 EXPECT_TRUE(IsPathInUse(path)); 272 EXPECT_TRUE(IsPathInUse(path));
303 EXPECT_TRUE(IsPathInUse(reserved_path)); 273 EXPECT_TRUE(IsPathInUse(reserved_path));
304 EXPECT_TRUE(verified); 274 EXPECT_TRUE(verified);
305 // The path should be uniquified, skipping over foo.txt but not over 275 // The path should be uniquified, skipping over foo.txt but not over
306 // "foo (1).txt.crdownload" 276 // "foo (1).txt.crdownload"
307 EXPECT_EQ( 277 EXPECT_EQ(
308 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt")).value(), 278 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt")).value(),
309 reserved_path.value()); 279 reserved_path.value());
310 280
311 item->SetState(DownloadItem::COMPLETE); 281 EXPECT_CALL(*item, GetState()).WillRepeatedly(Return(DownloadItem::COMPLETE));
282 item->UpdateObservers();
312 item.reset(); 283 item.reset();
313 message_loop_.RunUntilIdle(); 284 message_loop_.RunUntilIdle();
314 EXPECT_TRUE(IsPathInUse(path)); 285 EXPECT_TRUE(IsPathInUse(path));
315 EXPECT_FALSE(IsPathInUse(reserved_path)); 286 EXPECT_FALSE(IsPathInUse(reserved_path));
316 } 287 }
317 288
318 // Multiple reservations for the same path should uniquify around each other. 289 // Multiple reservations for the same path should uniquify around each other.
319 TEST_F(DownloadPathReservationTrackerTest, ConflictingReservations) { 290 TEST_F(DownloadPathReservationTrackerTest, ConflictingReservations) {
320 scoped_ptr<FakeDownloadItem> item1(CreateDownloadItem(1)); 291 scoped_ptr<MockDownloadItem> item1(CreateDownloadItem(1));
321 base::FilePath path( 292 base::FilePath path(
322 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); 293 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
323 base::FilePath uniquified_path( 294 base::FilePath uniquified_path(
324 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt"))); 295 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt")));
325 ASSERT_FALSE(IsPathInUse(path)); 296 ASSERT_FALSE(IsPathInUse(path));
326 ASSERT_FALSE(IsPathInUse(uniquified_path)); 297 ASSERT_FALSE(IsPathInUse(uniquified_path));
327 298
328 base::FilePath reserved_path1; 299 base::FilePath reserved_path1;
329 bool verified = false; 300 bool verified = false;
330 bool create_directory = false; 301 bool create_directory = false;
331 302
332 DownloadPathReservationTracker::FilenameConflictAction conflict_action = 303 DownloadPathReservationTracker::FilenameConflictAction conflict_action =
333 DownloadPathReservationTracker::UNIQUIFY; 304 DownloadPathReservationTracker::UNIQUIFY;
334 CallGetReservedPath( 305 CallGetReservedPath(
335 item1.get(), 306 item1.get(),
336 path, 307 path,
337 create_directory, 308 create_directory,
338 conflict_action, 309 conflict_action,
339 &reserved_path1, 310 &reserved_path1,
340 &verified); 311 &verified);
341 EXPECT_TRUE(IsPathInUse(path)); 312 EXPECT_TRUE(IsPathInUse(path));
342 EXPECT_TRUE(verified); 313 EXPECT_TRUE(verified);
343 314
344 315
345 { 316 {
346 // Requesting a reservation for the same path with uniquification results in 317 // Requesting a reservation for the same path with uniquification results in
347 // a uniquified path. 318 // a uniquified path.
348 scoped_ptr<FakeDownloadItem> item2(CreateDownloadItem(2)); 319 scoped_ptr<MockDownloadItem> item2(CreateDownloadItem(2));
349 base::FilePath reserved_path2; 320 base::FilePath reserved_path2;
350 CallGetReservedPath( 321 CallGetReservedPath(
351 item2.get(), 322 item2.get(),
352 path, 323 path,
353 create_directory, 324 create_directory,
354 conflict_action, 325 conflict_action,
355 &reserved_path2, 326 &reserved_path2,
356 &verified); 327 &verified);
357 EXPECT_TRUE(IsPathInUse(path)); 328 EXPECT_TRUE(IsPathInUse(path));
358 EXPECT_TRUE(IsPathInUse(uniquified_path)); 329 EXPECT_TRUE(IsPathInUse(uniquified_path));
359 EXPECT_EQ(uniquified_path.value(), reserved_path2.value()); 330 EXPECT_EQ(uniquified_path.value(), reserved_path2.value());
360 item2->SetState(DownloadItem::COMPLETE); 331 EXPECT_CALL(*item2, GetState())
332 .WillRepeatedly(Return(DownloadItem::COMPLETE));
333 item2->UpdateObservers();
Randy Smith (Not in Mondays) 2014/04/23 19:28:35 You do this often enough that maybe a helper funct
asanka 2014/04/25 21:31:27 Done. Added a SetDownloadItemState() function.
361 } 334 }
362 message_loop_.RunUntilIdle(); 335 message_loop_.RunUntilIdle();
363 EXPECT_TRUE(IsPathInUse(path)); 336 EXPECT_TRUE(IsPathInUse(path));
364 EXPECT_FALSE(IsPathInUse(uniquified_path)); 337 EXPECT_FALSE(IsPathInUse(uniquified_path));
365 338
366 { 339 {
367 // Since the previous download item was removed, requesting a reservation 340 // Since the previous download item was removed, requesting a reservation
368 // for the same path should result in the same uniquified path. 341 // for the same path should result in the same uniquified path.
369 scoped_ptr<FakeDownloadItem> item2(CreateDownloadItem(2)); 342 scoped_ptr<MockDownloadItem> item2(CreateDownloadItem(2));
370 base::FilePath reserved_path2; 343 base::FilePath reserved_path2;
371 CallGetReservedPath( 344 CallGetReservedPath(
372 item2.get(), 345 item2.get(),
373 path, 346 path,
374 create_directory, 347 create_directory,
375 conflict_action, 348 conflict_action,
376 &reserved_path2, 349 &reserved_path2,
377 &verified); 350 &verified);
378 EXPECT_TRUE(IsPathInUse(path)); 351 EXPECT_TRUE(IsPathInUse(path));
379 EXPECT_TRUE(IsPathInUse(uniquified_path)); 352 EXPECT_TRUE(IsPathInUse(uniquified_path));
380 EXPECT_EQ(uniquified_path.value(), reserved_path2.value()); 353 EXPECT_EQ(uniquified_path.value(), reserved_path2.value());
381 item2->SetState(DownloadItem::COMPLETE); 354 EXPECT_CALL(*item2, GetState())
355 .WillRepeatedly(Return(DownloadItem::COMPLETE));
356 item2->UpdateObservers();
382 } 357 }
383 message_loop_.RunUntilIdle(); 358 message_loop_.RunUntilIdle();
384 359
385 // Now acquire an overwriting reservation. We should end up with the same 360 // Now acquire an overwriting reservation. We should end up with the same
386 // non-uniquified path for both reservations. 361 // non-uniquified path for both reservations.
387 scoped_ptr<FakeDownloadItem> item3(CreateDownloadItem(2)); 362 scoped_ptr<MockDownloadItem> item3(CreateDownloadItem(2));
388 base::FilePath reserved_path3; 363 base::FilePath reserved_path3;
389 conflict_action = DownloadPathReservationTracker::OVERWRITE; 364 conflict_action = DownloadPathReservationTracker::OVERWRITE;
390 CallGetReservedPath( 365 CallGetReservedPath(
391 item3.get(), 366 item3.get(),
392 path, 367 path,
393 create_directory, 368 create_directory,
394 conflict_action, 369 conflict_action,
395 &reserved_path3, 370 &reserved_path3,
396 &verified); 371 &verified);
397 EXPECT_TRUE(IsPathInUse(path)); 372 EXPECT_TRUE(IsPathInUse(path));
398 EXPECT_FALSE(IsPathInUse(uniquified_path)); 373 EXPECT_FALSE(IsPathInUse(uniquified_path));
399 374
400 EXPECT_EQ(path.value(), reserved_path1.value()); 375 EXPECT_EQ(path.value(), reserved_path1.value());
401 EXPECT_EQ(path.value(), reserved_path3.value()); 376 EXPECT_EQ(path.value(), reserved_path3.value());
402 377
403 item1->SetState(DownloadItem::COMPLETE); 378 EXPECT_CALL(*item1, GetState())
404 item3->SetState(DownloadItem::COMPLETE); 379 .WillRepeatedly(Return(DownloadItem::COMPLETE));
380 EXPECT_CALL(*item3, GetState())
381 .WillRepeatedly(Return(DownloadItem::COMPLETE));
382 item1->UpdateObservers();
383 item3->UpdateObservers();
405 } 384 }
406 385
407 // If a unique path cannot be determined after trying kMaxUniqueFiles 386 // If a unique path cannot be determined after trying kMaxUniqueFiles
408 // uniquifiers, then the callback should notified that verification failed, and 387 // uniquifiers, then the callback should notified that verification failed, and
409 // the returned path should be set to the original requested path. 388 // the returned path should be set to the original requested path.
410 TEST_F(DownloadPathReservationTrackerTest, UnresolvedConflicts) { 389 TEST_F(DownloadPathReservationTrackerTest, UnresolvedConflicts) {
411 base::FilePath path( 390 base::FilePath path(
412 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); 391 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
413 scoped_ptr<FakeDownloadItem> items[ 392 scoped_ptr<MockDownloadItem>
414 DownloadPathReservationTracker::kMaxUniqueFiles + 1]; 393 items[DownloadPathReservationTracker::kMaxUniqueFiles + 1];
415 DownloadPathReservationTracker::FilenameConflictAction conflict_action = 394 DownloadPathReservationTracker::FilenameConflictAction conflict_action =
416 DownloadPathReservationTracker::UNIQUIFY; 395 DownloadPathReservationTracker::UNIQUIFY;
417 bool create_directory = false; 396 bool create_directory = false;
418 397
419 // Create |kMaxUniqueFiles + 1| reservations for |path|. The first reservation 398 // Create |kMaxUniqueFiles + 1| reservations for |path|. The first reservation
420 // will have no uniquifier. The |kMaxUniqueFiles| remaining reservations do. 399 // will have no uniquifier. The |kMaxUniqueFiles| remaining reservations do.
421 for (int i = 0; i <= DownloadPathReservationTracker::kMaxUniqueFiles; i++) { 400 for (int i = 0; i <= DownloadPathReservationTracker::kMaxUniqueFiles; i++) {
422 base::FilePath reserved_path; 401 base::FilePath reserved_path;
423 base::FilePath expected_path; 402 base::FilePath expected_path;
424 bool verified = false; 403 bool verified = false;
(...skipping 10 matching lines...) Expand all
435 path, 414 path,
436 create_directory, 415 create_directory,
437 conflict_action, 416 conflict_action,
438 &reserved_path, 417 &reserved_path,
439 &verified); 418 &verified);
440 EXPECT_TRUE(IsPathInUse(expected_path)); 419 EXPECT_TRUE(IsPathInUse(expected_path));
441 EXPECT_EQ(expected_path.value(), reserved_path.value()); 420 EXPECT_EQ(expected_path.value(), reserved_path.value());
442 EXPECT_TRUE(verified); 421 EXPECT_TRUE(verified);
443 } 422 }
444 // The next reservation for |path| will fail to be unique. 423 // The next reservation for |path| will fail to be unique.
445 scoped_ptr<FakeDownloadItem> item( 424 scoped_ptr<MockDownloadItem> item(
446 CreateDownloadItem(DownloadPathReservationTracker::kMaxUniqueFiles + 1)); 425 CreateDownloadItem(DownloadPathReservationTracker::kMaxUniqueFiles + 1));
447 base::FilePath reserved_path; 426 base::FilePath reserved_path;
448 bool verified = true; 427 bool verified = true;
449 CallGetReservedPath( 428 CallGetReservedPath(
450 item.get(), 429 item.get(),
451 path, 430 path,
452 create_directory, 431 create_directory,
453 conflict_action, 432 conflict_action,
454 &reserved_path, 433 &reserved_path,
455 &verified); 434 &verified);
456 EXPECT_FALSE(verified); 435 EXPECT_FALSE(verified);
457 EXPECT_EQ(path.value(), reserved_path.value()); 436 EXPECT_EQ(path.value(), reserved_path.value());
458 437
459 item->SetState(DownloadItem::COMPLETE); 438 EXPECT_CALL(*item, GetState()).WillRepeatedly(Return(DownloadItem::COMPLETE));
439 item->UpdateObservers();
460 for (int i = 0; i <= DownloadPathReservationTracker::kMaxUniqueFiles; i++) { 440 for (int i = 0; i <= DownloadPathReservationTracker::kMaxUniqueFiles; i++) {
461 items[i]->SetState(DownloadItem::COMPLETE); 441 EXPECT_CALL(*items[i], GetState())
442 .WillRepeatedly(Return(DownloadItem::COMPLETE));
443 items[i]->UpdateObservers();
462 } 444 }
463 } 445 }
464 446
465 // If the target directory is unwriteable, then callback should be notified that 447 // If the target directory is unwriteable, then callback should be notified that
466 // verification failed. 448 // verification failed.
467 TEST_F(DownloadPathReservationTrackerTest, UnwriteableDirectory) { 449 TEST_F(DownloadPathReservationTrackerTest, UnwriteableDirectory) {
468 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); 450 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
469 base::FilePath path( 451 base::FilePath path(
470 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); 452 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
471 base::FilePath dir(path.DirName()); 453 base::FilePath dir(path.DirName());
472 ASSERT_FALSE(IsPathInUse(path)); 454 ASSERT_FALSE(IsPathInUse(path));
473 455
474 { 456 {
475 // Scope for PermissionRestorer 457 // Scope for PermissionRestorer
476 file_util::PermissionRestorer restorer(dir); 458 file_util::PermissionRestorer restorer(dir);
477 EXPECT_TRUE(file_util::MakeFileUnwritable(dir)); 459 EXPECT_TRUE(file_util::MakeFileUnwritable(dir));
478 base::FilePath reserved_path; 460 base::FilePath reserved_path;
479 bool verified = true; 461 bool verified = true;
480 DownloadPathReservationTracker::FilenameConflictAction conflict_action = 462 DownloadPathReservationTracker::FilenameConflictAction conflict_action =
481 DownloadPathReservationTracker::OVERWRITE; 463 DownloadPathReservationTracker::OVERWRITE;
482 bool create_directory = false; 464 bool create_directory = false;
483 CallGetReservedPath( 465 CallGetReservedPath(
484 item.get(), 466 item.get(),
485 path, 467 path,
486 create_directory, 468 create_directory,
487 conflict_action, 469 conflict_action,
488 &reserved_path, 470 &reserved_path,
489 &verified); 471 &verified);
490 // Verification fails. 472 // Verification fails.
491 EXPECT_FALSE(verified); 473 EXPECT_FALSE(verified);
492 EXPECT_EQ(path.BaseName().value(), reserved_path.BaseName().value()); 474 EXPECT_EQ(path.BaseName().value(), reserved_path.BaseName().value());
493 } 475 }
494 item->SetState(DownloadItem::COMPLETE); 476 EXPECT_CALL(*item, GetState()).WillRepeatedly(Return(DownloadItem::COMPLETE));
477 item->UpdateObservers();
495 } 478 }
496 479
497 // If the default download directory doesn't exist, then it should be 480 // If the default download directory doesn't exist, then it should be
498 // created. But only if we are actually going to create the download path there. 481 // created. But only if we are actually going to create the download path there.
499 TEST_F(DownloadPathReservationTrackerTest, CreateDefaultDownloadPath) { 482 TEST_F(DownloadPathReservationTrackerTest, CreateDefaultDownloadPath) {
500 base::FilePath path( 483 base::FilePath path(
501 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo/foo.txt"))); 484 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo/foo.txt")));
502 base::FilePath dir(path.DirName()); 485 base::FilePath dir(path.DirName());
503 ASSERT_FALSE(base::DirectoryExists(dir)); 486 ASSERT_FALSE(base::DirectoryExists(dir));
504 DownloadPathReservationTracker::FilenameConflictAction conflict_action = 487 DownloadPathReservationTracker::FilenameConflictAction conflict_action =
505 DownloadPathReservationTracker::OVERWRITE; 488 DownloadPathReservationTracker::OVERWRITE;
506 bool create_directory = false; 489 bool create_directory = false;
507 490
508 { 491 {
509 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); 492 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
510 base::FilePath reserved_path; 493 base::FilePath reserved_path;
511 bool verified = true; 494 bool verified = true;
512 CallGetReservedPath( 495 CallGetReservedPath(
513 item.get(), 496 item.get(),
514 path, 497 path,
515 create_directory, 498 create_directory,
516 conflict_action, 499 conflict_action,
517 &reserved_path, 500 &reserved_path,
518 &verified); 501 &verified);
519 // Verification fails because the directory doesn't exist. 502 // Verification fails because the directory doesn't exist.
520 EXPECT_FALSE(verified); 503 EXPECT_FALSE(verified);
521 item->SetState(DownloadItem::COMPLETE); 504 EXPECT_CALL(*item, GetState())
505 .WillRepeatedly(Return(DownloadItem::COMPLETE));
506 item->UpdateObservers();
522 } 507 }
523 ASSERT_FALSE(IsPathInUse(path)); 508 ASSERT_FALSE(IsPathInUse(path));
524 { 509 {
525 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); 510 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
526 base::FilePath reserved_path; 511 base::FilePath reserved_path;
527 bool verified = true; 512 bool verified = true;
528 set_default_download_path(dir); 513 set_default_download_path(dir);
529 CallGetReservedPath( 514 CallGetReservedPath(
530 item.get(), 515 item.get(),
531 path, 516 path,
532 create_directory, 517 create_directory,
533 conflict_action, 518 conflict_action,
534 &reserved_path, 519 &reserved_path,
535 &verified); 520 &verified);
536 // Verification succeeds because the directory is created. 521 // Verification succeeds because the directory is created.
537 EXPECT_TRUE(verified); 522 EXPECT_TRUE(verified);
538 EXPECT_TRUE(base::DirectoryExists(dir)); 523 EXPECT_TRUE(base::DirectoryExists(dir));
539 item->SetState(DownloadItem::COMPLETE); 524 EXPECT_CALL(*item, GetState())
525 .WillRepeatedly(Return(DownloadItem::COMPLETE));
526 item->UpdateObservers();
540 } 527 }
541 } 528 }
542 529
543 // If the target path of the download item changes, the reservation should be 530 // If the target path of the download item changes, the reservation should be
544 // updated to match. 531 // updated to match.
545 TEST_F(DownloadPathReservationTrackerTest, UpdatesToTargetPath) { 532 TEST_F(DownloadPathReservationTrackerTest, UpdatesToTargetPath) {
546 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); 533 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
547 base::FilePath path( 534 base::FilePath path(
548 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); 535 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
549 ASSERT_FALSE(IsPathInUse(path)); 536 ASSERT_FALSE(IsPathInUse(path));
550 537
551 base::FilePath reserved_path; 538 base::FilePath reserved_path;
552 bool verified = false; 539 bool verified = false;
553 DownloadPathReservationTracker::FilenameConflictAction conflict_action = 540 DownloadPathReservationTracker::FilenameConflictAction conflict_action =
554 DownloadPathReservationTracker::OVERWRITE; 541 DownloadPathReservationTracker::OVERWRITE;
555 bool create_directory = false; 542 bool create_directory = false;
556 CallGetReservedPath( 543 CallGetReservedPath(
(...skipping 19 matching lines...) Expand all
576 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("bar.txt"))); 563 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("bar.txt")));
577 ASSERT_FALSE(IsPathInUse(new_target_path)); 564 ASSERT_FALSE(IsPathInUse(new_target_path));
578 EXPECT_CALL(*item, GetTargetFilePath()) 565 EXPECT_CALL(*item, GetTargetFilePath())
579 .WillRepeatedly(ReturnRef(new_target_path)); 566 .WillRepeatedly(ReturnRef(new_target_path));
580 item->UpdateObservers(); 567 item->UpdateObservers();
581 message_loop_.RunUntilIdle(); 568 message_loop_.RunUntilIdle();
582 EXPECT_FALSE(IsPathInUse(path)); 569 EXPECT_FALSE(IsPathInUse(path));
583 EXPECT_TRUE(IsPathInUse(new_target_path)); 570 EXPECT_TRUE(IsPathInUse(new_target_path));
584 571
585 // Destroying the item should release the reservation. 572 // Destroying the item should release the reservation.
586 item->SetState(DownloadItem::COMPLETE); 573 EXPECT_CALL(*item, GetState()).WillRepeatedly(Return(DownloadItem::COMPLETE));
574 item->UpdateObservers();
587 item.reset(); 575 item.reset();
588 message_loop_.RunUntilIdle(); 576 message_loop_.RunUntilIdle();
589 EXPECT_FALSE(IsPathInUse(new_target_path)); 577 EXPECT_FALSE(IsPathInUse(new_target_path));
590 } 578 }
591 579
592 // Tests for long name truncation. On other platforms automatic truncation 580 // Tests for long name truncation. On other platforms automatic truncation
593 // is not performed (yet). 581 // is not performed (yet).
594 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) 582 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS)
595 583
596 TEST_F(DownloadPathReservationTrackerTest, BasicTruncation) { 584 TEST_F(DownloadPathReservationTrackerTest, BasicTruncation) {
597 int real_max_length = 585 int real_max_length =
598 base::GetMaximumPathComponentLength(default_download_path()); 586 base::GetMaximumPathComponentLength(default_download_path());
599 ASSERT_NE(-1, real_max_length); 587 ASSERT_NE(-1, real_max_length);
600 588
601 // TODO(kinaba): the current implementation leaves spaces for appending 589 // TODO(kinaba): the current implementation leaves spaces for appending
602 // ".crdownload". So take it into account. Should be removed in the future. 590 // ".crdownload". So take it into account. Should be removed in the future.
603 const size_t max_length = real_max_length - 11; 591 const size_t max_length = real_max_length - 11;
604 592
605 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); 593 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
606 base::FilePath path(GetLongNamePathInDownloadsDirectory( 594 base::FilePath path(GetLongNamePathInDownloadsDirectory(
607 max_length, FILE_PATH_LITERAL(".txt"))); 595 max_length, FILE_PATH_LITERAL(".txt")));
608 ASSERT_FALSE(IsPathInUse(path)); 596 ASSERT_FALSE(IsPathInUse(path));
609 597
610 base::FilePath reserved_path; 598 base::FilePath reserved_path;
611 bool verified = false; 599 bool verified = false;
612 DownloadPathReservationTracker::FilenameConflictAction conflict_action = 600 DownloadPathReservationTracker::FilenameConflictAction conflict_action =
613 DownloadPathReservationTracker::OVERWRITE; 601 DownloadPathReservationTracker::OVERWRITE;
614 bool create_directory = false; 602 bool create_directory = false;
615 CallGetReservedPath( 603 CallGetReservedPath(
616 item.get(), 604 item.get(),
617 path, 605 path,
618 create_directory, 606 create_directory,
619 conflict_action, 607 conflict_action,
620 &reserved_path, 608 &reserved_path,
621 &verified); 609 &verified);
622 EXPECT_TRUE(IsPathInUse(reserved_path)); 610 EXPECT_TRUE(IsPathInUse(reserved_path));
623 EXPECT_TRUE(verified); 611 EXPECT_TRUE(verified);
624 // The file name length is truncated to max_length. 612 // The file name length is truncated to max_length.
625 EXPECT_EQ(max_length, reserved_path.BaseName().value().size()); 613 EXPECT_EQ(max_length, reserved_path.BaseName().value().size());
626 // But the extension is kept unchanged. 614 // But the extension is kept unchanged.
627 EXPECT_EQ(path.Extension(), reserved_path.Extension()); 615 EXPECT_EQ(path.Extension(), reserved_path.Extension());
628 item->SetState(DownloadItem::COMPLETE); 616 EXPECT_CALL(*item, GetState()).WillRepeatedly(Return(DownloadItem::COMPLETE));
617 item->UpdateObservers();
629 } 618 }
630 619
631 TEST_F(DownloadPathReservationTrackerTest, TruncationConflict) { 620 TEST_F(DownloadPathReservationTrackerTest, TruncationConflict) {
632 int real_max_length = 621 int real_max_length =
633 base::GetMaximumPathComponentLength(default_download_path()); 622 base::GetMaximumPathComponentLength(default_download_path());
634 ASSERT_NE(-1, real_max_length); 623 ASSERT_NE(-1, real_max_length);
635 const size_t max_length = real_max_length - 11; 624 const size_t max_length = real_max_length - 11;
636 625
637 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); 626 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
638 base::FilePath path(GetLongNamePathInDownloadsDirectory( 627 base::FilePath path(GetLongNamePathInDownloadsDirectory(
639 max_length, FILE_PATH_LITERAL(".txt"))); 628 max_length, FILE_PATH_LITERAL(".txt")));
640 base::FilePath path0(GetLongNamePathInDownloadsDirectory( 629 base::FilePath path0(GetLongNamePathInDownloadsDirectory(
641 max_length - 4, FILE_PATH_LITERAL(".txt"))); 630 max_length - 4, FILE_PATH_LITERAL(".txt")));
642 base::FilePath path1(GetLongNamePathInDownloadsDirectory( 631 base::FilePath path1(GetLongNamePathInDownloadsDirectory(
643 max_length - 8, FILE_PATH_LITERAL(" (1).txt"))); 632 max_length - 8, FILE_PATH_LITERAL(" (1).txt")));
644 base::FilePath path2(GetLongNamePathInDownloadsDirectory( 633 base::FilePath path2(GetLongNamePathInDownloadsDirectory(
645 max_length - 8, FILE_PATH_LITERAL(" (2).txt"))); 634 max_length - 8, FILE_PATH_LITERAL(" (2).txt")));
646 ASSERT_FALSE(IsPathInUse(path)); 635 ASSERT_FALSE(IsPathInUse(path));
647 // "aaa...aaaaaaa.txt" (truncated path) and 636 // "aaa...aaaaaaa.txt" (truncated path) and
(...skipping 10 matching lines...) Expand all
658 CallGetReservedPath( 647 CallGetReservedPath(
659 item.get(), 648 item.get(),
660 path, 649 path,
661 create_directory, 650 create_directory,
662 conflict_action, 651 conflict_action,
663 &reserved_path, 652 &reserved_path,
664 &verified); 653 &verified);
665 EXPECT_TRUE(IsPathInUse(reserved_path)); 654 EXPECT_TRUE(IsPathInUse(reserved_path));
666 EXPECT_TRUE(verified); 655 EXPECT_TRUE(verified);
667 EXPECT_EQ(path2, reserved_path); 656 EXPECT_EQ(path2, reserved_path);
668 item->SetState(DownloadItem::COMPLETE); 657 EXPECT_CALL(*item, GetState()).WillRepeatedly(Return(DownloadItem::COMPLETE));
658 item->UpdateObservers();
669 } 659 }
670 660
671 TEST_F(DownloadPathReservationTrackerTest, TruncationFail) { 661 TEST_F(DownloadPathReservationTrackerTest, TruncationFail) {
672 int real_max_length = 662 int real_max_length =
673 base::GetMaximumPathComponentLength(default_download_path()); 663 base::GetMaximumPathComponentLength(default_download_path());
674 ASSERT_NE(-1, real_max_length); 664 ASSERT_NE(-1, real_max_length);
675 const size_t max_length = real_max_length - 11; 665 const size_t max_length = real_max_length - 11;
676 666
677 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); 667 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
678 base::FilePath path(GetPathInDownloadsDirectory( 668 base::FilePath path(GetPathInDownloadsDirectory(
679 (FILE_PATH_LITERAL("a.") + 669 (FILE_PATH_LITERAL("a.") +
680 base::FilePath::StringType(max_length, 'b')).c_str())); 670 base::FilePath::StringType(max_length, 'b')).c_str()));
681 ASSERT_FALSE(IsPathInUse(path)); 671 ASSERT_FALSE(IsPathInUse(path));
682 672
683 base::FilePath reserved_path; 673 base::FilePath reserved_path;
684 bool verified = false; 674 bool verified = false;
685 DownloadPathReservationTracker::FilenameConflictAction conflict_action = 675 DownloadPathReservationTracker::FilenameConflictAction conflict_action =
686 DownloadPathReservationTracker::OVERWRITE; 676 DownloadPathReservationTracker::OVERWRITE;
687 bool create_directory = false; 677 bool create_directory = false;
688 CallGetReservedPath( 678 CallGetReservedPath(
689 item.get(), 679 item.get(),
690 path, 680 path,
691 create_directory, 681 create_directory,
692 conflict_action, 682 conflict_action,
693 &reserved_path, 683 &reserved_path,
694 &verified); 684 &verified);
695 // We cannot truncate a path with very long extension. 685 // We cannot truncate a path with very long extension.
696 EXPECT_FALSE(verified); 686 EXPECT_FALSE(verified);
697 item->SetState(DownloadItem::COMPLETE); 687 EXPECT_CALL(*item, GetState()).WillRepeatedly(Return(DownloadItem::COMPLETE));
688 item->UpdateObservers();
698 } 689 }
699 690
700 #endif 691 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698