Chromium Code Reviews| Index: chrome/browser/custom_handlers/protocol_handler_registry_unittest.cc |
| diff --git a/chrome/browser/custom_handlers/protocol_handler_registry_unittest.cc b/chrome/browser/custom_handlers/protocol_handler_registry_unittest.cc |
| index 7872f81ef2603804c200559a31e5685ada952c52..d2369bf73609998965dd0da763b98bdab8f37afc 100644 |
| --- a/chrome/browser/custom_handlers/protocol_handler_registry_unittest.cc |
| +++ b/chrome/browser/custom_handlers/protocol_handler_registry_unittest.cc |
| @@ -13,6 +13,7 @@ |
| #include "chrome/browser/chrome_notification_types.h" |
| #include "chrome/browser/prefs/pref_service_syncable.h" |
| #include "chrome/common/custom_handlers/protocol_handler.h" |
| +#include "chrome/common/pref_names.h" |
| #include "chrome/test/base/testing_browser_process.h" |
| #include "chrome/test/base/testing_profile.h" |
| #include "components/pref_registry/pref_registry_syncable.h" |
| @@ -103,6 +104,22 @@ void AssertWillHandle( |
| base::MessageLoop::current()->RunUntilIdle(); |
| } |
| +base::DictionaryValue* GetProtocolHandlerValue(std::string protocol, |
| + std::string url) { |
| + base::DictionaryValue* value = new base::DictionaryValue(); |
| + value->SetString("protocol", protocol); |
| + value->SetString("url", url); |
| + return value; |
| +} |
| + |
| +base::DictionaryValue* GetProtocolHandlerValueWithDefault(std::string protocol, |
| + std::string url, |
| + bool is_default) { |
| + base::DictionaryValue* value = GetProtocolHandlerValue(protocol, url); |
| + value->SetBoolean("default", is_default); |
| + return value; |
| +} |
| + |
| class FakeDelegate : public ProtocolHandlerRegistry::Delegate { |
| public: |
| FakeDelegate() : force_os_failure_(false) {} |
| @@ -334,6 +351,36 @@ class ProtocolHandlerRegistryTest : public testing::Test { |
| SetUpRegistry(initialize); |
| } |
| + int InPrefHandlerCount() { |
| + const base::ListValue* in_pref_handlers = |
| + profile()->GetPrefs()->GetList(prefs::kRegisteredProtocolHandlers); |
| + return static_cast<int>(in_pref_handlers->GetSize()); |
| + } |
| + |
| + int InMemoryHandlerCount() { |
| + int in_memory_handler_count = 0; |
| + ProtocolHandlerRegistry::ProtocolHandlerMultiMap::iterator it = |
| + registry()->protocol_handlers_.begin(); |
| + for (; it != registry()->protocol_handlers_.end(); ++it) |
| + in_memory_handler_count += it->second.size(); |
| + return in_memory_handler_count; |
| + } |
| + |
| + int InPrefIgnoredHandlerCount() { |
| + const base::ListValue* in_pref_ignored_handlers = |
| + profile()->GetPrefs()->GetList(prefs::kIgnoredProtocolHandlers); |
| + return static_cast<int>(in_pref_ignored_handlers->GetSize()); |
| + } |
| + |
| + int InMemoryIgnoredHandlerCount() { |
| + int in_memory_ignored_handler_count = 0; |
| + ProtocolHandlerRegistry::ProtocolHandlerList::iterator it = |
| + registry()->ignored_protocol_handlers_.begin(); |
| + for (; it != registry()->ignored_protocol_handlers_.end(); ++it) |
| + in_memory_ignored_handler_count++; |
| + return in_memory_ignored_handler_count; |
| + } |
| + |
| // Returns a new registry, initializing it if |initialize| is true. |
| // Caller assumes ownership for the object |
| void SetUpRegistry(bool initialize) { |
| @@ -916,3 +963,182 @@ TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { |
| registry()->GetRegisteredProtocols(&protocols); |
| ASSERT_EQ(static_cast<size_t>(1), protocols.size()); |
| } |
| + |
| +TEST_F(ProtocolHandlerRegistryTest, TestPrefPolicyOverlapRegister) { |
| + base::ListValue* handlers_registered_by_pref = new base::ListValue(); |
| + base::ListValue* handlers_registered_by_policy = new base::ListValue(); |
| + |
| + handlers_registered_by_pref->Append( |
| + GetProtocolHandlerValueWithDefault("p1", "http://p1u2.com/%s", true)); |
|
koz (OOO until 15th September)
2014/06/01 23:44:29
Cool tests! One nit: Would this be easier to read
kaliamoorthi
2014/06/02 09:47:31
Done.
|
| + handlers_registered_by_pref->Append( |
| + GetProtocolHandlerValueWithDefault("p1", "http://p1u1.com/%s", true)); |
| + handlers_registered_by_pref->Append( |
| + GetProtocolHandlerValueWithDefault("p1", "http://p1u2.com/%s", false)); |
| + |
| + handlers_registered_by_policy->Append( |
| + GetProtocolHandlerValueWithDefault("p1", "http://p1u1.com/%s", false)); |
| + handlers_registered_by_policy->Append( |
| + GetProtocolHandlerValueWithDefault("p3", "http://p3u1.com/%s", true)); |
| + |
| + profile()->GetPrefs()->Set( |
| + prefs::kRegisteredProtocolHandlers, |
| + *static_cast<base::Value*>(handlers_registered_by_pref)); |
| + profile()->GetPrefs()->Set( |
| + prefs::kPolicyRegisteredProtocolHandlers, |
| + *static_cast<base::Value*>(handlers_registered_by_policy)); |
| + registry()->InitProtocolSettings(); |
| + |
| + // Duplicate p1u2 eliminated in memory but not yet saved in pref |
| + ASSERT_EQ(InPrefHandlerCount(), 3); |
| + ASSERT_EQ(InMemoryHandlerCount(), 3); |
| + ASSERT_TRUE(registry()->IsDefault( |
| + CreateProtocolHandler("p1", GURL("http://p1u1.com/%s")))); |
| + ASSERT_FALSE(registry()->IsDefault( |
| + CreateProtocolHandler("p1", GURL("http://p1u2.com/%s")))); |
| + |
| + registry()->OnDenyRegisterProtocolHandler( |
| + CreateProtocolHandler("p2", GURL("http://p2u1.com/%s"))); |
| + |
| + // Duplicate p1u2 saved in pref and a new handler added to pref and memory |
| + ASSERT_EQ(InPrefHandlerCount(), 3); |
| + ASSERT_EQ(InMemoryHandlerCount(), 4); |
| + ASSERT_FALSE(registry()->IsDefault( |
| + CreateProtocolHandler("p2", GURL("http://p2u1.com/%s")))); |
| + |
| + registry()->RemoveHandler( |
| + CreateProtocolHandler("p1", GURL("http://p1u1.com/%s"))); |
| + |
| + // p1u1 removed from user pref but not from memory due to policy. |
| + ASSERT_EQ(InPrefHandlerCount(), 2); |
| + ASSERT_EQ(InMemoryHandlerCount(), 4); |
| + ASSERT_TRUE(registry()->IsDefault( |
| + CreateProtocolHandler("p1", GURL("http://p1u1.com/%s")))); |
| + |
| + registry()->RemoveHandler( |
| + CreateProtocolHandler("p3", GURL("http://p3u1.com/%s"))); |
| + |
| + // p3u1 not removed from memory due to policy and it was never in pref. |
| + ASSERT_EQ(InPrefHandlerCount(), 2); |
| + ASSERT_EQ(InMemoryHandlerCount(), 4); |
| + ASSERT_TRUE(registry()->IsDefault( |
| + CreateProtocolHandler("p3", GURL("http://p3u1.com/%s")))); |
| + |
| + registry()->RemoveHandler( |
| + CreateProtocolHandler("p1", GURL("http://p1u2.com/%s"))); |
| + |
| + // p1u2 removed from user pref and memory. |
| + ASSERT_EQ(InPrefHandlerCount(), 1); |
| + ASSERT_EQ(InMemoryHandlerCount(), 3); |
| + ASSERT_TRUE(registry()->IsDefault( |
| + CreateProtocolHandler("p1", GURL("http://p1u1.com/%s")))); |
| + |
| + registry()->OnAcceptRegisterProtocolHandler( |
| + CreateProtocolHandler("p1", GURL("http://p1u3.com/%s"))); |
| + |
| + // p1u3 added to pref and memory. |
| + ASSERT_EQ(InPrefHandlerCount(), 2); |
| + ASSERT_EQ(InMemoryHandlerCount(), 4); |
| + ASSERT_FALSE(registry()->IsDefault( |
| + CreateProtocolHandler("p1", GURL("http://p1u1.com/%s")))); |
| + ASSERT_TRUE(registry()->IsDefault( |
| + CreateProtocolHandler("p1", GURL("http://p1u3.com/%s")))); |
| + |
| + registry()->RemoveHandler( |
| + CreateProtocolHandler("p1", GURL("http://p1u3.com/%s"))); |
| + |
| + // p1u3 the default handler for p1 removed from user pref and memory. |
| + ASSERT_EQ(InPrefHandlerCount(), 1); |
| + ASSERT_EQ(InMemoryHandlerCount(), 3); |
| + ASSERT_FALSE(registry()->IsDefault( |
| + CreateProtocolHandler("p1", GURL("http://p1u3.com/%s")))); |
| + ASSERT_TRUE(registry()->IsDefault( |
| + CreateProtocolHandler("p1", GURL("http://p1u1.com/%s")))); |
| + ASSERT_TRUE(registry()->IsDefault( |
| + CreateProtocolHandler("p3", GURL("http://p3u1.com/%s")))); |
| + ASSERT_FALSE(registry()->IsDefault( |
| + CreateProtocolHandler("p2", GURL("http://p2u1.com/%s")))); |
| +} |
| + |
| +TEST_F(ProtocolHandlerRegistryTest, TestPrefPolicyOverlapIgnore) { |
| + base::ListValue* handlers_ignored_by_pref = new base::ListValue(); |
| + base::ListValue* handlers_ignored_by_policy = new base::ListValue(); |
| + |
| + handlers_ignored_by_pref->Append( |
| + GetProtocolHandlerValue("p1", "http://p1u1.com/%s")); |
| + handlers_ignored_by_pref->Append( |
| + GetProtocolHandlerValue("p1", "http://p1u2.com/%s")); |
| + handlers_ignored_by_pref->Append( |
| + GetProtocolHandlerValue("p1", "http://p1u2.com/%s")); |
| + handlers_ignored_by_pref->Append( |
| + GetProtocolHandlerValue("p3", "http://p3u1.com/%s")); |
| + |
| + handlers_ignored_by_policy->Append( |
| + GetProtocolHandlerValue("p1", "http://p1u2.com/%s")); |
| + handlers_ignored_by_policy->Append( |
| + GetProtocolHandlerValue("p1", "http://p1u3.com/%s")); |
| + handlers_ignored_by_policy->Append( |
| + GetProtocolHandlerValue("p2", "http://p2u1.com/%s")); |
| + |
| + profile()->GetPrefs()->Set( |
| + prefs::kIgnoredProtocolHandlers, |
| + *static_cast<base::Value*>(handlers_ignored_by_pref)); |
| + profile()->GetPrefs()->Set( |
| + prefs::kPolicyIgnoredProtocolHandlers, |
| + *static_cast<base::Value*>(handlers_ignored_by_policy)); |
| + registry()->InitProtocolSettings(); |
| + |
| + // Duplicate p1u2 eliminated in memory but not yet saved in pref |
| + ASSERT_EQ(InPrefIgnoredHandlerCount(), 4); |
| + ASSERT_EQ(InMemoryIgnoredHandlerCount(), 5); |
| + |
| + registry()->OnIgnoreRegisterProtocolHandler( |
| + CreateProtocolHandler("p2", GURL("http://p2u2.com/%s"))); |
| + |
| + // Duplicate p1u2 eliminated in pref, p2u2 added to pref and memory. |
| + ASSERT_EQ(InPrefIgnoredHandlerCount(), 4); |
| + ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6); |
| + |
| + registry()->RemoveIgnoredHandler( |
| + CreateProtocolHandler("p2", GURL("http://p2u1.com/%s"))); |
| + |
| + // p2u1 installed by policy so cant be removed. |
| + ASSERT_EQ(InPrefIgnoredHandlerCount(), 4); |
| + ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6); |
| + |
| + registry()->RemoveIgnoredHandler( |
| + CreateProtocolHandler("p1", GURL("http://p1u2.com/%s"))); |
| + |
| + // p1u2 installed by policy and pref so it is removed from pref and not from |
| + // memory. |
| + ASSERT_EQ(InPrefIgnoredHandlerCount(), 3); |
| + ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6); |
| + |
| + registry()->RemoveIgnoredHandler( |
| + CreateProtocolHandler("p1", GURL("http://p1u1.com/%s"))); |
| + |
| + // p1u1 installed by pref so it is removed from pref and memory. |
| + ASSERT_EQ(InPrefIgnoredHandlerCount(), 2); |
| + ASSERT_EQ(InMemoryIgnoredHandlerCount(), 5); |
| + |
| + registry()->RemoveIgnoredHandler( |
| + CreateProtocolHandler("p2", GURL("http://p2u2.com/%s"))); |
| + |
| + // p2u2 installed by user so it is removed from pref and memory. |
| + ASSERT_EQ(InPrefIgnoredHandlerCount(), 1); |
| + ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4); |
| + |
| + registry()->OnIgnoreRegisterProtocolHandler( |
| + CreateProtocolHandler("p2", GURL("http://p2u1.com/%s"))); |
| + |
| + // p2u1 installed by user but it is already installed by policy, so it is |
| + // added to pref. |
| + ASSERT_EQ(InPrefIgnoredHandlerCount(), 2); |
| + ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4); |
| + |
| + registry()->RemoveIgnoredHandler( |
| + CreateProtocolHandler("p2", GURL("http://p2u1.com/%s"))); |
| + |
| + // p2u1 installed by user and policy, so it is removed from pref alone. |
| + ASSERT_EQ(InPrefIgnoredHandlerCount(), 1); |
| + ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4); |
| +} |