blob: 0ff97e3966b4a7265bbefcea6967ebc87e6c991b [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Deymo81f30e82014-01-08 14:33:06 -080016
Alex Deymo63784a52014-05-28 10:46:14 -070017#include "update_engine/update_manager/generic_variables.h"
Alex Deymobd04b142014-03-18 15:00:05 -070018
Ben Chan02f7c1d2014-10-18 15:18:02 -070019#include <memory>
20
Gilad Arnoldc16fca22014-05-20 15:10:40 -070021#include <base/callback.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070022#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 Deymo81f30e82014-01-08 14:33:06 -080025#include <gtest/gtest.h>
Gilad Arnolda87340b2014-01-30 11:10:18 -080026
Alex Deymo63784a52014-05-28 10:46:14 -070027#include "update_engine/update_manager/umtest_utils.h"
Alex Deymo81f30e82014-01-08 14:33:06 -080028
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070029using brillo::MessageLoop;
30using brillo::MessageLoopRunMaxIterations;
Ben Chan02f7c1d2014-10-18 15:18:02 -070031using std::unique_ptr;
Alex Deymo81f30e82014-01-08 14:33:06 -080032
Alex Deymo63784a52014-05-28 10:46:14 -070033namespace chromeos_update_manager {
Alex Deymo81f30e82014-01-08 14:33:06 -080034
Alex Deymo63784a52014-05-28 10:46:14 -070035class UmPollCopyVariableTest : public ::testing::Test {};
Alex Deymo81f30e82014-01-08 14:33:06 -080036
Alex Deymo63784a52014-05-28 10:46:14 -070037TEST_F(UmPollCopyVariableTest, SimpleTest) {
Gilad Arnolda87340b2014-01-30 11:10:18 -080038 // Tests that copies are generated as intended.
39 int source = 5;
Gilad Arnold46eb5f62014-05-20 13:21:25 -070040 PollCopyVariable<int> var("var", source);
Alex Deymo81f30e82014-01-08 14:33:06 -080041
Gilad Arnolda87340b2014-01-30 11:10:18 -080042 // Generate and validate a copy.
Amin Hassani4b717432019-01-14 16:24:20 -080043 unique_ptr<const int> copy_1(
44 var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
Alex Vakulenko88b591f2014-08-28 16:48:57 -070045 ASSERT_NE(nullptr, copy_1.get());
Gilad Arnolda87340b2014-01-30 11:10:18 -080046 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080047
Gilad Arnolda87340b2014-01-30 11:10:18 -080048 // Assign a different value to the source variable.
49 source = 42;
Alex Deymo81f30e82014-01-08 14:33:06 -080050
Gilad Arnolda87340b2014-01-30 11:10:18 -080051 // Check that the content of the copy was not affected (distinct instance).
52 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080053
Gilad Arnolda87340b2014-01-30 11:10:18 -080054 // Generate and validate a second copy.
Alex Deymo63784a52014-05-28 10:46:14 -070055 UmTestUtils::ExpectVariableHasValue(42, &var);
Alex Deymo81f30e82014-01-08 14:33:06 -080056}
57
Alex Deymo63784a52014-05-28 10:46:14 -070058TEST_F(UmPollCopyVariableTest, SetFlagTest) {
Gilad Arnold9f7ab352014-04-16 15:27:37 -070059 // Tests that the set flag is being referred to as expected.
60 int source = 5;
61 bool is_set = false;
Gilad Arnold46eb5f62014-05-20 13:21:25 -070062 PollCopyVariable<int> var("var", source, &is_set);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070063
64 // Flag marked unset, nothing should be returned.
Alex Deymo63784a52014-05-28 10:46:14 -070065 UmTestUtils::ExpectVariableNotSet(&var);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070066
67 // Flag marked set, we should be getting a value.
68 is_set = true;
Alex Deymo63784a52014-05-28 10:46:14 -070069 UmTestUtils::ExpectVariableHasValue(5, &var);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070070}
71
Gilad Arnolda87340b2014-01-30 11:10:18 -080072class CopyConstructorTestClass {
73 public:
74 CopyConstructorTestClass(void) : copied_(false) {}
Gilad Arnoldc16fca22014-05-20 15:10:40 -070075 CopyConstructorTestClass(const CopyConstructorTestClass& other)
76 : copied_(true), val_(other.val_ * 2) {}
Alex Deymo81f30e82014-01-08 14:33:06 -080077
78 // Tells if the instance was constructed using the copy-constructor.
Gilad Arnoldc16fca22014-05-20 15:10:40 -070079 const bool copied_;
80
81 // An auxiliary internal value.
82 int val_ = 0;
Alex Deymo81f30e82014-01-08 14:33:06 -080083};
84
Alex Deymo63784a52014-05-28 10:46:14 -070085TEST_F(UmPollCopyVariableTest, UseCopyConstructorTest) {
Alex Vakulenko072359c2014-07-18 11:41:07 -070086 // Ensures that CopyVariables indeed uses the copy constructor.
Gilad Arnolda87340b2014-01-30 11:10:18 -080087 const CopyConstructorTestClass source;
88 ASSERT_FALSE(source.copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080089
Gilad Arnold46eb5f62014-05-20 13:21:25 -070090 PollCopyVariable<CopyConstructorTestClass> var("var", source);
Ben Chan02f7c1d2014-10-18 15:18:02 -070091 unique_ptr<const CopyConstructorTestClass> copy(
Alex Vakulenko88b591f2014-08-28 16:48:57 -070092 var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
93 ASSERT_NE(nullptr, copy.get());
Gilad Arnolda87340b2014-01-30 11:10:18 -080094 EXPECT_TRUE(copy->copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080095}
96
Alex Deymo63784a52014-05-28 10:46:14 -070097class UmConstCopyVariableTest : public ::testing::Test {};
Alex Deymobd04b142014-03-18 15:00:05 -070098
Alex Deymo63784a52014-05-28 10:46:14 -070099TEST_F(UmConstCopyVariableTest, SimpleTest) {
Alex Deymobd04b142014-03-18 15:00:05 -0700100 int source = 5;
101 ConstCopyVariable<int> var("var", source);
Alex Deymo63784a52014-05-28 10:46:14 -0700102 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymobd04b142014-03-18 15:00:05 -0700103
104 // Ensure the value is cached.
105 source = 42;
Alex Deymo63784a52014-05-28 10:46:14 -0700106 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymobd04b142014-03-18 15:00:05 -0700107}
108
Alex Deymo63784a52014-05-28 10:46:14 -0700109class UmCallCopyVariableTest : public ::testing::Test {};
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700110
111CopyConstructorTestClass test_func(CopyConstructorTestClass* obj) {
112 obj->val_++; // So we can check that the function was called.
113 return *obj;
114}
115
Alex Deymo63784a52014-05-28 10:46:14 -0700116TEST_F(UmCallCopyVariableTest, SimpleTest) {
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700117 // 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 Hassani4b717432019-01-14 16:24:20 -0800124 base::Callback<CopyConstructorTestClass(void)> cb =
125 base::Bind(test_func, &test_obj);
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700126 CallCopyVariable<CopyConstructorTestClass> var("var", cb);
127
Ben Chan02f7c1d2014-10-18 15:18:02 -0700128 unique_ptr<const CopyConstructorTestClass> copy(
Alex Deymo63784a52014-05-28 10:46:14 -0700129 var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700130 EXPECT_EQ(6, test_obj.val_); // Check that the function was called.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700131 ASSERT_NE(nullptr, copy.get());
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700132 EXPECT_TRUE(copy->copied_);
133 EXPECT_EQ(12, copy->val_); // Check that copying occurred once.
134}
135
Alex Deymo63784a52014-05-28 10:46:14 -0700136TEST_F(UmCallCopyVariableTest, NullTest) {
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700137 // 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 Deymo63784a52014-05-28 10:46:14 -0700141 UmTestUtils::ExpectVariableNotSet(&var);
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700142}
143
Alex Deymo63784a52014-05-28 10:46:14 -0700144class UmAsyncCopyVariableTest : public ::testing::Test {
Alex Deymo509dd532015-06-10 14:11:05 -0700145 protected:
Amin Hassani4b717432019-01-14 16:24:20 -0800146 void SetUp() override { loop_.SetAsCurrent(); }
Alex Deymo509dd532015-06-10 14:11:05 -0700147
Alex Deymo610277e2014-11-11 21:18:11 -0800148 void TearDown() override {
Alex Deymoc83baf62014-04-02 17:43:35 -0700149 // No remaining event on the main loop.
Alex Deymo509dd532015-06-10 14:11:05 -0700150 EXPECT_FALSE(loop_.PendingTasks());
Alex Deymoc83baf62014-04-02 17:43:35 -0700151 }
Alex Deymo509dd532015-06-10 14:11:05 -0700152
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700153 brillo::FakeMessageLoop loop_{nullptr};
Alex Deymoc83baf62014-04-02 17:43:35 -0700154};
155
Alex Deymo63784a52014-05-28 10:46:14 -0700156TEST_F(UmAsyncCopyVariableTest, ConstructorTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700157 AsyncCopyVariable<int> var("var");
Alex Deymo63784a52014-05-28 10:46:14 -0700158 UmTestUtils::ExpectVariableNotSet(&var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700159 EXPECT_EQ(kVariableModeAsync, var.GetMode());
160}
161
Alex Deymo63784a52014-05-28 10:46:14 -0700162TEST_F(UmAsyncCopyVariableTest, SetValueTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700163 AsyncCopyVariable<int> var("var");
164 var.SetValue(5);
Alex Deymo63784a52014-05-28 10:46:14 -0700165 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700166 // Execute all the pending observers.
Alex Deymo509dd532015-06-10 14:11:05 -0700167 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700168}
169
Alex Deymo63784a52014-05-28 10:46:14 -0700170TEST_F(UmAsyncCopyVariableTest, UnsetValueTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700171 AsyncCopyVariable<int> var("var", 42);
172 var.UnsetValue();
Alex Deymo63784a52014-05-28 10:46:14 -0700173 UmTestUtils::ExpectVariableNotSet(&var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700174 // Execute all the pending observers.
Alex Deymo509dd532015-06-10 14:11:05 -0700175 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700176}
177
178class CallCounterObserver : public BaseVariable::ObserverInterface {
179 public:
Amin Hassani4b717432019-01-14 16:24:20 -0800180 void ValueChanged(BaseVariable* variable) { calls_count_++; }
Alex Deymoc83baf62014-04-02 17:43:35 -0700181
182 int calls_count_ = 0;
183};
184
Alex Deymo63784a52014-05-28 10:46:14 -0700185TEST_F(UmAsyncCopyVariableTest, ObserverCalledTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700186 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 Deymo509dd532015-06-10 14:11:05 -0700193 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700194 EXPECT_EQ(1, observer.calls_count_);
195
196 // Check the same value doesn't.
197 var.SetValue(5);
Alex Deymo509dd532015-06-10 14:11:05 -0700198 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700199 EXPECT_EQ(1, observer.calls_count_);
200
201 // Check that unsetting a previously set value fires the notification.
202 var.UnsetValue();
Alex Deymo509dd532015-06-10 14:11:05 -0700203 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700204 EXPECT_EQ(2, observer.calls_count_);
205
206 // Check that unsetting again doesn't.
207 var.UnsetValue();
Alex Deymo509dd532015-06-10 14:11:05 -0700208 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700209 EXPECT_EQ(2, observer.calls_count_);
210
211 var.RemoveObserver(&observer);
212}
213
Alex Deymo63784a52014-05-28 10:46:14 -0700214} // namespace chromeos_update_manager