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
3 // found in the LICENSE file.
5 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/prefs/pref_service_syncable.h"
15 #include "chrome/common/custom_handlers/protocol_handler.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/test/base/testing_browser_process.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "components/pref_registry/pref_registry_syncable.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "content/public/browser/notification_source.h"
23 #include "content/public/test/test_browser_thread.h"
24 #include "content/public/test/test_renderer_host.h"
25 #include "net/base/request_priority.h"
26 #include "net/url_request/url_request.h"
27 #include "net/url_request/url_request_context.h"
28 #include "testing/gtest/include/gtest/gtest.h"
30 using content::BrowserThread
;
34 void AssertInterceptedIO(
36 net::URLRequestJobFactory
* interceptor
) {
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
38 net::URLRequestContext context
;
39 net::URLRequest
request(url
, net::DEFAULT_PRIORITY
, NULL
, &context
);
40 scoped_refptr
<net::URLRequestJob
> job
=
41 interceptor
->MaybeCreateJobWithProtocolHandler(
42 url
.scheme(), &request
, context
.network_delegate());
43 ASSERT_TRUE(job
.get() != NULL
);
46 void AssertIntercepted(
48 net::URLRequestJobFactory
* interceptor
) {
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
50 BrowserThread::PostTask(BrowserThread::IO
,
52 base::Bind(AssertInterceptedIO
,
54 base::Unretained(interceptor
)));
55 base::MessageLoop::current()->RunUntilIdle();
58 // FakeURLRequestJobFactory returns NULL for all job creation requests and false
59 // for all IsHandled*() requests. FakeURLRequestJobFactory can be chained to
60 // ProtocolHandlerRegistry::JobInterceptorFactory so the result of
61 // MaybeCreateJobWithProtocolHandler() indicates whether the
62 // ProtocolHandlerRegistry properly handled a job creation request.
63 class FakeURLRequestJobFactory
: public net::URLRequestJobFactory
{
64 // net::URLRequestJobFactory implementation:
65 virtual net::URLRequestJob
* MaybeCreateJobWithProtocolHandler(
66 const std::string
& scheme
,
67 net::URLRequest
* request
,
68 net::NetworkDelegate
* network_delegate
) const OVERRIDE
{
71 virtual bool IsHandledProtocol(const std::string
& scheme
) const OVERRIDE
{
74 virtual bool IsHandledURL(const GURL
& url
) const OVERRIDE
{
77 virtual bool IsSafeRedirectTarget(const GURL
& location
) const OVERRIDE
{
82 void AssertWillHandleIO(
83 const std::string
& scheme
,
85 ProtocolHandlerRegistry::JobInterceptorFactory
* interceptor
) {
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
87 interceptor
->Chain(scoped_ptr
<net::URLRequestJobFactory
>(
88 new FakeURLRequestJobFactory()));
89 ASSERT_EQ(expected
, interceptor
->IsHandledProtocol(scheme
));
90 interceptor
->Chain(scoped_ptr
<net::URLRequestJobFactory
>());
93 void AssertWillHandle(
94 const std::string
& scheme
,
96 ProtocolHandlerRegistry::JobInterceptorFactory
* interceptor
) {
97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
98 BrowserThread::PostTask(BrowserThread::IO
,
100 base::Bind(AssertWillHandleIO
,
103 base::Unretained(interceptor
)));
104 base::MessageLoop::current()->RunUntilIdle();
107 base::DictionaryValue
* GetProtocolHandlerValue(std::string protocol
,
109 base::DictionaryValue
* value
= new base::DictionaryValue();
110 value
->SetString("protocol", protocol
);
111 value
->SetString("url", url
);
115 base::DictionaryValue
* GetProtocolHandlerValueWithDefault(std::string protocol
,
118 base::DictionaryValue
* value
= GetProtocolHandlerValue(protocol
, url
);
119 value
->SetBoolean("default", is_default
);
123 class FakeDelegate
: public ProtocolHandlerRegistry::Delegate
{
125 FakeDelegate() : force_os_failure_(false) {}
126 virtual ~FakeDelegate() { }
127 virtual void RegisterExternalHandler(const std::string
& protocol
) OVERRIDE
{
129 registered_protocols_
.find(protocol
) == registered_protocols_
.end());
130 registered_protocols_
.insert(protocol
);
133 virtual void DeregisterExternalHandler(const std::string
& protocol
) OVERRIDE
{
134 registered_protocols_
.erase(protocol
);
137 virtual ShellIntegration::DefaultProtocolClientWorker
* CreateShellWorker(
138 ShellIntegration::DefaultWebClientObserver
* observer
,
139 const std::string
& protocol
) OVERRIDE
;
141 virtual ProtocolHandlerRegistry::DefaultClientObserver
* CreateShellObserver(
142 ProtocolHandlerRegistry
* registry
) OVERRIDE
;
144 virtual void RegisterWithOSAsDefaultClient(
145 const std::string
& protocol
,
146 ProtocolHandlerRegistry
* reg
) OVERRIDE
{
147 ProtocolHandlerRegistry::Delegate::RegisterWithOSAsDefaultClient(protocol
,
149 ASSERT_FALSE(IsFakeRegisteredWithOS(protocol
));
152 virtual bool IsExternalHandlerRegistered(
153 const std::string
& protocol
) OVERRIDE
{
154 return registered_protocols_
.find(protocol
) != registered_protocols_
.end();
157 bool IsFakeRegisteredWithOS(const std::string
& protocol
) {
158 return os_registered_protocols_
.find(protocol
) !=
159 os_registered_protocols_
.end();
162 void FakeRegisterWithOS(const std::string
& protocol
) {
163 os_registered_protocols_
.insert(protocol
);
167 registered_protocols_
.clear();
168 os_registered_protocols_
.clear();
169 force_os_failure_
= false;
172 void set_force_os_failure(bool force
) { force_os_failure_
= force
; }
174 bool force_os_failure() { return force_os_failure_
; }
177 std::set
<std::string
> registered_protocols_
;
178 std::set
<std::string
> os_registered_protocols_
;
179 bool force_os_failure_
;
182 class FakeClientObserver
183 : public ProtocolHandlerRegistry::DefaultClientObserver
{
185 FakeClientObserver(ProtocolHandlerRegistry
* registry
,
186 FakeDelegate
* registry_delegate
)
187 : ProtocolHandlerRegistry::DefaultClientObserver(registry
),
188 delegate_(registry_delegate
) {}
190 virtual void SetDefaultWebClientUIState(
191 ShellIntegration::DefaultWebClientUIState state
) OVERRIDE
{
192 ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState(
194 if (state
== ShellIntegration::STATE_IS_DEFAULT
) {
195 delegate_
->FakeRegisterWithOS(worker_
->protocol());
197 if (state
!= ShellIntegration::STATE_PROCESSING
) {
198 base::MessageLoop::current()->Quit();
203 FakeDelegate
* delegate_
;
206 class FakeProtocolClientWorker
207 : public ShellIntegration::DefaultProtocolClientWorker
{
209 FakeProtocolClientWorker(ShellIntegration::DefaultWebClientObserver
* observer
,
210 const std::string
& protocol
,
212 : ShellIntegration::DefaultProtocolClientWorker(observer
, protocol
),
213 force_failure_(force_failure
) {}
216 virtual ~FakeProtocolClientWorker() {}
218 virtual ShellIntegration::DefaultWebClientState
CheckIsDefault() OVERRIDE
{
219 if (force_failure_
) {
220 return ShellIntegration::NOT_DEFAULT
;
222 return ShellIntegration::IS_DEFAULT
;
226 virtual bool SetAsDefault(bool interactive_permitted
) OVERRIDE
{
234 ProtocolHandlerRegistry::DefaultClientObserver
*
235 FakeDelegate::CreateShellObserver(ProtocolHandlerRegistry
* registry
) {
236 return new FakeClientObserver(registry
, this);
239 ShellIntegration::DefaultProtocolClientWorker
* FakeDelegate::CreateShellWorker(
240 ShellIntegration::DefaultWebClientObserver
* observer
,
241 const std::string
& protocol
) {
242 return new FakeProtocolClientWorker(observer
, protocol
, force_os_failure_
);
245 class NotificationCounter
: public content::NotificationObserver
{
247 explicit NotificationCounter(Profile
* profile
)
249 notification_registrar_() {
250 notification_registrar_
.Add(this,
251 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED
,
252 content::Source
<Profile
>(profile
));
255 int events() { return events_
; }
256 bool notified() { return events_
> 0; }
257 void Clear() { events_
= 0; }
258 virtual void Observe(int type
,
259 const content::NotificationSource
& source
,
260 const content::NotificationDetails
& details
) OVERRIDE
{
265 content::NotificationRegistrar notification_registrar_
;
268 class QueryProtocolHandlerOnChange
269 : public content::NotificationObserver
{
271 QueryProtocolHandlerOnChange(Profile
* profile
,
272 ProtocolHandlerRegistry
* registry
)
273 : local_registry_(registry
),
275 notification_registrar_() {
276 notification_registrar_
.Add(this,
277 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED
,
278 content::Source
<Profile
>(profile
));
281 virtual void Observe(int type
,
282 const content::NotificationSource
& source
,
283 const content::NotificationDetails
& details
) OVERRIDE
{
284 std::vector
<std::string
> output
;
285 local_registry_
->GetRegisteredProtocols(&output
);
289 ProtocolHandlerRegistry
* local_registry_
;
291 content::NotificationRegistrar notification_registrar_
;
294 // URLRequest DCHECKS that the current MessageLoop is IO. It does this because
295 // it can't check the thread id (since net can't depend on content.) We want
296 // to harness our tests so all threads use the same loop allowing us to
297 // guarantee all messages are processed.) By overriding the IsType method
298 // we basically ignore the supplied message loop type, and instead infer
299 // our type based on the current thread. GO DEPENDENCY INJECTION!
300 class TestMessageLoop
: public base::MessageLoop
{
303 virtual ~TestMessageLoop() {}
304 virtual bool IsType(base::MessageLoop::Type type
) const OVERRIDE
{
306 case base::MessageLoop::TYPE_UI
:
307 return BrowserThread::CurrentlyOn(BrowserThread::UI
);
308 case base::MessageLoop::TYPE_IO
:
309 return BrowserThread::CurrentlyOn(BrowserThread::IO
);
310 #if defined(OS_ANDROID)
311 case base::MessageLoop::TYPE_JAVA
: // fall-through
312 #endif // defined(OS_ANDROID)
313 case base::MessageLoop::TYPE_CUSTOM
:
314 case base::MessageLoop::TYPE_DEFAULT
:
315 return !BrowserThread::CurrentlyOn(BrowserThread::UI
) &&
316 !BrowserThread::CurrentlyOn(BrowserThread::IO
);
324 class ProtocolHandlerRegistryTest
: public testing::Test
{
326 ProtocolHandlerRegistryTest()
327 : ui_thread_(BrowserThread::UI
, &loop_
),
328 file_thread_(BrowserThread::FILE, &loop_
),
329 io_thread_(BrowserThread::IO
, &loop_
),
330 test_protocol_handler_(CreateProtocolHandler("test", "test")) {}
332 FakeDelegate
* delegate() const { return delegate_
; }
333 ProtocolHandlerRegistry
* registry() { return registry_
.get(); }
334 TestingProfile
* profile() const { return profile_
.get(); }
335 const ProtocolHandler
& test_protocol_handler() const {
336 return test_protocol_handler_
;
339 ProtocolHandler
CreateProtocolHandler(const std::string
& protocol
,
341 return ProtocolHandler::CreateProtocolHandler(protocol
, url
);
344 ProtocolHandler
CreateProtocolHandler(const std::string
& protocol
,
345 const std::string
& name
) {
346 return CreateProtocolHandler(protocol
, GURL("http://" + name
+ "/%s"));
349 void RecreateRegistry(bool initialize
) {
351 SetUpRegistry(initialize
);
354 int InPrefHandlerCount() {
355 const base::ListValue
* in_pref_handlers
=
356 profile()->GetPrefs()->GetList(prefs::kRegisteredProtocolHandlers
);
357 return static_cast<int>(in_pref_handlers
->GetSize());
360 int InMemoryHandlerCount() {
361 int in_memory_handler_count
= 0;
362 ProtocolHandlerRegistry::ProtocolHandlerMultiMap::iterator it
=
363 registry()->protocol_handlers_
.begin();
364 for (; it
!= registry()->protocol_handlers_
.end(); ++it
)
365 in_memory_handler_count
+= it
->second
.size();
366 return in_memory_handler_count
;
369 int InPrefIgnoredHandlerCount() {
370 const base::ListValue
* in_pref_ignored_handlers
=
371 profile()->GetPrefs()->GetList(prefs::kIgnoredProtocolHandlers
);
372 return static_cast<int>(in_pref_ignored_handlers
->GetSize());
375 int InMemoryIgnoredHandlerCount() {
376 int in_memory_ignored_handler_count
= 0;
377 ProtocolHandlerRegistry::ProtocolHandlerList::iterator it
=
378 registry()->ignored_protocol_handlers_
.begin();
379 for (; it
!= registry()->ignored_protocol_handlers_
.end(); ++it
)
380 in_memory_ignored_handler_count
++;
381 return in_memory_ignored_handler_count
;
384 // Returns a new registry, initializing it if |initialize| is true.
385 // Caller assumes ownership for the object
386 void SetUpRegistry(bool initialize
) {
387 delegate_
= new FakeDelegate();
388 registry_
.reset(new ProtocolHandlerRegistry(profile(), delegate()));
389 if (initialize
) registry_
->InitProtocolSettings();
392 void TeadDownRegistry() {
393 registry_
->Shutdown();
395 // Registry owns the delegate_ it handles deletion of that object.
398 virtual void SetUp() {
399 profile_
.reset(new TestingProfile());
400 CHECK(profile_
->GetPrefs());
402 test_protocol_handler_
=
403 CreateProtocolHandler("test", GURL("http://test.com/%s"));
406 virtual void TearDown() {
410 TestMessageLoop loop_
;
413 content::TestBrowserThread ui_thread_
;
414 content::TestBrowserThread file_thread_
;
415 content::TestBrowserThread io_thread_
;
417 scoped_ptr
<TestingProfile
> profile_
;
418 FakeDelegate
* delegate_
; // Registry assumes ownership of delegate_.
419 scoped_ptr
<ProtocolHandlerRegistry
> registry_
;
420 ProtocolHandler test_protocol_handler_
;
423 // ProtocolHandlerRegistryTest tests are flaky on Linux & ChromeOS.
424 // http://crbug.com/133023
425 #if defined(OS_LINUX)
426 #define MAYBE_AcceptProtocolHandlerHandlesProtocol \
427 DISABLED_AcceptProtocolHandlerHandlesProtocol
428 #define MAYBE_DeniedProtocolIsntHandledUntilAccepted \
429 DISABLED_DeniedProtocolIsntHandledUntilAccepted
430 #define MAYBE_TestStartsAsDefault DISABLED_TestStartsAsDefault
431 #define MAYBE_TestRemoveHandlerRemovesDefault \
432 DISABLED_TestRemoveHandlerRemovesDefault
433 #define MAYBE_TestClearDefaultGetsPropagatedToIO \
434 DISABLED_TestClearDefaultGetsPropagatedToIO
435 #define MAYBE_TestIsHandledProtocolWorksOnIOThread \
436 DISABLED_TestIsHandledProtocolWorksOnIOThread
437 #define MAYBE_TestInstallDefaultHandler \
438 DISABLED_TestInstallDefaultHandler
440 #define MAYBE_AcceptProtocolHandlerHandlesProtocol \
441 AcceptProtocolHandlerHandlesProtocol
442 #define MAYBE_DeniedProtocolIsntHandledUntilAccepted \
443 DeniedProtocolIsntHandledUntilAccepted
444 #define MAYBE_TestStartsAsDefault TestStartsAsDefault
445 #define MAYBE_TestRemoveHandlerRemovesDefault TestRemoveHandlerRemovesDefault
446 #define MAYBE_TestClearDefaultGetsPropagatedToIO \
447 TestClearDefaultGetsPropagatedToIO
448 #define MAYBE_TestIsHandledProtocolWorksOnIOThread \
449 TestIsHandledProtocolWorksOnIOThread
450 #define MAYBE_TestInstallDefaultHandler TestInstallDefaultHandler
451 #endif // defined(OS_LINUX)
453 TEST_F(ProtocolHandlerRegistryTest
,
454 MAYBE_AcceptProtocolHandlerHandlesProtocol
) {
455 ASSERT_FALSE(registry()->IsHandledProtocol("test"));
456 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler());
457 ASSERT_TRUE(registry()->IsHandledProtocol("test"));
460 TEST_F(ProtocolHandlerRegistryTest
,
461 MAYBE_DeniedProtocolIsntHandledUntilAccepted
) {
462 registry()->OnDenyRegisterProtocolHandler(test_protocol_handler());
463 ASSERT_FALSE(registry()->IsHandledProtocol("test"));
464 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler());
465 ASSERT_TRUE(registry()->IsHandledProtocol("test"));
468 TEST_F(ProtocolHandlerRegistryTest
, ClearDefaultMakesProtocolNotHandled
) {
469 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler());
470 registry()->ClearDefault("test");
471 ASSERT_FALSE(registry()->IsHandledProtocol("test"));
472 ASSERT_TRUE(registry()->GetHandlerFor("test").IsEmpty());
475 TEST_F(ProtocolHandlerRegistryTest
, DisableDeregistersProtocolHandlers
) {
476 ASSERT_FALSE(delegate()->IsExternalHandlerRegistered("test"));
477 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler());
478 ASSERT_TRUE(delegate()->IsExternalHandlerRegistered("test"));
480 registry()->Disable();
481 ASSERT_FALSE(delegate()->IsExternalHandlerRegistered("test"));
482 registry()->Enable();
483 ASSERT_TRUE(delegate()->IsExternalHandlerRegistered("test"));
486 TEST_F(ProtocolHandlerRegistryTest
, IgnoreProtocolHandler
) {
487 registry()->OnIgnoreRegisterProtocolHandler(test_protocol_handler());
488 ASSERT_TRUE(registry()->IsIgnored(test_protocol_handler()));
490 registry()->RemoveIgnoredHandler(test_protocol_handler());
491 ASSERT_FALSE(registry()->IsIgnored(test_protocol_handler()));
494 TEST_F(ProtocolHandlerRegistryTest
, IgnoreEquivalentProtocolHandler
) {
495 ProtocolHandler ph1
= CreateProtocolHandler("test", GURL("http://test/%s"));
496 ProtocolHandler ph2
= CreateProtocolHandler("test", GURL("http://test/%s"));
498 registry()->OnIgnoreRegisterProtocolHandler(ph1
);
499 ASSERT_TRUE(registry()->IsIgnored(ph1
));
500 ASSERT_TRUE(registry()->HasIgnoredEquivalent(ph2
));
502 registry()->RemoveIgnoredHandler(ph1
);
503 ASSERT_FALSE(registry()->IsIgnored(ph1
));
504 ASSERT_FALSE(registry()->HasIgnoredEquivalent(ph2
));
507 TEST_F(ProtocolHandlerRegistryTest
, SaveAndLoad
) {
508 ProtocolHandler
stuff_protocol_handler(
509 CreateProtocolHandler("stuff", "stuff"));
510 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler());
511 registry()->OnIgnoreRegisterProtocolHandler(stuff_protocol_handler
);
513 ASSERT_TRUE(registry()->IsHandledProtocol("test"));
514 ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler
));
516 RecreateRegistry(true);
517 ASSERT_TRUE(registry()->IsHandledProtocol("test"));
518 ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler
));
521 TEST_F(ProtocolHandlerRegistryTest
, TestEnabledDisabled
) {
522 registry()->Disable();
523 ASSERT_FALSE(registry()->enabled());
524 registry()->Enable();
525 ASSERT_TRUE(registry()->enabled());
528 TEST_F(ProtocolHandlerRegistryTest
,
529 DisallowRegisteringExternallyHandledProtocols
) {
530 delegate()->RegisterExternalHandler("test");
531 ASSERT_FALSE(registry()->CanSchemeBeOverridden("test"));
534 TEST_F(ProtocolHandlerRegistryTest
, RemovingHandlerMeansItCanBeAddedAgain
) {
535 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler());
536 ASSERT_TRUE(registry()->CanSchemeBeOverridden("test"));
537 registry()->RemoveHandler(test_protocol_handler());
538 ASSERT_TRUE(registry()->CanSchemeBeOverridden("test"));
541 TEST_F(ProtocolHandlerRegistryTest
, MAYBE_TestStartsAsDefault
) {
542 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler());
543 ASSERT_TRUE(registry()->IsDefault(test_protocol_handler()));
546 TEST_F(ProtocolHandlerRegistryTest
, TestClearDefault
) {
547 ProtocolHandler ph1
= CreateProtocolHandler("test", "test1");
548 ProtocolHandler ph2
= CreateProtocolHandler("test", "test2");
549 registry()->OnAcceptRegisterProtocolHandler(ph1
);
550 registry()->OnAcceptRegisterProtocolHandler(ph2
);
552 registry()->OnAcceptRegisterProtocolHandler(ph1
);
553 registry()->ClearDefault("test");
554 ASSERT_FALSE(registry()->IsDefault(ph1
));
555 ASSERT_FALSE(registry()->IsDefault(ph2
));
558 TEST_F(ProtocolHandlerRegistryTest
, TestGetHandlerFor
) {
559 ProtocolHandler ph1
= CreateProtocolHandler("test", "test1");
560 ProtocolHandler ph2
= CreateProtocolHandler("test", "test2");
561 registry()->OnAcceptRegisterProtocolHandler(ph1
);
562 registry()->OnAcceptRegisterProtocolHandler(ph2
);
564 registry()->OnAcceptRegisterProtocolHandler(ph2
);
565 ASSERT_EQ(ph2
, registry()->GetHandlerFor("test"));
566 ASSERT_TRUE(registry()->IsHandledProtocol("test"));
569 TEST_F(ProtocolHandlerRegistryTest
, TestMostRecentHandlerIsDefault
) {
570 ProtocolHandler ph1
= CreateProtocolHandler("test", "test1");
571 ProtocolHandler ph2
= CreateProtocolHandler("test", "test2");
572 registry()->OnAcceptRegisterProtocolHandler(ph1
);
573 registry()->OnAcceptRegisterProtocolHandler(ph2
);
574 ASSERT_FALSE(registry()->IsDefault(ph1
));
575 ASSERT_TRUE(registry()->IsDefault(ph2
));
578 TEST_F(ProtocolHandlerRegistryTest
, TestOnAcceptRegisterProtocolHandler
) {
579 ProtocolHandler ph1
= CreateProtocolHandler("test", "test1");
580 ProtocolHandler ph2
= CreateProtocolHandler("test", "test2");
581 registry()->OnAcceptRegisterProtocolHandler(ph1
);
582 registry()->OnAcceptRegisterProtocolHandler(ph2
);
584 registry()->OnAcceptRegisterProtocolHandler(ph1
);
585 ASSERT_TRUE(registry()->IsDefault(ph1
));
586 ASSERT_FALSE(registry()->IsDefault(ph2
));
588 registry()->OnAcceptRegisterProtocolHandler(ph2
);
589 ASSERT_FALSE(registry()->IsDefault(ph1
));
590 ASSERT_TRUE(registry()->IsDefault(ph2
));
593 TEST_F(ProtocolHandlerRegistryTest
, TestDefaultSaveLoad
) {
594 ProtocolHandler ph1
= CreateProtocolHandler("test", "test1");
595 ProtocolHandler ph2
= CreateProtocolHandler("test", "test2");
596 registry()->OnDenyRegisterProtocolHandler(ph1
);
597 registry()->OnDenyRegisterProtocolHandler(ph2
);
599 registry()->OnAcceptRegisterProtocolHandler(ph2
);
600 registry()->Disable();
602 RecreateRegistry(true);
604 ASSERT_FALSE(registry()->enabled());
605 registry()->Enable();
606 ASSERT_FALSE(registry()->IsDefault(ph1
));
607 ASSERT_TRUE(registry()->IsDefault(ph2
));
609 RecreateRegistry(true);
610 ASSERT_TRUE(registry()->enabled());
613 TEST_F(ProtocolHandlerRegistryTest
, TestRemoveHandler
) {
614 ProtocolHandler ph1
= CreateProtocolHandler("test", "test1");
615 registry()->OnAcceptRegisterProtocolHandler(ph1
);
616 registry()->OnAcceptRegisterProtocolHandler(ph1
);
618 registry()->RemoveHandler(ph1
);
619 ASSERT_FALSE(registry()->IsRegistered(ph1
));
620 ASSERT_FALSE(registry()->IsHandledProtocol("test"));
623 TEST_F(ProtocolHandlerRegistryTest
, TestIsRegistered
) {
624 ProtocolHandler ph1
= CreateProtocolHandler("test", "test1");
625 ProtocolHandler ph2
= CreateProtocolHandler("test", "test2");
626 registry()->OnAcceptRegisterProtocolHandler(ph1
);
627 registry()->OnAcceptRegisterProtocolHandler(ph2
);
629 ASSERT_TRUE(registry()->IsRegistered(ph1
));
632 TEST_F(ProtocolHandlerRegistryTest
, TestIsEquivalentRegistered
) {
633 ProtocolHandler ph1
= CreateProtocolHandler("test", GURL("http://test/%s"));
634 ProtocolHandler ph2
= CreateProtocolHandler("test", GURL("http://test/%s"));
635 registry()->OnAcceptRegisterProtocolHandler(ph1
);
637 ASSERT_TRUE(registry()->IsRegistered(ph1
));
638 ASSERT_TRUE(registry()->HasRegisteredEquivalent(ph2
));
641 TEST_F(ProtocolHandlerRegistryTest
, TestSilentlyRegisterHandler
) {
642 ProtocolHandler ph1
= CreateProtocolHandler("test", GURL("http://test/1/%s"));
643 ProtocolHandler ph2
= CreateProtocolHandler("test", GURL("http://test/2/%s"));
644 ProtocolHandler ph3
= CreateProtocolHandler("ignore", GURL("http://test/%s"));
645 ProtocolHandler ph4
= CreateProtocolHandler("ignore", GURL("http://test/%s"));
647 ASSERT_FALSE(registry()->SilentlyHandleRegisterHandlerRequest(ph1
));
648 ASSERT_FALSE(registry()->IsRegistered(ph1
));
650 registry()->OnAcceptRegisterProtocolHandler(ph1
);
651 ASSERT_TRUE(registry()->IsRegistered(ph1
));
653 ASSERT_TRUE(registry()->SilentlyHandleRegisterHandlerRequest(ph2
));
654 ASSERT_FALSE(registry()->IsRegistered(ph1
));
655 ASSERT_TRUE(registry()->IsRegistered(ph2
));
657 ASSERT_FALSE(registry()->SilentlyHandleRegisterHandlerRequest(ph3
));
658 ASSERT_FALSE(registry()->IsRegistered(ph3
));
660 registry()->OnIgnoreRegisterProtocolHandler(ph3
);
661 ASSERT_FALSE(registry()->IsRegistered(ph3
));
662 ASSERT_TRUE(registry()->IsIgnored(ph3
));
664 ASSERT_TRUE(registry()->SilentlyHandleRegisterHandlerRequest(ph4
));
665 ASSERT_FALSE(registry()->IsRegistered(ph4
));
666 ASSERT_TRUE(registry()->HasIgnoredEquivalent(ph4
));
669 TEST_F(ProtocolHandlerRegistryTest
, MAYBE_TestRemoveHandlerRemovesDefault
) {
670 ProtocolHandler ph1
= CreateProtocolHandler("test", "test1");
671 ProtocolHandler ph2
= CreateProtocolHandler("test", "test2");
672 ProtocolHandler ph3
= CreateProtocolHandler("test", "test3");
674 registry()->OnAcceptRegisterProtocolHandler(ph1
);
675 registry()->OnAcceptRegisterProtocolHandler(ph2
);
676 registry()->OnAcceptRegisterProtocolHandler(ph3
);
678 registry()->OnAcceptRegisterProtocolHandler(ph1
);
679 registry()->RemoveHandler(ph1
);
680 ASSERT_FALSE(registry()->IsDefault(ph1
));
683 TEST_F(ProtocolHandlerRegistryTest
, TestGetHandlersFor
) {
684 ProtocolHandler ph1
= CreateProtocolHandler("test", "test1");
685 ProtocolHandler ph2
= CreateProtocolHandler("test", "test2");
686 ProtocolHandler ph3
= CreateProtocolHandler("test", "test3");
687 registry()->OnAcceptRegisterProtocolHandler(ph1
);
688 registry()->OnAcceptRegisterProtocolHandler(ph2
);
689 registry()->OnAcceptRegisterProtocolHandler(ph3
);
691 ProtocolHandlerRegistry::ProtocolHandlerList handlers
=
692 registry()->GetHandlersFor("test");
693 ASSERT_EQ(static_cast<size_t>(3), handlers
.size());
695 ASSERT_EQ(ph3
, handlers
[0]);
696 ASSERT_EQ(ph2
, handlers
[1]);
697 ASSERT_EQ(ph1
, handlers
[2]);
700 TEST_F(ProtocolHandlerRegistryTest
, TestGetRegisteredProtocols
) {
701 std::vector
<std::string
> protocols
;
702 registry()->GetRegisteredProtocols(&protocols
);
703 ASSERT_EQ(static_cast<size_t>(0), protocols
.size());
705 registry()->GetHandlersFor("test");
708 registry()->GetRegisteredProtocols(&protocols
);
709 ASSERT_EQ(static_cast<size_t>(0), protocols
.size());
712 TEST_F(ProtocolHandlerRegistryTest
, TestIsHandledProtocol
) {
713 registry()->GetHandlersFor("test");
714 ASSERT_FALSE(registry()->IsHandledProtocol("test"));
717 TEST_F(ProtocolHandlerRegistryTest
, TestNotifications
) {
718 ProtocolHandler ph1
= CreateProtocolHandler("test", "test1");
719 NotificationCounter
counter(profile());
721 registry()->OnAcceptRegisterProtocolHandler(ph1
);
722 ASSERT_TRUE(counter
.notified());
725 registry()->Disable();
726 ASSERT_TRUE(counter
.notified());
729 registry()->Enable();
730 ASSERT_TRUE(counter
.notified());
733 registry()->RemoveHandler(ph1
);
734 ASSERT_TRUE(counter
.notified());
738 TEST_F(ProtocolHandlerRegistryTest
, TestReentrantNotifications
) {
739 QueryProtocolHandlerOnChange
queryer(profile(), registry());
740 ProtocolHandler ph1
= CreateProtocolHandler("test", "test1");
741 registry()->OnAcceptRegisterProtocolHandler(ph1
);
742 ASSERT_TRUE(queryer
.called_
);
745 TEST_F(ProtocolHandlerRegistryTest
, TestProtocolsWithNoDefaultAreHandled
) {
746 ProtocolHandler ph1
= CreateProtocolHandler("test", "test1");
747 registry()->OnAcceptRegisterProtocolHandler(ph1
);
748 registry()->ClearDefault("test");
749 std::vector
<std::string
> handled_protocols
;
750 registry()->GetRegisteredProtocols(&handled_protocols
);
751 ASSERT_EQ(static_cast<size_t>(1), handled_protocols
.size());
752 ASSERT_EQ("test", handled_protocols
[0]);
755 TEST_F(ProtocolHandlerRegistryTest
, TestDisablePreventsHandling
) {
756 ProtocolHandler ph1
= CreateProtocolHandler("test", "test1");
757 registry()->OnAcceptRegisterProtocolHandler(ph1
);
758 ASSERT_TRUE(registry()->IsHandledProtocol("test"));
759 registry()->Disable();
760 ASSERT_FALSE(registry()->IsHandledProtocol("test"));
763 // TODO(smckay): This is much more appropriately an integration
764 // test. Make that so, then update the
765 // ShellIntegretion{Delegate,Observer,Worker} test classes we use to fully
766 // isolate this test from the FILE thread.
767 TEST_F(ProtocolHandlerRegistryTest
, TestOSRegistration
) {
768 ProtocolHandler ph_do1
= CreateProtocolHandler("do", "test1");
769 ProtocolHandler ph_do2
= CreateProtocolHandler("do", "test2");
770 ProtocolHandler ph_dont
= CreateProtocolHandler("dont", "test");
772 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("do"));
773 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont"));
775 registry()->OnAcceptRegisterProtocolHandler(ph_do1
);
776 registry()->OnDenyRegisterProtocolHandler(ph_dont
);
777 base::MessageLoop::current()->Run(); // FILE thread needs to run.
778 ASSERT_TRUE(delegate()->IsFakeRegisteredWithOS("do"));
779 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont"));
781 // This should not register with the OS, if it does the delegate
782 // will assert for us. We don't need to wait for the message loop
783 // as it should not go through to the shell worker.
784 registry()->OnAcceptRegisterProtocolHandler(ph_do2
);
787 #if defined(OS_LINUX)
788 // TODO(benwells): When Linux support is more reliable and
789 // http://crbut.com/88255 is fixed this test will pass.
790 #define MAYBE_TestOSRegistrationFailure DISABLED_TestOSRegistrationFailure
792 #define MAYBE_TestOSRegistrationFailure TestOSRegistrationFailure
795 // TODO(smckay): This is much more appropriately an integration
796 // test. Make that so, then update the
797 // ShellIntegretion{Delegate,Observer,Worker} test classes we use to fully
798 // isolate this test from the FILE thread.
799 TEST_F(ProtocolHandlerRegistryTest
, MAYBE_TestOSRegistrationFailure
) {
800 ProtocolHandler ph_do
= CreateProtocolHandler("do", "test1");
801 ProtocolHandler ph_dont
= CreateProtocolHandler("dont", "test");
803 ASSERT_FALSE(registry()->IsHandledProtocol("do"));
804 ASSERT_FALSE(registry()->IsHandledProtocol("dont"));
806 registry()->OnAcceptRegisterProtocolHandler(ph_do
);
807 base::MessageLoop::current()->Run(); // FILE thread needs to run.
808 delegate()->set_force_os_failure(true);
809 registry()->OnAcceptRegisterProtocolHandler(ph_dont
);
810 base::MessageLoop::current()->Run(); // FILE thread needs to run.
811 ASSERT_TRUE(registry()->IsHandledProtocol("do"));
812 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size());
813 ASSERT_FALSE(registry()->IsHandledProtocol("dont"));
814 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size());
817 TEST_F(ProtocolHandlerRegistryTest
, TestMaybeCreateTaskWorksFromIOThread
) {
818 ProtocolHandler ph1
= CreateProtocolHandler("mailto", "test1");
819 registry()->OnAcceptRegisterProtocolHandler(ph1
);
820 GURL
url("mailto:someone@something.com");
822 scoped_ptr
<net::URLRequestJobFactory
> interceptor(
823 registry()->CreateJobInterceptorFactory());
824 AssertIntercepted(url
, interceptor
.get());
827 TEST_F(ProtocolHandlerRegistryTest
,
828 MAYBE_TestIsHandledProtocolWorksOnIOThread
) {
829 std::string
scheme("mailto");
830 ProtocolHandler ph1
= CreateProtocolHandler(scheme
, "test1");
831 registry()->OnAcceptRegisterProtocolHandler(ph1
);
833 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
> interceptor(
834 registry()->CreateJobInterceptorFactory());
835 AssertWillHandle(scheme
, true, interceptor
.get());
838 TEST_F(ProtocolHandlerRegistryTest
, TestRemovingDefaultFallsBackToOldDefault
) {
839 ProtocolHandler ph1
= CreateProtocolHandler("mailto", "test1");
840 ProtocolHandler ph2
= CreateProtocolHandler("mailto", "test2");
841 ProtocolHandler ph3
= CreateProtocolHandler("mailto", "test3");
842 registry()->OnAcceptRegisterProtocolHandler(ph1
);
843 registry()->OnAcceptRegisterProtocolHandler(ph2
);
844 registry()->OnAcceptRegisterProtocolHandler(ph3
);
846 ASSERT_TRUE(registry()->IsDefault(ph3
));
847 registry()->RemoveHandler(ph3
);
848 ASSERT_TRUE(registry()->IsDefault(ph2
));
849 registry()->OnAcceptRegisterProtocolHandler(ph3
);
850 ASSERT_TRUE(registry()->IsDefault(ph3
));
851 registry()->RemoveHandler(ph2
);
852 ASSERT_TRUE(registry()->IsDefault(ph3
));
853 registry()->RemoveHandler(ph3
);
854 ASSERT_TRUE(registry()->IsDefault(ph1
));
857 TEST_F(ProtocolHandlerRegistryTest
, TestRemovingDefaultDoesntChangeHandlers
) {
858 ProtocolHandler ph1
= CreateProtocolHandler("mailto", "test1");
859 ProtocolHandler ph2
= CreateProtocolHandler("mailto", "test2");
860 ProtocolHandler ph3
= CreateProtocolHandler("mailto", "test3");
861 registry()->OnAcceptRegisterProtocolHandler(ph1
);
862 registry()->OnAcceptRegisterProtocolHandler(ph2
);
863 registry()->OnAcceptRegisterProtocolHandler(ph3
);
864 registry()->RemoveHandler(ph3
);
866 ProtocolHandlerRegistry::ProtocolHandlerList handlers
=
867 registry()->GetHandlersFor("mailto");
868 ASSERT_EQ(static_cast<size_t>(2), handlers
.size());
870 ASSERT_EQ(ph2
, handlers
[0]);
871 ASSERT_EQ(ph1
, handlers
[1]);
874 TEST_F(ProtocolHandlerRegistryTest
, MAYBE_TestClearDefaultGetsPropagatedToIO
) {
875 std::string
scheme("mailto");
876 ProtocolHandler ph1
= CreateProtocolHandler(scheme
, "test1");
877 registry()->OnAcceptRegisterProtocolHandler(ph1
);
878 registry()->ClearDefault(scheme
);
880 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
> interceptor(
881 registry()->CreateJobInterceptorFactory());
882 AssertWillHandle(scheme
, false, interceptor
.get());
885 TEST_F(ProtocolHandlerRegistryTest
, TestLoadEnabledGetsPropogatedToIO
) {
886 std::string
mailto("mailto");
887 ProtocolHandler ph1
= CreateProtocolHandler(mailto
, "MailtoHandler");
888 registry()->OnAcceptRegisterProtocolHandler(ph1
);
890 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
> interceptor(
891 registry()->CreateJobInterceptorFactory());
892 AssertWillHandle(mailto
, true, interceptor
.get());
893 registry()->Disable();
894 AssertWillHandle(mailto
, false, interceptor
.get());
897 TEST_F(ProtocolHandlerRegistryTest
, TestReplaceHandler
) {
898 ProtocolHandler ph1
=
899 CreateProtocolHandler("mailto", GURL("http://test.com/%s"));
900 ProtocolHandler ph2
=
901 CreateProtocolHandler("mailto", GURL("http://test.com/updated-url/%s"));
902 registry()->OnAcceptRegisterProtocolHandler(ph1
);
903 ASSERT_TRUE(registry()->AttemptReplace(ph2
));
904 const ProtocolHandler
& handler(registry()->GetHandlerFor("mailto"));
905 ASSERT_EQ(handler
.url(), ph2
.url());
908 TEST_F(ProtocolHandlerRegistryTest
, TestReplaceNonDefaultHandler
) {
909 ProtocolHandler ph1
=
910 CreateProtocolHandler("mailto", GURL("http://test.com/%s"));
911 ProtocolHandler ph2
=
912 CreateProtocolHandler("mailto", GURL("http://test.com/updated-url/%s"));
913 ProtocolHandler ph3
=
914 CreateProtocolHandler("mailto", GURL("http://else.com/%s"));
915 registry()->OnAcceptRegisterProtocolHandler(ph1
);
916 registry()->OnAcceptRegisterProtocolHandler(ph3
);
917 ASSERT_TRUE(registry()->AttemptReplace(ph2
));
918 const ProtocolHandler
& handler(registry()->GetHandlerFor("mailto"));
919 ASSERT_EQ(handler
.url(), ph3
.url());
922 TEST_F(ProtocolHandlerRegistryTest
, TestReplaceRemovesStaleHandlers
) {
923 ProtocolHandler ph1
=
924 CreateProtocolHandler("mailto", GURL("http://test.com/%s"));
925 ProtocolHandler ph2
=
926 CreateProtocolHandler("mailto", GURL("http://test.com/updated-url/%s"));
927 ProtocolHandler ph3
=
928 CreateProtocolHandler("mailto", GURL("http://test.com/third/%s"));
929 registry()->OnAcceptRegisterProtocolHandler(ph1
);
930 registry()->OnAcceptRegisterProtocolHandler(ph2
);
932 // This should replace the previous two handlers.
933 ASSERT_TRUE(registry()->AttemptReplace(ph3
));
934 const ProtocolHandler
& handler(registry()->GetHandlerFor("mailto"));
935 ASSERT_EQ(handler
.url(), ph3
.url());
936 registry()->RemoveHandler(ph3
);
937 ASSERT_TRUE(registry()->GetHandlerFor("mailto").IsEmpty());
940 TEST_F(ProtocolHandlerRegistryTest
, TestIsSameOrigin
) {
941 ProtocolHandler ph1
=
942 CreateProtocolHandler("mailto", GURL("http://test.com/%s"));
943 ProtocolHandler ph2
=
944 CreateProtocolHandler("mailto", GURL("http://test.com/updated-url/%s"));
945 ProtocolHandler ph3
=
946 CreateProtocolHandler("mailto", GURL("http://other.com/%s"));
947 ASSERT_EQ(ph1
.url().GetOrigin() == ph2
.url().GetOrigin(),
948 ph1
.IsSameOrigin(ph2
));
949 ASSERT_EQ(ph1
.url().GetOrigin() == ph2
.url().GetOrigin(),
950 ph2
.IsSameOrigin(ph1
));
951 ASSERT_EQ(ph2
.url().GetOrigin() == ph3
.url().GetOrigin(),
952 ph2
.IsSameOrigin(ph3
));
953 ASSERT_EQ(ph3
.url().GetOrigin() == ph2
.url().GetOrigin(),
954 ph3
.IsSameOrigin(ph2
));
957 TEST_F(ProtocolHandlerRegistryTest
, MAYBE_TestInstallDefaultHandler
) {
958 RecreateRegistry(false);
959 registry()->AddPredefinedHandler(
960 CreateProtocolHandler("test", GURL("http://test.com/%s")));
961 registry()->InitProtocolSettings();
962 std::vector
<std::string
> protocols
;
963 registry()->GetRegisteredProtocols(&protocols
);
964 ASSERT_EQ(static_cast<size_t>(1), protocols
.size());
967 #define URL_p1u1 "http://p1u1.com/%s"
968 #define URL_p1u2 "http://p1u2.com/%s"
969 #define URL_p1u3 "http://p1u3.com/%s"
970 #define URL_p2u1 "http://p2u1.com/%s"
971 #define URL_p2u2 "http://p2u2.com/%s"
972 #define URL_p3u1 "http://p3u1.com/%s"
974 TEST_F(ProtocolHandlerRegistryTest
, TestPrefPolicyOverlapRegister
) {
975 base::ListValue handlers_registered_by_pref
;
976 base::ListValue handlers_registered_by_policy
;
978 handlers_registered_by_pref
.Append(
979 GetProtocolHandlerValueWithDefault("p1", URL_p1u2
, true));
980 handlers_registered_by_pref
.Append(
981 GetProtocolHandlerValueWithDefault("p1", URL_p1u1
, true));
982 handlers_registered_by_pref
.Append(
983 GetProtocolHandlerValueWithDefault("p1", URL_p1u2
, false));
985 handlers_registered_by_policy
.Append(
986 GetProtocolHandlerValueWithDefault("p1", URL_p1u1
, false));
987 handlers_registered_by_policy
.Append(
988 GetProtocolHandlerValueWithDefault("p3", URL_p3u1
, true));
990 profile()->GetPrefs()->Set(prefs::kRegisteredProtocolHandlers
,
991 handlers_registered_by_pref
);
992 profile()->GetPrefs()->Set(prefs::kPolicyRegisteredProtocolHandlers
,
993 handlers_registered_by_policy
);
994 registry()->InitProtocolSettings();
996 // Duplicate p1u2 eliminated in memory but not yet saved in pref
997 ProtocolHandler p1u1
= CreateProtocolHandler("p1", GURL(URL_p1u1
));
998 ProtocolHandler p1u2
= CreateProtocolHandler("p1", GURL(URL_p1u2
));
999 ASSERT_EQ(InPrefHandlerCount(), 3);
1000 ASSERT_EQ(InMemoryHandlerCount(), 3);
1001 ASSERT_TRUE(registry()->IsDefault(p1u1
));
1002 ASSERT_FALSE(registry()->IsDefault(p1u2
));
1004 ProtocolHandler p2u1
= CreateProtocolHandler("p2", GURL(URL_p2u1
));
1005 registry()->OnDenyRegisterProtocolHandler(p2u1
);
1007 // Duplicate p1u2 saved in pref and a new handler added to pref and memory
1008 ASSERT_EQ(InPrefHandlerCount(), 3);
1009 ASSERT_EQ(InMemoryHandlerCount(), 4);
1010 ASSERT_FALSE(registry()->IsDefault(p2u1
));
1012 registry()->RemoveHandler(p1u1
);
1014 // p1u1 removed from user pref but not from memory due to policy.
1015 ASSERT_EQ(InPrefHandlerCount(), 2);
1016 ASSERT_EQ(InMemoryHandlerCount(), 4);
1017 ASSERT_TRUE(registry()->IsDefault(p1u1
));
1019 ProtocolHandler p3u1
= CreateProtocolHandler("p3", GURL(URL_p3u1
));
1020 registry()->RemoveHandler(p3u1
);
1022 // p3u1 not removed from memory due to policy and it was never in pref.
1023 ASSERT_EQ(InPrefHandlerCount(), 2);
1024 ASSERT_EQ(InMemoryHandlerCount(), 4);
1025 ASSERT_TRUE(registry()->IsDefault(p3u1
));
1027 registry()->RemoveHandler(p1u2
);
1029 // p1u2 removed from user pref and memory.
1030 ASSERT_EQ(InPrefHandlerCount(), 1);
1031 ASSERT_EQ(InMemoryHandlerCount(), 3);
1032 ASSERT_TRUE(registry()->IsDefault(p1u1
));
1034 ProtocolHandler p1u3
= CreateProtocolHandler("p1", GURL(URL_p1u3
));
1035 registry()->OnAcceptRegisterProtocolHandler(p1u3
);
1037 // p1u3 added to pref and memory.
1038 ASSERT_EQ(InPrefHandlerCount(), 2);
1039 ASSERT_EQ(InMemoryHandlerCount(), 4);
1040 ASSERT_FALSE(registry()->IsDefault(p1u1
));
1041 ASSERT_TRUE(registry()->IsDefault(p1u3
));
1043 registry()->RemoveHandler(p1u3
);
1045 // p1u3 the default handler for p1 removed from user pref and memory.
1046 ASSERT_EQ(InPrefHandlerCount(), 1);
1047 ASSERT_EQ(InMemoryHandlerCount(), 3);
1048 ASSERT_FALSE(registry()->IsDefault(p1u3
));
1049 ASSERT_TRUE(registry()->IsDefault(p1u1
));
1050 ASSERT_TRUE(registry()->IsDefault(p3u1
));
1051 ASSERT_FALSE(registry()->IsDefault(p2u1
));
1054 TEST_F(ProtocolHandlerRegistryTest
, TestPrefPolicyOverlapIgnore
) {
1055 base::ListValue handlers_ignored_by_pref
;
1056 base::ListValue handlers_ignored_by_policy
;
1058 handlers_ignored_by_pref
.Append(GetProtocolHandlerValue("p1", URL_p1u1
));
1059 handlers_ignored_by_pref
.Append(GetProtocolHandlerValue("p1", URL_p1u2
));
1060 handlers_ignored_by_pref
.Append(GetProtocolHandlerValue("p1", URL_p1u2
));
1061 handlers_ignored_by_pref
.Append(GetProtocolHandlerValue("p3", URL_p3u1
));
1063 handlers_ignored_by_policy
.Append(GetProtocolHandlerValue("p1", URL_p1u2
));
1064 handlers_ignored_by_policy
.Append(GetProtocolHandlerValue("p1", URL_p1u3
));
1065 handlers_ignored_by_policy
.Append(GetProtocolHandlerValue("p2", URL_p2u1
));
1067 profile()->GetPrefs()->Set(prefs::kIgnoredProtocolHandlers
,
1068 handlers_ignored_by_pref
);
1069 profile()->GetPrefs()->Set(prefs::kPolicyIgnoredProtocolHandlers
,
1070 handlers_ignored_by_policy
);
1071 registry()->InitProtocolSettings();
1073 // Duplicate p1u2 eliminated in memory but not yet saved in pref
1074 ASSERT_EQ(InPrefIgnoredHandlerCount(), 4);
1075 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 5);
1077 ProtocolHandler p2u2
= CreateProtocolHandler("p2", GURL(URL_p2u2
));
1078 registry()->OnIgnoreRegisterProtocolHandler(p2u2
);
1080 // Duplicate p1u2 eliminated in pref, p2u2 added to pref and memory.
1081 ASSERT_EQ(InPrefIgnoredHandlerCount(), 4);
1082 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6);
1084 ProtocolHandler p2u1
= CreateProtocolHandler("p2", GURL(URL_p2u1
));
1085 registry()->RemoveIgnoredHandler(p2u1
);
1087 // p2u1 installed by policy so cant be removed.
1088 ASSERT_EQ(InPrefIgnoredHandlerCount(), 4);
1089 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6);
1091 ProtocolHandler p1u2
= CreateProtocolHandler("p1", GURL(URL_p1u2
));
1092 registry()->RemoveIgnoredHandler(p1u2
);
1094 // p1u2 installed by policy and pref so it is removed from pref and not from
1096 ASSERT_EQ(InPrefIgnoredHandlerCount(), 3);
1097 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6);
1099 ProtocolHandler p1u1
= CreateProtocolHandler("p1", GURL(URL_p1u1
));
1100 registry()->RemoveIgnoredHandler(p1u1
);
1102 // p1u1 installed by pref so it is removed from pref and memory.
1103 ASSERT_EQ(InPrefIgnoredHandlerCount(), 2);
1104 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 5);
1106 registry()->RemoveIgnoredHandler(p2u2
);
1108 // p2u2 installed by user so it is removed from pref and memory.
1109 ASSERT_EQ(InPrefIgnoredHandlerCount(), 1);
1110 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4);
1112 registry()->OnIgnoreRegisterProtocolHandler(p2u1
);
1114 // p2u1 installed by user but it is already installed by policy, so it is
1116 ASSERT_EQ(InPrefIgnoredHandlerCount(), 2);
1117 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4);
1119 registry()->RemoveIgnoredHandler(p2u1
);
1121 // p2u1 installed by user and policy, so it is removed from pref alone.
1122 ASSERT_EQ(InPrefIgnoredHandlerCount(), 1);
1123 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4);