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

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: test 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 const char kPolicies[] = "policies";
13
14 namespace policy {
15
16 namespace {
17
18 class TestHarness : public PolicyProviderTestHarness {
19 public:
20 TestHarness();
21 virtual ~TestHarness();
22
23 static PolicyProviderTestHarness* Create();
24
25 virtual void SetUp() OVERRIDE;
26
27 // PolicyProviderTestHarness implementation:
28 virtual ConfigurationPolicyProvider* CreateProvider(
29 const PolicyDefinitionList* policy_definition_list) OVERRIDE;
30
31 virtual void InstallEmptyPolicy() OVERRIDE;
32 virtual void InstallStringPolicy(const std::string& policy_name,
33 const std::string& policy_value) OVERRIDE;
34 virtual void InstallIntegerPolicy(const std::string& policy_name,
35 int policy_value) OVERRIDE;
36 virtual void InstallBooleanPolicy(const std::string& policy_name,
37 bool policy_value) OVERRIDE;
38 virtual void InstallStringListPolicy(
39 const std::string& policy_name,
40 const base::ListValue* policy_value) OVERRIDE;
41 virtual void InstallDictionaryPolicy(
42 const std::string& policy_name,
43 const base::DictionaryValue* policy_value) OVERRIDE;
44
45 private:
46 void InstallPolicy(const std::string& policy_name, base::Value* policy_value);
47
48 scoped_refptr<TestingPrefStore> pref_store_;
49
50 DISALLOW_COPY_AND_ASSIGN(TestHarness);
51 };
52
53 TestHarness::TestHarness()
54 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER),
55 pref_store_(new TestingPrefStore) {}
56
57 TestHarness::~TestHarness() {}
58
59 // static
60 PolicyProviderTestHarness* TestHarness::Create() {
61 return new TestHarness();
62 }
63
64 void TestHarness::SetUp() {
65 }
66
67 ConfigurationPolicyProvider* TestHarness::CreateProvider(
68 const PolicyDefinitionList* policy_definition_list) {
69 return new ManagedModePolicyProvider(pref_store_);
70 }
71
72 void TestHarness::InstallEmptyPolicy() {}
73
74 void TestHarness::InstallStringPolicy(const std::string& policy_name,
75 const std::string& policy_value) {
76 InstallPolicy(policy_name, base::Value::CreateStringValue(policy_value));
77 }
78
79 void TestHarness::InstallIntegerPolicy(const std::string& policy_name,
80 int policy_value) {
81 InstallPolicy(policy_name, base::Value::CreateIntegerValue(policy_value));
82 }
83
84 void TestHarness::InstallBooleanPolicy(const std::string& policy_name,
85 bool policy_value) {
86 InstallPolicy(policy_name, base::Value::CreateBooleanValue(policy_value));
87 }
88
89 void TestHarness::InstallStringListPolicy(const std::string& policy_name,
90 const base::ListValue* policy_value) {
91 InstallPolicy(policy_name, policy_value->DeepCopy());
92 }
93
94 void TestHarness::InstallDictionaryPolicy(
95 const std::string& policy_name,
96 const base::DictionaryValue* policy_value) {
97 InstallPolicy(policy_name, policy_value->DeepCopy());
98 }
99
100 void TestHarness::InstallPolicy(const std::string& policy_name,
101 base::Value* policy_value) {
102 base::DictionaryValue* cached_policy = NULL;
103 base::Value* value = NULL;
104 PrefStore::ReadResult result =
105 pref_store_->GetMutableValue(ManagedModePolicyProvider::kPolicies,
106 &value);
107 switch (result) {
108 case PrefStore::READ_NO_VALUE:
109 cached_policy = new base::DictionaryValue;
110 pref_store_->SetValue(ManagedModePolicyProvider::kPolicies,
111 cached_policy);
112 break;
113 case PrefStore::READ_OK:
114 ASSERT_TRUE(value->GetAsDictionary(&cached_policy));
115 break;
116 default:
117 FAIL() << "Invalid result reading policy: " << result;
118 return;
119 }
120 cached_policy->SetWithoutPathExpansion(policy_name, policy_value);
121 }
122
123 } // namespace
124
125 // Instantiate abstract test case for basic policy reading tests.
126 INSTANTIATE_TEST_CASE_P(
127 ManagedModePolicyProviderTest,
128 ConfigurationPolicyProviderTest,
129 testing::Values(TestHarness::Create));
130
131 class ManagedModePolicyProviderAPITest : public PolicyTestBase {
132 protected:
133 ManagedModePolicyProviderAPITest()
134 : pref_store_(new TestingPrefStore),
135 provider_(pref_store_) {}
136 virtual ~ManagedModePolicyProviderAPITest() {}
137
138 scoped_refptr<TestingPrefStore> pref_store_;
139 ManagedModePolicyProvider provider_;
140 };
141
142 const char kPolicyKey[] = "TestingPolicy";
143
144 TEST_F(ManagedModePolicyProviderAPITest, Empty) {
145 EXPECT_FALSE(provider_.GetPolicy(kPolicyKey));
146
147 const PolicyBundle kEmptyBundle;
148 EXPECT_TRUE(provider_.policies().Equals(kEmptyBundle));
149 }
150
151 TEST_F(ManagedModePolicyProviderAPITest, SetPolicy) {
152 base::StringValue policy_value("PolicyValue");
153 provider_.SetPolicy(kPolicyKey, &policy_value);
154
155 EXPECT_TRUE(base::Value::Equals(&policy_value,
156 provider_.GetPolicy(kPolicyKey)));
157
158 PolicyBundle expected_bundle;
159 PolicyMap* policy_map =
160 &expected_bundle.Get(POLICY_DOMAIN_CHROME, std::string());
161 policy_map->Set(kPolicyKey, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
162 policy_value.DeepCopy());
163 EXPECT_TRUE(provider_.policies().Equals(expected_bundle));
164 }
Mattias Nissler (ping if slow) 2012/06/13 04:53:44 To get full coverage, a test making sure that pers
Bernhard Bauer 2012/06/13 14:42:29 Done.
165
166 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698