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 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_ |
Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 7 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 10 | #include <base/memory/scoped_ptr.h> |
| 11 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 12 | #include "update_engine/update_manager/variable.h" |
Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 13 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 14 | namespace chromeos_update_manager { |
Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 15 | |
| 16 | // A fake typed variable to use while testing policy implementations. The |
| 17 | // variable can be instructed to return any object of its type. |
| 18 | template<typename T> |
| 19 | class FakeVariable : public Variable<T> { |
| 20 | public: |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 21 | FakeVariable(const std::string& name, VariableMode mode) |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 22 | : Variable<T>(name, mode) {} |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 23 | FakeVariable(const std::string& name, base::TimeDelta poll_interval) |
| 24 | : Variable<T>(name, poll_interval) {} |
Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame] | 25 | virtual ~FakeVariable() {} |
| 26 | |
| 27 | // Sets the next value of this variable to the passed |p_value| pointer. Once |
| 28 | // returned by GetValue(), the pointer is released and has to be set again. |
| 29 | // A value of NULL means that the GetValue() call will fail and return NULL. |
| 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 |
| 43 | // call to GetValue() without reset() will return NULL and set the error |
| 44 | // message. |
| 45 | virtual const T* GetValue(base::TimeDelta /* timeout */, |
| 46 | std::string* errmsg) { |
| 47 | if (ptr_ == NULL && errmsg != NULL) |
| 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(). |
| 55 | scoped_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 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 62 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_ |