utils-fake.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include <gmock/gmock.h>
  17. #include <vintf/ObjectFactory.h>
  18. #include <vintf/PropertyFetcher.h>
  19. #include "utils.h"
  20. using ::testing::_;
  21. using ::testing::AtLeast;
  22. using ::testing::Invoke;
  23. using ::testing::Return;
  24. namespace android {
  25. namespace vintf {
  26. namespace details {
  27. class MockFileSystem : public FileSystem {
  28. public:
  29. MockFileSystem() {}
  30. MOCK_CONST_METHOD2(fetch, status_t(const std::string& path, std::string& fetched));
  31. MOCK_CONST_METHOD3(listFiles,
  32. status_t(const std::string&, std::vector<std::string>*, std::string*));
  33. status_t fetch(const std::string& path, std::string* fetched, std::string*) const override {
  34. // Call the mocked function
  35. return fetch(path, *fetched);
  36. }
  37. private:
  38. FileSystemImpl mImpl;
  39. };
  40. class MockRuntimeInfo : public RuntimeInfo {
  41. public:
  42. MockRuntimeInfo() {
  43. ON_CALL(*this, fetchAllInformation(_))
  44. .WillByDefault(Invoke(this, &MockRuntimeInfo::doFetch));
  45. }
  46. MOCK_METHOD1(fetchAllInformation, status_t(RuntimeInfo::FetchFlags));
  47. status_t doFetch(RuntimeInfo::FetchFlags flags);
  48. void failNextFetch() { failNextFetch_ = true; }
  49. private:
  50. bool failNextFetch_ = false;
  51. };
  52. class MockRuntimeInfoFactory : public ObjectFactory<RuntimeInfo> {
  53. public:
  54. MockRuntimeInfoFactory(const std::shared_ptr<MockRuntimeInfo>& info) { object_ = info; }
  55. std::shared_ptr<RuntimeInfo> make_shared() const override { return object_; }
  56. std::shared_ptr<MockRuntimeInfo> getInfo() const { return object_; }
  57. private:
  58. std::shared_ptr<MockRuntimeInfo> object_;
  59. };
  60. class MockPropertyFetcher : public PropertyFetcher {
  61. public:
  62. MockPropertyFetcher() = default;
  63. MOCK_CONST_METHOD2(getProperty, std::string(const std::string&, const std::string&));
  64. MOCK_CONST_METHOD2(getBoolProperty, bool(const std::string&, bool));
  65. MOCK_CONST_METHOD3(getUintProperty, uint64_t(const std::string&, uint64_t, uint64_t));
  66. };
  67. } // namespace details
  68. } // namespace vintf
  69. } // namespace android