blob: 541d1ec61431f2c84f0a5b1a835b15077f89a951 [file] [log] [blame]
Alex Deymo272d9492014-02-03 20:28:40 -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
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_FAKE_VARIABLE_H
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_FAKE_VARIABLE_H
7
8#include <base/memory/scoped_ptr.h>
9
Alex Deymo6e97bb22014-02-05 16:46:16 -080010#include "update_engine/policy_manager/variable.h"
Alex Deymo272d9492014-02-03 20:28:40 -080011
12namespace chromeos_policy_manager {
13
14// A fake typed variable to use while testing policy implementations. The
15// variable can be instructed to return any object of its type.
16template<typename T>
17class FakeVariable : public Variable<T> {
18 public:
Alex Deymo0e433692014-02-20 07:23:03 -080019 explicit FakeVariable(const std::string& name, VariableMode mode)
20 : Variable<T>(name, mode) {}
Alex Deymo272d9492014-02-03 20:28:40 -080021 virtual ~FakeVariable() {}
22
23 // Sets the next value of this variable to the passed |p_value| pointer. Once
24 // returned by GetValue(), the pointer is released and has to be set again.
25 // A value of NULL means that the GetValue() call will fail and return NULL.
26 void reset(const T* p_value) {
27 ptr_.reset(p_value);
28 }
29
30 protected:
31 // Variable<T> overrides.
32 // Returns the pointer set with reset(). The ownership of the object is passed
33 // to the caller and the pointer is release from the FakeVariable. A second
34 // call to GetValue() without reset() will return NULL and set the error
35 // message.
36 virtual const T* GetValue(base::TimeDelta /* timeout */,
37 std::string* errmsg) {
38 if (ptr_ == NULL && errmsg != NULL)
39 *errmsg = this->GetName() + " is an empty FakeVariable";
40 // Passes the pointer ownership to the caller.
41 return ptr_.release();
42 }
43
44 private:
45 // The pointer returned by GetValue().
46 scoped_ptr<const T> ptr_;
47};
48
49} // namespace chromeos_policy_manager
50
51#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_FAKE_VARIABLE_H