1 /*
2  * Copyright (C) 2017 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 #include <gmock/gmock.h>
18 
19 #include <vintf/ObjectFactory.h>
20 #include <vintf/PropertyFetcher.h>
21 #include "utils.h"
22 
23 using ::testing::_;
24 using ::testing::AtLeast;
25 using ::testing::Invoke;
26 using ::testing::Return;
27 
28 namespace android {
29 namespace vintf {
30 namespace details {
31 
32 class MockFileSystem : public FileSystem {
33    public:
34     MockFileSystem() {}
35 
36     MOCK_CONST_METHOD2(fetch, status_t(const std::string& path, std::string& fetched));
37     MOCK_CONST_METHOD3(listFiles,
38                        status_t(const std::string&, std::vector<std::string>*, std::string*));
39 
40     status_t fetch(const std::string& path, std::string* fetched, std::string*) const override {
41         // Call the mocked function
42         return fetch(path, *fetched);
43     }
44    private:
45     FileSystemImpl mImpl;
46 };
47 
48 class MockRuntimeInfo : public RuntimeInfo {
49    public:
50     MockRuntimeInfo();
51     MOCK_METHOD1(fetchAllInformation, status_t(RuntimeInfo::FetchFlags));
52     status_t doFetch(RuntimeInfo::FetchFlags flags);
53     void failNextFetch() { failNextFetch_ = true; }
54     void setNextFetchKernelInfo(KernelVersion&& v, std::map<std::string, std::string>&& configs);
55     void setNextFetchKernelInfo(const KernelVersion& v,
56                                 const std::map<std::string, std::string>& configs);
57 
58    private:
59     bool failNextFetch_ = false;
60     // KernelInfo returned in next fetch.
61     KernelInfo kernel_info_;
62 };
63 class MockRuntimeInfoFactory : public ObjectFactory<RuntimeInfo> {
64    public:
65     MockRuntimeInfoFactory(const std::shared_ptr<MockRuntimeInfo>& info) { object_ = info; }
66     std::shared_ptr<RuntimeInfo> make_shared() const override { return object_; }
67     std::shared_ptr<MockRuntimeInfo> getInfo() const { return object_; }
68 
69    private:
70     std::shared_ptr<MockRuntimeInfo> object_;
71 };
72 
73 class MockPropertyFetcher : public PropertyFetcher {
74    public:
75     MockPropertyFetcher() = default;
76     MOCK_CONST_METHOD2(getProperty, std::string(const std::string&, const std::string&));
77     MOCK_CONST_METHOD2(getBoolProperty, bool(const std::string&, bool));
78     MOCK_CONST_METHOD3(getUintProperty, uint64_t(const std::string&, uint64_t, uint64_t));
79 };
80 
81 }  // namespace details
82 }  // namespace vintf
83 }  // namespace android
84