Commit 826f8e2c authored by Guillaume Jenkins's avatar Guillaume Jenkins Committed by Commit Bot

[iOS CBCM] Add unit tests for ProfileReportGeneratorIOS

Adds simple unit test cases for the ProfileReportGeneratorIOS class. The
tests generate a report that includes some policies tied to the main
profile (browser state), and check that the values in the proto make
sense.

Bug: 1114853
Change-Id: I4e56e79754c2e30e6e02080fc58b43ae63b8ae2e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2495342
Commit-Queue: Guillaume Jenkins <gujen@google.com>
Reviewed-by: default avatarOwen Min <zmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#820854}
parent d8bb00b7
......@@ -136,6 +136,7 @@ source_set("unit_tests") {
"browser_dm_token_storage_ios_unittest.mm",
"policy_unittest.mm",
"reporting/browser_report_generator_ios_unittest.mm",
"reporting/profile_report_generator_ios_unittest.mm",
]
deps = [
":policy",
......@@ -149,7 +150,11 @@ source_set("unit_tests") {
"//ios/chrome/browser:utils",
"//ios/chrome/browser/browser_state:test_support",
"//ios/chrome/browser/prefs",
"//ios/chrome/browser/signin:signin",
"//ios/chrome/browser/signin:test_support",
"//ios/chrome/test:test_support",
"//ios/public/provider/chrome/browser/signin:test_support",
"//ios/web/public/test:test",
"//testing/gtest",
]
}
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/chrome/browser/policy/reporting/profile_report_generator_ios.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace em = enterprise_management;
#import <Foundation/Foundation.h>
#include "base/run_loop.h"
#include "base/test/bind_test_util.h"
#include "components/policy/core/common/mock_policy_service.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/schema_registry.h"
#include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
#include "ios/chrome/browser/browser_state/test_chrome_browser_state_manager.h"
#import "ios/chrome/browser/policy/browser_state_policy_connector_mock.h"
#include "ios/chrome/browser/policy/reporting/reporting_delegate_factory_ios.h"
#include "ios/chrome/browser/signin/authentication_service_factory.h"
#import "ios/chrome/browser/signin/authentication_service_fake.h"
#include "ios/chrome/test/ios_chrome_scoped_testing_chrome_browser_state_manager.h"
#include "ios/chrome/test/testing_application_context.h"
#include "ios/public/provider/chrome/browser/signin/fake_chrome_identity_service.h"
#include "ios/web/public/test/web_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
namespace enterprise_reporting {
namespace {
const base::FilePath kProfilePath = base::FilePath("/fake/profile/default");
} // namespace
class ProfileReportGeneratorIOSTest : public PlatformTest {
public:
ProfileReportGeneratorIOSTest() : generator_(&delegate_factory_) {
TestChromeBrowserState::Builder builder;
builder.SetPath(kProfilePath);
builder.AddTestingFactory(
AuthenticationServiceFactory::GetInstance(),
base::BindRepeating(
&AuthenticationServiceFake::CreateAuthenticationService));
InitMockPolicyService();
builder.SetPolicyConnector(
std::make_unique<BrowserStatePolicyConnectorMock>(
std::move(policy_service_), &schema_registry_));
std::unique_ptr<TestChromeBrowserState> browser_state = builder.Build();
InitPolicyMap();
scoped_browser_state_manager_ =
std::make_unique<IOSChromeScopedTestingChromeBrowserStateManager>(
std::make_unique<TestChromeBrowserStateManager>(
std::move(browser_state)));
}
ProfileReportGeneratorIOSTest(const ProfileReportGeneratorIOSTest&) = delete;
ProfileReportGeneratorIOSTest& operator=(
const ProfileReportGeneratorIOSTest&) = delete;
~ProfileReportGeneratorIOSTest() override = default;
void InitMockPolicyService() {
policy_service_ = std::make_unique<policy::MockPolicyService>();
ON_CALL(*policy_service_.get(),
GetPolicies(::testing::Eq(policy::PolicyNamespace(
policy::POLICY_DOMAIN_CHROME, std::string()))))
.WillByDefault(::testing::ReturnRef(policy_map_));
}
void InitPolicyMap() {
policy_map_.Set("kPolicyName1", policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(std::vector<base::Value>()), nullptr);
policy_map_.Set("kPolicyName2", policy::POLICY_LEVEL_RECOMMENDED,
policy::POLICY_SCOPE_MACHINE, policy::POLICY_SOURCE_MERGED,
base::Value(true), nullptr);
}
std::unique_ptr<em::ChromeUserProfileInfo> GenerateReport() {
std::unique_ptr<em::ChromeUserProfileInfo> report =
generator_.MaybeGenerate(kProfilePath,
kProfilePath.BaseName().AsUTF8Unsafe(),
ReportType::kFull);
if (!report)
return nullptr;
EXPECT_EQ(kProfilePath.BaseName().AsUTF8Unsafe(), report->name());
EXPECT_EQ(kProfilePath.AsUTF8Unsafe(), report->id());
EXPECT_TRUE(report->is_full_report());
return report;
}
ReportingDelegateFactoryIOS delegate_factory_;
ProfileReportGenerator generator_;
private:
web::WebTaskEnvironment task_environment_;
std::unique_ptr<policy::MockPolicyService> policy_service_;
policy::SchemaRegistry schema_registry_;
policy::PolicyMap policy_map_;
std::unique_ptr<IOSChromeScopedTestingChromeBrowserStateManager>
scoped_browser_state_manager_;
};
TEST_F(ProfileReportGeneratorIOSTest, UnsignedInProfile) {
auto report = GenerateReport();
ASSERT_TRUE(report);
EXPECT_FALSE(report->has_chrome_signed_in_user());
}
TEST_F(ProfileReportGeneratorIOSTest, PoliciesReportedOnlyWhenEnabled) {
// Policies are reported by default.
std::unique_ptr<em::ChromeUserProfileInfo> report = GenerateReport();
ASSERT_TRUE(report);
EXPECT_EQ(2, report->chrome_policies_size());
// Make sure policies are no longer reported when |set_policies_enabled| is
// set to false.
generator_.set_policies_enabled(false);
report = GenerateReport();
ASSERT_TRUE(report);
EXPECT_EQ(0, report->chrome_policies_size());
// Make sure policies are once again being reported after setting
// |set_policies_enabled| back to true.
generator_.set_policies_enabled(true);
report = GenerateReport();
ASSERT_TRUE(report);
EXPECT_EQ(2, report->chrome_policies_size());
}
} // namespace enterprise_reporting
\ No newline at end of file
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