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:
32   FakeVariable(const std::string& name, VariableMode mode)
33       : Variable<T>(name, mode) {}
34   FakeVariable(const std::string& name, base::TimeDelta poll_interval)
35       : Variable<T>(name, poll_interval) {}
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.
42   void reset(const T* p_value) { ptr_.reset(p_value); }
43 
44   // Make the NotifyValueChanged() public for FakeVariables.
45   void NotifyValueChanged() { Variable<T>::NotifyValueChanged(); }
46 
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
51   // call to GetValue() without reset() will return null and set the error
52   // message.
53   const T* GetValue(base::TimeDelta /* timeout */,
54                     std::string* errmsg) override {
55     if (ptr_ == nullptr && errmsg != nullptr)
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().
63   std::unique_ptr<const T> ptr_;
64 
65   DISALLOW_COPY_AND_ASSIGN(FakeVariable);
66 };
67 
68 }  // namespace chromeos_update_manager
69 
70 #endif  // UPDATE_ENGINE_UPDATE_MANAGER_FAKE_VARIABLE_H_
71