Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: chrome/browser/custom_handlers/protocol_handler_registry_unittest.cc

Issue 14113053: chrome: Use base::MessageLoop. (Part 3) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 44
45 void AssertIntercepted( 45 void AssertIntercepted(
46 const GURL& url, 46 const GURL& url,
47 net::URLRequestJobFactory* interceptor) { 47 net::URLRequestJobFactory* interceptor) {
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
49 BrowserThread::PostTask(BrowserThread::IO, 49 BrowserThread::PostTask(BrowserThread::IO,
50 FROM_HERE, 50 FROM_HERE,
51 base::Bind(AssertInterceptedIO, 51 base::Bind(AssertInterceptedIO,
52 url, 52 url,
53 base::Unretained(interceptor))); 53 base::Unretained(interceptor)));
54 MessageLoop::current()->RunUntilIdle(); 54 base::MessageLoop::current()->RunUntilIdle();
55 } 55 }
56 56
57 // FakeURLRequestJobFactory returns NULL for all job creation requests and false 57 // FakeURLRequestJobFactory returns NULL for all job creation requests and false
58 // for all IsHandled*() requests. FakeURLRequestJobFactory can be chained to 58 // for all IsHandled*() requests. FakeURLRequestJobFactory can be chained to
59 // ProtocolHandlerRegistry::JobInterceptorFactory so the result of 59 // ProtocolHandlerRegistry::JobInterceptorFactory so the result of
60 // MaybeCreateJobWithProtocolHandler() indicates whether the 60 // MaybeCreateJobWithProtocolHandler() indicates whether the
61 // ProtocolHandlerRegistry properly handled a job creation request. 61 // ProtocolHandlerRegistry properly handled a job creation request.
62 class FakeURLRequestJobFactory : public net::URLRequestJobFactory { 62 class FakeURLRequestJobFactory : public net::URLRequestJobFactory {
63 // net::URLRequestJobFactory implementation: 63 // net::URLRequestJobFactory implementation:
64 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( 64 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler(
(...skipping 25 matching lines...) Expand all
90 const std::string& scheme, 90 const std::string& scheme,
91 bool expected, 91 bool expected,
92 ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) { 92 ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) {
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
94 BrowserThread::PostTask(BrowserThread::IO, 94 BrowserThread::PostTask(BrowserThread::IO,
95 FROM_HERE, 95 FROM_HERE,
96 base::Bind(AssertWillHandleIO, 96 base::Bind(AssertWillHandleIO,
97 scheme, 97 scheme,
98 expected, 98 expected,
99 base::Unretained(interceptor))); 99 base::Unretained(interceptor)));
100 MessageLoop::current()->RunUntilIdle(); 100 base::MessageLoop::current()->RunUntilIdle();
101 } 101 }
102 102
103 class FakeDelegate : public ProtocolHandlerRegistry::Delegate { 103 class FakeDelegate : public ProtocolHandlerRegistry::Delegate {
104 public: 104 public:
105 FakeDelegate() : force_os_failure_(false) {} 105 FakeDelegate() : force_os_failure_(false) {}
106 virtual ~FakeDelegate() { } 106 virtual ~FakeDelegate() { }
107 virtual void RegisterExternalHandler(const std::string& protocol) OVERRIDE { 107 virtual void RegisterExternalHandler(const std::string& protocol) OVERRIDE {
108 ASSERT_TRUE( 108 ASSERT_TRUE(
109 registered_protocols_.find(protocol) == registered_protocols_.end()); 109 registered_protocols_.find(protocol) == registered_protocols_.end());
110 registered_protocols_.insert(protocol); 110 registered_protocols_.insert(protocol);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 delegate_(registry_delegate) {} 168 delegate_(registry_delegate) {}
169 169
170 virtual void SetDefaultWebClientUIState( 170 virtual void SetDefaultWebClientUIState(
171 ShellIntegration::DefaultWebClientUIState state) OVERRIDE { 171 ShellIntegration::DefaultWebClientUIState state) OVERRIDE {
172 ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState( 172 ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState(
173 state); 173 state);
174 if (state == ShellIntegration::STATE_IS_DEFAULT) { 174 if (state == ShellIntegration::STATE_IS_DEFAULT) {
175 delegate_->FakeRegisterWithOS(worker_->protocol()); 175 delegate_->FakeRegisterWithOS(worker_->protocol());
176 } 176 }
177 if (state != ShellIntegration::STATE_PROCESSING) { 177 if (state != ShellIntegration::STATE_PROCESSING) {
178 MessageLoop::current()->Quit(); 178 base::MessageLoop::current()->Quit();
179 } 179 }
180 } 180 }
181 181
182 private: 182 private:
183 FakeDelegate* delegate_; 183 FakeDelegate* delegate_;
184 }; 184 };
185 185
186 class FakeProtocolClientWorker 186 class FakeProtocolClientWorker
187 : public ShellIntegration::DefaultProtocolClientWorker { 187 : public ShellIntegration::DefaultProtocolClientWorker {
188 public: 188 public:
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 bool called_; 270 bool called_;
271 content::NotificationRegistrar notification_registrar_; 271 content::NotificationRegistrar notification_registrar_;
272 }; 272 };
273 273
274 // URLRequest DCHECKS that the current MessageLoop is IO. It does this because 274 // URLRequest DCHECKS that the current MessageLoop is IO. It does this because
275 // it can't check the thread id (since net can't depend on content.) We want 275 // it can't check the thread id (since net can't depend on content.) We want
276 // to harness our tests so all threads use the same loop allowing us to 276 // to harness our tests so all threads use the same loop allowing us to
277 // guarantee all messages are processed.) By overriding the IsType method 277 // guarantee all messages are processed.) By overriding the IsType method
278 // we basically ignore the supplied message loop type, and instead infer 278 // we basically ignore the supplied message loop type, and instead infer
279 // our type based on the current thread. GO DEPENDENCY INJECTION! 279 // our type based on the current thread. GO DEPENDENCY INJECTION!
280 class TestMessageLoop : public MessageLoop { 280 class TestMessageLoop : public base::MessageLoop {
281 public: 281 public:
282 TestMessageLoop() : MessageLoop(MessageLoop::TYPE_DEFAULT) {} 282 TestMessageLoop() : base::MessageLoop(base::MessageLoop::TYPE_DEFAULT) {}
283 virtual ~TestMessageLoop() {} 283 virtual ~TestMessageLoop() {}
284 virtual bool IsType(MessageLoop::Type type) const OVERRIDE { 284 virtual bool IsType(base::MessageLoop::Type type) const OVERRIDE {
285 switch (type) { 285 switch (type) {
286 case MessageLoop::TYPE_UI: 286 case base::MessageLoop::TYPE_UI:
287 return BrowserThread::CurrentlyOn(BrowserThread::UI); 287 return BrowserThread::CurrentlyOn(BrowserThread::UI);
288 case MessageLoop::TYPE_IO: 288 case base::MessageLoop::TYPE_IO:
289 return BrowserThread::CurrentlyOn(BrowserThread::IO); 289 return BrowserThread::CurrentlyOn(BrowserThread::IO);
290 case MessageLoop::TYPE_DEFAULT: 290 case base::MessageLoop::TYPE_DEFAULT:
291 return !BrowserThread::CurrentlyOn(BrowserThread::UI) && 291 return !BrowserThread::CurrentlyOn(BrowserThread::UI) &&
292 !BrowserThread::CurrentlyOn(BrowserThread::IO); 292 !BrowserThread::CurrentlyOn(BrowserThread::IO);
293 } 293 }
294 return false; 294 return false;
295 } 295 }
296 }; 296 };
297 297
298 } // namespace 298 } // namespace
299 299
300 class ProtocolHandlerRegistryTest : public testing::Test { 300 class ProtocolHandlerRegistryTest : public testing::Test {
301 protected: 301 protected:
302 ProtocolHandlerRegistryTest() 302 ProtocolHandlerRegistryTest()
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after
724 TEST_F(ProtocolHandlerRegistryTest, TestOSRegistration) { 724 TEST_F(ProtocolHandlerRegistryTest, TestOSRegistration) {
725 ProtocolHandler ph_do1 = CreateProtocolHandler("do", "test1"); 725 ProtocolHandler ph_do1 = CreateProtocolHandler("do", "test1");
726 ProtocolHandler ph_do2 = CreateProtocolHandler("do", "test2"); 726 ProtocolHandler ph_do2 = CreateProtocolHandler("do", "test2");
727 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test"); 727 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test");
728 728
729 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("do")); 729 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("do"));
730 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont")); 730 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont"));
731 731
732 registry()->OnAcceptRegisterProtocolHandler(ph_do1); 732 registry()->OnAcceptRegisterProtocolHandler(ph_do1);
733 registry()->OnDenyRegisterProtocolHandler(ph_dont); 733 registry()->OnDenyRegisterProtocolHandler(ph_dont);
734 MessageLoop::current()->Run(); // FILE thread needs to run. 734 base::MessageLoop::current()->Run(); // FILE thread needs to run.
735 ASSERT_TRUE(delegate()->IsFakeRegisteredWithOS("do")); 735 ASSERT_TRUE(delegate()->IsFakeRegisteredWithOS("do"));
736 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont")); 736 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont"));
737 737
738 // This should not register with the OS, if it does the delegate 738 // This should not register with the OS, if it does the delegate
739 // will assert for us. We don't need to wait for the message loop 739 // will assert for us. We don't need to wait for the message loop
740 // as it should not go through to the shell worker. 740 // as it should not go through to the shell worker.
741 registry()->OnAcceptRegisterProtocolHandler(ph_do2); 741 registry()->OnAcceptRegisterProtocolHandler(ph_do2);
742 } 742 }
743 743
744 #if defined(OS_LINUX) 744 #if defined(OS_LINUX)
745 // TODO(benwells): When Linux support is more reliable and 745 // TODO(benwells): When Linux support is more reliable and
746 // http://crbut.com/88255 is fixed this test will pass. 746 // http://crbut.com/88255 is fixed this test will pass.
747 #define MAYBE_TestOSRegistrationFailure DISABLED_TestOSRegistrationFailure 747 #define MAYBE_TestOSRegistrationFailure DISABLED_TestOSRegistrationFailure
748 #else 748 #else
749 #define MAYBE_TestOSRegistrationFailure TestOSRegistrationFailure 749 #define MAYBE_TestOSRegistrationFailure TestOSRegistrationFailure
750 #endif 750 #endif
751 751
752 // TODO(smckay): This is much more appropriately an integration 752 // TODO(smckay): This is much more appropriately an integration
753 // test. Make that so, then update the 753 // test. Make that so, then update the
754 // ShellIntegretion{Delegate,Observer,Worker} test classes we use to fully 754 // ShellIntegretion{Delegate,Observer,Worker} test classes we use to fully
755 // isolate this test from the FILE thread. 755 // isolate this test from the FILE thread.
756 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestOSRegistrationFailure) { 756 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestOSRegistrationFailure) {
757 ProtocolHandler ph_do = CreateProtocolHandler("do", "test1"); 757 ProtocolHandler ph_do = CreateProtocolHandler("do", "test1");
758 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test"); 758 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test");
759 759
760 ASSERT_FALSE(registry()->IsHandledProtocol("do")); 760 ASSERT_FALSE(registry()->IsHandledProtocol("do"));
761 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); 761 ASSERT_FALSE(registry()->IsHandledProtocol("dont"));
762 762
763 registry()->OnAcceptRegisterProtocolHandler(ph_do); 763 registry()->OnAcceptRegisterProtocolHandler(ph_do);
764 MessageLoop::current()->Run(); // FILE thread needs to run. 764 base::MessageLoop::current()->Run(); // FILE thread needs to run.
765 delegate()->set_force_os_failure(true); 765 delegate()->set_force_os_failure(true);
766 registry()->OnAcceptRegisterProtocolHandler(ph_dont); 766 registry()->OnAcceptRegisterProtocolHandler(ph_dont);
767 MessageLoop::current()->Run(); // FILE thread needs to run. 767 base::MessageLoop::current()->Run(); // FILE thread needs to run.
768 ASSERT_TRUE(registry()->IsHandledProtocol("do")); 768 ASSERT_TRUE(registry()->IsHandledProtocol("do"));
769 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size()); 769 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size());
770 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); 770 ASSERT_FALSE(registry()->IsHandledProtocol("dont"));
771 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size()); 771 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size());
772 } 772 }
773 773
774 TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) { 774 TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) {
775 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); 775 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1");
776 registry()->OnAcceptRegisterProtocolHandler(ph1); 776 registry()->OnAcceptRegisterProtocolHandler(ph1);
777 GURL url("mailto:someone@something.com"); 777 GURL url("mailto:someone@something.com");
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
913 913
914 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { 914 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) {
915 RecreateRegistry(false); 915 RecreateRegistry(false);
916 registry()->AddPredefinedHandler(CreateProtocolHandler( 916 registry()->AddPredefinedHandler(CreateProtocolHandler(
917 "test", GURL("http://test.com/%s"), "Test")); 917 "test", GURL("http://test.com/%s"), "Test"));
918 registry()->InitProtocolSettings(); 918 registry()->InitProtocolSettings();
919 std::vector<std::string> protocols; 919 std::vector<std::string> protocols;
920 registry()->GetRegisteredProtocols(&protocols); 920 registry()->GetRegisteredProtocols(&protocols);
921 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); 921 ASSERT_EQ(static_cast<size_t>(1), protocols.size());
922 } 922 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698