blob: 2dae3db2ef3cf6d34bca7ad21a6c17b4fbde3314 [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
Gilad Arnold48415f12014-06-27 07:10:58 -07005#ifndef UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_
6#define 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.
Alex Vakulenko88b591f2014-08-28 16:48:57 -070029 // A value of null means that the GetValue() call will fail and return
30 // null.
Alex Deymo272d9492014-02-03 20:28:40 -080031 void reset(const T* p_value) {
32 ptr_.reset(p_value);
33 }
34
Alex Deymo53556ec2014-03-17 10:05:57 -070035 // Make the NotifyValueChanged() public for FakeVariables.
36 void NotifyValueChanged() {
37 Variable<T>::NotifyValueChanged();
38 }
39
Alex Deymo272d9492014-02-03 20:28:40 -080040 protected:
41 // Variable<T> overrides.
42 // Returns the pointer set with reset(). The ownership of the object is passed
43 // to the caller and the pointer is release from the FakeVariable. A second
Alex Vakulenko88b591f2014-08-28 16:48:57 -070044 // call to GetValue() without reset() will return null and set the error
Alex Deymo272d9492014-02-03 20:28:40 -080045 // message.
46 virtual const T* GetValue(base::TimeDelta /* timeout */,
47 std::string* errmsg) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -070048 if (ptr_ == nullptr && errmsg != nullptr)
Alex Deymo272d9492014-02-03 20:28:40 -080049 *errmsg = this->GetName() + " is an empty FakeVariable";
50 // Passes the pointer ownership to the caller.
51 return ptr_.release();
52 }
53
54 private:
55 // The pointer returned by GetValue().
56 scoped_ptr<const T> ptr_;
Alex Deymo231a8512014-03-21 12:56:10 -070057
58 DISALLOW_COPY_AND_ASSIGN(FakeVariable);
Alex Deymo272d9492014-02-03 20:28:40 -080059};
60
Alex Deymo63784a52014-05-28 10:46:14 -070061} // namespace chromeos_update_manager
Alex Deymo272d9492014-02-03 20:28:40 -080062
Gilad Arnold48415f12014-06-27 07:10:58 -070063#endif // UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_