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/schema_registry.h"
20 #include "policy/policy_constants.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 using testing::Return
;
29 class ProfilePolicyConnectorTest
: public testing::Test
{
31 ProfilePolicyConnectorTest() {}
32 ~ProfilePolicyConnectorTest() override
{}
34 void SetUp() override
{
35 // This must be set up before the TestingBrowserProcess is created.
36 BrowserPolicyConnector::SetPolicyProviderForTesting(&mock_provider_
);
38 EXPECT_CALL(mock_provider_
, IsInitializationComplete(_
))
39 .WillRepeatedly(Return(true));
41 cloud_policy_store_
.NotifyStoreLoaded();
42 cloud_policy_manager_
.reset(new CloudPolicyManager(
43 std::string(), std::string(), &cloud_policy_store_
, loop_
.task_runner(),
44 loop_
.task_runner(), loop_
.task_runner()));
47 void TearDown() override
{
48 TestingBrowserProcess::GetGlobal()->SetBrowserPolicyConnector(nullptr);
49 cloud_policy_manager_
->Shutdown();
52 base::MessageLoop loop_
;
53 SchemaRegistry schema_registry_
;
54 MockConfigurationPolicyProvider mock_provider_
;
55 MockCloudPolicyStore cloud_policy_store_
;
56 scoped_ptr
<CloudPolicyManager
> cloud_policy_manager_
;
59 TEST_F(ProfilePolicyConnectorTest
, IsPolicyFromCloudPolicy
) {
60 ProfilePolicyConnector connector
;
62 #if defined(OS_CHROMEOS)
66 cloud_policy_manager_
.get());
68 // No policy is set initially.
70 connector
.IsPolicyFromCloudPolicy(autofill::prefs::kAutofillEnabled
));
71 PolicyNamespace
chrome_ns(POLICY_DOMAIN_CHROME
, std::string());
72 EXPECT_FALSE(connector
.policy_service()->GetPolicies(chrome_ns
).GetValue(
73 key::kAutoFillEnabled
));
75 // Set the policy at the cloud provider.
76 cloud_policy_store_
.policy_map_
.Set(key::kAutoFillEnabled
,
77 POLICY_LEVEL_MANDATORY
,
79 new base::FundamentalValue(false),
81 cloud_policy_store_
.NotifyStoreLoaded();
82 base::RunLoop().RunUntilIdle();
83 EXPECT_TRUE(connector
.IsPolicyFromCloudPolicy(key::kAutoFillEnabled
));
84 const base::Value
* value
=
85 connector
.policy_service()->GetPolicies(chrome_ns
).GetValue(
86 key::kAutoFillEnabled
);
88 EXPECT_TRUE(base::FundamentalValue(false).Equals(value
));
90 // Now test with a higher-priority provider also setting the policy.
92 map
.Set(key::kAutoFillEnabled
,
93 POLICY_LEVEL_MANDATORY
,
95 new base::FundamentalValue(true),
97 mock_provider_
.UpdateChromePolicy(map
);
98 EXPECT_FALSE(connector
.IsPolicyFromCloudPolicy(key::kAutoFillEnabled
));
99 value
= connector
.policy_service()->GetPolicies(chrome_ns
).GetValue(
100 key::kAutoFillEnabled
);
102 EXPECT_TRUE(base::FundamentalValue(true).Equals(value
));
105 connector
.Shutdown();
108 } // namespace policy