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

Side by Side Diff: chrome/installer/util/work_item_list_unittest.cc

Issue 1869383003: Simplify WorkItemListTest. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: self review Created 4 years, 8 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <windows.h> 5 #include "chrome/installer/util/work_item_list.h"
6 6
7 #include "base/base_paths.h" 7 #include <memory>
8 #include "base/files/file_util.h" 8
9 #include "base/files/file_path.h"
9 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_ptr.h" 11 #include "base/macros.h"
11 #include "base/strings/string_util.h"
12 #include "base/test/test_reg_util_win.h"
13 #include "base/win/registry.h"
14 #include "chrome/installer/util/conditional_work_item_list.h" 12 #include "chrome/installer/util/conditional_work_item_list.h"
15 #include "chrome/installer/util/work_item.h" 13 #include "chrome/installer/util/work_item.h"
16 #include "chrome/installer/util/work_item_list.h" 14 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
18 16
19 using base::win::RegKey;
20
21 namespace { 17 namespace {
22 18
23 const wchar_t kTestRoot[] = L"ListList"; 19 class MockWorkItem : public WorkItem {
24 const wchar_t kDataStr[] = L"data_111"; 20 public:
25 const wchar_t kName[] = L"name"; 21 MockWorkItem() = default;
26 22
27 class WorkItemListTest : public testing::Test { 23 MOCK_METHOD0(Do, bool());
28 protected: 24 MOCK_METHOD0(Rollback, void());
29 void SetUp() override {
30 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
31 registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER);
32 }
33 25
34 void TearDown() override { logging::CloseLogFile(); } 26 private:
27 DISALLOW_COPY_AND_ASSIGN(MockWorkItem);
28 };
35 29
36 base::ScopedTempDir temp_dir_; 30 class AlwaysTrueCondition : public WorkItem::Condition {
grt (UTC plus 2) 2016/04/13 18:08:51 why not use a Mock for this so that the trueness a
fdoray 2016/04/13 18:57:16 Done.
37 registry_util::RegistryOverrideManager registry_override_manager_; 31 public:
32 bool ShouldRun() const override { return true; }
38 }; 33 };
39 34
35 class AlwaysFalseCondition : public WorkItem::Condition {
36 public:
37 bool ShouldRun() const override { return false; }
38 };
39
40 using StrictMockWorkItem = testing::StrictMock<MockWorkItem>;
41 using testing::InSequence;
42 using testing::Return;
43
40 } // namespace 44 } // namespace
41 45
42 // Execute a WorkItem list successfully and then rollback. 46 // Execute a WorkItemList successfully and then rollback.
43 TEST_F(WorkItemListTest, ExecutionSuccess) { 47 TEST(WorkItemListTest, ExecutionSuccess) {
44 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); 48 std::unique_ptr<WorkItemList> list(WorkItem::CreateWorkItemList());
45 scoped_ptr<WorkItem> work_item;
46 49
47 base::FilePath top_dir_to_create(temp_dir_.path()); 50 // Create the mock work items.
48 top_dir_to_create = top_dir_to_create.AppendASCII("a"); 51 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem);
49 base::FilePath dir_to_create(top_dir_to_create); 52 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem);
50 dir_to_create = dir_to_create.AppendASCII("b"); 53 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem);
51 ASSERT_FALSE(base::PathExists(dir_to_create));
52 54
53 work_item.reset(reinterpret_cast<WorkItem*>( 55 {
54 WorkItem::CreateCreateDirWorkItem(dir_to_create))); 56 // Expect all three items to be done in order then undone.
55 work_item_list->AddWorkItem(work_item.release()); 57 InSequence s;
56 58
57 std::wstring key_to_create(kTestRoot); 59 EXPECT_CALL(*item1, Do()).WillOnce(Return(true));
58 key_to_create.push_back(base::FilePath::kSeparators[0]); 60 EXPECT_CALL(*item2, Do()).WillOnce(Return(true));
59 key_to_create.append(L"ExecutionSuccess"); 61 EXPECT_CALL(*item3, Do()).WillOnce(Return(true));
62 EXPECT_CALL(*item3, Rollback());
63 EXPECT_CALL(*item2, Rollback());
64 EXPECT_CALL(*item1, Rollback());
65 }
60 66
61 work_item.reset( 67 // Add the items to the list.
62 reinterpret_cast<WorkItem*>(WorkItem::CreateCreateRegKeyWorkItem( 68 list->AddWorkItem(item1.release());
63 HKEY_CURRENT_USER, key_to_create, WorkItem::kWow64Default))); 69 list->AddWorkItem(item2.release());
64 work_item_list->AddWorkItem(work_item.release()); 70 list->AddWorkItem(item3.release());
65 71
66 std::wstring name(kName); 72 // Do and rollback the list.
67 std::wstring data(kDataStr); 73 EXPECT_TRUE(list->Do());
68 work_item.reset(reinterpret_cast<WorkItem*>( 74 list->Rollback();
69 WorkItem::CreateSetRegValueWorkItem(HKEY_CURRENT_USER,
70 key_to_create,
71 WorkItem::kWow64Default,
72 name,
73 data,
74 false)));
75 work_item_list->AddWorkItem(work_item.release());
76
77 EXPECT_TRUE(work_item_list->Do());
78
79 // Verify all WorkItems have been executed.
80 RegKey key;
81 EXPECT_EQ(ERROR_SUCCESS,
82 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
83 std::wstring read_out;
84 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(name.c_str(), &read_out));
85 EXPECT_EQ(0, read_out.compare(kDataStr));
86 key.Close();
87 EXPECT_TRUE(base::PathExists(dir_to_create));
88
89 work_item_list->Rollback();
90
91 // Verify everything is rolled back.
92 // The value must have been deleted first in roll back otherwise the key
93 // can not be deleted.
94 EXPECT_NE(ERROR_SUCCESS,
95 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
96 EXPECT_FALSE(base::PathExists(top_dir_to_create));
97 } 75 }
98 76
99 // Execute a WorkItem list. Fail in the middle. Rollback what has been done. 77 // Execute a WorkItemList. Fail in the middle. Rollback what has been done.
100 TEST_F(WorkItemListTest, ExecutionFailAndRollback) { 78 TEST(WorkItemListTest, ExecutionFailAndRollback) {
101 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); 79 std::unique_ptr<WorkItemList> list(WorkItem::CreateWorkItemList());
102 scoped_ptr<WorkItem> work_item;
103 80
104 base::FilePath top_dir_to_create(temp_dir_.path()); 81 // Create the mock work items.
105 top_dir_to_create = top_dir_to_create.AppendASCII("a"); 82 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem);
106 base::FilePath dir_to_create(top_dir_to_create); 83 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem);
107 dir_to_create = dir_to_create.AppendASCII("b"); 84 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem);
108 ASSERT_FALSE(base::PathExists(dir_to_create));
109 85
110 work_item.reset(reinterpret_cast<WorkItem*>( 86 {
111 WorkItem::CreateCreateDirWorkItem(dir_to_create))); 87 // Expect the two first work items to be done in order then undone.
112 work_item_list->AddWorkItem(work_item.release()); 88 InSequence s;
113 89
114 std::wstring key_to_create(kTestRoot); 90 EXPECT_CALL(*item1, Do()).WillOnce(Return(true));
115 key_to_create.push_back(base::FilePath::kSeparators[0]); 91 EXPECT_CALL(*item2, Do()).WillOnce(Return(false));
116 key_to_create.append(L"ExecutionFail"); 92 EXPECT_CALL(*item2, Rollback());
93 EXPECT_CALL(*item1, Rollback());
94 }
117 95
118 work_item.reset( 96 // Add the items to the list.
119 reinterpret_cast<WorkItem*>(WorkItem::CreateCreateRegKeyWorkItem( 97 list->AddWorkItem(item1.release());
120 HKEY_CURRENT_USER, key_to_create, WorkItem::kWow64Default))); 98 list->AddWorkItem(item2.release());
121 work_item_list->AddWorkItem(work_item.release()); 99 list->AddWorkItem(item3.release());
122 100
123 std::wstring not_created_key(kTestRoot); 101 // Do and rollback the list.
124 not_created_key.push_back(base::FilePath::kSeparators[0]); 102 EXPECT_FALSE(list->Do());
125 not_created_key.append(L"NotCreated"); 103 list->Rollback();
126 std::wstring name(kName);
127 std::wstring data(kDataStr);
128 work_item.reset(reinterpret_cast<WorkItem*>(
129 WorkItem::CreateSetRegValueWorkItem(HKEY_CURRENT_USER,
130 not_created_key,
131 WorkItem::kWow64Default,
132 name,
133 data,
134 false)));
135 work_item_list->AddWorkItem(work_item.release());
136
137 // This one will not be executed because we will fail early.
138 work_item.reset(
139 reinterpret_cast<WorkItem*>(WorkItem::CreateCreateRegKeyWorkItem(
140 HKEY_CURRENT_USER, not_created_key, WorkItem::kWow64Default)));
141 work_item_list->AddWorkItem(work_item.release());
142
143 EXPECT_FALSE(work_item_list->Do());
144
145 // Verify the first 2 WorkItems have been executed.
146 RegKey key;
147 EXPECT_EQ(ERROR_SUCCESS,
148 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
149 key.Close();
150 EXPECT_TRUE(base::PathExists(dir_to_create));
151 // The last one should not be there.
152 EXPECT_NE(ERROR_SUCCESS,
153 key.Open(HKEY_CURRENT_USER, not_created_key.c_str(), KEY_READ));
154
155 work_item_list->Rollback();
156
157 // Verify everything is rolled back.
158 EXPECT_NE(ERROR_SUCCESS,
159 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
160 EXPECT_FALSE(base::PathExists(top_dir_to_create));
161 } 104 }
162 105
163 TEST_F(WorkItemListTest, ConditionalExecutionSuccess) { 106 // Execute a ConditionalWorkItemList whose condition is met and then rollback.
164 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); 107 TEST(WorkItemListTest, ConditionalExecutionSuccess) {
165 scoped_ptr<WorkItem> work_item; 108 std::unique_ptr<WorkItemList> list(
109 WorkItem::CreateConditionalWorkItemList(new AlwaysTrueCondition()));
166 110
167 base::FilePath top_dir_to_create(temp_dir_.path()); 111 // Create the mock work items.
168 top_dir_to_create = top_dir_to_create.AppendASCII("a"); 112 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem);
169 base::FilePath dir_to_create(top_dir_to_create); 113 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem);
170 dir_to_create = dir_to_create.AppendASCII("b"); 114 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem);
171 ASSERT_FALSE(base::PathExists(dir_to_create));
172 115
173 work_item.reset(reinterpret_cast<WorkItem*>( 116 {
174 WorkItem::CreateCreateDirWorkItem(dir_to_create))); 117 // Expect all three items to be done in order then undone.
175 work_item_list->AddWorkItem(work_item.release()); 118 InSequence s;
176 119
177 scoped_ptr<WorkItemList> conditional_work_item_list( 120 EXPECT_CALL(*item1, Do()).WillOnce(Return(true));
178 WorkItem::CreateConditionalWorkItemList( 121 EXPECT_CALL(*item2, Do()).WillOnce(Return(true));
179 new ConditionRunIfFileExists(dir_to_create))); 122 EXPECT_CALL(*item3, Do()).WillOnce(Return(true));
123 EXPECT_CALL(*item3, Rollback());
124 EXPECT_CALL(*item2, Rollback());
125 EXPECT_CALL(*item1, Rollback());
126 }
180 127
181 std::wstring key_to_create(kTestRoot); 128 // Add the items to the list.
182 key_to_create.push_back(base::FilePath::kSeparators[0]); 129 list->AddWorkItem(item1.release());
183 key_to_create.append(L"ExecutionSuccess"); 130 list->AddWorkItem(item2.release());
184 work_item.reset( 131 list->AddWorkItem(item3.release());
185 reinterpret_cast<WorkItem*>(WorkItem::CreateCreateRegKeyWorkItem(
186 HKEY_CURRENT_USER, key_to_create, WorkItem::kWow64Default)));
187 conditional_work_item_list->AddWorkItem(work_item.release());
188 132
189 std::wstring name(kName); 133 // Do and rollback the list.
190 std::wstring data(kDataStr); 134 EXPECT_TRUE(list->Do());
191 work_item.reset(reinterpret_cast<WorkItem*>( 135 list->Rollback();
192 WorkItem::CreateSetRegValueWorkItem(HKEY_CURRENT_USER,
193 key_to_create,
194 WorkItem::kWow64Default,
195 name,
196 data,
197 false)));
198 conditional_work_item_list->AddWorkItem(work_item.release());
199
200 work_item_list->AddWorkItem(conditional_work_item_list.release());
201
202 EXPECT_TRUE(work_item_list->Do());
203
204 // Verify all WorkItems have been executed.
205 RegKey key;
206 EXPECT_EQ(ERROR_SUCCESS,
207 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
208 std::wstring read_out;
209 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(name.c_str(), &read_out));
210 EXPECT_EQ(0, read_out.compare(kDataStr));
211 key.Close();
212 EXPECT_TRUE(base::PathExists(dir_to_create));
213
214 work_item_list->Rollback();
215
216 // Verify everything is rolled back.
217 // The value must have been deleted first in roll back otherwise the key
218 // can not be deleted.
219 EXPECT_NE(ERROR_SUCCESS,
220 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
221 EXPECT_FALSE(base::PathExists(top_dir_to_create));
222 } 136 }
223 137
224 TEST_F(WorkItemListTest, ConditionalExecutionConditionFailure) { 138 // Execute a ConditionalWorkItemList whose condition is met. Fail in the middle.
225 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); 139 // Rollback what has been done.
226 scoped_ptr<WorkItem> work_item; 140 TEST(WorkItemListTest, ConditionalExecutionFailAndRollback) {
141 std::unique_ptr<WorkItemList> list(
142 WorkItem::CreateConditionalWorkItemList(new AlwaysTrueCondition()));
227 143
228 base::FilePath top_dir_to_create(temp_dir_.path()); 144 // Create the mock work items.
229 top_dir_to_create = top_dir_to_create.AppendASCII("a"); 145 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem);
230 base::FilePath dir_to_create(top_dir_to_create); 146 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem);
231 dir_to_create = dir_to_create.AppendASCII("b"); 147 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem);
232 ASSERT_FALSE(base::PathExists(dir_to_create));
233 148
234 work_item.reset(reinterpret_cast<WorkItem*>( 149 {
235 WorkItem::CreateCreateDirWorkItem(dir_to_create))); 150 // Expect the two first work items to be done in order then undone.
236 work_item_list->AddWorkItem(work_item.release()); 151 InSequence s;
237 152
238 scoped_ptr<WorkItemList> conditional_work_item_list( 153 EXPECT_CALL(*item1, Do()).WillOnce(Return(true));
239 WorkItem::CreateConditionalWorkItemList( 154 EXPECT_CALL(*item2, Do()).WillOnce(Return(false));
240 new ConditionRunIfFileExists(dir_to_create.AppendASCII("c")))); 155 EXPECT_CALL(*item2, Rollback());
156 EXPECT_CALL(*item1, Rollback());
157 }
241 158
242 std::wstring key_to_create(kTestRoot); 159 // Add the items to the list.
243 key_to_create.push_back(base::FilePath::kSeparators[0]); 160 list->AddWorkItem(item1.release());
244 key_to_create.append(L"ExecutionSuccess"); 161 list->AddWorkItem(item2.release());
245 work_item.reset( 162 list->AddWorkItem(item3.release());
246 reinterpret_cast<WorkItem*>(WorkItem::CreateCreateRegKeyWorkItem(
247 HKEY_CURRENT_USER, key_to_create, WorkItem::kWow64Default)));
248 conditional_work_item_list->AddWorkItem(work_item.release());
249 163
250 std::wstring name(kName); 164 // Do and rollback the list.
251 std::wstring data(kDataStr); 165 EXPECT_FALSE(list->Do());
252 work_item.reset(reinterpret_cast<WorkItem*>( 166 list->Rollback();
253 WorkItem::CreateSetRegValueWorkItem(HKEY_CURRENT_USER, 167 }
254 key_to_create,
255 WorkItem::kWow64Default,
256 name,
257 data,
258 false)));
259 conditional_work_item_list->AddWorkItem(work_item.release());
260 168
261 work_item_list->AddWorkItem(conditional_work_item_list.release()); 169 // Execute a ConditionalWorkItemList whose condition isn't met.
170 TEST(WorkItemListTest, ConditionalExecutionConditionFailure) {
171 std::unique_ptr<WorkItemList> list(
172 WorkItem::CreateConditionalWorkItemList(new AlwaysFalseCondition));
262 173
263 EXPECT_TRUE(work_item_list->Do()); 174 // Create the mock work items.
175 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem);
176 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem);
177 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem);
264 178
265 // Verify that the WorkItems added as part of the conditional list have NOT 179 // Don't expect any call to the methods of the work items.
266 // been executed.
267 RegKey key;
268 EXPECT_NE(ERROR_SUCCESS,
269 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
270 std::wstring read_out;
271 EXPECT_NE(ERROR_SUCCESS, key.ReadValue(name.c_str(), &read_out));
272 key.Close();
273 180
274 // Verify that the other work item was executed. 181 // Add the items to the list.
275 EXPECT_TRUE(base::PathExists(dir_to_create)); 182 list->AddWorkItem(item1.release());
183 list->AddWorkItem(item2.release());
184 list->AddWorkItem(item3.release());
276 185
277 work_item_list->Rollback(); 186 // Do and rollback the list.
187 EXPECT_TRUE(list->Do());
188 list->Rollback();
189 }
278 190
279 // Verify everything is rolled back. 191 TEST(WorkItemListTest, ConditionRunIfFileExists) {
280 // The value must have been deleted first in roll back otherwise the key 192 base::ScopedTempDir temp_dir;
281 // can not be deleted. 193 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
282 EXPECT_NE(ERROR_SUCCESS, 194
283 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ)); 195 EXPECT_TRUE(ConditionRunIfFileExists(temp_dir.path()).ShouldRun());
284 EXPECT_FALSE(base::PathExists(top_dir_to_create)); 196 EXPECT_FALSE(ConditionRunIfFileExists(
197 temp_dir.path().Append(FILE_PATH_LITERAL("DoesNotExist")))
198 .ShouldRun());
285 } 199 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698