umtest_utils.h revision 48415f1f6c6c356bfa9ac85b76d8ebcf053f7157
1// 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
5#ifndef UPDATE_ENGINE_UPDATE_MANAGER_UMTEST_UTILS_H_
6#define UPDATE_ENGINE_UPDATE_MANAGER_UMTEST_UTILS_H_
7
8#include <iostream>  // NOLINT(readability/streams)
9
10#include <base/memory/scoped_ptr.h>
11#include <base/time/time.h>
12#include <gtest/gtest.h>
13
14#include "update_engine/update_manager/policy.h"
15#include "update_engine/update_manager/variable.h"
16
17// 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 UMTEST_ASSERT_NULL(p) ASSERT_EQ(NULL, p)
28#define UMTEST_ASSERT_NOT_NULL(p) ASSERT_NE(reinterpret_cast<void*>(NULL), p)
29#define UMTEST_EXPECT_NULL(p) EXPECT_EQ(NULL, p)
30#define UMTEST_EXPECT_NOT_NULL(p) EXPECT_NE(reinterpret_cast<void*>(NULL), p)
31
32
33namespace chromeos_update_manager {
34
35// A help class with common functionality for use in Update Manager testing.
36class UmTestUtils {
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    UMTEST_ASSERT_NOT_NULL(variable);
47    scoped_ptr<const T> value(variable->GetValue(DefaultTimeout(), nullptr));
48    UMTEST_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    UMTEST_ASSERT_NOT_NULL(variable);
56    scoped_ptr<const T> value(variable->GetValue(DefaultTimeout(), nullptr));
57    UMTEST_EXPECT_NULL(value.get()) << "Variable: " << variable->GetName();
58  }
59
60 private:
61  static const unsigned kDefaultTimeoutInSeconds;
62};
63
64// 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
68}  // namespace chromeos_update_manager
69
70#endif  // UPDATE_ENGINE_UPDATE_MANAGER_UMTEST_UTILS_H_
71