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

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

Issue 12212010: Truncate the download file name if it exceeds the filesystem limit. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add test. Created 7 years, 10 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_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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 bool uniquify_path, 79 bool uniquify_path,
80 base::FilePath* return_path, 80 base::FilePath* return_path,
81 bool* return_verified); 81 bool* return_verified);
82 82
83 const base::FilePath& default_download_path() const { 83 const base::FilePath& default_download_path() const {
84 return default_download_path_; 84 return default_download_path_;
85 } 85 }
86 void set_default_download_path(const base::FilePath& path) { 86 void set_default_download_path(const base::FilePath& path) {
87 default_download_path_ = path; 87 default_download_path_ = path;
88 } 88 }
89 // Creates a name of form 'a'*repeat + suffix
90 base::FilePath GetLongNamePathInDownloadsDirectory(
91 size_t repeat, const base::FilePath::CharType* suffix);
89 92
90 protected: 93 protected:
91 base::ScopedTempDir test_download_dir_; 94 base::ScopedTempDir test_download_dir_;
92 base::FilePath default_download_path_; 95 base::FilePath default_download_path_;
93 MessageLoopForUI message_loop_; 96 MessageLoopForUI message_loop_;
94 content::TestBrowserThread ui_thread_; 97 content::TestBrowserThread ui_thread_;
95 content::TestBrowserThread file_thread_; 98 content::TestBrowserThread file_thread_;
96 99
97 private: 100 private:
98 void TestReservedPathCallback(base::FilePath* return_path, 101 void TestReservedPathCallback(base::FilePath* return_path,
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 } 159 }
157 160
158 void DownloadPathReservationTrackerTest::TestReservedPathCallback( 161 void DownloadPathReservationTrackerTest::TestReservedPathCallback(
159 base::FilePath* return_path, bool* return_verified, bool* did_run_callback, 162 base::FilePath* return_path, bool* return_verified, bool* did_run_callback,
160 const base::FilePath& path, bool verified) { 163 const base::FilePath& path, bool verified) {
161 *did_run_callback = true; 164 *did_run_callback = true;
162 *return_path = path; 165 *return_path = path;
163 *return_verified = verified; 166 *return_verified = verified;
164 } 167 }
165 168
169 base::FilePath
170 DownloadPathReservationTrackerTest::GetLongNamePathInDownloadsDirectory(
171 size_t repeat, const base::FilePath::CharType* suffix) {
172 return GetPathInDownloadsDirectory(
173 (base::FilePath::StringType(repeat, FILE_PATH_LITERAL('a'))
174 + suffix).c_str());
175 }
176
166 } // namespace 177 } // namespace
167 178
168 // A basic reservation is acquired and committed. 179 // A basic reservation is acquired and committed.
169 TEST_F(DownloadPathReservationTrackerTest, BasicReservation) { 180 TEST_F(DownloadPathReservationTrackerTest, BasicReservation) {
170 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1)); 181 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
171 base::FilePath path( 182 base::FilePath path(
172 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt"))); 183 GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
173 ASSERT_FALSE(IsPathInUse(path)); 184 ASSERT_FALSE(IsPathInUse(path));
174 185
175 base::FilePath reserved_path; 186 base::FilePath reserved_path;
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 item->UpdateObservers(); 443 item->UpdateObservers();
433 message_loop_.RunUntilIdle(); 444 message_loop_.RunUntilIdle();
434 EXPECT_FALSE(IsPathInUse(path)); 445 EXPECT_FALSE(IsPathInUse(path));
435 EXPECT_TRUE(IsPathInUse(new_target_path)); 446 EXPECT_TRUE(IsPathInUse(new_target_path));
436 447
437 // Destroying the item should release the reservation. 448 // Destroying the item should release the reservation.
438 item.reset(); 449 item.reset();
439 message_loop_.RunUntilIdle(); 450 message_loop_.RunUntilIdle();
440 EXPECT_FALSE(IsPathInUse(new_target_path)); 451 EXPECT_FALSE(IsPathInUse(new_target_path));
441 } 452 }
453
454 // Tests for long name truncation. On other platforms automatic truncation
455 // is not performed (yet).
456 #if defined(OS_WIN) || defined(OS_MAC) || defined(OS_CHROMEOS)
Nico 2013/03/13 23:25:00 It's OS_MACOSX ( https://codereview.chromium.org/1
457
458 TEST_F(DownloadPathReservationTrackerTest, BasicTruncation) {
459 int real_max_length =
460 file_util::GetMaximumPathComponentLength(default_download_path());
461 ASSERT_NE(-1, real_max_length);
462
463 // TODO(kinaba): the current implementation leaves spaces for appending
464 // ".crdownload". So take it into account. Should be removed in the future.
465 const size_t max_length = real_max_length - 11;
466
467 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
468 base::FilePath path(GetLongNamePathInDownloadsDirectory(
469 max_length, FILE_PATH_LITERAL(".txt")));
470 ASSERT_FALSE(IsPathInUse(path));
471
472 base::FilePath reserved_path;
473 bool verified = false;
474 CallGetReservedPath(*item, path, false, &reserved_path, &verified);
475 EXPECT_TRUE(IsPathInUse(reserved_path));
476 EXPECT_TRUE(verified);
477 // The file name length is truncated to max_length.
478 EXPECT_EQ(max_length, reserved_path.BaseName().value().size());
479 // But the extension is kept unchanged.
480 EXPECT_EQ(path.Extension(), reserved_path.Extension());
481 }
482
483 TEST_F(DownloadPathReservationTrackerTest, TruncationConflict) {
484 int real_max_length =
485 file_util::GetMaximumPathComponentLength(default_download_path());
486 ASSERT_NE(-1, real_max_length);
487 const size_t max_length = real_max_length - 11;
488
489 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
490 base::FilePath path(GetLongNamePathInDownloadsDirectory(
491 max_length, FILE_PATH_LITERAL(".txt")));
492 base::FilePath path0(GetLongNamePathInDownloadsDirectory(
493 max_length - 4, FILE_PATH_LITERAL(".txt")));
494 base::FilePath path1(GetLongNamePathInDownloadsDirectory(
495 max_length - 8, FILE_PATH_LITERAL(" (1).txt")));
496 base::FilePath path2(GetLongNamePathInDownloadsDirectory(
497 max_length - 8, FILE_PATH_LITERAL(" (2).txt")));
498 ASSERT_FALSE(IsPathInUse(path));
499 // "aaa...aaaaaaa.txt" (truncated path) and
500 // "aaa...aaa (1).txt" (truncated and first uniquification try) exists.
501 // "aaa...aaa (2).txt" should be used.
502 ASSERT_EQ(0, file_util::WriteFile(path0, "", 0));
503 ASSERT_EQ(0, file_util::WriteFile(path1, "", 0));
504
505 base::FilePath reserved_path;
506 bool verified = false;
507 CallGetReservedPath(*item, path, true, &reserved_path, &verified);
508 EXPECT_TRUE(IsPathInUse(reserved_path));
509 EXPECT_TRUE(verified);
510 EXPECT_EQ(path2, reserved_path);
511 }
512
513 TEST_F(DownloadPathReservationTrackerTest, TruncationFail) {
514 int real_max_length =
515 file_util::GetMaximumPathComponentLength(default_download_path());
516 ASSERT_NE(-1, real_max_length);
517 const size_t max_length = real_max_length - 11;
518
519 scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
520 base::FilePath path(GetPathInDownloadsDirectory(
521 (FILE_PATH_LITERAL("a.") +
522 base::FilePath::StringType(max_length, 'b')).c_str()));
523 ASSERT_FALSE(IsPathInUse(path));
524
525 base::FilePath reserved_path;
526 bool verified = false;
527 CallGetReservedPath(*item, path, false, &reserved_path, &verified);
528 // We cannot truncate a path with very long extension.
529 EXPECT_FALSE(verified);
530 }
531
532 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698