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

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: rebase again Created 7 years, 6 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 28 matching lines...) Expand all
93 const std::string& scheme, 93 const std::string& scheme,
94 bool expected, 94 bool expected,
95 ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) { 95 ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) {
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
97 BrowserThread::PostTask(BrowserThread::IO, 97 BrowserThread::PostTask(BrowserThread::IO,
98 FROM_HERE, 98 FROM_HERE,
99 base::Bind(AssertWillHandleIO, 99 base::Bind(AssertWillHandleIO,
100 scheme, 100 scheme,
101 expected, 101 expected,
102 base::Unretained(interceptor))); 102 base::Unretained(interceptor)));
103 MessageLoop::current()->RunUntilIdle(); 103 base::MessageLoop::current()->RunUntilIdle();
104 } 104 }
105 105
106 class FakeDelegate : public ProtocolHandlerRegistry::Delegate { 106 class FakeDelegate : public ProtocolHandlerRegistry::Delegate {
107 public: 107 public:
108 FakeDelegate() : force_os_failure_(false) {} 108 FakeDelegate() : force_os_failure_(false) {}
109 virtual ~FakeDelegate() { } 109 virtual ~FakeDelegate() { }
110 virtual void RegisterExternalHandler(const std::string& protocol) OVERRIDE { 110 virtual void RegisterExternalHandler(const std::string& protocol) OVERRIDE {
111 ASSERT_TRUE( 111 ASSERT_TRUE(
112 registered_protocols_.find(protocol) == registered_protocols_.end()); 112 registered_protocols_.find(protocol) == registered_protocols_.end());
113 registered_protocols_.insert(protocol); 113 registered_protocols_.insert(protocol);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 delegate_(registry_delegate) {} 171 delegate_(registry_delegate) {}
172 172
173 virtual void SetDefaultWebClientUIState( 173 virtual void SetDefaultWebClientUIState(
174 ShellIntegration::DefaultWebClientUIState state) OVERRIDE { 174 ShellIntegration::DefaultWebClientUIState state) OVERRIDE {
175 ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState( 175 ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState(
176 state); 176 state);
177 if (state == ShellIntegration::STATE_IS_DEFAULT) { 177 if (state == ShellIntegration::STATE_IS_DEFAULT) {
178 delegate_->FakeRegisterWithOS(worker_->protocol()); 178 delegate_->FakeRegisterWithOS(worker_->protocol());
179 } 179 }
180 if (state != ShellIntegration::STATE_PROCESSING) { 180 if (state != ShellIntegration::STATE_PROCESSING) {
181 MessageLoop::current()->Quit(); 181 base::MessageLoop::current()->Quit();
182 } 182 }
183 } 183 }
184 184
185 private: 185 private:
186 FakeDelegate* delegate_; 186 FakeDelegate* delegate_;
187 }; 187 };
188 188
189 class FakeProtocolClientWorker 189 class FakeProtocolClientWorker
190 : public ShellIntegration::DefaultProtocolClientWorker { 190 : public ShellIntegration::DefaultProtocolClientWorker {
191 public: 191 public:
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 bool called_; 273 bool called_;
274 content::NotificationRegistrar notification_registrar_; 274 content::NotificationRegistrar notification_registrar_;
275 }; 275 };
276 276
277 // URLRequest DCHECKS that the current MessageLoop is IO. It does this because 277 // URLRequest DCHECKS that the current MessageLoop is IO. It does this because
278 // it can't check the thread id (since net can't depend on content.) We want 278 // it can't check the thread id (since net can't depend on content.) We want
279 // to harness our tests so all threads use the same loop allowing us to 279 // to harness our tests so all threads use the same loop allowing us to
280 // guarantee all messages are processed.) By overriding the IsType method 280 // guarantee all messages are processed.) By overriding the IsType method
281 // we basically ignore the supplied message loop type, and instead infer 281 // we basically ignore the supplied message loop type, and instead infer
282 // our type based on the current thread. GO DEPENDENCY INJECTION! 282 // our type based on the current thread. GO DEPENDENCY INJECTION!
283 class TestMessageLoop : public MessageLoop { 283 class TestMessageLoop : public base::MessageLoop {
284 public: 284 public:
285 TestMessageLoop() : MessageLoop(MessageLoop::TYPE_DEFAULT) {} 285 TestMessageLoop() : base::MessageLoop(base::MessageLoop::TYPE_DEFAULT) {}
286 virtual ~TestMessageLoop() {} 286 virtual ~TestMessageLoop() {}
287 virtual bool IsType(MessageLoop::Type type) const OVERRIDE { 287 virtual bool IsType(base::MessageLoop::Type type) const OVERRIDE {
288 switch (type) { 288 switch (type) {
289 case MessageLoop::TYPE_UI: 289 case base::MessageLoop::TYPE_UI:
290 return BrowserThread::CurrentlyOn(BrowserThread::UI); 290 return BrowserThread::CurrentlyOn(BrowserThread::UI);
291 case MessageLoop::TYPE_IO: 291 case base::MessageLoop::TYPE_IO:
292 return BrowserThread::CurrentlyOn(BrowserThread::IO); 292 return BrowserThread::CurrentlyOn(BrowserThread::IO);
293 case MessageLoop::TYPE_DEFAULT: 293 case base::MessageLoop::TYPE_DEFAULT:
294 return !BrowserThread::CurrentlyOn(BrowserThread::UI) && 294 return !BrowserThread::CurrentlyOn(BrowserThread::UI) &&
295 !BrowserThread::CurrentlyOn(BrowserThread::IO); 295 !BrowserThread::CurrentlyOn(BrowserThread::IO);
296 } 296 }
297 return false; 297 return false;
298 } 298 }
299 }; 299 };
300 300
301 } // namespace 301 } // namespace
302 302
303 class ProtocolHandlerRegistryTest : public testing::Test { 303 class ProtocolHandlerRegistryTest : public testing::Test {
304 protected: 304 protected:
305 ProtocolHandlerRegistryTest() 305 ProtocolHandlerRegistryTest()
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after
727 TEST_F(ProtocolHandlerRegistryTest, TestOSRegistration) { 727 TEST_F(ProtocolHandlerRegistryTest, TestOSRegistration) {
728 ProtocolHandler ph_do1 = CreateProtocolHandler("do", "test1"); 728 ProtocolHandler ph_do1 = CreateProtocolHandler("do", "test1");
729 ProtocolHandler ph_do2 = CreateProtocolHandler("do", "test2"); 729 ProtocolHandler ph_do2 = CreateProtocolHandler("do", "test2");
730 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test"); 730 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test");
731 731
732 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("do")); 732 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("do"));
733 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont")); 733 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont"));
734 734
735 registry()->OnAcceptRegisterProtocolHandler(ph_do1); 735 registry()->OnAcceptRegisterProtocolHandler(ph_do1);
736 registry()->OnDenyRegisterProtocolHandler(ph_dont); 736 registry()->OnDenyRegisterProtocolHandler(ph_dont);
737 MessageLoop::current()->Run(); // FILE thread needs to run. 737 base::MessageLoop::current()->Run(); // FILE thread needs to run.
738 ASSERT_TRUE(delegate()->IsFakeRegisteredWithOS("do")); 738 ASSERT_TRUE(delegate()->IsFakeRegisteredWithOS("do"));
739 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont")); 739 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont"));
740 740
741 // This should not register with the OS, if it does the delegate 741 // This should not register with the OS, if it does the delegate
742 // will assert for us. We don't need to wait for the message loop 742 // will assert for us. We don't need to wait for the message loop
743 // as it should not go through to the shell worker. 743 // as it should not go through to the shell worker.
744 registry()->OnAcceptRegisterProtocolHandler(ph_do2); 744 registry()->OnAcceptRegisterProtocolHandler(ph_do2);
745 } 745 }
746 746
747 #if defined(OS_LINUX) 747 #if defined(OS_LINUX)
748 // TODO(benwells): When Linux support is more reliable and 748 // TODO(benwells): When Linux support is more reliable and
749 // http://crbut.com/88255 is fixed this test will pass. 749 // http://crbut.com/88255 is fixed this test will pass.
750 #define MAYBE_TestOSRegistrationFailure DISABLED_TestOSRegistrationFailure 750 #define MAYBE_TestOSRegistrationFailure DISABLED_TestOSRegistrationFailure
751 #else 751 #else
752 #define MAYBE_TestOSRegistrationFailure TestOSRegistrationFailure 752 #define MAYBE_TestOSRegistrationFailure TestOSRegistrationFailure
753 #endif 753 #endif
754 754
755 // TODO(smckay): This is much more appropriately an integration 755 // TODO(smckay): This is much more appropriately an integration
756 // test. Make that so, then update the 756 // test. Make that so, then update the
757 // ShellIntegretion{Delegate,Observer,Worker} test classes we use to fully 757 // ShellIntegretion{Delegate,Observer,Worker} test classes we use to fully
758 // isolate this test from the FILE thread. 758 // isolate this test from the FILE thread.
759 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestOSRegistrationFailure) { 759 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestOSRegistrationFailure) {
760 ProtocolHandler ph_do = CreateProtocolHandler("do", "test1"); 760 ProtocolHandler ph_do = CreateProtocolHandler("do", "test1");
761 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test"); 761 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test");
762 762
763 ASSERT_FALSE(registry()->IsHandledProtocol("do")); 763 ASSERT_FALSE(registry()->IsHandledProtocol("do"));
764 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); 764 ASSERT_FALSE(registry()->IsHandledProtocol("dont"));
765 765
766 registry()->OnAcceptRegisterProtocolHandler(ph_do); 766 registry()->OnAcceptRegisterProtocolHandler(ph_do);
767 MessageLoop::current()->Run(); // FILE thread needs to run. 767 base::MessageLoop::current()->Run(); // FILE thread needs to run.
768 delegate()->set_force_os_failure(true); 768 delegate()->set_force_os_failure(true);
769 registry()->OnAcceptRegisterProtocolHandler(ph_dont); 769 registry()->OnAcceptRegisterProtocolHandler(ph_dont);
770 MessageLoop::current()->Run(); // FILE thread needs to run. 770 base::MessageLoop::current()->Run(); // FILE thread needs to run.
771 ASSERT_TRUE(registry()->IsHandledProtocol("do")); 771 ASSERT_TRUE(registry()->IsHandledProtocol("do"));
772 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size()); 772 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size());
773 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); 773 ASSERT_FALSE(registry()->IsHandledProtocol("dont"));
774 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size()); 774 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size());
775 } 775 }
776 776
777 TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) { 777 TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) {
778 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); 778 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1");
779 registry()->OnAcceptRegisterProtocolHandler(ph1); 779 registry()->OnAcceptRegisterProtocolHandler(ph1);
780 GURL url("mailto:someone@something.com"); 780 GURL url("mailto:someone@something.com");
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
916 916
917 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { 917 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) {
918 RecreateRegistry(false); 918 RecreateRegistry(false);
919 registry()->AddPredefinedHandler(CreateProtocolHandler( 919 registry()->AddPredefinedHandler(CreateProtocolHandler(
920 "test", GURL("http://test.com/%s"), "Test")); 920 "test", GURL("http://test.com/%s"), "Test"));
921 registry()->InitProtocolSettings(); 921 registry()->InitProtocolSettings();
922 std::vector<std::string> protocols; 922 std::vector<std::string> protocols;
923 registry()->GetRegisteredProtocols(&protocols); 923 registry()->GetRegisteredProtocols(&protocols);
924 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); 924 ASSERT_EQ(static_cast<size_t>(1), protocols.size());
925 } 925 }
OLDNEW
« no previous file with comments | « chrome/browser/crash_handler_host_linux.cc ('k') | chrome/browser/devtools/devtools_adb_bridge.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698