OLD | NEW |
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 "chrome/installer/util/work_item_list.h" | 5 #include "chrome/installer/util/work_item_list.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "chrome/installer/util/work_item.h" | 10 #include "chrome/installer/util/work_item.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 class MockWorkItem : public WorkItem { | 16 class MockWorkItem : public WorkItem { |
17 public: | 17 public: |
18 MockWorkItem() = default; | 18 MockWorkItem() = default; |
19 | 19 |
20 MOCK_METHOD0(Do, bool()); | 20 MOCK_METHOD0(DoImpl, bool()); |
21 MOCK_METHOD0(Rollback, void()); | 21 MOCK_METHOD0(RollbackImpl, void()); |
22 | 22 |
23 private: | 23 private: |
24 DISALLOW_COPY_AND_ASSIGN(MockWorkItem); | 24 DISALLOW_COPY_AND_ASSIGN(MockWorkItem); |
25 }; | 25 }; |
26 | 26 |
27 using StrictMockWorkItem = testing::StrictMock<MockWorkItem>; | 27 using StrictMockWorkItem = testing::StrictMock<MockWorkItem>; |
28 using testing::InSequence; | 28 using testing::InSequence; |
29 using testing::Return; | 29 using testing::Return; |
30 | 30 |
31 } // namespace | 31 } // namespace |
32 | 32 |
33 // Execute a WorkItemList successfully and then rollback. | 33 // Execute a WorkItemList successfully and then rollback. |
34 TEST(WorkItemListTest, ExecutionSuccess) { | 34 TEST(WorkItemListTest, ExecutionSuccess) { |
35 std::unique_ptr<WorkItemList> list(WorkItem::CreateWorkItemList()); | 35 std::unique_ptr<WorkItemList> list(WorkItem::CreateWorkItemList()); |
36 | 36 |
37 // Create the mock work items. | 37 // Create the mock work items. |
38 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem); | 38 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem); |
39 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem); | 39 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem); |
40 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem); | 40 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem); |
41 | 41 |
42 { | 42 { |
43 // Expect all three items to be done in order then undone. | 43 // Expect all three items to be done in order then undone. |
44 InSequence s; | 44 InSequence s; |
45 | 45 |
46 EXPECT_CALL(*item1, Do()).WillOnce(Return(true)); | 46 EXPECT_CALL(*item1, DoImpl()).WillOnce(Return(true)); |
47 EXPECT_CALL(*item2, Do()).WillOnce(Return(true)); | 47 EXPECT_CALL(*item2, DoImpl()).WillOnce(Return(true)); |
48 EXPECT_CALL(*item3, Do()).WillOnce(Return(true)); | 48 EXPECT_CALL(*item3, DoImpl()).WillOnce(Return(true)); |
49 EXPECT_CALL(*item3, Rollback()); | 49 EXPECT_CALL(*item3, RollbackImpl()); |
50 EXPECT_CALL(*item2, Rollback()); | 50 EXPECT_CALL(*item2, RollbackImpl()); |
51 EXPECT_CALL(*item1, Rollback()); | 51 EXPECT_CALL(*item1, RollbackImpl()); |
52 } | 52 } |
53 | 53 |
54 // Add the items to the list. | 54 // Add the items to the list. |
55 list->AddWorkItem(item1.release()); | 55 list->AddWorkItem(item1.release()); |
56 list->AddWorkItem(item2.release()); | 56 list->AddWorkItem(item2.release()); |
57 list->AddWorkItem(item3.release()); | 57 list->AddWorkItem(item3.release()); |
58 | 58 |
59 // Do and rollback the list. | 59 // Do and rollback the list. |
60 EXPECT_TRUE(list->Do()); | 60 EXPECT_TRUE(list->Do()); |
61 list->Rollback(); | 61 list->Rollback(); |
62 } | 62 } |
63 | 63 |
64 // Execute a WorkItemList. Fail in the middle. Rollback what has been done. | 64 // Execute a WorkItemList. Fail in the middle. Rollback what has been done. |
65 TEST(WorkItemListTest, ExecutionFailAndRollback) { | 65 TEST(WorkItemListTest, ExecutionFailAndRollback) { |
66 std::unique_ptr<WorkItemList> list(WorkItem::CreateWorkItemList()); | 66 std::unique_ptr<WorkItemList> list(WorkItem::CreateWorkItemList()); |
67 | 67 |
68 // Create the mock work items. | 68 // Create the mock work items. |
69 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem); | 69 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem); |
70 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem); | 70 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem); |
71 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem); | 71 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem); |
72 | 72 |
73 { | 73 { |
74 // Expect the two first work items to be done in order then undone. | 74 // Expect the two first work items to be done in order then undone. |
75 InSequence s; | 75 InSequence s; |
76 | 76 |
77 EXPECT_CALL(*item1, Do()).WillOnce(Return(true)); | 77 EXPECT_CALL(*item1, DoImpl()).WillOnce(Return(true)); |
78 EXPECT_CALL(*item2, Do()).WillOnce(Return(false)); | 78 EXPECT_CALL(*item2, DoImpl()).WillOnce(Return(false)); |
79 EXPECT_CALL(*item2, Rollback()); | 79 EXPECT_CALL(*item2, RollbackImpl()); |
80 EXPECT_CALL(*item1, Rollback()); | 80 EXPECT_CALL(*item1, RollbackImpl()); |
81 } | 81 } |
82 | 82 |
83 // Add the items to the list. | 83 // Add the items to the list. |
84 list->AddWorkItem(item1.release()); | 84 list->AddWorkItem(item1.release()); |
85 list->AddWorkItem(item2.release()); | 85 list->AddWorkItem(item2.release()); |
86 list->AddWorkItem(item3.release()); | 86 list->AddWorkItem(item3.release()); |
87 | 87 |
88 // Do and rollback the list. | 88 // Do and rollback the list. |
89 EXPECT_FALSE(list->Do()); | 89 EXPECT_FALSE(list->Do()); |
90 list->Rollback(); | 90 list->Rollback(); |
91 } | 91 } |
OLD | NEW |