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 // 16 17 #ifndef UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_ 18 #define UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_ 19 20 #include <memory> 21 #include <string> 22 23 #include "update_engine/update_manager/variable.h" 24 25 namespace chromeos_update_manager { 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. 29 template<typename T> 30 class FakeVariable : public Variable<T> { 31 public: FakeVariable(const std::string & name,VariableMode mode)32 FakeVariable(const std::string& name, VariableMode mode) 33 : Variable<T>(name, mode) {} FakeVariable(const std::string & name,base::TimeDelta poll_interval)34 FakeVariable(const std::string& name, base::TimeDelta poll_interval) 35 : Variable<T>(name, poll_interval) {} ~FakeVariable()36 ~FakeVariable() override {} 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. 40 // A value of null means that the GetValue() call will fail and return 41 // null. reset(const T * p_value)42 void reset(const T* p_value) { 43 ptr_.reset(p_value); 44 } 45 46 // Make the NotifyValueChanged() public for FakeVariables. NotifyValueChanged()47 void NotifyValueChanged() { 48 Variable<T>::NotifyValueChanged(); 49 } 50 51 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 55 // call to GetValue() without reset() will return null and set the error 56 // message. GetValue(base::TimeDelta,std::string * errmsg)57 const T* GetValue(base::TimeDelta /* timeout */, 58 std::string* errmsg) override { 59 if (ptr_ == nullptr && errmsg != nullptr) 60 *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(). 67 std::unique_ptr<const T> ptr_; 68 69 DISALLOW_COPY_AND_ASSIGN(FakeVariable); 70 }; 71 72 } // namespace chromeos_update_manager 73 74 #endif // UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_ 75