OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/installer/util/create_dir_work_item.h" |
| 6 |
5 #include <windows.h> | 7 #include <windows.h> |
6 | 8 |
| 9 #include <memory> |
| 10 |
7 #include "base/base_paths.h" | 11 #include "base/base_paths.h" |
8 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
12 #include "chrome/installer/util/create_dir_work_item.h" | |
13 #include "chrome/installer/util/work_item.h" | 15 #include "chrome/installer/util/work_item.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
15 | 17 |
16 namespace { | 18 namespace { |
17 class CreateDirWorkItemTest : public testing::Test { | 19 class CreateDirWorkItemTest : public testing::Test { |
18 protected: | 20 protected: |
19 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } | 21 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } |
20 | 22 |
21 base::ScopedTempDir temp_dir_; | 23 base::ScopedTempDir temp_dir_; |
22 }; | 24 }; |
23 }; | 25 }; |
24 | 26 |
25 TEST_F(CreateDirWorkItemTest, CreatePath) { | 27 TEST_F(CreateDirWorkItemTest, CreatePath) { |
26 base::FilePath parent_dir(temp_dir_.path()); | 28 base::FilePath parent_dir(temp_dir_.path()); |
27 parent_dir = parent_dir.AppendASCII("a"); | 29 parent_dir = parent_dir.AppendASCII("a"); |
28 base::CreateDirectory(parent_dir); | 30 base::CreateDirectory(parent_dir); |
29 ASSERT_TRUE(base::PathExists(parent_dir)); | 31 ASSERT_TRUE(base::PathExists(parent_dir)); |
30 | 32 |
31 base::FilePath top_dir_to_create(parent_dir); | 33 base::FilePath top_dir_to_create(parent_dir); |
32 top_dir_to_create = top_dir_to_create.AppendASCII("b"); | 34 top_dir_to_create = top_dir_to_create.AppendASCII("b"); |
33 | 35 |
34 base::FilePath dir_to_create(top_dir_to_create); | 36 base::FilePath dir_to_create(top_dir_to_create); |
35 dir_to_create = dir_to_create.AppendASCII("c"); | 37 dir_to_create = dir_to_create.AppendASCII("c"); |
36 dir_to_create = dir_to_create.AppendASCII("d"); | 38 dir_to_create = dir_to_create.AppendASCII("d"); |
37 | 39 |
38 scoped_ptr<CreateDirWorkItem> work_item( | 40 std::unique_ptr<CreateDirWorkItem> work_item( |
39 WorkItem::CreateCreateDirWorkItem(dir_to_create)); | 41 WorkItem::CreateCreateDirWorkItem(dir_to_create)); |
40 | 42 |
41 EXPECT_TRUE(work_item->Do()); | 43 EXPECT_TRUE(work_item->Do()); |
42 | 44 |
43 EXPECT_TRUE(base::PathExists(dir_to_create)); | 45 EXPECT_TRUE(base::PathExists(dir_to_create)); |
44 | 46 |
45 work_item->Rollback(); | 47 work_item->Rollback(); |
46 | 48 |
47 // Rollback should delete all the paths up to top_dir_to_create. | 49 // Rollback should delete all the paths up to top_dir_to_create. |
48 EXPECT_FALSE(base::PathExists(top_dir_to_create)); | 50 EXPECT_FALSE(base::PathExists(top_dir_to_create)); |
49 EXPECT_TRUE(base::PathExists(parent_dir)); | 51 EXPECT_TRUE(base::PathExists(parent_dir)); |
50 } | 52 } |
51 | 53 |
52 TEST_F(CreateDirWorkItemTest, CreateExistingPath) { | 54 TEST_F(CreateDirWorkItemTest, CreateExistingPath) { |
53 base::FilePath dir_to_create(temp_dir_.path()); | 55 base::FilePath dir_to_create(temp_dir_.path()); |
54 dir_to_create = dir_to_create.AppendASCII("aa"); | 56 dir_to_create = dir_to_create.AppendASCII("aa"); |
55 base::CreateDirectory(dir_to_create); | 57 base::CreateDirectory(dir_to_create); |
56 ASSERT_TRUE(base::PathExists(dir_to_create)); | 58 ASSERT_TRUE(base::PathExists(dir_to_create)); |
57 | 59 |
58 scoped_ptr<CreateDirWorkItem> work_item( | 60 std::unique_ptr<CreateDirWorkItem> work_item( |
59 WorkItem::CreateCreateDirWorkItem(dir_to_create)); | 61 WorkItem::CreateCreateDirWorkItem(dir_to_create)); |
60 | 62 |
61 EXPECT_TRUE(work_item->Do()); | 63 EXPECT_TRUE(work_item->Do()); |
62 | 64 |
63 EXPECT_TRUE(base::PathExists(dir_to_create)); | 65 EXPECT_TRUE(base::PathExists(dir_to_create)); |
64 | 66 |
65 work_item->Rollback(); | 67 work_item->Rollback(); |
66 | 68 |
67 // Rollback should not remove the path since it exists before | 69 // Rollback should not remove the path since it exists before |
68 // the CreateDirWorkItem is called. | 70 // the CreateDirWorkItem is called. |
69 EXPECT_TRUE(base::PathExists(dir_to_create)); | 71 EXPECT_TRUE(base::PathExists(dir_to_create)); |
70 } | 72 } |
71 | 73 |
72 TEST_F(CreateDirWorkItemTest, CreateSharedPath) { | 74 TEST_F(CreateDirWorkItemTest, CreateSharedPath) { |
73 base::FilePath dir_to_create_1(temp_dir_.path()); | 75 base::FilePath dir_to_create_1(temp_dir_.path()); |
74 dir_to_create_1 = dir_to_create_1.AppendASCII("aaa"); | 76 dir_to_create_1 = dir_to_create_1.AppendASCII("aaa"); |
75 | 77 |
76 base::FilePath dir_to_create_2(dir_to_create_1); | 78 base::FilePath dir_to_create_2(dir_to_create_1); |
77 dir_to_create_2 = dir_to_create_2.AppendASCII("bbb"); | 79 dir_to_create_2 = dir_to_create_2.AppendASCII("bbb"); |
78 | 80 |
79 base::FilePath dir_to_create_3(dir_to_create_2); | 81 base::FilePath dir_to_create_3(dir_to_create_2); |
80 dir_to_create_3 = dir_to_create_3.AppendASCII("ccc"); | 82 dir_to_create_3 = dir_to_create_3.AppendASCII("ccc"); |
81 | 83 |
82 scoped_ptr<CreateDirWorkItem> work_item( | 84 std::unique_ptr<CreateDirWorkItem> work_item( |
83 WorkItem::CreateCreateDirWorkItem(dir_to_create_3)); | 85 WorkItem::CreateCreateDirWorkItem(dir_to_create_3)); |
84 | 86 |
85 EXPECT_TRUE(work_item->Do()); | 87 EXPECT_TRUE(work_item->Do()); |
86 | 88 |
87 EXPECT_TRUE(base::PathExists(dir_to_create_3)); | 89 EXPECT_TRUE(base::PathExists(dir_to_create_3)); |
88 | 90 |
89 // Create another directory under dir_to_create_2 | 91 // Create another directory under dir_to_create_2 |
90 base::FilePath dir_to_create_4(dir_to_create_2); | 92 base::FilePath dir_to_create_4(dir_to_create_2); |
91 dir_to_create_4 = dir_to_create_4.AppendASCII("ddd"); | 93 dir_to_create_4 = dir_to_create_4.AppendASCII("ddd"); |
92 base::CreateDirectory(dir_to_create_4); | 94 base::CreateDirectory(dir_to_create_4); |
(...skipping 12 matching lines...) Expand all Loading... |
105 TEST_F(CreateDirWorkItemTest, RollbackWithMissingDir) { | 107 TEST_F(CreateDirWorkItemTest, RollbackWithMissingDir) { |
106 base::FilePath dir_to_create_1(temp_dir_.path()); | 108 base::FilePath dir_to_create_1(temp_dir_.path()); |
107 dir_to_create_1 = dir_to_create_1.AppendASCII("aaaa"); | 109 dir_to_create_1 = dir_to_create_1.AppendASCII("aaaa"); |
108 | 110 |
109 base::FilePath dir_to_create_2(dir_to_create_1); | 111 base::FilePath dir_to_create_2(dir_to_create_1); |
110 dir_to_create_2 = dir_to_create_2.AppendASCII("bbbb"); | 112 dir_to_create_2 = dir_to_create_2.AppendASCII("bbbb"); |
111 | 113 |
112 base::FilePath dir_to_create_3(dir_to_create_2); | 114 base::FilePath dir_to_create_3(dir_to_create_2); |
113 dir_to_create_3 = dir_to_create_3.AppendASCII("cccc"); | 115 dir_to_create_3 = dir_to_create_3.AppendASCII("cccc"); |
114 | 116 |
115 scoped_ptr<CreateDirWorkItem> work_item( | 117 std::unique_ptr<CreateDirWorkItem> work_item( |
116 WorkItem::CreateCreateDirWorkItem(dir_to_create_3)); | 118 WorkItem::CreateCreateDirWorkItem(dir_to_create_3)); |
117 | 119 |
118 EXPECT_TRUE(work_item->Do()); | 120 EXPECT_TRUE(work_item->Do()); |
119 | 121 |
120 EXPECT_TRUE(base::PathExists(dir_to_create_3)); | 122 EXPECT_TRUE(base::PathExists(dir_to_create_3)); |
121 | 123 |
122 RemoveDirectory(dir_to_create_3.value().c_str()); | 124 RemoveDirectory(dir_to_create_3.value().c_str()); |
123 ASSERT_FALSE(base::PathExists(dir_to_create_3)); | 125 ASSERT_FALSE(base::PathExists(dir_to_create_3)); |
124 | 126 |
125 work_item->Rollback(); | 127 work_item->Rollback(); |
126 | 128 |
127 // dir_to_create_3 has already been deleted, Rollback should delete | 129 // dir_to_create_3 has already been deleted, Rollback should delete |
128 // the rest. | 130 // the rest. |
129 EXPECT_FALSE(base::PathExists(dir_to_create_1)); | 131 EXPECT_FALSE(base::PathExists(dir_to_create_1)); |
130 } | 132 } |
OLD | NEW |