OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/conditional_work_item_list.h" | 5 #include "chrome/installer/util/conditional_work_item_list.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "chrome/installer/util/work_item.h" | 12 #include "chrome/installer/util/work_item.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 class MockWorkItem : public WorkItem { | 18 class MockWorkItem : public WorkItem { |
19 public: | 19 public: |
20 MockWorkItem() = default; | 20 MockWorkItem() = default; |
21 | 21 |
22 MOCK_METHOD0(Do, bool()); | 22 MOCK_METHOD0(DoImpl, bool()); |
23 MOCK_METHOD0(Rollback, void()); | 23 MOCK_METHOD0(RollbackImpl, void()); |
24 | 24 |
25 private: | 25 private: |
26 DISALLOW_COPY_AND_ASSIGN(MockWorkItem); | 26 DISALLOW_COPY_AND_ASSIGN(MockWorkItem); |
27 }; | 27 }; |
28 | 28 |
29 class MockCondition : public WorkItem::Condition { | 29 class MockCondition : public WorkItem::Condition { |
30 public: | 30 public: |
31 MockCondition() = default; | 31 MockCondition() = default; |
32 | 32 |
33 MOCK_CONST_METHOD0(ShouldRun, bool()); | 33 MOCK_CONST_METHOD0(ShouldRun, bool()); |
(...skipping 18 matching lines...) Expand all Loading... |
52 | 52 |
53 // Create the mock work items. | 53 // Create the mock work items. |
54 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem); | 54 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem); |
55 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem); | 55 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem); |
56 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem); | 56 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem); |
57 | 57 |
58 { | 58 { |
59 // Expect all three items to be done in order then undone. | 59 // Expect all three items to be done in order then undone. |
60 InSequence s; | 60 InSequence s; |
61 | 61 |
62 EXPECT_CALL(*item1, Do()).WillOnce(Return(true)); | 62 EXPECT_CALL(*item1, DoImpl()).WillOnce(Return(true)); |
63 EXPECT_CALL(*item2, Do()).WillOnce(Return(true)); | 63 EXPECT_CALL(*item2, DoImpl()).WillOnce(Return(true)); |
64 EXPECT_CALL(*item3, Do()).WillOnce(Return(true)); | 64 EXPECT_CALL(*item3, DoImpl()).WillOnce(Return(true)); |
65 EXPECT_CALL(*item3, Rollback()); | 65 EXPECT_CALL(*item3, RollbackImpl()); |
66 EXPECT_CALL(*item2, Rollback()); | 66 EXPECT_CALL(*item2, RollbackImpl()); |
67 EXPECT_CALL(*item1, Rollback()); | 67 EXPECT_CALL(*item1, RollbackImpl()); |
68 } | 68 } |
69 | 69 |
70 // Add the items to the list. | 70 // Add the items to the list. |
71 list->AddWorkItem(item1.release()); | 71 list->AddWorkItem(item1.release()); |
72 list->AddWorkItem(item2.release()); | 72 list->AddWorkItem(item2.release()); |
73 list->AddWorkItem(item3.release()); | 73 list->AddWorkItem(item3.release()); |
74 | 74 |
75 // Do and rollback the list. | 75 // Do and rollback the list. |
76 EXPECT_TRUE(list->Do()); | 76 EXPECT_TRUE(list->Do()); |
77 list->Rollback(); | 77 list->Rollback(); |
78 } | 78 } |
79 | 79 |
80 // Execute a ConditionalWorkItemList whose condition is met. Fail in the middle. | 80 // Execute a ConditionalWorkItemList whose condition is met. Fail in the middle. |
81 // Rollback what has been done. | 81 // Rollback what has been done. |
82 TEST(ConditionalWorkItemListTest, ExecutionFailAndRollback) { | 82 TEST(ConditionalWorkItemListTest, ExecutionFailAndRollback) { |
83 std::unique_ptr<StrictMockCondition> condition(new StrictMockCondition); | 83 std::unique_ptr<StrictMockCondition> condition(new StrictMockCondition); |
84 EXPECT_CALL(*condition, ShouldRun()).WillOnce(Return(true)); | 84 EXPECT_CALL(*condition, ShouldRun()).WillOnce(Return(true)); |
85 std::unique_ptr<WorkItemList> list( | 85 std::unique_ptr<WorkItemList> list( |
86 WorkItem::CreateConditionalWorkItemList(condition.release())); | 86 WorkItem::CreateConditionalWorkItemList(condition.release())); |
87 | 87 |
88 // Create the mock work items. | 88 // Create the mock work items. |
89 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem); | 89 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem); |
90 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem); | 90 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem); |
91 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem); | 91 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem); |
92 | 92 |
93 { | 93 { |
94 // Expect the two first work items to be done in order then undone. | 94 // Expect the two first work items to be done in order then undone. |
95 InSequence s; | 95 InSequence s; |
96 | 96 |
97 EXPECT_CALL(*item1, Do()).WillOnce(Return(true)); | 97 EXPECT_CALL(*item1, DoImpl()).WillOnce(Return(true)); |
98 EXPECT_CALL(*item2, Do()).WillOnce(Return(false)); | 98 EXPECT_CALL(*item2, DoImpl()).WillOnce(Return(false)); |
99 EXPECT_CALL(*item2, Rollback()); | 99 EXPECT_CALL(*item2, RollbackImpl()); |
100 EXPECT_CALL(*item1, Rollback()); | 100 EXPECT_CALL(*item1, RollbackImpl()); |
101 } | 101 } |
102 | 102 |
103 // Add the items to the list. | 103 // Add the items to the list. |
104 list->AddWorkItem(item1.release()); | 104 list->AddWorkItem(item1.release()); |
105 list->AddWorkItem(item2.release()); | 105 list->AddWorkItem(item2.release()); |
106 list->AddWorkItem(item3.release()); | 106 list->AddWorkItem(item3.release()); |
107 | 107 |
108 // Do and rollback the list. | 108 // Do and rollback the list. |
109 EXPECT_FALSE(list->Do()); | 109 EXPECT_FALSE(list->Do()); |
110 list->Rollback(); | 110 list->Rollback(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 | 146 |
147 TEST(ConditionalWorkItemListTest, ConditionRunIfFileExists) { | 147 TEST(ConditionalWorkItemListTest, ConditionRunIfFileExists) { |
148 base::ScopedTempDir temp_dir; | 148 base::ScopedTempDir temp_dir; |
149 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 149 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
150 | 150 |
151 EXPECT_TRUE(ConditionRunIfFileExists(temp_dir.path()).ShouldRun()); | 151 EXPECT_TRUE(ConditionRunIfFileExists(temp_dir.path()).ShouldRun()); |
152 EXPECT_FALSE(ConditionRunIfFileExists( | 152 EXPECT_FALSE(ConditionRunIfFileExists( |
153 temp_dir.path().Append(FILE_PATH_LITERAL("DoesNotExist"))) | 153 temp_dir.path().Append(FILE_PATH_LITERAL("DoesNotExist"))) |
154 .ShouldRun()); | 154 .ShouldRun()); |
155 } | 155 } |
OLD | NEW |