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