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