blob: 9e51f7b109f4f157d099aaa2c49ba7cd4d616197 [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
Ben Chan02f7c1d2014-10-18 15:18:02 -07008#include <memory>
Alex Deymo53556ec2014-03-17 10:05:57 -07009#include <string>
10
Alex Deymo63784a52014-05-28 10:46:14 -070011#include "update_engine/update_manager/variable.h"
Alex Deymo272d9492014-02-03 20:28:40 -080012
Alex Deymo63784a52014-05-28 10:46:14 -070013namespace chromeos_update_manager {
Alex Deymo272d9492014-02-03 20:28:40 -080014
15// A fake typed variable to use while testing policy implementations. The
16// variable can be instructed to return any object of its type.
17template<typename T>
18class FakeVariable : public Variable<T> {
19 public:
Alex Deymo53556ec2014-03-17 10:05:57 -070020 FakeVariable(const std::string& name, VariableMode mode)
Alex Deymo0e433692014-02-20 07:23:03 -080021 : Variable<T>(name, mode) {}
Alex Deymo53556ec2014-03-17 10:05:57 -070022 FakeVariable(const std::string& name, base::TimeDelta poll_interval)
23 : Variable<T>(name, poll_interval) {}
Alex Deymo610277e2014-11-11 21:18:11 -080024 ~FakeVariable() override {}
Alex Deymo272d9492014-02-03 20:28:40 -080025
26 // Sets the next value of this variable to the passed |p_value| pointer. Once
27 // returned by GetValue(), the pointer is released and has to be set again.
Alex Vakulenko88b591f2014-08-28 16:48:57 -070028 // A value of null means that the GetValue() call will fail and return
29 // null.
Alex Deymo272d9492014-02-03 20:28:40 -080030 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
Alex Vakulenko88b591f2014-08-28 16:48:57 -070043 // call to GetValue() without reset() will return null and set the error
Alex Deymo272d9492014-02-03 20:28:40 -080044 // message.
Yunlian Jiang35866ed2015-01-29 13:09:20 -080045 const T* GetValue(base::TimeDelta /* timeout */,
46 std::string* errmsg) override {
Alex Vakulenko88b591f2014-08-28 16:48:57 -070047 if (ptr_ == nullptr && errmsg != nullptr)
Alex Deymo272d9492014-02-03 20:28:40 -080048 *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().
Ben Chan02f7c1d2014-10-18 15:18:02 -070055 std::unique_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
Gilad Arnold48415f12014-06-27 07:10:58 -070062#endif // UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_