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 //
16
17 #include "update_engine/update_manager/generic_variables.h"
18
19 #include <memory>
20
21 #include <base/callback.h>
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>
25 #include <gtest/gtest.h>
26
27 #include "update_engine/update_manager/umtest_utils.h"
28
29 using brillo::MessageLoop;
30 using brillo::MessageLoopRunMaxIterations;
31 using std::unique_ptr;
32
33 namespace chromeos_update_manager {
34
35 class UmPollCopyVariableTest : public ::testing::Test {};
36
37
TEST_F(UmPollCopyVariableTest,SimpleTest)38 TEST_F(UmPollCopyVariableTest, SimpleTest) {
39 // Tests that copies are generated as intended.
40 int source = 5;
41 PollCopyVariable<int> var("var", source);
42
43 // Generate and validate a copy.
44 unique_ptr<const int> copy_1(var.GetValue(
45 UmTestUtils::DefaultTimeout(), nullptr));
46 ASSERT_NE(nullptr, copy_1.get());
47 EXPECT_EQ(5, *copy_1);
48
49 // Assign a different value to the source variable.
50 source = 42;
51
52 // Check that the content of the copy was not affected (distinct instance).
53 EXPECT_EQ(5, *copy_1);
54
55 // Generate and validate a second copy.
56 UmTestUtils::ExpectVariableHasValue(42, &var);
57 }
58
TEST_F(UmPollCopyVariableTest,SetFlagTest)59 TEST_F(UmPollCopyVariableTest, SetFlagTest) {
60 // Tests that the set flag is being referred to as expected.
61 int source = 5;
62 bool is_set = false;
63 PollCopyVariable<int> var("var", source, &is_set);
64
65 // Flag marked unset, nothing should be returned.
66 UmTestUtils::ExpectVariableNotSet(&var);
67
68 // Flag marked set, we should be getting a value.
69 is_set = true;
70 UmTestUtils::ExpectVariableHasValue(5, &var);
71 }
72
73
74 class CopyConstructorTestClass {
75 public:
CopyConstructorTestClass(void)76 CopyConstructorTestClass(void) : copied_(false) {}
CopyConstructorTestClass(const CopyConstructorTestClass & other)77 CopyConstructorTestClass(const CopyConstructorTestClass& other)
78 : copied_(true), val_(other.val_ * 2) {}
79
80 // Tells if the instance was constructed using the copy-constructor.
81 const bool copied_;
82
83 // An auxiliary internal value.
84 int val_ = 0;
85 };
86
87
TEST_F(UmPollCopyVariableTest,UseCopyConstructorTest)88 TEST_F(UmPollCopyVariableTest, UseCopyConstructorTest) {
89 // Ensures that CopyVariables indeed uses the copy constructor.
90 const CopyConstructorTestClass source;
91 ASSERT_FALSE(source.copied_);
92
93 PollCopyVariable<CopyConstructorTestClass> var("var", source);
94 unique_ptr<const CopyConstructorTestClass> copy(
95 var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
96 ASSERT_NE(nullptr, copy.get());
97 EXPECT_TRUE(copy->copied_);
98 }
99
100
101 class UmConstCopyVariableTest : public ::testing::Test {};
102
TEST_F(UmConstCopyVariableTest,SimpleTest)103 TEST_F(UmConstCopyVariableTest, SimpleTest) {
104 int source = 5;
105 ConstCopyVariable<int> var("var", source);
106 UmTestUtils::ExpectVariableHasValue(5, &var);
107
108 // Ensure the value is cached.
109 source = 42;
110 UmTestUtils::ExpectVariableHasValue(5, &var);
111 }
112
113
114 class UmCallCopyVariableTest : public ::testing::Test {};
115
test_func(CopyConstructorTestClass * obj)116 CopyConstructorTestClass test_func(CopyConstructorTestClass* obj) {
117 obj->val_++; // So we can check that the function was called.
118 return *obj;
119 }
120
TEST_F(UmCallCopyVariableTest,SimpleTest)121 TEST_F(UmCallCopyVariableTest, SimpleTest) {
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
133 unique_ptr<const CopyConstructorTestClass> copy(
134 var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
135 EXPECT_EQ(6, test_obj.val_); // Check that the function was called.
136 ASSERT_NE(nullptr, copy.get());
137 EXPECT_TRUE(copy->copied_);
138 EXPECT_EQ(12, copy->val_); // Check that copying occurred once.
139 }
140
TEST_F(UmCallCopyVariableTest,NullTest)141 TEST_F(UmCallCopyVariableTest, NullTest) {
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);
146 UmTestUtils::ExpectVariableNotSet(&var);
147 }
148
149 class UmAsyncCopyVariableTest : public ::testing::Test {
150 protected:
SetUp()151 void SetUp() override {
152 loop_.SetAsCurrent();
153 }
154
TearDown()155 void TearDown() override {
156 // No remaining event on the main loop.
157 EXPECT_FALSE(loop_.PendingTasks());
158 }
159
160
161 brillo::FakeMessageLoop loop_{nullptr};
162 };
163
TEST_F(UmAsyncCopyVariableTest,ConstructorTest)164 TEST_F(UmAsyncCopyVariableTest, ConstructorTest) {
165 AsyncCopyVariable<int> var("var");
166 UmTestUtils::ExpectVariableNotSet(&var);
167 EXPECT_EQ(kVariableModeAsync, var.GetMode());
168 }
169
TEST_F(UmAsyncCopyVariableTest,SetValueTest)170 TEST_F(UmAsyncCopyVariableTest, SetValueTest) {
171 AsyncCopyVariable<int> var("var");
172 var.SetValue(5);
173 UmTestUtils::ExpectVariableHasValue(5, &var);
174 // Execute all the pending observers.
175 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
176 }
177
TEST_F(UmAsyncCopyVariableTest,UnsetValueTest)178 TEST_F(UmAsyncCopyVariableTest, UnsetValueTest) {
179 AsyncCopyVariable<int> var("var", 42);
180 var.UnsetValue();
181 UmTestUtils::ExpectVariableNotSet(&var);
182 // Execute all the pending observers.
183 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
184 }
185
186 class CallCounterObserver : public BaseVariable::ObserverInterface {
187 public:
ValueChanged(BaseVariable * variable)188 void ValueChanged(BaseVariable* variable) {
189 calls_count_++;
190 }
191
192 int calls_count_ = 0;
193 };
194
TEST_F(UmAsyncCopyVariableTest,ObserverCalledTest)195 TEST_F(UmAsyncCopyVariableTest, ObserverCalledTest) {
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);
203 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
204 EXPECT_EQ(1, observer.calls_count_);
205
206 // Check the same value doesn't.
207 var.SetValue(5);
208 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
209 EXPECT_EQ(1, observer.calls_count_);
210
211 // Check that unsetting a previously set value fires the notification.
212 var.UnsetValue();
213 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
214 EXPECT_EQ(2, observer.calls_count_);
215
216 // Check that unsetting again doesn't.
217 var.UnsetValue();
218 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
219 EXPECT_EQ(2, observer.calls_count_);
220
221 var.RemoveObserver(&observer);
222 }
223
224 } // namespace chromeos_update_manager
225