blob: 279c7298c204703a1ca8fb265c36fb861830765a [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 Deymobd04b142014-03-18 15:00:05 -07005#include "update_engine/policy_manager/generic_variables.h"
6
Gilad Arnoldc16fca22014-05-20 15:10:40 -07007#include <base/callback.h>
Gilad Arnolda87340b2014-01-30 11:10:18 -08008#include <base/memory/scoped_ptr.h>
Alex Deymo81f30e82014-01-08 14:33:06 -08009#include <gtest/gtest.h>
Gilad Arnolda87340b2014-01-30 11:10:18 -080010
Alex Deymo6e97bb22014-02-05 16:46:16 -080011#include "update_engine/policy_manager/pmtest_utils.h"
Alex Deymoc83baf62014-04-02 17:43:35 -070012#include "update_engine/test_utils.h"
Alex Deymo81f30e82014-01-08 14:33:06 -080013
14using base::TimeDelta;
Alex Deymoc83baf62014-04-02 17:43:35 -070015using chromeos_update_engine::RunGMainLoopMaxIterations;
Alex Deymo81f30e82014-01-08 14:33:06 -080016
17namespace chromeos_policy_manager {
18
Gilad Arnold67ed78d2014-04-23 13:17:46 -070019class PmCopyVariableTest : public ::testing::Test {};
Alex Deymo81f30e82014-01-08 14:33:06 -080020
Alex Deymo81f30e82014-01-08 14:33:06 -080021
Gilad Arnolda87340b2014-01-30 11:10:18 -080022TEST_F(PmCopyVariableTest, SimpleTest) {
23 // Tests that copies are generated as intended.
24 int source = 5;
Alex Deymo0e433692014-02-20 07:23:03 -080025 CopyVariable<int> var("var", kVariableModePoll, source);
Alex Deymo81f30e82014-01-08 14:33:06 -080026
Gilad Arnolda87340b2014-01-30 11:10:18 -080027 // Generate and validate a copy.
Gilad Arnold67ed78d2014-04-23 13:17:46 -070028 scoped_ptr<const int> copy_1(var.GetValue(
29 PmTestUtils::DefaultTimeout(), NULL));
Gilad Arnolda87340b2014-01-30 11:10:18 -080030 PMTEST_ASSERT_NOT_NULL(copy_1.get());
31 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080032
Gilad Arnolda87340b2014-01-30 11:10:18 -080033 // Assign a different value to the source variable.
34 source = 42;
Alex Deymo81f30e82014-01-08 14:33:06 -080035
Gilad Arnolda87340b2014-01-30 11:10:18 -080036 // Check that the content of the copy was not affected (distinct instance).
37 EXPECT_EQ(5, *copy_1);
Alex Deymo81f30e82014-01-08 14:33:06 -080038
Gilad Arnolda87340b2014-01-30 11:10:18 -080039 // Generate and validate a second copy.
Gilad Arnold67ed78d2014-04-23 13:17:46 -070040 PmTestUtils::ExpectVariableHasValue(42, &var);
Alex Deymo81f30e82014-01-08 14:33:06 -080041}
42
Gilad Arnold9f7ab352014-04-16 15:27:37 -070043TEST_F(PmCopyVariableTest, SetFlagTest) {
44 // Tests that the set flag is being referred to as expected.
45 int source = 5;
46 bool is_set = false;
47 CopyVariable<int> var("var", kVariableModePoll, source, &is_set);
48
49 // Flag marked unset, nothing should be returned.
Gilad Arnold67ed78d2014-04-23 13:17:46 -070050 PmTestUtils::ExpectVariableNotSet(&var);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070051
52 // Flag marked set, we should be getting a value.
53 is_set = true;
Gilad Arnold67ed78d2014-04-23 13:17:46 -070054 PmTestUtils::ExpectVariableHasValue(5, &var);
Gilad Arnold9f7ab352014-04-16 15:27:37 -070055}
56
Alex Deymo81f30e82014-01-08 14:33:06 -080057
Gilad Arnolda87340b2014-01-30 11:10:18 -080058class CopyConstructorTestClass {
59 public:
60 CopyConstructorTestClass(void) : copied_(false) {}
Gilad Arnoldc16fca22014-05-20 15:10:40 -070061 CopyConstructorTestClass(const CopyConstructorTestClass& other)
62 : copied_(true), val_(other.val_ * 2) {}
Alex Deymo81f30e82014-01-08 14:33:06 -080063
64 // Tells if the instance was constructed using the copy-constructor.
Gilad Arnoldc16fca22014-05-20 15:10:40 -070065 const bool copied_;
66
67 // An auxiliary internal value.
68 int val_ = 0;
Alex Deymo81f30e82014-01-08 14:33:06 -080069};
70
Alex Deymo81f30e82014-01-08 14:33:06 -080071
Gilad Arnolda87340b2014-01-30 11:10:18 -080072TEST_F(PmCopyVariableTest, UseCopyConstructorTest) {
73 // Ensures that CopyVariables indeed uses the copy contructor.
74 const CopyConstructorTestClass source;
75 ASSERT_FALSE(source.copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080076
Alex Deymo0e433692014-02-20 07:23:03 -080077 CopyVariable<CopyConstructorTestClass> var("var", kVariableModePoll, source);
Gilad Arnolda87340b2014-01-30 11:10:18 -080078 scoped_ptr<const CopyConstructorTestClass> copy(
Gilad Arnold67ed78d2014-04-23 13:17:46 -070079 var.GetValue(PmTestUtils::DefaultTimeout(), NULL));
Gilad Arnolda87340b2014-01-30 11:10:18 -080080 PMTEST_ASSERT_NOT_NULL(copy.get());
81 EXPECT_TRUE(copy->copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080082}
83
Alex Deymobd04b142014-03-18 15:00:05 -070084
Gilad Arnold67ed78d2014-04-23 13:17:46 -070085class PmConstCopyVariableTest : public ::testing::Test {};
Alex Deymobd04b142014-03-18 15:00:05 -070086
87TEST_F(PmConstCopyVariableTest, SimpleTest) {
88 int source = 5;
89 ConstCopyVariable<int> var("var", source);
Gilad Arnold67ed78d2014-04-23 13:17:46 -070090 PmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymobd04b142014-03-18 15:00:05 -070091
92 // Ensure the value is cached.
93 source = 42;
Gilad Arnold67ed78d2014-04-23 13:17:46 -070094 PmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymobd04b142014-03-18 15:00:05 -070095}
96
Alex Deymoc83baf62014-04-02 17:43:35 -070097
Gilad Arnoldc16fca22014-05-20 15:10:40 -070098class PmCallCopyVariableTest : public ::testing::Test {};
99
100CopyConstructorTestClass test_func(CopyConstructorTestClass* obj) {
101 obj->val_++; // So we can check that the function was called.
102 return *obj;
103}
104
105TEST_F(PmCallCopyVariableTest, SimpleTest) {
106 // Tests that the returned value is generated by copying the value returned by
107 // the function call.
108
109 CopyConstructorTestClass test_obj;
110 ASSERT_FALSE(test_obj.copied_);
111 test_obj.val_ = 5;
112
113 base::Callback<CopyConstructorTestClass(void)> cb = base::Bind(
114 test_func, &test_obj);
115 CallCopyVariable<CopyConstructorTestClass> var("var", cb);
116
117 scoped_ptr<const CopyConstructorTestClass> copy(
118 var.GetValue(PmTestUtils::DefaultTimeout(), nullptr));
119 EXPECT_EQ(6, test_obj.val_); // Check that the function was called.
120 PMTEST_ASSERT_NOT_NULL(copy.get());
121 EXPECT_TRUE(copy->copied_);
122 EXPECT_EQ(12, copy->val_); // Check that copying occurred once.
123}
124
125TEST_F(PmCallCopyVariableTest, NullTest) {
126 // Ensures that the variable returns null when the callback is null.
127
128 base::Callback<bool(void)> cb;
129 CallCopyVariable<bool> var("var", cb);
130 PmTestUtils::ExpectVariableNotSet(&var);
131}
132
Alex Deymoc83baf62014-04-02 17:43:35 -0700133class PmAsyncCopyVariableTest : public ::testing::Test {
134 public:
135 void TearDown() {
136 // No remaining event on the main loop.
137 EXPECT_EQ(0, RunGMainLoopMaxIterations(1));
138 }
Alex Deymoc83baf62014-04-02 17:43:35 -0700139};
140
141TEST_F(PmAsyncCopyVariableTest, ConstructorTest) {
142 AsyncCopyVariable<int> var("var");
Gilad Arnold67ed78d2014-04-23 13:17:46 -0700143 PmTestUtils::ExpectVariableNotSet(&var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700144 EXPECT_EQ(kVariableModeAsync, var.GetMode());
145}
146
147TEST_F(PmAsyncCopyVariableTest, SetValueTest) {
148 AsyncCopyVariable<int> var("var");
149 var.SetValue(5);
Gilad Arnold67ed78d2014-04-23 13:17:46 -0700150 PmTestUtils::ExpectVariableHasValue(5, &var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700151 // Execute all the pending observers.
152 RunGMainLoopMaxIterations(100);
153}
154
155TEST_F(PmAsyncCopyVariableTest, UnsetValueTest) {
156 AsyncCopyVariable<int> var("var", 42);
157 var.UnsetValue();
Gilad Arnold67ed78d2014-04-23 13:17:46 -0700158 PmTestUtils::ExpectVariableNotSet(&var);
Alex Deymoc83baf62014-04-02 17:43:35 -0700159 // Execute all the pending observers.
160 RunGMainLoopMaxIterations(100);
161}
162
163class CallCounterObserver : public BaseVariable::ObserverInterface {
164 public:
165 void ValueChanged(BaseVariable* variable) {
166 calls_count_++;
167 }
168
169 int calls_count_ = 0;
170};
171
172TEST_F(PmAsyncCopyVariableTest, ObserverCalledTest) {
173 AsyncCopyVariable<int> var("var", 42);
174 CallCounterObserver observer;
175 var.AddObserver(&observer);
176 EXPECT_EQ(0, observer.calls_count_);
177
178 // Check that a different value fires the notification.
179 var.SetValue(5);
180 RunGMainLoopMaxIterations(100);
181 EXPECT_EQ(1, observer.calls_count_);
182
183 // Check the same value doesn't.
184 var.SetValue(5);
185 RunGMainLoopMaxIterations(100);
186 EXPECT_EQ(1, observer.calls_count_);
187
188 // Check that unsetting a previously set value fires the notification.
189 var.UnsetValue();
190 RunGMainLoopMaxIterations(100);
191 EXPECT_EQ(2, observer.calls_count_);
192
193 // Check that unsetting again doesn't.
194 var.UnsetValue();
195 RunGMainLoopMaxIterations(100);
196 EXPECT_EQ(2, observer.calls_count_);
197
198 var.RemoveObserver(&observer);
199}
200
Alex Deymo81f30e82014-01-08 14:33:06 -0800201} // namespace chromeos_policy_manager