blob: 65f7f51ef987fd0c2b0ba5a3335cf9d3249f9000 [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
Alex Deymo10875d92014-11-10 21:52:57 -080015using chromeos_update_engine::test_utils::RunGMainLoopMaxIterations;
Ben Chan02f7c1d2014-10-18 15:18:02 -070016using std::unique_ptr;
Alex Deymo81f30e82014-01-08 14:33:06 -080017
Alex Deymo63784a52014-05-28 10:46:14 -070018namespace chromeos_update_manager {
Alex Deymo81f30e82014-01-08 14:33:06 -080019
Alex Deymo63784a52014-05-28 10:46:14 -070020class UmPollCopyVariableTest : public ::testing::Test {};
Alex Deymo81f30e82014-01-08 14:33:06 -080021
Alex Deymo81f30e82014-01-08 14:33:06 -080022
Alex Deymo63784a52014-05-28 10:46:14 -070023TEST_F(UmPollCopyVariableTest, SimpleTest) {
Gilad Arnolda87340b2014-01-30 11:10:18 -080024 // Tests that copies are generated as intended.
25 int source = 5;
Gilad Arnold46eb5f62014-05-20 13:21:25 -070026 PollCopyVariable<int> var("var", source);
Alex Deymo81f30e82014-01-08 14:33:06 -080027
Gilad Arnolda87340b2014-01-30 11:10:18 -080028 // Generate and validate a copy.
Ben Chan02f7c1d2014-10-18 15:18:02 -070029 unique_ptr<const int> copy_1(var.GetValue(
Alex Vakulenko88b591f2014-08-28 16:48:57 -070030 UmTestUtils::DefaultTimeout(), nullptr));
31 ASSERT_NE(nullptr, copy_1.get());
Gilad Arnolda87340b2014-01-30 11:10:18 -080032 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080033
Gilad Arnolda87340b2014-01-30 11:10:18 -080034 // Assign a different value to the source variable.
35 source = 42;
Alex Deymo81f30e82014-01-08 14:33:06 -080036
Gilad Arnolda87340b2014-01-30 11:10:18 -080037 // Check that the content of the copy was not affected (distinct instance).
38 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080039
Gilad Arnolda87340b2014-01-30 11:10:18 -080040 // Generate and validate a second copy.
Alex Deymo63784a52014-05-28 10:46:14 -070041 UmTestUtils::ExpectVariableHasValue(42, &var);
Alex Deymo81f30e82014-01-08 14:33:06 -080042}
43
Alex Deymo63784a52014-05-28 10:46:14 -070044TEST_F(UmPollCopyVariableTest, SetFlagTest) {
Gilad Arnold9f7ab352014-04-16 15:27:37 -070045 // Tests that the set flag is being referred to as expected.
46 int source = 5;
47 bool is_set = false;
Gilad Arnold46eb5f62014-05-20 13:21:25 -070048 PollCopyVariable<int> var("var", source, &is_set);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070049
50 // Flag marked unset, nothing should be returned.
Alex Deymo63784a52014-05-28 10:46:14 -070051 UmTestUtils::ExpectVariableNotSet(&var);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070052
53 // Flag marked set, we should be getting a value.
54 is_set = true;
Alex Deymo63784a52014-05-28 10:46:14 -070055 UmTestUtils::ExpectVariableHasValue(5, &var);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070056}
57
Alex Deymo81f30e82014-01-08 14:33:06 -080058
Gilad Arnolda87340b2014-01-30 11:10:18 -080059class CopyConstructorTestClass {
60 public:
61 CopyConstructorTestClass(void) : copied_(false) {}
Gilad Arnoldc16fca22014-05-20 15:10:40 -070062 CopyConstructorTestClass(const CopyConstructorTestClass& other)
63 : copied_(true), val_(other.val_ * 2) {}
Alex Deymo81f30e82014-01-08 14:33:06 -080064
65 // Tells if the instance was constructed using the copy-constructor.
Gilad Arnoldc16fca22014-05-20 15:10:40 -070066 const bool copied_;
67
68 // An auxiliary internal value.
69 int val_ = 0;
Alex Deymo81f30e82014-01-08 14:33:06 -080070};
71
Alex Deymo81f30e82014-01-08 14:33:06 -080072
Alex Deymo63784a52014-05-28 10:46:14 -070073TEST_F(UmPollCopyVariableTest, UseCopyConstructorTest) {
Alex Vakulenko072359c2014-07-18 11:41:07 -070074 // Ensures that CopyVariables indeed uses the copy constructor.
Gilad Arnolda87340b2014-01-30 11:10:18 -080075 const CopyConstructorTestClass source;
76 ASSERT_FALSE(source.copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080077
Gilad Arnold46eb5f62014-05-20 13:21:25 -070078 PollCopyVariable<CopyConstructorTestClass> var("var", source);
Ben Chan02f7c1d2014-10-18 15:18:02 -070079 unique_ptr<const CopyConstructorTestClass> copy(
Alex Vakulenko88b591f2014-08-28 16:48:57 -070080 var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
81 ASSERT_NE(nullptr, copy.get());
Gilad Arnolda87340b2014-01-30 11:10:18 -080082 EXPECT_TRUE(copy->copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080083}
84
Alex Deymobd04b142014-03-18 15:00:05 -070085
Alex Deymo63784a52014-05-28 10:46:14 -070086class UmConstCopyVariableTest : public ::testing::Test {};
Alex Deymobd04b142014-03-18 15:00:05 -070087
Alex Deymo63784a52014-05-28 10:46:14 -070088TEST_F(UmConstCopyVariableTest, SimpleTest) {
Alex Deymobd04b142014-03-18 15:00:05 -070089 int source = 5;
90 ConstCopyVariable<int> var("var", source);
Alex Deymo63784a52014-05-28 10:46:14 -070091 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymobd04b142014-03-18 15:00:05 -070092
93 // Ensure the value is cached.
94 source = 42;
Alex Deymo63784a52014-05-28 10:46:14 -070095 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymobd04b142014-03-18 15:00:05 -070096}
97
Alex Deymoc83baf62014-04-02 17:43:35 -070098
Alex Deymo63784a52014-05-28 10:46:14 -070099class UmCallCopyVariableTest : public ::testing::Test {};
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700100
101CopyConstructorTestClass test_func(CopyConstructorTestClass* obj) {
102 obj->val_++; // So we can check that the function was called.
103 return *obj;
104}
105
Alex Deymo63784a52014-05-28 10:46:14 -0700106TEST_F(UmCallCopyVariableTest, SimpleTest) {
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700107 // Tests that the returned value is generated by copying the value returned by
108 // the function call.
109
110 CopyConstructorTestClass test_obj;
111 ASSERT_FALSE(test_obj.copied_);
112 test_obj.val_ = 5;
113
114 base::Callback<CopyConstructorTestClass(void)> cb = base::Bind(
115 test_func, &test_obj);
116 CallCopyVariable<CopyConstructorTestClass> var("var", cb);
117
Ben Chan02f7c1d2014-10-18 15:18:02 -0700118 unique_ptr<const CopyConstructorTestClass> copy(
Alex Deymo63784a52014-05-28 10:46:14 -0700119 var.GetValue(UmTestUtils::DefaultTimeout(), nullptr));
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700120 EXPECT_EQ(6, test_obj.val_); // Check that the function was called.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700121 ASSERT_NE(nullptr, copy.get());
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700122 EXPECT_TRUE(copy->copied_);
123 EXPECT_EQ(12, copy->val_); // Check that copying occurred once.
124}
125
Alex Deymo63784a52014-05-28 10:46:14 -0700126TEST_F(UmCallCopyVariableTest, NullTest) {
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700127 // Ensures that the variable returns null when the callback is null.
128
129 base::Callback<bool(void)> cb;
130 CallCopyVariable<bool> var("var", cb);
Alex Deymo63784a52014-05-28 10:46:14 -0700131 UmTestUtils::ExpectVariableNotSet(&var);
Gilad Arnoldc16fca22014-05-20 15:10:40 -0700132}
133
Alex Deymo63784a52014-05-28 10:46:14 -0700134class UmAsyncCopyVariableTest : public ::testing::Test {
Alex Deymoc83baf62014-04-02 17:43:35 -0700135 public:
Alex Deymo610277e2014-11-11 21:18:11 -0800136 void TearDown() override {
Alex Deymoc83baf62014-04-02 17:43:35 -0700137 // No remaining event on the main loop.
138 EXPECT_EQ(0, RunGMainLoopMaxIterations(1));
139 }
Alex Deymoc83baf62014-04-02 17:43:35 -0700140};
141
Alex Deymo63784a52014-05-28 10:46:14 -0700142TEST_F(UmAsyncCopyVariableTest, ConstructorTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700143 AsyncCopyVariable<int> var("var");
Alex Deymo63784a52014-05-28 10:46:14 -0700144 UmTestUtils::ExpectVariableNotSet(&var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700145 EXPECT_EQ(kVariableModeAsync, var.GetMode());
146}
147
Alex Deymo63784a52014-05-28 10:46:14 -0700148TEST_F(UmAsyncCopyVariableTest, SetValueTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700149 AsyncCopyVariable<int> var("var");
150 var.SetValue(5);
Alex Deymo63784a52014-05-28 10:46:14 -0700151 UmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700152 // Execute all the pending observers.
153 RunGMainLoopMaxIterations(100);
154}
155
Alex Deymo63784a52014-05-28 10:46:14 -0700156TEST_F(UmAsyncCopyVariableTest, UnsetValueTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700157 AsyncCopyVariable<int> var("var", 42);
158 var.UnsetValue();
Alex Deymo63784a52014-05-28 10:46:14 -0700159 UmTestUtils::ExpectVariableNotSet(&var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700160 // Execute all the pending observers.
161 RunGMainLoopMaxIterations(100);
162}
163
164class CallCounterObserver : public BaseVariable::ObserverInterface {
165 public:
166 void ValueChanged(BaseVariable* variable) {
167 calls_count_++;
168 }
169
170 int calls_count_ = 0;
171};
172
Alex Deymo63784a52014-05-28 10:46:14 -0700173TEST_F(UmAsyncCopyVariableTest, ObserverCalledTest) {
Alex Deymoc83baf62014-04-02 17:43:35 -0700174 AsyncCopyVariable<int> var("var", 42);
175 CallCounterObserver observer;
176 var.AddObserver(&observer);
177 EXPECT_EQ(0, observer.calls_count_);
178
179 // Check that a different value fires the notification.
180 var.SetValue(5);
181 RunGMainLoopMaxIterations(100);
182 EXPECT_EQ(1, observer.calls_count_);
183
184 // Check the same value doesn't.
185 var.SetValue(5);
186 RunGMainLoopMaxIterations(100);
187 EXPECT_EQ(1, observer.calls_count_);
188
189 // Check that unsetting a previously set value fires the notification.
190 var.UnsetValue();
191 RunGMainLoopMaxIterations(100);
192 EXPECT_EQ(2, observer.calls_count_);
193
194 // Check that unsetting again doesn't.
195 var.UnsetValue();
196 RunGMainLoopMaxIterations(100);
197 EXPECT_EQ(2, observer.calls_count_);
198
199 var.RemoveObserver(&observer);
200}
201
Alex Deymo63784a52014-05-28 10:46:14 -0700202} // namespace chromeos_update_manager