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

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: Reduce differences between orig and updated test. Trying to figure out why tests fail on try server… Created 8 years, 5 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/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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 ProtocolHandlerRegistry* registry_; 239 ProtocolHandlerRegistry* registry_;
194 bool called_; 240 bool called_;
195 content::NotificationRegistrar notification_registrar_; 241 content::NotificationRegistrar notification_registrar_;
196 }; 242 };
197 243
198 } // namespace 244 } // namespace
199 245
200 class ProtocolHandlerRegistryTest : public testing::Test { 246 class ProtocolHandlerRegistryTest : public testing::Test {
201 protected: 247 protected:
202 ProtocolHandlerRegistryTest() 248 ProtocolHandlerRegistryTest()
203 : test_protocol_handler_(CreateProtocolHandler("test", "test")) {} 249 : ui_thread_(BrowserThread::UI, MessageLoop::current()),
250 file_thread_(BrowserThread::FILE),
251 io_thread_(BrowserThread::IO),
252 test_protocol_handler_(CreateProtocolHandler("test", "test")) {}
204 253
205 FakeDelegate* delegate() const { return delegate_; } 254 FakeDelegate* delegate() const { return delegate_; }
255 ProtocolHandlerRegistry* registry() const { return registry_; }
206 TestingProfile* profile() const { return profile_.get(); } 256 TestingProfile* profile() const { return profile_.get(); }
207 PrefService* pref_service() const { return profile_->GetPrefs(); } 257 PrefService* pref_service() const { return profile_->GetPrefs(); }
208 ProtocolHandlerRegistry* registry() const { return registry_.get(); }
209 const ProtocolHandler& test_protocol_handler() const { 258 const ProtocolHandler& test_protocol_handler() const {
210 return test_protocol_handler_; 259 return test_protocol_handler_;
211 } 260 }
212 261
213 ProtocolHandler CreateProtocolHandler(const std::string& protocol, 262 ProtocolHandler CreateProtocolHandler(const std::string& protocol,
214 const GURL& url, 263 const GURL& url,
215 const std::string& title) { 264 const std::string& title) {
216 return ProtocolHandler::CreateProtocolHandler(protocol, url, 265 return ProtocolHandler::CreateProtocolHandler(protocol, url,
217 UTF8ToUTF16(title)); 266 UTF8ToUTF16(title));
218 } 267 }
219 268
220 ProtocolHandler CreateProtocolHandler(const std::string& protocol, 269 ProtocolHandler CreateProtocolHandler(const std::string& protocol,
221 const std::string& name) { 270 const std::string& name) {
222 return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s"), 271 return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s"),
223 name); 272 name);
224 } 273 }
225 274
226 void ReloadProtocolHandlerRegistry() { 275 void RecreateRegistry(bool initilized) {
227 delegate_ = new FakeDelegate(); 276 DestroyRegistry();
228 registry_->Finalize(); 277 CreateRegistry(initilized);
229 registry_ = NULL;
230 registry_ = new ProtocolHandlerRegistry(profile(), delegate());
231 registry_->Load();
232 } 278 }
233 279
234 void ReloadProtocolHandlerRegistryAndInstallDefaultHandler() { 280 // Returns a new registry. Caller assumes ownership for the object
281 void CreateRegistry(bool initilized) {
235 delegate_ = new FakeDelegate(); 282 delegate_ = new FakeDelegate();
236 registry_->Finalize();
237 registry_ = NULL;
238 registry_ = new ProtocolHandlerRegistry(profile(), delegate()); 283 registry_ = new ProtocolHandlerRegistry(profile(), delegate());
239 registry_->AddPredefinedHandler(CreateProtocolHandler( 284 if (initilized) registry_->InitProtocolSettings();
240 "test", GURL("http://test.com/%s"), "Test")); 285 }
241 registry_->Load(); 286
287 void DestroyRegistry() {
288 registry_->Shutdown();
289 delete registry_;
290 // Since registry assumed ownership of delegate_ it handles deletion
291 // of that object.
242 } 292 }
243 293
244 virtual void SetUp() { 294 virtual void SetUp() {
245 ui_message_loop_.reset(new MessageLoopForUI()); 295 // We must explicitly start/stop the FILE thread. Not sure why.
246 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, 296 file_thread_.Start();
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()); 297 profile_.reset(new TestingProfile());
255 profile_->SetPrefService(new TestingPrefService()); 298 profile_->SetPrefService(new TestingPrefService());
256 delegate_ = new FakeDelegate(); 299 CreateRegistry(true);
257 registry_ = new ProtocolHandlerRegistry(profile(), delegate());
258 registry_->Load();
259 test_protocol_handler_ = 300 test_protocol_handler_ =
260 CreateProtocolHandler("test", GURL("http://test.com/%s"), "Test"); 301 CreateProtocolHandler("test", GURL("http://test.com/%s"), "Test");
261
262 ProtocolHandlerRegistry::RegisterPrefs(pref_service()); 302 ProtocolHandlerRegistry::RegisterPrefs(pref_service());
263 } 303 }
264 304
265 virtual void TearDown() { 305 virtual void TearDown() {
266 registry_->Finalize(); 306 DestroyRegistry();
267 registry_ = NULL; 307 // We explicitly start/stop the FILE thread. UI and IO start automatically.
268 io_thread_->Stop(); 308 file_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 } 309 }
275 310
276 bool enabled_io() { 311 MessageLoopForUI ui_message_loop_;
277 return registry()->enabled_io_; 312 content::TestBrowserThread ui_thread_;
278 } 313 content::TestBrowserThread file_thread_;
279 314 content::TestBrowserThread io_thread_;
280 scoped_ptr<MessageLoopForUI> ui_message_loop_; 315 scoped_ptr<TestingProfile> profile_;
281 scoped_ptr<content::TestBrowserThread> ui_thread_;
282 scoped_ptr<content::TestBrowserThread> io_thread_;
283 scoped_ptr<content::TestBrowserThread> file_thread_;
284 316
285 FakeDelegate* delegate_; 317 FakeDelegate* delegate_;
286 scoped_ptr<TestingProfile> profile_; 318 ProtocolHandlerRegistry* registry_;
benwells 2012/06/28 08:15:46 You should use a scoped_ptr for registry_, unless
Steve McKay 2012/06/28 23:05:58 Done.
287 scoped_refptr<ProtocolHandlerRegistry> registry_;
288 ProtocolHandler test_protocol_handler_; 319 ProtocolHandler test_protocol_handler_;
289 }; 320 };
290 321
291 // ProtocolHandlerRegistryTest tests are flaky on Linux & ChromeOS. 322 // ProtocolHandlerRegistryTest tests are flaky on Linux & ChromeOS.
292 // http://crbug.com/133023 323 // http://crbug.com/133023
293 #if defined(OS_LINUX) || defined(OS_CHROMEOS) 324 #if defined(OS_LINUX) || defined(OS_CHROMEOS)
294 #define MAYBE_AcceptProtocolHandlerHandlesProtocol \ 325 #define MAYBE_AcceptProtocolHandlerHandlesProtocol \
295 DISABLED_AcceptProtocolHandlerHandlesProtocol 326 DISABLED_AcceptProtocolHandlerHandlesProtocol
296 #define MAYBE_DeniedProtocolIsntHandledUntilAccepted \ 327 #define MAYBE_DeniedProtocolIsntHandledUntilAccepted \
297 DISABLED_DeniedProtocolIsntHandledUntilAccepted 328 DISABLED_DeniedProtocolIsntHandledUntilAccepted
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 407
377 TEST_F(ProtocolHandlerRegistryTest, SaveAndLoad) { 408 TEST_F(ProtocolHandlerRegistryTest, SaveAndLoad) {
378 ProtocolHandler stuff_protocol_handler( 409 ProtocolHandler stuff_protocol_handler(
379 CreateProtocolHandler("stuff", "stuff")); 410 CreateProtocolHandler("stuff", "stuff"));
380 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); 411 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler());
381 registry()->OnIgnoreRegisterProtocolHandler(stuff_protocol_handler); 412 registry()->OnIgnoreRegisterProtocolHandler(stuff_protocol_handler);
382 413
383 ASSERT_TRUE(registry()->IsHandledProtocol("test")); 414 ASSERT_TRUE(registry()->IsHandledProtocol("test"));
384 ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler)); 415 ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler));
385 delegate()->Reset(); 416 delegate()->Reset();
386 ReloadProtocolHandlerRegistry(); 417 RecreateRegistry(true);
387 ASSERT_TRUE(registry()->IsHandledProtocol("test")); 418 ASSERT_TRUE(registry()->IsHandledProtocol("test"));
388 ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler)); 419 ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler));
389 } 420 }
390 421
391 TEST_F(ProtocolHandlerRegistryTest, TestEnabledDisabled) { 422 TEST_F(ProtocolHandlerRegistryTest, TestEnabledDisabled) {
392 registry()->Disable(); 423 registry()->Disable();
393 ASSERT_FALSE(registry()->enabled()); 424 ASSERT_FALSE(registry()->enabled());
394 registry()->Enable(); 425 registry()->Enable();
395 ASSERT_TRUE(registry()->enabled()); 426 ASSERT_TRUE(registry()->enabled());
396 } 427 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 493
463 TEST_F(ProtocolHandlerRegistryTest, TestDefaultSaveLoad) { 494 TEST_F(ProtocolHandlerRegistryTest, TestDefaultSaveLoad) {
464 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); 495 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
465 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); 496 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2");
466 registry()->OnDenyRegisterProtocolHandler(ph1); 497 registry()->OnDenyRegisterProtocolHandler(ph1);
467 registry()->OnDenyRegisterProtocolHandler(ph2); 498 registry()->OnDenyRegisterProtocolHandler(ph2);
468 499
469 registry()->OnAcceptRegisterProtocolHandler(ph2); 500 registry()->OnAcceptRegisterProtocolHandler(ph2);
470 registry()->Disable(); 501 registry()->Disable();
471 502
472 ReloadProtocolHandlerRegistry(); 503 RecreateRegistry(true);
473 504
474 ASSERT_FALSE(registry()->enabled()); 505 ASSERT_FALSE(registry()->enabled());
475 registry()->Enable(); 506 registry()->Enable();
476 ASSERT_FALSE(registry()->IsDefault(ph1)); 507 ASSERT_FALSE(registry()->IsDefault(ph1));
477 ASSERT_TRUE(registry()->IsDefault(ph2)); 508 ASSERT_TRUE(registry()->IsDefault(ph2));
478 509
479 ReloadProtocolHandlerRegistry(); 510 RecreateRegistry(true);
480 ASSERT_TRUE(registry()->enabled()); 511 ASSERT_TRUE(registry()->enabled());
481 } 512 }
482 513
483 TEST_F(ProtocolHandlerRegistryTest, TestRemoveHandler) { 514 TEST_F(ProtocolHandlerRegistryTest, TestRemoveHandler) {
484 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); 515 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
485 registry()->OnAcceptRegisterProtocolHandler(ph1); 516 registry()->OnAcceptRegisterProtocolHandler(ph1);
486 registry()->OnAcceptRegisterProtocolHandler(ph1); 517 registry()->OnAcceptRegisterProtocolHandler(ph1);
487 518
488 registry()->RemoveHandler(ph1); 519 registry()->RemoveHandler(ph1);
489 ASSERT_FALSE(registry()->IsRegistered(ph1)); 520 ASSERT_FALSE(registry()->IsRegistered(ph1));
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 } 660 }
630 661
631 TEST_F(ProtocolHandlerRegistryTest, TestDisablePreventsHandling) { 662 TEST_F(ProtocolHandlerRegistryTest, TestDisablePreventsHandling) {
632 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); 663 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
633 registry()->OnAcceptRegisterProtocolHandler(ph1); 664 registry()->OnAcceptRegisterProtocolHandler(ph1);
634 ASSERT_TRUE(registry()->IsHandledProtocol("test")); 665 ASSERT_TRUE(registry()->IsHandledProtocol("test"));
635 registry()->Disable(); 666 registry()->Disable();
636 ASSERT_FALSE(registry()->IsHandledProtocol("test")); 667 ASSERT_FALSE(registry()->IsHandledProtocol("test"));
637 } 668 }
638 669
670 // TODO(smckay): This is much more appropriately an integration
671 // test. Make that so, then update the
672 // ShellIntegretion{Delegate,Observer,Worker} test classes we use to fully
673 // isolate this test from the FILE thread.
639 TEST_F(ProtocolHandlerRegistryTest, TestOSRegistration) { 674 TEST_F(ProtocolHandlerRegistryTest, TestOSRegistration) {
640 ProtocolHandler ph_do1 = CreateProtocolHandler("do", "test1"); 675 ProtocolHandler ph_do1 = CreateProtocolHandler("do", "test1");
641 ProtocolHandler ph_do2 = CreateProtocolHandler("do", "test2"); 676 ProtocolHandler ph_do2 = CreateProtocolHandler("do", "test2");
642 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test"); 677 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test");
643 678
644 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("do")); 679 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("do"));
645 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont")); 680 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont"));
646 681
647 registry()->OnAcceptRegisterProtocolHandler(ph_do1); 682 registry()->OnAcceptRegisterProtocolHandler(ph_do1);
648 registry()->OnDenyRegisterProtocolHandler(ph_dont); 683 registry()->OnDenyRegisterProtocolHandler(ph_dont);
649 MessageLoop::current()->Run(); 684 MessageLoop::current()->Run(); // FILE thread needs to run.
650 ASSERT_TRUE(delegate()->IsFakeRegisteredWithOS("do")); 685 ASSERT_TRUE(delegate()->IsFakeRegisteredWithOS("do"));
651 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont")); 686 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont"));
652 687
653 // This should not register with the OS, if it does the delegate 688 // 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 689 // 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. 690 // as it should not go through to the shell worker.
656 registry()->OnAcceptRegisterProtocolHandler(ph_do2); 691 registry()->OnAcceptRegisterProtocolHandler(ph_do2);
657 } 692 }
658 693
659 #if defined(OS_LINUX) 694 #if defined(OS_LINUX)
660 // TODO(benwells): When Linux support is more reliable and 695 // TODO(benwells): When Linux support is more reliable and
661 // http://crbut.com/88255 is fixed this test will pass. 696 // http://crbut.com/88255 is fixed this test will pass.
662 #define MAYBE_TestOSRegistrationFailure FAILS_TestOSRegistrationFailure 697 #define MAYBE_TestOSRegistrationFailure FAILS_TestOSRegistrationFailure
663 #else 698 #else
664 #define MAYBE_TestOSRegistrationFailure TestOSRegistrationFailure 699 #define MAYBE_TestOSRegistrationFailure TestOSRegistrationFailure
665 #endif 700 #endif
666 701
702 // TODO(smckay): This is much more appropriately an integration
703 // test. Make that so, then update the
704 // ShellIntegretion{Delegate,Observer,Worker} test classes we use to fully
705 // isolate this test from the FILE thread.
667 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestOSRegistrationFailure) { 706 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestOSRegistrationFailure) {
668 ProtocolHandler ph_do = CreateProtocolHandler("do", "test1"); 707 ProtocolHandler ph_do = CreateProtocolHandler("do", "test1");
669 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test"); 708 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test");
670 709
671 ASSERT_FALSE(registry()->IsHandledProtocol("do")); 710 ASSERT_FALSE(registry()->IsHandledProtocol("do"));
672 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); 711 ASSERT_FALSE(registry()->IsHandledProtocol("dont"));
673 712
674 registry()->OnAcceptRegisterProtocolHandler(ph_do); 713 registry()->OnAcceptRegisterProtocolHandler(ph_do);
675 MessageLoop::current()->Run(); 714 MessageLoop::current()->Run(); // FILE thread needs to run.
676 delegate()->set_force_os_failure(true); 715 delegate()->set_force_os_failure(true);
677 registry()->OnAcceptRegisterProtocolHandler(ph_dont); 716 registry()->OnAcceptRegisterProtocolHandler(ph_dont);
678 MessageLoop::current()->Run(); 717 MessageLoop::current()->Run(); // FILE thread needs to run.
679 ASSERT_TRUE(registry()->IsHandledProtocol("do")); 718 ASSERT_TRUE(registry()->IsHandledProtocol("do"));
680 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size()); 719 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size());
681 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); 720 ASSERT_FALSE(registry()->IsHandledProtocol("dont"));
682 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size()); 721 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size());
683 } 722 }
684 723
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) { 724 TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) {
695 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); 725 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1");
696 registry()->OnAcceptRegisterProtocolHandler(ph1); 726 registry()->OnAcceptRegisterProtocolHandler(ph1);
697 GURL url("mailto:someone@something.com"); 727 GURL url("mailto:someone@something.com");
698 scoped_refptr<ProtocolHandlerRegistry> r(registry()); 728 net::URLRequestJobFactory::Interceptor* interceptor =
699 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 729 registry()->CreateURLInterceptor();
700 base::Bind(MakeRequest, url, r));
701 MessageLoop::current()->Run();
702 }
703 730
704 static void CheckIsHandled(const std::string& scheme, bool expected, 731 AssertIntercepted(url, interceptor);
705 ProtocolHandlerRegistry* registry) {
706 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
707 MessageLoop::QuitClosure());
708 ASSERT_EQ(expected, registry->IsHandledProtocolIO(scheme));
709 } 732 }
710 733
711 TEST_F(ProtocolHandlerRegistryTest, 734 TEST_F(ProtocolHandlerRegistryTest,
712 MAYBE_TestIsHandledProtocolWorksOnIOThread) { 735 MAYBE_TestIsHandledProtocolWorksOnIOThread) {
713 std::string scheme("mailto"); 736 std::string scheme("mailto");
714 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); 737 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1");
715 registry()->OnAcceptRegisterProtocolHandler(ph1); 738 registry()->OnAcceptRegisterProtocolHandler(ph1);
716 scoped_refptr<ProtocolHandlerRegistry> r(registry()); 739 net::URLRequestJobFactory::Interceptor* interceptor =
717 BrowserThread::PostTask( 740 registry()->CreateURLInterceptor();
718 BrowserThread::IO, 741
719 FROM_HERE, 742 AssertWillHandle(scheme, true, interceptor);
720 base::Bind(CheckIsHandled, scheme, true, r));
721 } 743 }
722 744
723 TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultFallsBackToOldDefault) { 745 TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultFallsBackToOldDefault) {
724 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); 746 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1");
725 ProtocolHandler ph2 = CreateProtocolHandler("mailto", "test2"); 747 ProtocolHandler ph2 = CreateProtocolHandler("mailto", "test2");
726 ProtocolHandler ph3 = CreateProtocolHandler("mailto", "test3"); 748 ProtocolHandler ph3 = CreateProtocolHandler("mailto", "test3");
727 registry()->OnAcceptRegisterProtocolHandler(ph1); 749 registry()->OnAcceptRegisterProtocolHandler(ph1);
728 registry()->OnAcceptRegisterProtocolHandler(ph2); 750 registry()->OnAcceptRegisterProtocolHandler(ph2);
729 registry()->OnAcceptRegisterProtocolHandler(ph3); 751 registry()->OnAcceptRegisterProtocolHandler(ph3);
730 752
(...skipping 23 matching lines...) Expand all
754 776
755 ASSERT_EQ(ph2, handlers[0]); 777 ASSERT_EQ(ph2, handlers[0]);
756 ASSERT_EQ(ph1, handlers[1]); 778 ASSERT_EQ(ph1, handlers[1]);
757 } 779 }
758 780
759 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestClearDefaultGetsPropagatedToIO) { 781 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestClearDefaultGetsPropagatedToIO) {
760 std::string scheme("mailto"); 782 std::string scheme("mailto");
761 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); 783 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1");
762 registry()->OnAcceptRegisterProtocolHandler(ph1); 784 registry()->OnAcceptRegisterProtocolHandler(ph1);
763 registry()->ClearDefault(scheme); 785 registry()->ClearDefault(scheme);
764 scoped_refptr<ProtocolHandlerRegistry> r(registry()); 786 net::URLRequestJobFactory::Interceptor* interceptor =
787 registry()->CreateURLInterceptor();
765 788
766 BrowserThread::PostTask( 789 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 } 790 }
776 791
777 TEST_F(ProtocolHandlerRegistryTest, TestLoadEnabledGetsPropogatedToIO) { 792 TEST_F(ProtocolHandlerRegistryTest, TestLoadEnabledGetsPropogatedToIO) {
793 std::string mailto("mailto");
794 ProtocolHandler ph1 = CreateProtocolHandler(mailto, "MailtoHandler");
795 registry()->OnAcceptRegisterProtocolHandler(ph1);
796
797 AssertWillHandle(mailto, true, registry()->CreateURLInterceptor());
778 registry()->Disable(); 798 registry()->Disable();
779 ReloadProtocolHandlerRegistry(); 799 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 } 800 }
785 801
786 TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) { 802 TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) {
787 ProtocolHandler ph1 = CreateProtocolHandler("mailto", 803 ProtocolHandler ph1 = CreateProtocolHandler("mailto",
788 GURL("http://test.com/%s"), "test1"); 804 GURL("http://test.com/%s"), "test1");
789 ProtocolHandler ph2 = CreateProtocolHandler("mailto", 805 ProtocolHandler ph2 = CreateProtocolHandler("mailto",
790 GURL("http://test.com/updated-url/%s"), "test2"); 806 GURL("http://test.com/updated-url/%s"), "test2");
791 registry()->OnAcceptRegisterProtocolHandler(ph1); 807 registry()->OnAcceptRegisterProtocolHandler(ph1);
792 ASSERT_TRUE(registry()->AttemptReplace(ph2)); 808 ASSERT_TRUE(registry()->AttemptReplace(ph2));
793 const ProtocolHandler& handler(registry()->GetHandlerFor("mailto")); 809 const ProtocolHandler& handler(registry()->GetHandlerFor("mailto"));
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
837 ph1.IsSameOrigin(ph2)); 853 ph1.IsSameOrigin(ph2));
838 ASSERT_EQ(ph1.url().GetOrigin() == ph2.url().GetOrigin(), 854 ASSERT_EQ(ph1.url().GetOrigin() == ph2.url().GetOrigin(),
839 ph2.IsSameOrigin(ph1)); 855 ph2.IsSameOrigin(ph1));
840 ASSERT_EQ(ph2.url().GetOrigin() == ph3.url().GetOrigin(), 856 ASSERT_EQ(ph2.url().GetOrigin() == ph3.url().GetOrigin(),
841 ph2.IsSameOrigin(ph3)); 857 ph2.IsSameOrigin(ph3));
842 ASSERT_EQ(ph3.url().GetOrigin() == ph2.url().GetOrigin(), 858 ASSERT_EQ(ph3.url().GetOrigin() == ph2.url().GetOrigin(),
843 ph3.IsSameOrigin(ph2)); 859 ph3.IsSameOrigin(ph2));
844 } 860 }
845 861
846 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { 862 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) {
847 ReloadProtocolHandlerRegistryAndInstallDefaultHandler(); 863 RecreateRegistry(false);
864 registry_->AddPredefinedHandler(CreateProtocolHandler(
865 "test", GURL("http://test.com/%s"), "Test"));
866 registry_->InitProtocolSettings();
848 std::vector<std::string> protocols; 867 std::vector<std::string> protocols;
849 registry()->GetRegisteredProtocols(&protocols); 868 registry()->GetRegisteredProtocols(&protocols);
850 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); 869 ASSERT_EQ(static_cast<size_t>(1), protocols.size());
851 } 870 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698