blob: 79a773385ad8c53b184ff1dc04f13520b93ee5c6 [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
Alex Deymo0d11c602014-04-23 20:12:20 -07008#include <iostream>
9
Alex Deymo1f012912014-04-24 19:08:04 -070010#include <base/memory/scoped_ptr.h>
Gilad Arnold67ed78d2014-04-23 13:17:46 -070011#include <base/time/time.h>
12#include <gtest/gtest.h>
13
Alex Deymo0d11c602014-04-23 20:12:20 -070014#include "update_engine/policy_manager/policy.h"
Gilad Arnold67ed78d2014-04-23 13:17:46 -070015#include "update_engine/policy_manager/variable.h"
16
Gilad Arnolda87340b2014-01-30 11:10:18 -080017// Convenience macros for checking null-ness of pointers.
18//
19// Purportedly, gtest should support pointer comparison when the first argument
20// is a pointer (e.g. NULL). It is therefore appropriate to use
21// {ASSERT,EXPECT}_{EQ,NE} for our purposes. However, gtest fails to realize
22// that NULL is a pointer when used with the _NE variants, unless we explicitly
23// cast it to a pointer type, and so we inject this casting.
24//
25// Note that checking nullness of the content of a scoped_ptr requires getting
26// the inner pointer value via get().
27#define PMTEST_ASSERT_NULL(p) ASSERT_EQ(NULL, p)
28#define PMTEST_ASSERT_NOT_NULL(p) ASSERT_NE(reinterpret_cast<void*>(NULL), p)
29#define PMTEST_EXPECT_NULL(p) EXPECT_EQ(NULL, p)
30#define PMTEST_EXPECT_NOT_NULL(p) EXPECT_NE(reinterpret_cast<void*>(NULL), p)
31
Gilad Arnold67ed78d2014-04-23 13:17:46 -070032
33namespace chromeos_policy_manager {
34
35// A help class with common functionality for use in Policy Manager testing.
36class PmTestUtils {
37 public:
38 // A default timeout to use when making various queries.
39 static const base::TimeDelta DefaultTimeout() {
40 return base::TimeDelta::FromSeconds(kDefaultTimeoutInSeconds);
41 }
42
43 // Calls GetValue on |variable| and expects its result to be |expected|.
44 template<typename T>
45 static void ExpectVariableHasValue(const T& expected, Variable<T>* variable) {
46 PMTEST_ASSERT_NOT_NULL(variable);
47 scoped_ptr<const T> value(variable->GetValue(DefaultTimeout(), nullptr));
48 PMTEST_ASSERT_NOT_NULL(value.get()) << "Variable: " << variable->GetName();
49 EXPECT_EQ(expected, *value) << "Variable: " << variable->GetName();
50 }
51
52 // Calls GetValue on |variable| and expects its result to be null.
53 template<typename T>
54 static void ExpectVariableNotSet(Variable<T>* variable) {
55 PMTEST_ASSERT_NOT_NULL(variable);
56 scoped_ptr<const T> value(variable->GetValue(DefaultTimeout(), nullptr));
57 PMTEST_EXPECT_NULL(value.get()) << "Variable: " << variable->GetName();
58 }
59
60 private:
61 static const unsigned kDefaultTimeoutInSeconds;
62};
63
Alex Deymo0d11c602014-04-23 20:12:20 -070064// PrintTo() functions are used by gtest to print these values. They need to be
65// defined on the same namespace where the type was defined.
66void PrintTo(const EvalStatus& status, ::std::ostream* os);
67
Gilad Arnold67ed78d2014-04-23 13:17:46 -070068} // namespace chromeos_policy_manager
69
Gilad Arnold2cbb3852014-03-07 12:40:50 -080070#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PMTEST_UTILS_H_