// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/file_util.h"
#include "base/files/file_path.h"
+#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
namespace {
-// MockDownloadItem with real observers and state.
-class FakeDownloadItem : public MockDownloadItem {
- public:
- explicit FakeDownloadItem()
- : state_(IN_PROGRESS) {
- }
- virtual ~FakeDownloadItem() {
- FOR_EACH_OBSERVER(Observer, observers_, OnDownloadDestroyed(this));
- EXPECT_FALSE(observers_.might_have_observers());
- }
- virtual void AddObserver(Observer* observer) OVERRIDE {
- observers_.AddObserver(observer);
- }
- virtual void RemoveObserver(Observer* observer) OVERRIDE {
- observers_.RemoveObserver(observer);
- }
- virtual void UpdateObservers() OVERRIDE {
- FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this));
- }
-
- virtual DownloadState GetState() const OVERRIDE {
- return state_;
- }
-
- void SetState(DownloadState state) {
- state_ = state;
- UpdateObservers();
- }
-
- private:
- DownloadState state_;
- ObserverList<Observer> observers_;
-};
-
class DownloadPathReservationTrackerTest : public testing::Test {
public:
DownloadPathReservationTrackerTest();
// testing::Test
- virtual void SetUp() OVERRIDE;
- virtual void TearDown() OVERRIDE;
+ void SetUp() override;
+ void TearDown() override;
- FakeDownloadItem* CreateDownloadItem(int32 id);
+ MockDownloadItem* CreateDownloadItem(int32 id);
base::FilePath GetPathInDownloadsDirectory(
const base::FilePath::CharType* suffix);
bool IsPathInUse(const base::FilePath& path);
message_loop_.RunUntilIdle();
}
-FakeDownloadItem* DownloadPathReservationTrackerTest::CreateDownloadItem(
+MockDownloadItem* DownloadPathReservationTrackerTest::CreateDownloadItem(
int32 id) {
- FakeDownloadItem* item = new ::testing::StrictMock<FakeDownloadItem>;
+ MockDownloadItem* item = new ::testing::StrictMock<MockDownloadItem>;
EXPECT_CALL(*item, GetId())
.WillRepeatedly(Return(id));
EXPECT_CALL(*item, GetTargetFilePath())
.WillRepeatedly(ReturnRefOfCopy(base::FilePath()));
+ EXPECT_CALL(*item, GetState())
+ .WillRepeatedly(Return(DownloadItem::IN_PROGRESS));
return item;
}
+ suffix).c_str());
}
+void SetDownloadItemState(content::MockDownloadItem* download_item,
+ content::DownloadItem::DownloadState state) {
+ EXPECT_CALL(*download_item, GetState())
+ .WillRepeatedly(Return(state));
+ download_item->NotifyObserversDownloadUpdated();
+}
+
} // namespace
// A basic reservation is acquired and committed.
TEST_F(DownloadPathReservationTrackerTest, BasicReservation) {
- scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
+ scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
ASSERT_FALSE(IsPathInUse(path));
EXPECT_EQ(path.value(), reserved_path.value());
// Destroying the item should release the reservation.
- item->SetState(DownloadItem::COMPLETE);
+ SetDownloadItemState(item.get(), DownloadItem::COMPLETE);
item.reset();
message_loop_.RunUntilIdle();
EXPECT_FALSE(IsPathInUse(path));
// A download that is interrupted should lose its reservation.
TEST_F(DownloadPathReservationTrackerTest, InterruptedDownload) {
- scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
+ scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
ASSERT_FALSE(IsPathInUse(path));
EXPECT_EQ(path.value(), reserved_path.value());
// Once the download is interrupted, the path should become available again.
- item->SetState(DownloadItem::INTERRUPTED);
+ SetDownloadItemState(item.get(), DownloadItem::INTERRUPTED);
message_loop_.RunUntilIdle();
EXPECT_FALSE(IsPathInUse(path));
}
// A completed download should also lose its reservation.
TEST_F(DownloadPathReservationTrackerTest, CompleteDownload) {
- scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
+ scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
ASSERT_FALSE(IsPathInUse(path));
// real download, at this point only the path reservation will be released.
// The path wouldn't be available since it is occupied on disk by the
// completed download.
- item->SetState(DownloadItem::COMPLETE);
+ SetDownloadItemState(item.get(), DownloadItem::COMPLETE);
message_loop_.RunUntilIdle();
EXPECT_FALSE(IsPathInUse(path));
}
// If there are files on the file system, a unique reservation should uniquify
// around it.
TEST_F(DownloadPathReservationTrackerTest, ConflictingFiles) {
- scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
+ scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
base::FilePath path1(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt")).value(),
reserved_path.value());
- item->SetState(DownloadItem::COMPLETE);
+ SetDownloadItemState(item.get(), DownloadItem::COMPLETE);
item.reset();
message_loop_.RunUntilIdle();
EXPECT_TRUE(IsPathInUse(path));
// Multiple reservations for the same path should uniquify around each other.
TEST_F(DownloadPathReservationTrackerTest, ConflictingReservations) {
- scoped_ptr<FakeDownloadItem> item1(CreateDownloadItem(1));
+ scoped_ptr<MockDownloadItem> item1(CreateDownloadItem(1));
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
base::FilePath uniquified_path(
{
// Requesting a reservation for the same path with uniquification results in
// a uniquified path.
- scoped_ptr<FakeDownloadItem> item2(CreateDownloadItem(2));
+ scoped_ptr<MockDownloadItem> item2(CreateDownloadItem(2));
base::FilePath reserved_path2;
CallGetReservedPath(
item2.get(),
EXPECT_TRUE(IsPathInUse(path));
EXPECT_TRUE(IsPathInUse(uniquified_path));
EXPECT_EQ(uniquified_path.value(), reserved_path2.value());
- item2->SetState(DownloadItem::COMPLETE);
+ SetDownloadItemState(item2.get(), DownloadItem::COMPLETE);
}
message_loop_.RunUntilIdle();
EXPECT_TRUE(IsPathInUse(path));
{
// Since the previous download item was removed, requesting a reservation
// for the same path should result in the same uniquified path.
- scoped_ptr<FakeDownloadItem> item2(CreateDownloadItem(2));
+ scoped_ptr<MockDownloadItem> item2(CreateDownloadItem(2));
base::FilePath reserved_path2;
CallGetReservedPath(
item2.get(),
EXPECT_TRUE(IsPathInUse(path));
EXPECT_TRUE(IsPathInUse(uniquified_path));
EXPECT_EQ(uniquified_path.value(), reserved_path2.value());
- item2->SetState(DownloadItem::COMPLETE);
+ SetDownloadItemState(item2.get(), DownloadItem::COMPLETE);
}
message_loop_.RunUntilIdle();
// Now acquire an overwriting reservation. We should end up with the same
// non-uniquified path for both reservations.
- scoped_ptr<FakeDownloadItem> item3(CreateDownloadItem(2));
+ scoped_ptr<MockDownloadItem> item3(CreateDownloadItem(2));
base::FilePath reserved_path3;
conflict_action = DownloadPathReservationTracker::OVERWRITE;
CallGetReservedPath(
EXPECT_EQ(path.value(), reserved_path1.value());
EXPECT_EQ(path.value(), reserved_path3.value());
- item1->SetState(DownloadItem::COMPLETE);
- item3->SetState(DownloadItem::COMPLETE);
+ SetDownloadItemState(item1.get(), DownloadItem::COMPLETE);
+ SetDownloadItemState(item3.get(), DownloadItem::COMPLETE);
}
// If a unique path cannot be determined after trying kMaxUniqueFiles
TEST_F(DownloadPathReservationTrackerTest, UnresolvedConflicts) {
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
- scoped_ptr<FakeDownloadItem> items[
- DownloadPathReservationTracker::kMaxUniqueFiles + 1];
+ scoped_ptr<MockDownloadItem>
+ items[DownloadPathReservationTracker::kMaxUniqueFiles + 1];
DownloadPathReservationTracker::FilenameConflictAction conflict_action =
DownloadPathReservationTracker::UNIQUIFY;
bool create_directory = false;
EXPECT_TRUE(verified);
}
// The next reservation for |path| will fail to be unique.
- scoped_ptr<FakeDownloadItem> item(
+ scoped_ptr<MockDownloadItem> item(
CreateDownloadItem(DownloadPathReservationTracker::kMaxUniqueFiles + 1));
base::FilePath reserved_path;
bool verified = true;
EXPECT_FALSE(verified);
EXPECT_EQ(path.value(), reserved_path.value());
- item->SetState(DownloadItem::COMPLETE);
- for (int i = 0; i <= DownloadPathReservationTracker::kMaxUniqueFiles; i++) {
- items[i]->SetState(DownloadItem::COMPLETE);
- }
+ SetDownloadItemState(item.get(), DownloadItem::COMPLETE);
+ for (int i = 0; i <= DownloadPathReservationTracker::kMaxUniqueFiles; i++)
+ SetDownloadItemState(items[i].get(), DownloadItem::COMPLETE);
}
// If the target directory is unwriteable, then callback should be notified that
// verification failed.
TEST_F(DownloadPathReservationTrackerTest, UnwriteableDirectory) {
- scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
+ scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
base::FilePath dir(path.DirName());
ASSERT_FALSE(IsPathInUse(path));
{
- // Scope for PermissionRestorer
- file_util::PermissionRestorer restorer(dir);
- EXPECT_TRUE(file_util::MakeFileUnwritable(dir));
+ // Scope for FilePermissionRestorer
+ base::FilePermissionRestorer restorer(dir);
+ EXPECT_TRUE(base::MakeFileUnwritable(dir));
base::FilePath reserved_path;
bool verified = true;
DownloadPathReservationTracker::FilenameConflictAction conflict_action =
EXPECT_FALSE(verified);
EXPECT_EQ(path.BaseName().value(), reserved_path.BaseName().value());
}
- item->SetState(DownloadItem::COMPLETE);
+ SetDownloadItemState(item.get(), DownloadItem::COMPLETE);
}
// If the default download directory doesn't exist, then it should be
bool create_directory = false;
{
- scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
+ scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
base::FilePath reserved_path;
bool verified = true;
CallGetReservedPath(
&verified);
// Verification fails because the directory doesn't exist.
EXPECT_FALSE(verified);
- item->SetState(DownloadItem::COMPLETE);
+ SetDownloadItemState(item.get(), DownloadItem::COMPLETE);
}
ASSERT_FALSE(IsPathInUse(path));
{
- scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
+ scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
base::FilePath reserved_path;
bool verified = true;
set_default_download_path(dir);
// Verification succeeds because the directory is created.
EXPECT_TRUE(verified);
EXPECT_TRUE(base::DirectoryExists(dir));
- item->SetState(DownloadItem::COMPLETE);
+ SetDownloadItemState(item.get(), DownloadItem::COMPLETE);
}
}
// If the target path of the download item changes, the reservation should be
// updated to match.
TEST_F(DownloadPathReservationTrackerTest, UpdatesToTargetPath) {
- scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
+ scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
ASSERT_FALSE(IsPathInUse(path));
// The target path is initially empty. If an OnDownloadUpdated() is issued in
// this state, we shouldn't lose the reservation.
ASSERT_EQ(base::FilePath::StringType(), item->GetTargetFilePath().value());
- item->UpdateObservers();
+ item->NotifyObserversDownloadUpdated();
message_loop_.RunUntilIdle();
EXPECT_TRUE(IsPathInUse(path));
ASSERT_FALSE(IsPathInUse(new_target_path));
EXPECT_CALL(*item, GetTargetFilePath())
.WillRepeatedly(ReturnRef(new_target_path));
- item->UpdateObservers();
+ item->NotifyObserversDownloadUpdated();
message_loop_.RunUntilIdle();
EXPECT_FALSE(IsPathInUse(path));
EXPECT_TRUE(IsPathInUse(new_target_path));
// Destroying the item should release the reservation.
- item->SetState(DownloadItem::COMPLETE);
+ SetDownloadItemState(item.get(), DownloadItem::COMPLETE);
item.reset();
message_loop_.RunUntilIdle();
EXPECT_FALSE(IsPathInUse(new_target_path));
// ".crdownload". So take it into account. Should be removed in the future.
const size_t max_length = real_max_length - 11;
- scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
+ scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(GetLongNamePathInDownloadsDirectory(
max_length, FILE_PATH_LITERAL(".txt")));
ASSERT_FALSE(IsPathInUse(path));
EXPECT_EQ(max_length, reserved_path.BaseName().value().size());
// But the extension is kept unchanged.
EXPECT_EQ(path.Extension(), reserved_path.Extension());
- item->SetState(DownloadItem::COMPLETE);
+ SetDownloadItemState(item.get(), DownloadItem::COMPLETE);
}
TEST_F(DownloadPathReservationTrackerTest, TruncationConflict) {
ASSERT_NE(-1, real_max_length);
const size_t max_length = real_max_length - 11;
- scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
+ scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(GetLongNamePathInDownloadsDirectory(
max_length, FILE_PATH_LITERAL(".txt")));
base::FilePath path0(GetLongNamePathInDownloadsDirectory(
EXPECT_TRUE(IsPathInUse(reserved_path));
EXPECT_TRUE(verified);
EXPECT_EQ(path2, reserved_path);
- item->SetState(DownloadItem::COMPLETE);
+ SetDownloadItemState(item.get(), DownloadItem::COMPLETE);
}
TEST_F(DownloadPathReservationTrackerTest, TruncationFail) {
ASSERT_NE(-1, real_max_length);
const size_t max_length = real_max_length - 11;
- scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
+ scoped_ptr<MockDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(GetPathInDownloadsDirectory(
(FILE_PATH_LITERAL("a.") +
base::FilePath::StringType(max_length, 'b')).c_str()));
&verified);
// We cannot truncate a path with very long extension.
EXPECT_FALSE(verified);
- item->SetState(DownloadItem::COMPLETE);
+ SetDownloadItemState(item.get(), DownloadItem::COMPLETE);
}
#endif