Chromium Code Reviews| Index: chrome/browser/custom_handlers/protocol_handler_registry_unittest.cc |
| diff --git a/chrome/browser/custom_handlers/protocol_handler_registry_unittest.cc b/chrome/browser/custom_handlers/protocol_handler_registry_unittest.cc |
| index 78e755c894ded0acea43e5df6e1c719a865c8cbe..e343dab9112429cd4f60aac8735d4d3196200721 100644 |
| --- a/chrome/browser/custom_handlers/protocol_handler_registry_unittest.cc |
| +++ b/chrome/browser/custom_handlers/protocol_handler_registry_unittest.cc |
| @@ -26,6 +26,57 @@ using content::BrowserThread; |
| namespace { |
| +static scoped_refptr<net::URLRequestJob> MaybeInterceptIO( |
| + const GURL& url, |
| + net::URLRequestJobFactory::Interceptor* interceptor) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + net::URLRequestContext context; |
| + net::URLRequest request(url, NULL, &context); |
| + return interceptor->MaybeIntercept(&request); |
| +} |
| + |
| +static void AssertInterceptedIO( |
| + const GURL& url, |
| + net::URLRequestJobFactory::Interceptor* interceptor) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + scoped_refptr<net::URLRequestJob> job = MaybeInterceptIO(url, interceptor); |
| + ASSERT_TRUE(job.get() != NULL); |
| +} |
| + |
| +static void AssertIntercepted( |
| + const GURL& url, |
| + net::URLRequestJobFactory::Interceptor* interceptor) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + BrowserThread::PostTask(BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(AssertInterceptedIO, |
| + url, |
| + base::Unretained(interceptor))); |
| + MessageLoop::current()->RunAllPending(); |
| +} |
| + |
| +static void WillHandleIO( |
| + const std::string& scheme, |
| + bool expected, |
| + net::URLRequestJobFactory::Interceptor* interceptor) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + ASSERT_EQ(expected, interceptor->WillHandleProtocol(scheme)); |
| +} |
| + |
| +static void WillHandle( |
| + const std::string& scheme, |
| + bool expected, |
| + net::URLRequestJobFactory::Interceptor* interceptor) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + BrowserThread::PostTask(BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(WillHandleIO, |
| + scheme, |
| + expected, |
| + base::Unretained(interceptor))); |
| + MessageLoop::current()->RunAllPending(); |
| +} |
| + |
| class FakeDelegate : public ProtocolHandlerRegistry::Delegate { |
| public: |
| FakeDelegate() : force_os_failure_(false) {} |
| @@ -203,7 +254,7 @@ class ProtocolHandlerRegistryTest : public testing::Test { |
| FakeDelegate* delegate() const { return delegate_; } |
| TestingProfile* profile() const { return profile_.get(); } |
| PrefService* pref_service() const { return profile_->GetPrefs(); } |
| - ProtocolHandlerRegistry* registry() const { return registry_.get(); } |
| + ProtocolHandlerRegistry* registry() const { return registry_; } |
| const ProtocolHandler& test_protocol_handler() const { |
| return test_protocol_handler_; |
| } |
| @@ -223,20 +274,20 @@ class ProtocolHandlerRegistryTest : public testing::Test { |
| void ReloadProtocolHandlerRegistry() { |
| delegate_ = new FakeDelegate(); |
| - registry_->Finalize(); |
| + registry_->Shutdown(); |
| registry_ = NULL; |
| registry_ = new ProtocolHandlerRegistry(profile(), delegate()); |
| - registry_->Load(); |
| + registry_->InitProtocolSettings(); |
| } |
| void ReloadProtocolHandlerRegistryAndInstallDefaultHandler() { |
| delegate_ = new FakeDelegate(); |
| - registry_->Finalize(); |
| + registry_->Shutdown(); |
| registry_ = NULL; |
| registry_ = new ProtocolHandlerRegistry(profile(), delegate()); |
| registry_->AddPredefinedHandler(CreateProtocolHandler( |
| "test", GURL("http://test.com/%s"), "Test")); |
| - registry_->Load(); |
| + registry_->InitProtocolSettings(); |
| } |
| virtual void SetUp() { |
| @@ -253,7 +304,7 @@ class ProtocolHandlerRegistryTest : public testing::Test { |
| profile_->SetPrefService(new TestingPrefService()); |
| delegate_ = new FakeDelegate(); |
| registry_ = new ProtocolHandlerRegistry(profile(), delegate()); |
| - registry_->Load(); |
| + registry_->InitProtocolSettings(); |
| test_protocol_handler_ = |
| CreateProtocolHandler("test", GURL("http://test.com/%s"), "Test"); |
| @@ -261,7 +312,7 @@ class ProtocolHandlerRegistryTest : public testing::Test { |
| } |
| virtual void TearDown() { |
| - registry_->Finalize(); |
| + registry_->Shutdown(); |
| registry_ = NULL; |
| io_thread_->Stop(); |
| io_thread_.reset(NULL); |
| @@ -271,10 +322,6 @@ class ProtocolHandlerRegistryTest : public testing::Test { |
| ui_message_loop_.reset(NULL); |
| } |
| - bool enabled_io() { |
| - return registry()->enabled_io_; |
| - } |
| - |
| scoped_ptr<MessageLoopForUI> ui_message_loop_; |
| scoped_ptr<content::TestBrowserThread> ui_thread_; |
| scoped_ptr<content::TestBrowserThread> io_thread_; |
| @@ -282,7 +329,7 @@ class ProtocolHandlerRegistryTest : public testing::Test { |
| FakeDelegate* delegate_; |
| scoped_ptr<TestingProfile> profile_; |
| - scoped_refptr<ProtocolHandlerRegistry> registry_; |
| + ProtocolHandlerRegistry* registry_; |
|
benwells
2012/06/22 23:31:27
I think you've got a leak now. You need to delete
Steve McKay
2012/06/27 21:56:45
Test is updated to explicitly manage the lifecycle
|
| ProtocolHandler test_protocol_handler_; |
| }; |
| @@ -675,30 +722,19 @@ TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestOSRegistrationFailure) { |
| ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size()); |
| } |
| -static void MakeRequest(const GURL& url, ProtocolHandlerRegistry* registry) { |
| - net::URLRequestContext context; |
| - net::URLRequest request(url, NULL, &context); |
| - BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| - MessageLoop::QuitClosure()); |
| - scoped_refptr<net::URLRequestJob> job(registry->MaybeCreateJob(&request)); |
| - ASSERT_TRUE(job.get() != NULL); |
| -} |
| - |
| TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) { |
| ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); |
| registry()->OnAcceptRegisterProtocolHandler(ph1); |
| GURL url("mailto:someone@something.com"); |
| - scoped_refptr<ProtocolHandlerRegistry> r(registry()); |
| - BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| - base::Bind(MakeRequest, url, r)); |
| - MessageLoop::current()->Run(); |
| -} |
| -static void CheckIsHandled(const std::string& scheme, bool expected, |
| - ProtocolHandlerRegistry* registry) { |
| + net::URLRequestJobFactory::Interceptor* interceptor = |
| + registry()->CreateIOURLInterceptor(); |
| + |
| + // tell the UI thread it can quit after we're done |
| BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| MessageLoop::QuitClosure()); |
| - ASSERT_EQ(expected, registry->IsHandledProtocolIO(scheme)); |
| + |
| + AssertIntercepted(url, interceptor); |
| } |
| TEST_F(ProtocolHandlerRegistryTest, |
| @@ -706,11 +742,12 @@ TEST_F(ProtocolHandlerRegistryTest, |
| std::string scheme("mailto"); |
| ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); |
| registry()->OnAcceptRegisterProtocolHandler(ph1); |
| - scoped_refptr<ProtocolHandlerRegistry> r(registry()); |
| - BrowserThread::PostTask( |
| - BrowserThread::IO, |
| - FROM_HERE, |
| - base::Bind(CheckIsHandled, scheme, true, r)); |
| + |
| + net::URLRequestJobFactory::Interceptor* interceptor = |
| + registry()->CreateIOURLInterceptor(); |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + MessageLoop::QuitClosure()); |
| + WillHandle(scheme, true, interceptor); |
| } |
| TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultFallsBackToOldDefault) { |
| @@ -754,26 +791,20 @@ TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestClearDefaultGetsPropagatedToIO) { |
| ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); |
| registry()->OnAcceptRegisterProtocolHandler(ph1); |
| registry()->ClearDefault(scheme); |
| - scoped_refptr<ProtocolHandlerRegistry> r(registry()); |
| - |
| - BrowserThread::PostTask( |
| - BrowserThread::IO, |
| - FROM_HERE, |
| - base::Bind(CheckIsHandled, scheme, false, r)); |
| -} |
| - |
| -static void QuitUILoop() { |
| + net::URLRequestJobFactory::Interceptor* interceptor = |
| + registry()->CreateIOURLInterceptor(); |
| BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| MessageLoop::QuitClosure()); |
| + WillHandle(scheme, false, interceptor); |
| } |
| TEST_F(ProtocolHandlerRegistryTest, TestLoadEnabledGetsPropogatedToIO) { |
| + std::string mailto("mailto"); |
| + ProtocolHandler ph1 = CreateProtocolHandler(mailto, "MailtoHandler"); |
| + registry()->OnAcceptRegisterProtocolHandler(ph1); |
| + WillHandle(mailto, true, registry()->CreateIOURLInterceptor()); |
| registry()->Disable(); |
| - ReloadProtocolHandlerRegistry(); |
| - BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| - base::Bind(QuitUILoop)); |
| - MessageLoop::current()->Run(); |
| - ASSERT_FALSE(enabled_io()); |
| + WillHandle(mailto, false, registry()->CreateIOURLInterceptor()); |
| } |
| TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) { |