Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / user_policy_test_helper.cc
blobecc45eb27cec9effbaa581fafbdc55321a9e9515
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/policy/user_policy_test_helper.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/json/json_writer.h"
11 #include "base/run_loop.h"
12 #include "base/values.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
15 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h"
16 #include "chrome/browser/policy/profile_policy_connector.h"
17 #include "chrome/browser/policy/profile_policy_connector_factory.h"
18 #include "chrome/browser/policy/test/local_policy_test_server.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "components/policy/core/browser/browser_policy_connector.h"
21 #include "components/policy/core/common/cloud/cloud_policy_client.h"
22 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
23 #include "components/policy/core/common/cloud/cloud_policy_core.h"
24 #include "components/policy/core/common/policy_service.h"
25 #include "components/policy/core/common/policy_switches.h"
26 #include "policy/proto/device_management_backend.pb.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "url/gurl.h"
30 namespace policy {
32 namespace {
34 std::string BuildPolicy(const base::DictionaryValue& mandatory,
35 const base::DictionaryValue& recommended,
36 const std::string& policyType,
37 const std::string& account_id) {
38 scoped_ptr<base::DictionaryValue> policy_type_dict(new base::DictionaryValue);
39 policy_type_dict->SetWithoutPathExpansion("mandatory",
40 mandatory.CreateDeepCopy());
41 policy_type_dict->SetWithoutPathExpansion("recommended",
42 recommended.CreateDeepCopy());
44 scoped_ptr<base::ListValue> managed_users_list(new base::ListValue);
45 managed_users_list->AppendString("*");
47 base::DictionaryValue root_dict;
48 root_dict.SetWithoutPathExpansion(policyType, policy_type_dict.Pass());
49 root_dict.SetWithoutPathExpansion("managed_users", managed_users_list.Pass());
50 root_dict.SetStringWithoutPathExpansion("policy_user", account_id);
51 root_dict.SetIntegerWithoutPathExpansion("current_key_index", 0);
53 std::string json_policy;
54 base::JSONWriter::WriteWithOptions(
55 root_dict, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json_policy);
56 return json_policy;
59 } // namespace
61 UserPolicyTestHelper::UserPolicyTestHelper(const std::string& account_id)
62 : account_id_(account_id) {
65 UserPolicyTestHelper::~UserPolicyTestHelper() {
68 void UserPolicyTestHelper::Init(
69 const base::DictionaryValue& mandatory_policy,
70 const base::DictionaryValue& recommended_policy) {
71 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
72 WritePolicyFile(mandatory_policy, recommended_policy);
74 test_server_.reset(new LocalPolicyTestServer(PolicyFilePath()));
75 ASSERT_TRUE(test_server_->Start());
78 void UserPolicyTestHelper::UpdateCommandLine(
79 base::CommandLine* command_line) const {
80 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl,
81 test_server_->GetServiceURL().spec());
84 void UserPolicyTestHelper::WaitForInitialPolicy(Profile* profile) {
85 BrowserPolicyConnector* const connector =
86 g_browser_process->browser_policy_connector();
87 connector->ScheduleServiceInitialization(0);
89 UserCloudPolicyManagerChromeOS* const policy_manager =
90 UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile);
92 // Give a bogus OAuth token to the |policy_manager|. This should make its
93 // CloudPolicyClient fetch the DMToken.
94 ASSERT_FALSE(policy_manager->core()->client()->is_registered());
95 const enterprise_management::DeviceRegisterRequest::Type registration_type =
96 enterprise_management::DeviceRegisterRequest::USER;
97 policy_manager->core()->client()->Register(
98 registration_type,
99 enterprise_management::DeviceRegisterRequest::FLAVOR_USER_REGISTRATION,
100 "bogus", std::string(), std::string(), std::string());
102 policy::ProfilePolicyConnector* const profile_connector =
103 policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile);
104 policy::PolicyService* const policy_service =
105 profile_connector->policy_service();
107 base::RunLoop run_loop;
108 policy_service->RefreshPolicies(run_loop.QuitClosure());
109 run_loop.Run();
112 void UserPolicyTestHelper::UpdatePolicy(
113 const base::DictionaryValue& mandatory_policy,
114 const base::DictionaryValue& recommended_policy,
115 Profile* profile) {
116 WritePolicyFile(mandatory_policy, recommended_policy);
118 policy::ProfilePolicyConnector* const profile_connector =
119 policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile);
120 policy::PolicyService* const policy_service =
121 profile_connector->policy_service();
123 base::RunLoop run_loop;
124 policy_service->RefreshPolicies(run_loop.QuitClosure());
125 run_loop.Run();
128 void UserPolicyTestHelper::WritePolicyFile(
129 const base::DictionaryValue& mandatory,
130 const base::DictionaryValue& recommended) {
131 const std::string policy = BuildPolicy(
132 mandatory, recommended, dm_protocol::kChromeUserPolicyType, account_id_);
133 const int bytes_written =
134 base::WriteFile(PolicyFilePath(), policy.data(), policy.size());
135 ASSERT_EQ(static_cast<int>(policy.size()), bytes_written);
138 base::FilePath UserPolicyTestHelper::PolicyFilePath() const {
139 return temp_dir_.path().AppendASCII("policy.json");
142 } // namespace policy