Commit 48561d50 authored by Owen Min's avatar Owen Min Committed by Commit Bot

Refactor cloud policy client mock class

Move UploadChromeDesktopReport mock function to
policy::MockCloudPolicyClient so that it can be re-used.

Bug: 956237
Change-Id: I58eaae0def68766e3ba4ae1acb254034af5688e1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1606587Reviewed-by: default avatarJulian Pastarmov <pastarmovj@chromium.org>
Commit-Queue: Owen Min <zmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#659049}
parent 6b2582da
...@@ -26,36 +26,6 @@ const char kFakeDMToken[] = "fake-dm-token"; ...@@ -26,36 +26,6 @@ const char kFakeDMToken[] = "fake-dm-token";
const char kFakeClientId[] = "fake-client-id"; const char kFakeClientId[] = "fake-client-id";
const char kFakeMachineNameReport[] = "{\"computername\":\"name\"}"; const char kFakeMachineNameReport[] = "{\"computername\":\"name\"}";
class MockCloudPolicyClient : public policy::MockCloudPolicyClient {
public:
explicit MockCloudPolicyClient(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: policy::MockCloudPolicyClient(std::move(url_loader_factory)) {}
void UploadChromeDesktopReport(
std::unique_ptr<enterprise_management::ChromeDesktopReportRequest>
request,
const StatusCallback& callback) override {
UploadChromeDesktopReportProxy(request.get(), callback);
}
MOCK_METHOD2(UploadChromeDesktopReportProxy,
void(enterprise_management::ChromeDesktopReportRequest*,
const StatusCallback&));
void OnReportUploadedFailed(const StatusCallback& callback) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(callback, false));
}
void OnReportUploadedSucceeded(const StatusCallback& callback) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(callback, true));
}
private:
DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyClient);
};
class FakeBrowserDMTokenStorage : public policy::BrowserDMTokenStorage { class FakeBrowserDMTokenStorage : public policy::BrowserDMTokenStorage {
public: public:
FakeBrowserDMTokenStorage() = default; FakeBrowserDMTokenStorage() = default;
...@@ -89,7 +59,7 @@ class EnterpriseReportingPrivateUploadChromeDesktopReportTest ...@@ -89,7 +59,7 @@ class EnterpriseReportingPrivateUploadChromeDesktopReportTest
EnterpriseReportingPrivateUploadChromeDesktopReportFunction* function = EnterpriseReportingPrivateUploadChromeDesktopReportFunction* function =
EnterpriseReportingPrivateUploadChromeDesktopReportFunction:: EnterpriseReportingPrivateUploadChromeDesktopReportFunction::
CreateForTesting(test_url_loader_factory_.GetSafeWeakWrapper()); CreateForTesting(test_url_loader_factory_.GetSafeWeakWrapper());
auto client = std::make_unique<MockCloudPolicyClient>( auto client = std::make_unique<policy::MockCloudPolicyClient>(
test_url_loader_factory_.GetSafeWeakWrapper()); test_url_loader_factory_.GetSafeWeakWrapper());
client_ = client.get(); client_ = client.get();
function->SetCloudPolicyClientForTesting(std::move(client)); function->SetCloudPolicyClientForTesting(std::move(client));
...@@ -109,7 +79,7 @@ class EnterpriseReportingPrivateUploadChromeDesktopReportTest ...@@ -109,7 +79,7 @@ class EnterpriseReportingPrivateUploadChromeDesktopReportTest
"{\"chromeUserProfileReport\":[{\"chromeSignInUser\":\"Name\"}]}}]"); "{\"chromeUserProfileReport\":[{\"chromeSignInUser\":\"Name\"}]}}]");
} }
MockCloudPolicyClient* client_; policy::MockCloudPolicyClient* client_;
private: private:
network::TestURLLoaderFactory test_url_loader_factory_; network::TestURLLoaderFactory test_url_loader_factory_;
...@@ -140,8 +110,7 @@ TEST_F(EnterpriseReportingPrivateUploadChromeDesktopReportTest, UploadFailed) { ...@@ -140,8 +110,7 @@ TEST_F(EnterpriseReportingPrivateUploadChromeDesktopReportTest, UploadFailed) {
EXPECT_CALL(*client_, SetupRegistration(kFakeDMToken, kFakeClientId, _)) EXPECT_CALL(*client_, SetupRegistration(kFakeDMToken, kFakeClientId, _))
.Times(1); .Times(1);
EXPECT_CALL(*client_, UploadChromeDesktopReportProxy(_, _)) EXPECT_CALL(*client_, UploadChromeDesktopReportProxy(_, _))
.WillOnce(WithArgs<1>( .WillOnce(WithArgs<1>(policy::ScheduleStatusCallback(false)));
Invoke(client_, &MockCloudPolicyClient::OnReportUploadedFailed)));
ASSERT_EQ(enterprise_reporting::kUploadFailed, ASSERT_EQ(enterprise_reporting::kUploadFailed,
RunFunctionAndReturnError(function, RunFunctionAndReturnError(function,
GenerateArgs(kFakeMachineNameReport))); GenerateArgs(kFakeMachineNameReport)));
...@@ -155,8 +124,7 @@ TEST_F(EnterpriseReportingPrivateUploadChromeDesktopReportTest, ...@@ -155,8 +124,7 @@ TEST_F(EnterpriseReportingPrivateUploadChromeDesktopReportTest,
EXPECT_CALL(*client_, SetupRegistration(kFakeDMToken, kFakeClientId, _)) EXPECT_CALL(*client_, SetupRegistration(kFakeDMToken, kFakeClientId, _))
.Times(1); .Times(1);
EXPECT_CALL(*client_, UploadChromeDesktopReportProxy(_, _)) EXPECT_CALL(*client_, UploadChromeDesktopReportProxy(_, _))
.WillOnce(WithArgs<1>( .WillOnce(WithArgs<1>(policy::ScheduleStatusCallback(true)));
Invoke(client_, &MockCloudPolicyClient::OnReportUploadedSucceeded)));
ASSERT_EQ(nullptr, RunFunctionAndReturnValue( ASSERT_EQ(nullptr, RunFunctionAndReturnValue(
function, GenerateArgs(kFakeMachineNameReport))); function, GenerateArgs(kFakeMachineNameReport)));
::testing::Mock::VerifyAndClearExpectations(client_); ::testing::Mock::VerifyAndClearExpectations(client_);
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <string> #include <string>
#include "base/macros.h" #include "base/macros.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/policy/core/common/cloud/cloud_policy_client.h" #include "components/policy/core/common/cloud/cloud_policy_client.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
...@@ -19,6 +20,11 @@ class SharedURLLoaderFactory; ...@@ -19,6 +20,11 @@ class SharedURLLoaderFactory;
namespace policy { namespace policy {
ACTION_P(ScheduleStatusCallback, status) {
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
base::BindOnce(arg0, status));
}
class MockCloudPolicyClient : public CloudPolicyClient { class MockCloudPolicyClient : public CloudPolicyClient {
public: public:
MockCloudPolicyClient(); MockCloudPolicyClient();
...@@ -64,6 +70,17 @@ class MockCloudPolicyClient : public CloudPolicyClient { ...@@ -64,6 +70,17 @@ class MockCloudPolicyClient : public CloudPolicyClient {
const std::string&, const std::string&,
const std::string&)); const std::string&));
void UploadChromeDesktopReport(
std::unique_ptr<enterprise_management::ChromeDesktopReportRequest>
request,
const StatusCallback& callback) override {
UploadChromeDesktopReportProxy(request.get(), callback);
}
// Use Proxy function because unique_ptr can't be used in mock function.
MOCK_METHOD2(UploadChromeDesktopReportProxy,
void(enterprise_management::ChromeDesktopReportRequest*,
const StatusCallback&));
// Sets the DMToken. // Sets the DMToken.
void SetDMToken(const std::string& token); void SetDMToken(const std::string& token);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment