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/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/values.h"
10 #include "chrome/test/base/testing_browser_process.h"
11 #include "components/autofill/core/common/autofill_pref_names.h"
12 #include "components/policy/core/browser/browser_policy_connector.h"
13 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
14 #include "components/policy/core/common/cloud/cloud_policy_manager.h"
15 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
16 #include "components/policy/core/common/mock_configuration_policy_provider.h"
17 #include "components/policy/core/common/policy_bundle.h"
18 #include "components/policy/core/common/policy_map.h"
19 #include "components/policy/core/common/policy_service.h"
20 #include "components/policy/core/common/schema_registry.h"
21 #include "policy/policy_constants.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 using testing::Return;
30 class ProfilePolicyConnectorTest : public testing::Test {
32 ProfilePolicyConnectorTest() {}
33 ~ProfilePolicyConnectorTest() override {}
35 void SetUp() override {
36 // This must be set up before the TestingBrowserProcess is created.
37 BrowserPolicyConnector::SetPolicyProviderForTesting(&mock_provider_);
39 EXPECT_CALL(mock_provider_, IsInitializationComplete(_))
40 .WillRepeatedly(Return(true));
42 cloud_policy_store_.NotifyStoreLoaded();
43 cloud_policy_manager_.reset(
44 new CloudPolicyManager(PolicyNamespaceKey("", ""),
46 loop_.message_loop_proxy(),
47 loop_.message_loop_proxy(),
48 loop_.message_loop_proxy()));
51 void TearDown() override {
52 TestingBrowserProcess::GetGlobal()->SetBrowserPolicyConnector(NULL);
53 cloud_policy_manager_->Shutdown();
56 base::MessageLoop loop_;
57 SchemaRegistry schema_registry_;
58 MockConfigurationPolicyProvider mock_provider_;
59 MockCloudPolicyStore cloud_policy_store_;
60 scoped_ptr<CloudPolicyManager> cloud_policy_manager_;
63 TEST_F(ProfilePolicyConnectorTest, IsPolicyFromCloudPolicy) {
64 ProfilePolicyConnector connector;
66 #if defined(OS_CHROMEOS)
70 cloud_policy_manager_.get());
72 // No policy is set initially.
74 connector.IsPolicyFromCloudPolicy(autofill::prefs::kAutofillEnabled));
75 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, "");
76 EXPECT_FALSE(connector.policy_service()->GetPolicies(chrome_ns).GetValue(
77 key::kAutoFillEnabled));
79 // Set the policy at the cloud provider.
80 cloud_policy_store_.policy_map_.Set(key::kAutoFillEnabled,
81 POLICY_LEVEL_MANDATORY,
83 new base::FundamentalValue(false),
85 cloud_policy_store_.NotifyStoreLoaded();
86 base::RunLoop().RunUntilIdle();
87 EXPECT_TRUE(connector.IsPolicyFromCloudPolicy(key::kAutoFillEnabled));
88 const base::Value* value =
89 connector.policy_service()->GetPolicies(chrome_ns).GetValue(
90 key::kAutoFillEnabled);
92 EXPECT_TRUE(base::FundamentalValue(false).Equals(value));
94 // Now test with a higher-priority provider also setting the policy.
96 map.Set(key::kAutoFillEnabled,
97 POLICY_LEVEL_MANDATORY,
99 new base::FundamentalValue(true),
101 mock_provider_.UpdateChromePolicy(map);
102 EXPECT_FALSE(connector.IsPolicyFromCloudPolicy(key::kAutoFillEnabled));
103 value = connector.policy_service()->GetPolicies(chrome_ns).GetValue(
104 key::kAutoFillEnabled);
106 EXPECT_TRUE(base::FundamentalValue(true).Equals(value));
109 connector.Shutdown();
112 } // namespace policy