1 // Copyright 2014 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/policy/profile_policy_connector.h"
7 #include "base/run_loop.h"
8 #include "base/values.h"
9 #include "chrome/test/base/testing_browser_process.h"
10 #include "components/autofill/core/common/autofill_pref_names.h"
11 #include "components/policy/core/browser/browser_policy_connector.h"
12 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
13 #include "components/policy/core/common/cloud/cloud_policy_manager.h"
14 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
15 #include "components/policy/core/common/mock_configuration_policy_provider.h"
16 #include "components/policy/core/common/policy_bundle.h"
17 #include "components/policy/core/common/policy_map.h"
18 #include "components/policy/core/common/policy_service.h"
19 #include "components/policy/core/common/policy_types.h"
20 #include "components/policy/core/common/schema_registry.h"
21 #include "policy/policy_constants.h"
22 #include "policy/proto/device_management_backend.pb.h"
23 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 using testing::Return
;
31 class ProfilePolicyConnectorTest
: public testing::Test
{
33 ProfilePolicyConnectorTest() {}
34 ~ProfilePolicyConnectorTest() override
{}
36 void SetUp() override
{
37 // This must be set up before the TestingBrowserProcess is created.
38 BrowserPolicyConnector::SetPolicyProviderForTesting(&mock_provider_
);
40 EXPECT_CALL(mock_provider_
, IsInitializationComplete(_
))
41 .WillRepeatedly(Return(true));
43 cloud_policy_store_
.NotifyStoreLoaded();
44 cloud_policy_manager_
.reset(new CloudPolicyManager(
45 std::string(), std::string(), &cloud_policy_store_
, loop_
.task_runner(),
46 loop_
.task_runner(), loop_
.task_runner()));
49 void TearDown() override
{
50 TestingBrowserProcess::GetGlobal()->ShutdownBrowserPolicyConnector();
51 cloud_policy_manager_
->Shutdown();
54 base::MessageLoop loop_
;
55 SchemaRegistry schema_registry_
;
56 MockConfigurationPolicyProvider mock_provider_
;
57 MockCloudPolicyStore cloud_policy_store_
;
58 scoped_ptr
<CloudPolicyManager
> cloud_policy_manager_
;
61 TEST_F(ProfilePolicyConnectorTest
, IsManagedForManagedUsers
) {
62 ProfilePolicyConnector connector
;
64 #if defined(OS_CHROMEOS)
67 &schema_registry_
, cloud_policy_manager_
.get());
68 EXPECT_FALSE(connector
.IsManaged());
69 EXPECT_EQ(connector
.GetManagementDomain(), "");
71 cloud_policy_store_
.policy_
.reset(new enterprise_management::PolicyData());
72 cloud_policy_store_
.policy_
->set_username("test@testdomain.com");
73 cloud_policy_store_
.policy_
->set_state(
74 enterprise_management::PolicyData::ACTIVE
);
75 EXPECT_TRUE(connector
.IsManaged());
76 EXPECT_EQ(connector
.GetManagementDomain(), "testdomain.com");
82 TEST_F(ProfilePolicyConnectorTest
, IsPolicyFromCloudPolicy
) {
83 ProfilePolicyConnector connector
;
85 #if defined(OS_CHROMEOS)
88 &schema_registry_
, cloud_policy_manager_
.get());
90 // No policy is set initially.
92 connector
.IsPolicyFromCloudPolicy(autofill::prefs::kAutofillEnabled
));
93 PolicyNamespace
chrome_ns(POLICY_DOMAIN_CHROME
, std::string());
94 EXPECT_FALSE(connector
.policy_service()->GetPolicies(chrome_ns
).GetValue(
95 key::kAutoFillEnabled
));
97 // Set the policy at the cloud provider.
98 cloud_policy_store_
.policy_map_
.Set(key::kAutoFillEnabled
,
99 POLICY_LEVEL_MANDATORY
,
102 new base::FundamentalValue(false),
104 cloud_policy_store_
.NotifyStoreLoaded();
105 base::RunLoop().RunUntilIdle();
106 EXPECT_TRUE(connector
.IsPolicyFromCloudPolicy(key::kAutoFillEnabled
));
107 const base::Value
* value
=
108 connector
.policy_service()->GetPolicies(chrome_ns
).GetValue(
109 key::kAutoFillEnabled
);
111 EXPECT_TRUE(base::FundamentalValue(false).Equals(value
));
113 // Now test with a higher-priority provider also setting the policy.
115 map
.Set(key::kAutoFillEnabled
,
116 POLICY_LEVEL_MANDATORY
,
119 new base::FundamentalValue(true),
121 mock_provider_
.UpdateChromePolicy(map
);
122 EXPECT_FALSE(connector
.IsPolicyFromCloudPolicy(key::kAutoFillEnabled
));
123 value
= connector
.policy_service()->GetPolicies(chrome_ns
).GetValue(
124 key::kAutoFillEnabled
);
126 EXPECT_TRUE(base::FundamentalValue(true).Equals(value
));
129 connector
.Shutdown();
132 } // namespace policy