1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/base_paths.h"
8 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/path_service.h"
12 #include "base/strings/string_util.h"
13 #include "chrome/installer/util/create_dir_work_item.h"
14 #include "chrome/installer/util/work_item.h"
15 #include "testing/gtest/include/gtest/gtest.h"
18 class CreateDirWorkItemTest
: public testing::Test
{
20 virtual void SetUp() {
21 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
24 base::ScopedTempDir temp_dir_
;
28 TEST_F(CreateDirWorkItemTest
, CreatePath
) {
29 base::FilePath
parent_dir(temp_dir_
.path());
30 parent_dir
= parent_dir
.AppendASCII("a");
31 base::CreateDirectory(parent_dir
);
32 ASSERT_TRUE(base::PathExists(parent_dir
));
34 base::FilePath
top_dir_to_create(parent_dir
);
35 top_dir_to_create
= top_dir_to_create
.AppendASCII("b");
37 base::FilePath
dir_to_create(top_dir_to_create
);
38 dir_to_create
= dir_to_create
.AppendASCII("c");
39 dir_to_create
= dir_to_create
.AppendASCII("d");
41 scoped_ptr
<CreateDirWorkItem
> work_item(
42 WorkItem::CreateCreateDirWorkItem(dir_to_create
));
44 EXPECT_TRUE(work_item
->Do());
46 EXPECT_TRUE(base::PathExists(dir_to_create
));
48 work_item
->Rollback();
50 // Rollback should delete all the paths up to top_dir_to_create.
51 EXPECT_FALSE(base::PathExists(top_dir_to_create
));
52 EXPECT_TRUE(base::PathExists(parent_dir
));
55 TEST_F(CreateDirWorkItemTest
, CreateExistingPath
) {
56 base::FilePath
dir_to_create(temp_dir_
.path());
57 dir_to_create
= dir_to_create
.AppendASCII("aa");
58 base::CreateDirectory(dir_to_create
);
59 ASSERT_TRUE(base::PathExists(dir_to_create
));
61 scoped_ptr
<CreateDirWorkItem
> work_item(
62 WorkItem::CreateCreateDirWorkItem(dir_to_create
));
64 EXPECT_TRUE(work_item
->Do());
66 EXPECT_TRUE(base::PathExists(dir_to_create
));
68 work_item
->Rollback();
70 // Rollback should not remove the path since it exists before
71 // the CreateDirWorkItem is called.
72 EXPECT_TRUE(base::PathExists(dir_to_create
));
75 TEST_F(CreateDirWorkItemTest
, CreateSharedPath
) {
76 base::FilePath
dir_to_create_1(temp_dir_
.path());
77 dir_to_create_1
= dir_to_create_1
.AppendASCII("aaa");
79 base::FilePath
dir_to_create_2(dir_to_create_1
);
80 dir_to_create_2
= dir_to_create_2
.AppendASCII("bbb");
82 base::FilePath
dir_to_create_3(dir_to_create_2
);
83 dir_to_create_3
= dir_to_create_3
.AppendASCII("ccc");
85 scoped_ptr
<CreateDirWorkItem
> work_item(
86 WorkItem::CreateCreateDirWorkItem(dir_to_create_3
));
88 EXPECT_TRUE(work_item
->Do());
90 EXPECT_TRUE(base::PathExists(dir_to_create_3
));
92 // Create another directory under dir_to_create_2
93 base::FilePath
dir_to_create_4(dir_to_create_2
);
94 dir_to_create_4
= dir_to_create_4
.AppendASCII("ddd");
95 base::CreateDirectory(dir_to_create_4
);
96 ASSERT_TRUE(base::PathExists(dir_to_create_4
));
98 work_item
->Rollback();
100 // Rollback should delete dir_to_create_3.
101 EXPECT_FALSE(base::PathExists(dir_to_create_3
));
103 // Rollback should not delete dir_to_create_2 as it is shared.
104 EXPECT_TRUE(base::PathExists(dir_to_create_2
));
105 EXPECT_TRUE(base::PathExists(dir_to_create_4
));
108 TEST_F(CreateDirWorkItemTest
, RollbackWithMissingDir
) {
109 base::FilePath
dir_to_create_1(temp_dir_
.path());
110 dir_to_create_1
= dir_to_create_1
.AppendASCII("aaaa");
112 base::FilePath
dir_to_create_2(dir_to_create_1
);
113 dir_to_create_2
= dir_to_create_2
.AppendASCII("bbbb");
115 base::FilePath
dir_to_create_3(dir_to_create_2
);
116 dir_to_create_3
= dir_to_create_3
.AppendASCII("cccc");
118 scoped_ptr
<CreateDirWorkItem
> work_item(
119 WorkItem::CreateCreateDirWorkItem(dir_to_create_3
));
121 EXPECT_TRUE(work_item
->Do());
123 EXPECT_TRUE(base::PathExists(dir_to_create_3
));
125 RemoveDirectory(dir_to_create_3
.value().c_str());
126 ASSERT_FALSE(base::PathExists(dir_to_create_3
));
128 work_item
->Rollback();
130 // dir_to_create_3 has already been deleted, Rollback should delete
132 EXPECT_FALSE(base::PathExists(dir_to_create_1
));