update_engine: Mock out UpdateAttempter and OmahaRequestParams.
These classes are used by other classes so we need to have a way to
unit test those. This patch converts some public methods on these
classes to virtual methods so they can be mocked. It implements a
new MockOmahaRequestParams with all the public methods behaving like
the real object by default. This is now the default class used by the
FakeSystemState. Finally, the UpdateAttempterMock is renamed to
MockUpdateAttempter to be more consistent with other classes in
the project.
BUG=None
TEST=Unittest pass. Follow up CL using these classes also passes.
Change-Id: Iacb7e19d10c1526cea9659c27ab798cad126816f
Reviewed-on: https://chromium-review.googlesource.com/225855
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
diff --git a/fake_system_state.cc b/fake_system_state.cc
index 8c5ffc9..8dec9f5 100644
--- a/fake_system_state.cc
+++ b/fake_system_state.cc
@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "update_engine/fake_system_state.h"
+
#include "update_engine/update_manager/fake_state.h"
using chromeos_update_manager::FakeState;
@@ -14,7 +15,7 @@
FakeSystemState::FakeSystemState()
: mock_connection_manager_(this),
mock_update_attempter_(this, &dbus_),
- default_request_params_(this),
+ mock_request_params_(this),
fake_update_manager_(&fake_clock_),
clock_(&fake_clock_),
connection_manager_(&mock_connection_manager_),
@@ -24,7 +25,7 @@
powerwash_safe_prefs_(&mock_powerwash_safe_prefs_),
payload_state_(&mock_payload_state_),
update_attempter_(&mock_update_attempter_),
- request_params_(&default_request_params_),
+ request_params_(&mock_request_params_),
p2p_manager_(&mock_p2p_manager_),
update_manager_(&fake_update_manager_),
device_policy_(nullptr),
diff --git a/fake_system_state.h b/fake_system_state.h
index 3a4f6b7..520b1a0 100644
--- a/fake_system_state.h
+++ b/fake_system_state.h
@@ -14,11 +14,12 @@
#include "update_engine/fake_hardware.h"
#include "update_engine/mock_connection_manager.h"
#include "update_engine/mock_dbus_wrapper.h"
+#include "update_engine/mock_omaha_request_params.h"
#include "update_engine/mock_p2p_manager.h"
#include "update_engine/mock_payload_state.h"
+#include "update_engine/mock_update_attempter.h"
#include "update_engine/prefs_mock.h"
#include "update_engine/system_state.h"
-#include "update_engine/update_attempter_mock.h"
#include "update_engine/update_manager/fake_update_manager.h"
namespace chromeos_update_engine {
@@ -121,7 +122,7 @@
inline void set_request_params(OmahaRequestParams* request_params) {
request_params_ = (request_params ? request_params :
- &default_request_params_);
+ &mock_request_params_);
}
inline void set_p2p_manager(P2PManager *p2p_manager) {
@@ -177,14 +178,14 @@
return &mock_payload_state_;
}
- inline testing::NiceMock<UpdateAttempterMock>* mock_update_attempter() {
+ inline testing::NiceMock<MockUpdateAttempter>* mock_update_attempter() {
CHECK(update_attempter_ == &mock_update_attempter_);
return &mock_update_attempter_;
}
- inline OmahaRequestParams* default_request_params() {
- CHECK(request_params_ == &default_request_params_);
- return &default_request_params_;
+ inline testing::NiceMock<MockOmahaRequestParams>* mock_request_params() {
+ CHECK(request_params_ == &mock_request_params_);
+ return &mock_request_params_;
}
inline testing::NiceMock<MockP2PManager>* mock_p2p_manager() {
@@ -206,8 +207,8 @@
testing::NiceMock<PrefsMock> mock_prefs_;
testing::NiceMock<PrefsMock> mock_powerwash_safe_prefs_;
testing::NiceMock<MockPayloadState> mock_payload_state_;
- testing::NiceMock<UpdateAttempterMock> mock_update_attempter_;
- OmahaRequestParams default_request_params_;
+ testing::NiceMock<MockUpdateAttempter> mock_update_attempter_;
+ testing::NiceMock<MockOmahaRequestParams> mock_request_params_;
testing::NiceMock<MockP2PManager> mock_p2p_manager_;
chromeos_update_manager::FakeUpdateManager fake_update_manager_;
diff --git a/mock_omaha_request_params.h b/mock_omaha_request_params.h
new file mode 100644
index 0000000..3f1c608
--- /dev/null
+++ b/mock_omaha_request_params.h
@@ -0,0 +1,74 @@
+// Copyright 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef UPDATE_ENGINE_MOCK_OMAHA_REQUEST_PARAMS_H_
+#define UPDATE_ENGINE_MOCK_OMAHA_REQUEST_PARAMS_H_
+
+#include <string>
+
+#include <gmock/gmock.h>
+
+#include "update_engine/omaha_request_params.h"
+
+namespace chromeos_update_engine {
+
+class MockOmahaRequestParams : public OmahaRequestParams {
+ public:
+ explicit MockOmahaRequestParams(SystemState* system_state)
+ : OmahaRequestParams(system_state) {
+ // Delegate all calls to the parent instance by default. This helps the
+ // migration from tests using the real RequestParams when they should have
+ // use a fake or mock.
+ ON_CALL(*this, to_more_stable_channel())
+ .WillByDefault(testing::Invoke(
+ this, &MockOmahaRequestParams::fake_to_more_stable_channel));
+ ON_CALL(*this, GetAppId())
+ .WillByDefault(testing::Invoke(
+ this, &MockOmahaRequestParams::FakeGetAppId));
+ ON_CALL(*this, SetTargetChannel(testing::_, testing::_))
+ .WillByDefault(testing::Invoke(
+ this, &MockOmahaRequestParams::FakeSetTargetChannel));
+ ON_CALL(*this, UpdateDownloadChannel())
+ .WillByDefault(testing::Invoke(
+ this, &MockOmahaRequestParams::FakeUpdateDownloadChannel));
+ ON_CALL(*this, is_powerwash_allowed())
+ .WillByDefault(testing::Invoke(
+ this, &MockOmahaRequestParams::fake_is_powerwash_allowed));
+ }
+
+ MOCK_CONST_METHOD0(to_more_stable_channel, bool(void));
+ MOCK_CONST_METHOD0(GetAppId, std::string(void));
+ MOCK_METHOD2(SetTargetChannel, bool(const std::string& channel,
+ bool is_powerwash_allowed));
+ MOCK_METHOD0(UpdateDownloadChannel, void(void));
+ MOCK_CONST_METHOD0(is_powerwash_allowed, bool(void));
+
+ private:
+ // Wrappers to call the parent class and behave like the real object by
+ // default. See "Delegating Calls to a Parent Class" in gmock's documentation.
+ bool fake_to_more_stable_channel() const {
+ return OmahaRequestParams::to_more_stable_channel();
+ }
+
+ std::string FakeGetAppId() const {
+ return OmahaRequestParams::GetAppId();
+ }
+
+ bool FakeSetTargetChannel(const std::string& channel,
+ bool is_powerwash_allowed) {
+ return OmahaRequestParams::SetTargetChannel(channel, is_powerwash_allowed);
+ }
+
+ void FakeUpdateDownloadChannel() {
+ return OmahaRequestParams::UpdateDownloadChannel();
+ }
+
+ bool fake_is_powerwash_allowed() const {
+ return OmahaRequestParams::is_powerwash_allowed();
+ }
+};
+
+} // namespace chromeos_update_engine
+
+#endif // UPDATE_ENGINE_MOCK_OMAHA_REQUEST_PARAMS_H_
diff --git a/update_attempter_mock.h b/mock_update_attempter.h
similarity index 71%
rename from update_attempter_mock.h
rename to mock_update_attempter.h
index 38bf9a7..698962a 100644
--- a/update_attempter_mock.h
+++ b/mock_update_attempter.h
@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef UPDATE_ENGINE_UPDATE_ATTEMPTER_MOCK_H_
-#define UPDATE_ENGINE_UPDATE_ATTEMPTER_MOCK_H_
+#ifndef UPDATE_ENGINE_MOCK_UPDATE_ATTEMPTER_H_
+#define UPDATE_ENGINE_MOCK_UPDATE_ATTEMPTER_H_
#include <string>
@@ -13,7 +13,7 @@
namespace chromeos_update_engine {
-class UpdateAttempterMock : public UpdateAttempter {
+class MockUpdateAttempter : public UpdateAttempter {
public:
using UpdateAttempter::UpdateAttempter;
@@ -32,6 +32,14 @@
MOCK_METHOD1(GetBootTimeAtUpdate, bool(base::Time* out_boot_time));
+ MOCK_METHOD0(ResetStatus, bool(void));
+
+ MOCK_METHOD3(CheckForUpdate, void(const std::string& app_version,
+ const std::string& omaha_url,
+ bool is_interactive));
+
+ MOCK_METHOD0(RefreshDevicePolicy, void(void));
+
MOCK_CONST_METHOD0(consecutive_failed_update_checks, unsigned int(void));
MOCK_CONST_METHOD0(server_dictated_poll_interval, unsigned int(void));
@@ -39,4 +47,4 @@
} // namespace chromeos_update_engine
-#endif // UPDATE_ENGINE_UPDATE_ATTEMPTER_MOCK_H_
+#endif // UPDATE_ENGINE_MOCK_UPDATE_ATTEMPTER_H_
diff --git a/omaha_request_params.h b/omaha_request_params.h
index 7e2486a..6290322 100644
--- a/omaha_request_params.h
+++ b/omaha_request_params.h
@@ -194,11 +194,11 @@
// True if we're trying to update to a more stable channel.
// i.e. index(target_channel) > index(current_channel).
- bool to_more_stable_channel() const;
+ virtual bool to_more_stable_channel() const;
// Returns the app id corresponding to the current value of the
// download channel.
- std::string GetAppId() const;
+ virtual std::string GetAppId() const;
// Suggested defaults
static const char* const kAppId;
@@ -224,7 +224,8 @@
// channel changes done by the user in order to avoid having to solve
// numerous edge cases around ensuring the powerwash happens as intended in
// all such cases.
- bool SetTargetChannel(const std::string& channel, bool is_powerwash_allowed);
+ virtual bool SetTargetChannel(const std::string& channel,
+ bool is_powerwash_allowed);
// Updates the download channel for this particular attempt from the current
// value of target channel. This method takes a "snapshot" of the current
@@ -232,9 +233,9 @@
// this attempt (i.e. initial request as well as download progress/error
// event requests). The snapshot will be updated only when either this method
// or Init is called again.
- void UpdateDownloadChannel();
+ virtual void UpdateDownloadChannel();
- bool is_powerwash_allowed() const { return is_powerwash_allowed_; }
+ virtual bool is_powerwash_allowed() const { return is_powerwash_allowed_; }
// For unit-tests.
void set_root(const std::string& root);
diff --git a/update_attempter.h b/update_attempter.h
index 7732c08..6702f1b 100644
--- a/update_attempter.h
+++ b/update_attempter.h
@@ -102,7 +102,7 @@
// having to reboot once the previous update has reached
// UPDATE_STATUS_UPDATED_NEED_REBOOT state. This is used only
// for testing purposes.
- bool ResetStatus();
+ virtual bool ResetStatus();
// Returns the current status in the out params. Returns true on success.
virtual bool GetStatus(int64_t* last_checked_time,
@@ -136,9 +136,9 @@
// This is the internal entry point for going through an
// update. If the current status is idle invokes Update.
// This is called by the DBus implementation.
- void CheckForUpdate(const std::string& app_version,
- const std::string& omaha_url,
- bool is_interactive);
+ virtual void CheckForUpdate(const std::string& app_version,
+ const std::string& omaha_url,
+ bool is_interactive);
// This is the internal entry point for going through a rollback. This will
// attempt to run the postinstall on the non-active partition and set it as
@@ -186,7 +186,7 @@
// cause a real-time policy fetch from the policy server. It just reloads the
// latest value that libchromeos has cached. libchromeos fetches the policies
// from the server asynchronously at its own frequency.
- void RefreshDevicePolicy();
+ virtual void RefreshDevicePolicy();
// Returns the boottime (CLOCK_BOOTTIME) recorded at the last
// successful update. Returns false if the device has not updated.
diff --git a/update_manager/real_updater_provider_unittest.cc b/update_manager/real_updater_provider_unittest.cc
index ab0ee20..eeee245 100644
--- a/update_manager/real_updater_provider_unittest.cc
+++ b/update_manager/real_updater_provider_unittest.cc
@@ -13,18 +13,18 @@
#include "update_engine/fake_clock.h"
#include "update_engine/fake_system_state.h"
+#include "update_engine/mock_update_attempter.h"
#include "update_engine/omaha_request_params.h"
#include "update_engine/prefs_mock.h"
-#include "update_engine/update_attempter_mock.h"
#include "update_engine/update_manager/umtest_utils.h"
using base::Time;
using base::TimeDelta;
using chromeos_update_engine::FakeClock;
using chromeos_update_engine::FakeSystemState;
+using chromeos_update_engine::MockUpdateAttempter;
using chromeos_update_engine::OmahaRequestParams;
using chromeos_update_engine::PrefsMock;
-using chromeos_update_engine::UpdateAttempterMock;
using std::string;
using std::unique_ptr;
using testing::Return;