Commit b446d0d8 authored by Guillaume Jenkins's avatar Guillaume Jenkins Committed by Commit Bot

[iOS Policy] Improve policy mocking infra for unit tests

In preparation for CBCM reporting unit tests, this extends the mock
infrastructure for policy objects that will be involved. In particular:

- A new mock BrowserStatePolicyConnector was added
- The TestChromeBrowserState builder now supports building the browser
  state with a mock BrowserStatePolicyConnector
- The TestChromeBrowserStateManager can return a TestChromeBrowserState
  by path
- The TestingApplicationContext now creates a real
  BrowserPolicyConnector if its accessor is called

These are all needed for the ProfileReportGeneratorIOS unit tests, which
will come in a separate CL.

Bug: 1055318
Change-Id: I3428e56d695c014d63245be6493d527787343cce
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2495265Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Guillaume Jenkins <gujen@google.com>
Cr-Commit-Position: refs/heads/master@{#820786}
parent 5110c3d3
......@@ -202,6 +202,7 @@ source_set("test_support") {
"//ios/chrome/browser/bookmarks",
"//ios/chrome/browser/history",
"//ios/chrome/browser/net",
"//ios/chrome/browser/policy:policy",
"//ios/chrome/browser/prefs",
"//ios/chrome/browser/prefs:browser_prefs",
"//ios/chrome/browser/sync/glue",
......
......@@ -16,6 +16,7 @@
#include "components/keyed_service/ios/refcounted_browser_state_keyed_service_factory.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/net/net_types.h"
#include "ios/chrome/browser/policy/browser_state_policy_connector.h"
namespace sync_preferences {
class PrefServiceSyncable;
......@@ -106,6 +107,9 @@ class TestChromeBrowserState : public ChromeBrowserState {
void SetPrefService(
std::unique_ptr<sync_preferences::PrefServiceSyncable> prefs);
void SetPolicyConnector(
std::unique_ptr<BrowserStatePolicyConnector> policy_connector);
// Creates the TestChromeBrowserState using previously-set settings.
std::unique_ptr<TestChromeBrowserState> Build();
......@@ -117,6 +121,8 @@ class TestChromeBrowserState : public ChromeBrowserState {
base::FilePath state_path_;
std::unique_ptr<sync_preferences::PrefServiceSyncable> pref_service_;
std::unique_ptr<BrowserStatePolicyConnector> policy_connector_;
TestingFactories testing_factories_;
RefcountedTestingFactories refcounted_testing_factories_;
......@@ -129,7 +135,8 @@ class TestChromeBrowserState : public ChromeBrowserState {
const base::FilePath& path,
std::unique_ptr<sync_preferences::PrefServiceSyncable> prefs,
TestingFactories testing_factories,
RefcountedTestingFactories refcounted_testing_factories);
RefcountedTestingFactories refcounted_testing_factories,
std::unique_ptr<BrowserStatePolicyConnector> policy_connector);
private:
friend class Builder;
......@@ -151,6 +158,8 @@ class TestChromeBrowserState : public ChromeBrowserState {
std::unique_ptr<sync_preferences::PrefServiceSyncable> prefs_;
sync_preferences::TestingPrefServiceSyncable* testing_prefs_;
std::unique_ptr<BrowserStatePolicyConnector> policy_connector_;
// The incognito ChromeBrowserState instance that is associated with this
// non-incognito ChromeBrowserState instance.
std::unique_ptr<TestChromeBrowserState> otr_browser_state_;
......
......@@ -105,12 +105,14 @@ TestChromeBrowserState::TestChromeBrowserState(
const base::FilePath& path,
std::unique_ptr<sync_preferences::PrefServiceSyncable> prefs,
TestingFactories testing_factories,
RefcountedTestingFactories refcounted_testing_factories)
RefcountedTestingFactories refcounted_testing_factories,
std::unique_ptr<BrowserStatePolicyConnector> policy_connector)
: ChromeBrowserState(base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskShutdownBehavior::BLOCK_SHUTDOWN})),
state_path_(path),
prefs_(std::move(prefs)),
testing_prefs_(nullptr),
policy_connector_(std::move(policy_connector)),
otr_browser_state_(nullptr),
original_browser_state_(nullptr) {
for (const auto& pair : testing_factories) {
......@@ -236,9 +238,7 @@ PrefProxyConfigTracker* TestChromeBrowserState::GetProxyConfigTracker() {
}
BrowserStatePolicyConnector* TestChromeBrowserState::GetPolicyConnector() {
// TODO(crbug.com/1055318): Determine what level of support is needed for
// unittesting and return a mock or fake here.
return nullptr;
return policy_connector_.get();
}
PrefService* TestChromeBrowserState::GetPrefs() {
......@@ -354,11 +354,17 @@ void TestChromeBrowserState::Builder::SetPrefService(
pref_service_ = std::move(prefs);
}
void TestChromeBrowserState::Builder::SetPolicyConnector(
std::unique_ptr<BrowserStatePolicyConnector> policy_connector) {
DCHECK(!build_called_);
policy_connector_ = std::move(policy_connector);
}
std::unique_ptr<TestChromeBrowserState>
TestChromeBrowserState::Builder::Build() {
DCHECK(!build_called_);
build_called_ = true;
return base::WrapUnique(new TestChromeBrowserState(
state_path_, std::move(pref_service_), std::move(testing_factories_),
std::move(refcounted_testing_factories_)));
std::move(refcounted_testing_factories_), std::move(policy_connector_)));
}
......@@ -30,6 +30,8 @@ ChromeBrowserState* TestChromeBrowserStateManager::GetLastUsedBrowserState() {
ChromeBrowserState* TestChromeBrowserStateManager::GetBrowserState(
const base::FilePath& path) {
if (browser_state_ && browser_state_->GetStatePath() == path)
return browser_state_.get();
return nullptr;
}
......
......@@ -87,6 +87,8 @@ source_set("feature_flags") {
source_set("test_support") {
testonly = true
sources = [
"browser_state_policy_connector_mock.h",
"browser_state_policy_connector_mock.mm",
"enterprise_policy_test_helper.cc",
"enterprise_policy_test_helper.h",
"test_platform_policy_provider.cc",
......
......@@ -43,6 +43,8 @@ class BrowserStatePolicyConnector {
policy::SchemaRegistry* GetSchemaRegistry() const { return schema_registry_; }
private:
friend class BrowserStatePolicyConnectorMock;
// |policy_providers_| contains a list of the policy providers available for
// the PolicyService of this connector, in decreasing order of priority.
//
......
// 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.
#ifndef IOS_CHROME_BROWSER_POLICY_BROWSER_STATE_POLICY_CONNECTOR_MOCK_H_
#define IOS_CHROME_BROWSER_POLICY_BROWSER_STATE_POLICY_CONNECTOR_MOCK_H_
#include <memory>
#include "ios/chrome/browser/policy/browser_state_policy_connector.h"
namespace policy {
class PolicyService;
class SchemaRegistry;
} // namespace policy
// BrowserStatePolicyConnector creates and manages the per-BrowserState policy
// components and their integration with PrefService.
class BrowserStatePolicyConnectorMock : public BrowserStatePolicyConnector {
public:
BrowserStatePolicyConnectorMock(
std::unique_ptr<policy::PolicyService> policy_service,
policy::SchemaRegistry* schema_registry);
~BrowserStatePolicyConnectorMock();
BrowserStatePolicyConnectorMock(const BrowserStatePolicyConnectorMock&) =
delete;
BrowserStatePolicyConnectorMock& operator=(
const BrowserStatePolicyConnectorMock&) = delete;
};
#endif // IOS_CHROME_BROWSER_POLICY_BROWSER_STATE_POLICY_CONNECTOR_MOCK_H_
// 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.
#import "ios/chrome/browser/policy/browser_state_policy_connector_mock.h"
#include "components/policy/core/common/policy_service_impl.h"
#include "components/policy/core/common/schema_registry.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
BrowserStatePolicyConnectorMock::BrowserStatePolicyConnectorMock(
std::unique_ptr<policy::PolicyService> policy_service,
policy::SchemaRegistry* schema_registry) {
policy_service_ = std::move(policy_service);
schema_registry_ = schema_registry;
}
BrowserStatePolicyConnectorMock::~BrowserStatePolicyConnectorMock() = default;
......@@ -54,6 +54,7 @@ source_set("test_support") {
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/browser_state:browser_state_impl",
"//ios/chrome/browser/content_settings",
"//ios/chrome/browser/policy:policy",
"//ios/chrome/browser/prefs:browser_prefs",
"//ios/chrome/browser/safe_browsing:test_support",
"//ios/chrome/browser/ui/util:multiwindow_util",
......
......@@ -70,6 +70,8 @@ class TestingApplicationContext : public ApplicationContext {
base::ThreadChecker thread_checker_;
std::string application_locale_;
PrefService* local_state_;
// Must be destroyed after |local_state_|.
std::unique_ptr<BrowserPolicyConnectorIOS> browser_policy_connector_;
ios::ChromeBrowserStateManager* chrome_browser_state_manager_;
std::unique_ptr<network_time::NetworkTimeTracker> network_time_tracker_;
bool was_last_shutdown_clean_;
......
......@@ -12,6 +12,8 @@
#include "base/time/default_tick_clock.h"
#include "components/network_time/network_time_tracker.h"
#include "components/safe_browsing/core/features.h"
#include "ios/chrome/browser/policy/browser_policy_connector_ios.h"
#include "ios/chrome/browser/policy/configuration_policy_handler_list_factory.h"
#import "ios/chrome/browser/safe_browsing/fake_safe_browsing_service.h"
#import "ios/public/provider/chrome/browser/chrome_browser_provider.h"
#include "net/url_request/url_request_context_getter.h"
......@@ -209,9 +211,12 @@ BrowserPolicyConnectorIOS*
TestingApplicationContext::GetBrowserPolicyConnector() {
DCHECK(thread_checker_.CalledOnValidThread());
// TODO(crbug.com/1055318): Determine what level of support is needed for
// unittesting and return a mock or fake here.
return nullptr;
if (!browser_policy_connector_.get()) {
browser_policy_connector_ = std::make_unique<BrowserPolicyConnectorIOS>(
base::Bind(&BuildPolicyHandlerList, true));
}
return browser_policy_connector_.get();
}
BreadcrumbPersistentStorageManager*
......
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