blob: bd8da8ca03e7b78cf24689e2330d5e6e020f0130 [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
Alex Deymo63784a52014-05-28 10:46:14 -07005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_UMTEST_UTILS_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_UMTEST_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 Deymo63784a52014-05-28 10:46:14 -070014#include "update_engine/update_manager/policy.h"
15#include "update_engine/update_manager/variable.h"
Gilad Arnold67ed78d2014-04-23 13:17:46 -070016
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().
Alex Deymo63784a52014-05-28 10:46:14 -070027#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)
Gilad Arnolda87340b2014-01-30 11:10:18 -080031
Gilad Arnold67ed78d2014-04-23 13:17:46 -070032
Alex Deymo63784a52014-05-28 10:46:14 -070033namespace chromeos_update_manager {
Gilad Arnold67ed78d2014-04-23 13:17:46 -070034
Alex Deymo63784a52014-05-28 10:46:14 -070035// A help class with common functionality for use in Update Manager testing.
36class UmTestUtils {
Gilad Arnold67ed78d2014-04-23 13:17:46 -070037 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) {
Alex Deymo63784a52014-05-28 10:46:14 -070046 UMTEST_ASSERT_NOT_NULL(variable);
Gilad Arnold67ed78d2014-04-23 13:17:46 -070047 scoped_ptr<const T> value(variable->GetValue(DefaultTimeout(), nullptr));
Alex Deymo63784a52014-05-28 10:46:14 -070048 UMTEST_ASSERT_NOT_NULL(value.get()) << "Variable: " << variable->GetName();
Gilad Arnold67ed78d2014-04-23 13:17:46 -070049 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) {
Alex Deymo63784a52014-05-28 10:46:14 -070055 UMTEST_ASSERT_NOT_NULL(variable);
Gilad Arnold67ed78d2014-04-23 13:17:46 -070056 scoped_ptr<const T> value(variable->GetValue(DefaultTimeout(), nullptr));
Alex Deymo63784a52014-05-28 10:46:14 -070057 UMTEST_EXPECT_NULL(value.get()) << "Variable: " << variable->GetName();
Gilad Arnold67ed78d2014-04-23 13:17:46 -070058 }
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
Alex Deymo63784a52014-05-28 10:46:14 -070068} // namespace chromeos_update_manager
Gilad Arnold67ed78d2014-04-23 13:17:46 -070069
Alex Deymo63784a52014-05-28 10:46:14 -070070#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_UMTEST_UTILS_H_