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