| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/synchronization/waitable_event.h" |
| 11 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
| 13 #include "chrome/common/custom_handlers/protocol_handler.h" | 14 #include "chrome/common/custom_handlers/protocol_handler.h" |
| 14 #include "chrome/test/base/testing_browser_process.h" | 15 #include "chrome/test/base/testing_browser_process.h" |
| 15 #include "chrome/test/base/testing_pref_service.h" | 16 #include "chrome/test/base/testing_pref_service.h" |
| 16 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 17 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
| 18 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
| 19 #include "content/public/browser/notification_source.h" | 20 #include "content/public/browser/notification_source.h" |
| 20 #include "content/public/test/test_browser_thread.h" | 21 #include "content/public/test/test_browser_thread.h" |
| 21 #include "content/public/test/test_renderer_host.h" | 22 #include "content/public/test/test_renderer_host.h" |
| 22 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
| 23 #include "net/url_request/url_request_context.h" | 24 #include "net/url_request/url_request_context.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 26 |
| 25 using content::BrowserThread; | 27 using content::BrowserThread; |
| 26 | 28 |
| 27 namespace { | 29 namespace { |
| 28 | 30 |
| 31 void AssertInterceptedIO( |
| 32 const GURL& url, |
| 33 net::URLRequestJobFactory::Interceptor* interceptor) { |
| 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 35 net::URLRequestContext context; |
| 36 net::URLRequest request(url, NULL, &context); |
| 37 scoped_refptr<net::URLRequestJob> job = interceptor->MaybeIntercept(&request); |
| 38 ASSERT_TRUE(job.get() != NULL); |
| 39 } |
| 40 |
| 41 void AssertIntercepted( |
| 42 const GURL& url, |
| 43 net::URLRequestJobFactory::Interceptor* interceptor) { |
| 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 45 BrowserThread::PostTask(BrowserThread::IO, |
| 46 FROM_HERE, |
| 47 base::Bind(AssertInterceptedIO, |
| 48 url, |
| 49 base::Unretained(interceptor))); |
| 50 MessageLoop::current()->RunAllPending(); |
| 51 } |
| 52 |
| 53 void AssertWillHandleIO( |
| 54 const std::string& scheme, |
| 55 bool expected, |
| 56 net::URLRequestJobFactory::Interceptor* interceptor) { |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 58 ASSERT_EQ(expected, interceptor->WillHandleProtocol(scheme)); |
| 59 } |
| 60 |
| 61 void AssertWillHandle( |
| 62 const std::string& scheme, |
| 63 bool expected, |
| 64 net::URLRequestJobFactory::Interceptor* interceptor) { |
| 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 66 BrowserThread::PostTask(BrowserThread::IO, |
| 67 FROM_HERE, |
| 68 base::Bind(AssertWillHandleIO, |
| 69 scheme, |
| 70 expected, |
| 71 base::Unretained(interceptor))); |
| 72 MessageLoop::current()->RunAllPending(); |
| 73 } |
| 74 |
| 29 class FakeDelegate : public ProtocolHandlerRegistry::Delegate { | 75 class FakeDelegate : public ProtocolHandlerRegistry::Delegate { |
| 30 public: | 76 public: |
| 31 FakeDelegate() : force_os_failure_(false) {} | 77 FakeDelegate() : force_os_failure_(false) {} |
| 32 virtual ~FakeDelegate() { } | 78 virtual ~FakeDelegate() { } |
| 33 virtual void RegisterExternalHandler(const std::string& protocol) { | 79 virtual void RegisterExternalHandler(const std::string& protocol) { |
| 34 ASSERT_TRUE( | 80 ASSERT_TRUE( |
| 35 registered_protocols_.find(protocol) == registered_protocols_.end()); | 81 registered_protocols_.find(protocol) == registered_protocols_.end()); |
| 36 registered_protocols_.insert(protocol); | 82 registered_protocols_.insert(protocol); |
| 37 } | 83 } |
| 38 | 84 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 | 213 |
| 168 int events_; | 214 int events_; |
| 169 content::NotificationRegistrar notification_registrar_; | 215 content::NotificationRegistrar notification_registrar_; |
| 170 }; | 216 }; |
| 171 | 217 |
| 172 class QueryProtocolHandlerOnChange | 218 class QueryProtocolHandlerOnChange |
| 173 : public content::NotificationObserver { | 219 : public content::NotificationObserver { |
| 174 public: | 220 public: |
| 175 QueryProtocolHandlerOnChange(Profile* profile, | 221 QueryProtocolHandlerOnChange(Profile* profile, |
| 176 ProtocolHandlerRegistry* registry) | 222 ProtocolHandlerRegistry* registry) |
| 177 : registry_(registry), | 223 : local_registry_(registry), |
| 178 called_(false), | 224 called_(false), |
| 179 notification_registrar_() { | 225 notification_registrar_() { |
| 180 notification_registrar_.Add(this, | 226 notification_registrar_.Add(this, |
| 181 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, | 227 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, |
| 182 content::Source<Profile>(profile)); | 228 content::Source<Profile>(profile)); |
| 183 } | 229 } |
| 184 | 230 |
| 185 virtual void Observe(int type, | 231 virtual void Observe(int type, |
| 186 const content::NotificationSource& source, | 232 const content::NotificationSource& source, |
| 187 const content::NotificationDetails& details) { | 233 const content::NotificationDetails& details) { |
| 188 std::vector<std::string> output; | 234 std::vector<std::string> output; |
| 189 registry_->GetRegisteredProtocols(&output); | 235 local_registry_->GetRegisteredProtocols(&output); |
| 190 called_ = true; | 236 called_ = true; |
| 191 } | 237 } |
| 192 | 238 |
| 193 ProtocolHandlerRegistry* registry_; | 239 ProtocolHandlerRegistry* local_registry_; |
| 194 bool called_; | 240 bool called_; |
| 195 content::NotificationRegistrar notification_registrar_; | 241 content::NotificationRegistrar notification_registrar_; |
| 196 }; | 242 }; |
| 197 | 243 |
| 244 // URLRequest DCHECKS that the current MessageLoop is IO. It does this because |
| 245 // it can't check the thread id (since net can't depend on content.) We want |
| 246 // to harness our tests so all threads use the same loop allowing us to |
| 247 // guarantee all messages are processed.) By overriding the IsType method |
| 248 // we basically ignore the supplied message loop type, and instead infer |
| 249 // our type based on the current thread. GO DEPENDENCY INJECTION! |
| 250 class TestMessageLoop : public MessageLoop { |
| 251 public: |
| 252 TestMessageLoop() : MessageLoop(MessageLoop::TYPE_DEFAULT) {} |
| 253 ~TestMessageLoop() {} |
| 254 virtual bool IsType(MessageLoop::Type type) const OVERRIDE { |
| 255 switch (type) { |
| 256 case MessageLoop::TYPE_UI: |
| 257 return BrowserThread::CurrentlyOn(BrowserThread::UI); |
| 258 case MessageLoop::TYPE_IO: |
| 259 return BrowserThread::CurrentlyOn(BrowserThread::IO); |
| 260 case MessageLoop::TYPE_DEFAULT: |
| 261 return !BrowserThread::CurrentlyOn(BrowserThread::UI) && |
| 262 !BrowserThread::CurrentlyOn(BrowserThread::IO); |
| 263 } |
| 264 return false; |
| 265 } |
| 266 }; |
| 267 |
| 198 } // namespace | 268 } // namespace |
| 199 | 269 |
| 200 class ProtocolHandlerRegistryTest : public testing::Test { | 270 class ProtocolHandlerRegistryTest : public testing::Test { |
| 201 protected: | 271 protected: |
| 202 ProtocolHandlerRegistryTest() | 272 ProtocolHandlerRegistryTest() |
| 203 : test_protocol_handler_(CreateProtocolHandler("test", "test")) {} | 273 : ui_thread_(BrowserThread::UI, &loop_), |
| 274 file_thread_(BrowserThread::FILE, &loop_), |
| 275 io_thread_(BrowserThread::IO, &loop_), |
| 276 test_protocol_handler_(CreateProtocolHandler("test", "test")) {} |
| 204 | 277 |
| 205 FakeDelegate* delegate() const { return delegate_; } | 278 FakeDelegate* delegate() const { return delegate_; } |
| 279 ProtocolHandlerRegistry* registry() { return registry_.get(); } |
| 206 TestingProfile* profile() const { return profile_.get(); } | 280 TestingProfile* profile() const { return profile_.get(); } |
| 207 PrefService* pref_service() const { return profile_->GetPrefs(); } | 281 PrefService* pref_service() const { return profile_->GetPrefs(); } |
| 208 ProtocolHandlerRegistry* registry() const { return registry_.get(); } | |
| 209 const ProtocolHandler& test_protocol_handler() const { | 282 const ProtocolHandler& test_protocol_handler() const { |
| 210 return test_protocol_handler_; | 283 return test_protocol_handler_; |
| 211 } | 284 } |
| 212 | 285 |
| 213 ProtocolHandler CreateProtocolHandler(const std::string& protocol, | 286 ProtocolHandler CreateProtocolHandler(const std::string& protocol, |
| 214 const GURL& url, | 287 const GURL& url, |
| 215 const std::string& title) { | 288 const std::string& title) { |
| 216 return ProtocolHandler::CreateProtocolHandler(protocol, url, | 289 return ProtocolHandler::CreateProtocolHandler(protocol, url, |
| 217 UTF8ToUTF16(title)); | 290 UTF8ToUTF16(title)); |
| 218 } | 291 } |
| 219 | 292 |
| 220 ProtocolHandler CreateProtocolHandler(const std::string& protocol, | 293 ProtocolHandler CreateProtocolHandler(const std::string& protocol, |
| 221 const std::string& name) { | 294 const std::string& name) { |
| 222 return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s"), | 295 return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s"), |
| 223 name); | 296 name); |
| 224 } | 297 } |
| 225 | 298 |
| 226 void ReloadProtocolHandlerRegistry() { | 299 void RecreateRegistry(bool initialize) { |
| 227 delegate_ = new FakeDelegate(); | 300 TeadDownRegistry(); |
| 228 registry_->Finalize(); | 301 SetUpRegistry(initialize); |
| 229 registry_ = NULL; | |
| 230 registry_ = new ProtocolHandlerRegistry(profile(), delegate()); | |
| 231 registry_->Load(); | |
| 232 } | 302 } |
| 233 | 303 |
| 234 void ReloadProtocolHandlerRegistryAndInstallDefaultHandler() { | 304 // Returns a new registry, initializing it if |initialize| is true. |
| 305 // Caller assumes ownership for the object |
| 306 void SetUpRegistry(bool initialize) { |
| 235 delegate_ = new FakeDelegate(); | 307 delegate_ = new FakeDelegate(); |
| 236 registry_->Finalize(); | 308 registry_.reset(new ProtocolHandlerRegistry(profile(), delegate())); |
| 237 registry_ = NULL; | 309 if (initialize) registry_->InitProtocolSettings(); |
| 238 registry_ = new ProtocolHandlerRegistry(profile(), delegate()); | 310 } |
| 239 registry_->AddPredefinedHandler(CreateProtocolHandler( | 311 |
| 240 "test", GURL("http://test.com/%s"), "Test")); | 312 void TeadDownRegistry() { |
| 241 registry_->Load(); | 313 registry_->Shutdown(); |
| 314 registry_.reset(); |
| 315 // Registry owns the delegate_ it handles deletion of that object. |
| 242 } | 316 } |
| 243 | 317 |
| 244 virtual void SetUp() { | 318 virtual void SetUp() { |
| 245 ui_message_loop_.reset(new MessageLoopForUI()); | |
| 246 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, | |
| 247 MessageLoop::current())); | |
| 248 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO)); | |
| 249 io_thread_->StartIOThread(); | |
| 250 | |
| 251 file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE)); | |
| 252 file_thread_->Start(); | |
| 253 | |
| 254 profile_.reset(new TestingProfile()); | 319 profile_.reset(new TestingProfile()); |
| 255 profile_->SetPrefService(new TestingPrefService()); | 320 profile_->SetPrefService(new TestingPrefService()); |
| 256 delegate_ = new FakeDelegate(); | 321 SetUpRegistry(true); |
| 257 registry_ = new ProtocolHandlerRegistry(profile(), delegate()); | |
| 258 registry_->Load(); | |
| 259 test_protocol_handler_ = | 322 test_protocol_handler_ = |
| 260 CreateProtocolHandler("test", GURL("http://test.com/%s"), "Test"); | 323 CreateProtocolHandler("test", GURL("http://test.com/%s"), "Test"); |
| 261 | |
| 262 ProtocolHandlerRegistry::RegisterPrefs(pref_service()); | 324 ProtocolHandlerRegistry::RegisterPrefs(pref_service()); |
| 263 } | 325 } |
| 264 | 326 |
| 265 virtual void TearDown() { | 327 virtual void TearDown() { |
| 266 registry_->Finalize(); | 328 TeadDownRegistry(); |
| 267 registry_ = NULL; | |
| 268 io_thread_->Stop(); | |
| 269 io_thread_.reset(NULL); | |
| 270 file_thread_->Stop(); | |
| 271 file_thread_.reset(NULL); | |
| 272 ui_thread_.reset(NULL); | |
| 273 ui_message_loop_.reset(NULL); | |
| 274 } | 329 } |
| 275 | 330 |
| 276 bool enabled_io() { | 331 TestMessageLoop loop_; |
| 277 return registry()->enabled_io_; | |
| 278 } | |
| 279 | 332 |
| 280 scoped_ptr<MessageLoopForUI> ui_message_loop_; | 333 private: |
| 281 scoped_ptr<content::TestBrowserThread> ui_thread_; | 334 content::TestBrowserThread ui_thread_; |
| 282 scoped_ptr<content::TestBrowserThread> io_thread_; | 335 content::TestBrowserThread file_thread_; |
| 283 scoped_ptr<content::TestBrowserThread> file_thread_; | 336 content::TestBrowserThread io_thread_; |
| 284 | 337 |
| 285 FakeDelegate* delegate_; | |
| 286 scoped_ptr<TestingProfile> profile_; | 338 scoped_ptr<TestingProfile> profile_; |
| 287 scoped_refptr<ProtocolHandlerRegistry> registry_; | 339 FakeDelegate* delegate_; // Registry assumes ownership of delegate_. |
| 340 scoped_ptr<ProtocolHandlerRegistry> registry_; |
| 288 ProtocolHandler test_protocol_handler_; | 341 ProtocolHandler test_protocol_handler_; |
| 289 }; | 342 }; |
| 290 | 343 |
| 291 // ProtocolHandlerRegistryTest tests are flaky on Linux & ChromeOS. | 344 // ProtocolHandlerRegistryTest tests are flaky on Linux & ChromeOS. |
| 292 // http://crbug.com/133023 | 345 // http://crbug.com/133023 |
| 293 #if defined(OS_LINUX) || defined(OS_CHROMEOS) | 346 #if defined(OS_LINUX) || defined(OS_CHROMEOS) |
| 294 #define MAYBE_AcceptProtocolHandlerHandlesProtocol \ | 347 #define MAYBE_AcceptProtocolHandlerHandlesProtocol \ |
| 295 DISABLED_AcceptProtocolHandlerHandlesProtocol | 348 DISABLED_AcceptProtocolHandlerHandlesProtocol |
| 296 #define MAYBE_DeniedProtocolIsntHandledUntilAccepted \ | 349 #define MAYBE_DeniedProtocolIsntHandledUntilAccepted \ |
| 297 DISABLED_DeniedProtocolIsntHandledUntilAccepted | 350 DISABLED_DeniedProtocolIsntHandledUntilAccepted |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 | 429 |
| 377 TEST_F(ProtocolHandlerRegistryTest, SaveAndLoad) { | 430 TEST_F(ProtocolHandlerRegistryTest, SaveAndLoad) { |
| 378 ProtocolHandler stuff_protocol_handler( | 431 ProtocolHandler stuff_protocol_handler( |
| 379 CreateProtocolHandler("stuff", "stuff")); | 432 CreateProtocolHandler("stuff", "stuff")); |
| 380 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); | 433 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); |
| 381 registry()->OnIgnoreRegisterProtocolHandler(stuff_protocol_handler); | 434 registry()->OnIgnoreRegisterProtocolHandler(stuff_protocol_handler); |
| 382 | 435 |
| 383 ASSERT_TRUE(registry()->IsHandledProtocol("test")); | 436 ASSERT_TRUE(registry()->IsHandledProtocol("test")); |
| 384 ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler)); | 437 ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler)); |
| 385 delegate()->Reset(); | 438 delegate()->Reset(); |
| 386 ReloadProtocolHandlerRegistry(); | 439 RecreateRegistry(true); |
| 387 ASSERT_TRUE(registry()->IsHandledProtocol("test")); | 440 ASSERT_TRUE(registry()->IsHandledProtocol("test")); |
| 388 ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler)); | 441 ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler)); |
| 389 } | 442 } |
| 390 | 443 |
| 391 TEST_F(ProtocolHandlerRegistryTest, TestEnabledDisabled) { | 444 TEST_F(ProtocolHandlerRegistryTest, TestEnabledDisabled) { |
| 392 registry()->Disable(); | 445 registry()->Disable(); |
| 393 ASSERT_FALSE(registry()->enabled()); | 446 ASSERT_FALSE(registry()->enabled()); |
| 394 registry()->Enable(); | 447 registry()->Enable(); |
| 395 ASSERT_TRUE(registry()->enabled()); | 448 ASSERT_TRUE(registry()->enabled()); |
| 396 } | 449 } |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 | 515 |
| 463 TEST_F(ProtocolHandlerRegistryTest, TestDefaultSaveLoad) { | 516 TEST_F(ProtocolHandlerRegistryTest, TestDefaultSaveLoad) { |
| 464 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); | 517 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 465 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); | 518 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
| 466 registry()->OnDenyRegisterProtocolHandler(ph1); | 519 registry()->OnDenyRegisterProtocolHandler(ph1); |
| 467 registry()->OnDenyRegisterProtocolHandler(ph2); | 520 registry()->OnDenyRegisterProtocolHandler(ph2); |
| 468 | 521 |
| 469 registry()->OnAcceptRegisterProtocolHandler(ph2); | 522 registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 470 registry()->Disable(); | 523 registry()->Disable(); |
| 471 | 524 |
| 472 ReloadProtocolHandlerRegistry(); | 525 RecreateRegistry(true); |
| 473 | 526 |
| 474 ASSERT_FALSE(registry()->enabled()); | 527 ASSERT_FALSE(registry()->enabled()); |
| 475 registry()->Enable(); | 528 registry()->Enable(); |
| 476 ASSERT_FALSE(registry()->IsDefault(ph1)); | 529 ASSERT_FALSE(registry()->IsDefault(ph1)); |
| 477 ASSERT_TRUE(registry()->IsDefault(ph2)); | 530 ASSERT_TRUE(registry()->IsDefault(ph2)); |
| 478 | 531 |
| 479 ReloadProtocolHandlerRegistry(); | 532 RecreateRegistry(true); |
| 480 ASSERT_TRUE(registry()->enabled()); | 533 ASSERT_TRUE(registry()->enabled()); |
| 481 } | 534 } |
| 482 | 535 |
| 483 TEST_F(ProtocolHandlerRegistryTest, TestRemoveHandler) { | 536 TEST_F(ProtocolHandlerRegistryTest, TestRemoveHandler) { |
| 484 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); | 537 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 485 registry()->OnAcceptRegisterProtocolHandler(ph1); | 538 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 486 registry()->OnAcceptRegisterProtocolHandler(ph1); | 539 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 487 | 540 |
| 488 registry()->RemoveHandler(ph1); | 541 registry()->RemoveHandler(ph1); |
| 489 ASSERT_FALSE(registry()->IsRegistered(ph1)); | 542 ASSERT_FALSE(registry()->IsRegistered(ph1)); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 629 } | 682 } |
| 630 | 683 |
| 631 TEST_F(ProtocolHandlerRegistryTest, TestDisablePreventsHandling) { | 684 TEST_F(ProtocolHandlerRegistryTest, TestDisablePreventsHandling) { |
| 632 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); | 685 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 633 registry()->OnAcceptRegisterProtocolHandler(ph1); | 686 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 634 ASSERT_TRUE(registry()->IsHandledProtocol("test")); | 687 ASSERT_TRUE(registry()->IsHandledProtocol("test")); |
| 635 registry()->Disable(); | 688 registry()->Disable(); |
| 636 ASSERT_FALSE(registry()->IsHandledProtocol("test")); | 689 ASSERT_FALSE(registry()->IsHandledProtocol("test")); |
| 637 } | 690 } |
| 638 | 691 |
| 692 // TODO(smckay): This is much more appropriately an integration |
| 693 // test. Make that so, then update the |
| 694 // ShellIntegretion{Delegate,Observer,Worker} test classes we use to fully |
| 695 // isolate this test from the FILE thread. |
| 639 TEST_F(ProtocolHandlerRegistryTest, TestOSRegistration) { | 696 TEST_F(ProtocolHandlerRegistryTest, TestOSRegistration) { |
| 640 ProtocolHandler ph_do1 = CreateProtocolHandler("do", "test1"); | 697 ProtocolHandler ph_do1 = CreateProtocolHandler("do", "test1"); |
| 641 ProtocolHandler ph_do2 = CreateProtocolHandler("do", "test2"); | 698 ProtocolHandler ph_do2 = CreateProtocolHandler("do", "test2"); |
| 642 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test"); | 699 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test"); |
| 643 | 700 |
| 644 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("do")); | 701 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("do")); |
| 645 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont")); | 702 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont")); |
| 646 | 703 |
| 647 registry()->OnAcceptRegisterProtocolHandler(ph_do1); | 704 registry()->OnAcceptRegisterProtocolHandler(ph_do1); |
| 648 registry()->OnDenyRegisterProtocolHandler(ph_dont); | 705 registry()->OnDenyRegisterProtocolHandler(ph_dont); |
| 649 MessageLoop::current()->Run(); | 706 MessageLoop::current()->Run(); // FILE thread needs to run. |
| 650 ASSERT_TRUE(delegate()->IsFakeRegisteredWithOS("do")); | 707 ASSERT_TRUE(delegate()->IsFakeRegisteredWithOS("do")); |
| 651 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont")); | 708 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont")); |
| 652 | 709 |
| 653 // This should not register with the OS, if it does the delegate | 710 // This should not register with the OS, if it does the delegate |
| 654 // will assert for us. We don't need to wait for the message loop | 711 // will assert for us. We don't need to wait for the message loop |
| 655 // as it should not go through to the shell worker. | 712 // as it should not go through to the shell worker. |
| 656 registry()->OnAcceptRegisterProtocolHandler(ph_do2); | 713 registry()->OnAcceptRegisterProtocolHandler(ph_do2); |
| 657 } | 714 } |
| 658 | 715 |
| 659 #if defined(OS_LINUX) | 716 #if defined(OS_LINUX) |
| 660 // TODO(benwells): When Linux support is more reliable and | 717 // TODO(benwells): When Linux support is more reliable and |
| 661 // http://crbut.com/88255 is fixed this test will pass. | 718 // http://crbut.com/88255 is fixed this test will pass. |
| 662 #define MAYBE_TestOSRegistrationFailure FAILS_TestOSRegistrationFailure | 719 #define MAYBE_TestOSRegistrationFailure FAILS_TestOSRegistrationFailure |
| 663 #else | 720 #else |
| 664 #define MAYBE_TestOSRegistrationFailure TestOSRegistrationFailure | 721 #define MAYBE_TestOSRegistrationFailure TestOSRegistrationFailure |
| 665 #endif | 722 #endif |
| 666 | 723 |
| 724 // TODO(smckay): This is much more appropriately an integration |
| 725 // test. Make that so, then update the |
| 726 // ShellIntegretion{Delegate,Observer,Worker} test classes we use to fully |
| 727 // isolate this test from the FILE thread. |
| 667 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestOSRegistrationFailure) { | 728 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestOSRegistrationFailure) { |
| 668 ProtocolHandler ph_do = CreateProtocolHandler("do", "test1"); | 729 ProtocolHandler ph_do = CreateProtocolHandler("do", "test1"); |
| 669 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test"); | 730 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test"); |
| 670 | 731 |
| 671 ASSERT_FALSE(registry()->IsHandledProtocol("do")); | 732 ASSERT_FALSE(registry()->IsHandledProtocol("do")); |
| 672 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); | 733 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); |
| 673 | 734 |
| 674 registry()->OnAcceptRegisterProtocolHandler(ph_do); | 735 registry()->OnAcceptRegisterProtocolHandler(ph_do); |
| 675 MessageLoop::current()->Run(); | 736 MessageLoop::current()->Run(); // FILE thread needs to run. |
| 676 delegate()->set_force_os_failure(true); | 737 delegate()->set_force_os_failure(true); |
| 677 registry()->OnAcceptRegisterProtocolHandler(ph_dont); | 738 registry()->OnAcceptRegisterProtocolHandler(ph_dont); |
| 678 MessageLoop::current()->Run(); | 739 MessageLoop::current()->Run(); // FILE thread needs to run. |
| 679 ASSERT_TRUE(registry()->IsHandledProtocol("do")); | 740 ASSERT_TRUE(registry()->IsHandledProtocol("do")); |
| 680 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size()); | 741 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size()); |
| 681 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); | 742 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); |
| 682 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size()); | 743 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size()); |
| 683 } | 744 } |
| 684 | 745 |
| 685 static void MakeRequest(const GURL& url, ProtocolHandlerRegistry* registry) { | |
| 686 net::URLRequestContext context; | |
| 687 net::URLRequest request(url, NULL, &context); | |
| 688 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 689 MessageLoop::QuitClosure()); | |
| 690 scoped_refptr<net::URLRequestJob> job(registry->MaybeCreateJob(&request)); | |
| 691 ASSERT_TRUE(job.get() != NULL); | |
| 692 } | |
| 693 | |
| 694 TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) { | 746 TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) { |
| 695 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); | 747 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); |
| 696 registry()->OnAcceptRegisterProtocolHandler(ph1); | 748 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 697 GURL url("mailto:someone@something.com"); | 749 GURL url("mailto:someone@something.com"); |
| 698 scoped_refptr<ProtocolHandlerRegistry> r(registry()); | 750 net::URLRequestJobFactory::Interceptor* interceptor = |
| 699 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 751 registry()->CreateURLInterceptor(); |
| 700 base::Bind(MakeRequest, url, r)); | |
| 701 MessageLoop::current()->Run(); | |
| 702 } | |
| 703 | 752 |
| 704 static void CheckIsHandled(const std::string& scheme, bool expected, | 753 AssertIntercepted(url, interceptor); |
| 705 ProtocolHandlerRegistry* registry) { | |
| 706 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 707 MessageLoop::QuitClosure()); | |
| 708 ASSERT_EQ(expected, registry->IsHandledProtocolIO(scheme)); | |
| 709 } | 754 } |
| 710 | 755 |
| 711 TEST_F(ProtocolHandlerRegistryTest, | 756 TEST_F(ProtocolHandlerRegistryTest, |
| 712 MAYBE_TestIsHandledProtocolWorksOnIOThread) { | 757 MAYBE_TestIsHandledProtocolWorksOnIOThread) { |
| 713 std::string scheme("mailto"); | 758 std::string scheme("mailto"); |
| 714 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); | 759 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); |
| 715 registry()->OnAcceptRegisterProtocolHandler(ph1); | 760 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 716 scoped_refptr<ProtocolHandlerRegistry> r(registry()); | 761 net::URLRequestJobFactory::Interceptor* interceptor = |
| 717 BrowserThread::PostTask( | 762 registry()->CreateURLInterceptor(); |
| 718 BrowserThread::IO, | 763 |
| 719 FROM_HERE, | 764 AssertWillHandle(scheme, true, interceptor); |
| 720 base::Bind(CheckIsHandled, scheme, true, r)); | |
| 721 } | 765 } |
| 722 | 766 |
| 723 TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultFallsBackToOldDefault) { | 767 TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultFallsBackToOldDefault) { |
| 724 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); | 768 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); |
| 725 ProtocolHandler ph2 = CreateProtocolHandler("mailto", "test2"); | 769 ProtocolHandler ph2 = CreateProtocolHandler("mailto", "test2"); |
| 726 ProtocolHandler ph3 = CreateProtocolHandler("mailto", "test3"); | 770 ProtocolHandler ph3 = CreateProtocolHandler("mailto", "test3"); |
| 727 registry()->OnAcceptRegisterProtocolHandler(ph1); | 771 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 728 registry()->OnAcceptRegisterProtocolHandler(ph2); | 772 registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 729 registry()->OnAcceptRegisterProtocolHandler(ph3); | 773 registry()->OnAcceptRegisterProtocolHandler(ph3); |
| 730 | 774 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 754 | 798 |
| 755 ASSERT_EQ(ph2, handlers[0]); | 799 ASSERT_EQ(ph2, handlers[0]); |
| 756 ASSERT_EQ(ph1, handlers[1]); | 800 ASSERT_EQ(ph1, handlers[1]); |
| 757 } | 801 } |
| 758 | 802 |
| 759 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestClearDefaultGetsPropagatedToIO) { | 803 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestClearDefaultGetsPropagatedToIO) { |
| 760 std::string scheme("mailto"); | 804 std::string scheme("mailto"); |
| 761 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); | 805 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); |
| 762 registry()->OnAcceptRegisterProtocolHandler(ph1); | 806 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 763 registry()->ClearDefault(scheme); | 807 registry()->ClearDefault(scheme); |
| 764 scoped_refptr<ProtocolHandlerRegistry> r(registry()); | 808 net::URLRequestJobFactory::Interceptor* interceptor = |
| 809 registry()->CreateURLInterceptor(); |
| 765 | 810 |
| 766 BrowserThread::PostTask( | 811 AssertWillHandle(scheme, false, interceptor); |
| 767 BrowserThread::IO, | |
| 768 FROM_HERE, | |
| 769 base::Bind(CheckIsHandled, scheme, false, r)); | |
| 770 } | |
| 771 | |
| 772 static void QuitUILoop() { | |
| 773 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 774 MessageLoop::QuitClosure()); | |
| 775 } | 812 } |
| 776 | 813 |
| 777 TEST_F(ProtocolHandlerRegistryTest, TestLoadEnabledGetsPropogatedToIO) { | 814 TEST_F(ProtocolHandlerRegistryTest, TestLoadEnabledGetsPropogatedToIO) { |
| 815 std::string mailto("mailto"); |
| 816 ProtocolHandler ph1 = CreateProtocolHandler(mailto, "MailtoHandler"); |
| 817 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 818 |
| 819 AssertWillHandle(mailto, true, registry()->CreateURLInterceptor()); |
| 778 registry()->Disable(); | 820 registry()->Disable(); |
| 779 ReloadProtocolHandlerRegistry(); | 821 AssertWillHandle(mailto, false, registry()->CreateURLInterceptor()); |
| 780 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 781 base::Bind(QuitUILoop)); | |
| 782 MessageLoop::current()->Run(); | |
| 783 ASSERT_FALSE(enabled_io()); | |
| 784 } | 822 } |
| 785 | 823 |
| 786 TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) { | 824 TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) { |
| 787 ProtocolHandler ph1 = CreateProtocolHandler("mailto", | 825 ProtocolHandler ph1 = CreateProtocolHandler("mailto", |
| 788 GURL("http://test.com/%s"), "test1"); | 826 GURL("http://test.com/%s"), "test1"); |
| 789 ProtocolHandler ph2 = CreateProtocolHandler("mailto", | 827 ProtocolHandler ph2 = CreateProtocolHandler("mailto", |
| 790 GURL("http://test.com/updated-url/%s"), "test2"); | 828 GURL("http://test.com/updated-url/%s"), "test2"); |
| 791 registry()->OnAcceptRegisterProtocolHandler(ph1); | 829 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 792 ASSERT_TRUE(registry()->AttemptReplace(ph2)); | 830 ASSERT_TRUE(registry()->AttemptReplace(ph2)); |
| 793 const ProtocolHandler& handler(registry()->GetHandlerFor("mailto")); | 831 const ProtocolHandler& handler(registry()->GetHandlerFor("mailto")); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 837 ph1.IsSameOrigin(ph2)); | 875 ph1.IsSameOrigin(ph2)); |
| 838 ASSERT_EQ(ph1.url().GetOrigin() == ph2.url().GetOrigin(), | 876 ASSERT_EQ(ph1.url().GetOrigin() == ph2.url().GetOrigin(), |
| 839 ph2.IsSameOrigin(ph1)); | 877 ph2.IsSameOrigin(ph1)); |
| 840 ASSERT_EQ(ph2.url().GetOrigin() == ph3.url().GetOrigin(), | 878 ASSERT_EQ(ph2.url().GetOrigin() == ph3.url().GetOrigin(), |
| 841 ph2.IsSameOrigin(ph3)); | 879 ph2.IsSameOrigin(ph3)); |
| 842 ASSERT_EQ(ph3.url().GetOrigin() == ph2.url().GetOrigin(), | 880 ASSERT_EQ(ph3.url().GetOrigin() == ph2.url().GetOrigin(), |
| 843 ph3.IsSameOrigin(ph2)); | 881 ph3.IsSameOrigin(ph2)); |
| 844 } | 882 } |
| 845 | 883 |
| 846 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { | 884 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { |
| 847 ReloadProtocolHandlerRegistryAndInstallDefaultHandler(); | 885 RecreateRegistry(false); |
| 886 registry()->AddPredefinedHandler(CreateProtocolHandler( |
| 887 "test", GURL("http://test.com/%s"), "Test")); |
| 888 registry()->InitProtocolSettings(); |
| 848 std::vector<std::string> protocols; | 889 std::vector<std::string> protocols; |
| 849 registry()->GetRegisteredProtocols(&protocols); | 890 registry()->GetRegisteredProtocols(&protocols); |
| 850 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); | 891 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); |
| 851 } | 892 } |
| OLD | NEW |