1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/policy/policy_test_utils.h"
#include <optional>
#include <string>
#include <utility>
#include "base/callback_list.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/path_service.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/current_thread.h"
#include "base/test/bind.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/lifetime/termination_notification.h"
#include "chrome/common/chrome_paths.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/policy_constants.h"
#include "components/safe_search_api/safe_search_util.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/network_service_util.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/dns/mock_host_resolver.h"
using content::BrowserThread;
namespace policy {
void GetTestDataDirectory(base::FilePath* test_data_directory) {
ASSERT_TRUE(
base::PathService::Get(chrome::DIR_TEST_DATA, test_data_directory));
}
PolicyTest::PolicyTest() = default;
PolicyTest::~PolicyTest() = default;
void PolicyTest::SetUpInProcessBrowserTestFixture() {
base::CommandLine::ForCurrentProcess()->AppendSwitch("noerrdialogs");
provider_.SetDefaultReturns(true /* is_initialization_complete_return */,
true /* is_first_policy_load_complete_return */);
BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
}
void PolicyTest::SetUpOnMainThread() {
host_resolver()->AddRule("*", "127.0.0.1");
}
void PolicyTest::UpdateProviderPolicy(const PolicyMap& policy) {
PolicyMap policy_with_defaults = policy.Clone();
#if BUILDFLAG(IS_CHROMEOS)
SetEnterpriseUsersDefaults(&policy_with_defaults);
#endif
provider_.UpdateChromePolicy(policy_with_defaults);
}
// static
void PolicyTest::SetPolicy(PolicyMap* policies,
const char* key,
std::optional<base::Value> value) {
policies->Set(key, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
POLICY_SOURCE_CLOUD, std::move(value), nullptr);
}
// static
bool PolicyTest::FetchSubresource(content::WebContents* web_contents,
const GURL& url) {
std::string script(
"var xhr = new XMLHttpRequest();"
"xhr.open('GET', '");
script += url.spec() +
"', true);"
"new Promise(resolve => {"
" xhr.onload = function (e) {"
" if (xhr.readyState === 4) {"
" resolve(xhr.status === 200);"
" }"
" };"
" xhr.onerror = function () {"
" resolve(false);"
" };"
" xhr.send(null)"
"});";
return content::EvalJs(web_contents, script).ExtractBool();
}
void PolicyTest::FlushBlocklistPolicy() {
// Updates of the URLBlocklist are done on IO, after building the blocklist
// on the blocking pool, which is initiated from IO.
content::RunAllPendingInMessageLoop(BrowserThread::IO);
content::RunAllTasksUntilIdle();
content::RunAllPendingInMessageLoop(BrowserThread::IO);
}
PolicyTestAppTerminationObserver::PolicyTestAppTerminationObserver() {
terminating_subscription_ = browser_shutdown::AddAppTerminatingCallback(
base::BindOnce(&PolicyTestAppTerminationObserver::OnAppTerminating,
base::Unretained(this)));
}
PolicyTestAppTerminationObserver::~PolicyTestAppTerminationObserver() = default;
} // namespace policy
|