| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2014 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 16 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 17 | #include "update_engine/update_manager/generic_variables.h" | 
| Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 18 |  | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 19 | #include <memory> | 
|  | 20 |  | 
| Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 21 | #include <base/callback.h> | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 22 | #include <brillo/message_loops/fake_message_loop.h> | 
|  | 23 | #include <brillo/message_loops/message_loop.h> | 
|  | 24 | #include <brillo/message_loops/message_loop_utils.h> | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 25 | #include <gtest/gtest.h> | 
| Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 26 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 27 | #include "update_engine/update_manager/umtest_utils.h" | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 28 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 29 | using brillo::MessageLoop; | 
|  | 30 | using brillo::MessageLoopRunMaxIterations; | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 31 | using std::unique_ptr; | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 32 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 33 | namespace chromeos_update_manager { | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 34 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 35 | class UmPollCopyVariableTest : public ::testing::Test {}; | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 36 |  | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 37 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 38 | TEST_F(UmPollCopyVariableTest, SimpleTest) { | 
| Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 39 | // Tests that copies are generated as intended. | 
|  | 40 | int source = 5; | 
| Gilad Arnold | 46eb5f6 | 2014-05-20 13:21:25 -0700 | [diff] [blame] | 41 | PollCopyVariable<int> var("var", source); | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 42 |  | 
| Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 43 | // Generate and validate a copy. | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 44 | unique_ptr<const int> copy_1(var.GetValue( | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 45 | UmTestUtils::DefaultTimeout(), nullptr)); | 
|  | 46 | ASSERT_NE(nullptr, copy_1.get()); | 
| Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 47 | EXPECT_EQ(5, *copy_1); | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 48 |  | 
| Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 49 | // Assign a different value to the source variable. | 
|  | 50 | source = 42; | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 51 |  | 
| Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 52 | // Check that the content of the copy was not affected (distinct instance). | 
|  | 53 | EXPECT_EQ(5, *copy_1); | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 54 |  | 
| Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 55 | // Generate and validate a second copy. | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 56 | UmTestUtils::ExpectVariableHasValue(42, &var); | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 59 | TEST_F(UmPollCopyVariableTest, SetFlagTest) { | 
| Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 60 | // Tests that the set flag is being referred to as expected. | 
|  | 61 | int source = 5; | 
|  | 62 | bool is_set = false; | 
| Gilad Arnold | 46eb5f6 | 2014-05-20 13:21:25 -0700 | [diff] [blame] | 63 | PollCopyVariable<int> var("var", source, &is_set); | 
| Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 64 |  | 
|  | 65 | // Flag marked unset, nothing should be returned. | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 66 | UmTestUtils::ExpectVariableNotSet(&var); | 
| Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 67 |  | 
|  | 68 | // Flag marked set, we should be getting a value. | 
|  | 69 | is_set = true; | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 70 | UmTestUtils::ExpectVariableHasValue(5, &var); | 
| Gilad Arnold | 9f7ab35 | 2014-04-16 15:27:37 -0700 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 73 |  | 
| Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 74 | class CopyConstructorTestClass { | 
|  | 75 | public: | 
|  | 76 | CopyConstructorTestClass(void) : copied_(false) {} | 
| Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 77 | CopyConstructorTestClass(const CopyConstructorTestClass& other) | 
|  | 78 | : copied_(true), val_(other.val_ * 2) {} | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 79 |  | 
|  | 80 | // Tells if the instance was constructed using the copy-constructor. | 
| Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 81 | const bool copied_; | 
|  | 82 |  | 
|  | 83 | // An auxiliary internal value. | 
|  | 84 | int val_ = 0; | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 85 | }; | 
|  | 86 |  | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 87 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 88 | TEST_F(UmPollCopyVariableTest, UseCopyConstructorTest) { | 
| Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 89 | // Ensures that CopyVariables indeed uses the copy constructor. | 
| Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 90 | const CopyConstructorTestClass source; | 
|  | 91 | ASSERT_FALSE(source.copied_); | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 92 |  | 
| Gilad Arnold | 46eb5f6 | 2014-05-20 13:21:25 -0700 | [diff] [blame] | 93 | PollCopyVariable<CopyConstructorTestClass> var("var", source); | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 94 | unique_ptr<const CopyConstructorTestClass> copy( | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 95 | var.GetValue(UmTestUtils::DefaultTimeout(), nullptr)); | 
|  | 96 | ASSERT_NE(nullptr, copy.get()); | 
| Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 97 | EXPECT_TRUE(copy->copied_); | 
| Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
| Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 100 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 101 | class UmConstCopyVariableTest : public ::testing::Test {}; | 
| Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 102 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 103 | TEST_F(UmConstCopyVariableTest, SimpleTest) { | 
| Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 104 | int source = 5; | 
|  | 105 | ConstCopyVariable<int> var("var", source); | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 106 | UmTestUtils::ExpectVariableHasValue(5, &var); | 
| Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 107 |  | 
|  | 108 | // Ensure the value is cached. | 
|  | 109 | source = 42; | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 110 | UmTestUtils::ExpectVariableHasValue(5, &var); | 
| Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 111 | } | 
|  | 112 |  | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 113 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 114 | class UmCallCopyVariableTest : public ::testing::Test {}; | 
| Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 115 |  | 
|  | 116 | CopyConstructorTestClass test_func(CopyConstructorTestClass* obj) { | 
|  | 117 | obj->val_++;  // So we can check that the function was called. | 
|  | 118 | return *obj; | 
|  | 119 | } | 
|  | 120 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 121 | TEST_F(UmCallCopyVariableTest, SimpleTest) { | 
| Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 122 | // Tests that the returned value is generated by copying the value returned by | 
|  | 123 | // the function call. | 
|  | 124 |  | 
|  | 125 | CopyConstructorTestClass test_obj; | 
|  | 126 | ASSERT_FALSE(test_obj.copied_); | 
|  | 127 | test_obj.val_ = 5; | 
|  | 128 |  | 
|  | 129 | base::Callback<CopyConstructorTestClass(void)> cb = base::Bind( | 
|  | 130 | test_func, &test_obj); | 
|  | 131 | CallCopyVariable<CopyConstructorTestClass> var("var", cb); | 
|  | 132 |  | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 133 | unique_ptr<const CopyConstructorTestClass> copy( | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 134 | var.GetValue(UmTestUtils::DefaultTimeout(), nullptr)); | 
| Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 135 | EXPECT_EQ(6, test_obj.val_);  // Check that the function was called. | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 136 | ASSERT_NE(nullptr, copy.get()); | 
| Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 137 | EXPECT_TRUE(copy->copied_); | 
|  | 138 | EXPECT_EQ(12, copy->val_);  // Check that copying occurred once. | 
|  | 139 | } | 
|  | 140 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 141 | TEST_F(UmCallCopyVariableTest, NullTest) { | 
| Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 142 | // Ensures that the variable returns null when the callback is null. | 
|  | 143 |  | 
|  | 144 | base::Callback<bool(void)> cb; | 
|  | 145 | CallCopyVariable<bool> var("var", cb); | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 146 | UmTestUtils::ExpectVariableNotSet(&var); | 
| Gilad Arnold | c16fca2 | 2014-05-20 15:10:40 -0700 | [diff] [blame] | 147 | } | 
|  | 148 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 149 | class UmAsyncCopyVariableTest : public ::testing::Test { | 
| Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 150 | protected: | 
|  | 151 | void SetUp() override { | 
|  | 152 | loop_.SetAsCurrent(); | 
|  | 153 | } | 
|  | 154 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 155 | void TearDown() override { | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 156 | // No remaining event on the main loop. | 
| Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 157 | EXPECT_FALSE(loop_.PendingTasks()); | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 158 | } | 
| Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 159 |  | 
|  | 160 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 161 | brillo::FakeMessageLoop loop_{nullptr}; | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 162 | }; | 
|  | 163 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 164 | TEST_F(UmAsyncCopyVariableTest, ConstructorTest) { | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 165 | AsyncCopyVariable<int> var("var"); | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 166 | UmTestUtils::ExpectVariableNotSet(&var); | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 167 | EXPECT_EQ(kVariableModeAsync, var.GetMode()); | 
|  | 168 | } | 
|  | 169 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 170 | TEST_F(UmAsyncCopyVariableTest, SetValueTest) { | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 171 | AsyncCopyVariable<int> var("var"); | 
|  | 172 | var.SetValue(5); | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 173 | UmTestUtils::ExpectVariableHasValue(5, &var); | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 174 | // Execute all the pending observers. | 
| Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 175 | MessageLoopRunMaxIterations(MessageLoop::current(), 100); | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 178 | TEST_F(UmAsyncCopyVariableTest, UnsetValueTest) { | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 179 | AsyncCopyVariable<int> var("var", 42); | 
|  | 180 | var.UnsetValue(); | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 181 | UmTestUtils::ExpectVariableNotSet(&var); | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 182 | // Execute all the pending observers. | 
| Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 183 | MessageLoopRunMaxIterations(MessageLoop::current(), 100); | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 184 | } | 
|  | 185 |  | 
|  | 186 | class CallCounterObserver : public BaseVariable::ObserverInterface { | 
|  | 187 | public: | 
|  | 188 | void ValueChanged(BaseVariable* variable) { | 
|  | 189 | calls_count_++; | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | int calls_count_ = 0; | 
|  | 193 | }; | 
|  | 194 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 195 | TEST_F(UmAsyncCopyVariableTest, ObserverCalledTest) { | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 196 | AsyncCopyVariable<int> var("var", 42); | 
|  | 197 | CallCounterObserver observer; | 
|  | 198 | var.AddObserver(&observer); | 
|  | 199 | EXPECT_EQ(0, observer.calls_count_); | 
|  | 200 |  | 
|  | 201 | // Check that a different value fires the notification. | 
|  | 202 | var.SetValue(5); | 
| Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 203 | MessageLoopRunMaxIterations(MessageLoop::current(), 100); | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 204 | EXPECT_EQ(1, observer.calls_count_); | 
|  | 205 |  | 
|  | 206 | // Check the same value doesn't. | 
|  | 207 | var.SetValue(5); | 
| Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 208 | MessageLoopRunMaxIterations(MessageLoop::current(), 100); | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 209 | EXPECT_EQ(1, observer.calls_count_); | 
|  | 210 |  | 
|  | 211 | // Check that unsetting a previously set value fires the notification. | 
|  | 212 | var.UnsetValue(); | 
| Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 213 | MessageLoopRunMaxIterations(MessageLoop::current(), 100); | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 214 | EXPECT_EQ(2, observer.calls_count_); | 
|  | 215 |  | 
|  | 216 | // Check that unsetting again doesn't. | 
|  | 217 | var.UnsetValue(); | 
| Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 218 | MessageLoopRunMaxIterations(MessageLoop::current(), 100); | 
| Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 219 | EXPECT_EQ(2, observer.calls_count_); | 
|  | 220 |  | 
|  | 221 | var.RemoveObserver(&observer); | 
|  | 222 | } | 
|  | 223 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 224 | }  // namespace chromeos_update_manager |