OLD | NEW |
---|---|
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | |
5 #include "base/callback.h" | 6 #include "base/callback.h" |
6 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
7 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
8 #include "base/prefs/pref_value_map.h" | 9 #include "base/prefs/pref_value_map.h" |
9 #include "base/values.h" | 10 #include "base/values.h" |
10 #include "components/policy/core/browser/configuration_policy_handler.h" | 11 #include "components/policy/core/browser/configuration_policy_handler.h" |
11 #include "components/policy/core/browser/policy_error_map.h" | 12 #include "components/policy/core/browser/policy_error_map.h" |
12 #include "components/policy/core/common/policy_map.h" | 13 #include "components/policy/core/common/policy_map.h" |
13 #include "components/policy/core/common/schema.h" | 14 #include "components/policy/core/common/schema.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace policy { | 17 namespace policy { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 StringToIntEnumListPolicyHandler::MappingEntry kTestTypeMap[] = { | 21 void GetIntegerTypeMap( |
21 { "one", 1 }, | 22 ScopedVector<StringMappingListPolicyHandler::MappingEntry>* result) { |
22 { "two", 2 }, | 23 result->push_back(new StringMappingListPolicyHandler::MappingEntry( |
23 }; | 24 "one", scoped_ptr<base::Value>(base::Value::CreateIntegerValue(1)))); |
25 result->push_back(new StringMappingListPolicyHandler::MappingEntry( | |
26 "two", scoped_ptr<base::Value>(base::Value::CreateIntegerValue(2)))); | |
Joao da Silva
2014/06/18 08:45:26
CreateIntegerValue() is deprecated, use new base::
| |
27 } | |
24 | 28 |
25 const char kTestPolicy[] = "unit_test.test_policy"; | 29 const char kTestPolicy[] = "unit_test.test_policy"; |
26 const char kTestPref[] = "unit_test.test_pref"; | 30 const char kTestPref[] = "unit_test.test_pref"; |
27 | 31 |
28 class TestSchemaValidatingPolicyHandler : public SchemaValidatingPolicyHandler { | 32 class TestSchemaValidatingPolicyHandler : public SchemaValidatingPolicyHandler { |
29 public: | 33 public: |
30 TestSchemaValidatingPolicyHandler(const Schema& schema, | 34 TestSchemaValidatingPolicyHandler(const Schema& schema, |
31 SchemaOnErrorStrategy strategy) | 35 SchemaOnErrorStrategy strategy) |
32 : SchemaValidatingPolicyHandler("PolicyForTesting", schema, strategy) {} | 36 : SchemaValidatingPolicyHandler("PolicyForTesting", schema, strategy) {} |
33 virtual ~TestSchemaValidatingPolicyHandler() {} | 37 virtual ~TestSchemaValidatingPolicyHandler() {} |
34 | 38 |
35 virtual void ApplyPolicySettings(const policy::PolicyMap&, | 39 virtual void ApplyPolicySettings(const policy::PolicyMap&, |
36 PrefValueMap*) OVERRIDE { | 40 PrefValueMap*) OVERRIDE { |
37 } | 41 } |
38 | 42 |
39 bool CheckAndGetValueForTest(const PolicyMap& policies, | 43 bool CheckAndGetValueForTest(const PolicyMap& policies, |
40 scoped_ptr<base::Value>* value) { | 44 scoped_ptr<base::Value>* value) { |
41 return SchemaValidatingPolicyHandler::CheckAndGetValue( | 45 return SchemaValidatingPolicyHandler::CheckAndGetValue( |
42 policies, NULL, value); | 46 policies, NULL, value); |
43 } | 47 } |
44 }; | 48 }; |
45 | 49 |
46 } // namespace | 50 } // namespace |
47 | 51 |
48 TEST(StringToIntEnumListPolicyHandlerTest, CheckPolicySettings) { | 52 TEST(StringToIntEnumListPolicyHandlerTest, CheckPolicySettings) { |
49 base::ListValue list; | 53 base::ListValue list; |
50 PolicyMap policy_map; | 54 PolicyMap policy_map; |
51 PolicyErrorMap errors; | 55 PolicyErrorMap errors; |
52 StringToIntEnumListPolicyHandler handler( | 56 StringMappingListPolicyHandler handler( |
53 kTestPolicy, | 57 kTestPolicy, |
54 kTestPref, | 58 kTestPref, |
55 kTestTypeMap, | 59 base::Bind(GetIntegerTypeMap)); |
56 kTestTypeMap + arraysize(kTestTypeMap)); | |
57 | 60 |
58 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, | 61 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, |
59 POLICY_SCOPE_USER, list.DeepCopy(), NULL); | 62 POLICY_SCOPE_USER, list.DeepCopy(), NULL); |
60 errors.Clear(); | 63 errors.Clear(); |
61 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 64 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
62 EXPECT_TRUE(errors.empty()); | 65 EXPECT_TRUE(errors.empty()); |
63 | 66 |
64 list.AppendString("one"); | 67 list.AppendString("one"); |
65 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, | 68 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, |
66 POLICY_SCOPE_USER, list.DeepCopy(), NULL); | 69 POLICY_SCOPE_USER, list.DeepCopy(), NULL); |
(...skipping 11 matching lines...) Expand all Loading... | |
78 | 81 |
79 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, | 82 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, |
80 POLICY_SCOPE_USER, | 83 POLICY_SCOPE_USER, |
81 base::Value::CreateStringValue("no list"), NULL); | 84 base::Value::CreateStringValue("no list"), NULL); |
82 errors.Clear(); | 85 errors.Clear(); |
83 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); | 86 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); |
84 EXPECT_FALSE(errors.empty()); | 87 EXPECT_FALSE(errors.empty()); |
85 EXPECT_FALSE(errors.GetErrors(kTestPolicy).empty()); | 88 EXPECT_FALSE(errors.GetErrors(kTestPolicy).empty()); |
86 } | 89 } |
87 | 90 |
88 TEST(StringToIntEnumListPolicyHandlerTest, ApplyPolicySettings) { | 91 TEST(StringMappingListPolicyHandlerTest, ApplyPolicySettings) { |
89 base::ListValue list; | 92 base::ListValue list; |
90 base::ListValue expected; | 93 base::ListValue expected; |
91 PolicyMap policy_map; | 94 PolicyMap policy_map; |
92 PrefValueMap prefs; | 95 PrefValueMap prefs; |
93 base::Value* value; | 96 base::Value* value; |
94 StringToIntEnumListPolicyHandler handler( | 97 StringMappingListPolicyHandler handler( |
95 kTestPolicy, | 98 kTestPolicy, |
96 kTestPref, | 99 kTestPref, |
97 kTestTypeMap, | 100 base::Bind(GetIntegerTypeMap)); |
98 kTestTypeMap + arraysize(kTestTypeMap)); | |
99 | 101 |
100 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, | 102 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, |
101 POLICY_SCOPE_USER, list.DeepCopy(), NULL); | 103 POLICY_SCOPE_USER, list.DeepCopy(), NULL); |
102 handler.ApplyPolicySettings(policy_map, &prefs); | 104 handler.ApplyPolicySettings(policy_map, &prefs); |
103 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 105 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
104 EXPECT_TRUE(base::Value::Equals(&expected, value)); | 106 EXPECT_TRUE(base::Value::Equals(&expected, value)); |
105 | 107 |
106 list.AppendString("two"); | 108 list.AppendString("two"); |
107 expected.AppendInteger(2); | 109 expected.AppendInteger(2); |
108 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, | 110 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, |
(...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
687 handler_recommended.ApplyPolicySettings(policy_map_mandatory, &prefs); | 689 handler_recommended.ApplyPolicySettings(policy_map_mandatory, &prefs); |
688 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref)); | 690 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref)); |
689 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref)); | 691 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref)); |
690 | 692 |
691 EXPECT_FALSE( | 693 EXPECT_FALSE( |
692 handler_none.CheckPolicySettings(policy_map_recommended, &errors)); | 694 handler_none.CheckPolicySettings(policy_map_recommended, &errors)); |
693 EXPECT_FALSE(errors.empty()); | 695 EXPECT_FALSE(errors.empty()); |
694 } | 696 } |
695 | 697 |
696 } // namespace policy | 698 } // namespace policy |
OLD | NEW |