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/pmtest_utils.h b/policy_manager/pmtest_utils.h
index ade85d8..b41eda8 100644
--- a/policy_manager/pmtest_utils.h
+++ b/policy_manager/pmtest_utils.h
@@ -5,6 +5,11 @@
 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PMTEST_UTILS_H_
 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PMTEST_UTILS_H_
 
+#include <base/time/time.h>
+#include <gtest/gtest.h>
+
+#include "update_engine/policy_manager/variable.h"
+
 // Convenience macros for checking null-ness of pointers.
 //
 // Purportedly, gtest should support pointer comparison when the first argument
@@ -20,4 +25,38 @@
 #define PMTEST_EXPECT_NULL(p) EXPECT_EQ(NULL, p)
 #define PMTEST_EXPECT_NOT_NULL(p) EXPECT_NE(reinterpret_cast<void*>(NULL), p)
 
+
+namespace chromeos_policy_manager {
+
+// A help class with common functionality for use in Policy Manager testing.
+class PmTestUtils {
+ public:
+  // A default timeout to use when making various queries.
+  static const base::TimeDelta DefaultTimeout() {
+    return base::TimeDelta::FromSeconds(kDefaultTimeoutInSeconds);
+  }
+
+  // Calls GetValue on |variable| and expects its result to be |expected|.
+  template<typename T>
+  static void ExpectVariableHasValue(const T& expected, Variable<T>* variable) {
+    PMTEST_ASSERT_NOT_NULL(variable);
+    scoped_ptr<const T> value(variable->GetValue(DefaultTimeout(), nullptr));
+    PMTEST_ASSERT_NOT_NULL(value.get()) << "Variable: " << variable->GetName();
+    EXPECT_EQ(expected, *value) << "Variable: " << variable->GetName();
+  }
+
+  // Calls GetValue on |variable| and expects its result to be null.
+  template<typename T>
+  static void ExpectVariableNotSet(Variable<T>* variable) {
+    PMTEST_ASSERT_NOT_NULL(variable);
+    scoped_ptr<const T> value(variable->GetValue(DefaultTimeout(), nullptr));
+    PMTEST_EXPECT_NULL(value.get()) << "Variable: " << variable->GetName();
+  }
+
+ private:
+  static const unsigned kDefaultTimeoutInSeconds;
+};
+
+}  // namespace chromeos_policy_manager
+
 #endif  // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PMTEST_UTILS_H_