PM: Move Variable content testing logic into a common unittest module.
Testing the content of variables is done frequently when testing various
providers. This results in plenty of boilerplate code, as well as
numerous friend declarations in the different FooVariable class
definitions.
This CL introduces a PmTestUtils helper class providing common Variable
testing methods that test fixtures can use.
BUG=chromium:366259
TEST=Unit tests.
Change-Id: I7bb1cea080c3b79c203607550259f4c277e84f3b
Reviewed-on: https://chromium-review.googlesource.com/196529
Tested-by: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Gilad Arnold <garnold@chromium.org>
diff --git a/policy_manager/generic_variables_unittest.cc b/policy_manager/generic_variables_unittest.cc
index 490f949..8aad753 100644
--- a/policy_manager/generic_variables_unittest.cc
+++ b/policy_manager/generic_variables_unittest.cc
@@ -15,10 +15,7 @@
namespace chromeos_policy_manager {
-class PmCopyVariableTest : public ::testing::Test {
- protected:
- TimeDelta default_timeout_ = TimeDelta::FromSeconds(1);
-};
+class PmCopyVariableTest : public ::testing::Test {};
TEST_F(PmCopyVariableTest, SimpleTest) {
@@ -27,7 +24,8 @@
CopyVariable<int> var("var", kVariableModePoll, source);
// Generate and validate a copy.
- scoped_ptr<const int> copy_1(var.GetValue(default_timeout_, NULL));
+ scoped_ptr<const int> copy_1(var.GetValue(
+ PmTestUtils::DefaultTimeout(), NULL));
PMTEST_ASSERT_NOT_NULL(copy_1.get());
EXPECT_EQ(5, *copy_1);
@@ -38,9 +36,7 @@
EXPECT_EQ(5, *copy_1);
// Generate and validate a second copy.
- scoped_ptr<const int> copy_2(var.GetValue(default_timeout_, NULL));
- PMTEST_ASSERT_NOT_NULL(copy_2.get());
- EXPECT_EQ(42, *copy_2);
+ PmTestUtils::ExpectVariableHasValue(42, &var);
}
TEST_F(PmCopyVariableTest, SetFlagTest) {
@@ -50,13 +46,11 @@
CopyVariable<int> var("var", kVariableModePoll, source, &is_set);
// Flag marked unset, nothing should be returned.
- PMTEST_ASSERT_NULL(var.GetValue(default_timeout_, NULL));
+ PmTestUtils::ExpectVariableNotSet(&var);
// Flag marked set, we should be getting a value.
is_set = true;
- scoped_ptr<const int> copy(var.GetValue(default_timeout_, NULL));
- PMTEST_ASSERT_NOT_NULL(copy.get());
- EXPECT_EQ(5, *copy);
+ PmTestUtils::ExpectVariableHasValue(5, &var);
}
@@ -78,29 +72,22 @@
CopyVariable<CopyConstructorTestClass> var("var", kVariableModePoll, source);
scoped_ptr<const CopyConstructorTestClass> copy(
- var.GetValue(default_timeout_, NULL));
+ var.GetValue(PmTestUtils::DefaultTimeout(), NULL));
PMTEST_ASSERT_NOT_NULL(copy.get());
EXPECT_TRUE(copy->copied_);
}
-class PmConstCopyVariableTest : public ::testing::Test {
- protected:
- TimeDelta default_timeout_ = TimeDelta::FromSeconds(1);
-};
+class PmConstCopyVariableTest : public ::testing::Test {};
TEST_F(PmConstCopyVariableTest, SimpleTest) {
int source = 5;
ConstCopyVariable<int> var("var", source);
- scoped_ptr<const int> copy(var.GetValue(default_timeout_, NULL));
- PMTEST_ASSERT_NOT_NULL(copy.get());
- EXPECT_EQ(5, *copy);
+ PmTestUtils::ExpectVariableHasValue(5, &var);
// Ensure the value is cached.
source = 42;
- copy.reset(var.GetValue(default_timeout_, NULL));
- PMTEST_ASSERT_NOT_NULL(copy.get());
- EXPECT_EQ(5, *copy);
+ PmTestUtils::ExpectVariableHasValue(5, &var);
}
@@ -110,23 +97,18 @@
// No remaining event on the main loop.
EXPECT_EQ(0, RunGMainLoopMaxIterations(1));
}
-
- protected:
- TimeDelta default_timeout_ = TimeDelta::FromSeconds(1);
};
TEST_F(PmAsyncCopyVariableTest, ConstructorTest) {
AsyncCopyVariable<int> var("var");
- PMTEST_EXPECT_NULL(var.GetValue(default_timeout_, NULL));
+ PmTestUtils::ExpectVariableNotSet(&var);
EXPECT_EQ(kVariableModeAsync, var.GetMode());
}
TEST_F(PmAsyncCopyVariableTest, SetValueTest) {
AsyncCopyVariable<int> var("var");
var.SetValue(5);
- scoped_ptr<const int> copy(var.GetValue(default_timeout_, NULL));
- PMTEST_ASSERT_NOT_NULL(copy.get());
- EXPECT_EQ(5, *copy);
+ PmTestUtils::ExpectVariableHasValue(5, &var);
// Execute all the pending observers.
RunGMainLoopMaxIterations(100);
}
@@ -134,7 +116,7 @@
TEST_F(PmAsyncCopyVariableTest, UnsetValueTest) {
AsyncCopyVariable<int> var("var", 42);
var.UnsetValue();
- PMTEST_EXPECT_NULL(var.GetValue(default_timeout_, NULL));
+ PmTestUtils::ExpectVariableNotSet(&var);
// Execute all the pending observers.
RunGMainLoopMaxIterations(100);
}