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