fake_system_state.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // Copyright (C) 2012 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. #ifndef UPDATE_ENGINE_FAKE_SYSTEM_STATE_H_
  17. #define UPDATE_ENGINE_FAKE_SYSTEM_STATE_H_
  18. #include <base/logging.h>
  19. #include <gmock/gmock.h>
  20. #include <policy/mock_device_policy.h>
  21. #include "metrics/metrics_library_mock.h"
  22. #include "update_engine/common/fake_boot_control.h"
  23. #include "update_engine/common/fake_clock.h"
  24. #include "update_engine/common/fake_hardware.h"
  25. #include "update_engine/common/mock_prefs.h"
  26. #include "update_engine/mock_connection_manager.h"
  27. #include "update_engine/mock_metrics_reporter.h"
  28. #include "update_engine/mock_omaha_request_params.h"
  29. #include "update_engine/mock_p2p_manager.h"
  30. #include "update_engine/mock_payload_state.h"
  31. #include "update_engine/mock_power_manager.h"
  32. #include "update_engine/mock_update_attempter.h"
  33. #include "update_engine/system_state.h"
  34. #include "update_engine/update_manager/fake_update_manager.h"
  35. namespace chromeos_update_engine {
  36. // Mock the SystemStateInterface so that we could lie that
  37. // OOBE is completed even when there's no such marker file, etc.
  38. class FakeSystemState : public SystemState {
  39. public:
  40. FakeSystemState();
  41. // Base class overrides. All getters return the current implementation of
  42. // various members, either the default (fake/mock) or the one set to override
  43. // it by client code.
  44. BootControlInterface* boot_control() override { return boot_control_; }
  45. inline ClockInterface* clock() override { return clock_; }
  46. inline void set_device_policy(
  47. const policy::DevicePolicy* device_policy) override {
  48. device_policy_ = device_policy;
  49. }
  50. inline const policy::DevicePolicy* device_policy() override {
  51. return device_policy_;
  52. }
  53. inline ConnectionManagerInterface* connection_manager() override {
  54. return connection_manager_;
  55. }
  56. inline HardwareInterface* hardware() override { return hardware_; }
  57. inline MetricsReporterInterface* metrics_reporter() override {
  58. CHECK(metrics_reporter_ != nullptr);
  59. return metrics_reporter_;
  60. }
  61. inline PrefsInterface* prefs() override { return prefs_; }
  62. inline PrefsInterface* powerwash_safe_prefs() override {
  63. return powerwash_safe_prefs_;
  64. }
  65. inline PayloadStateInterface* payload_state() override {
  66. return payload_state_;
  67. }
  68. inline UpdateAttempter* update_attempter() override {
  69. return update_attempter_;
  70. }
  71. inline OmahaRequestParams* request_params() override {
  72. return request_params_;
  73. }
  74. inline P2PManager* p2p_manager() override { return p2p_manager_; }
  75. inline chromeos_update_manager::UpdateManager* update_manager() override {
  76. return update_manager_;
  77. }
  78. inline PowerManagerInterface* power_manager() override {
  79. return power_manager_;
  80. }
  81. inline DlcServiceInterface* dlcservice() override { return dlcservice_; }
  82. inline bool system_rebooted() override { return fake_system_rebooted_; }
  83. // Setters for the various members, can be used for overriding the default
  84. // implementations. For convenience, setting to a null pointer will restore
  85. // the default implementation.
  86. void set_boot_control(BootControlInterface* boot_control) {
  87. boot_control_ = boot_control ? boot_control : &fake_boot_control_;
  88. }
  89. inline void set_clock(ClockInterface* clock) {
  90. clock_ = clock ? clock : &fake_clock_;
  91. }
  92. inline void set_connection_manager(
  93. ConnectionManagerInterface* connection_manager) {
  94. connection_manager_ =
  95. (connection_manager ? connection_manager : &mock_connection_manager_);
  96. }
  97. inline void set_hardware(HardwareInterface* hardware) {
  98. hardware_ = hardware ? hardware : &fake_hardware_;
  99. }
  100. inline void set_metrics_reporter(MetricsReporterInterface* metrics_reporter) {
  101. metrics_reporter_ =
  102. metrics_reporter ? metrics_reporter : &mock_metrics_reporter_;
  103. }
  104. inline void set_prefs(PrefsInterface* prefs) {
  105. prefs_ = prefs ? prefs : &mock_prefs_;
  106. }
  107. inline void set_powerwash_safe_prefs(PrefsInterface* powerwash_safe_prefs) {
  108. powerwash_safe_prefs_ =
  109. (powerwash_safe_prefs ? powerwash_safe_prefs
  110. : &mock_powerwash_safe_prefs_);
  111. }
  112. inline void set_payload_state(PayloadStateInterface* payload_state) {
  113. payload_state_ = payload_state ? payload_state : &mock_payload_state_;
  114. }
  115. inline void set_update_attempter(UpdateAttempter* update_attempter) {
  116. update_attempter_ =
  117. (update_attempter ? update_attempter : &mock_update_attempter_);
  118. }
  119. inline void set_request_params(OmahaRequestParams* request_params) {
  120. request_params_ = (request_params ? request_params : &mock_request_params_);
  121. }
  122. inline void set_p2p_manager(P2PManager* p2p_manager) {
  123. p2p_manager_ = p2p_manager ? p2p_manager : &mock_p2p_manager_;
  124. }
  125. inline void set_update_manager(
  126. chromeos_update_manager::UpdateManager* update_manager) {
  127. update_manager_ = update_manager ? update_manager : &fake_update_manager_;
  128. }
  129. inline void set_system_rebooted(bool system_rebooted) {
  130. fake_system_rebooted_ = system_rebooted;
  131. }
  132. inline void set_dlcservice(DlcServiceInterface* dlcservice) {
  133. dlcservice_ = dlcservice;
  134. }
  135. // Getters for the built-in default implementations. These return the actual
  136. // concrete type of each implementation. For additional safety, they will fail
  137. // whenever the requested default was overridden by a different
  138. // implementation.
  139. inline FakeBootControl* fake_boot_control() {
  140. CHECK(boot_control_ == &fake_boot_control_);
  141. return &fake_boot_control_;
  142. }
  143. inline FakeClock* fake_clock() {
  144. CHECK(clock_ == &fake_clock_);
  145. return &fake_clock_;
  146. }
  147. inline testing::NiceMock<MockConnectionManager>* mock_connection_manager() {
  148. CHECK(connection_manager_ == &mock_connection_manager_);
  149. return &mock_connection_manager_;
  150. }
  151. inline FakeHardware* fake_hardware() {
  152. CHECK(hardware_ == &fake_hardware_);
  153. return &fake_hardware_;
  154. }
  155. inline testing::NiceMock<MockMetricsReporter>* mock_metrics_reporter() {
  156. CHECK(metrics_reporter_ == &mock_metrics_reporter_);
  157. return &mock_metrics_reporter_;
  158. }
  159. inline testing::NiceMock<MockPrefs>* mock_prefs() {
  160. CHECK(prefs_ == &mock_prefs_);
  161. return &mock_prefs_;
  162. }
  163. inline testing::NiceMock<MockPrefs>* mock_powerwash_safe_prefs() {
  164. CHECK(powerwash_safe_prefs_ == &mock_powerwash_safe_prefs_);
  165. return &mock_powerwash_safe_prefs_;
  166. }
  167. inline testing::NiceMock<MockPayloadState>* mock_payload_state() {
  168. CHECK(payload_state_ == &mock_payload_state_);
  169. return &mock_payload_state_;
  170. }
  171. inline testing::NiceMock<MockUpdateAttempter>* mock_update_attempter() {
  172. CHECK(update_attempter_ == &mock_update_attempter_);
  173. return &mock_update_attempter_;
  174. }
  175. inline testing::NiceMock<MockOmahaRequestParams>* mock_request_params() {
  176. CHECK(request_params_ == &mock_request_params_);
  177. return &mock_request_params_;
  178. }
  179. inline testing::NiceMock<MockP2PManager>* mock_p2p_manager() {
  180. CHECK(p2p_manager_ == &mock_p2p_manager_);
  181. return &mock_p2p_manager_;
  182. }
  183. inline chromeos_update_manager::FakeUpdateManager* fake_update_manager() {
  184. CHECK(update_manager_ == &fake_update_manager_);
  185. return &fake_update_manager_;
  186. }
  187. private:
  188. // Default mock/fake implementations (owned).
  189. FakeBootControl fake_boot_control_;
  190. FakeClock fake_clock_;
  191. testing::NiceMock<MockConnectionManager> mock_connection_manager_;
  192. FakeHardware fake_hardware_;
  193. testing::NiceMock<MockMetricsReporter> mock_metrics_reporter_;
  194. testing::NiceMock<MockPrefs> mock_prefs_;
  195. testing::NiceMock<MockPrefs> mock_powerwash_safe_prefs_;
  196. testing::NiceMock<MockPayloadState> mock_payload_state_;
  197. testing::NiceMock<MockUpdateAttempter> mock_update_attempter_;
  198. testing::NiceMock<MockOmahaRequestParams> mock_request_params_;
  199. testing::NiceMock<MockP2PManager> mock_p2p_manager_;
  200. chromeos_update_manager::FakeUpdateManager fake_update_manager_;
  201. testing::NiceMock<MockPowerManager> mock_power_manager_;
  202. // Pointers to objects that client code can override. They are initialized to
  203. // the default implementations above.
  204. BootControlInterface* boot_control_{&fake_boot_control_};
  205. ClockInterface* clock_;
  206. ConnectionManagerInterface* connection_manager_;
  207. HardwareInterface* hardware_;
  208. MetricsReporterInterface* metrics_reporter_;
  209. PrefsInterface* prefs_;
  210. PrefsInterface* powerwash_safe_prefs_;
  211. PayloadStateInterface* payload_state_;
  212. UpdateAttempter* update_attempter_;
  213. OmahaRequestParams* request_params_;
  214. P2PManager* p2p_manager_;
  215. chromeos_update_manager::UpdateManager* update_manager_;
  216. PowerManagerInterface* power_manager_{&mock_power_manager_};
  217. DlcServiceInterface* dlcservice_;
  218. // Other object pointers (not preinitialized).
  219. const policy::DevicePolicy* device_policy_;
  220. // Other data members.
  221. bool fake_system_rebooted_;
  222. };
  223. } // namespace chromeos_update_engine
  224. #endif // UPDATE_ENGINE_FAKE_SYSTEM_STATE_H_