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