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

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

Issue 10546083: Convert ProtocolHandlerRegistry to be a ProfileKeyedService. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Respond to Elliot's comments. Created 8 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
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"
11 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
12 #include "chrome/common/chrome_notification_types.h" 12 #include "chrome/common/chrome_notification_types.h"
13 #include "chrome/common/custom_handlers/protocol_handler.h" 13 #include "chrome/common/custom_handlers/protocol_handler.h"
14 #include "chrome/test/base/testing_browser_process.h" 14 #include "chrome/test/base/testing_browser_process.h"
15 #include "chrome/test/base/testing_pref_service.h" 15 #include "chrome/test/base/testing_pref_service.h"
16 #include "chrome/test/base/testing_profile.h" 16 #include "chrome/test/base/testing_profile.h"
17 #include "content/public/browser/notification_observer.h" 17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h" 18 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/notification_source.h" 19 #include "content/public/browser/notification_source.h"
20 #include "content/public/test/test_browser_thread.h" 20 #include "content/public/test/test_browser_thread.h"
21 #include "content/public/test/test_renderer_host.h" 21 #include "content/public/test/test_renderer_host.h"
22 #include "net/url_request/url_request.h" 22 #include "net/url_request/url_request.h"
23 23
24 using content::BrowserThread; 24 using content::BrowserThread;
25 25
26 namespace { 26 namespace {
27 27
28 static scoped_refptr<net::URLRequestJob> MaybeInterceptIO(
29 const GURL& url,
30 net::URLRequestJobFactory::Interceptor* interceptor) {
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
32 net::URLRequest request(url, NULL);
33 return interceptor->MaybeIntercept(&request);
34 }
35
36 static void AssertInterceptedIO(
37 const GURL& url,
38 net::URLRequestJobFactory::Interceptor* interceptor) {
39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
40 scoped_refptr<net::URLRequestJob> job = MaybeInterceptIO(url, interceptor);
41 ASSERT_TRUE(job.get() != NULL);
42 }
43
44 static void AssertIntercepted(
45 const GURL& url,
46 net::URLRequestJobFactory::Interceptor* interceptor) {
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
48 BrowserThread::PostTask(BrowserThread::IO,
49 FROM_HERE,
50 base::Bind(AssertInterceptedIO,
51 url,
52 base::Unretained(interceptor)));
53 MessageLoop::current()->RunAllPending();
54 }
55
56 static void WillHandleIO(
57 const std::string& scheme,
58 bool expected,
59 net::URLRequestJobFactory::Interceptor* interceptor) {
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
61 ASSERT_EQ(expected, interceptor->WillHandleProtocol(scheme));
62 }
63
64 static void WillHandle(
65 const std::string& scheme,
66 bool expected,
67 net::URLRequestJobFactory::Interceptor* interceptor) {
68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
69 BrowserThread::PostTask(BrowserThread::IO,
70 FROM_HERE,
71 base::Bind(WillHandleIO,
72 scheme,
73 expected,
74 base::Unretained(interceptor)));
75 MessageLoop::current()->RunAllPending();
76 }
77
28 class FakeDelegate : public ProtocolHandlerRegistry::Delegate { 78 class FakeDelegate : public ProtocolHandlerRegistry::Delegate {
29 public: 79 public:
30 FakeDelegate() : force_os_failure_(false) {} 80 FakeDelegate() : force_os_failure_(false) {}
31 virtual ~FakeDelegate() { } 81 virtual ~FakeDelegate() { }
32 virtual void RegisterExternalHandler(const std::string& protocol) { 82 virtual void RegisterExternalHandler(const std::string& protocol) {
33 ASSERT_TRUE( 83 ASSERT_TRUE(
34 registered_protocols_.find(protocol) == registered_protocols_.end()); 84 registered_protocols_.find(protocol) == registered_protocols_.end());
35 registered_protocols_.insert(protocol); 85 registered_protocols_.insert(protocol);
36 } 86 }
37 87
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 } // namespace 245 } // namespace
196 246
197 class ProtocolHandlerRegistryTest : public testing::Test { 247 class ProtocolHandlerRegistryTest : public testing::Test {
198 protected: 248 protected:
199 ProtocolHandlerRegistryTest() 249 ProtocolHandlerRegistryTest()
200 : test_protocol_handler_(CreateProtocolHandler("test", "test")) {} 250 : test_protocol_handler_(CreateProtocolHandler("test", "test")) {}
201 251
202 FakeDelegate* delegate() const { return delegate_; } 252 FakeDelegate* delegate() const { return delegate_; }
203 TestingProfile* profile() const { return profile_.get(); } 253 TestingProfile* profile() const { return profile_.get(); }
204 PrefService* pref_service() const { return profile_->GetPrefs(); } 254 PrefService* pref_service() const { return profile_->GetPrefs(); }
205 ProtocolHandlerRegistry* registry() const { return registry_.get(); } 255 ProtocolHandlerRegistry* registry() const { return registry_; }
206 const ProtocolHandler& test_protocol_handler() const { 256 const ProtocolHandler& test_protocol_handler() const {
207 return test_protocol_handler_; 257 return test_protocol_handler_;
208 } 258 }
209 259
210 ProtocolHandler CreateProtocolHandler(const std::string& protocol, 260 ProtocolHandler CreateProtocolHandler(const std::string& protocol,
211 const GURL& url, 261 const GURL& url,
212 const std::string& title) { 262 const std::string& title) {
213 return ProtocolHandler::CreateProtocolHandler(protocol, url, 263 return ProtocolHandler::CreateProtocolHandler(protocol, url,
214 UTF8ToUTF16(title)); 264 UTF8ToUTF16(title));
215 } 265 }
216 266
217 ProtocolHandler CreateProtocolHandler(const std::string& protocol, 267 ProtocolHandler CreateProtocolHandler(const std::string& protocol,
218 const std::string& name) { 268 const std::string& name) {
219 return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s"), 269 return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s"),
220 name); 270 name);
221 } 271 }
222 272
223 void ReloadProtocolHandlerRegistry() { 273 void ReloadProtocolHandlerRegistry() {
224 delegate_ = new FakeDelegate(); 274 delegate_ = new FakeDelegate();
225 registry_->Finalize(); 275 registry_->Shutdown();
226 registry_ = NULL; 276 registry_ = NULL;
227 registry_ = new ProtocolHandlerRegistry(profile(), delegate()); 277 registry_ = new ProtocolHandlerRegistry(profile(), delegate());
228 registry_->Load(); 278 registry_->InitProtocolSettings();
229 } 279 }
230 280
231 void ReloadProtocolHandlerRegistryAndInstallDefaultHandler() { 281 void ReloadProtocolHandlerRegistryAndInstallDefaultHandler() {
232 delegate_ = new FakeDelegate(); 282 delegate_ = new FakeDelegate();
233 registry_->Finalize(); 283 registry_->Shutdown();
234 registry_ = NULL; 284 registry_ = NULL;
235 registry_ = new ProtocolHandlerRegistry(profile(), delegate()); 285 registry_ = new ProtocolHandlerRegistry(profile(), delegate());
236 registry_->AddPredefinedHandler(CreateProtocolHandler( 286 registry_->AddPredefinedHandler(CreateProtocolHandler(
237 "test", GURL("http://test.com/%s"), "Test")); 287 "test", GURL("http://test.com/%s"), "Test"));
238 registry_->Load(); 288 registry_->InitProtocolSettings();
239 } 289 }
240 290
241 virtual void SetUp() { 291 virtual void SetUp() {
242 ui_message_loop_.reset(new MessageLoopForUI()); 292 ui_message_loop_.reset(new MessageLoopForUI());
243 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, 293 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI,
244 MessageLoop::current())); 294 MessageLoop::current()));
245 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO)); 295 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO));
246 io_thread_->StartIOThread(); 296 io_thread_->StartIOThread();
247 297
248 file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE)); 298 file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE));
249 file_thread_->Start(); 299 file_thread_->Start();
250 300
251 profile_.reset(new TestingProfile()); 301 profile_.reset(new TestingProfile());
252 profile_->SetPrefService(new TestingPrefService()); 302 profile_->SetPrefService(new TestingPrefService());
253 delegate_ = new FakeDelegate(); 303 delegate_ = new FakeDelegate();
254 registry_ = new ProtocolHandlerRegistry(profile(), delegate()); 304 registry_ = new ProtocolHandlerRegistry(profile(), delegate());
255 registry_->Load(); 305 registry_->InitProtocolSettings();
256 test_protocol_handler_ = 306 test_protocol_handler_ =
257 CreateProtocolHandler("test", GURL("http://test.com/%s"), "Test"); 307 CreateProtocolHandler("test", GURL("http://test.com/%s"), "Test");
258 308
259 ProtocolHandlerRegistry::RegisterPrefs(pref_service()); 309 ProtocolHandlerRegistry::RegisterPrefs(pref_service());
260 } 310 }
261 311
262 virtual void TearDown() { 312 virtual void TearDown() {
263 registry_->Finalize(); 313 registry_->Shutdown();
264 registry_ = NULL; 314 registry_ = NULL;
265 io_thread_->Stop(); 315 io_thread_->Stop();
266 io_thread_.reset(NULL); 316 io_thread_.reset(NULL);
267 file_thread_->Stop(); 317 file_thread_->Stop();
268 file_thread_.reset(NULL); 318 file_thread_.reset(NULL);
269 ui_thread_.reset(NULL); 319 ui_thread_.reset(NULL);
270 ui_message_loop_.reset(NULL); 320 ui_message_loop_.reset(NULL);
271 } 321 }
272 322
273 bool enabled_io() {
274 return registry()->enabled_io_;
275 }
276
277 scoped_ptr<MessageLoopForUI> ui_message_loop_; 323 scoped_ptr<MessageLoopForUI> ui_message_loop_;
278 scoped_ptr<content::TestBrowserThread> ui_thread_; 324 scoped_ptr<content::TestBrowserThread> ui_thread_;
279 scoped_ptr<content::TestBrowserThread> io_thread_; 325 scoped_ptr<content::TestBrowserThread> io_thread_;
280 scoped_ptr<content::TestBrowserThread> file_thread_; 326 scoped_ptr<content::TestBrowserThread> file_thread_;
281 327
282 FakeDelegate* delegate_; 328 FakeDelegate* delegate_;
283 scoped_ptr<TestingProfile> profile_; 329 scoped_ptr<TestingProfile> profile_;
284 scoped_refptr<ProtocolHandlerRegistry> registry_; 330 ProtocolHandlerRegistry* registry_;
285 ProtocolHandler test_protocol_handler_; 331 ProtocolHandler test_protocol_handler_;
286 }; 332 };
287 333
288 // ProtocolHandlerRegistryTest tests are flaky on Linux & ChromeOS. 334 // ProtocolHandlerRegistryTest tests are flaky on Linux & ChromeOS.
289 // http://crbug.com/133023 335 // http://crbug.com/133023
290 #if defined(OS_LINUX) || defined(OS_CHROMEOS) 336 #if defined(OS_LINUX) || defined(OS_CHROMEOS)
291 #define MAYBE_AcceptProtocolHandlerHandlesProtocol \ 337 #define MAYBE_AcceptProtocolHandlerHandlesProtocol \
292 DISABLED_AcceptProtocolHandlerHandlesProtocol 338 DISABLED_AcceptProtocolHandlerHandlesProtocol
293 #define MAYBE_DeniedProtocolIsntHandledUntilAccepted \ 339 #define MAYBE_DeniedProtocolIsntHandledUntilAccepted \
294 DISABLED_DeniedProtocolIsntHandledUntilAccepted 340 DISABLED_DeniedProtocolIsntHandledUntilAccepted
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
667 MessageLoop::current()->Run(); 713 MessageLoop::current()->Run();
668 delegate()->set_force_os_failure(true); 714 delegate()->set_force_os_failure(true);
669 registry()->OnAcceptRegisterProtocolHandler(ph_dont); 715 registry()->OnAcceptRegisterProtocolHandler(ph_dont);
670 MessageLoop::current()->Run(); 716 MessageLoop::current()->Run();
671 ASSERT_TRUE(registry()->IsHandledProtocol("do")); 717 ASSERT_TRUE(registry()->IsHandledProtocol("do"));
672 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size()); 718 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size());
673 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); 719 ASSERT_FALSE(registry()->IsHandledProtocol("dont"));
674 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size()); 720 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size());
675 } 721 }
676 722
677 static void MakeRequest(const GURL& url, ProtocolHandlerRegistry* registry) {
678 net::URLRequest request(url, NULL);
679 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
680 MessageLoop::QuitClosure());
681 scoped_refptr<net::URLRequestJob> job(registry->MaybeCreateJob(&request));
682 ASSERT_TRUE(job.get() != NULL);
683 }
684
685 TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) { 723 TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) {
686 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); 724 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1");
687 registry()->OnAcceptRegisterProtocolHandler(ph1); 725 registry()->OnAcceptRegisterProtocolHandler(ph1);
688 GURL url("mailto:someone@something.com"); 726 GURL url("mailto:someone@something.com");
689 scoped_refptr<ProtocolHandlerRegistry> r(registry());
690 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
691 base::Bind(MakeRequest, url, r));
692 MessageLoop::current()->Run();
693 }
694 727
695 static void CheckIsHandled(const std::string& scheme, bool expected, 728 net::URLRequestJobFactory::Interceptor* interceptor =
696 ProtocolHandlerRegistry* registry) { 729 registry()->CreateIOURLInterceptor();
730
731 // tell the UI thread it can quit after we're done
697 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 732 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
698 MessageLoop::QuitClosure()); 733 MessageLoop::QuitClosure());
699 ASSERT_EQ(expected, registry->IsHandledProtocolIO(scheme)); 734
735 AssertIntercepted(url, interceptor);
700 } 736 }
701 737
702 TEST_F(ProtocolHandlerRegistryTest, 738 TEST_F(ProtocolHandlerRegistryTest,
703 MAYBE_TestIsHandledProtocolWorksOnIOThread) { 739 MAYBE_TestIsHandledProtocolWorksOnIOThread) {
704 std::string scheme("mailto"); 740 std::string scheme("mailto");
705 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); 741 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1");
706 registry()->OnAcceptRegisterProtocolHandler(ph1); 742 registry()->OnAcceptRegisterProtocolHandler(ph1);
707 scoped_refptr<ProtocolHandlerRegistry> r(registry()); 743
708 BrowserThread::PostTask( 744 net::URLRequestJobFactory::Interceptor* interceptor =
709 BrowserThread::IO, 745 registry()->CreateIOURLInterceptor();
710 FROM_HERE, 746 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
711 base::Bind(CheckIsHandled, scheme, true, r)); 747 MessageLoop::QuitClosure());
748 WillHandle(scheme, true, interceptor);
712 } 749 }
713 750
714 TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultFallsBackToOldDefault) { 751 TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultFallsBackToOldDefault) {
715 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); 752 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1");
716 ProtocolHandler ph2 = CreateProtocolHandler("mailto", "test2"); 753 ProtocolHandler ph2 = CreateProtocolHandler("mailto", "test2");
717 ProtocolHandler ph3 = CreateProtocolHandler("mailto", "test3"); 754 ProtocolHandler ph3 = CreateProtocolHandler("mailto", "test3");
718 registry()->OnAcceptRegisterProtocolHandler(ph1); 755 registry()->OnAcceptRegisterProtocolHandler(ph1);
719 registry()->OnAcceptRegisterProtocolHandler(ph2); 756 registry()->OnAcceptRegisterProtocolHandler(ph2);
720 registry()->OnAcceptRegisterProtocolHandler(ph3); 757 registry()->OnAcceptRegisterProtocolHandler(ph3);
721 758
(...skipping 23 matching lines...) Expand all
745 782
746 ASSERT_EQ(ph2, handlers[0]); 783 ASSERT_EQ(ph2, handlers[0]);
747 ASSERT_EQ(ph1, handlers[1]); 784 ASSERT_EQ(ph1, handlers[1]);
748 } 785 }
749 786
750 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestClearDefaultGetsPropagatedToIO) { 787 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestClearDefaultGetsPropagatedToIO) {
751 std::string scheme("mailto"); 788 std::string scheme("mailto");
752 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); 789 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1");
753 registry()->OnAcceptRegisterProtocolHandler(ph1); 790 registry()->OnAcceptRegisterProtocolHandler(ph1);
754 registry()->ClearDefault(scheme); 791 registry()->ClearDefault(scheme);
755 scoped_refptr<ProtocolHandlerRegistry> r(registry()); 792 net::URLRequestJobFactory::Interceptor* interceptor =
756 793 registry()->CreateIOURLInterceptor();
757 BrowserThread::PostTask(
758 BrowserThread::IO,
759 FROM_HERE,
760 base::Bind(CheckIsHandled, scheme, false, r));
761 }
762
763 static void QuitUILoop() {
764 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 794 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
765 MessageLoop::QuitClosure()); 795 MessageLoop::QuitClosure());
796 WillHandle(scheme, false, interceptor);
766 } 797 }
767 798
768 TEST_F(ProtocolHandlerRegistryTest, TestLoadEnabledGetsPropogatedToIO) { 799 TEST_F(ProtocolHandlerRegistryTest, TestLoadEnabledGetsPropogatedToIO) {
800 std::string mailto("mailto");
801 ProtocolHandler ph1 = CreateProtocolHandler(mailto, "MailtoHandler");
802 registry()->OnAcceptRegisterProtocolHandler(ph1);
803 WillHandle(mailto, true, registry()->CreateIOURLInterceptor());
769 registry()->Disable(); 804 registry()->Disable();
770 ReloadProtocolHandlerRegistry(); 805 WillHandle(mailto, false, registry()->CreateIOURLInterceptor());
771 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
772 base::Bind(QuitUILoop));
773 MessageLoop::current()->Run();
774 ASSERT_FALSE(enabled_io());
775 } 806 }
776 807
777 TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) { 808 TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) {
778 ProtocolHandler ph1 = CreateProtocolHandler("mailto", 809 ProtocolHandler ph1 = CreateProtocolHandler("mailto",
779 GURL("http://test.com/%s"), "test1"); 810 GURL("http://test.com/%s"), "test1");
780 ProtocolHandler ph2 = CreateProtocolHandler("mailto", 811 ProtocolHandler ph2 = CreateProtocolHandler("mailto",
781 GURL("http://test.com/updated-url/%s"), "test2"); 812 GURL("http://test.com/updated-url/%s"), "test2");
782 registry()->OnAcceptRegisterProtocolHandler(ph1); 813 registry()->OnAcceptRegisterProtocolHandler(ph1);
783 ASSERT_TRUE(registry()->AttemptReplace(ph2)); 814 ASSERT_TRUE(registry()->AttemptReplace(ph2));
784 const ProtocolHandler& handler(registry()->GetHandlerFor("mailto")); 815 const ProtocolHandler& handler(registry()->GetHandlerFor("mailto"));
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
833 ASSERT_EQ(ph3.url().GetOrigin() == ph2.url().GetOrigin(), 864 ASSERT_EQ(ph3.url().GetOrigin() == ph2.url().GetOrigin(),
834 ph3.IsSameOrigin(ph2)); 865 ph3.IsSameOrigin(ph2));
835 } 866 }
836 867
837 TEST_F(ProtocolHandlerRegistryTest, TestInstallDefaultHandler) { 868 TEST_F(ProtocolHandlerRegistryTest, TestInstallDefaultHandler) {
838 ReloadProtocolHandlerRegistryAndInstallDefaultHandler(); 869 ReloadProtocolHandlerRegistryAndInstallDefaultHandler();
839 std::vector<std::string> protocols; 870 std::vector<std::string> protocols;
840 registry()->GetRegisteredProtocols(&protocols); 871 registry()->GetRegisteredProtocols(&protocols);
841 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); 872 ASSERT_EQ(static_cast<size_t>(1), protocols.size());
842 } 873 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698