blob: 22259332919e1c3eabc91068329bece425281560 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
Jay Srinivasan08fce042012-06-07 16:31:01 -070016
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_FAKE_SYSTEM_STATE_H_
18#define UPDATE_ENGINE_FAKE_SYSTEM_STATE_H_
Jay Srinivasan08fce042012-06-07 16:31:01 -070019
Gilad Arnold1f847232014-04-07 12:07:49 -070020#include <base/logging.h>
Jay Srinivasan08fce042012-06-07 16:31:01 -070021#include <gmock/gmock.h>
Jay Srinivasan43488792012-06-19 00:25:31 -070022#include <policy/mock_device_policy.h>
Jay Srinivasanf0572052012-10-23 18:12:56 -070023
Gilad Arnold1f847232014-04-07 12:07:49 -070024#include "metrics/metrics_library_mock.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/fake_boot_control.h"
26#include "update_engine/common/fake_clock.h"
27#include "update_engine/common/fake_hardware.h"
28#include "update_engine/common/mock_prefs.h"
David Zeuthen1cac5aa2014-04-02 10:18:00 -070029#include "update_engine/mock_connection_manager.h"
Alex Deymo560ae1d2014-10-28 02:17:54 -070030#include "update_engine/mock_omaha_request_params.h"
David Zeuthen526cb582013-08-06 12:26:18 -070031#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080032#include "update_engine/mock_payload_state.h"
Sen Jiangb8c6a8f2016-06-07 17:33:17 -070033#include "update_engine/mock_power_manager.h"
Alex Deymo560ae1d2014-10-28 02:17:54 -070034#include "update_engine/mock_update_attempter.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070035#include "update_engine/system_state.h"
Alex Deymo63784a52014-05-28 10:46:14 -070036#include "update_engine/update_manager/fake_update_manager.h"
Jay Srinivasan08fce042012-06-07 16:31:01 -070037
38namespace chromeos_update_engine {
39
40// Mock the SystemStateInterface so that we could lie that
41// OOBE is completed even when there's no such marker file, etc.
Gilad Arnold5bb4c902014-04-10 12:32:13 -070042class FakeSystemState : public SystemState {
Jay Srinivasan43488792012-06-19 00:25:31 -070043 public:
Gilad Arnold5bb4c902014-04-10 12:32:13 -070044 FakeSystemState();
Jay Srinivasan55f50c22013-01-10 19:24:35 -080045
Gilad Arnold1f847232014-04-07 12:07:49 -070046 // Base class overrides. All getters return the current implementation of
47 // various members, either the default (fake/mock) or the one set to override
48 // it by client code.
Jay Srinivasan43488792012-06-19 00:25:31 -070049
Alex Deymo763e7db2015-08-27 21:08:08 -070050 BootControlInterface* boot_control() override { return boot_control_; }
51
Alex Vakulenko157fe302014-08-11 15:59:58 -070052 inline ClockInterface* clock() override { return clock_; }
David Zeuthenf413fe52013-04-22 14:04:39 -070053
Alex Vakulenko157fe302014-08-11 15:59:58 -070054 inline void set_device_policy(
Gilad Arnold1f847232014-04-07 12:07:49 -070055 const policy::DevicePolicy* device_policy) override {
56 device_policy_ = device_policy;
57 }
58
Alex Vakulenko157fe302014-08-11 15:59:58 -070059 inline const policy::DevicePolicy* device_policy() override {
Gilad Arnold1f847232014-04-07 12:07:49 -070060 return device_policy_;
61 }
62
Alex Deymof6ee0162015-07-31 12:35:22 -070063 inline ConnectionManagerInterface* connection_manager() override {
Jay Srinivasan43488792012-06-19 00:25:31 -070064 return connection_manager_;
65 }
66
Alex Vakulenko157fe302014-08-11 15:59:58 -070067 inline HardwareInterface* hardware() override { return hardware_; }
Alex Deymo42432912013-07-12 20:21:15 -070068
Alex Vakulenko157fe302014-08-11 15:59:58 -070069 inline MetricsLibraryInterface* metrics_lib() override {
Gilad Arnold1f847232014-04-07 12:07:49 -070070 return metrics_lib_;
Jay Srinivasanf0572052012-10-23 18:12:56 -070071 }
72
Alex Vakulenko157fe302014-08-11 15:59:58 -070073 inline PrefsInterface* prefs() override { return prefs_; }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080074
Alex Vakulenko157fe302014-08-11 15:59:58 -070075 inline PrefsInterface* powerwash_safe_prefs() override {
Chris Sosaaa18e162013-06-20 13:20:30 -070076 return powerwash_safe_prefs_;
77 }
78
Alex Vakulenko157fe302014-08-11 15:59:58 -070079 inline PayloadStateInterface* payload_state() override {
David Zeuthen526cb582013-08-06 12:26:18 -070080 return payload_state_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080081 }
82
Alex Vakulenko157fe302014-08-11 15:59:58 -070083 inline UpdateAttempter* update_attempter() override {
Gilad Arnold1f847232014-04-07 12:07:49 -070084 return update_attempter_;
Gilad Arnold76b2b482014-04-01 13:32:43 -070085 }
Jay Srinivasan55f50c22013-01-10 19:24:35 -080086
Alex Vakulenko157fe302014-08-11 15:59:58 -070087 inline OmahaRequestParams* request_params() override {
Jay Srinivasanae4697c2013-03-18 17:08:08 -070088 return request_params_;
89 }
90
Alex Vakulenko157fe302014-08-11 15:59:58 -070091 inline P2PManager* p2p_manager() override { return p2p_manager_; }
David Zeuthen526cb582013-08-06 12:26:18 -070092
Alex Vakulenko157fe302014-08-11 15:59:58 -070093 inline chromeos_update_manager::UpdateManager* update_manager() override {
Alex Deymo63784a52014-05-28 10:46:14 -070094 return update_manager_;
Alex Deymo94c06162014-03-21 20:34:46 -070095 }
96
Sen Jiangb8c6a8f2016-06-07 17:33:17 -070097 inline PowerManagerInterface* power_manager() override {
98 return power_manager_;
Alex Deymo30534502015-07-20 15:06:33 -070099 }
100
Alex Vakulenko157fe302014-08-11 15:59:58 -0700101 inline bool system_rebooted() override { return fake_system_rebooted_; }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800102
Gilad Arnold1f847232014-04-07 12:07:49 -0700103 // Setters for the various members, can be used for overriding the default
104 // implementations. For convenience, setting to a null pointer will restore
105 // the default implementation.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800106
Alex Deymo763e7db2015-08-27 21:08:08 -0700107 void set_boot_control(BootControlInterface* boot_control) {
108 boot_control_ = boot_control ? boot_control : &fake_boot_control_;
109 }
110
David Zeuthenf413fe52013-04-22 14:04:39 -0700111 inline void set_clock(ClockInterface* clock) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700112 clock_ = clock ? clock : &fake_clock_;
113 }
114
Alex Deymof6ee0162015-07-31 12:35:22 -0700115 inline void set_connection_manager(
116 ConnectionManagerInterface* connection_manager) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700117 connection_manager_ = (connection_manager ? connection_manager :
118 &mock_connection_manager_);
David Zeuthenf413fe52013-04-22 14:04:39 -0700119 }
120
Alex Deymo42432912013-07-12 20:21:15 -0700121 inline void set_hardware(HardwareInterface* hardware) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700122 hardware_ = hardware ? hardware : &fake_hardware_;
Alex Deymo42432912013-07-12 20:21:15 -0700123 }
124
Gilad Arnold1f847232014-04-07 12:07:49 -0700125 inline void set_metrics_lib(MetricsLibraryInterface* metrics_lib) {
126 metrics_lib_ = metrics_lib ? metrics_lib : &mock_metrics_lib_;
J. Richard Barnette4da2cc12013-10-28 16:11:10 -0700127 }
128
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800129 inline void set_prefs(PrefsInterface* prefs) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700130 prefs_ = prefs ? prefs : &mock_prefs_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800131 }
132
Gilad Arnold1f847232014-04-07 12:07:49 -0700133 inline void set_powerwash_safe_prefs(PrefsInterface* powerwash_safe_prefs) {
134 powerwash_safe_prefs_ = (powerwash_safe_prefs ? powerwash_safe_prefs :
135 &mock_powerwash_safe_prefs_);
Chris Sosaaa18e162013-06-20 13:20:30 -0700136 }
137
Gilad Arnold1f847232014-04-07 12:07:49 -0700138 inline void set_payload_state(PayloadStateInterface *payload_state) {
139 payload_state_ = payload_state ? payload_state : &mock_payload_state_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800140 }
141
Gilad Arnold1f847232014-04-07 12:07:49 -0700142 inline void set_update_attempter(UpdateAttempter* update_attempter) {
143 update_attempter_ = (update_attempter ? update_attempter :
144 &mock_update_attempter_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800145 }
Jay Srinivasanf0572052012-10-23 18:12:56 -0700146
Gilad Arnold1f847232014-04-07 12:07:49 -0700147 inline void set_request_params(OmahaRequestParams* request_params) {
148 request_params_ = (request_params ? request_params :
Alex Deymo560ae1d2014-10-28 02:17:54 -0700149 &mock_request_params_);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700150 }
151
David Zeuthen526cb582013-08-06 12:26:18 -0700152 inline void set_p2p_manager(P2PManager *p2p_manager) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700153 p2p_manager_ = p2p_manager ? p2p_manager : &mock_p2p_manager_;
David Zeuthen526cb582013-08-06 12:26:18 -0700154 }
155
Alex Deymo63784a52014-05-28 10:46:14 -0700156 inline void set_update_manager(
157 chromeos_update_manager::UpdateManager *update_manager) {
158 update_manager_ = update_manager ? update_manager : &fake_update_manager_;
Alex Deymo94c06162014-03-21 20:34:46 -0700159 }
160
Gilad Arnold1f847232014-04-07 12:07:49 -0700161 inline void set_system_rebooted(bool system_rebooted) {
162 fake_system_rebooted_ = system_rebooted;
163 }
164
165 // Getters for the built-in default implementations. These return the actual
166 // concrete type of each implementation. For additional safety, they will fail
167 // whenever the requested default was overridden by a different
168 // implementation.
169
Alex Deymo763e7db2015-08-27 21:08:08 -0700170 inline FakeBootControl* fake_boot_control() {
171 CHECK(boot_control_ == &fake_boot_control_);
172 return &fake_boot_control_;
173 }
174
Gilad Arnold1f847232014-04-07 12:07:49 -0700175 inline FakeClock* fake_clock() {
176 CHECK(clock_ == &fake_clock_);
177 return &fake_clock_;
178 }
179
180 inline testing::NiceMock<MockConnectionManager>* mock_connection_manager() {
181 CHECK(connection_manager_ == &mock_connection_manager_);
182 return &mock_connection_manager_;
183 }
184
185 inline FakeHardware* fake_hardware() {
186 CHECK(hardware_ == &fake_hardware_);
187 return &fake_hardware_;
188 }
189
190 inline testing::NiceMock<MetricsLibraryMock>* mock_metrics_lib() {
191 CHECK(metrics_lib_ == &mock_metrics_lib_);
192 return &mock_metrics_lib_;
193 }
194
Alex Deymo8427b4a2014-11-05 14:00:32 -0800195 inline testing::NiceMock<MockPrefs> *mock_prefs() {
Gilad Arnold1f847232014-04-07 12:07:49 -0700196 CHECK(prefs_ == &mock_prefs_);
197 return &mock_prefs_;
198 }
199
Alex Deymo8427b4a2014-11-05 14:00:32 -0800200 inline testing::NiceMock<MockPrefs> *mock_powerwash_safe_prefs() {
Gilad Arnold1f847232014-04-07 12:07:49 -0700201 CHECK(powerwash_safe_prefs_ == &mock_powerwash_safe_prefs_);
202 return &mock_powerwash_safe_prefs_;
203 }
204
205 inline testing::NiceMock<MockPayloadState>* mock_payload_state() {
206 CHECK(payload_state_ == &mock_payload_state_);
207 return &mock_payload_state_;
208 }
209
Alex Deymo560ae1d2014-10-28 02:17:54 -0700210 inline testing::NiceMock<MockUpdateAttempter>* mock_update_attempter() {
Gilad Arnold1f847232014-04-07 12:07:49 -0700211 CHECK(update_attempter_ == &mock_update_attempter_);
212 return &mock_update_attempter_;
213 }
214
Alex Deymo560ae1d2014-10-28 02:17:54 -0700215 inline testing::NiceMock<MockOmahaRequestParams>* mock_request_params() {
216 CHECK(request_params_ == &mock_request_params_);
217 return &mock_request_params_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700218 }
219
220 inline testing::NiceMock<MockP2PManager>* mock_p2p_manager() {
221 CHECK(p2p_manager_ == &mock_p2p_manager_);
222 return &mock_p2p_manager_;
223 }
224
Alex Deymo63784a52014-05-28 10:46:14 -0700225 inline chromeos_update_manager::FakeUpdateManager* fake_update_manager() {
226 CHECK(update_manager_ == &fake_update_manager_);
227 return &fake_update_manager_;
David Zeuthen526cb582013-08-06 12:26:18 -0700228 }
229
Jay Srinivasan43488792012-06-19 00:25:31 -0700230 private:
Gilad Arnold1f847232014-04-07 12:07:49 -0700231 // Default mock/fake implementations (owned).
Alex Deymo763e7db2015-08-27 21:08:08 -0700232 FakeBootControl fake_boot_control_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700233 FakeClock fake_clock_;
234 testing::NiceMock<MockConnectionManager> mock_connection_manager_;
235 FakeHardware fake_hardware_;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800236 testing::NiceMock<MetricsLibraryMock> mock_metrics_lib_;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800237 testing::NiceMock<MockPrefs> mock_prefs_;
238 testing::NiceMock<MockPrefs> mock_powerwash_safe_prefs_;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800239 testing::NiceMock<MockPayloadState> mock_payload_state_;
Alex Deymo560ae1d2014-10-28 02:17:54 -0700240 testing::NiceMock<MockUpdateAttempter> mock_update_attempter_;
241 testing::NiceMock<MockOmahaRequestParams> mock_request_params_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700242 testing::NiceMock<MockP2PManager> mock_p2p_manager_;
Alex Deymo63784a52014-05-28 10:46:14 -0700243 chromeos_update_manager::FakeUpdateManager fake_update_manager_;
Sen Jiangb8c6a8f2016-06-07 17:33:17 -0700244 testing::NiceMock<MockPowerManager> mock_power_manager_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700245
Gilad Arnold1f847232014-04-07 12:07:49 -0700246 // Pointers to objects that client code can override. They are initialized to
247 // the default implementations above.
Alex Deymo763e7db2015-08-27 21:08:08 -0700248 BootControlInterface* boot_control_{&fake_boot_control_};
David Zeuthenf413fe52013-04-22 14:04:39 -0700249 ClockInterface* clock_;
Alex Deymof6ee0162015-07-31 12:35:22 -0700250 ConnectionManagerInterface* connection_manager_;
Alex Deymo42432912013-07-12 20:21:15 -0700251 HardwareInterface* hardware_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700252 MetricsLibraryInterface* metrics_lib_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800253 PrefsInterface* prefs_;
Chris Sosaaa18e162013-06-20 13:20:30 -0700254 PrefsInterface* powerwash_safe_prefs_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700255 PayloadStateInterface* payload_state_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700256 UpdateAttempter* update_attempter_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700257 OmahaRequestParams* request_params_;
Alex Deymo94c06162014-03-21 20:34:46 -0700258 P2PManager* p2p_manager_;
Alex Deymo63784a52014-05-28 10:46:14 -0700259 chromeos_update_manager::UpdateManager* update_manager_;
Sen Jiangb8c6a8f2016-06-07 17:33:17 -0700260 PowerManagerInterface* power_manager_{&mock_power_manager_};
Gilad Arnold1f847232014-04-07 12:07:49 -0700261
262 // Other object pointers (not preinitialized).
263 const policy::DevicePolicy* device_policy_;
264
265 // Other data members.
Gilad Arnold1f847232014-04-07 12:07:49 -0700266 bool fake_system_rebooted_;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700267};
268
Alex Deymo94c06162014-03-21 20:34:46 -0700269} // namespace chromeos_update_engine
Jay Srinivasan08fce042012-06-07 16:31:01 -0700270
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700271#endif // UPDATE_ENGINE_FAKE_SYSTEM_STATE_H_