OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/custom_handlers/protocol_handler_registry.h" | 5 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" |
6 | 6 |
7 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/custom_handlers/protocol_handler.h" | 9 #include "chrome/browser/custom_handlers/protocol_handler.h" |
10 #include "chrome/test/testing_browser_process.h" | 10 #include "chrome/test/testing_browser_process.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 } | 33 } |
34 | 34 |
35 void Reset() { | 35 void Reset() { |
36 registered_protocols_.clear(); | 36 registered_protocols_.clear(); |
37 } | 37 } |
38 | 38 |
39 private: | 39 private: |
40 std::set<std::string> registered_protocols_; | 40 std::set<std::string> registered_protocols_; |
41 }; | 41 }; |
42 | 42 |
| 43 class NotificationCounter : public NotificationObserver { |
| 44 public: |
| 45 NotificationCounter() |
| 46 : events_(0), |
| 47 notification_registrar_() { |
| 48 notification_registrar_.Add(this, |
| 49 NotificationType::PROTOCOL_HANDLER_REGISTRY_CHANGED, |
| 50 NotificationService::AllSources()); |
| 51 } |
| 52 |
| 53 ~NotificationCounter() { |
| 54 notification_registrar_.Remove(this, |
| 55 NotificationType::PROTOCOL_HANDLER_REGISTRY_CHANGED, |
| 56 NotificationService::AllSources()); |
| 57 } |
| 58 |
| 59 int events() { return events_; } |
| 60 virtual void Observe(NotificationType type, |
| 61 const NotificationSource& source, |
| 62 const NotificationDetails& details) { |
| 63 ++events_; |
| 64 } |
| 65 |
| 66 int events_; |
| 67 NotificationRegistrar notification_registrar_; |
| 68 }; |
| 69 |
43 | 70 |
44 class ProtocolHandlerRegistryTest : public testing::Test { | 71 class ProtocolHandlerRegistryTest : public testing::Test { |
45 protected: | 72 protected: |
46 ProtocolHandlerRegistryTest() | 73 ProtocolHandlerRegistryTest() |
47 : test_protocol_handler_(CreateProtocolHandler("test", "test")) {} | 74 : test_protocol_handler_(CreateProtocolHandler("test", "test")) {} |
48 | 75 |
49 FakeDelegate* delegate() const { return delegate_; } | 76 FakeDelegate* delegate() const { return delegate_; } |
50 TestingProfile* profile() const { return profile_.get(); } | 77 TestingProfile* profile() const { return profile_.get(); } |
51 PrefService* pref_service() const { return profile_->GetPrefs(); } | 78 PrefService* pref_service() const { return profile_->GetPrefs(); } |
52 ProtocolHandlerRegistry* registry() const { return registry_.get(); } | 79 ProtocolHandlerRegistry* registry() const { return registry_.get(); } |
(...skipping 10 matching lines...) Expand all Loading... |
63 | 90 |
64 ProtocolHandler CreateProtocolHandler(const std::string& protocol, | 91 ProtocolHandler CreateProtocolHandler(const std::string& protocol, |
65 const std::string& name) { | 92 const std::string& name) { |
66 return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s"), | 93 return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s"), |
67 name); | 94 name); |
68 } | 95 } |
69 | 96 |
70 void ReloadProtocolHandlerRegistry() { | 97 void ReloadProtocolHandlerRegistry() { |
71 delegate_ = new FakeDelegate(); | 98 delegate_ = new FakeDelegate(); |
72 registry_ = new ProtocolHandlerRegistry(profile(), delegate()); | 99 registry_ = new ProtocolHandlerRegistry(profile(), delegate()); |
| 100 registry_->set_in_unit_test(true); |
73 registry_->Load(); | 101 registry_->Load(); |
74 } | 102 } |
75 | 103 |
76 virtual void SetUp() { | 104 virtual void SetUp() { |
77 profile_.reset(new TestingProfile()); | 105 profile_.reset(new TestingProfile()); |
78 profile_->SetPrefService(new TestingPrefService()); | 106 profile_->SetPrefService(new TestingPrefService()); |
79 delegate_ = new FakeDelegate(); | 107 delegate_ = new FakeDelegate(); |
80 registry_ = new ProtocolHandlerRegistry(profile(), delegate()); | 108 registry_ = new ProtocolHandlerRegistry(profile(), delegate()); |
| 109 registry_->set_in_unit_test(true); |
81 registry_->Load(); | 110 registry_->Load(); |
82 test_protocol_handler_ = CreateProtocolHandler("test", GURL("http://test.com
/%s"), "Test"); | 111 test_protocol_handler_ = CreateProtocolHandler("test", GURL("http://test.com
/%s"), "Test"); |
83 | 112 |
84 ProtocolHandlerRegistry::RegisterPrefs(pref_service()); | 113 ProtocolHandlerRegistry::RegisterPrefs(pref_service()); |
85 } | 114 } |
86 | 115 |
87 FakeDelegate* delegate_; | 116 FakeDelegate* delegate_; |
88 scoped_ptr<TestingProfile> profile_; | 117 scoped_ptr<TestingProfile> profile_; |
89 scoped_refptr<ProtocolHandlerRegistry> registry_; | 118 scoped_refptr<ProtocolHandlerRegistry> registry_; |
90 ProtocolHandler test_protocol_handler_; | 119 ProtocolHandler test_protocol_handler_; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 registry()->Enable(); | 163 registry()->Enable(); |
135 ASSERT_TRUE(registry()->enabled()); | 164 ASSERT_TRUE(registry()->enabled()); |
136 } | 165 } |
137 | 166 |
138 TEST_F(ProtocolHandlerRegistryTest, | 167 TEST_F(ProtocolHandlerRegistryTest, |
139 DisallowRegisteringExternallyHandledProtocols) { | 168 DisallowRegisteringExternallyHandledProtocols) { |
140 delegate()->RegisterExternalHandler("test"); | 169 delegate()->RegisterExternalHandler("test"); |
141 ASSERT_FALSE(registry()->CanSchemeBeOverridden("test")); | 170 ASSERT_FALSE(registry()->CanSchemeBeOverridden("test")); |
142 } | 171 } |
143 | 172 |
| 173 TEST_F(ProtocolHandlerRegistryTest, RemovingHandlerMeansItCanBeAddedAgain) { |
| 174 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); |
| 175 ASSERT_TRUE(registry()->CanSchemeBeOverridden("test")); |
| 176 registry()->RemoveHandler(test_protocol_handler()); |
| 177 ASSERT_TRUE(registry()->CanSchemeBeOverridden("test")); |
| 178 } |
| 179 |
144 TEST_F(ProtocolHandlerRegistryTest, TestStartsAsDefault) { | 180 TEST_F(ProtocolHandlerRegistryTest, TestStartsAsDefault) { |
145 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); | 181 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); |
146 ASSERT_TRUE(registry()->IsDefault(test_protocol_handler())); | 182 ASSERT_TRUE(registry()->IsDefault(test_protocol_handler())); |
147 } | 183 } |
148 | 184 |
149 TEST_F(ProtocolHandlerRegistryTest, | 185 TEST_F(ProtocolHandlerRegistryTest, |
150 TestClearDefaultDoesntClearImplicitDefault) { | 186 TestClearDefaultDoesntClearImplicitDefault) { |
151 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); | 187 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); |
152 registry()->ClearDefault("test"); | 188 registry()->ClearDefault("test"); |
153 ASSERT_TRUE(registry()->IsDefault(test_protocol_handler())); | 189 ASSERT_TRUE(registry()->IsDefault(test_protocol_handler())); |
154 } | 190 } |
155 | 191 |
156 TEST_F(ProtocolHandlerRegistryTest, TestClearDefault) { | 192 TEST_F(ProtocolHandlerRegistryTest, TestClearDefault) { |
157 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); | 193 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
158 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); | 194 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
159 registry()->OnAcceptRegisterProtocolHandler(ph1); | 195 registry()->OnAcceptRegisterProtocolHandler(ph1); |
160 registry()->OnAcceptRegisterProtocolHandler(ph2); | 196 registry()->OnAcceptRegisterProtocolHandler(ph2); |
161 | 197 |
162 registry()->SetDefault(ph2); | 198 registry()->SetDefault(ph1); |
163 registry()->ClearDefault("test"); | 199 registry()->ClearDefault("test"); |
164 ASSERT_FALSE(registry()->IsDefault(ph2)); | 200 ASSERT_FALSE(registry()->IsDefault(ph1)); |
| 201 ASSERT_TRUE(registry()->IsDefault(ph2)); |
165 } | 202 } |
166 | 203 |
167 TEST_F(ProtocolHandlerRegistryTest, TestGetHandlerFor) { | 204 TEST_F(ProtocolHandlerRegistryTest, TestGetHandlerFor) { |
168 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); | 205 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
169 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); | 206 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
170 registry()->OnAcceptRegisterProtocolHandler(ph1); | 207 registry()->OnAcceptRegisterProtocolHandler(ph1); |
171 registry()->OnAcceptRegisterProtocolHandler(ph2); | 208 registry()->OnAcceptRegisterProtocolHandler(ph2); |
172 | 209 |
173 registry()->SetDefault(ph2); | 210 registry()->SetDefault(ph2); |
174 ASSERT_EQ(ph2, registry()->GetHandlerFor("test")); | 211 ASSERT_EQ(ph2, registry()->GetHandlerFor("test")); |
175 ASSERT_TRUE(registry()->HasDefault("test")); | 212 ASSERT_TRUE(registry()->HasDefault("test")); |
176 } | 213 } |
177 | 214 |
178 TEST_F(ProtocolHandlerRegistryTest, TestMultipleHandlersClearsDefault) { | 215 TEST_F(ProtocolHandlerRegistryTest, TestMostRecentHandlerIsDefault) { |
179 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); | 216 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
180 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); | 217 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
181 registry()->OnAcceptRegisterProtocolHandler(ph1); | 218 registry()->OnAcceptRegisterProtocolHandler(ph1); |
182 registry()->OnAcceptRegisterProtocolHandler(ph2); | 219 registry()->OnAcceptRegisterProtocolHandler(ph2); |
183 ASSERT_FALSE(registry()->IsDefault(ph1)); | 220 ASSERT_FALSE(registry()->IsDefault(ph1)); |
184 ASSERT_FALSE(registry()->IsDefault(ph2)); | 221 ASSERT_TRUE(registry()->IsDefault(ph2)); |
185 } | 222 } |
186 | 223 |
187 TEST_F(ProtocolHandlerRegistryTest, TestSetDefault) { | 224 TEST_F(ProtocolHandlerRegistryTest, TestSetDefault) { |
188 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); | 225 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
189 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); | 226 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
190 registry()->OnAcceptRegisterProtocolHandler(ph1); | 227 registry()->OnAcceptRegisterProtocolHandler(ph1); |
191 registry()->OnAcceptRegisterProtocolHandler(ph2); | 228 registry()->OnAcceptRegisterProtocolHandler(ph2); |
192 | 229 |
193 registry()->SetDefault(ph1); | 230 registry()->SetDefault(ph1); |
194 ASSERT_TRUE(registry()->IsDefault(ph1)); | 231 ASSERT_TRUE(registry()->IsDefault(ph1)); |
(...skipping 18 matching lines...) Expand all Loading... |
213 ASSERT_TRUE(registry()->IsDefault(ph2)); | 250 ASSERT_TRUE(registry()->IsDefault(ph2)); |
214 } | 251 } |
215 | 252 |
216 TEST_F(ProtocolHandlerRegistryTest, TestRemoveHandler) { | 253 TEST_F(ProtocolHandlerRegistryTest, TestRemoveHandler) { |
217 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); | 254 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
218 registry()->OnAcceptRegisterProtocolHandler(ph1); | 255 registry()->OnAcceptRegisterProtocolHandler(ph1); |
219 registry()->OnAcceptRegisterProtocolHandler(ph1); | 256 registry()->OnAcceptRegisterProtocolHandler(ph1); |
220 | 257 |
221 registry()->RemoveHandler(ph1); | 258 registry()->RemoveHandler(ph1); |
222 ASSERT_FALSE(registry()->IsRegistered(ph1)); | 259 ASSERT_FALSE(registry()->IsRegistered(ph1)); |
223 ASSERT_FALSE(registry()->HasHandler("test")); | 260 ASSERT_FALSE(registry()->IsHandledProtocol("test")); |
224 } | 261 } |
225 | 262 |
226 TEST_F(ProtocolHandlerRegistryTest, TestIsRegistered) { | 263 TEST_F(ProtocolHandlerRegistryTest, TestIsRegistered) { |
227 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); | 264 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
228 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); | 265 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
229 registry()->OnAcceptRegisterProtocolHandler(ph1); | 266 registry()->OnAcceptRegisterProtocolHandler(ph1); |
230 registry()->OnAcceptRegisterProtocolHandler(ph2); | 267 registry()->OnAcceptRegisterProtocolHandler(ph2); |
231 | 268 |
232 ASSERT_TRUE(registry()->IsRegistered(ph1)); | 269 ASSERT_TRUE(registry()->IsRegistered(ph1)); |
233 } | 270 } |
234 | 271 |
235 TEST_F(ProtocolHandlerRegistryTest, TestRemoveHandlerRemovesDefault) { | 272 TEST_F(ProtocolHandlerRegistryTest, TestRemoveHandlerRemovesDefault) { |
236 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); | 273 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
237 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); | 274 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
238 ProtocolHandler ph3 = CreateProtocolHandler("test", "test3"); | 275 ProtocolHandler ph3 = CreateProtocolHandler("test", "test3"); |
| 276 |
239 registry()->OnAcceptRegisterProtocolHandler(ph1); | 277 registry()->OnAcceptRegisterProtocolHandler(ph1); |
240 registry()->OnAcceptRegisterProtocolHandler(ph2); | 278 registry()->OnAcceptRegisterProtocolHandler(ph2); |
241 registry()->OnAcceptRegisterProtocolHandler(ph3); | 279 registry()->OnAcceptRegisterProtocolHandler(ph3); |
242 | 280 |
243 registry()->SetDefault(ph1); | 281 registry()->SetDefault(ph1); |
244 registry()->RemoveHandler(ph1); | 282 registry()->RemoveHandler(ph1); |
245 ASSERT_FALSE(registry()->IsDefault(ph1)); | 283 ASSERT_FALSE(registry()->IsDefault(ph1)); |
246 } | 284 } |
| 285 |
| 286 TEST_F(ProtocolHandlerRegistryTest, TestGetHandlersFor) { |
| 287 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 288 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
| 289 ProtocolHandler ph3 = CreateProtocolHandler("test", "test3"); |
| 290 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 291 registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 292 registry()->OnAcceptRegisterProtocolHandler(ph3); |
| 293 |
| 294 const ProtocolHandlerRegistry::ProtocolHandlerList* handlers = |
| 295 registry()->GetHandlersFor("test"); |
| 296 ASSERT_TRUE(handlers != NULL); |
| 297 ASSERT_EQ(ph1, (*handlers)[0]); |
| 298 ASSERT_EQ(ph2, (*handlers)[1]); |
| 299 ASSERT_EQ(ph3, (*handlers)[2]); |
| 300 } |
| 301 |
| 302 TEST_F(ProtocolHandlerRegistryTest, TestGetHandledProtocols) { |
| 303 std::vector<std::string> protocols; |
| 304 registry()->GetHandledProtocols(&protocols); |
| 305 ASSERT_EQ((size_t) 0, protocols.size()); |
| 306 |
| 307 registry()->GetHandlersFor("test"); |
| 308 |
| 309 protocols.clear(); |
| 310 registry()->GetHandledProtocols(&protocols); |
| 311 ASSERT_EQ((size_t) 0, protocols.size()); |
| 312 } |
| 313 |
| 314 TEST_F(ProtocolHandlerRegistryTest, TestIsHandledProtocol) { |
| 315 registry()->GetHandlersFor("test"); |
| 316 ASSERT_FALSE(registry()->IsHandledProtocol("test")); |
| 317 } |
| 318 |
| 319 TEST_F(ProtocolHandlerRegistryTest, TestNotifications) { |
| 320 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 321 NotificationCounter counter; |
| 322 |
| 323 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 324 ASSERT_EQ(1, counter.events()); |
| 325 |
| 326 registry()->Disable(); |
| 327 ASSERT_EQ(2, counter.events()); |
| 328 |
| 329 registry()->Enable(); |
| 330 ASSERT_EQ(3, counter.events()); |
| 331 |
| 332 registry()->RemoveHandler(ph1); |
| 333 ASSERT_EQ(4, counter.events()); |
| 334 } |
| 335 // TODO add unit tests for notifications |
OLD | NEW |