blob: 490f94946a7a5528d9657b64d16806b48c4ca90e [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 Arnolda87340b2014-01-30 11:10:18 -08007#include <base/memory/scoped_ptr.h>
Alex Deymo81f30e82014-01-08 14:33:06 -08008#include <gtest/gtest.h>
Gilad Arnolda87340b2014-01-30 11:10:18 -08009
Alex Deymo6e97bb22014-02-05 16:46:16 -080010#include "update_engine/policy_manager/pmtest_utils.h"
Alex Deymoc83baf62014-04-02 17:43:35 -070011#include "update_engine/test_utils.h"
Alex Deymo81f30e82014-01-08 14:33:06 -080012
13using base::TimeDelta;
Alex Deymoc83baf62014-04-02 17:43:35 -070014using chromeos_update_engine::RunGMainLoopMaxIterations;
Alex Deymo81f30e82014-01-08 14:33:06 -080015
16namespace chromeos_policy_manager {
17
Gilad Arnolda87340b2014-01-30 11:10:18 -080018class PmCopyVariableTest : public ::testing::Test {
19 protected:
Alex Deymobd04b142014-03-18 15:00:05 -070020 TimeDelta default_timeout_ = TimeDelta::FromSeconds(1);
Gilad Arnolda87340b2014-01-30 11:10:18 -080021};
Alex Deymo81f30e82014-01-08 14:33:06 -080022
Alex Deymo81f30e82014-01-08 14:33:06 -080023
Gilad Arnolda87340b2014-01-30 11:10:18 -080024TEST_F(PmCopyVariableTest, SimpleTest) {
25 // Tests that copies are generated as intended.
26 int source = 5;
Alex Deymo0e433692014-02-20 07:23:03 -080027 CopyVariable<int> var("var", kVariableModePoll, source);
Alex Deymo81f30e82014-01-08 14:33:06 -080028
Gilad Arnolda87340b2014-01-30 11:10:18 -080029 // Generate and validate a copy.
30 scoped_ptr<const int> copy_1(var.GetValue(default_timeout_, NULL));
31 PMTEST_ASSERT_NOT_NULL(copy_1.get());
32 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.
41 scoped_ptr<const int> copy_2(var.GetValue(default_timeout_, NULL));
42 PMTEST_ASSERT_NOT_NULL(copy_2.get());
43 EXPECT_EQ(42, *copy_2);
Alex Deymo81f30e82014-01-08 14:33:06 -080044}
45
Gilad Arnold9f7ab352014-04-16 15:27:37 -070046TEST_F(PmCopyVariableTest, SetFlagTest) {
47 // Tests that the set flag is being referred to as expected.
48 int source = 5;
49 bool is_set = false;
50 CopyVariable<int> var("var", kVariableModePoll, source, &is_set);
51
52 // Flag marked unset, nothing should be returned.
53 PMTEST_ASSERT_NULL(var.GetValue(default_timeout_, NULL));
54
55 // Flag marked set, we should be getting a value.
56 is_set = true;
57 scoped_ptr<const int> copy(var.GetValue(default_timeout_, NULL));
58 PMTEST_ASSERT_NOT_NULL(copy.get());
59 EXPECT_EQ(5, *copy);
60}
61
Alex Deymo81f30e82014-01-08 14:33:06 -080062
Gilad Arnolda87340b2014-01-30 11:10:18 -080063class CopyConstructorTestClass {
64 public:
65 CopyConstructorTestClass(void) : copied_(false) {}
66 CopyConstructorTestClass(const CopyConstructorTestClass& /* ignored */)
Alex Deymo81f30e82014-01-08 14:33:06 -080067 : copied_(true) {}
68
69 // Tells if the instance was constructed using the copy-constructor.
70 bool copied_;
71};
72
Alex Deymo81f30e82014-01-08 14:33:06 -080073
Gilad Arnolda87340b2014-01-30 11:10:18 -080074TEST_F(PmCopyVariableTest, UseCopyConstructorTest) {
75 // Ensures that CopyVariables indeed uses the copy contructor.
76 const CopyConstructorTestClass source;
77 ASSERT_FALSE(source.copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080078
Alex Deymo0e433692014-02-20 07:23:03 -080079 CopyVariable<CopyConstructorTestClass> var("var", kVariableModePoll, source);
Gilad Arnolda87340b2014-01-30 11:10:18 -080080 scoped_ptr<const CopyConstructorTestClass> copy(
81 var.GetValue(default_timeout_, NULL));
82 PMTEST_ASSERT_NOT_NULL(copy.get());
83 EXPECT_TRUE(copy->copied_);
Alex Deymo81f30e82014-01-08 14:33:06 -080084}
85
Alex Deymobd04b142014-03-18 15:00:05 -070086
87class PmConstCopyVariableTest : public ::testing::Test {
88 protected:
89 TimeDelta default_timeout_ = TimeDelta::FromSeconds(1);
90};
91
92TEST_F(PmConstCopyVariableTest, SimpleTest) {
93 int source = 5;
94 ConstCopyVariable<int> var("var", source);
95 scoped_ptr<const int> copy(var.GetValue(default_timeout_, NULL));
96 PMTEST_ASSERT_NOT_NULL(copy.get());
97 EXPECT_EQ(5, *copy);
98
99 // Ensure the value is cached.
100 source = 42;
101 copy.reset(var.GetValue(default_timeout_, NULL));
102 PMTEST_ASSERT_NOT_NULL(copy.get());
103 EXPECT_EQ(5, *copy);
104}
105
Alex Deymoc83baf62014-04-02 17:43:35 -0700106
107class PmAsyncCopyVariableTest : public ::testing::Test {
108 public:
109 void TearDown() {
110 // No remaining event on the main loop.
111 EXPECT_EQ(0, RunGMainLoopMaxIterations(1));
112 }
113
114 protected:
115 TimeDelta default_timeout_ = TimeDelta::FromSeconds(1);
116};
117
118TEST_F(PmAsyncCopyVariableTest, ConstructorTest) {
119 AsyncCopyVariable<int> var("var");
120 PMTEST_EXPECT_NULL(var.GetValue(default_timeout_, NULL));
121 EXPECT_EQ(kVariableModeAsync, var.GetMode());
122}
123
124TEST_F(PmAsyncCopyVariableTest, SetValueTest) {
125 AsyncCopyVariable<int> var("var");
126 var.SetValue(5);
127 scoped_ptr<const int> copy(var.GetValue(default_timeout_, NULL));
128 PMTEST_ASSERT_NOT_NULL(copy.get());
129 EXPECT_EQ(5, *copy);
130 // Execute all the pending observers.
131 RunGMainLoopMaxIterations(100);
132}
133
134TEST_F(PmAsyncCopyVariableTest, UnsetValueTest) {
135 AsyncCopyVariable<int> var("var", 42);
136 var.UnsetValue();
137 PMTEST_EXPECT_NULL(var.GetValue(default_timeout_, NULL));
138 // Execute all the pending observers.
139 RunGMainLoopMaxIterations(100);
140}
141
142class CallCounterObserver : public BaseVariable::ObserverInterface {
143 public:
144 void ValueChanged(BaseVariable* variable) {
145 calls_count_++;
146 }
147
148 int calls_count_ = 0;
149};
150
151TEST_F(PmAsyncCopyVariableTest, ObserverCalledTest) {
152 AsyncCopyVariable<int> var("var", 42);
153 CallCounterObserver observer;
154 var.AddObserver(&observer);
155 EXPECT_EQ(0, observer.calls_count_);
156
157 // Check that a different value fires the notification.
158 var.SetValue(5);
159 RunGMainLoopMaxIterations(100);
160 EXPECT_EQ(1, observer.calls_count_);
161
162 // Check the same value doesn't.
163 var.SetValue(5);
164 RunGMainLoopMaxIterations(100);
165 EXPECT_EQ(1, observer.calls_count_);
166
167 // Check that unsetting a previously set value fires the notification.
168 var.UnsetValue();
169 RunGMainLoopMaxIterations(100);
170 EXPECT_EQ(2, observer.calls_count_);
171
172 // Check that unsetting again doesn't.
173 var.UnsetValue();
174 RunGMainLoopMaxIterations(100);
175 EXPECT_EQ(2, observer.calls_count_);
176
177 var.RemoveObserver(&observer);
178}
179
Alex Deymo81f30e82014-01-08 14:33:06 -0800180} // namespace chromeos_policy_manager