blob: 0f976e3bb31e4ba3762de009fce8edcf36af5118 [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
Alex Deymo63784a52014-05-28 10:46:14 -07005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_
Alex Deymo272d9492014-02-03 20:28:40 -08007
Alex Deymo53556ec2014-03-17 10:05:57 -07008#include <string>
9
Alex Deymo272d9492014-02-03 20:28:40 -080010#include <base/memory/scoped_ptr.h>
11
Alex Deymo63784a52014-05-28 10:46:14 -070012#include "update_engine/update_manager/variable.h"
Alex Deymo272d9492014-02-03 20:28:40 -080013
Alex Deymo63784a52014-05-28 10:46:14 -070014namespace chromeos_update_manager {
Alex Deymo272d9492014-02-03 20:28:40 -080015
16// A fake typed variable to use while testing policy implementations. The
17// variable can be instructed to return any object of its type.
18template<typename T>
19class FakeVariable : public Variable<T> {
20 public:
Alex Deymo53556ec2014-03-17 10:05:57 -070021 FakeVariable(const std::string& name, VariableMode mode)
Alex Deymo0e433692014-02-20 07:23:03 -080022 : Variable<T>(name, mode) {}
Alex Deymo53556ec2014-03-17 10:05:57 -070023 FakeVariable(const std::string& name, base::TimeDelta poll_interval)
24 : Variable<T>(name, poll_interval) {}
Alex Deymo272d9492014-02-03 20:28:40 -080025 virtual ~FakeVariable() {}
26
27 // Sets the next value of this variable to the passed |p_value| pointer. Once
28 // returned by GetValue(), the pointer is released and has to be set again.
29 // A value of NULL means that the GetValue() call will fail and return NULL.
30 void reset(const T* p_value) {
31 ptr_.reset(p_value);
32 }
33
Alex Deymo53556ec2014-03-17 10:05:57 -070034 // Make the NotifyValueChanged() public for FakeVariables.
35 void NotifyValueChanged() {
36 Variable<T>::NotifyValueChanged();
37 }
38
Alex Deymo272d9492014-02-03 20:28:40 -080039 protected:
40 // Variable<T> overrides.
41 // Returns the pointer set with reset(). The ownership of the object is passed
42 // to the caller and the pointer is release from the FakeVariable. A second
43 // call to GetValue() without reset() will return NULL and set the error
44 // message.
45 virtual const T* GetValue(base::TimeDelta /* timeout */,
46 std::string* errmsg) {
47 if (ptr_ == NULL && errmsg != NULL)
48 *errmsg = this->GetName() + " is an empty FakeVariable";
49 // Passes the pointer ownership to the caller.
50 return ptr_.release();
51 }
52
53 private:
54 // The pointer returned by GetValue().
55 scoped_ptr<const T> ptr_;
Alex Deymo231a8512014-03-21 12:56:10 -070056
57 DISALLOW_COPY_AND_ASSIGN(FakeVariable);
Alex Deymo272d9492014-02-03 20:28:40 -080058};
59
Alex Deymo63784a52014-05-28 10:46:14 -070060} // namespace chromeos_update_manager
Alex Deymo272d9492014-02-03 20:28:40 -080061
Alex Deymo63784a52014-05-28 10:46:14 -070062#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_