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 "base/memory/scoped_ptr.h" |
7 #include "components/policy/core/common/policy_namespace.h" | 8 #include "components/policy/core/common/policy_namespace.h" |
8 #include "components/policy/core/common/schema.h" | 9 #include "components/policy/core/common/schema.h" |
9 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 using ::testing::Mock; | 13 using ::testing::Mock; |
13 using ::testing::_; | 14 using ::testing::_; |
14 | 15 |
15 namespace policy { | 16 namespace policy { |
16 | 17 |
(...skipping 24 matching lines...) Expand all Loading... |
41 | 42 |
42 class MockSchemaRegistryObserver : public SchemaRegistry::Observer { | 43 class MockSchemaRegistryObserver : public SchemaRegistry::Observer { |
43 public: | 44 public: |
44 MockSchemaRegistryObserver() {} | 45 MockSchemaRegistryObserver() {} |
45 virtual ~MockSchemaRegistryObserver() {} | 46 virtual ~MockSchemaRegistryObserver() {} |
46 | 47 |
47 MOCK_METHOD1(OnSchemaRegistryUpdated, void(bool)); | 48 MOCK_METHOD1(OnSchemaRegistryUpdated, void(bool)); |
48 MOCK_METHOD0(OnSchemaRegistryReady, void()); | 49 MOCK_METHOD0(OnSchemaRegistryReady, void()); |
49 }; | 50 }; |
50 | 51 |
| 52 bool SchemaMapEquals(const scoped_refptr<SchemaMap>& schema_map1, |
| 53 const scoped_refptr<SchemaMap>& schema_map2) { |
| 54 PolicyNamespaceList added; |
| 55 PolicyNamespaceList removed; |
| 56 schema_map1->GetChanges(schema_map2, &removed, &added); |
| 57 return added.empty() && removed.empty(); |
| 58 } |
| 59 |
51 } // namespace | 60 } // namespace |
52 | 61 |
53 TEST(SchemaRegistryTest, Notifications) { | 62 TEST(SchemaRegistryTest, Notifications) { |
54 std::string error; | 63 std::string error; |
55 Schema schema = Schema::Parse(kTestSchema, &error); | 64 Schema schema = Schema::Parse(kTestSchema, &error); |
56 ASSERT_TRUE(schema.valid()) << error; | 65 ASSERT_TRUE(schema.valid()) << error; |
57 | 66 |
58 MockSchemaRegistryObserver observer; | 67 MockSchemaRegistryObserver observer; |
59 SchemaRegistry registry; | 68 SchemaRegistry registry; |
60 EXPECT_FALSE(registry.HasObservers()); | |
61 registry.AddObserver(&observer); | 69 registry.AddObserver(&observer); |
62 EXPECT_TRUE(registry.HasObservers()); | |
63 | 70 |
64 ASSERT_TRUE(registry.schema_map()); | 71 ASSERT_TRUE(registry.schema_map()); |
65 EXPECT_FALSE(registry.schema_map()->GetSchema( | 72 EXPECT_FALSE(registry.schema_map()->GetSchema( |
66 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); | 73 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); |
67 | 74 |
68 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); | 75 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
69 registry.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), | 76 registry.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), |
70 schema); | 77 schema); |
71 Mock::VerifyAndClearExpectations(&observer); | 78 Mock::VerifyAndClearExpectations(&observer); |
72 | 79 |
(...skipping 18 matching lines...) Expand all Loading... |
91 // Registering multiple components at once issues only one notification. | 98 // Registering multiple components at once issues only one notification. |
92 ComponentMap components; | 99 ComponentMap components; |
93 components["abc"] = schema; | 100 components["abc"] = schema; |
94 components["def"] = schema; | 101 components["def"] = schema; |
95 components["xyz"] = schema; | 102 components["xyz"] = schema; |
96 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); | 103 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
97 registry.RegisterComponents(POLICY_DOMAIN_EXTENSIONS, components); | 104 registry.RegisterComponents(POLICY_DOMAIN_EXTENSIONS, components); |
98 Mock::VerifyAndClearExpectations(&observer); | 105 Mock::VerifyAndClearExpectations(&observer); |
99 | 106 |
100 registry.RemoveObserver(&observer); | 107 registry.RemoveObserver(&observer); |
101 EXPECT_FALSE(registry.HasObservers()); | |
102 } | 108 } |
103 | 109 |
104 TEST(SchemaRegistryTest, IsReady) { | 110 TEST(SchemaRegistryTest, IsReady) { |
105 SchemaRegistry registry; | 111 SchemaRegistry registry; |
106 MockSchemaRegistryObserver observer; | 112 MockSchemaRegistryObserver observer; |
107 registry.AddObserver(&observer); | 113 registry.AddObserver(&observer); |
108 | 114 |
109 EXPECT_FALSE(registry.IsReady()); | 115 EXPECT_FALSE(registry.IsReady()); |
110 #if defined(ENABLE_EXTENSIONS) | 116 #if defined(ENABLE_EXTENSIONS) |
111 EXPECT_CALL(observer, OnSchemaRegistryReady()).Times(0); | 117 EXPECT_CALL(observer, OnSchemaRegistryReady()).Times(0); |
(...skipping 15 matching lines...) Expand all Loading... |
127 | 133 |
128 registry.RemoveObserver(&observer); | 134 registry.RemoveObserver(&observer); |
129 } | 135 } |
130 | 136 |
131 TEST(SchemaRegistryTest, Combined) { | 137 TEST(SchemaRegistryTest, Combined) { |
132 std::string error; | 138 std::string error; |
133 Schema schema = Schema::Parse(kTestSchema, &error); | 139 Schema schema = Schema::Parse(kTestSchema, &error); |
134 ASSERT_TRUE(schema.valid()) << error; | 140 ASSERT_TRUE(schema.valid()) << error; |
135 | 141 |
136 MockSchemaRegistryObserver observer; | 142 MockSchemaRegistryObserver observer; |
137 SchemaRegistry registry1; | 143 scoped_ptr<SchemaRegistry> registry1(new SchemaRegistry); |
138 SchemaRegistry registry2; | 144 scoped_ptr<SchemaRegistry> registry2(new SchemaRegistry); |
139 CombinedSchemaRegistry combined; | 145 CombinedSchemaRegistry combined; |
140 combined.AddObserver(&observer); | 146 combined.AddObserver(&observer); |
141 | 147 |
142 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0); | 148 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0); |
143 registry1.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), | 149 registry1->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), |
144 schema); | 150 schema); |
145 Mock::VerifyAndClearExpectations(&observer); | 151 Mock::VerifyAndClearExpectations(&observer); |
146 | 152 |
147 // Starting to track a registry issues notifications when it comes with new | 153 // Starting to track a registry issues notifications when it comes with new |
148 // schemas. | 154 // schemas. |
149 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); | 155 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
150 combined.Track(®istry1); | 156 combined.Track(registry1.get()); |
151 Mock::VerifyAndClearExpectations(&observer); | 157 Mock::VerifyAndClearExpectations(&observer); |
152 | 158 |
153 // Adding a new empty registry does not trigger notifications. | 159 // Adding a new empty registry does not trigger notifications. |
154 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0); | 160 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0); |
155 combined.Track(®istry2); | 161 combined.Track(registry2.get()); |
156 Mock::VerifyAndClearExpectations(&observer); | 162 Mock::VerifyAndClearExpectations(&observer); |
157 | 163 |
158 // Adding the same component to the combined registry itself triggers | 164 // Adding the same component to the combined registry itself triggers |
159 // notifications. | 165 // notifications. |
160 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); | 166 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
161 combined.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), | 167 combined.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), |
162 schema); | 168 schema); |
163 Mock::VerifyAndClearExpectations(&observer); | 169 Mock::VerifyAndClearExpectations(&observer); |
164 | 170 |
165 // Adding components to the sub-registries triggers notifications. | 171 // Adding components to the sub-registries triggers notifications. |
166 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); | 172 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
167 registry2.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"), | 173 registry2->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"), |
168 schema); | 174 schema); |
169 Mock::VerifyAndClearExpectations(&observer); | 175 Mock::VerifyAndClearExpectations(&observer); |
170 | 176 |
171 // If the same component is published in 2 sub-registries then the combined | 177 // If the same component is published in 2 sub-registries then the combined |
172 // registry publishes one of them. | 178 // registry publishes one of them. |
173 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); | 179 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
174 registry1.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"), | 180 registry1->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"), |
175 schema); | 181 schema); |
176 Mock::VerifyAndClearExpectations(&observer); | 182 Mock::VerifyAndClearExpectations(&observer); |
177 | 183 |
178 ASSERT_EQ(1u, combined.schema_map()->GetDomains().size()); | 184 ASSERT_EQ(1u, combined.schema_map()->GetDomains().size()); |
179 ASSERT_TRUE(combined.schema_map()->GetComponents(POLICY_DOMAIN_EXTENSIONS)); | 185 ASSERT_TRUE(combined.schema_map()->GetComponents(POLICY_DOMAIN_EXTENSIONS)); |
180 ASSERT_EQ( | 186 ASSERT_EQ( |
181 2u, | 187 2u, |
182 combined.schema_map()->GetComponents(POLICY_DOMAIN_EXTENSIONS)->size()); | 188 combined.schema_map()->GetComponents(POLICY_DOMAIN_EXTENSIONS)->size()); |
183 EXPECT_TRUE(combined.schema_map()->GetSchema( | 189 EXPECT_TRUE(combined.schema_map()->GetSchema( |
184 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); | 190 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); |
185 EXPECT_TRUE(combined.schema_map()->GetSchema( | 191 EXPECT_TRUE(combined.schema_map()->GetSchema( |
186 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); | 192 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); |
187 EXPECT_FALSE(combined.schema_map()->GetSchema( | 193 EXPECT_FALSE(combined.schema_map()->GetSchema( |
188 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "xyz"))); | 194 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "xyz"))); |
189 | 195 |
190 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); | 196 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
191 registry1.UnregisterComponent( | 197 registry1->UnregisterComponent( |
192 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")); | 198 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")); |
193 Mock::VerifyAndClearExpectations(&observer); | 199 Mock::VerifyAndClearExpectations(&observer); |
194 // Still registered at the combined registry. | 200 // Still registered at the combined registry. |
195 EXPECT_TRUE(combined.schema_map()->GetSchema( | 201 EXPECT_TRUE(combined.schema_map()->GetSchema( |
196 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); | 202 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); |
197 | 203 |
198 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); | 204 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
199 combined.UnregisterComponent( | 205 combined.UnregisterComponent( |
200 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")); | 206 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")); |
201 Mock::VerifyAndClearExpectations(&observer); | 207 Mock::VerifyAndClearExpectations(&observer); |
202 // Now it's gone. | 208 // Now it's gone. |
203 EXPECT_FALSE(combined.schema_map()->GetSchema( | 209 EXPECT_FALSE(combined.schema_map()->GetSchema( |
204 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); | 210 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); |
205 | 211 |
206 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); | 212 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
207 registry1.UnregisterComponent( | 213 registry1->UnregisterComponent( |
208 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def")); | 214 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def")); |
209 Mock::VerifyAndClearExpectations(&observer); | 215 Mock::VerifyAndClearExpectations(&observer); |
210 // Still registered at registry2. | 216 // Still registered at registry2. |
211 EXPECT_TRUE(combined.schema_map()->GetSchema( | 217 EXPECT_TRUE(combined.schema_map()->GetSchema( |
212 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); | 218 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); |
213 | 219 |
214 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); | 220 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
215 registry2.UnregisterComponent( | 221 registry2->UnregisterComponent( |
216 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def")); | 222 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def")); |
217 Mock::VerifyAndClearExpectations(&observer); | 223 Mock::VerifyAndClearExpectations(&observer); |
218 // Now it's gone. | 224 // Now it's gone. |
219 EXPECT_FALSE(combined.schema_map()->GetSchema( | 225 EXPECT_FALSE(combined.schema_map()->GetSchema( |
220 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); | 226 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); |
221 | 227 |
222 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)).Times(2); | 228 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)).Times(2); |
223 registry1.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_CHROME, ""), | 229 registry1->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_CHROME, ""), |
224 schema); | 230 schema); |
225 registry2.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "hij"), | 231 registry2->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "hij"), |
226 schema); | 232 schema); |
227 Mock::VerifyAndClearExpectations(&observer); | 233 Mock::VerifyAndClearExpectations(&observer); |
228 | 234 |
229 // Untracking |registry1| doesn't trigger an update notification, because it | 235 // Untracking |registry1| doesn't trigger an update notification, because it |
230 // doesn't contain any components. | 236 // doesn't contain any components. |
231 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0); | 237 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0); |
232 combined.Untrack(®istry1); | 238 registry1.reset(); |
233 Mock::VerifyAndClearExpectations(&observer); | 239 Mock::VerifyAndClearExpectations(&observer); |
234 | 240 |
235 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); | 241 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
236 combined.Untrack(®istry2); | 242 registry2.reset(); |
237 Mock::VerifyAndClearExpectations(&observer); | 243 Mock::VerifyAndClearExpectations(&observer); |
238 | 244 |
239 combined.RemoveObserver(&observer); | 245 combined.RemoveObserver(&observer); |
240 } | 246 } |
241 | 247 |
| 248 TEST(SchemaRegistryTest, ForwardingSchemaRegistry) { |
| 249 scoped_ptr<SchemaRegistry> registry(new SchemaRegistry); |
| 250 ForwardingSchemaRegistry forwarding(registry.get()); |
| 251 MockSchemaRegistryObserver observer; |
| 252 forwarding.AddObserver(&observer); |
| 253 |
| 254 EXPECT_FALSE(registry->IsReady()); |
| 255 // The ForwardingSchemaRegistry is always ready, even if the wrapped registry |
| 256 // isn't. |
| 257 EXPECT_TRUE(forwarding.IsReady()); |
| 258 // But they alreday have the same SchemaMap. |
| 259 EXPECT_TRUE(SchemaMapEquals(registry->schema_map(), forwarding.schema_map())); |
| 260 |
| 261 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
| 262 registry->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), |
| 263 Schema()); |
| 264 Mock::VerifyAndClearExpectations(&observer); |
| 265 EXPECT_TRUE(SchemaMapEquals(registry->schema_map(), forwarding.schema_map())); |
| 266 |
| 267 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
| 268 registry->UnregisterComponent( |
| 269 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")); |
| 270 Mock::VerifyAndClearExpectations(&observer); |
| 271 EXPECT_TRUE(SchemaMapEquals(registry->schema_map(), forwarding.schema_map())); |
| 272 |
| 273 // No notifications expected for this call. |
| 274 EXPECT_FALSE(registry->IsReady()); |
| 275 registry->SetReady(POLICY_DOMAIN_CHROME); |
| 276 registry->SetReady(POLICY_DOMAIN_EXTENSIONS); |
| 277 EXPECT_TRUE(registry->IsReady()); |
| 278 EXPECT_TRUE(SchemaMapEquals(registry->schema_map(), forwarding.schema_map())); |
| 279 Mock::VerifyAndClearExpectations(&observer); |
| 280 |
| 281 // Keep the same SchemaMap when the original registry is gone. |
| 282 // No notifications are expected in this case either. |
| 283 scoped_refptr<SchemaMap> schema_map = registry->schema_map(); |
| 284 registry.reset(); |
| 285 EXPECT_TRUE(SchemaMapEquals(schema_map, forwarding.schema_map())); |
| 286 Mock::VerifyAndClearExpectations(&observer); |
| 287 |
| 288 forwarding.RemoveObserver(&observer); |
| 289 } |
| 290 |
242 } // namespace policy | 291 } // namespace policy |
OLD | NEW |