blob: f0ea60c8cccfbbbc20323400e354a9d4111c0ed5 [file] [log] [blame]
Alex Deymo81f30e82014-01-08 14:33:06 -08001// 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 Deymo63784a52014-05-28 10:46:14 -07005#include "update_engine/update_manager/generic_variables.h"
Alex Deymobd04b142014-03-18 15:00:05 -07006
Ben Chan02f7c1d2014-10-18 15:18:02 -07007#include <memory>
8
Gilad Arnoldc16fca22014-05-20 15:10:40 -07009#include <base/callback.h>
Alex Deymo81f30e82014-01-08 14:33:06 -080010#include <gtest/gtest.h>
Gilad Arnolda87340b2014-01-30 11:10:18 -080011
Alex Deymoc83baf62014-04-02 17:43:35 -070012#include "update_engine/test_utils.h"
Alex Deymo63784a52014-05-28 10:46:14 -070013#include "update_engine/update_manager/umtest_utils.h"
Alex Deymo81f30e82014-01-08 14:33:06 -080014
15using base::TimeDelta;
Alex Deymoc83baf62014-04-02 17:43:35 -070016using chromeos_update_engine::RunGMainLoopMaxIterations;
Ben Chan02f7c1d2014-10-18 15:18:02 -070017using std::unique_ptr;
Alex Deymo81f30e82014-01-08 14:33:06 -080018
Alex Deymo63784a52014-05-28 10:46:14 -070019namespace chromeos_update_manager {
Alex Deymo81f30e82014-01-08 14:33:06 -080020
Alex Deymo63784a52014-05-28 10:46:14 -070021class UmPollCopyVariableTest : public ::testing::Test {};
Alex Deymo81f30e82014-01-08 14:33:06 -080022
Alex Deymo81f30e82014-01-08 14:33:06 -080023
Alex Deymo63784a52014-05-28 10:46:14 -070024TEST_F(UmPollCopyVariableTest, SimpleTest) {
Gilad Arnolda87340b2014-01-30 11:10:18 -080025 // Tests that copies are generated as intended.
26 int source = 5;
Gilad Arnold46eb5f62014-05-20 13:21:25 -070027 PollCopyVariable<int> var("var", source);
Alex Deymo81f30e82014-01-08 14:33:06 -080028
Gilad Arnolda87340b2014-01-30 11:10:18 -080029 // Generate and validate a copy.
Ben Chan02f7c1d2014-10-18 15:18:02 -070030 unique_ptr<const int> copy_1(var.GetValue(
Alex Vakulenko88b591f2014-08-28 16:48:57 -070031 UmTestUtils::DefaultTimeout(), nullptr));
32 ASSERT_NE(nullptr, copy_1.get());
Gilad Arnolda87340b2014-01-30 11:10:18 -080033 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080034
Gilad Arnolda87340b2014-01-30 11:10:18 -080035 // Assign a different value to the source variable.
36 source = 42;
Alex Deymo81f30e82014-01-08 14:33:06 -080037
Gilad Arnolda87340b2014-01-30 11:10:18 -080038 // Check that the content of the copy was not affected (distinct instance).
39 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080040
Gilad Arnolda87340b2014-01-30 11:10:18 -080041 // Generate and validate a second copy.
Alex Deymo63784a52014-05-28 10:46:14 -070042 UmTestUtils::ExpectVariableHasValue(42, &var);
Alex Deymo81f30e82014-01-08 14:33:06 -080043}
44
Alex Deymo63784a52014-05-28 10:46:14 -070045TEST_F(UmPollCopyVariableTest, SetFlagTest) {
Gilad Arnold9f7ab352014-04-16 15:27:37 -070046 // Tests that the set flag is being referred to as expected.
47 int source = 5;
48 bool is_set = false;
Gilad Arnold46eb5f62014-05-20 13:21:25 -070049 PollCopyVariable<int> var("var", source, &is_set);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070050
51 // Flag marked unset, nothing should be returned.
Alex Deymo63784a52014-05-28 10:46:14 -070052 UmTestUtils::ExpectVariableNotSet(&var);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070053
54 // Flag marked set, we should be getting a value.
55 is_set = true;
Alex Deymo63784a52014-05-28 10:46:14 -070056 UmTestUtils::ExpectVariableHasValue(5, &var);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070057}
58
Alex Deymo81f30e82014-01-08 14:33:06 -080059
Gilad Arnolda87340b2014-01-30 11:10:18 -080060class CopyConstructorTestClass {
61 public:
62 CopyConstructorTestClass(void) : copied_(false) {}
Gilad Arnoldc16fca22014-05-20 15:10:40 -070063 CopyConstructorTestClass(const CopyConstructorTestClass& other)
64 : copied_(true), val_(other.val_ * 2) {}
Alex Deymo81f30e82014-01-08 14:33:06 -080065
66 // Tells if the instance was constructed using the copy-constructor.
Gilad Arnoldc16fca22014-05-20 15:10:40 -070067 const bool copied_;
68
69 // An auxiliary internal value.
70 int val_ = 0;
Alex Deymo81f30e82014-01-08 14:33:06 -080071};
72
Alex Deymo81f30e82014-01-08 14:33:06 -080073
Alex Deymo63784a52014-05-28 10:46:14 -070074TEST_F(UmPollCopyVariableTest, UseCopyConstructorTest) {
Alex Vakulenko072359c2014-07-18 11:41:07 -070075 // Ensures that CopyVariables indeed uses the copy constructor.
Gilad Arnolda87340b2014-01-30 11:10:18 -080076 const CopyConstructorTestClass source;
77 ASSERT_FALSE(source.copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080078
Gilad Arnold46eb5f62014-05-20 13:21:25 -070079 PollCopyVariable<CopyConstructorTestClass> var("var", source);
Ben Chan02f7c1d2014-10-18 15:18:02 -070080 unique_ptr<const CopyConstructorTestClass> copy(
Alex Vakulenko88b591f2014-08-28 16:48:57 -070081 var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
82 ASSERT_NE(nullptr, copy.get());
Gilad Arnolda87340b2014-01-30 11:10:18 -080083 EXPECT_TRUE(copy->copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080084}
85
Alex Deymobd04b142014-03-18 15:00:05 -070086
Alex Deymo63784a52014-05-28 10:46:14 -070087class UmConstCopyVariableTest : public ::testing::Test {};
Alex Deymobd04b142014-03-18 15:00:05 -070088
Alex Deymo63784a52014-05-28 10:46:14 -070089TEST_F(UmConstCopyVariableTest, SimpleTest) {
Alex Deymobd04b142014-03-18 15:00:05 -070090 int source = 5;
91 ConstCopyVariable<int> var("var", source);
Alex Deymo63784a52014-05-28 10:46:14 -070092 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymobd04b142014-03-18 15:00:05 -070093
94 // Ensure the value is cached.
95 source = 42;
Alex Deymo63784a52014-05-28 10:46:14 -070096 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymobd04b142014-03-18 15:00:05 -070097}
98
Alex Deymoc83baf62014-04-02 17:43:35 -070099
Alex Deymo63784a52014-05-28 10:46:14 -0700100class UmCallCopyVariableTest : public ::testing::Test {};
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700101
102CopyConstructorTestClass test_func(CopyConstructorTestClass* obj) {
103 obj->val_++; // So we can check that the function was called.
104 return *obj;
105}
106
Alex Deymo63784a52014-05-28 10:46:14 -0700107TEST_F(UmCallCopyVariableTest, SimpleTest) {
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700108 // Tests that the returned value is generated by copying the value returned by
109 // the function call.
110
111 CopyConstructorTestClass test_obj;
112 ASSERT_FALSE(test_obj.copied_);
113 test_obj.val_ = 5;
114
115 base::Callback<CopyConstructorTestClass(void)> cb = base::Bind(
116 test_func, &test_obj);
117 CallCopyVariable<CopyConstructorTestClass> var("var", cb);
118
Ben Chan02f7c1d2014-10-18 15:18:02 -0700119 unique_ptr<const CopyConstructorTestClass> copy(
Alex Deymo63784a52014-05-28 10:46:14 -0700120 var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700121 EXPECT_EQ(6, test_obj.val_); // Check that the function was called.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700122 ASSERT_NE(nullptr, copy.get());
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700123 EXPECT_TRUE(copy->copied_);
124 EXPECT_EQ(12, copy->val_); // Check that copying occurred once.
125}
126
Alex Deymo63784a52014-05-28 10:46:14 -0700127TEST_F(UmCallCopyVariableTest, NullTest) {
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700128 // Ensures that the variable returns null when the callback is null.
129
130 base::Callback<bool(void)> cb;
131 CallCopyVariable<bool> var("var", cb);
Alex Deymo63784a52014-05-28 10:46:14 -0700132 UmTestUtils::ExpectVariableNotSet(&var);
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700133}
134
Alex Deymo63784a52014-05-28 10:46:14 -0700135class UmAsyncCopyVariableTest : public ::testing::Test {
Alex Deymoc83baf62014-04-02 17:43:35 -0700136 public:
137 void TearDown() {
138 // No remaining event on the main loop.
139 EXPECT_EQ(0, RunGMainLoopMaxIterations(1));
140 }
Alex Deymoc83baf62014-04-02 17:43:35 -0700141};
142
Alex Deymo63784a52014-05-28 10:46:14 -0700143TEST_F(UmAsyncCopyVariableTest, ConstructorTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700144 AsyncCopyVariable<int> var("var");
Alex Deymo63784a52014-05-28 10:46:14 -0700145 UmTestUtils::ExpectVariableNotSet(&var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700146 EXPECT_EQ(kVariableModeAsync, var.GetMode());
147}
148
Alex Deymo63784a52014-05-28 10:46:14 -0700149TEST_F(UmAsyncCopyVariableTest, SetValueTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700150 AsyncCopyVariable<int> var("var");
151 var.SetValue(5);
Alex Deymo63784a52014-05-28 10:46:14 -0700152 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700153 // Execute all the pending observers.
154 RunGMainLoopMaxIterations(100);
155}
156
Alex Deymo63784a52014-05-28 10:46:14 -0700157TEST_F(UmAsyncCopyVariableTest, UnsetValueTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700158 AsyncCopyVariable<int> var("var", 42);
159 var.UnsetValue();
Alex Deymo63784a52014-05-28 10:46:14 -0700160 UmTestUtils::ExpectVariableNotSet(&var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700161 // Execute all the pending observers.
162 RunGMainLoopMaxIterations(100);
163}
164
165class CallCounterObserver : public BaseVariable::ObserverInterface {
166 public:
167 void ValueChanged(BaseVariable* variable) {
168 calls_count_++;
169 }
170
171 int calls_count_ = 0;
172};
173
Alex Deymo63784a52014-05-28 10:46:14 -0700174TEST_F(UmAsyncCopyVariableTest, ObserverCalledTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700175 AsyncCopyVariable<int> var("var", 42);
176 CallCounterObserver observer;
177 var.AddObserver(&observer);
178 EXPECT_EQ(0, observer.calls_count_);
179
180 // Check that a different value fires the notification.
181 var.SetValue(5);
182 RunGMainLoopMaxIterations(100);
183 EXPECT_EQ(1, observer.calls_count_);
184
185 // Check the same value doesn't.
186 var.SetValue(5);
187 RunGMainLoopMaxIterations(100);
188 EXPECT_EQ(1, observer.calls_count_);
189
190 // Check that unsetting a previously set value fires the notification.
191 var.UnsetValue();
192 RunGMainLoopMaxIterations(100);
193 EXPECT_EQ(2, observer.calls_count_);
194
195 // Check that unsetting again doesn't.
196 var.UnsetValue();
197 RunGMainLoopMaxIterations(100);
198 EXPECT_EQ(2, observer.calls_count_);
199
200 var.RemoveObserver(&observer);
201}
202
Alex Deymo63784a52014-05-28 10:46:14 -0700203} // namespace chromeos_update_manager