Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / custom_handlers / protocol_handler_registry_unittest.cc
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
30 using content::BrowserThread;
31
32 namespace {
33
34 void AssertInterceptedIO(
35     const GURL& url,
36     net::URLRequestJobFactory* interceptor) {
37   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
38   net::URLRequestContext context;
39   scoped_ptr<net::URLRequest> request(context.CreateRequest(
40       url, net::DEFAULT_PRIORITY, NULL, NULL));
41   scoped_refptr<net::URLRequestJob> job =
42       interceptor->MaybeCreateJobWithProtocolHandler(
43           url.scheme(), request.get(), context.network_delegate());
44   ASSERT_TRUE(job.get() != NULL);
45 }
46
47 void AssertIntercepted(
48     const GURL& url,
49     net::URLRequestJobFactory* interceptor) {
50   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
51   BrowserThread::PostTask(BrowserThread::IO,
52                           FROM_HERE,
53                           base::Bind(AssertInterceptedIO,
54                                      url,
55                                      base::Unretained(interceptor)));
56   base::MessageLoop::current()->RunUntilIdle();
57 }
58
59 // FakeURLRequestJobFactory returns NULL for all job creation requests and false
60 // for all IsHandled*() requests. FakeURLRequestJobFactory can be chained to
61 // ProtocolHandlerRegistry::JobInterceptorFactory so the result of
62 // MaybeCreateJobWithProtocolHandler() indicates whether the
63 // ProtocolHandlerRegistry properly handled a job creation request.
64 class FakeURLRequestJobFactory : public net::URLRequestJobFactory {
65   // net::URLRequestJobFactory implementation:
66   net::URLRequestJob* MaybeCreateJobWithProtocolHandler(
67       const std::string& scheme,
68       net::URLRequest* request,
69       net::NetworkDelegate* network_delegate) const override {
70     return NULL;
71   }
72
73   net::URLRequestJob* MaybeInterceptRedirect(
74       net::URLRequest* request,
75       net::NetworkDelegate* network_delegate,
76       const GURL& location) const override {
77     return nullptr;
78   }
79
80   net::URLRequestJob* MaybeInterceptResponse(
81       net::URLRequest* request,
82       net::NetworkDelegate* network_delegate) const override {
83     return nullptr;
84   }
85
86   bool IsHandledProtocol(const std::string& scheme) const override {
87     return false;
88   }
89   bool IsHandledURL(const GURL& url) const override { return false; }
90   bool IsSafeRedirectTarget(const GURL& location) const override {
91     return true;
92   }
93 };
94
95 void AssertWillHandleIO(
96     const std::string& scheme,
97     bool expected,
98     ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) {
99   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
100   interceptor->Chain(scoped_ptr<net::URLRequestJobFactory>(
101       new FakeURLRequestJobFactory()));
102   ASSERT_EQ(expected, interceptor->IsHandledProtocol(scheme));
103   interceptor->Chain(scoped_ptr<net::URLRequestJobFactory>());
104 }
105
106 void AssertWillHandle(
107     const std::string& scheme,
108     bool expected,
109     ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) {
110   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
111   BrowserThread::PostTask(BrowserThread::IO,
112                           FROM_HERE,
113                           base::Bind(AssertWillHandleIO,
114                                      scheme,
115                                      expected,
116                                      base::Unretained(interceptor)));
117   base::MessageLoop::current()->RunUntilIdle();
118 }
119
120 base::DictionaryValue* GetProtocolHandlerValue(std::string protocol,
121                                                std::string url) {
122   base::DictionaryValue* value = new base::DictionaryValue();
123   value->SetString("protocol", protocol);
124   value->SetString("url", url);
125   return value;
126 }
127
128 base::DictionaryValue* GetProtocolHandlerValueWithDefault(std::string protocol,
129                                                           std::string url,
130                                                           bool is_default) {
131   base::DictionaryValue* value = GetProtocolHandlerValue(protocol, url);
132   value->SetBoolean("default", is_default);
133   return value;
134 }
135
136 class FakeDelegate : public ProtocolHandlerRegistry::Delegate {
137  public:
138   FakeDelegate() : force_os_failure_(false) {}
139   ~FakeDelegate() override {}
140   void RegisterExternalHandler(const std::string& protocol) override {
141     ASSERT_TRUE(
142         registered_protocols_.find(protocol) == registered_protocols_.end());
143     registered_protocols_.insert(protocol);
144   }
145
146   void DeregisterExternalHandler(const std::string& protocol) override {
147     registered_protocols_.erase(protocol);
148   }
149
150   ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker(
151       ShellIntegration::DefaultWebClientObserver* observer,
152       const std::string& protocol) override;
153
154   ProtocolHandlerRegistry::DefaultClientObserver* CreateShellObserver(
155       ProtocolHandlerRegistry* registry) override;
156
157   void RegisterWithOSAsDefaultClient(const std::string& protocol,
158                                      ProtocolHandlerRegistry* reg) override {
159     ProtocolHandlerRegistry::Delegate::RegisterWithOSAsDefaultClient(protocol,
160                                                                      reg);
161     ASSERT_FALSE(IsFakeRegisteredWithOS(protocol));
162   }
163
164   bool IsExternalHandlerRegistered(const std::string& protocol) override {
165     return registered_protocols_.find(protocol) != registered_protocols_.end();
166   }
167
168   bool IsFakeRegisteredWithOS(const std::string& protocol) {
169     return os_registered_protocols_.find(protocol) !=
170         os_registered_protocols_.end();
171   }
172
173   void FakeRegisterWithOS(const std::string& protocol) {
174     os_registered_protocols_.insert(protocol);
175   }
176
177   void Reset() {
178     registered_protocols_.clear();
179     os_registered_protocols_.clear();
180     force_os_failure_ = false;
181   }
182
183   void set_force_os_failure(bool force) { force_os_failure_ = force; }
184
185   bool force_os_failure() { return force_os_failure_; }
186
187  private:
188   std::set<std::string> registered_protocols_;
189   std::set<std::string> os_registered_protocols_;
190   bool force_os_failure_;
191 };
192
193 class FakeClientObserver
194     : public ProtocolHandlerRegistry::DefaultClientObserver {
195  public:
196   FakeClientObserver(ProtocolHandlerRegistry* registry,
197                      FakeDelegate* registry_delegate)
198       : ProtocolHandlerRegistry::DefaultClientObserver(registry),
199         delegate_(registry_delegate) {}
200
201   void SetDefaultWebClientUIState(
202       ShellIntegration::DefaultWebClientUIState state) override {
203     ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState(
204         state);
205     if (state == ShellIntegration::STATE_IS_DEFAULT) {
206       delegate_->FakeRegisterWithOS(worker_->protocol());
207     }
208     if (state != ShellIntegration::STATE_PROCESSING) {
209       base::MessageLoop::current()->Quit();
210     }
211   }
212
213  private:
214   FakeDelegate* delegate_;
215 };
216
217 class FakeProtocolClientWorker
218     : public ShellIntegration::DefaultProtocolClientWorker {
219  public:
220   FakeProtocolClientWorker(ShellIntegration::DefaultWebClientObserver* observer,
221                            const std::string& protocol,
222                            bool force_failure)
223       : ShellIntegration::DefaultProtocolClientWorker(observer, protocol),
224         force_failure_(force_failure) {}
225
226  private:
227   ~FakeProtocolClientWorker() override {}
228
229   ShellIntegration::DefaultWebClientState CheckIsDefault() override {
230     if (force_failure_) {
231       return ShellIntegration::NOT_DEFAULT;
232     } else {
233       return ShellIntegration::IS_DEFAULT;
234     }
235   }
236
237   bool SetAsDefault(bool interactive_permitted) override { return true; }
238
239  private:
240   bool force_failure_;
241 };
242
243 ProtocolHandlerRegistry::DefaultClientObserver*
244     FakeDelegate::CreateShellObserver(ProtocolHandlerRegistry* registry) {
245   return new FakeClientObserver(registry, this);
246 }
247
248 ShellIntegration::DefaultProtocolClientWorker* FakeDelegate::CreateShellWorker(
249     ShellIntegration::DefaultWebClientObserver* observer,
250     const std::string& protocol) {
251   return new FakeProtocolClientWorker(observer, protocol, force_os_failure_);
252 }
253
254 class NotificationCounter : public content::NotificationObserver {
255  public:
256   explicit NotificationCounter(content::BrowserContext* context)
257       : events_(0),
258         notification_registrar_() {
259     notification_registrar_.Add(this,
260         chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED,
261             content::Source<content::BrowserContext>(context));
262   }
263
264   int events() { return events_; }
265   bool notified() { return events_ > 0; }
266   void Clear() { events_ = 0; }
267   void Observe(int type,
268                const content::NotificationSource& source,
269                const content::NotificationDetails& details) override {
270     ++events_;
271   }
272
273   int events_;
274   content::NotificationRegistrar notification_registrar_;
275 };
276
277 class QueryProtocolHandlerOnChange
278     : public content::NotificationObserver {
279  public:
280   QueryProtocolHandlerOnChange(content::BrowserContext* context,
281                                ProtocolHandlerRegistry* registry)
282     : local_registry_(registry),
283       called_(false),
284       notification_registrar_() {
285     notification_registrar_.Add(this,
286         chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED,
287             content::Source<content::BrowserContext>(context));
288   }
289
290   void Observe(int type,
291                const content::NotificationSource& source,
292                const content::NotificationDetails& details) override {
293     std::vector<std::string> output;
294     local_registry_->GetRegisteredProtocols(&output);
295     called_ = true;
296   }
297
298   ProtocolHandlerRegistry* local_registry_;
299   bool called_;
300   content::NotificationRegistrar notification_registrar_;
301 };
302
303 // URLRequest DCHECKS that the current MessageLoop is IO. It does this because
304 // it can't check the thread id (since net can't depend on content.) We want
305 // to harness our tests so all threads use the same loop allowing us to
306 // guarantee all messages are processed.) By overriding the IsType method
307 // we basically ignore the supplied message loop type, and instead infer
308 // our type based on the current thread. GO DEPENDENCY INJECTION!
309 class TestMessageLoop : public base::MessageLoop {
310  public:
311   TestMessageLoop() {}
312   ~TestMessageLoop() override {}
313   bool IsType(base::MessageLoop::Type type) const override {
314     switch (type) {
315       case base::MessageLoop::TYPE_UI:
316         return BrowserThread::CurrentlyOn(BrowserThread::UI);
317       case base::MessageLoop::TYPE_IO:
318         return BrowserThread::CurrentlyOn(BrowserThread::IO);
319 #if defined(OS_ANDROID)
320       case base::MessageLoop::TYPE_JAVA: // fall-through
321 #endif // defined(OS_ANDROID)
322       case base::MessageLoop::TYPE_CUSTOM:
323       case base::MessageLoop::TYPE_DEFAULT:
324         return !BrowserThread::CurrentlyOn(BrowserThread::UI) &&
325                !BrowserThread::CurrentlyOn(BrowserThread::IO);
326     }
327     return false;
328   }
329 };
330
331 }  // namespace
332
333 class ProtocolHandlerRegistryTest : public testing::Test {
334  protected:
335   ProtocolHandlerRegistryTest()
336   : ui_thread_(BrowserThread::UI, &loop_),
337     file_thread_(BrowserThread::FILE, &loop_),
338     io_thread_(BrowserThread::IO, &loop_),
339     test_protocol_handler_(CreateProtocolHandler("test", "test")) {}
340
341   FakeDelegate* delegate() const { return delegate_; }
342   ProtocolHandlerRegistry* registry() { return registry_.get(); }
343   TestingProfile* profile() const { return profile_.get(); }
344   const ProtocolHandler& test_protocol_handler() const {
345     return test_protocol_handler_;
346   }
347
348   ProtocolHandler CreateProtocolHandler(const std::string& protocol,
349                                         const GURL& url) {
350     return ProtocolHandler::CreateProtocolHandler(protocol, url);
351   }
352
353   ProtocolHandler CreateProtocolHandler(const std::string& protocol,
354                                         const std::string& name) {
355     return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s"));
356   }
357
358   void RecreateRegistry(bool initialize) {
359     TeadDownRegistry();
360     SetUpRegistry(initialize);
361   }
362
363   int InPrefHandlerCount() {
364     const base::ListValue* in_pref_handlers =
365         profile()->GetPrefs()->GetList(prefs::kRegisteredProtocolHandlers);
366     return static_cast<int>(in_pref_handlers->GetSize());
367   }
368
369   int InMemoryHandlerCount() {
370     int in_memory_handler_count = 0;
371     ProtocolHandlerRegistry::ProtocolHandlerMultiMap::iterator it =
372         registry()->protocol_handlers_.begin();
373     for (; it != registry()->protocol_handlers_.end(); ++it)
374       in_memory_handler_count += it->second.size();
375     return in_memory_handler_count;
376   }
377
378   int InPrefIgnoredHandlerCount() {
379     const base::ListValue* in_pref_ignored_handlers =
380         profile()->GetPrefs()->GetList(prefs::kIgnoredProtocolHandlers);
381     return static_cast<int>(in_pref_ignored_handlers->GetSize());
382   }
383
384   int InMemoryIgnoredHandlerCount() {
385     int in_memory_ignored_handler_count = 0;
386     ProtocolHandlerRegistry::ProtocolHandlerList::iterator it =
387         registry()->ignored_protocol_handlers_.begin();
388     for (; it != registry()->ignored_protocol_handlers_.end(); ++it)
389       in_memory_ignored_handler_count++;
390     return in_memory_ignored_handler_count;
391   }
392
393   // Returns a new registry, initializing it if |initialize| is true.
394   // Caller assumes ownership for the object
395   void SetUpRegistry(bool initialize) {
396     delegate_ = new FakeDelegate();
397     registry_.reset(new ProtocolHandlerRegistry(profile(), delegate()));
398     if (initialize) registry_->InitProtocolSettings();
399   }
400
401   void TeadDownRegistry() {
402     registry_->Shutdown();
403     registry_.reset();
404     // Registry owns the delegate_ it handles deletion of that object.
405   }
406
407   void SetUp() override {
408     profile_.reset(new TestingProfile());
409     CHECK(profile_->GetPrefs());
410     SetUpRegistry(true);
411     test_protocol_handler_ =
412         CreateProtocolHandler("test", GURL("http://test.com/%s"));
413   }
414
415   void TearDown() override { TeadDownRegistry(); }
416
417   TestMessageLoop loop_;
418
419  private:
420   content::TestBrowserThread ui_thread_;
421   content::TestBrowserThread file_thread_;
422   content::TestBrowserThread io_thread_;
423
424   scoped_ptr<TestingProfile> profile_;
425   FakeDelegate* delegate_;  // Registry assumes ownership of delegate_.
426   scoped_ptr<ProtocolHandlerRegistry> registry_;
427   ProtocolHandler test_protocol_handler_;
428 };
429
430 // ProtocolHandlerRegistryTest tests are flaky on Linux & ChromeOS.
431 // http://crbug.com/133023
432 #if defined(OS_LINUX)
433 #define MAYBE_AcceptProtocolHandlerHandlesProtocol \
434     DISABLED_AcceptProtocolHandlerHandlesProtocol
435 #define MAYBE_DeniedProtocolIsntHandledUntilAccepted \
436     DISABLED_DeniedProtocolIsntHandledUntilAccepted
437 #define MAYBE_TestStartsAsDefault DISABLED_TestStartsAsDefault
438 #define MAYBE_TestRemoveHandlerRemovesDefault \
439     DISABLED_TestRemoveHandlerRemovesDefault
440 #define MAYBE_TestClearDefaultGetsPropagatedToIO \
441     DISABLED_TestClearDefaultGetsPropagatedToIO
442 #define MAYBE_TestIsHandledProtocolWorksOnIOThread \
443     DISABLED_TestIsHandledProtocolWorksOnIOThread
444 #define MAYBE_TestInstallDefaultHandler \
445     DISABLED_TestInstallDefaultHandler
446 #else
447 #define MAYBE_AcceptProtocolHandlerHandlesProtocol \
448     AcceptProtocolHandlerHandlesProtocol
449 #define MAYBE_DeniedProtocolIsntHandledUntilAccepted \
450     DeniedProtocolIsntHandledUntilAccepted
451 #define MAYBE_TestStartsAsDefault TestStartsAsDefault
452 #define MAYBE_TestRemoveHandlerRemovesDefault TestRemoveHandlerRemovesDefault
453 #define MAYBE_TestClearDefaultGetsPropagatedToIO \
454     TestClearDefaultGetsPropagatedToIO
455 #define MAYBE_TestIsHandledProtocolWorksOnIOThread \
456     TestIsHandledProtocolWorksOnIOThread
457 #define MAYBE_TestInstallDefaultHandler TestInstallDefaultHandler
458 #endif  // defined(OS_LINUX)
459
460 TEST_F(ProtocolHandlerRegistryTest,
461        MAYBE_AcceptProtocolHandlerHandlesProtocol) {
462   ASSERT_FALSE(registry()->IsHandledProtocol("test"));
463   registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler());
464   ASSERT_TRUE(registry()->IsHandledProtocol("test"));
465 }
466
467 TEST_F(ProtocolHandlerRegistryTest,
468        MAYBE_DeniedProtocolIsntHandledUntilAccepted) {
469   registry()->OnDenyRegisterProtocolHandler(test_protocol_handler());
470   ASSERT_FALSE(registry()->IsHandledProtocol("test"));
471   registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler());
472   ASSERT_TRUE(registry()->IsHandledProtocol("test"));
473 }
474
475 TEST_F(ProtocolHandlerRegistryTest, ClearDefaultMakesProtocolNotHandled) {
476   registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler());
477   registry()->ClearDefault("test");
478   ASSERT_FALSE(registry()->IsHandledProtocol("test"));
479   ASSERT_TRUE(registry()->GetHandlerFor("test").IsEmpty());
480 }
481
482 TEST_F(ProtocolHandlerRegistryTest, DisableDeregistersProtocolHandlers) {
483   ASSERT_FALSE(delegate()->IsExternalHandlerRegistered("test"));
484   registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler());
485   ASSERT_TRUE(delegate()->IsExternalHandlerRegistered("test"));
486
487   registry()->Disable();
488   ASSERT_FALSE(delegate()->IsExternalHandlerRegistered("test"));
489   registry()->Enable();
490   ASSERT_TRUE(delegate()->IsExternalHandlerRegistered("test"));
491 }
492
493 TEST_F(ProtocolHandlerRegistryTest, IgnoreProtocolHandler) {
494   registry()->OnIgnoreRegisterProtocolHandler(test_protocol_handler());
495   ASSERT_TRUE(registry()->IsIgnored(test_protocol_handler()));
496
497   registry()->RemoveIgnoredHandler(test_protocol_handler());
498   ASSERT_FALSE(registry()->IsIgnored(test_protocol_handler()));
499 }
500
501 TEST_F(ProtocolHandlerRegistryTest, IgnoreEquivalentProtocolHandler) {
502   ProtocolHandler ph1 = CreateProtocolHandler("test", GURL("http://test/%s"));
503   ProtocolHandler ph2 = CreateProtocolHandler("test", GURL("http://test/%s"));
504
505   registry()->OnIgnoreRegisterProtocolHandler(ph1);
506   ASSERT_TRUE(registry()->IsIgnored(ph1));
507   ASSERT_TRUE(registry()->HasIgnoredEquivalent(ph2));
508
509   registry()->RemoveIgnoredHandler(ph1);
510   ASSERT_FALSE(registry()->IsIgnored(ph1));
511   ASSERT_FALSE(registry()->HasIgnoredEquivalent(ph2));
512 }
513
514 TEST_F(ProtocolHandlerRegistryTest, SaveAndLoad) {
515   ProtocolHandler stuff_protocol_handler(
516       CreateProtocolHandler("stuff", "stuff"));
517   registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler());
518   registry()->OnIgnoreRegisterProtocolHandler(stuff_protocol_handler);
519
520   ASSERT_TRUE(registry()->IsHandledProtocol("test"));
521   ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler));
522   delegate()->Reset();
523   RecreateRegistry(true);
524   ASSERT_TRUE(registry()->IsHandledProtocol("test"));
525   ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler));
526 }
527
528 TEST_F(ProtocolHandlerRegistryTest, TestEnabledDisabled) {
529   registry()->Disable();
530   ASSERT_FALSE(registry()->enabled());
531   registry()->Enable();
532   ASSERT_TRUE(registry()->enabled());
533 }
534
535 TEST_F(ProtocolHandlerRegistryTest,
536     DisallowRegisteringExternallyHandledProtocols) {
537   delegate()->RegisterExternalHandler("test");
538   ASSERT_FALSE(registry()->CanSchemeBeOverridden("test"));
539 }
540
541 TEST_F(ProtocolHandlerRegistryTest, RemovingHandlerMeansItCanBeAddedAgain) {
542   registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler());
543   ASSERT_TRUE(registry()->CanSchemeBeOverridden("test"));
544   registry()->RemoveHandler(test_protocol_handler());
545   ASSERT_TRUE(registry()->CanSchemeBeOverridden("test"));
546 }
547
548 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestStartsAsDefault) {
549   registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler());
550   ASSERT_TRUE(registry()->IsDefault(test_protocol_handler()));
551 }
552
553 TEST_F(ProtocolHandlerRegistryTest, TestClearDefault) {
554   ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
555   ProtocolHandler ph2 = CreateProtocolHandler("test", "test2");
556   registry()->OnAcceptRegisterProtocolHandler(ph1);
557   registry()->OnAcceptRegisterProtocolHandler(ph2);
558
559   registry()->OnAcceptRegisterProtocolHandler(ph1);
560   registry()->ClearDefault("test");
561   ASSERT_FALSE(registry()->IsDefault(ph1));
562   ASSERT_FALSE(registry()->IsDefault(ph2));
563 }
564
565 TEST_F(ProtocolHandlerRegistryTest, TestGetHandlerFor) {
566   ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
567   ProtocolHandler ph2 = CreateProtocolHandler("test", "test2");
568   registry()->OnAcceptRegisterProtocolHandler(ph1);
569   registry()->OnAcceptRegisterProtocolHandler(ph2);
570
571   registry()->OnAcceptRegisterProtocolHandler(ph2);
572   ASSERT_EQ(ph2, registry()->GetHandlerFor("test"));
573   ASSERT_TRUE(registry()->IsHandledProtocol("test"));
574 }
575
576 TEST_F(ProtocolHandlerRegistryTest, TestMostRecentHandlerIsDefault) {
577   ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
578   ProtocolHandler ph2 = CreateProtocolHandler("test", "test2");
579   registry()->OnAcceptRegisterProtocolHandler(ph1);
580   registry()->OnAcceptRegisterProtocolHandler(ph2);
581   ASSERT_FALSE(registry()->IsDefault(ph1));
582   ASSERT_TRUE(registry()->IsDefault(ph2));
583 }
584
585 TEST_F(ProtocolHandlerRegistryTest, TestOnAcceptRegisterProtocolHandler) {
586   ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
587   ProtocolHandler ph2 = CreateProtocolHandler("test", "test2");
588   registry()->OnAcceptRegisterProtocolHandler(ph1);
589   registry()->OnAcceptRegisterProtocolHandler(ph2);
590
591   registry()->OnAcceptRegisterProtocolHandler(ph1);
592   ASSERT_TRUE(registry()->IsDefault(ph1));
593   ASSERT_FALSE(registry()->IsDefault(ph2));
594
595   registry()->OnAcceptRegisterProtocolHandler(ph2);
596   ASSERT_FALSE(registry()->IsDefault(ph1));
597   ASSERT_TRUE(registry()->IsDefault(ph2));
598 }
599
600 TEST_F(ProtocolHandlerRegistryTest, TestDefaultSaveLoad) {
601   ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
602   ProtocolHandler ph2 = CreateProtocolHandler("test", "test2");
603   registry()->OnDenyRegisterProtocolHandler(ph1);
604   registry()->OnDenyRegisterProtocolHandler(ph2);
605
606   registry()->OnAcceptRegisterProtocolHandler(ph2);
607   registry()->Disable();
608
609   RecreateRegistry(true);
610
611   ASSERT_FALSE(registry()->enabled());
612   registry()->Enable();
613   ASSERT_FALSE(registry()->IsDefault(ph1));
614   ASSERT_TRUE(registry()->IsDefault(ph2));
615
616   RecreateRegistry(true);
617   ASSERT_TRUE(registry()->enabled());
618 }
619
620 TEST_F(ProtocolHandlerRegistryTest, TestRemoveHandler) {
621   ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
622   registry()->OnAcceptRegisterProtocolHandler(ph1);
623   registry()->OnAcceptRegisterProtocolHandler(ph1);
624
625   registry()->RemoveHandler(ph1);
626   ASSERT_FALSE(registry()->IsRegistered(ph1));
627   ASSERT_FALSE(registry()->IsHandledProtocol("test"));
628 }
629
630 TEST_F(ProtocolHandlerRegistryTest, TestIsRegistered) {
631   ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
632   ProtocolHandler ph2 = CreateProtocolHandler("test", "test2");
633   registry()->OnAcceptRegisterProtocolHandler(ph1);
634   registry()->OnAcceptRegisterProtocolHandler(ph2);
635
636   ASSERT_TRUE(registry()->IsRegistered(ph1));
637 }
638
639 TEST_F(ProtocolHandlerRegistryTest, TestIsEquivalentRegistered) {
640   ProtocolHandler ph1 = CreateProtocolHandler("test", GURL("http://test/%s"));
641   ProtocolHandler ph2 = CreateProtocolHandler("test", GURL("http://test/%s"));
642   registry()->OnAcceptRegisterProtocolHandler(ph1);
643
644   ASSERT_TRUE(registry()->IsRegistered(ph1));
645   ASSERT_TRUE(registry()->HasRegisteredEquivalent(ph2));
646 }
647
648 TEST_F(ProtocolHandlerRegistryTest, TestSilentlyRegisterHandler) {
649   ProtocolHandler ph1 = CreateProtocolHandler("test", GURL("http://test/1/%s"));
650   ProtocolHandler ph2 = CreateProtocolHandler("test", GURL("http://test/2/%s"));
651   ProtocolHandler ph3 = CreateProtocolHandler("ignore", GURL("http://test/%s"));
652   ProtocolHandler ph4 = CreateProtocolHandler("ignore", GURL("http://test/%s"));
653
654   ASSERT_FALSE(registry()->SilentlyHandleRegisterHandlerRequest(ph1));
655   ASSERT_FALSE(registry()->IsRegistered(ph1));
656
657   registry()->OnAcceptRegisterProtocolHandler(ph1);
658   ASSERT_TRUE(registry()->IsRegistered(ph1));
659
660   ASSERT_TRUE(registry()->SilentlyHandleRegisterHandlerRequest(ph2));
661   ASSERT_FALSE(registry()->IsRegistered(ph1));
662   ASSERT_TRUE(registry()->IsRegistered(ph2));
663
664   ASSERT_FALSE(registry()->SilentlyHandleRegisterHandlerRequest(ph3));
665   ASSERT_FALSE(registry()->IsRegistered(ph3));
666
667   registry()->OnIgnoreRegisterProtocolHandler(ph3);
668   ASSERT_FALSE(registry()->IsRegistered(ph3));
669   ASSERT_TRUE(registry()->IsIgnored(ph3));
670
671   ASSERT_TRUE(registry()->SilentlyHandleRegisterHandlerRequest(ph4));
672   ASSERT_FALSE(registry()->IsRegistered(ph4));
673   ASSERT_TRUE(registry()->HasIgnoredEquivalent(ph4));
674 }
675
676 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestRemoveHandlerRemovesDefault) {
677   ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
678   ProtocolHandler ph2 = CreateProtocolHandler("test", "test2");
679   ProtocolHandler ph3 = CreateProtocolHandler("test", "test3");
680
681   registry()->OnAcceptRegisterProtocolHandler(ph1);
682   registry()->OnAcceptRegisterProtocolHandler(ph2);
683   registry()->OnAcceptRegisterProtocolHandler(ph3);
684
685   registry()->OnAcceptRegisterProtocolHandler(ph1);
686   registry()->RemoveHandler(ph1);
687   ASSERT_FALSE(registry()->IsDefault(ph1));
688 }
689
690 TEST_F(ProtocolHandlerRegistryTest, TestGetHandlersFor) {
691   ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
692   ProtocolHandler ph2 = CreateProtocolHandler("test", "test2");
693   ProtocolHandler ph3 = CreateProtocolHandler("test", "test3");
694   registry()->OnAcceptRegisterProtocolHandler(ph1);
695   registry()->OnAcceptRegisterProtocolHandler(ph2);
696   registry()->OnAcceptRegisterProtocolHandler(ph3);
697
698   ProtocolHandlerRegistry::ProtocolHandlerList handlers =
699       registry()->GetHandlersFor("test");
700   ASSERT_EQ(static_cast<size_t>(3), handlers.size());
701
702   ASSERT_EQ(ph3, handlers[0]);
703   ASSERT_EQ(ph2, handlers[1]);
704   ASSERT_EQ(ph1, handlers[2]);
705 }
706
707 TEST_F(ProtocolHandlerRegistryTest, TestGetRegisteredProtocols) {
708   std::vector<std::string> protocols;
709   registry()->GetRegisteredProtocols(&protocols);
710   ASSERT_EQ(static_cast<size_t>(0), protocols.size());
711
712   registry()->GetHandlersFor("test");
713
714   protocols.clear();
715   registry()->GetRegisteredProtocols(&protocols);
716   ASSERT_EQ(static_cast<size_t>(0), protocols.size());
717 }
718
719 TEST_F(ProtocolHandlerRegistryTest, TestIsHandledProtocol) {
720   registry()->GetHandlersFor("test");
721   ASSERT_FALSE(registry()->IsHandledProtocol("test"));
722 }
723
724 TEST_F(ProtocolHandlerRegistryTest, TestNotifications) {
725   ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
726   NotificationCounter counter(profile());
727
728   registry()->OnAcceptRegisterProtocolHandler(ph1);
729   ASSERT_TRUE(counter.notified());
730   counter.Clear();
731
732   registry()->Disable();
733   ASSERT_TRUE(counter.notified());
734   counter.Clear();
735
736   registry()->Enable();
737   ASSERT_TRUE(counter.notified());
738   counter.Clear();
739
740   registry()->RemoveHandler(ph1);
741   ASSERT_TRUE(counter.notified());
742   counter.Clear();
743 }
744
745 TEST_F(ProtocolHandlerRegistryTest, TestReentrantNotifications) {
746   QueryProtocolHandlerOnChange queryer(profile(), registry());
747   ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
748   registry()->OnAcceptRegisterProtocolHandler(ph1);
749   ASSERT_TRUE(queryer.called_);
750 }
751
752 TEST_F(ProtocolHandlerRegistryTest, TestProtocolsWithNoDefaultAreHandled) {
753   ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
754   registry()->OnAcceptRegisterProtocolHandler(ph1);
755   registry()->ClearDefault("test");
756   std::vector<std::string> handled_protocols;
757   registry()->GetRegisteredProtocols(&handled_protocols);
758   ASSERT_EQ(static_cast<size_t>(1), handled_protocols.size());
759   ASSERT_EQ("test", handled_protocols[0]);
760 }
761
762 TEST_F(ProtocolHandlerRegistryTest, TestDisablePreventsHandling) {
763   ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
764   registry()->OnAcceptRegisterProtocolHandler(ph1);
765   ASSERT_TRUE(registry()->IsHandledProtocol("test"));
766   registry()->Disable();
767   ASSERT_FALSE(registry()->IsHandledProtocol("test"));
768 }
769
770 // TODO(smckay): This is much more appropriately an integration
771 // test. Make that so, then update the
772 // ShellIntegretion{Delegate,Observer,Worker} test classes we use to fully
773 // isolate this test from the FILE thread.
774 TEST_F(ProtocolHandlerRegistryTest, TestOSRegistration) {
775   ProtocolHandler ph_do1 = CreateProtocolHandler("do", "test1");
776   ProtocolHandler ph_do2 = CreateProtocolHandler("do", "test2");
777   ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test");
778
779   ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("do"));
780   ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont"));
781
782   registry()->OnAcceptRegisterProtocolHandler(ph_do1);
783   registry()->OnDenyRegisterProtocolHandler(ph_dont);
784   base::MessageLoop::current()->Run();  // FILE thread needs to run.
785   ASSERT_TRUE(delegate()->IsFakeRegisteredWithOS("do"));
786   ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont"));
787
788   // This should not register with the OS, if it does the delegate
789   // will assert for us. We don't need to wait for the message loop
790   // as it should not go through to the shell worker.
791   registry()->OnAcceptRegisterProtocolHandler(ph_do2);
792 }
793
794 #if defined(OS_LINUX)
795 // TODO(benwells): When Linux support is more reliable and
796 // http://crbut.com/88255 is fixed this test will pass.
797 #define MAYBE_TestOSRegistrationFailure DISABLED_TestOSRegistrationFailure
798 #else
799 #define MAYBE_TestOSRegistrationFailure TestOSRegistrationFailure
800 #endif
801
802 // TODO(smckay): This is much more appropriately an integration
803 // test. Make that so, then update the
804 // ShellIntegretion{Delegate,Observer,Worker} test classes we use to fully
805 // isolate this test from the FILE thread.
806 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestOSRegistrationFailure) {
807   ProtocolHandler ph_do = CreateProtocolHandler("do", "test1");
808   ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test");
809
810   ASSERT_FALSE(registry()->IsHandledProtocol("do"));
811   ASSERT_FALSE(registry()->IsHandledProtocol("dont"));
812
813   registry()->OnAcceptRegisterProtocolHandler(ph_do);
814   base::MessageLoop::current()->Run();  // FILE thread needs to run.
815   delegate()->set_force_os_failure(true);
816   registry()->OnAcceptRegisterProtocolHandler(ph_dont);
817   base::MessageLoop::current()->Run();  // FILE thread needs to run.
818   ASSERT_TRUE(registry()->IsHandledProtocol("do"));
819   ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size());
820   ASSERT_FALSE(registry()->IsHandledProtocol("dont"));
821   ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size());
822 }
823
824 TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) {
825   ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1");
826   registry()->OnAcceptRegisterProtocolHandler(ph1);
827   GURL url("mailto:someone@something.com");
828
829   scoped_ptr<net::URLRequestJobFactory> interceptor(
830       registry()->CreateJobInterceptorFactory());
831   AssertIntercepted(url, interceptor.get());
832 }
833
834 TEST_F(ProtocolHandlerRegistryTest,
835        MAYBE_TestIsHandledProtocolWorksOnIOThread) {
836   std::string scheme("mailto");
837   ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1");
838   registry()->OnAcceptRegisterProtocolHandler(ph1);
839
840   scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor(
841       registry()->CreateJobInterceptorFactory());
842   AssertWillHandle(scheme, true, interceptor.get());
843 }
844
845 TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultFallsBackToOldDefault) {
846   ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1");
847   ProtocolHandler ph2 = CreateProtocolHandler("mailto", "test2");
848   ProtocolHandler ph3 = CreateProtocolHandler("mailto", "test3");
849   registry()->OnAcceptRegisterProtocolHandler(ph1);
850   registry()->OnAcceptRegisterProtocolHandler(ph2);
851   registry()->OnAcceptRegisterProtocolHandler(ph3);
852
853   ASSERT_TRUE(registry()->IsDefault(ph3));
854   registry()->RemoveHandler(ph3);
855   ASSERT_TRUE(registry()->IsDefault(ph2));
856   registry()->OnAcceptRegisterProtocolHandler(ph3);
857   ASSERT_TRUE(registry()->IsDefault(ph3));
858   registry()->RemoveHandler(ph2);
859   ASSERT_TRUE(registry()->IsDefault(ph3));
860   registry()->RemoveHandler(ph3);
861   ASSERT_TRUE(registry()->IsDefault(ph1));
862 }
863
864 TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultDoesntChangeHandlers) {
865   ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1");
866   ProtocolHandler ph2 = CreateProtocolHandler("mailto", "test2");
867   ProtocolHandler ph3 = CreateProtocolHandler("mailto", "test3");
868   registry()->OnAcceptRegisterProtocolHandler(ph1);
869   registry()->OnAcceptRegisterProtocolHandler(ph2);
870   registry()->OnAcceptRegisterProtocolHandler(ph3);
871   registry()->RemoveHandler(ph3);
872
873   ProtocolHandlerRegistry::ProtocolHandlerList handlers =
874       registry()->GetHandlersFor("mailto");
875   ASSERT_EQ(static_cast<size_t>(2), handlers.size());
876
877   ASSERT_EQ(ph2, handlers[0]);
878   ASSERT_EQ(ph1, handlers[1]);
879 }
880
881 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestClearDefaultGetsPropagatedToIO) {
882   std::string scheme("mailto");
883   ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1");
884   registry()->OnAcceptRegisterProtocolHandler(ph1);
885   registry()->ClearDefault(scheme);
886
887   scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor(
888       registry()->CreateJobInterceptorFactory());
889   AssertWillHandle(scheme, false, interceptor.get());
890 }
891
892 TEST_F(ProtocolHandlerRegistryTest, TestLoadEnabledGetsPropogatedToIO) {
893   std::string mailto("mailto");
894   ProtocolHandler ph1 = CreateProtocolHandler(mailto, "MailtoHandler");
895   registry()->OnAcceptRegisterProtocolHandler(ph1);
896
897   scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor(
898       registry()->CreateJobInterceptorFactory());
899   AssertWillHandle(mailto, true, interceptor.get());
900   registry()->Disable();
901   AssertWillHandle(mailto, false, interceptor.get());
902 }
903
904 TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) {
905   ProtocolHandler ph1 =
906       CreateProtocolHandler("mailto", GURL("http://test.com/%s"));
907   ProtocolHandler ph2 =
908       CreateProtocolHandler("mailto", GURL("http://test.com/updated-url/%s"));
909   registry()->OnAcceptRegisterProtocolHandler(ph1);
910   ASSERT_TRUE(registry()->AttemptReplace(ph2));
911   const ProtocolHandler& handler(registry()->GetHandlerFor("mailto"));
912   ASSERT_EQ(handler.url(), ph2.url());
913 }
914
915 TEST_F(ProtocolHandlerRegistryTest, TestReplaceNonDefaultHandler) {
916   ProtocolHandler ph1 =
917       CreateProtocolHandler("mailto", GURL("http://test.com/%s"));
918   ProtocolHandler ph2 =
919       CreateProtocolHandler("mailto", GURL("http://test.com/updated-url/%s"));
920   ProtocolHandler ph3 =
921       CreateProtocolHandler("mailto", GURL("http://else.com/%s"));
922   registry()->OnAcceptRegisterProtocolHandler(ph1);
923   registry()->OnAcceptRegisterProtocolHandler(ph3);
924   ASSERT_TRUE(registry()->AttemptReplace(ph2));
925   const ProtocolHandler& handler(registry()->GetHandlerFor("mailto"));
926   ASSERT_EQ(handler.url(), ph3.url());
927 }
928
929 TEST_F(ProtocolHandlerRegistryTest, TestReplaceRemovesStaleHandlers) {
930   ProtocolHandler ph1 =
931       CreateProtocolHandler("mailto", GURL("http://test.com/%s"));
932   ProtocolHandler ph2 =
933       CreateProtocolHandler("mailto", GURL("http://test.com/updated-url/%s"));
934   ProtocolHandler ph3 =
935       CreateProtocolHandler("mailto", GURL("http://test.com/third/%s"));
936   registry()->OnAcceptRegisterProtocolHandler(ph1);
937   registry()->OnAcceptRegisterProtocolHandler(ph2);
938
939   // This should replace the previous two handlers.
940   ASSERT_TRUE(registry()->AttemptReplace(ph3));
941   const ProtocolHandler& handler(registry()->GetHandlerFor("mailto"));
942   ASSERT_EQ(handler.url(), ph3.url());
943   registry()->RemoveHandler(ph3);
944   ASSERT_TRUE(registry()->GetHandlerFor("mailto").IsEmpty());
945 }
946
947 TEST_F(ProtocolHandlerRegistryTest, TestIsSameOrigin) {
948   ProtocolHandler ph1 =
949       CreateProtocolHandler("mailto", GURL("http://test.com/%s"));
950   ProtocolHandler ph2 =
951       CreateProtocolHandler("mailto", GURL("http://test.com/updated-url/%s"));
952   ProtocolHandler ph3 =
953       CreateProtocolHandler("mailto", GURL("http://other.com/%s"));
954   ASSERT_EQ(ph1.url().GetOrigin() == ph2.url().GetOrigin(),
955       ph1.IsSameOrigin(ph2));
956   ASSERT_EQ(ph1.url().GetOrigin() == ph2.url().GetOrigin(),
957       ph2.IsSameOrigin(ph1));
958   ASSERT_EQ(ph2.url().GetOrigin() == ph3.url().GetOrigin(),
959       ph2.IsSameOrigin(ph3));
960   ASSERT_EQ(ph3.url().GetOrigin() == ph2.url().GetOrigin(),
961       ph3.IsSameOrigin(ph2));
962 }
963
964 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) {
965   RecreateRegistry(false);
966   registry()->AddPredefinedHandler(
967       CreateProtocolHandler("test", GURL("http://test.com/%s")));
968   registry()->InitProtocolSettings();
969   std::vector<std::string> protocols;
970   registry()->GetRegisteredProtocols(&protocols);
971   ASSERT_EQ(static_cast<size_t>(1), protocols.size());
972 }
973
974 #define URL_p1u1 "http://p1u1.com/%s"
975 #define URL_p1u2 "http://p1u2.com/%s"
976 #define URL_p1u3 "http://p1u3.com/%s"
977 #define URL_p2u1 "http://p2u1.com/%s"
978 #define URL_p2u2 "http://p2u2.com/%s"
979 #define URL_p3u1 "http://p3u1.com/%s"
980
981 TEST_F(ProtocolHandlerRegistryTest, TestPrefPolicyOverlapRegister) {
982   base::ListValue handlers_registered_by_pref;
983   base::ListValue handlers_registered_by_policy;
984
985   handlers_registered_by_pref.Append(
986       GetProtocolHandlerValueWithDefault("p1", URL_p1u2, true));
987   handlers_registered_by_pref.Append(
988       GetProtocolHandlerValueWithDefault("p1", URL_p1u1, true));
989   handlers_registered_by_pref.Append(
990       GetProtocolHandlerValueWithDefault("p1", URL_p1u2, false));
991
992   handlers_registered_by_policy.Append(
993       GetProtocolHandlerValueWithDefault("p1", URL_p1u1, false));
994   handlers_registered_by_policy.Append(
995       GetProtocolHandlerValueWithDefault("p3", URL_p3u1, true));
996
997   profile()->GetPrefs()->Set(prefs::kRegisteredProtocolHandlers,
998                              handlers_registered_by_pref);
999   profile()->GetPrefs()->Set(prefs::kPolicyRegisteredProtocolHandlers,
1000                              handlers_registered_by_policy);
1001   registry()->InitProtocolSettings();
1002
1003   // Duplicate p1u2 eliminated in memory but not yet saved in pref
1004   ProtocolHandler p1u1 = CreateProtocolHandler("p1", GURL(URL_p1u1));
1005   ProtocolHandler p1u2 = CreateProtocolHandler("p1", GURL(URL_p1u2));
1006   ASSERT_EQ(InPrefHandlerCount(), 3);
1007   ASSERT_EQ(InMemoryHandlerCount(), 3);
1008   ASSERT_TRUE(registry()->IsDefault(p1u1));
1009   ASSERT_FALSE(registry()->IsDefault(p1u2));
1010
1011   ProtocolHandler p2u1 = CreateProtocolHandler("p2", GURL(URL_p2u1));
1012   registry()->OnDenyRegisterProtocolHandler(p2u1);
1013
1014   // Duplicate p1u2 saved in pref and a new handler added to pref and memory
1015   ASSERT_EQ(InPrefHandlerCount(), 3);
1016   ASSERT_EQ(InMemoryHandlerCount(), 4);
1017   ASSERT_FALSE(registry()->IsDefault(p2u1));
1018
1019   registry()->RemoveHandler(p1u1);
1020
1021   // p1u1 removed from user pref but not from memory due to policy.
1022   ASSERT_EQ(InPrefHandlerCount(), 2);
1023   ASSERT_EQ(InMemoryHandlerCount(), 4);
1024   ASSERT_TRUE(registry()->IsDefault(p1u1));
1025
1026   ProtocolHandler p3u1 = CreateProtocolHandler("p3", GURL(URL_p3u1));
1027   registry()->RemoveHandler(p3u1);
1028
1029   // p3u1 not removed from memory due to policy and it was never in pref.
1030   ASSERT_EQ(InPrefHandlerCount(), 2);
1031   ASSERT_EQ(InMemoryHandlerCount(), 4);
1032   ASSERT_TRUE(registry()->IsDefault(p3u1));
1033
1034   registry()->RemoveHandler(p1u2);
1035
1036   // p1u2 removed from user pref and memory.
1037   ASSERT_EQ(InPrefHandlerCount(), 1);
1038   ASSERT_EQ(InMemoryHandlerCount(), 3);
1039   ASSERT_TRUE(registry()->IsDefault(p1u1));
1040
1041   ProtocolHandler p1u3 = CreateProtocolHandler("p1", GURL(URL_p1u3));
1042   registry()->OnAcceptRegisterProtocolHandler(p1u3);
1043
1044   // p1u3 added to pref and memory.
1045   ASSERT_EQ(InPrefHandlerCount(), 2);
1046   ASSERT_EQ(InMemoryHandlerCount(), 4);
1047   ASSERT_FALSE(registry()->IsDefault(p1u1));
1048   ASSERT_TRUE(registry()->IsDefault(p1u3));
1049
1050   registry()->RemoveHandler(p1u3);
1051
1052   // p1u3 the default handler for p1 removed from user pref and memory.
1053   ASSERT_EQ(InPrefHandlerCount(), 1);
1054   ASSERT_EQ(InMemoryHandlerCount(), 3);
1055   ASSERT_FALSE(registry()->IsDefault(p1u3));
1056   ASSERT_TRUE(registry()->IsDefault(p1u1));
1057   ASSERT_TRUE(registry()->IsDefault(p3u1));
1058   ASSERT_FALSE(registry()->IsDefault(p2u1));
1059 }
1060
1061 TEST_F(ProtocolHandlerRegistryTest, TestPrefPolicyOverlapIgnore) {
1062   base::ListValue handlers_ignored_by_pref;
1063   base::ListValue handlers_ignored_by_policy;
1064
1065   handlers_ignored_by_pref.Append(GetProtocolHandlerValue("p1", URL_p1u1));
1066   handlers_ignored_by_pref.Append(GetProtocolHandlerValue("p1", URL_p1u2));
1067   handlers_ignored_by_pref.Append(GetProtocolHandlerValue("p1", URL_p1u2));
1068   handlers_ignored_by_pref.Append(GetProtocolHandlerValue("p3", URL_p3u1));
1069
1070   handlers_ignored_by_policy.Append(GetProtocolHandlerValue("p1", URL_p1u2));
1071   handlers_ignored_by_policy.Append(GetProtocolHandlerValue("p1", URL_p1u3));
1072   handlers_ignored_by_policy.Append(GetProtocolHandlerValue("p2", URL_p2u1));
1073
1074   profile()->GetPrefs()->Set(prefs::kIgnoredProtocolHandlers,
1075                              handlers_ignored_by_pref);
1076   profile()->GetPrefs()->Set(prefs::kPolicyIgnoredProtocolHandlers,
1077                              handlers_ignored_by_policy);
1078   registry()->InitProtocolSettings();
1079
1080   // Duplicate p1u2 eliminated in memory but not yet saved in pref
1081   ASSERT_EQ(InPrefIgnoredHandlerCount(), 4);
1082   ASSERT_EQ(InMemoryIgnoredHandlerCount(), 5);
1083
1084   ProtocolHandler p2u2 = CreateProtocolHandler("p2", GURL(URL_p2u2));
1085   registry()->OnIgnoreRegisterProtocolHandler(p2u2);
1086
1087   // Duplicate p1u2 eliminated in pref, p2u2 added to pref and memory.
1088   ASSERT_EQ(InPrefIgnoredHandlerCount(), 4);
1089   ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6);
1090
1091   ProtocolHandler p2u1 = CreateProtocolHandler("p2", GURL(URL_p2u1));
1092   registry()->RemoveIgnoredHandler(p2u1);
1093
1094   // p2u1 installed by policy so cant be removed.
1095   ASSERT_EQ(InPrefIgnoredHandlerCount(), 4);
1096   ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6);
1097
1098   ProtocolHandler p1u2 = CreateProtocolHandler("p1", GURL(URL_p1u2));
1099   registry()->RemoveIgnoredHandler(p1u2);
1100
1101   // p1u2 installed by policy and pref so it is removed from pref and not from
1102   // memory.
1103   ASSERT_EQ(InPrefIgnoredHandlerCount(), 3);
1104   ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6);
1105
1106   ProtocolHandler p1u1 = CreateProtocolHandler("p1", GURL(URL_p1u1));
1107   registry()->RemoveIgnoredHandler(p1u1);
1108
1109   // p1u1 installed by pref so it is removed from pref and memory.
1110   ASSERT_EQ(InPrefIgnoredHandlerCount(), 2);
1111   ASSERT_EQ(InMemoryIgnoredHandlerCount(), 5);
1112
1113   registry()->RemoveIgnoredHandler(p2u2);
1114
1115   // p2u2 installed by user so it is removed from pref and memory.
1116   ASSERT_EQ(InPrefIgnoredHandlerCount(), 1);
1117   ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4);
1118
1119   registry()->OnIgnoreRegisterProtocolHandler(p2u1);
1120
1121   // p2u1 installed by user but it is already installed by policy, so it is
1122   // added to pref.
1123   ASSERT_EQ(InPrefIgnoredHandlerCount(), 2);
1124   ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4);
1125
1126   registry()->RemoveIgnoredHandler(p2u1);
1127
1128   // p2u1 installed by user and policy, so it is removed from pref alone.
1129   ASSERT_EQ(InPrefIgnoredHandlerCount(), 1);
1130   ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4);
1131 }