Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS 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 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 5 | #include "update_engine/update_manager/generic_variables.h" |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 6 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 9 | #include <base/callback.h> |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 11 | |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 12 | #include "update_engine/test_utils.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 13 | #include "update_engine/update_manager/umtest_utils.h" |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 14 | |
| 15 | using base::TimeDelta; |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 16 | using chromeos_update_engine::RunGMainLoopMaxIterations; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 17 | using std::unique_ptr; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 18 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 19 | namespace chromeos_update_manager { |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 20 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 21 | class UmPollCopyVariableTest : public ::testing::Test {}; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 22 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 23 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 24 | TEST_F(UmPollCopyVariableTest, SimpleTest) { |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 25 | // Tests that copies are generated as intended. |
| 26 | int source = 5; |
Gilad Arnold | 46eb5f6 | 2014-05-20 13:21:25 -0700 | [diff] [blame] | 27 | PollCopyVariable<int> var("var", source); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 28 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 29 | // Generate and validate a copy. |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 30 | unique_ptr<const int> copy_1(var.GetValue( |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 31 | UmTestUtils::DefaultTimeout(), nullptr)); |
| 32 | ASSERT_NE(nullptr, copy_1.get()); |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 33 | EXPECT_EQ(5, *copy_1); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 34 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 35 | // Assign a different value to the source variable. |
| 36 | source = 42; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 37 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 38 | // Check that the content of the copy was not affected (distinct instance). |
| 39 | EXPECT_EQ(5, *copy_1); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 40 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 41 | // Generate and validate a second copy. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 42 | UmTestUtils::ExpectVariableHasValue(42, &var); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 45 | TEST_F(UmPollCopyVariableTest, SetFlagTest) { |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 46 | // Tests that the set flag is being referred to as expected. |
| 47 | int source = 5; |
| 48 | bool is_set = false; |
Gilad Arnold | 46eb5f6 | 2014-05-20 13:21:25 -0700 | [diff] [blame] | 49 | PollCopyVariable<int> var("var", source, &is_set); |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 50 | |
| 51 | // Flag marked unset, nothing should be returned. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 52 | UmTestUtils::ExpectVariableNotSet(&var); |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 53 | |
| 54 | // Flag marked set, we should be getting a value. |
| 55 | is_set = true; |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 56 | UmTestUtils::ExpectVariableHasValue(5, &var); |
Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 59 | |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 60 | class CopyConstructorTestClass { |
| 61 | public: |
| 62 | CopyConstructorTestClass(void) : copied_(false) {} |
Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 63 | CopyConstructorTestClass(const CopyConstructorTestClass& other) |
| 64 | : copied_(true), val_(other.val_ * 2) {} |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 65 | |
| 66 | // Tells if the instance was constructed using the copy-constructor. |
Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 67 | const bool copied_; |
| 68 | |
| 69 | // An auxiliary internal value. |
| 70 | int val_ = 0; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 71 | }; |
| 72 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 73 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 74 | TEST_F(UmPollCopyVariableTest, UseCopyConstructorTest) { |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 75 | // Ensures that CopyVariables indeed uses the copy constructor. |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 76 | const CopyConstructorTestClass source; |
| 77 | ASSERT_FALSE(source.copied_); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 78 | |
Gilad Arnold | 46eb5f6 | 2014-05-20 13:21:25 -0700 | [diff] [blame] | 79 | PollCopyVariable<CopyConstructorTestClass> var("var", source); |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 80 | unique_ptr<const CopyConstructorTestClass> copy( |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 81 | var.GetValue(UmTestUtils::DefaultTimeout(), nullptr)); |
| 82 | ASSERT_NE(nullptr, copy.get()); |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 83 | EXPECT_TRUE(copy->copied_); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 86 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 87 | class UmConstCopyVariableTest : public ::testing::Test {}; |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 88 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 89 | TEST_F(UmConstCopyVariableTest, SimpleTest) { |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 90 | int source = 5; |
| 91 | ConstCopyVariable<int> var("var", source); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 92 | UmTestUtils::ExpectVariableHasValue(5, &var); |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 93 | |
| 94 | // Ensure the value is cached. |
| 95 | source = 42; |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 96 | UmTestUtils::ExpectVariableHasValue(5, &var); |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 99 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 100 | class UmCallCopyVariableTest : public ::testing::Test {}; |
Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 101 | |
| 102 | CopyConstructorTestClass test_func(CopyConstructorTestClass* obj) { |
| 103 | obj->val_++; // So we can check that the function was called. |
| 104 | return *obj; |
| 105 | } |
| 106 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 107 | TEST_F(UmCallCopyVariableTest, SimpleTest) { |
Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 108 | // Tests that the returned value is generated by copying the value returned by |
| 109 | // the function call. |
| 110 | |
| 111 | CopyConstructorTestClass test_obj; |
| 112 | ASSERT_FALSE(test_obj.copied_); |
| 113 | test_obj.val_ = 5; |
| 114 | |
| 115 | base::Callback<CopyConstructorTestClass(void)> cb = base::Bind( |
| 116 | test_func, &test_obj); |
| 117 | CallCopyVariable<CopyConstructorTestClass> var("var", cb); |
| 118 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 119 | unique_ptr<const CopyConstructorTestClass> copy( |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 120 | var.GetValue(UmTestUtils::DefaultTimeout(), nullptr)); |
Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 121 | EXPECT_EQ(6, test_obj.val_); // Check that the function was called. |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 122 | ASSERT_NE(nullptr, copy.get()); |
Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 123 | EXPECT_TRUE(copy->copied_); |
| 124 | EXPECT_EQ(12, copy->val_); // Check that copying occurred once. |
| 125 | } |
| 126 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 127 | TEST_F(UmCallCopyVariableTest, NullTest) { |
Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 128 | // Ensures that the variable returns null when the callback is null. |
| 129 | |
| 130 | base::Callback<bool(void)> cb; |
| 131 | CallCopyVariable<bool> var("var", cb); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 132 | UmTestUtils::ExpectVariableNotSet(&var); |
Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 135 | class UmAsyncCopyVariableTest : public ::testing::Test { |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 136 | public: |
| 137 | void TearDown() { |
| 138 | // No remaining event on the main loop. |
| 139 | EXPECT_EQ(0, RunGMainLoopMaxIterations(1)); |
| 140 | } |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 143 | TEST_F(UmAsyncCopyVariableTest, ConstructorTest) { |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 144 | AsyncCopyVariable<int> var("var"); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 145 | UmTestUtils::ExpectVariableNotSet(&var); |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 146 | EXPECT_EQ(kVariableModeAsync, var.GetMode()); |
| 147 | } |
| 148 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 149 | TEST_F(UmAsyncCopyVariableTest, SetValueTest) { |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 150 | AsyncCopyVariable<int> var("var"); |
| 151 | var.SetValue(5); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 152 | UmTestUtils::ExpectVariableHasValue(5, &var); |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 153 | // Execute all the pending observers. |
| 154 | RunGMainLoopMaxIterations(100); |
| 155 | } |
| 156 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 157 | TEST_F(UmAsyncCopyVariableTest, UnsetValueTest) { |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 158 | AsyncCopyVariable<int> var("var", 42); |
| 159 | var.UnsetValue(); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 160 | UmTestUtils::ExpectVariableNotSet(&var); |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 161 | // Execute all the pending observers. |
| 162 | RunGMainLoopMaxIterations(100); |
| 163 | } |
| 164 | |
| 165 | class CallCounterObserver : public BaseVariable::ObserverInterface { |
| 166 | public: |
| 167 | void ValueChanged(BaseVariable* variable) { |
| 168 | calls_count_++; |
| 169 | } |
| 170 | |
| 171 | int calls_count_ = 0; |
| 172 | }; |
| 173 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 174 | TEST_F(UmAsyncCopyVariableTest, ObserverCalledTest) { |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 175 | AsyncCopyVariable<int> var("var", 42); |
| 176 | CallCounterObserver observer; |
| 177 | var.AddObserver(&observer); |
| 178 | EXPECT_EQ(0, observer.calls_count_); |
| 179 | |
| 180 | // Check that a different value fires the notification. |
| 181 | var.SetValue(5); |
| 182 | RunGMainLoopMaxIterations(100); |
| 183 | EXPECT_EQ(1, observer.calls_count_); |
| 184 | |
| 185 | // Check the same value doesn't. |
| 186 | var.SetValue(5); |
| 187 | RunGMainLoopMaxIterations(100); |
| 188 | EXPECT_EQ(1, observer.calls_count_); |
| 189 | |
| 190 | // Check that unsetting a previously set value fires the notification. |
| 191 | var.UnsetValue(); |
| 192 | RunGMainLoopMaxIterations(100); |
| 193 | EXPECT_EQ(2, observer.calls_count_); |
| 194 | |
| 195 | // Check that unsetting again doesn't. |
| 196 | var.UnsetValue(); |
| 197 | RunGMainLoopMaxIterations(100); |
| 198 | EXPECT_EQ(2, observer.calls_count_); |
| 199 | |
| 200 | var.RemoveObserver(&observer); |
| 201 | } |
| 202 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 203 | } // namespace chromeos_update_manager |