blob: 5ea48f9a6d2eda54d3d6bb15d0c87cb71711559f [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 Deymo509dd532015-06-10 14:11:05 -070022#include <chromeos/message_loops/fake_message_loop.h>
23#include <chromeos/message_loops/message_loop.h>
24#include <chromeos/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 Deymo509dd532015-06-10 14:11:05 -070029using chromeos::MessageLoop;
30using chromeos::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 Deymo81f30e82014-01-08 14:33:06 -080037
Alex Deymo63784a52014-05-28 10:46:14 -070038TEST_F(UmPollCopyVariableTest, SimpleTest) {
Gilad Arnolda87340b2014-01-30 11:10:18 -080039 // Tests that copies are generated as intended.
40 int source = 5;
Gilad Arnold46eb5f62014-05-20 13:21:25 -070041 PollCopyVariable<int> var("var", source);
Alex Deymo81f30e82014-01-08 14:33:06 -080042
Gilad Arnolda87340b2014-01-30 11:10:18 -080043 // Generate and validate a copy.
Ben Chan02f7c1d2014-10-18 15:18:02 -070044 unique_ptr<const int> copy_1(var.GetValue(
Alex Vakulenko88b591f2014-08-28 16:48:57 -070045 UmTestUtils::DefaultTimeout(), nullptr));
46 ASSERT_NE(nullptr, copy_1.get());
Gilad Arnolda87340b2014-01-30 11:10:18 -080047 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080048
Gilad Arnolda87340b2014-01-30 11:10:18 -080049 // Assign a different value to the source variable.
50 source = 42;
Alex Deymo81f30e82014-01-08 14:33:06 -080051
Gilad Arnolda87340b2014-01-30 11:10:18 -080052 // Check that the content of the copy was not affected (distinct instance).
53 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080054
Gilad Arnolda87340b2014-01-30 11:10:18 -080055 // Generate and validate a second copy.
Alex Deymo63784a52014-05-28 10:46:14 -070056 UmTestUtils::ExpectVariableHasValue(42, &var);
Alex Deymo81f30e82014-01-08 14:33:06 -080057}
58
Alex Deymo63784a52014-05-28 10:46:14 -070059TEST_F(UmPollCopyVariableTest, SetFlagTest) {
Gilad Arnold9f7ab352014-04-16 15:27:37 -070060 // Tests that the set flag is being referred to as expected.
61 int source = 5;
62 bool is_set = false;
Gilad Arnold46eb5f62014-05-20 13:21:25 -070063 PollCopyVariable<int> var("var", source, &is_set);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070064
65 // Flag marked unset, nothing should be returned.
Alex Deymo63784a52014-05-28 10:46:14 -070066 UmTestUtils::ExpectVariableNotSet(&var);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070067
68 // Flag marked set, we should be getting a value.
69 is_set = true;
Alex Deymo63784a52014-05-28 10:46:14 -070070 UmTestUtils::ExpectVariableHasValue(5, &var);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070071}
72
Alex Deymo81f30e82014-01-08 14:33:06 -080073
Gilad Arnolda87340b2014-01-30 11:10:18 -080074class CopyConstructorTestClass {
75 public:
76 CopyConstructorTestClass(void) : copied_(false) {}
Gilad Arnoldc16fca22014-05-20 15:10:40 -070077 CopyConstructorTestClass(const CopyConstructorTestClass& other)
78 : copied_(true), val_(other.val_ * 2) {}
Alex Deymo81f30e82014-01-08 14:33:06 -080079
80 // Tells if the instance was constructed using the copy-constructor.
Gilad Arnoldc16fca22014-05-20 15:10:40 -070081 const bool copied_;
82
83 // An auxiliary internal value.
84 int val_ = 0;
Alex Deymo81f30e82014-01-08 14:33:06 -080085};
86
Alex Deymo81f30e82014-01-08 14:33:06 -080087
Alex Deymo63784a52014-05-28 10:46:14 -070088TEST_F(UmPollCopyVariableTest, UseCopyConstructorTest) {
Alex Vakulenko072359c2014-07-18 11:41:07 -070089 // Ensures that CopyVariables indeed uses the copy constructor.
Gilad Arnolda87340b2014-01-30 11:10:18 -080090 const CopyConstructorTestClass source;
91 ASSERT_FALSE(source.copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080092
Gilad Arnold46eb5f62014-05-20 13:21:25 -070093 PollCopyVariable<CopyConstructorTestClass> var("var", source);
Ben Chan02f7c1d2014-10-18 15:18:02 -070094 unique_ptr<const CopyConstructorTestClass> copy(
Alex Vakulenko88b591f2014-08-28 16:48:57 -070095 var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
96 ASSERT_NE(nullptr, copy.get());
Gilad Arnolda87340b2014-01-30 11:10:18 -080097 EXPECT_TRUE(copy->copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080098}
99
Alex Deymobd04b142014-03-18 15:00:05 -0700100
Alex Deymo63784a52014-05-28 10:46:14 -0700101class UmConstCopyVariableTest : public ::testing::Test {};
Alex Deymobd04b142014-03-18 15:00:05 -0700102
Alex Deymo63784a52014-05-28 10:46:14 -0700103TEST_F(UmConstCopyVariableTest, SimpleTest) {
Alex Deymobd04b142014-03-18 15:00:05 -0700104 int source = 5;
105 ConstCopyVariable<int> var("var", source);
Alex Deymo63784a52014-05-28 10:46:14 -0700106 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymobd04b142014-03-18 15:00:05 -0700107
108 // Ensure the value is cached.
109 source = 42;
Alex Deymo63784a52014-05-28 10:46:14 -0700110 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymobd04b142014-03-18 15:00:05 -0700111}
112
Alex Deymoc83baf62014-04-02 17:43:35 -0700113
Alex Deymo63784a52014-05-28 10:46:14 -0700114class UmCallCopyVariableTest : public ::testing::Test {};
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700115
116CopyConstructorTestClass test_func(CopyConstructorTestClass* obj) {
117 obj->val_++; // So we can check that the function was called.
118 return *obj;
119}
120
Alex Deymo63784a52014-05-28 10:46:14 -0700121TEST_F(UmCallCopyVariableTest, SimpleTest) {
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700122 // 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
Ben Chan02f7c1d2014-10-18 15:18:02 -0700133 unique_ptr<const CopyConstructorTestClass> copy(
Alex Deymo63784a52014-05-28 10:46:14 -0700134 var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700135 EXPECT_EQ(6, test_obj.val_); // Check that the function was called.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700136 ASSERT_NE(nullptr, copy.get());
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700137 EXPECT_TRUE(copy->copied_);
138 EXPECT_EQ(12, copy->val_); // Check that copying occurred once.
139}
140
Alex Deymo63784a52014-05-28 10:46:14 -0700141TEST_F(UmCallCopyVariableTest, NullTest) {
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700142 // Ensures that the variable returns null when the callback is null.
143
144 base::Callback<bool(void)> cb;
145 CallCopyVariable<bool> var("var", cb);
Alex Deymo63784a52014-05-28 10:46:14 -0700146 UmTestUtils::ExpectVariableNotSet(&var);
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700147}
148
Alex Deymo63784a52014-05-28 10:46:14 -0700149class UmAsyncCopyVariableTest : public ::testing::Test {
Alex Deymo509dd532015-06-10 14:11:05 -0700150 protected:
151 void SetUp() override {
152 loop_.SetAsCurrent();
153 }
154
Alex Deymo610277e2014-11-11 21:18:11 -0800155 void TearDown() override {
Alex Deymoc83baf62014-04-02 17:43:35 -0700156 // No remaining event on the main loop.
Alex Deymo509dd532015-06-10 14:11:05 -0700157 EXPECT_FALSE(loop_.PendingTasks());
Alex Deymoc83baf62014-04-02 17:43:35 -0700158 }
Alex Deymo509dd532015-06-10 14:11:05 -0700159
160
161 chromeos::FakeMessageLoop loop_{nullptr};
Alex Deymoc83baf62014-04-02 17:43:35 -0700162};
163
Alex Deymo63784a52014-05-28 10:46:14 -0700164TEST_F(UmAsyncCopyVariableTest, ConstructorTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700165 AsyncCopyVariable<int> var("var");
Alex Deymo63784a52014-05-28 10:46:14 -0700166 UmTestUtils::ExpectVariableNotSet(&var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700167 EXPECT_EQ(kVariableModeAsync, var.GetMode());
168}
169
Alex Deymo63784a52014-05-28 10:46:14 -0700170TEST_F(UmAsyncCopyVariableTest, SetValueTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700171 AsyncCopyVariable<int> var("var");
172 var.SetValue(5);
Alex Deymo63784a52014-05-28 10:46:14 -0700173 UmTestUtils::ExpectVariableHasValue(5, &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
Alex Deymo63784a52014-05-28 10:46:14 -0700178TEST_F(UmAsyncCopyVariableTest, UnsetValueTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700179 AsyncCopyVariable<int> var("var", 42);
180 var.UnsetValue();
Alex Deymo63784a52014-05-28 10:46:14 -0700181 UmTestUtils::ExpectVariableNotSet(&var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700182 // Execute all the pending observers.
Alex Deymo509dd532015-06-10 14:11:05 -0700183 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700184}
185
186class CallCounterObserver : public BaseVariable::ObserverInterface {
187 public:
188 void ValueChanged(BaseVariable* variable) {
189 calls_count_++;
190 }
191
192 int calls_count_ = 0;
193};
194
Alex Deymo63784a52014-05-28 10:46:14 -0700195TEST_F(UmAsyncCopyVariableTest, ObserverCalledTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700196 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);
Alex Deymo509dd532015-06-10 14:11:05 -0700203 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700204 EXPECT_EQ(1, observer.calls_count_);
205
206 // Check the same value doesn't.
207 var.SetValue(5);
Alex Deymo509dd532015-06-10 14:11:05 -0700208 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700209 EXPECT_EQ(1, observer.calls_count_);
210
211 // Check that unsetting a previously set value fires the notification.
212 var.UnsetValue();
Alex Deymo509dd532015-06-10 14:11:05 -0700213 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700214 EXPECT_EQ(2, observer.calls_count_);
215
216 // Check that unsetting again doesn't.
217 var.UnsetValue();
Alex Deymo509dd532015-06-10 14:11:05 -0700218 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymoc83baf62014-04-02 17:43:35 -0700219 EXPECT_EQ(2, observer.calls_count_);
220
221 var.RemoveObserver(&observer);
222}
223
Alex Deymo63784a52014-05-28 10:46:14 -0700224} // namespace chromeos_update_manager