Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 1 | // 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 Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_ |
| 6 | #define UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_ |
Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 7 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 8 | #include <memory> |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 11 | #include "update_engine/update_manager/variable.h" |
Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 12 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 13 | namespace chromeos_update_manager { |
Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 14 | |
| 15 | // A fake typed variable to use while testing policy implementations. The |
| 16 | // variable can be instructed to return any object of its type. |
| 17 | template<typename T> |
| 18 | class FakeVariable : public Variable<T> { |
| 19 | public: |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 20 | FakeVariable(const std::string& name, VariableMode mode) |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 21 | : Variable<T>(name, mode) {} |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 22 | FakeVariable(const std::string& name, base::TimeDelta poll_interval) |
| 23 | : Variable<T>(name, poll_interval) {} |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 24 | ~FakeVariable() override {} |
Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 25 | |
| 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 Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 28 | // A value of null means that the GetValue() call will fail and return |
| 29 | // null. |
Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 30 | void reset(const T* p_value) { |
| 31 | ptr_.reset(p_value); |
| 32 | } |
| 33 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 34 | // Make the NotifyValueChanged() public for FakeVariables. |
| 35 | void NotifyValueChanged() { |
| 36 | Variable<T>::NotifyValueChanged(); |
| 37 | } |
| 38 | |
Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 39 | 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 Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 43 | // call to GetValue() without reset() will return null and set the error |
Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 44 | // message. |
Yunlian Jiang | 35866ed | 2015-01-29 13:09:20 -0800 | [diff] [blame^] | 45 | const T* GetValue(base::TimeDelta /* timeout */, |
| 46 | std::string* errmsg) override { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 47 | if (ptr_ == nullptr && errmsg != nullptr) |
Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 48 | *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 Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 55 | std::unique_ptr<const T> ptr_; |
Alex Deymo | 231a851 | 2014-03-21 12:56:10 -0700 | [diff] [blame] | 56 | |
| 57 | DISALLOW_COPY_AND_ASSIGN(FakeVariable); |
Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 58 | }; |
| 59 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 60 | } // namespace chromeos_update_manager |
Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 61 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 62 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_ |