OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/policy/core/common/schema_registry.h" | 5 #include "components/policy/core/common/schema_registry.h" |
6 | 6 |
7 #include "components/policy/core/common/policy_namespace.h" | 7 #include "components/policy/core/common/policy_namespace.h" |
8 #include "components/policy/core/common/schema.h" | 8 #include "components/policy/core/common/schema.h" |
9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
50 | 50 |
51 } // namespace | 51 } // namespace |
52 | 52 |
53 TEST(SchemaRegistryTest, Notifications) { | 53 TEST(SchemaRegistryTest, Notifications) { |
54 std::string error; | 54 std::string error; |
55 Schema schema = Schema::Parse(kTestSchema, &error); | 55 Schema schema = Schema::Parse(kTestSchema, &error); |
56 ASSERT_TRUE(schema.valid()) << error; | 56 ASSERT_TRUE(schema.valid()) << error; |
57 | 57 |
58 MockSchemaRegistryObserver observer; | 58 MockSchemaRegistryObserver observer; |
59 SchemaRegistry registry; | 59 SchemaRegistry registry; |
60 EXPECT_FALSE(registry.HasObservers()); | |
61 registry.AddObserver(&observer); | 60 registry.AddObserver(&observer); |
62 EXPECT_TRUE(registry.HasObservers()); | |
63 | 61 |
64 ASSERT_TRUE(registry.schema_map()); | 62 ASSERT_TRUE(registry.schema_map()); |
65 EXPECT_FALSE(registry.schema_map()->GetSchema( | 63 EXPECT_FALSE(registry.schema_map()->GetSchema( |
66 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); | 64 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); |
67 | 65 |
68 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); | 66 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
69 registry.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), | 67 registry.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), |
70 schema); | 68 schema); |
71 Mock::VerifyAndClearExpectations(&observer); | 69 Mock::VerifyAndClearExpectations(&observer); |
72 | 70 |
(...skipping 18 matching lines...) Expand all Loading... | |
91 // Registering multiple components at once issues only one notification. | 89 // Registering multiple components at once issues only one notification. |
92 ComponentMap components; | 90 ComponentMap components; |
93 components["abc"] = schema; | 91 components["abc"] = schema; |
94 components["def"] = schema; | 92 components["def"] = schema; |
95 components["xyz"] = schema; | 93 components["xyz"] = schema; |
96 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); | 94 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
97 registry.RegisterComponents(POLICY_DOMAIN_EXTENSIONS, components); | 95 registry.RegisterComponents(POLICY_DOMAIN_EXTENSIONS, components); |
98 Mock::VerifyAndClearExpectations(&observer); | 96 Mock::VerifyAndClearExpectations(&observer); |
99 | 97 |
100 registry.RemoveObserver(&observer); | 98 registry.RemoveObserver(&observer); |
101 EXPECT_FALSE(registry.HasObservers()); | |
102 } | 99 } |
103 | 100 |
104 TEST(SchemaRegistryTest, IsReady) { | 101 TEST(SchemaRegistryTest, IsReady) { |
105 SchemaRegistry registry; | 102 SchemaRegistry registry; |
106 MockSchemaRegistryObserver observer; | 103 MockSchemaRegistryObserver observer; |
107 registry.AddObserver(&observer); | 104 registry.AddObserver(&observer); |
108 | 105 |
109 EXPECT_FALSE(registry.IsReady()); | 106 EXPECT_FALSE(registry.IsReady()); |
110 #if defined(ENABLE_EXTENSIONS) | 107 #if defined(ENABLE_EXTENSIONS) |
111 EXPECT_CALL(observer, OnSchemaRegistryReady()).Times(0); | 108 EXPECT_CALL(observer, OnSchemaRegistryReady()).Times(0); |
(...skipping 15 matching lines...) Expand all Loading... | |
127 | 124 |
128 registry.RemoveObserver(&observer); | 125 registry.RemoveObserver(&observer); |
129 } | 126 } |
130 | 127 |
131 TEST(SchemaRegistryTest, Combined) { | 128 TEST(SchemaRegistryTest, Combined) { |
132 std::string error; | 129 std::string error; |
133 Schema schema = Schema::Parse(kTestSchema, &error); | 130 Schema schema = Schema::Parse(kTestSchema, &error); |
134 ASSERT_TRUE(schema.valid()) << error; | 131 ASSERT_TRUE(schema.valid()) << error; |
135 | 132 |
136 MockSchemaRegistryObserver observer; | 133 MockSchemaRegistryObserver observer; |
137 SchemaRegistry registry1; | 134 scoped_ptr<SchemaRegistry> registry1(new SchemaRegistry); |
bartfab (slow)
2014/06/20 14:48:01
Nit: #include "base/memory/scoped_ptr.h"
Joao da Silva
2014/06/20 15:30:14
Done.
| |
138 SchemaRegistry registry2; | 135 scoped_ptr<SchemaRegistry> registry2(new SchemaRegistry); |
139 CombinedSchemaRegistry combined; | 136 CombinedSchemaRegistry combined; |
140 combined.AddObserver(&observer); | 137 combined.AddObserver(&observer); |
141 | 138 |
142 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0); | 139 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0); |
143 registry1.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), | 140 registry1->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), |
144 schema); | 141 schema); |
145 Mock::VerifyAndClearExpectations(&observer); | 142 Mock::VerifyAndClearExpectations(&observer); |
146 | 143 |
147 // Starting to track a registry issues notifications when it comes with new | 144 // Starting to track a registry issues notifications when it comes with new |
148 // schemas. | 145 // schemas. |
149 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); | 146 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
150 combined.Track(®istry1); | 147 combined.Track(registry1.get()); |
151 Mock::VerifyAndClearExpectations(&observer); | 148 Mock::VerifyAndClearExpectations(&observer); |
152 | 149 |
153 // Adding a new empty registry does not trigger notifications. | 150 // Adding a new empty registry does not trigger notifications. |
154 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0); | 151 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0); |
155 combined.Track(®istry2); | 152 combined.Track(registry2.get()); |
156 Mock::VerifyAndClearExpectations(&observer); | 153 Mock::VerifyAndClearExpectations(&observer); |
157 | 154 |
158 // Adding the same component to the combined registry itself triggers | 155 // Adding the same component to the combined registry itself triggers |
159 // notifications. | 156 // notifications. |
160 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); | 157 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
161 combined.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), | 158 combined.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), |
162 schema); | 159 schema); |
163 Mock::VerifyAndClearExpectations(&observer); | 160 Mock::VerifyAndClearExpectations(&observer); |
164 | 161 |
165 // Adding components to the sub-registries triggers notifications. | 162 // Adding components to the sub-registries triggers notifications. |
166 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); | 163 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
167 registry2.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"), | 164 registry2->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"), |
168 schema); | 165 schema); |
169 Mock::VerifyAndClearExpectations(&observer); | 166 Mock::VerifyAndClearExpectations(&observer); |
170 | 167 |
171 // If the same component is published in 2 sub-registries then the combined | 168 // If the same component is published in 2 sub-registries then the combined |
172 // registry publishes one of them. | 169 // registry publishes one of them. |
173 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); | 170 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
174 registry1.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"), | 171 registry1->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"), |
175 schema); | 172 schema); |
176 Mock::VerifyAndClearExpectations(&observer); | 173 Mock::VerifyAndClearExpectations(&observer); |
177 | 174 |
178 ASSERT_EQ(1u, combined.schema_map()->GetDomains().size()); | 175 ASSERT_EQ(1u, combined.schema_map()->GetDomains().size()); |
179 ASSERT_TRUE(combined.schema_map()->GetComponents(POLICY_DOMAIN_EXTENSIONS)); | 176 ASSERT_TRUE(combined.schema_map()->GetComponents(POLICY_DOMAIN_EXTENSIONS)); |
180 ASSERT_EQ( | 177 ASSERT_EQ( |
181 2u, | 178 2u, |
182 combined.schema_map()->GetComponents(POLICY_DOMAIN_EXTENSIONS)->size()); | 179 combined.schema_map()->GetComponents(POLICY_DOMAIN_EXTENSIONS)->size()); |
183 EXPECT_TRUE(combined.schema_map()->GetSchema( | 180 EXPECT_TRUE(combined.schema_map()->GetSchema( |
184 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); | 181 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); |
185 EXPECT_TRUE(combined.schema_map()->GetSchema( | 182 EXPECT_TRUE(combined.schema_map()->GetSchema( |
186 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); | 183 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); |
187 EXPECT_FALSE(combined.schema_map()->GetSchema( | 184 EXPECT_FALSE(combined.schema_map()->GetSchema( |
188 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "xyz"))); | 185 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "xyz"))); |
189 | 186 |
190 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); | 187 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
191 registry1.UnregisterComponent( | 188 registry1->UnregisterComponent( |
192 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")); | 189 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")); |
193 Mock::VerifyAndClearExpectations(&observer); | 190 Mock::VerifyAndClearExpectations(&observer); |
194 // Still registered at the combined registry. | 191 // Still registered at the combined registry. |
195 EXPECT_TRUE(combined.schema_map()->GetSchema( | 192 EXPECT_TRUE(combined.schema_map()->GetSchema( |
196 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); | 193 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); |
197 | 194 |
198 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); | 195 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
199 combined.UnregisterComponent( | 196 combined.UnregisterComponent( |
200 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")); | 197 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")); |
201 Mock::VerifyAndClearExpectations(&observer); | 198 Mock::VerifyAndClearExpectations(&observer); |
202 // Now it's gone. | 199 // Now it's gone. |
203 EXPECT_FALSE(combined.schema_map()->GetSchema( | 200 EXPECT_FALSE(combined.schema_map()->GetSchema( |
204 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); | 201 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); |
205 | 202 |
206 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); | 203 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
207 registry1.UnregisterComponent( | 204 registry1->UnregisterComponent( |
208 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def")); | 205 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def")); |
209 Mock::VerifyAndClearExpectations(&observer); | 206 Mock::VerifyAndClearExpectations(&observer); |
210 // Still registered at registry2. | 207 // Still registered at registry2. |
211 EXPECT_TRUE(combined.schema_map()->GetSchema( | 208 EXPECT_TRUE(combined.schema_map()->GetSchema( |
212 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); | 209 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); |
213 | 210 |
214 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); | 211 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
215 registry2.UnregisterComponent( | 212 registry2->UnregisterComponent( |
216 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def")); | 213 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def")); |
217 Mock::VerifyAndClearExpectations(&observer); | 214 Mock::VerifyAndClearExpectations(&observer); |
218 // Now it's gone. | 215 // Now it's gone. |
219 EXPECT_FALSE(combined.schema_map()->GetSchema( | 216 EXPECT_FALSE(combined.schema_map()->GetSchema( |
220 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); | 217 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); |
221 | 218 |
222 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)).Times(2); | 219 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)).Times(2); |
223 registry1.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_CHROME, ""), | 220 registry1->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_CHROME, ""), |
224 schema); | 221 schema); |
225 registry2.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "hij"), | 222 registry2->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "hij"), |
226 schema); | 223 schema); |
227 Mock::VerifyAndClearExpectations(&observer); | 224 Mock::VerifyAndClearExpectations(&observer); |
228 | 225 |
229 // Untracking |registry1| doesn't trigger an update notification, because it | 226 // Untracking |registry1| doesn't trigger an update notification, because it |
230 // doesn't contain any components. | 227 // doesn't contain any components. |
231 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0); | 228 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0); |
232 combined.Untrack(®istry1); | 229 registry1.reset(); |
233 Mock::VerifyAndClearExpectations(&observer); | 230 Mock::VerifyAndClearExpectations(&observer); |
234 | 231 |
235 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); | 232 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
236 combined.Untrack(®istry2); | 233 registry2.reset(); |
237 Mock::VerifyAndClearExpectations(&observer); | 234 Mock::VerifyAndClearExpectations(&observer); |
238 | 235 |
239 combined.RemoveObserver(&observer); | 236 combined.RemoveObserver(&observer); |
240 } | 237 } |
241 | 238 |
242 } // namespace policy | 239 } // namespace policy |
OLD | NEW |