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