protocol_handler_registry_unittest.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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. 4 5#include "chrome/browser/custom_handlers/protocol_handler_registry.h" 6 7#include <set> 8 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" 29 30using content::BrowserThread; 31 32namespace { 33 34void AssertInterceptedIO( 35 const GURL& url, 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); 44} 45 46void AssertIntercepted( 47 const GURL& url, 48 net::URLRequestJobFactory* interceptor) { 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 50 BrowserThread::PostTask(BrowserThread::IO, 51 FROM_HERE, 52 base::Bind(AssertInterceptedIO, 53 url, 54 base::Unretained(interceptor))); 55 base::MessageLoop::current()->RunUntilIdle(); 56} 57 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. 63class 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 { 69 return NULL; 70 } 71 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { 72 return false; 73 } 74 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { 75 return false; 76 } 77 virtual bool IsSafeRedirectTarget(const GURL& location) const OVERRIDE { 78 return true; 79 } 80}; 81 82void AssertWillHandleIO( 83 const std::string& scheme, 84 bool expected, 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>()); 91} 92 93void AssertWillHandle( 94 const std::string& scheme, 95 bool expected, 96 ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) { 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 98 BrowserThread::PostTask(BrowserThread::IO, 99 FROM_HERE, 100 base::Bind(AssertWillHandleIO, 101 scheme, 102 expected, 103 base::Unretained(interceptor))); 104 base::MessageLoop::current()->RunUntilIdle(); 105} 106 107base::DictionaryValue* GetProtocolHandlerValue(std::string protocol, 108 std::string url) { 109 base::DictionaryValue* value = new base::DictionaryValue(); 110 value->SetString("protocol", protocol); 111 value->SetString("url", url); 112 return value; 113} 114 115base::DictionaryValue* GetProtocolHandlerValueWithDefault(std::string protocol, 116 std::string url, 117 bool is_default) { 118 base::DictionaryValue* value = GetProtocolHandlerValue(protocol, url); 119 value->SetBoolean("default", is_default); 120 return value; 121} 122 123class FakeDelegate : public ProtocolHandlerRegistry::Delegate { 124 public: 125 FakeDelegate() : force_os_failure_(false) {} 126 virtual ~FakeDelegate() { } 127 virtual void RegisterExternalHandler(const std::string& protocol) OVERRIDE { 128 ASSERT_TRUE( 129 registered_protocols_.find(protocol) == registered_protocols_.end()); 130 registered_protocols_.insert(protocol); 131 } 132 133 virtual void DeregisterExternalHandler(const std::string& protocol) OVERRIDE { 134 registered_protocols_.erase(protocol); 135 } 136 137 virtual ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker( 138 ShellIntegration::DefaultWebClientObserver* observer, 139 const std::string& protocol) OVERRIDE; 140 141 virtual ProtocolHandlerRegistry::DefaultClientObserver* CreateShellObserver( 142 ProtocolHandlerRegistry* registry) OVERRIDE; 143 144 virtual void RegisterWithOSAsDefaultClient( 145 const std::string& protocol, 146 ProtocolHandlerRegistry* reg) OVERRIDE { 147 ProtocolHandlerRegistry::Delegate::RegisterWithOSAsDefaultClient(protocol, 148 reg); 149 ASSERT_FALSE(IsFakeRegisteredWithOS(protocol)); 150 } 151 152 virtual bool IsExternalHandlerRegistered( 153 const std::string& protocol) OVERRIDE { 154 return registered_protocols_.find(protocol) != registered_protocols_.end(); 155 } 156 157 bool IsFakeRegisteredWithOS(const std::string& protocol) { 158 return os_registered_protocols_.find(protocol) != 159 os_registered_protocols_.end(); 160 } 161 162 void FakeRegisterWithOS(const std::string& protocol) { 163 os_registered_protocols_.insert(protocol); 164 } 165 166 void Reset() { 167 registered_protocols_.clear(); 168 os_registered_protocols_.clear(); 169 force_os_failure_ = false; 170 } 171 172 void set_force_os_failure(bool force) { force_os_failure_ = force; } 173 174 bool force_os_failure() { return force_os_failure_; } 175 176 private: 177 std::set<std::string> registered_protocols_; 178 std::set<std::string> os_registered_protocols_; 179 bool force_os_failure_; 180}; 181 182class FakeClientObserver 183 : public ProtocolHandlerRegistry::DefaultClientObserver { 184 public: 185 FakeClientObserver(ProtocolHandlerRegistry* registry, 186 FakeDelegate* registry_delegate) 187 : ProtocolHandlerRegistry::DefaultClientObserver(registry), 188 delegate_(registry_delegate) {} 189 190 virtual void SetDefaultWebClientUIState( 191 ShellIntegration::DefaultWebClientUIState state) OVERRIDE { 192 ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState( 193 state); 194 if (state == ShellIntegration::STATE_IS_DEFAULT) { 195 delegate_->FakeRegisterWithOS(worker_->protocol()); 196 } 197 if (state != ShellIntegration::STATE_PROCESSING) { 198 base::MessageLoop::current()->Quit(); 199 } 200 } 201 202 private: 203 FakeDelegate* delegate_; 204}; 205 206class FakeProtocolClientWorker 207 : public ShellIntegration::DefaultProtocolClientWorker { 208 public: 209 FakeProtocolClientWorker(ShellIntegration::DefaultWebClientObserver* observer, 210 const std::string& protocol, 211 bool force_failure) 212 : ShellIntegration::DefaultProtocolClientWorker(observer, protocol), 213 force_failure_(force_failure) {} 214 215 private: 216 virtual ~FakeProtocolClientWorker() {} 217 218 virtual ShellIntegration::DefaultWebClientState CheckIsDefault() OVERRIDE { 219 if (force_failure_) { 220 return ShellIntegration::NOT_DEFAULT; 221 } else { 222 return ShellIntegration::IS_DEFAULT; 223 } 224 } 225 226 virtual bool SetAsDefault(bool interactive_permitted) OVERRIDE { 227 return true; 228 } 229 230 private: 231 bool force_failure_; 232}; 233 234ProtocolHandlerRegistry::DefaultClientObserver* 235 FakeDelegate::CreateShellObserver(ProtocolHandlerRegistry* registry) { 236 return new FakeClientObserver(registry, this); 237} 238 239ShellIntegration::DefaultProtocolClientWorker* FakeDelegate::CreateShellWorker( 240 ShellIntegration::DefaultWebClientObserver* observer, 241 const std::string& protocol) { 242 return new FakeProtocolClientWorker(observer, protocol, force_os_failure_); 243} 244 245class NotificationCounter : public content::NotificationObserver { 246 public: 247 explicit NotificationCounter(Profile* profile) 248 : events_(0), 249 notification_registrar_() { 250 notification_registrar_.Add(this, 251 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, 252 content::Source<Profile>(profile)); 253 } 254 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 { 261 ++events_; 262 } 263 264 int events_; 265 content::NotificationRegistrar notification_registrar_; 266}; 267 268class QueryProtocolHandlerOnChange 269 : public content::NotificationObserver { 270 public: 271 QueryProtocolHandlerOnChange(Profile* profile, 272 ProtocolHandlerRegistry* registry) 273 : local_registry_(registry), 274 called_(false), 275 notification_registrar_() { 276 notification_registrar_.Add(this, 277 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, 278 content::Source<Profile>(profile)); 279 } 280 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); 286 called_ = true; 287 } 288 289 ProtocolHandlerRegistry* local_registry_; 290 bool called_; 291 content::NotificationRegistrar notification_registrar_; 292}; 293 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! 300class TestMessageLoop : public base::MessageLoop { 301 public: 302 TestMessageLoop() {} 303 virtual ~TestMessageLoop() {} 304 virtual bool IsType(base::MessageLoop::Type type) const OVERRIDE { 305 switch (type) { 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); 317 } 318 return false; 319 } 320}; 321 322} // namespace 323 324class ProtocolHandlerRegistryTest : public testing::Test { 325 protected: 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")) {} 331 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_; 337 } 338 339 ProtocolHandler CreateProtocolHandler(const std::string& protocol, 340 const GURL& url) { 341 return ProtocolHandler::CreateProtocolHandler(protocol, url); 342 } 343 344 ProtocolHandler CreateProtocolHandler(const std::string& protocol, 345 const std::string& name) { 346 return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s")); 347 } 348 349 void RecreateRegistry(bool initialize) { 350 TeadDownRegistry(); 351 SetUpRegistry(initialize); 352 } 353 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()); 358 } 359 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; 367 } 368 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()); 373 } 374 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; 382 } 383 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(); 390 } 391 392 void TeadDownRegistry() { 393 registry_->Shutdown(); 394 registry_.reset(); 395 // Registry owns the delegate_ it handles deletion of that object. 396 } 397 398 virtual void SetUp() { 399 profile_.reset(new TestingProfile()); 400 CHECK(profile_->GetPrefs()); 401 SetUpRegistry(true); 402 test_protocol_handler_ = 403 CreateProtocolHandler("test", GURL("http://test.com/%s")); 404 } 405 406 virtual void TearDown() { 407 TeadDownRegistry(); 408 } 409 410 TestMessageLoop loop_; 411 412 private: 413 content::TestBrowserThread ui_thread_; 414 content::TestBrowserThread file_thread_; 415 content::TestBrowserThread io_thread_; 416 417 scoped_ptr<TestingProfile> profile_; 418 FakeDelegate* delegate_; // Registry assumes ownership of delegate_. 419 scoped_ptr<ProtocolHandlerRegistry> registry_; 420 ProtocolHandler test_protocol_handler_; 421}; 422 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 439#else 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) 452 453TEST_F(ProtocolHandlerRegistryTest, 454 MAYBE_AcceptProtocolHandlerHandlesProtocol) { 455 ASSERT_FALSE(registry()->IsHandledProtocol("test")); 456 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); 457 ASSERT_TRUE(registry()->IsHandledProtocol("test")); 458} 459 460TEST_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")); 466} 467 468TEST_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()); 473} 474 475TEST_F(ProtocolHandlerRegistryTest, DisableDeregistersProtocolHandlers) { 476 ASSERT_FALSE(delegate()->IsExternalHandlerRegistered("test")); 477 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); 478 ASSERT_TRUE(delegate()->IsExternalHandlerRegistered("test")); 479 480 registry()->Disable(); 481 ASSERT_FALSE(delegate()->IsExternalHandlerRegistered("test")); 482 registry()->Enable(); 483 ASSERT_TRUE(delegate()->IsExternalHandlerRegistered("test")); 484} 485 486TEST_F(ProtocolHandlerRegistryTest, IgnoreProtocolHandler) { 487 registry()->OnIgnoreRegisterProtocolHandler(test_protocol_handler()); 488 ASSERT_TRUE(registry()->IsIgnored(test_protocol_handler())); 489 490 registry()->RemoveIgnoredHandler(test_protocol_handler()); 491 ASSERT_FALSE(registry()->IsIgnored(test_protocol_handler())); 492} 493 494TEST_F(ProtocolHandlerRegistryTest, IgnoreEquivalentProtocolHandler) { 495 ProtocolHandler ph1 = CreateProtocolHandler("test", GURL("http://test/%s")); 496 ProtocolHandler ph2 = CreateProtocolHandler("test", GURL("http://test/%s")); 497 498 registry()->OnIgnoreRegisterProtocolHandler(ph1); 499 ASSERT_TRUE(registry()->IsIgnored(ph1)); 500 ASSERT_TRUE(registry()->HasIgnoredEquivalent(ph2)); 501 502 registry()->RemoveIgnoredHandler(ph1); 503 ASSERT_FALSE(registry()->IsIgnored(ph1)); 504 ASSERT_FALSE(registry()->HasIgnoredEquivalent(ph2)); 505} 506 507TEST_F(ProtocolHandlerRegistryTest, SaveAndLoad) { 508 ProtocolHandler stuff_protocol_handler( 509 CreateProtocolHandler("stuff", "stuff")); 510 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); 511 registry()->OnIgnoreRegisterProtocolHandler(stuff_protocol_handler); 512 513 ASSERT_TRUE(registry()->IsHandledProtocol("test")); 514 ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler)); 515 delegate()->Reset(); 516 RecreateRegistry(true); 517 ASSERT_TRUE(registry()->IsHandledProtocol("test")); 518 ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler)); 519} 520 521TEST_F(ProtocolHandlerRegistryTest, TestEnabledDisabled) { 522 registry()->Disable(); 523 ASSERT_FALSE(registry()->enabled()); 524 registry()->Enable(); 525 ASSERT_TRUE(registry()->enabled()); 526} 527 528TEST_F(ProtocolHandlerRegistryTest, 529 DisallowRegisteringExternallyHandledProtocols) { 530 delegate()->RegisterExternalHandler("test"); 531 ASSERT_FALSE(registry()->CanSchemeBeOverridden("test")); 532} 533 534TEST_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")); 539} 540 541TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestStartsAsDefault) { 542 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); 543 ASSERT_TRUE(registry()->IsDefault(test_protocol_handler())); 544} 545 546TEST_F(ProtocolHandlerRegistryTest, TestClearDefault) { 547 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); 548 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); 549 registry()->OnAcceptRegisterProtocolHandler(ph1); 550 registry()->OnAcceptRegisterProtocolHandler(ph2); 551 552 registry()->OnAcceptRegisterProtocolHandler(ph1); 553 registry()->ClearDefault("test"); 554 ASSERT_FALSE(registry()->IsDefault(ph1)); 555 ASSERT_FALSE(registry()->IsDefault(ph2)); 556} 557 558TEST_F(ProtocolHandlerRegistryTest, TestGetHandlerFor) { 559 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); 560 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); 561 registry()->OnAcceptRegisterProtocolHandler(ph1); 562 registry()->OnAcceptRegisterProtocolHandler(ph2); 563 564 registry()->OnAcceptRegisterProtocolHandler(ph2); 565 ASSERT_EQ(ph2, registry()->GetHandlerFor("test")); 566 ASSERT_TRUE(registry()->IsHandledProtocol("test")); 567} 568 569TEST_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)); 576} 577 578TEST_F(ProtocolHandlerRegistryTest, TestOnAcceptRegisterProtocolHandler) { 579 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); 580 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); 581 registry()->OnAcceptRegisterProtocolHandler(ph1); 582 registry()->OnAcceptRegisterProtocolHandler(ph2); 583 584 registry()->OnAcceptRegisterProtocolHandler(ph1); 585 ASSERT_TRUE(registry()->IsDefault(ph1)); 586 ASSERT_FALSE(registry()->IsDefault(ph2)); 587 588 registry()->OnAcceptRegisterProtocolHandler(ph2); 589 ASSERT_FALSE(registry()->IsDefault(ph1)); 590 ASSERT_TRUE(registry()->IsDefault(ph2)); 591} 592 593TEST_F(ProtocolHandlerRegistryTest, TestDefaultSaveLoad) { 594 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); 595 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); 596 registry()->OnDenyRegisterProtocolHandler(ph1); 597 registry()->OnDenyRegisterProtocolHandler(ph2); 598 599 registry()->OnAcceptRegisterProtocolHandler(ph2); 600 registry()->Disable(); 601 602 RecreateRegistry(true); 603 604 ASSERT_FALSE(registry()->enabled()); 605 registry()->Enable(); 606 ASSERT_FALSE(registry()->IsDefault(ph1)); 607 ASSERT_TRUE(registry()->IsDefault(ph2)); 608 609 RecreateRegistry(true); 610 ASSERT_TRUE(registry()->enabled()); 611} 612 613TEST_F(ProtocolHandlerRegistryTest, TestRemoveHandler) { 614 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); 615 registry()->OnAcceptRegisterProtocolHandler(ph1); 616 registry()->OnAcceptRegisterProtocolHandler(ph1); 617 618 registry()->RemoveHandler(ph1); 619 ASSERT_FALSE(registry()->IsRegistered(ph1)); 620 ASSERT_FALSE(registry()->IsHandledProtocol("test")); 621} 622 623TEST_F(ProtocolHandlerRegistryTest, TestIsRegistered) { 624 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); 625 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); 626 registry()->OnAcceptRegisterProtocolHandler(ph1); 627 registry()->OnAcceptRegisterProtocolHandler(ph2); 628 629 ASSERT_TRUE(registry()->IsRegistered(ph1)); 630} 631 632TEST_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); 636 637 ASSERT_TRUE(registry()->IsRegistered(ph1)); 638 ASSERT_TRUE(registry()->HasRegisteredEquivalent(ph2)); 639} 640 641TEST_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")); 646 647 ASSERT_FALSE(registry()->SilentlyHandleRegisterHandlerRequest(ph1)); 648 ASSERT_FALSE(registry()->IsRegistered(ph1)); 649 650 registry()->OnAcceptRegisterProtocolHandler(ph1); 651 ASSERT_TRUE(registry()->IsRegistered(ph1)); 652 653 ASSERT_TRUE(registry()->SilentlyHandleRegisterHandlerRequest(ph2)); 654 ASSERT_FALSE(registry()->IsRegistered(ph1)); 655 ASSERT_TRUE(registry()->IsRegistered(ph2)); 656 657 ASSERT_FALSE(registry()->SilentlyHandleRegisterHandlerRequest(ph3)); 658 ASSERT_FALSE(registry()->IsRegistered(ph3)); 659 660 registry()->OnIgnoreRegisterProtocolHandler(ph3); 661 ASSERT_FALSE(registry()->IsRegistered(ph3)); 662 ASSERT_TRUE(registry()->IsIgnored(ph3)); 663 664 ASSERT_TRUE(registry()->SilentlyHandleRegisterHandlerRequest(ph4)); 665 ASSERT_FALSE(registry()->IsRegistered(ph4)); 666 ASSERT_TRUE(registry()->HasIgnoredEquivalent(ph4)); 667} 668 669TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestRemoveHandlerRemovesDefault) { 670 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); 671 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); 672 ProtocolHandler ph3 = CreateProtocolHandler("test", "test3"); 673 674 registry()->OnAcceptRegisterProtocolHandler(ph1); 675 registry()->OnAcceptRegisterProtocolHandler(ph2); 676 registry()->OnAcceptRegisterProtocolHandler(ph3); 677 678 registry()->OnAcceptRegisterProtocolHandler(ph1); 679 registry()->RemoveHandler(ph1); 680 ASSERT_FALSE(registry()->IsDefault(ph1)); 681} 682 683TEST_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); 690 691 ProtocolHandlerRegistry::ProtocolHandlerList handlers = 692 registry()->GetHandlersFor("test"); 693 ASSERT_EQ(static_cast<size_t>(3), handlers.size()); 694 695 ASSERT_EQ(ph3, handlers[0]); 696 ASSERT_EQ(ph2, handlers[1]); 697 ASSERT_EQ(ph1, handlers[2]); 698} 699 700TEST_F(ProtocolHandlerRegistryTest, TestGetRegisteredProtocols) { 701 std::vector<std::string> protocols; 702 registry()->GetRegisteredProtocols(&protocols); 703 ASSERT_EQ(static_cast<size_t>(0), protocols.size()); 704 705 registry()->GetHandlersFor("test"); 706 707 protocols.clear(); 708 registry()->GetRegisteredProtocols(&protocols); 709 ASSERT_EQ(static_cast<size_t>(0), protocols.size()); 710} 711 712TEST_F(ProtocolHandlerRegistryTest, TestIsHandledProtocol) { 713 registry()->GetHandlersFor("test"); 714 ASSERT_FALSE(registry()->IsHandledProtocol("test")); 715} 716 717TEST_F(ProtocolHandlerRegistryTest, TestNotifications) { 718 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); 719 NotificationCounter counter(profile()); 720 721 registry()->OnAcceptRegisterProtocolHandler(ph1); 722 ASSERT_TRUE(counter.notified()); 723 counter.Clear(); 724 725 registry()->Disable(); 726 ASSERT_TRUE(counter.notified()); 727 counter.Clear(); 728 729 registry()->Enable(); 730 ASSERT_TRUE(counter.notified()); 731 counter.Clear(); 732 733 registry()->RemoveHandler(ph1); 734 ASSERT_TRUE(counter.notified()); 735 counter.Clear(); 736} 737 738TEST_F(ProtocolHandlerRegistryTest, TestReentrantNotifications) { 739 QueryProtocolHandlerOnChange queryer(profile(), registry()); 740 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); 741 registry()->OnAcceptRegisterProtocolHandler(ph1); 742 ASSERT_TRUE(queryer.called_); 743} 744 745TEST_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]); 753} 754 755TEST_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")); 761} 762 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. 767TEST_F(ProtocolHandlerRegistryTest, TestOSRegistration) { 768 ProtocolHandler ph_do1 = CreateProtocolHandler("do", "test1"); 769 ProtocolHandler ph_do2 = CreateProtocolHandler("do", "test2"); 770 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test"); 771 772 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("do")); 773 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont")); 774 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")); 780 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); 785} 786 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 791#else 792#define MAYBE_TestOSRegistrationFailure TestOSRegistrationFailure 793#endif 794 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. 799TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestOSRegistrationFailure) { 800 ProtocolHandler ph_do = CreateProtocolHandler("do", "test1"); 801 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test"); 802 803 ASSERT_FALSE(registry()->IsHandledProtocol("do")); 804 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); 805 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()); 815} 816 817TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) { 818 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); 819 registry()->OnAcceptRegisterProtocolHandler(ph1); 820 GURL url("mailto:someone@something.com"); 821 822 scoped_ptr<net::URLRequestJobFactory> interceptor( 823 registry()->CreateJobInterceptorFactory()); 824 AssertIntercepted(url, interceptor.get()); 825} 826 827TEST_F(ProtocolHandlerRegistryTest, 828 MAYBE_TestIsHandledProtocolWorksOnIOThread) { 829 std::string scheme("mailto"); 830 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); 831 registry()->OnAcceptRegisterProtocolHandler(ph1); 832 833 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor( 834 registry()->CreateJobInterceptorFactory()); 835 AssertWillHandle(scheme, true, interceptor.get()); 836} 837 838TEST_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); 845 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)); 855} 856 857TEST_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); 865 866 ProtocolHandlerRegistry::ProtocolHandlerList handlers = 867 registry()->GetHandlersFor("mailto"); 868 ASSERT_EQ(static_cast<size_t>(2), handlers.size()); 869 870 ASSERT_EQ(ph2, handlers[0]); 871 ASSERT_EQ(ph1, handlers[1]); 872} 873 874TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestClearDefaultGetsPropagatedToIO) { 875 std::string scheme("mailto"); 876 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); 877 registry()->OnAcceptRegisterProtocolHandler(ph1); 878 registry()->ClearDefault(scheme); 879 880 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor( 881 registry()->CreateJobInterceptorFactory()); 882 AssertWillHandle(scheme, false, interceptor.get()); 883} 884 885TEST_F(ProtocolHandlerRegistryTest, TestLoadEnabledGetsPropogatedToIO) { 886 std::string mailto("mailto"); 887 ProtocolHandler ph1 = CreateProtocolHandler(mailto, "MailtoHandler"); 888 registry()->OnAcceptRegisterProtocolHandler(ph1); 889 890 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor( 891 registry()->CreateJobInterceptorFactory()); 892 AssertWillHandle(mailto, true, interceptor.get()); 893 registry()->Disable(); 894 AssertWillHandle(mailto, false, interceptor.get()); 895} 896 897TEST_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()); 906} 907 908TEST_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()); 920} 921 922TEST_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); 931 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()); 938} 939 940TEST_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)); 955} 956 957TEST_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()); 965} 966 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" 973 974TEST_F(ProtocolHandlerRegistryTest, TestPrefPolicyOverlapRegister) { 975 base::ListValue handlers_registered_by_pref; 976 base::ListValue handlers_registered_by_policy; 977 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)); 984 985 handlers_registered_by_policy.Append( 986 GetProtocolHandlerValueWithDefault("p1", URL_p1u1, false)); 987 handlers_registered_by_policy.Append( 988 GetProtocolHandlerValueWithDefault("p3", URL_p3u1, true)); 989 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(); 995 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)); 1003 1004 ProtocolHandler p2u1 = CreateProtocolHandler("p2", GURL(URL_p2u1)); 1005 registry()->OnDenyRegisterProtocolHandler(p2u1); 1006 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)); 1011 1012 registry()->RemoveHandler(p1u1); 1013 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)); 1018 1019 ProtocolHandler p3u1 = CreateProtocolHandler("p3", GURL(URL_p3u1)); 1020 registry()->RemoveHandler(p3u1); 1021 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)); 1026 1027 registry()->RemoveHandler(p1u2); 1028 1029 // p1u2 removed from user pref and memory. 1030 ASSERT_EQ(InPrefHandlerCount(), 1); 1031 ASSERT_EQ(InMemoryHandlerCount(), 3); 1032 ASSERT_TRUE(registry()->IsDefault(p1u1)); 1033 1034 ProtocolHandler p1u3 = CreateProtocolHandler("p1", GURL(URL_p1u3)); 1035 registry()->OnAcceptRegisterProtocolHandler(p1u3); 1036 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)); 1042 1043 registry()->RemoveHandler(p1u3); 1044 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)); 1052} 1053 1054TEST_F(ProtocolHandlerRegistryTest, TestPrefPolicyOverlapIgnore) { 1055 base::ListValue handlers_ignored_by_pref; 1056 base::ListValue handlers_ignored_by_policy; 1057 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)); 1062 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)); 1066 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(); 1072 1073 // Duplicate p1u2 eliminated in memory but not yet saved in pref 1074 ASSERT_EQ(InPrefIgnoredHandlerCount(), 4); 1075 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 5); 1076 1077 ProtocolHandler p2u2 = CreateProtocolHandler("p2", GURL(URL_p2u2)); 1078 registry()->OnIgnoreRegisterProtocolHandler(p2u2); 1079 1080 // Duplicate p1u2 eliminated in pref, p2u2 added to pref and memory. 1081 ASSERT_EQ(InPrefIgnoredHandlerCount(), 4); 1082 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6); 1083 1084 ProtocolHandler p2u1 = CreateProtocolHandler("p2", GURL(URL_p2u1)); 1085 registry()->RemoveIgnoredHandler(p2u1); 1086 1087 // p2u1 installed by policy so cant be removed. 1088 ASSERT_EQ(InPrefIgnoredHandlerCount(), 4); 1089 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6); 1090 1091 ProtocolHandler p1u2 = CreateProtocolHandler("p1", GURL(URL_p1u2)); 1092 registry()->RemoveIgnoredHandler(p1u2); 1093 1094 // p1u2 installed by policy and pref so it is removed from pref and not from 1095 // memory. 1096 ASSERT_EQ(InPrefIgnoredHandlerCount(), 3); 1097 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6); 1098 1099 ProtocolHandler p1u1 = CreateProtocolHandler("p1", GURL(URL_p1u1)); 1100 registry()->RemoveIgnoredHandler(p1u1); 1101 1102 // p1u1 installed by pref so it is removed from pref and memory. 1103 ASSERT_EQ(InPrefIgnoredHandlerCount(), 2); 1104 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 5); 1105 1106 registry()->RemoveIgnoredHandler(p2u2); 1107 1108 // p2u2 installed by user so it is removed from pref and memory. 1109 ASSERT_EQ(InPrefIgnoredHandlerCount(), 1); 1110 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4); 1111 1112 registry()->OnIgnoreRegisterProtocolHandler(p2u1); 1113 1114 // p2u1 installed by user but it is already installed by policy, so it is 1115 // added to pref. 1116 ASSERT_EQ(InPrefIgnoredHandlerCount(), 2); 1117 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4); 1118 1119 registry()->RemoveIgnoredHandler(p2u1); 1120 1121 // p2u1 installed by user and policy, so it is removed from pref alone. 1122 ASSERT_EQ(InPrefIgnoredHandlerCount(), 1); 1123 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4); 1124} 1125