| 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 "chrome/browser/policy/schema_registry.h" | 5 #include "chrome/browser/policy/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 26 matching lines...) Expand all Loading... |
| 37 " }" | 37 " }" |
| 38 " }" | 38 " }" |
| 39 " }" | 39 " }" |
| 40 "}"; | 40 "}"; |
| 41 | 41 |
| 42 class MockSchemaRegistryObserver : public SchemaRegistry::Observer { | 42 class MockSchemaRegistryObserver : public SchemaRegistry::Observer { |
| 43 public: | 43 public: |
| 44 MockSchemaRegistryObserver() {} | 44 MockSchemaRegistryObserver() {} |
| 45 virtual ~MockSchemaRegistryObserver() {} | 45 virtual ~MockSchemaRegistryObserver() {} |
| 46 | 46 |
| 47 MOCK_METHOD2(OnSchemaRegistryUpdated, | 47 MOCK_METHOD1(OnSchemaRegistryUpdated, void(bool)); |
| 48 void(const scoped_refptr<SchemaMap>&, bool)); | |
| 49 }; | 48 }; |
| 50 | 49 |
| 51 } // namespace | 50 } // namespace |
| 52 | 51 |
| 53 TEST(SchemaRegistryTest, Notifications) { | 52 TEST(SchemaRegistryTest, Notifications) { |
| 54 std::string error; | 53 std::string error; |
| 55 Schema schema = Schema::Parse(kTestSchema, &error); | 54 Schema schema = Schema::Parse(kTestSchema, &error); |
| 56 ASSERT_TRUE(schema.valid()) << error; | 55 ASSERT_TRUE(schema.valid()) << error; |
| 57 | 56 |
| 58 MockSchemaRegistryObserver observer; | 57 MockSchemaRegistryObserver observer; |
| 59 SchemaRegistry registry; | 58 SchemaRegistry registry; |
| 60 EXPECT_FALSE(registry.HasObservers()); | 59 EXPECT_FALSE(registry.HasObservers()); |
| 61 registry.AddObserver(&observer); | 60 registry.AddObserver(&observer); |
| 62 EXPECT_TRUE(registry.HasObservers()); | 61 EXPECT_TRUE(registry.HasObservers()); |
| 63 | 62 |
| 64 ASSERT_TRUE(registry.schema_map()); | 63 ASSERT_TRUE(registry.schema_map()); |
| 65 EXPECT_FALSE(registry.schema_map()->GetSchema( | 64 EXPECT_FALSE(registry.schema_map()->GetSchema( |
| 66 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); | 65 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); |
| 67 | 66 |
| 68 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, true)); | 67 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
| 69 registry.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), | 68 registry.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), |
| 70 schema); | 69 schema); |
| 71 Mock::VerifyAndClearExpectations(&observer); | 70 Mock::VerifyAndClearExpectations(&observer); |
| 72 | 71 |
| 73 // Re-register also triggers notifications, because the Schema might have | 72 // Re-register also triggers notifications, because the Schema might have |
| 74 // changed. | 73 // changed. |
| 75 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, true)); | 74 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
| 76 registry.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), | 75 registry.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), |
| 77 schema); | 76 schema); |
| 78 Mock::VerifyAndClearExpectations(&observer); | 77 Mock::VerifyAndClearExpectations(&observer); |
| 79 | 78 |
| 80 EXPECT_TRUE(registry.schema_map()->GetSchema( | 79 EXPECT_TRUE(registry.schema_map()->GetSchema( |
| 81 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); | 80 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); |
| 82 | 81 |
| 83 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, false)); | 82 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
| 84 registry.UnregisterComponent( | 83 registry.UnregisterComponent( |
| 85 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")); | 84 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")); |
| 86 Mock::VerifyAndClearExpectations(&observer); | 85 Mock::VerifyAndClearExpectations(&observer); |
| 87 | 86 |
| 88 EXPECT_FALSE(registry.schema_map()->GetSchema( | 87 EXPECT_FALSE(registry.schema_map()->GetSchema( |
| 89 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); | 88 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); |
| 90 | 89 |
| 91 // Registering multiple components at once issues only one notification. | 90 // Registering multiple components at once issues only one notification. |
| 92 ComponentMap components; | 91 ComponentMap components; |
| 93 components["abc"] = schema; | 92 components["abc"] = schema; |
| 94 components["def"] = schema; | 93 components["def"] = schema; |
| 95 components["xyz"] = schema; | 94 components["xyz"] = schema; |
| 96 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, true)); | 95 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
| 97 registry.RegisterComponents(POLICY_DOMAIN_EXTENSIONS, components); | 96 registry.RegisterComponents(POLICY_DOMAIN_EXTENSIONS, components); |
| 98 Mock::VerifyAndClearExpectations(&observer); | 97 Mock::VerifyAndClearExpectations(&observer); |
| 99 | 98 |
| 100 registry.RemoveObserver(&observer); | 99 registry.RemoveObserver(&observer); |
| 101 EXPECT_FALSE(registry.HasObservers()); | 100 EXPECT_FALSE(registry.HasObservers()); |
| 102 } | 101 } |
| 103 | 102 |
| 104 TEST(SchemaRegistryTest, Combined) { | 103 TEST(SchemaRegistryTest, Combined) { |
| 105 std::string error; | 104 std::string error; |
| 106 Schema schema = Schema::Parse(kTestSchema, &error); | 105 Schema schema = Schema::Parse(kTestSchema, &error); |
| 107 ASSERT_TRUE(schema.valid()) << error; | 106 ASSERT_TRUE(schema.valid()) << error; |
| 108 | 107 |
| 109 MockSchemaRegistryObserver observer; | 108 MockSchemaRegistryObserver observer; |
| 110 SchemaRegistry registry1; | 109 SchemaRegistry registry1; |
| 111 SchemaRegistry registry2; | 110 SchemaRegistry registry2; |
| 112 CombinedSchemaRegistry combined; | 111 CombinedSchemaRegistry combined; |
| 113 combined.AddObserver(&observer); | 112 combined.AddObserver(&observer); |
| 114 | 113 |
| 115 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, _)).Times(0); | 114 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0); |
| 116 registry1.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), | 115 registry1.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), |
| 117 schema); | 116 schema); |
| 118 Mock::VerifyAndClearExpectations(&observer); | 117 Mock::VerifyAndClearExpectations(&observer); |
| 119 | 118 |
| 120 // Starting to track a registry issues notifications when it comes with new | 119 // Starting to track a registry issues notifications when it comes with new |
| 121 // schemas. | 120 // schemas. |
| 122 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, true)); | 121 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
| 123 combined.Track(®istry1); | 122 combined.Track(®istry1); |
| 124 Mock::VerifyAndClearExpectations(&observer); | 123 Mock::VerifyAndClearExpectations(&observer); |
| 125 | 124 |
| 126 // Adding a new empty registry does not trigger notifications. | 125 // Adding a new empty registry does not trigger notifications. |
| 127 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, _)).Times(0); | 126 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0); |
| 128 combined.Track(®istry2); | 127 combined.Track(®istry2); |
| 129 Mock::VerifyAndClearExpectations(&observer); | 128 Mock::VerifyAndClearExpectations(&observer); |
| 130 | 129 |
| 131 // Adding the same component to the combined registry itself triggers | 130 // Adding the same component to the combined registry itself triggers |
| 132 // notifications. | 131 // notifications. |
| 133 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, true)); | 132 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
| 134 combined.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), | 133 combined.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), |
| 135 schema); | 134 schema); |
| 136 Mock::VerifyAndClearExpectations(&observer); | 135 Mock::VerifyAndClearExpectations(&observer); |
| 137 | 136 |
| 138 // Adding components to the sub-registries triggers notifications. | 137 // Adding components to the sub-registries triggers notifications. |
| 139 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, true)); | 138 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
| 140 registry2.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"), | 139 registry2.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"), |
| 141 schema); | 140 schema); |
| 142 Mock::VerifyAndClearExpectations(&observer); | 141 Mock::VerifyAndClearExpectations(&observer); |
| 143 | 142 |
| 144 // If the same component is published in 2 sub-registries then the combined | 143 // If the same component is published in 2 sub-registries then the combined |
| 145 // registry publishes one of them. | 144 // registry publishes one of them. |
| 146 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, true)); | 145 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)); |
| 147 registry1.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"), | 146 registry1.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"), |
| 148 schema); | 147 schema); |
| 149 Mock::VerifyAndClearExpectations(&observer); | 148 Mock::VerifyAndClearExpectations(&observer); |
| 150 | 149 |
| 151 ASSERT_EQ(1u, combined.schema_map()->GetDomains().size()); | 150 ASSERT_EQ(1u, combined.schema_map()->GetDomains().size()); |
| 152 ASSERT_TRUE(combined.schema_map()->GetComponents(POLICY_DOMAIN_EXTENSIONS)); | 151 ASSERT_TRUE(combined.schema_map()->GetComponents(POLICY_DOMAIN_EXTENSIONS)); |
| 153 ASSERT_EQ( | 152 ASSERT_EQ( |
| 154 2u, | 153 2u, |
| 155 combined.schema_map()->GetComponents(POLICY_DOMAIN_EXTENSIONS)->size()); | 154 combined.schema_map()->GetComponents(POLICY_DOMAIN_EXTENSIONS)->size()); |
| 156 EXPECT_TRUE(combined.schema_map()->GetSchema( | 155 EXPECT_TRUE(combined.schema_map()->GetSchema( |
| 157 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); | 156 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); |
| 158 EXPECT_TRUE(combined.schema_map()->GetSchema( | 157 EXPECT_TRUE(combined.schema_map()->GetSchema( |
| 159 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); | 158 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); |
| 160 EXPECT_FALSE(combined.schema_map()->GetSchema( | 159 EXPECT_FALSE(combined.schema_map()->GetSchema( |
| 161 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "xyz"))); | 160 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "xyz"))); |
| 162 | 161 |
| 163 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, false)); | 162 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
| 164 registry1.UnregisterComponent( | 163 registry1.UnregisterComponent( |
| 165 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")); | 164 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")); |
| 166 Mock::VerifyAndClearExpectations(&observer); | 165 Mock::VerifyAndClearExpectations(&observer); |
| 167 // Still registered at the combined registry. | 166 // Still registered at the combined registry. |
| 168 EXPECT_TRUE(combined.schema_map()->GetSchema( | 167 EXPECT_TRUE(combined.schema_map()->GetSchema( |
| 169 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); | 168 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); |
| 170 | 169 |
| 171 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, false)); | 170 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
| 172 combined.UnregisterComponent( | 171 combined.UnregisterComponent( |
| 173 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")); | 172 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")); |
| 174 Mock::VerifyAndClearExpectations(&observer); | 173 Mock::VerifyAndClearExpectations(&observer); |
| 175 // Now it's gone. | 174 // Now it's gone. |
| 176 EXPECT_FALSE(combined.schema_map()->GetSchema( | 175 EXPECT_FALSE(combined.schema_map()->GetSchema( |
| 177 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); | 176 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"))); |
| 178 | 177 |
| 179 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, false)); | 178 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
| 180 registry1.UnregisterComponent( | 179 registry1.UnregisterComponent( |
| 181 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def")); | 180 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def")); |
| 182 Mock::VerifyAndClearExpectations(&observer); | 181 Mock::VerifyAndClearExpectations(&observer); |
| 183 // Still registered at registry2. | 182 // Still registered at registry2. |
| 184 EXPECT_TRUE(combined.schema_map()->GetSchema( | 183 EXPECT_TRUE(combined.schema_map()->GetSchema( |
| 185 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); | 184 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); |
| 186 | 185 |
| 187 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, false)); | 186 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
| 188 registry2.UnregisterComponent( | 187 registry2.UnregisterComponent( |
| 189 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def")); | 188 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def")); |
| 190 Mock::VerifyAndClearExpectations(&observer); | 189 Mock::VerifyAndClearExpectations(&observer); |
| 191 // Now it's gone. | 190 // Now it's gone. |
| 192 EXPECT_FALSE(combined.schema_map()->GetSchema( | 191 EXPECT_FALSE(combined.schema_map()->GetSchema( |
| 193 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); | 192 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"))); |
| 194 | 193 |
| 195 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, true)).Times(2); | 194 EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)).Times(2); |
| 196 registry1.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_CHROME, ""), | 195 registry1.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_CHROME, ""), |
| 197 schema); | 196 schema); |
| 198 registry2.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "hij"), | 197 registry2.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "hij"), |
| 199 schema); | 198 schema); |
| 200 Mock::VerifyAndClearExpectations(&observer); | 199 Mock::VerifyAndClearExpectations(&observer); |
| 201 | 200 |
| 202 // Untracking |registry1| doesn't trigger an update nofitication, because it | 201 // Untracking |registry1| doesn't trigger an update nofitication, because it |
| 203 // doesn't contain any components. | 202 // doesn't contain any components. |
| 204 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, _)).Times(0); | 203 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0); |
| 205 combined.Untrack(®istry1); | 204 combined.Untrack(®istry1); |
| 206 Mock::VerifyAndClearExpectations(&observer); | 205 Mock::VerifyAndClearExpectations(&observer); |
| 207 | 206 |
| 208 EXPECT_CALL(observer, OnSchemaRegistryUpdated(_, false)); | 207 EXPECT_CALL(observer, OnSchemaRegistryUpdated(false)); |
| 209 combined.Untrack(®istry2); | 208 combined.Untrack(®istry2); |
| 210 Mock::VerifyAndClearExpectations(&observer); | 209 Mock::VerifyAndClearExpectations(&observer); |
| 211 | 210 |
| 212 combined.RemoveObserver(&observer); | 211 combined.RemoveObserver(&observer); |
| 213 } | 212 } |
| 214 | 213 |
| 215 } // namespace policy | 214 } // namespace policy |
| OLD | NEW |