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_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 Loading... |
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())); |
| 94 EXPECT_CALL(*item, GetState()) |
| 95 .WillRepeatedly(Return(DownloadItem::IN_PROGRESS)); |
128 return item; | 96 return item; |
129 } | 97 } |
130 | 98 |
131 base::FilePath DownloadPathReservationTrackerTest::GetPathInDownloadsDirectory( | 99 base::FilePath DownloadPathReservationTrackerTest::GetPathInDownloadsDirectory( |
132 const base::FilePath::CharType* suffix) { | 100 const base::FilePath::CharType* suffix) { |
133 return default_download_path().Append(suffix).NormalizePathSeparators(); | 101 return default_download_path().Append(suffix).NormalizePathSeparators(); |
134 } | 102 } |
135 | 103 |
136 bool DownloadPathReservationTrackerTest::IsPathInUse( | 104 bool DownloadPathReservationTrackerTest::IsPathInUse( |
137 const base::FilePath& path) { | 105 const base::FilePath& path) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 } | 140 } |
173 | 141 |
174 base::FilePath | 142 base::FilePath |
175 DownloadPathReservationTrackerTest::GetLongNamePathInDownloadsDirectory( | 143 DownloadPathReservationTrackerTest::GetLongNamePathInDownloadsDirectory( |
176 size_t repeat, const base::FilePath::CharType* suffix) { | 144 size_t repeat, const base::FilePath::CharType* suffix) { |
177 return GetPathInDownloadsDirectory( | 145 return GetPathInDownloadsDirectory( |
178 (base::FilePath::StringType(repeat, FILE_PATH_LITERAL('a')) | 146 (base::FilePath::StringType(repeat, FILE_PATH_LITERAL('a')) |
179 + suffix).c_str()); | 147 + suffix).c_str()); |
180 } | 148 } |
181 | 149 |
| 150 void SetDownloadItemState(content::MockDownloadItem* download_item, |
| 151 content::DownloadItem::DownloadState state) { |
| 152 EXPECT_CALL(*download_item, GetState()) |
| 153 .WillRepeatedly(Return(state)); |
| 154 download_item->NotifyObserversDownloadUpdated(); |
| 155 } |
| 156 |
182 } // namespace | 157 } // namespace |
183 | 158 |
184 // A basic reservation is acquired and committed. | 159 // A basic reservation is acquired and committed. |
185 TEST_F(DownloadPathReservationTrackerTest, BasicReservation) { | 160 TEST_F(DownloadPathReservationTrackerTest, BasicReservation) { |
186 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); | 161 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1)); |
187 base::FilePath path( | 162 base::FilePath path( |
188 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); | 163 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); |
189 ASSERT_FALSE(IsPathInUse(path)); | 164 ASSERT_FALSE(IsPathInUse(path)); |
190 | 165 |
191 base::FilePath reserved_path; | 166 base::FilePath reserved_path; |
192 bool verified = false; | 167 bool verified = false; |
193 DownloadPathReservationTracker::FilenameConflictAction conflict_action = | 168 DownloadPathReservationTracker::FilenameConflictAction conflict_action = |
194 DownloadPathReservationTracker::OVERWRITE; | 169 DownloadPathReservationTracker::OVERWRITE; |
195 bool create_directory = false; | 170 bool create_directory = false; |
196 CallGetReservedPath( | 171 CallGetReservedPath( |
197 item.get(), | 172 item.get(), |
198 path, | 173 path, |
199 create_directory, | 174 create_directory, |
200 conflict_action, | 175 conflict_action, |
201 &reserved_path, | 176 &reserved_path, |
202 &verified); | 177 &verified); |
203 EXPECT_TRUE(IsPathInUse(path)); | 178 EXPECT_TRUE(IsPathInUse(path)); |
204 EXPECT_TRUE(verified); | 179 EXPECT_TRUE(verified); |
205 EXPECT_EQ(path.value(), reserved_path.value()); | 180 EXPECT_EQ(path.value(), reserved_path.value()); |
206 | 181 |
207 // Destroying the item should release the reservation. | 182 // Destroying the item should release the reservation. |
208 item->SetState(DownloadItem::COMPLETE); | 183 SetDownloadItemState(item.get(), DownloadItem::COMPLETE); |
209 item.reset(); | 184 item.reset(); |
210 message_loop_.RunUntilIdle(); | 185 message_loop_.RunUntilIdle(); |
211 EXPECT_FALSE(IsPathInUse(path)); | 186 EXPECT_FALSE(IsPathInUse(path)); |
212 } | 187 } |
213 | 188 |
214 // A download that is interrupted should lose its reservation. | 189 // A download that is interrupted should lose its reservation. |
215 TEST_F(DownloadPathReservationTrackerTest, InterruptedDownload) { | 190 TEST_F(DownloadPathReservationTrackerTest, InterruptedDownload) { |
216 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); | 191 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1)); |
217 base::FilePath path( | 192 base::FilePath path( |
218 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); | 193 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); |
219 ASSERT_FALSE(IsPathInUse(path)); | 194 ASSERT_FALSE(IsPathInUse(path)); |
220 | 195 |
221 base::FilePath reserved_path; | 196 base::FilePath reserved_path; |
222 bool verified = false; | 197 bool verified = false; |
223 DownloadPathReservationTracker::FilenameConflictAction conflict_action = | 198 DownloadPathReservationTracker::FilenameConflictAction conflict_action = |
224 DownloadPathReservationTracker::OVERWRITE; | 199 DownloadPathReservationTracker::OVERWRITE; |
225 bool create_directory = false; | 200 bool create_directory = false; |
226 CallGetReservedPath( | 201 CallGetReservedPath( |
227 item.get(), | 202 item.get(), |
228 path, | 203 path, |
229 create_directory, | 204 create_directory, |
230 conflict_action, | 205 conflict_action, |
231 &reserved_path, | 206 &reserved_path, |
232 &verified); | 207 &verified); |
233 EXPECT_TRUE(IsPathInUse(path)); | 208 EXPECT_TRUE(IsPathInUse(path)); |
234 EXPECT_TRUE(verified); | 209 EXPECT_TRUE(verified); |
235 EXPECT_EQ(path.value(), reserved_path.value()); | 210 EXPECT_EQ(path.value(), reserved_path.value()); |
236 | 211 |
237 // Once the download is interrupted, the path should become available again. | 212 // Once the download is interrupted, the path should become available again. |
238 item->SetState(DownloadItem::INTERRUPTED); | 213 SetDownloadItemState(item.get(), DownloadItem::INTERRUPTED); |
239 message_loop_.RunUntilIdle(); | 214 message_loop_.RunUntilIdle(); |
240 EXPECT_FALSE(IsPathInUse(path)); | 215 EXPECT_FALSE(IsPathInUse(path)); |
241 } | 216 } |
242 | 217 |
243 // A completed download should also lose its reservation. | 218 // A completed download should also lose its reservation. |
244 TEST_F(DownloadPathReservationTrackerTest, CompleteDownload) { | 219 TEST_F(DownloadPathReservationTrackerTest, CompleteDownload) { |
245 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); | 220 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1)); |
246 base::FilePath path( | 221 base::FilePath path( |
247 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); | 222 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); |
248 ASSERT_FALSE(IsPathInUse(path)); | 223 ASSERT_FALSE(IsPathInUse(path)); |
249 | 224 |
250 base::FilePath reserved_path; | 225 base::FilePath reserved_path; |
251 bool verified = false; | 226 bool verified = false; |
252 DownloadPathReservationTracker::FilenameConflictAction conflict_action = | 227 DownloadPathReservationTracker::FilenameConflictAction conflict_action = |
253 DownloadPathReservationTracker::OVERWRITE; | 228 DownloadPathReservationTracker::OVERWRITE; |
254 bool create_directory = false; | 229 bool create_directory = false; |
255 CallGetReservedPath( | 230 CallGetReservedPath( |
256 item.get(), | 231 item.get(), |
257 path, | 232 path, |
258 create_directory, | 233 create_directory, |
259 conflict_action, | 234 conflict_action, |
260 &reserved_path, | 235 &reserved_path, |
261 &verified); | 236 &verified); |
262 EXPECT_TRUE(IsPathInUse(path)); | 237 EXPECT_TRUE(IsPathInUse(path)); |
263 EXPECT_TRUE(verified); | 238 EXPECT_TRUE(verified); |
264 EXPECT_EQ(path.value(), reserved_path.value()); | 239 EXPECT_EQ(path.value(), reserved_path.value()); |
265 | 240 |
266 // Once the download completes, the path should become available again. For a | 241 // 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. | 242 // 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 | 243 // The path wouldn't be available since it is occupied on disk by the |
269 // completed download. | 244 // completed download. |
270 item->SetState(DownloadItem::COMPLETE); | 245 SetDownloadItemState(item.get(), DownloadItem::COMPLETE); |
271 message_loop_.RunUntilIdle(); | 246 message_loop_.RunUntilIdle(); |
272 EXPECT_FALSE(IsPathInUse(path)); | 247 EXPECT_FALSE(IsPathInUse(path)); |
273 } | 248 } |
274 | 249 |
275 // If there are files on the file system, a unique reservation should uniquify | 250 // If there are files on the file system, a unique reservation should uniquify |
276 // around it. | 251 // around it. |
277 TEST_F(DownloadPathReservationTrackerTest, ConflictingFiles) { | 252 TEST_F(DownloadPathReservationTrackerTest, ConflictingFiles) { |
278 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); | 253 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1)); |
279 base::FilePath path( | 254 base::FilePath path( |
280 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); | 255 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); |
281 base::FilePath path1( | 256 base::FilePath path1( |
282 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt"))); | 257 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt"))); |
283 // Create a file at |path|, and a .crdownload file at |path1|. | 258 // Create a file at |path|, and a .crdownload file at |path1|. |
284 ASSERT_EQ(0, base::WriteFile(path, "", 0)); | 259 ASSERT_EQ(0, base::WriteFile(path, "", 0)); |
285 ASSERT_EQ(0, | 260 ASSERT_EQ(0, |
286 base::WriteFile( | 261 base::WriteFile( |
287 DownloadTargetDeterminer::GetCrDownloadPath(path1), "", 0)); | 262 DownloadTargetDeterminer::GetCrDownloadPath(path1), "", 0)); |
288 ASSERT_TRUE(IsPathInUse(path)); | 263 ASSERT_TRUE(IsPathInUse(path)); |
(...skipping 12 matching lines...) Expand all Loading... |
301 &verified); | 276 &verified); |
302 EXPECT_TRUE(IsPathInUse(path)); | 277 EXPECT_TRUE(IsPathInUse(path)); |
303 EXPECT_TRUE(IsPathInUse(reserved_path)); | 278 EXPECT_TRUE(IsPathInUse(reserved_path)); |
304 EXPECT_TRUE(verified); | 279 EXPECT_TRUE(verified); |
305 // The path should be uniquified, skipping over foo.txt but not over | 280 // The path should be uniquified, skipping over foo.txt but not over |
306 // "foo (1).txt.crdownload" | 281 // "foo (1).txt.crdownload" |
307 EXPECT_EQ( | 282 EXPECT_EQ( |
308 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt")).value(), | 283 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt")).value(), |
309 reserved_path.value()); | 284 reserved_path.value()); |
310 | 285 |
311 item->SetState(DownloadItem::COMPLETE); | 286 SetDownloadItemState(item.get(), DownloadItem::COMPLETE); |
312 item.reset(); | 287 item.reset(); |
313 message_loop_.RunUntilIdle(); | 288 message_loop_.RunUntilIdle(); |
314 EXPECT_TRUE(IsPathInUse(path)); | 289 EXPECT_TRUE(IsPathInUse(path)); |
315 EXPECT_FALSE(IsPathInUse(reserved_path)); | 290 EXPECT_FALSE(IsPathInUse(reserved_path)); |
316 } | 291 } |
317 | 292 |
318 // Multiple reservations for the same path should uniquify around each other. | 293 // Multiple reservations for the same path should uniquify around each other. |
319 TEST_F(DownloadPathReservationTrackerTest, ConflictingReservations) { | 294 TEST_F(DownloadPathReservationTrackerTest, ConflictingReservations) { |
320 scoped_ptr<FakeDownloadItem> item1(CreateDownloadItem(1)); | 295 scoped_ptr<MockDownloadItem> item1(CreateDownloadItem(1)); |
321 base::FilePath path( | 296 base::FilePath path( |
322 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); | 297 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); |
323 base::FilePath uniquified_path( | 298 base::FilePath uniquified_path( |
324 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt"))); | 299 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt"))); |
325 ASSERT_FALSE(IsPathInUse(path)); | 300 ASSERT_FALSE(IsPathInUse(path)); |
326 ASSERT_FALSE(IsPathInUse(uniquified_path)); | 301 ASSERT_FALSE(IsPathInUse(uniquified_path)); |
327 | 302 |
328 base::FilePath reserved_path1; | 303 base::FilePath reserved_path1; |
329 bool verified = false; | 304 bool verified = false; |
330 bool create_directory = false; | 305 bool create_directory = false; |
331 | 306 |
332 DownloadPathReservationTracker::FilenameConflictAction conflict_action = | 307 DownloadPathReservationTracker::FilenameConflictAction conflict_action = |
333 DownloadPathReservationTracker::UNIQUIFY; | 308 DownloadPathReservationTracker::UNIQUIFY; |
334 CallGetReservedPath( | 309 CallGetReservedPath( |
335 item1.get(), | 310 item1.get(), |
336 path, | 311 path, |
337 create_directory, | 312 create_directory, |
338 conflict_action, | 313 conflict_action, |
339 &reserved_path1, | 314 &reserved_path1, |
340 &verified); | 315 &verified); |
341 EXPECT_TRUE(IsPathInUse(path)); | 316 EXPECT_TRUE(IsPathInUse(path)); |
342 EXPECT_TRUE(verified); | 317 EXPECT_TRUE(verified); |
343 | 318 |
344 | 319 |
345 { | 320 { |
346 // Requesting a reservation for the same path with uniquification results in | 321 // Requesting a reservation for the same path with uniquification results in |
347 // a uniquified path. | 322 // a uniquified path. |
348 scoped_ptr<FakeDownloadItem> item2(CreateDownloadItem(2)); | 323 scoped_ptr<MockDownloadItem> item2(CreateDownloadItem(2)); |
349 base::FilePath reserved_path2; | 324 base::FilePath reserved_path2; |
350 CallGetReservedPath( | 325 CallGetReservedPath( |
351 item2.get(), | 326 item2.get(), |
352 path, | 327 path, |
353 create_directory, | 328 create_directory, |
354 conflict_action, | 329 conflict_action, |
355 &reserved_path2, | 330 &reserved_path2, |
356 &verified); | 331 &verified); |
357 EXPECT_TRUE(IsPathInUse(path)); | 332 EXPECT_TRUE(IsPathInUse(path)); |
358 EXPECT_TRUE(IsPathInUse(uniquified_path)); | 333 EXPECT_TRUE(IsPathInUse(uniquified_path)); |
359 EXPECT_EQ(uniquified_path.value(), reserved_path2.value()); | 334 EXPECT_EQ(uniquified_path.value(), reserved_path2.value()); |
360 item2->SetState(DownloadItem::COMPLETE); | 335 SetDownloadItemState(item2.get(), DownloadItem::COMPLETE); |
361 } | 336 } |
362 message_loop_.RunUntilIdle(); | 337 message_loop_.RunUntilIdle(); |
363 EXPECT_TRUE(IsPathInUse(path)); | 338 EXPECT_TRUE(IsPathInUse(path)); |
364 EXPECT_FALSE(IsPathInUse(uniquified_path)); | 339 EXPECT_FALSE(IsPathInUse(uniquified_path)); |
365 | 340 |
366 { | 341 { |
367 // Since the previous download item was removed, requesting a reservation | 342 // Since the previous download item was removed, requesting a reservation |
368 // for the same path should result in the same uniquified path. | 343 // for the same path should result in the same uniquified path. |
369 scoped_ptr<FakeDownloadItem> item2(CreateDownloadItem(2)); | 344 scoped_ptr<MockDownloadItem> item2(CreateDownloadItem(2)); |
370 base::FilePath reserved_path2; | 345 base::FilePath reserved_path2; |
371 CallGetReservedPath( | 346 CallGetReservedPath( |
372 item2.get(), | 347 item2.get(), |
373 path, | 348 path, |
374 create_directory, | 349 create_directory, |
375 conflict_action, | 350 conflict_action, |
376 &reserved_path2, | 351 &reserved_path2, |
377 &verified); | 352 &verified); |
378 EXPECT_TRUE(IsPathInUse(path)); | 353 EXPECT_TRUE(IsPathInUse(path)); |
379 EXPECT_TRUE(IsPathInUse(uniquified_path)); | 354 EXPECT_TRUE(IsPathInUse(uniquified_path)); |
380 EXPECT_EQ(uniquified_path.value(), reserved_path2.value()); | 355 EXPECT_EQ(uniquified_path.value(), reserved_path2.value()); |
381 item2->SetState(DownloadItem::COMPLETE); | 356 SetDownloadItemState(item2.get(), DownloadItem::COMPLETE); |
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 SetDownloadItemState(item1.get(), DownloadItem::COMPLETE); |
404 item3->SetState(DownloadItem::COMPLETE); | 379 SetDownloadItemState(item3.get(), DownloadItem::COMPLETE); |
405 } | 380 } |
406 | 381 |
407 // If a unique path cannot be determined after trying kMaxUniqueFiles | 382 // If a unique path cannot be determined after trying kMaxUniqueFiles |
408 // uniquifiers, then the callback should notified that verification failed, and | 383 // uniquifiers, then the callback should notified that verification failed, and |
409 // the returned path should be set to the original requested path. | 384 // the returned path should be set to the original requested path. |
410 TEST_F(DownloadPathReservationTrackerTest, UnresolvedConflicts) { | 385 TEST_F(DownloadPathReservationTrackerTest, UnresolvedConflicts) { |
411 base::FilePath path( | 386 base::FilePath path( |
412 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); | 387 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); |
413 scoped_ptr<FakeDownloadItem> items[ | 388 scoped_ptr<MockDownloadItem> |
414 DownloadPathReservationTracker::kMaxUniqueFiles + 1]; | 389 items[DownloadPathReservationTracker::kMaxUniqueFiles + 1]; |
415 DownloadPathReservationTracker::FilenameConflictAction conflict_action = | 390 DownloadPathReservationTracker::FilenameConflictAction conflict_action = |
416 DownloadPathReservationTracker::UNIQUIFY; | 391 DownloadPathReservationTracker::UNIQUIFY; |
417 bool create_directory = false; | 392 bool create_directory = false; |
418 | 393 |
419 // Create |kMaxUniqueFiles + 1| reservations for |path|. The first reservation | 394 // Create |kMaxUniqueFiles + 1| reservations for |path|. The first reservation |
420 // will have no uniquifier. The |kMaxUniqueFiles| remaining reservations do. | 395 // will have no uniquifier. The |kMaxUniqueFiles| remaining reservations do. |
421 for (int i = 0; i <= DownloadPathReservationTracker::kMaxUniqueFiles; i++) { | 396 for (int i = 0; i <= DownloadPathReservationTracker::kMaxUniqueFiles; i++) { |
422 base::FilePath reserved_path; | 397 base::FilePath reserved_path; |
423 base::FilePath expected_path; | 398 base::FilePath expected_path; |
424 bool verified = false; | 399 bool verified = false; |
(...skipping 10 matching lines...) Expand all Loading... |
435 path, | 410 path, |
436 create_directory, | 411 create_directory, |
437 conflict_action, | 412 conflict_action, |
438 &reserved_path, | 413 &reserved_path, |
439 &verified); | 414 &verified); |
440 EXPECT_TRUE(IsPathInUse(expected_path)); | 415 EXPECT_TRUE(IsPathInUse(expected_path)); |
441 EXPECT_EQ(expected_path.value(), reserved_path.value()); | 416 EXPECT_EQ(expected_path.value(), reserved_path.value()); |
442 EXPECT_TRUE(verified); | 417 EXPECT_TRUE(verified); |
443 } | 418 } |
444 // The next reservation for |path| will fail to be unique. | 419 // The next reservation for |path| will fail to be unique. |
445 scoped_ptr<FakeDownloadItem> item( | 420 scoped_ptr<MockDownloadItem> item( |
446 CreateDownloadItem(DownloadPathReservationTracker::kMaxUniqueFiles + 1)); | 421 CreateDownloadItem(DownloadPathReservationTracker::kMaxUniqueFiles + 1)); |
447 base::FilePath reserved_path; | 422 base::FilePath reserved_path; |
448 bool verified = true; | 423 bool verified = true; |
449 CallGetReservedPath( | 424 CallGetReservedPath( |
450 item.get(), | 425 item.get(), |
451 path, | 426 path, |
452 create_directory, | 427 create_directory, |
453 conflict_action, | 428 conflict_action, |
454 &reserved_path, | 429 &reserved_path, |
455 &verified); | 430 &verified); |
456 EXPECT_FALSE(verified); | 431 EXPECT_FALSE(verified); |
457 EXPECT_EQ(path.value(), reserved_path.value()); | 432 EXPECT_EQ(path.value(), reserved_path.value()); |
458 | 433 |
459 item->SetState(DownloadItem::COMPLETE); | 434 SetDownloadItemState(item.get(), DownloadItem::COMPLETE); |
460 for (int i = 0; i <= DownloadPathReservationTracker::kMaxUniqueFiles; i++) { | 435 for (int i = 0; i <= DownloadPathReservationTracker::kMaxUniqueFiles; i++) |
461 items[i]->SetState(DownloadItem::COMPLETE); | 436 SetDownloadItemState(items[i].get(), DownloadItem::COMPLETE); |
462 } | |
463 } | 437 } |
464 | 438 |
465 // If the target directory is unwriteable, then callback should be notified that | 439 // If the target directory is unwriteable, then callback should be notified that |
466 // verification failed. | 440 // verification failed. |
467 TEST_F(DownloadPathReservationTrackerTest, UnwriteableDirectory) { | 441 TEST_F(DownloadPathReservationTrackerTest, UnwriteableDirectory) { |
468 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); | 442 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1)); |
469 base::FilePath path( | 443 base::FilePath path( |
470 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); | 444 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); |
471 base::FilePath dir(path.DirName()); | 445 base::FilePath dir(path.DirName()); |
472 ASSERT_FALSE(IsPathInUse(path)); | 446 ASSERT_FALSE(IsPathInUse(path)); |
473 | 447 |
474 { | 448 { |
475 // Scope for PermissionRestorer | 449 // Scope for PermissionRestorer |
476 file_util::PermissionRestorer restorer(dir); | 450 file_util::PermissionRestorer restorer(dir); |
477 EXPECT_TRUE(file_util::MakeFileUnwritable(dir)); | 451 EXPECT_TRUE(file_util::MakeFileUnwritable(dir)); |
478 base::FilePath reserved_path; | 452 base::FilePath reserved_path; |
479 bool verified = true; | 453 bool verified = true; |
480 DownloadPathReservationTracker::FilenameConflictAction conflict_action = | 454 DownloadPathReservationTracker::FilenameConflictAction conflict_action = |
481 DownloadPathReservationTracker::OVERWRITE; | 455 DownloadPathReservationTracker::OVERWRITE; |
482 bool create_directory = false; | 456 bool create_directory = false; |
483 CallGetReservedPath( | 457 CallGetReservedPath( |
484 item.get(), | 458 item.get(), |
485 path, | 459 path, |
486 create_directory, | 460 create_directory, |
487 conflict_action, | 461 conflict_action, |
488 &reserved_path, | 462 &reserved_path, |
489 &verified); | 463 &verified); |
490 // Verification fails. | 464 // Verification fails. |
491 EXPECT_FALSE(verified); | 465 EXPECT_FALSE(verified); |
492 EXPECT_EQ(path.BaseName().value(), reserved_path.BaseName().value()); | 466 EXPECT_EQ(path.BaseName().value(), reserved_path.BaseName().value()); |
493 } | 467 } |
494 item->SetState(DownloadItem::COMPLETE); | 468 SetDownloadItemState(item.get(), DownloadItem::COMPLETE); |
495 } | 469 } |
496 | 470 |
497 // If the default download directory doesn't exist, then it should be | 471 // 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. | 472 // created. But only if we are actually going to create the download path there. |
499 TEST_F(DownloadPathReservationTrackerTest, CreateDefaultDownloadPath) { | 473 TEST_F(DownloadPathReservationTrackerTest, CreateDefaultDownloadPath) { |
500 base::FilePath path( | 474 base::FilePath path( |
501 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo/foo.txt"))); | 475 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo/foo.txt"))); |
502 base::FilePath dir(path.DirName()); | 476 base::FilePath dir(path.DirName()); |
503 ASSERT_FALSE(base::DirectoryExists(dir)); | 477 ASSERT_FALSE(base::DirectoryExists(dir)); |
504 DownloadPathReservationTracker::FilenameConflictAction conflict_action = | 478 DownloadPathReservationTracker::FilenameConflictAction conflict_action = |
505 DownloadPathReservationTracker::OVERWRITE; | 479 DownloadPathReservationTracker::OVERWRITE; |
506 bool create_directory = false; | 480 bool create_directory = false; |
507 | 481 |
508 { | 482 { |
509 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); | 483 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1)); |
510 base::FilePath reserved_path; | 484 base::FilePath reserved_path; |
511 bool verified = true; | 485 bool verified = true; |
512 CallGetReservedPath( | 486 CallGetReservedPath( |
513 item.get(), | 487 item.get(), |
514 path, | 488 path, |
515 create_directory, | 489 create_directory, |
516 conflict_action, | 490 conflict_action, |
517 &reserved_path, | 491 &reserved_path, |
518 &verified); | 492 &verified); |
519 // Verification fails because the directory doesn't exist. | 493 // Verification fails because the directory doesn't exist. |
520 EXPECT_FALSE(verified); | 494 EXPECT_FALSE(verified); |
521 item->SetState(DownloadItem::COMPLETE); | 495 SetDownloadItemState(item.get(), DownloadItem::COMPLETE); |
522 } | 496 } |
523 ASSERT_FALSE(IsPathInUse(path)); | 497 ASSERT_FALSE(IsPathInUse(path)); |
524 { | 498 { |
525 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); | 499 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1)); |
526 base::FilePath reserved_path; | 500 base::FilePath reserved_path; |
527 bool verified = true; | 501 bool verified = true; |
528 set_default_download_path(dir); | 502 set_default_download_path(dir); |
529 CallGetReservedPath( | 503 CallGetReservedPath( |
530 item.get(), | 504 item.get(), |
531 path, | 505 path, |
532 create_directory, | 506 create_directory, |
533 conflict_action, | 507 conflict_action, |
534 &reserved_path, | 508 &reserved_path, |
535 &verified); | 509 &verified); |
536 // Verification succeeds because the directory is created. | 510 // Verification succeeds because the directory is created. |
537 EXPECT_TRUE(verified); | 511 EXPECT_TRUE(verified); |
538 EXPECT_TRUE(base::DirectoryExists(dir)); | 512 EXPECT_TRUE(base::DirectoryExists(dir)); |
539 item->SetState(DownloadItem::COMPLETE); | 513 SetDownloadItemState(item.get(), DownloadItem::COMPLETE); |
540 } | 514 } |
541 } | 515 } |
542 | 516 |
543 // If the target path of the download item changes, the reservation should be | 517 // If the target path of the download item changes, the reservation should be |
544 // updated to match. | 518 // updated to match. |
545 TEST_F(DownloadPathReservationTrackerTest, UpdatesToTargetPath) { | 519 TEST_F(DownloadPathReservationTrackerTest, UpdatesToTargetPath) { |
546 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); | 520 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1)); |
547 base::FilePath path( | 521 base::FilePath path( |
548 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); | 522 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); |
549 ASSERT_FALSE(IsPathInUse(path)); | 523 ASSERT_FALSE(IsPathInUse(path)); |
550 | 524 |
551 base::FilePath reserved_path; | 525 base::FilePath reserved_path; |
552 bool verified = false; | 526 bool verified = false; |
553 DownloadPathReservationTracker::FilenameConflictAction conflict_action = | 527 DownloadPathReservationTracker::FilenameConflictAction conflict_action = |
554 DownloadPathReservationTracker::OVERWRITE; | 528 DownloadPathReservationTracker::OVERWRITE; |
555 bool create_directory = false; | 529 bool create_directory = false; |
556 CallGetReservedPath( | 530 CallGetReservedPath( |
557 item.get(), | 531 item.get(), |
558 path, | 532 path, |
559 create_directory, | 533 create_directory, |
560 conflict_action, | 534 conflict_action, |
561 &reserved_path, | 535 &reserved_path, |
562 &verified); | 536 &verified); |
563 EXPECT_TRUE(IsPathInUse(path)); | 537 EXPECT_TRUE(IsPathInUse(path)); |
564 EXPECT_TRUE(verified); | 538 EXPECT_TRUE(verified); |
565 EXPECT_EQ(path.value(), reserved_path.value()); | 539 EXPECT_EQ(path.value(), reserved_path.value()); |
566 | 540 |
567 // The target path is initially empty. If an OnDownloadUpdated() is issued in | 541 // The target path is initially empty. If an OnDownloadUpdated() is issued in |
568 // this state, we shouldn't lose the reservation. | 542 // this state, we shouldn't lose the reservation. |
569 ASSERT_EQ(base::FilePath::StringType(), item->GetTargetFilePath().value()); | 543 ASSERT_EQ(base::FilePath::StringType(), item->GetTargetFilePath().value()); |
570 item->UpdateObservers(); | 544 item->NotifyObserversDownloadUpdated(); |
571 message_loop_.RunUntilIdle(); | 545 message_loop_.RunUntilIdle(); |
572 EXPECT_TRUE(IsPathInUse(path)); | 546 EXPECT_TRUE(IsPathInUse(path)); |
573 | 547 |
574 // If the target path changes, we should update the reservation to match. | 548 // If the target path changes, we should update the reservation to match. |
575 base::FilePath new_target_path( | 549 base::FilePath new_target_path( |
576 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("bar.txt"))); | 550 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("bar.txt"))); |
577 ASSERT_FALSE(IsPathInUse(new_target_path)); | 551 ASSERT_FALSE(IsPathInUse(new_target_path)); |
578 EXPECT_CALL(*item, GetTargetFilePath()) | 552 EXPECT_CALL(*item, GetTargetFilePath()) |
579 .WillRepeatedly(ReturnRef(new_target_path)); | 553 .WillRepeatedly(ReturnRef(new_target_path)); |
580 item->UpdateObservers(); | 554 item->NotifyObserversDownloadUpdated(); |
581 message_loop_.RunUntilIdle(); | 555 message_loop_.RunUntilIdle(); |
582 EXPECT_FALSE(IsPathInUse(path)); | 556 EXPECT_FALSE(IsPathInUse(path)); |
583 EXPECT_TRUE(IsPathInUse(new_target_path)); | 557 EXPECT_TRUE(IsPathInUse(new_target_path)); |
584 | 558 |
585 // Destroying the item should release the reservation. | 559 // Destroying the item should release the reservation. |
586 item->SetState(DownloadItem::COMPLETE); | 560 SetDownloadItemState(item.get(), DownloadItem::COMPLETE); |
587 item.reset(); | 561 item.reset(); |
588 message_loop_.RunUntilIdle(); | 562 message_loop_.RunUntilIdle(); |
589 EXPECT_FALSE(IsPathInUse(new_target_path)); | 563 EXPECT_FALSE(IsPathInUse(new_target_path)); |
590 } | 564 } |
591 | 565 |
592 // Tests for long name truncation. On other platforms automatic truncation | 566 // Tests for long name truncation. On other platforms automatic truncation |
593 // is not performed (yet). | 567 // is not performed (yet). |
594 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) | 568 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) |
595 | 569 |
596 TEST_F(DownloadPathReservationTrackerTest, BasicTruncation) { | 570 TEST_F(DownloadPathReservationTrackerTest, BasicTruncation) { |
597 int real_max_length = | 571 int real_max_length = |
598 base::GetMaximumPathComponentLength(default_download_path()); | 572 base::GetMaximumPathComponentLength(default_download_path()); |
599 ASSERT_NE(-1, real_max_length); | 573 ASSERT_NE(-1, real_max_length); |
600 | 574 |
601 // TODO(kinaba): the current implementation leaves spaces for appending | 575 // TODO(kinaba): the current implementation leaves spaces for appending |
602 // ".crdownload". So take it into account. Should be removed in the future. | 576 // ".crdownload". So take it into account. Should be removed in the future. |
603 const size_t max_length = real_max_length - 11; | 577 const size_t max_length = real_max_length - 11; |
604 | 578 |
605 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); | 579 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1)); |
606 base::FilePath path(GetLongNamePathInDownloadsDirectory( | 580 base::FilePath path(GetLongNamePathInDownloadsDirectory( |
607 max_length, FILE_PATH_LITERAL(".txt"))); | 581 max_length, FILE_PATH_LITERAL(".txt"))); |
608 ASSERT_FALSE(IsPathInUse(path)); | 582 ASSERT_FALSE(IsPathInUse(path)); |
609 | 583 |
610 base::FilePath reserved_path; | 584 base::FilePath reserved_path; |
611 bool verified = false; | 585 bool verified = false; |
612 DownloadPathReservationTracker::FilenameConflictAction conflict_action = | 586 DownloadPathReservationTracker::FilenameConflictAction conflict_action = |
613 DownloadPathReservationTracker::OVERWRITE; | 587 DownloadPathReservationTracker::OVERWRITE; |
614 bool create_directory = false; | 588 bool create_directory = false; |
615 CallGetReservedPath( | 589 CallGetReservedPath( |
616 item.get(), | 590 item.get(), |
617 path, | 591 path, |
618 create_directory, | 592 create_directory, |
619 conflict_action, | 593 conflict_action, |
620 &reserved_path, | 594 &reserved_path, |
621 &verified); | 595 &verified); |
622 EXPECT_TRUE(IsPathInUse(reserved_path)); | 596 EXPECT_TRUE(IsPathInUse(reserved_path)); |
623 EXPECT_TRUE(verified); | 597 EXPECT_TRUE(verified); |
624 // The file name length is truncated to max_length. | 598 // The file name length is truncated to max_length. |
625 EXPECT_EQ(max_length, reserved_path.BaseName().value().size()); | 599 EXPECT_EQ(max_length, reserved_path.BaseName().value().size()); |
626 // But the extension is kept unchanged. | 600 // But the extension is kept unchanged. |
627 EXPECT_EQ(path.Extension(), reserved_path.Extension()); | 601 EXPECT_EQ(path.Extension(), reserved_path.Extension()); |
628 item->SetState(DownloadItem::COMPLETE); | 602 SetDownloadItemState(item.get(), DownloadItem::COMPLETE); |
629 } | 603 } |
630 | 604 |
631 TEST_F(DownloadPathReservationTrackerTest, TruncationConflict) { | 605 TEST_F(DownloadPathReservationTrackerTest, TruncationConflict) { |
632 int real_max_length = | 606 int real_max_length = |
633 base::GetMaximumPathComponentLength(default_download_path()); | 607 base::GetMaximumPathComponentLength(default_download_path()); |
634 ASSERT_NE(-1, real_max_length); | 608 ASSERT_NE(-1, real_max_length); |
635 const size_t max_length = real_max_length - 11; | 609 const size_t max_length = real_max_length - 11; |
636 | 610 |
637 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); | 611 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1)); |
638 base::FilePath path(GetLongNamePathInDownloadsDirectory( | 612 base::FilePath path(GetLongNamePathInDownloadsDirectory( |
639 max_length, FILE_PATH_LITERAL(".txt"))); | 613 max_length, FILE_PATH_LITERAL(".txt"))); |
640 base::FilePath path0(GetLongNamePathInDownloadsDirectory( | 614 base::FilePath path0(GetLongNamePathInDownloadsDirectory( |
641 max_length - 4, FILE_PATH_LITERAL(".txt"))); | 615 max_length - 4, FILE_PATH_LITERAL(".txt"))); |
642 base::FilePath path1(GetLongNamePathInDownloadsDirectory( | 616 base::FilePath path1(GetLongNamePathInDownloadsDirectory( |
643 max_length - 8, FILE_PATH_LITERAL(" (1).txt"))); | 617 max_length - 8, FILE_PATH_LITERAL(" (1).txt"))); |
644 base::FilePath path2(GetLongNamePathInDownloadsDirectory( | 618 base::FilePath path2(GetLongNamePathInDownloadsDirectory( |
645 max_length - 8, FILE_PATH_LITERAL(" (2).txt"))); | 619 max_length - 8, FILE_PATH_LITERAL(" (2).txt"))); |
646 ASSERT_FALSE(IsPathInUse(path)); | 620 ASSERT_FALSE(IsPathInUse(path)); |
647 // "aaa...aaaaaaa.txt" (truncated path) and | 621 // "aaa...aaaaaaa.txt" (truncated path) and |
(...skipping 10 matching lines...) Expand all Loading... |
658 CallGetReservedPath( | 632 CallGetReservedPath( |
659 item.get(), | 633 item.get(), |
660 path, | 634 path, |
661 create_directory, | 635 create_directory, |
662 conflict_action, | 636 conflict_action, |
663 &reserved_path, | 637 &reserved_path, |
664 &verified); | 638 &verified); |
665 EXPECT_TRUE(IsPathInUse(reserved_path)); | 639 EXPECT_TRUE(IsPathInUse(reserved_path)); |
666 EXPECT_TRUE(verified); | 640 EXPECT_TRUE(verified); |
667 EXPECT_EQ(path2, reserved_path); | 641 EXPECT_EQ(path2, reserved_path); |
668 item->SetState(DownloadItem::COMPLETE); | 642 SetDownloadItemState(item.get(), DownloadItem::COMPLETE); |
669 } | 643 } |
670 | 644 |
671 TEST_F(DownloadPathReservationTrackerTest, TruncationFail) { | 645 TEST_F(DownloadPathReservationTrackerTest, TruncationFail) { |
672 int real_max_length = | 646 int real_max_length = |
673 base::GetMaximumPathComponentLength(default_download_path()); | 647 base::GetMaximumPathComponentLength(default_download_path()); |
674 ASSERT_NE(-1, real_max_length); | 648 ASSERT_NE(-1, real_max_length); |
675 const size_t max_length = real_max_length - 11; | 649 const size_t max_length = real_max_length - 11; |
676 | 650 |
677 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); | 651 scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1)); |
678 base::FilePath path(GetPathInDownloadsDirectory( | 652 base::FilePath path(GetPathInDownloadsDirectory( |
679 (FILE_PATH_LITERAL("a.") + | 653 (FILE_PATH_LITERAL("a.") + |
680 base::FilePath::StringType(max_length, 'b')).c_str())); | 654 base::FilePath::StringType(max_length, 'b')).c_str())); |
681 ASSERT_FALSE(IsPathInUse(path)); | 655 ASSERT_FALSE(IsPathInUse(path)); |
682 | 656 |
683 base::FilePath reserved_path; | 657 base::FilePath reserved_path; |
684 bool verified = false; | 658 bool verified = false; |
685 DownloadPathReservationTracker::FilenameConflictAction conflict_action = | 659 DownloadPathReservationTracker::FilenameConflictAction conflict_action = |
686 DownloadPathReservationTracker::OVERWRITE; | 660 DownloadPathReservationTracker::OVERWRITE; |
687 bool create_directory = false; | 661 bool create_directory = false; |
688 CallGetReservedPath( | 662 CallGetReservedPath( |
689 item.get(), | 663 item.get(), |
690 path, | 664 path, |
691 create_directory, | 665 create_directory, |
692 conflict_action, | 666 conflict_action, |
693 &reserved_path, | 667 &reserved_path, |
694 &verified); | 668 &verified); |
695 // We cannot truncate a path with very long extension. | 669 // We cannot truncate a path with very long extension. |
696 EXPECT_FALSE(verified); | 670 EXPECT_FALSE(verified); |
697 item->SetState(DownloadItem::COMPLETE); | 671 SetDownloadItemState(item.get(), DownloadItem::COMPLETE); |
698 } | 672 } |
699 | 673 |
700 #endif | 674 #endif |
OLD | NEW |