Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(466)

Side by Side Diff: chrome/browser/policy/managed_mode_policy_provider_unittest.cc

Issue 10510006: Add ManagedModePolicyProvider and extension API to get and set policies. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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.
4
5 #include "chrome/browser/policy/configuration_policy_provider_test.h"
6 #include "chrome/browser/policy/managed_mode_policy_provider.h"
7 #include "chrome/browser/policy/policy_bundle.h"
8 #include "chrome/browser/policy/policy_map.h"
9 #include "chrome/browser/prefs/testing_pref_store.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace policy {
13
14 namespace {
15
16 class TestHarness : public PolicyProviderTestHarness {
17 public:
18 TestHarness();
19 virtual ~TestHarness();
20
21 static PolicyProviderTestHarness* Create();
22
23 virtual void SetUp() OVERRIDE;
24
25 // PolicyProviderTestHarness implementation:
26 virtual ConfigurationPolicyProvider* CreateProvider(
27 const PolicyDefinitionList* policy_definition_list) OVERRIDE;
28
29 virtual void InstallEmptyPolicy() OVERRIDE;
30 virtual void InstallStringPolicy(const std::string& policy_name,
31 const std::string& policy_value) OVERRIDE;
32 virtual void InstallIntegerPolicy(const std::string& policy_name,
33 int policy_value) OVERRIDE;
34 virtual void InstallBooleanPolicy(const std::string& policy_name,
35 bool policy_value) OVERRIDE;
36 virtual void InstallStringListPolicy(
37 const std::string& policy_name,
38 const base::ListValue* policy_value) OVERRIDE;
39 virtual void InstallDictionaryPolicy(
40 const std::string& policy_name,
41 const base::DictionaryValue* policy_value) OVERRIDE;
42
43 private:
44 void InstallPolicy(const std::string& policy_name, base::Value* policy_value);
45
46 scoped_refptr<TestingPrefStore> pref_store_;
47
48 DISALLOW_COPY_AND_ASSIGN(TestHarness);
49 };
50
51 TestHarness::TestHarness()
52 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER),
53 pref_store_(new TestingPrefStore) {}
54
55 TestHarness::~TestHarness() {}
56
57 // static
58 PolicyProviderTestHarness* TestHarness::Create() {
59 return new TestHarness();
60 }
61
62 void TestHarness::SetUp() {
63 }
64
65 ConfigurationPolicyProvider* TestHarness::CreateProvider(
66 const PolicyDefinitionList* policy_definition_list) {
67 return new ManagedModePolicyProvider(pref_store_);
68 }
69
70 void TestHarness::InstallEmptyPolicy() {}
71
72 void TestHarness::InstallStringPolicy(const std::string& policy_name,
73 const std::string& policy_value) {
74 InstallPolicy(policy_name, base::Value::CreateStringValue(policy_value));
75 }
76
77 void TestHarness::InstallIntegerPolicy(const std::string& policy_name,
78 int policy_value) {
79 InstallPolicy(policy_name, base::Value::CreateIntegerValue(policy_value));
80 }
81
82 void TestHarness::InstallBooleanPolicy(const std::string& policy_name,
83 bool policy_value) {
84 InstallPolicy(policy_name, base::Value::CreateBooleanValue(policy_value));
85 }
86
87 void TestHarness::InstallStringListPolicy(const std::string& policy_name,
88 const base::ListValue* policy_value) {
89 InstallPolicy(policy_name, policy_value->DeepCopy());
90 }
91
92 void TestHarness::InstallDictionaryPolicy(
93 const std::string& policy_name,
94 const base::DictionaryValue* policy_value) {
95 InstallPolicy(policy_name, policy_value->DeepCopy());
96 }
97
98 void TestHarness::InstallPolicy(const std::string& policy_name,
99 base::Value* policy_value) {
100 base::DictionaryValue* cached_policy = NULL;
101 base::Value* value = NULL;
102 PrefStore::ReadResult result =
103 pref_store_->GetMutableValue(ManagedModePolicyProvider::kPolicies,
104 &value);
105 switch (result) {
106 case PrefStore::READ_NO_VALUE:
107 cached_policy = new base::DictionaryValue;
108 pref_store_->SetValue(ManagedModePolicyProvider::kPolicies,
109 cached_policy);
110 break;
111 case PrefStore::READ_OK:
112 ASSERT_TRUE(value->GetAsDictionary(&cached_policy));
113 break;
114 default:
115 FAIL() << "Invalid result reading policy: " << result;
116 return;
117 }
118 cached_policy->SetWithoutPathExpansion(policy_name, policy_value);
119 }
120
121 } // namespace
122
123 // Instantiate abstract test case for basic policy reading tests.
124 INSTANTIATE_TEST_CASE_P(
125 ManagedModePolicyProviderTest,
126 ConfigurationPolicyProviderTest,
127 testing::Values(TestHarness::Create));
128
129 class ManagedModePolicyProviderAPITest : public PolicyTestBase {
130 protected:
131 ManagedModePolicyProviderAPITest()
132 : pref_store_(new TestingPrefStore),
133 provider_(pref_store_) {}
134 virtual ~ManagedModePolicyProviderAPITest() {}
135
136 scoped_refptr<TestingPrefStore> pref_store_;
137 ManagedModePolicyProvider provider_;
138 };
139
140 const char kPolicyKey[] = "TestingPolicy";
141
142 TEST_F(ManagedModePolicyProviderAPITest, Empty) {
143 EXPECT_FALSE(provider_.GetPolicy(kPolicyKey));
144
145 const PolicyBundle kEmptyBundle;
146 EXPECT_TRUE(provider_.policies().Equals(kEmptyBundle));
147 }
148
149 TEST_F(ManagedModePolicyProviderAPITest, SetPolicy) {
150 base::StringValue policy_value("PolicyValue");
151 provider_.SetPolicy(kPolicyKey, &policy_value);
152
153 EXPECT_TRUE(base::Value::Equals(&policy_value,
154 provider_.GetPolicy(kPolicyKey)));
155
156 PolicyBundle expected_bundle;
157 PolicyMap* policy_map =
158 &expected_bundle.Get(POLICY_DOMAIN_CHROME, std::string());
159 policy_map->Set(kPolicyKey, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
160 policy_value.DeepCopy());
161 EXPECT_TRUE(provider_.policies().Equals(expected_bundle));
162
163 // A newly-created provider should have the same policies.
164 ManagedModePolicyProvider new_provider(pref_store_);
165 EXPECT_TRUE(new_provider.policies().Equals(expected_bundle));
166 }
167
168 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/managed_mode_policy_provider_factory.cc ('k') | chrome/browser/prefs/testing_pref_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698