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 "utils.h" 20 21 using ::testing::_; 22 using ::testing::AtLeast; 23 using ::testing::Invoke; 24 using ::testing::Return; 25 26 namespace android { 27 namespace vintf { 28 namespace details { 29 30 class MockFileFetcher : public FileFetcher { 31 public: 32 MockFileFetcher() { 33 // By default call through to the original. 34 ON_CALL(*this, fetch(_, _)).WillByDefault(Invoke([this](const auto& path, auto& fetched) { 35 return real_.fetchInternal(path, fetched, nullptr); 36 })); 37 ON_CALL(*this, listFiles(_, _, _)) 38 .WillByDefault( 39 Invoke([this](const std::string& path, std::vector<std::string>* out, 40 std::string* error) { return real_.listFiles(path, out, error); })); 41 } 42 43 MOCK_METHOD2(fetch, status_t(const std::string& path, std::string& fetched)); 44 MOCK_METHOD3(listFiles, status_t(const std::string&, std::vector<std::string>*, std::string*)); 45 46 status_t fetch(const std::string& path, std::string& fetched, std::string*) override final { 47 return fetch(path, fetched); 48 } 49 50 private: 51 FileFetcher real_; 52 }; 53 54 class MockPartitionMounter : public PartitionMounter { 55 public: 56 MockPartitionMounter() { 57 ON_CALL(*this, mountSystem()).WillByDefault(Invoke([&] { 58 systemMounted_ = true; 59 return OK; 60 })); 61 ON_CALL(*this, umountSystem()).WillByDefault(Invoke([&] { 62 systemMounted_ = false; 63 return OK; 64 })); 65 ON_CALL(*this, mountVendor()).WillByDefault(Invoke([&] { 66 vendorMounted_ = true; 67 return OK; 68 })); 69 ON_CALL(*this, umountVendor()).WillByDefault(Invoke([&] { 70 vendorMounted_ = false; 71 return OK; 72 })); 73 } 74 MOCK_CONST_METHOD0(mountSystem, status_t()); 75 MOCK_CONST_METHOD0(umountSystem, status_t()); 76 MOCK_CONST_METHOD0(mountVendor, status_t()); 77 MOCK_CONST_METHOD0(umountVendor, status_t()); 78 79 bool systemMounted() const { return systemMounted_; } 80 bool vendorMounted() const { return vendorMounted_; } 81 82 void reset() { 83 systemMounted_ = false; 84 vendorMounted_ = false; 85 } 86 87 private: 88 bool systemMounted_; 89 bool vendorMounted_; 90 }; 91 92 class MockRuntimeInfo : public RuntimeInfo { 93 public: 94 MockRuntimeInfo() { 95 ON_CALL(*this, fetchAllInformation(_)) 96 .WillByDefault(Invoke(this, &MockRuntimeInfo::doFetch)); 97 } 98 MOCK_METHOD1(fetchAllInformation, status_t(RuntimeInfo::FetchFlags)); 99 status_t doFetch(RuntimeInfo::FetchFlags flags); 100 void failNextFetch() { failNextFetch_ = true; } 101 102 private: 103 bool failNextFetch_ = false; 104 }; 105 class MockRuntimeInfoFactory : public ObjectFactory<RuntimeInfo> { 106 public: 107 MockRuntimeInfoFactory(const std::shared_ptr<MockRuntimeInfo>& info) { object_ = info; } 108 std::shared_ptr<RuntimeInfo> make_shared() const override { return object_; } 109 std::shared_ptr<MockRuntimeInfo> getInfo() const { return object_; } 110 111 private: 112 std::shared_ptr<MockRuntimeInfo> object_; 113 }; 114 115 class MockPropertyFetcher : public PropertyFetcher { 116 public: 117 MockPropertyFetcher(); 118 MOCK_CONST_METHOD2(getProperty, std::string(const std::string&, const std::string&)); 119 120 private: 121 PropertyFetcher real_; 122 }; 123 extern MockPropertyFetcher* gPropertyFetcher; 124 125 } // namespace details 126 } // namespace vintf 127 } // namespace android 128