OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/installer/util/conditional_work_item_list.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/macros.h" |
| 12 #include "chrome/installer/util/work_item.h" |
| 13 #include "chrome/installer/util/work_item_mocks.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 using testing::InSequence; |
| 18 using testing::Return; |
| 19 |
| 20 // Execute a ConditionalWorkItemList whose condition is met and then rollback. |
| 21 TEST(ConditionalWorkItemListTest, ExecutionSuccess) { |
| 22 std::unique_ptr<StrictMockCondition> condition(new StrictMockCondition); |
| 23 EXPECT_CALL(*condition, ShouldRun()).WillOnce(Return(true)); |
| 24 std::unique_ptr<WorkItemList> list( |
| 25 WorkItem::CreateConditionalWorkItemList(condition.release())); |
| 26 |
| 27 // Create the mock work items. |
| 28 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem); |
| 29 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem); |
| 30 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem); |
| 31 |
| 32 { |
| 33 // Expect all three items to be done in order then undone. |
| 34 InSequence s; |
| 35 |
| 36 EXPECT_CALL(*item1, Do()).WillOnce(Return(true)); |
| 37 EXPECT_CALL(*item2, Do()).WillOnce(Return(true)); |
| 38 EXPECT_CALL(*item3, Do()).WillOnce(Return(true)); |
| 39 EXPECT_CALL(*item3, Rollback()); |
| 40 EXPECT_CALL(*item2, Rollback()); |
| 41 EXPECT_CALL(*item1, Rollback()); |
| 42 } |
| 43 |
| 44 // Add the items to the list. |
| 45 list->AddWorkItem(item1.release()); |
| 46 list->AddWorkItem(item2.release()); |
| 47 list->AddWorkItem(item3.release()); |
| 48 |
| 49 // Do and rollback the list. |
| 50 EXPECT_TRUE(list->Do()); |
| 51 list->Rollback(); |
| 52 } |
| 53 |
| 54 // Execute a ConditionalWorkItemList whose condition is met. Fail in the middle. |
| 55 // Rollback what has been done. |
| 56 TEST(ConditionalWorkItemListTest, ExecutionFailAndRollback) { |
| 57 std::unique_ptr<StrictMockCondition> condition(new StrictMockCondition); |
| 58 EXPECT_CALL(*condition, ShouldRun()).WillOnce(Return(true)); |
| 59 std::unique_ptr<WorkItemList> list( |
| 60 WorkItem::CreateConditionalWorkItemList(condition.release())); |
| 61 |
| 62 // Create the mock work items. |
| 63 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem); |
| 64 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem); |
| 65 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem); |
| 66 |
| 67 { |
| 68 // Expect the two first work items to be done in order then undone. |
| 69 InSequence s; |
| 70 |
| 71 EXPECT_CALL(*item1, Do()).WillOnce(Return(true)); |
| 72 EXPECT_CALL(*item2, Do()).WillOnce(Return(false)); |
| 73 EXPECT_CALL(*item2, Rollback()); |
| 74 EXPECT_CALL(*item1, Rollback()); |
| 75 } |
| 76 |
| 77 // Add the items to the list. |
| 78 list->AddWorkItem(item1.release()); |
| 79 list->AddWorkItem(item2.release()); |
| 80 list->AddWorkItem(item3.release()); |
| 81 |
| 82 // Do and rollback the list. |
| 83 EXPECT_FALSE(list->Do()); |
| 84 list->Rollback(); |
| 85 } |
| 86 |
| 87 // Execute a ConditionalWorkItemList whose condition isn't met. |
| 88 TEST(ConditionalWorkItemListTest, ConditionFailure) { |
| 89 std::unique_ptr<StrictMockCondition> condition(new StrictMockCondition); |
| 90 EXPECT_CALL(*condition, ShouldRun()).WillOnce(Return(false)); |
| 91 std::unique_ptr<WorkItemList> list( |
| 92 WorkItem::CreateConditionalWorkItemList(condition.release())); |
| 93 |
| 94 // Create the mock work items. |
| 95 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem); |
| 96 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem); |
| 97 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem); |
| 98 |
| 99 // Don't expect any call to the methods of the work items. |
| 100 |
| 101 // Add the items to the list. |
| 102 list->AddWorkItem(item1.release()); |
| 103 list->AddWorkItem(item2.release()); |
| 104 list->AddWorkItem(item3.release()); |
| 105 |
| 106 // Do and rollback the list. |
| 107 EXPECT_TRUE(list->Do()); |
| 108 list->Rollback(); |
| 109 } |
| 110 |
| 111 TEST(ConditionalWorkItemListTest, ConditionNot) { |
| 112 std::unique_ptr<StrictMockCondition> condition_true(new StrictMockCondition); |
| 113 EXPECT_CALL(*condition_true, ShouldRun()).WillOnce(Return(true)); |
| 114 EXPECT_FALSE(Not(condition_true.release()).ShouldRun()); |
| 115 |
| 116 std::unique_ptr<StrictMockCondition> condition_false(new StrictMockCondition); |
| 117 EXPECT_CALL(*condition_false, ShouldRun()).WillOnce(Return(false)); |
| 118 EXPECT_TRUE(Not(condition_false.release()).ShouldRun()); |
| 119 } |
| 120 |
| 121 TEST(ConditionalWorkItemListTest, ConditionRunIfFileExists) { |
| 122 base::ScopedTempDir temp_dir; |
| 123 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 124 |
| 125 EXPECT_TRUE(ConditionRunIfFileExists(temp_dir.path()).ShouldRun()); |
| 126 EXPECT_FALSE(ConditionRunIfFileExists( |
| 127 temp_dir.path().Append(FILE_PATH_LITERAL("DoesNotExist"))) |
| 128 .ShouldRun()); |
| 129 } |
OLD | NEW |