| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 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 Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 16 |  | 
| Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_ | 
|  | 18 | #define UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_ | 
| Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 19 |  | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 20 | #include <memory> | 
| Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 21 | #include <string> | 
|  | 22 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 23 | #include "update_engine/update_manager/variable.h" | 
| Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 24 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 25 | namespace chromeos_update_manager { | 
| Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 26 |  | 
|  | 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 Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 29 | template <typename T> | 
| Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 30 | class FakeVariable : public Variable<T> { | 
|  | 31 | public: | 
| Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 32 | FakeVariable(const std::string& name, VariableMode mode) | 
| Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 33 | : Variable<T>(name, mode) {} | 
| Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 34 | FakeVariable(const std::string& name, base::TimeDelta poll_interval) | 
|  | 35 | : Variable<T>(name, poll_interval) {} | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 36 | ~FakeVariable() override {} | 
| Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 37 |  | 
|  | 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 Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 40 | // A value of null means that the GetValue() call will fail and return | 
|  | 41 | // null. | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 42 | void reset(const T* p_value) { ptr_.reset(p_value); } | 
| Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 43 |  | 
| Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 44 | // Make the NotifyValueChanged() public for FakeVariables. | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 45 | void NotifyValueChanged() { Variable<T>::NotifyValueChanged(); } | 
| Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 46 |  | 
| Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 47 | 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 Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 51 | // call to GetValue() without reset() will return null and set the error | 
| Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 52 | // message. | 
| Yunlian Jiang | 35866ed | 2015-01-29 13:09:20 -0800 | [diff] [blame] | 53 | const T* GetValue(base::TimeDelta /* timeout */, | 
|  | 54 | std::string* errmsg) override { | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 55 | if (ptr_ == nullptr && errmsg != nullptr) | 
| Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 56 | *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 Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 63 | std::unique_ptr<const T> ptr_; | 
| Alex Deymo | 231a851 | 2014-03-21 12:56:10 -0700 | [diff] [blame] | 64 |  | 
|  | 65 | DISALLOW_COPY_AND_ASSIGN(FakeVariable); | 
| Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 66 | }; | 
|  | 67 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 68 | }  // namespace chromeos_update_manager | 
| Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 69 |  | 
| Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 70 | #endif  // UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_ |