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/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string_util.h"
12 #include "chrome/installer/util/create_dir_work_item.h"
13 #include "chrome/installer/util/work_item.h"
14 #include "testing/gtest/include/gtest/gtest.h"
17 class CreateDirWorkItemTest
: public testing::Test
{
19 virtual void SetUp() {
20 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
23 base::ScopedTempDir temp_dir_
;
27 TEST_F(CreateDirWorkItemTest
, CreatePath
) {
28 base::FilePath
parent_dir(temp_dir_
.path());
29 parent_dir
= parent_dir
.AppendASCII("a");
30 base::CreateDirectory(parent_dir
);
31 ASSERT_TRUE(base::PathExists(parent_dir
));
33 base::FilePath
top_dir_to_create(parent_dir
);
34 top_dir_to_create
= top_dir_to_create
.AppendASCII("b");
36 base::FilePath
dir_to_create(top_dir_to_create
);
37 dir_to_create
= dir_to_create
.AppendASCII("c");
38 dir_to_create
= dir_to_create
.AppendASCII("d");
40 scoped_ptr
<CreateDirWorkItem
> work_item(
41 WorkItem::CreateCreateDirWorkItem(dir_to_create
));
43 EXPECT_TRUE(work_item
->Do());
45 EXPECT_TRUE(base::PathExists(dir_to_create
));
47 work_item
->Rollback();
49 // Rollback should delete all the paths up to top_dir_to_create.
50 EXPECT_FALSE(base::PathExists(top_dir_to_create
));
51 EXPECT_TRUE(base::PathExists(parent_dir
));
54 TEST_F(CreateDirWorkItemTest
, CreateExistingPath
) {
55 base::FilePath
dir_to_create(temp_dir_
.path());
56 dir_to_create
= dir_to_create
.AppendASCII("aa");
57 base::CreateDirectory(dir_to_create
);
58 ASSERT_TRUE(base::PathExists(dir_to_create
));
60 scoped_ptr
<CreateDirWorkItem
> work_item(
61 WorkItem::CreateCreateDirWorkItem(dir_to_create
));
63 EXPECT_TRUE(work_item
->Do());
65 EXPECT_TRUE(base::PathExists(dir_to_create
));
67 work_item
->Rollback();
69 // Rollback should not remove the path since it exists before
70 // the CreateDirWorkItem is called.
71 EXPECT_TRUE(base::PathExists(dir_to_create
));
74 TEST_F(CreateDirWorkItemTest
, CreateSharedPath
) {
75 base::FilePath
dir_to_create_1(temp_dir_
.path());
76 dir_to_create_1
= dir_to_create_1
.AppendASCII("aaa");
78 base::FilePath
dir_to_create_2(dir_to_create_1
);
79 dir_to_create_2
= dir_to_create_2
.AppendASCII("bbb");
81 base::FilePath
dir_to_create_3(dir_to_create_2
);
82 dir_to_create_3
= dir_to_create_3
.AppendASCII("ccc");
84 scoped_ptr
<CreateDirWorkItem
> work_item(
85 WorkItem::CreateCreateDirWorkItem(dir_to_create_3
));
87 EXPECT_TRUE(work_item
->Do());
89 EXPECT_TRUE(base::PathExists(dir_to_create_3
));
91 // Create another directory under dir_to_create_2
92 base::FilePath
dir_to_create_4(dir_to_create_2
);
93 dir_to_create_4
= dir_to_create_4
.AppendASCII("ddd");
94 base::CreateDirectory(dir_to_create_4
);
95 ASSERT_TRUE(base::PathExists(dir_to_create_4
));
97 work_item
->Rollback();
99 // Rollback should delete dir_to_create_3.
100 EXPECT_FALSE(base::PathExists(dir_to_create_3
));
102 // Rollback should not delete dir_to_create_2 as it is shared.
103 EXPECT_TRUE(base::PathExists(dir_to_create_2
));
104 EXPECT_TRUE(base::PathExists(dir_to_create_4
));
107 TEST_F(CreateDirWorkItemTest
, RollbackWithMissingDir
) {
108 base::FilePath
dir_to_create_1(temp_dir_
.path());
109 dir_to_create_1
= dir_to_create_1
.AppendASCII("aaaa");
111 base::FilePath
dir_to_create_2(dir_to_create_1
);
112 dir_to_create_2
= dir_to_create_2
.AppendASCII("bbbb");
114 base::FilePath
dir_to_create_3(dir_to_create_2
);
115 dir_to_create_3
= dir_to_create_3
.AppendASCII("cccc");
117 scoped_ptr
<CreateDirWorkItem
> work_item(
118 WorkItem::CreateCreateDirWorkItem(dir_to_create_3
));
120 EXPECT_TRUE(work_item
->Do());
122 EXPECT_TRUE(base::PathExists(dir_to_create_3
));
124 RemoveDirectory(dir_to_create_3
.value().c_str());
125 ASSERT_FALSE(base::PathExists(dir_to_create_3
));
127 work_item
->Rollback();
129 // dir_to_create_3 has already been deleted, Rollback should delete
131 EXPECT_FALSE(base::PathExists(dir_to_create_1
));