blob: 2f8e079f16d184c1a0fdba4e00a380a1ea906b2c [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2014 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Alex Deymo272d9492014-02-03 20:28:40 -080016
Gilad Arnold48415f12014-06-27 07:10:58 -070017#ifndef UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_
18#define UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_
Alex Deymo272d9492014-02-03 20:28:40 -080019
Ben Chan02f7c1d2014-10-18 15:18:02 -070020#include <memory>
Alex Deymo53556ec2014-03-17 10:05:57 -070021#include <string>
22
Alex Deymo63784a52014-05-28 10:46:14 -070023#include "update_engine/update_manager/variable.h"
Alex Deymo272d9492014-02-03 20:28:40 -080024
Alex Deymo63784a52014-05-28 10:46:14 -070025namespace chromeos_update_manager {
Alex Deymo272d9492014-02-03 20:28:40 -080026
27// A fake typed variable to use while testing policy implementations. The
28// variable can be instructed to return any object of its type.
29template<typename T>
30class FakeVariable : public Variable<T> {
31 public:
Alex Deymo53556ec2014-03-17 10:05:57 -070032 FakeVariable(const std::string& name, VariableMode mode)
Alex Deymo0e433692014-02-20 07:23:03 -080033 : Variable<T>(name, mode) {}
Alex Deymo53556ec2014-03-17 10:05:57 -070034 FakeVariable(const std::string& name, base::TimeDelta poll_interval)
35 : Variable<T>(name, poll_interval) {}
Alex Deymo610277e2014-11-11 21:18:11 -080036 ~FakeVariable() override {}
Alex Deymo272d9492014-02-03 20:28:40 -080037
38 // Sets the next value of this variable to the passed |p_value| pointer. Once
39 // returned by GetValue(), the pointer is released and has to be set again.
Alex Vakulenko88b591f2014-08-28 16:48:57 -070040 // A value of null means that the GetValue() call will fail and return
41 // null.
Alex Deymo272d9492014-02-03 20:28:40 -080042 void reset(const T* p_value) {
43 ptr_.reset(p_value);
44 }
45
Alex Deymo53556ec2014-03-17 10:05:57 -070046 // Make the NotifyValueChanged() public for FakeVariables.
47 void NotifyValueChanged() {
48 Variable<T>::NotifyValueChanged();
49 }
50
Alex Deymo272d9492014-02-03 20:28:40 -080051 protected:
52 // Variable<T> overrides.
53 // Returns the pointer set with reset(). The ownership of the object is passed
54 // to the caller and the pointer is release from the FakeVariable. A second
Alex Vakulenko88b591f2014-08-28 16:48:57 -070055 // call to GetValue() without reset() will return null and set the error
Alex Deymo272d9492014-02-03 20:28:40 -080056 // message.
Yunlian Jiang35866ed2015-01-29 13:09:20 -080057 const T* GetValue(base::TimeDelta /* timeout */,
58 std::string* errmsg) override {
Alex Vakulenko88b591f2014-08-28 16:48:57 -070059 if (ptr_ == nullptr && errmsg != nullptr)
Alex Deymo272d9492014-02-03 20:28:40 -080060 *errmsg = this->GetName() + " is an empty FakeVariable";
61 // Passes the pointer ownership to the caller.
62 return ptr_.release();
63 }
64
65 private:
66 // The pointer returned by GetValue().
Ben Chan02f7c1d2014-10-18 15:18:02 -070067 std::unique_ptr<const T> ptr_;
Alex Deymo231a8512014-03-21 12:56:10 -070068
69 DISALLOW_COPY_AND_ASSIGN(FakeVariable);
Alex Deymo272d9492014-02-03 20:28:40 -080070};
71
Alex Deymo63784a52014-05-28 10:46:14 -070072} // namespace chromeos_update_manager
Alex Deymo272d9492014-02-03 20:28:40 -080073
Gilad Arnold48415f12014-06-27 07:10:58 -070074#endif // UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_