blob: b41eda8ca06767ceabfdf00bc0059f756bdc0bd3 [file] [log] [blame]
Gilad Arnolda87340b2014-01-30 11:10:18 -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
Gilad Arnold2cbb3852014-03-07 12:40:50 -08005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PMTEST_UTILS_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PMTEST_UTILS_H_
Gilad Arnolda87340b2014-01-30 11:10:18 -08007
Gilad Arnold67ed78d2014-04-23 13:17:46 -07008#include <base/time/time.h>
9#include <gtest/gtest.h>
10
11#include "update_engine/policy_manager/variable.h"
12
Gilad Arnolda87340b2014-01-30 11:10:18 -080013// Convenience macros for checking null-ness of pointers.
14//
15// Purportedly, gtest should support pointer comparison when the first argument
16// is a pointer (e.g. NULL). It is therefore appropriate to use
17// {ASSERT,EXPECT}_{EQ,NE} for our purposes. However, gtest fails to realize
18// that NULL is a pointer when used with the _NE variants, unless we explicitly
19// cast it to a pointer type, and so we inject this casting.
20//
21// Note that checking nullness of the content of a scoped_ptr requires getting
22// the inner pointer value via get().
23#define PMTEST_ASSERT_NULL(p) ASSERT_EQ(NULL, p)
24#define PMTEST_ASSERT_NOT_NULL(p) ASSERT_NE(reinterpret_cast<void*>(NULL), p)
25#define PMTEST_EXPECT_NULL(p) EXPECT_EQ(NULL, p)
26#define PMTEST_EXPECT_NOT_NULL(p) EXPECT_NE(reinterpret_cast<void*>(NULL), p)
27
Gilad Arnold67ed78d2014-04-23 13:17:46 -070028
29namespace chromeos_policy_manager {
30
31// A help class with common functionality for use in Policy Manager testing.
32class PmTestUtils {
33 public:
34 // A default timeout to use when making various queries.
35 static const base::TimeDelta DefaultTimeout() {
36 return base::TimeDelta::FromSeconds(kDefaultTimeoutInSeconds);
37 }
38
39 // Calls GetValue on |variable| and expects its result to be |expected|.
40 template<typename T>
41 static void ExpectVariableHasValue(const T& expected, Variable<T>* variable) {
42 PMTEST_ASSERT_NOT_NULL(variable);
43 scoped_ptr<const T> value(variable->GetValue(DefaultTimeout(), nullptr));
44 PMTEST_ASSERT_NOT_NULL(value.get()) << "Variable: " << variable->GetName();
45 EXPECT_EQ(expected, *value) << "Variable: " << variable->GetName();
46 }
47
48 // Calls GetValue on |variable| and expects its result to be null.
49 template<typename T>
50 static void ExpectVariableNotSet(Variable<T>* variable) {
51 PMTEST_ASSERT_NOT_NULL(variable);
52 scoped_ptr<const T> value(variable->GetValue(DefaultTimeout(), nullptr));
53 PMTEST_EXPECT_NULL(value.get()) << "Variable: " << variable->GetName();
54 }
55
56 private:
57 static const unsigned kDefaultTimeoutInSeconds;
58};
59
60} // namespace chromeos_policy_manager
61
Gilad Arnold2cbb3852014-03-07 12:40:50 -080062#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PMTEST_UTILS_H_