blob: ef5b4f32106b27eb0808958c59357c9cb325fe3a [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.
Amin Hassani4b717432019-01-14 16:24:20 -080029template <typename T>
Alex Deymo272d9492014-02-03 20:28:40 -080030class 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.
Amin Hassani4b717432019-01-14 16:24:20 -080042 void reset(const T* p_value) { ptr_.reset(p_value); }
Alex Deymo272d9492014-02-03 20:28:40 -080043
Alex Deymo53556ec2014-03-17 10:05:57 -070044 // Make the NotifyValueChanged() public for FakeVariables.
Amin Hassani4b717432019-01-14 16:24:20 -080045 void NotifyValueChanged() { Variable<T>::NotifyValueChanged(); }
Alex Deymo53556ec2014-03-17 10:05:57 -070046
Alex Deymo272d9492014-02-03 20:28:40 -080047 protected:
48 // Variable<T> overrides.
49 // Returns the pointer set with reset(). The ownership of the object is passed
50 // to the caller and the pointer is release from the FakeVariable. A second
Alex Vakulenko88b591f2014-08-28 16:48:57 -070051 // call to GetValue() without reset() will return null and set the error
Alex Deymo272d9492014-02-03 20:28:40 -080052 // message.
Yunlian Jiang35866ed02015-01-29 13:09:20 -080053 const T* GetValue(base::TimeDelta /* timeout */,
54 std::string* errmsg) override {
Alex Vakulenko88b591f2014-08-28 16:48:57 -070055 if (ptr_ == nullptr && errmsg != nullptr)
Alex Deymo272d9492014-02-03 20:28:40 -080056 *errmsg = this->GetName() + " is an empty FakeVariable";
57 // Passes the pointer ownership to the caller.
58 return ptr_.release();
59 }
60
61 private:
62 // The pointer returned by GetValue().
Ben Chan02f7c1d2014-10-18 15:18:02 -070063 std::unique_ptr<const T> ptr_;
Alex Deymo231a8512014-03-21 12:56:10 -070064
65 DISALLOW_COPY_AND_ASSIGN(FakeVariable);
Alex Deymo272d9492014-02-03 20:28:40 -080066};
67
Alex Deymo63784a52014-05-28 10:46:14 -070068} // namespace chromeos_update_manager
Alex Deymo272d9492014-02-03 20:28:40 -080069
Gilad Arnold48415f12014-06-27 07:10:58 -070070#endif // UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_