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

Side by Side Diff: chrome/browser/custom_handlers/protocol_handler_registry_unittest.cc

Issue 10546083: Convert ProtocolHandlerRegistry to be a ProfileKeyedService. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Merge with upstream. Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" 5 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/synchronization/waitable_event.h"
11 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
12 #include "chrome/common/chrome_notification_types.h" 13 #include "chrome/common/chrome_notification_types.h"
13 #include "chrome/common/custom_handlers/protocol_handler.h" 14 #include "chrome/common/custom_handlers/protocol_handler.h"
14 #include "chrome/test/base/testing_browser_process.h" 15 #include "chrome/test/base/testing_browser_process.h"
15 #include "chrome/test/base/testing_pref_service.h" 16 #include "chrome/test/base/testing_pref_service.h"
16 #include "chrome/test/base/testing_profile.h" 17 #include "chrome/test/base/testing_profile.h"
17 #include "content/public/browser/notification_observer.h" 18 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h" 19 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/notification_source.h" 20 #include "content/public/browser/notification_source.h"
20 #include "content/public/test/test_browser_thread.h" 21 #include "content/public/test/test_browser_thread.h"
21 #include "content/public/test/test_renderer_host.h" 22 #include "content/public/test/test_renderer_host.h"
22 #include "net/url_request/url_request.h" 23 #include "net/url_request/url_request.h"
23 #include "net/url_request/url_request_context.h" 24 #include "net/url_request/url_request_context.h"
25 #include "testing/gtest/include/gtest/gtest.h"
24 26
25 using content::BrowserThread; 27 using content::BrowserThread;
26 28
27 namespace { 29 namespace {
28 30
31 void AssertInterceptedIO(
32 const GURL& url,
33 net::URLRequestJobFactory::Interceptor* interceptor) {
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
35 net::URLRequestContext context;
36 net::URLRequest request(url, NULL, &context);
37 scoped_refptr<net::URLRequestJob> job = interceptor->MaybeIntercept(&request);
38 ASSERT_TRUE(job.get() != NULL);
39 }
40
41 void AssertIntercepted(
42 const GURL& url,
43 net::URLRequestJobFactory::Interceptor* interceptor) {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
45 BrowserThread::PostTask(BrowserThread::IO,
46 FROM_HERE,
47 base::Bind(AssertInterceptedIO,
48 url,
49 base::Unretained(interceptor)));
50 MessageLoop::current()->RunAllPending();
51 }
52
53 void AssertWillHandleIO(
54 const std::string& scheme,
55 bool expected,
56 net::URLRequestJobFactory::Interceptor* interceptor) {
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
58 ASSERT_EQ(expected, interceptor->WillHandleProtocol(scheme));
59 }
60
61 void AssertWillHandle(
62 const std::string& scheme,
63 bool expected,
64 net::URLRequestJobFactory::Interceptor* interceptor) {
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
66 BrowserThread::PostTask(BrowserThread::IO,
67 FROM_HERE,
68 base::Bind(AssertWillHandleIO,
69 scheme,
70 expected,
71 base::Unretained(interceptor)));
72 MessageLoop::current()->RunAllPending();
73 }
74
29 class FakeDelegate : public ProtocolHandlerRegistry::Delegate { 75 class FakeDelegate : public ProtocolHandlerRegistry::Delegate {
30 public: 76 public:
31 FakeDelegate() : force_os_failure_(false) {} 77 FakeDelegate() : force_os_failure_(false) {}
32 virtual ~FakeDelegate() { } 78 virtual ~FakeDelegate() { }
33 virtual void RegisterExternalHandler(const std::string& protocol) { 79 virtual void RegisterExternalHandler(const std::string& protocol) {
34 ASSERT_TRUE( 80 ASSERT_TRUE(
35 registered_protocols_.find(protocol) == registered_protocols_.end()); 81 registered_protocols_.find(protocol) == registered_protocols_.end());
36 registered_protocols_.insert(protocol); 82 registered_protocols_.insert(protocol);
37 } 83 }
38 84
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 213
168 int events_; 214 int events_;
169 content::NotificationRegistrar notification_registrar_; 215 content::NotificationRegistrar notification_registrar_;
170 }; 216 };
171 217
172 class QueryProtocolHandlerOnChange 218 class QueryProtocolHandlerOnChange
173 : public content::NotificationObserver { 219 : public content::NotificationObserver {
174 public: 220 public:
175 QueryProtocolHandlerOnChange(Profile* profile, 221 QueryProtocolHandlerOnChange(Profile* profile,
176 ProtocolHandlerRegistry* registry) 222 ProtocolHandlerRegistry* registry)
177 : registry_(registry), 223 : local_registry_(registry),
178 called_(false), 224 called_(false),
179 notification_registrar_() { 225 notification_registrar_() {
180 notification_registrar_.Add(this, 226 notification_registrar_.Add(this,
181 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, 227 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED,
182 content::Source<Profile>(profile)); 228 content::Source<Profile>(profile));
183 } 229 }
184 230
185 virtual void Observe(int type, 231 virtual void Observe(int type,
186 const content::NotificationSource& source, 232 const content::NotificationSource& source,
187 const content::NotificationDetails& details) { 233 const content::NotificationDetails& details) {
188 std::vector<std::string> output; 234 std::vector<std::string> output;
189 registry_->GetRegisteredProtocols(&output); 235 local_registry_->GetRegisteredProtocols(&output);
190 called_ = true; 236 called_ = true;
191 } 237 }
192 238
193 ProtocolHandlerRegistry* registry_; 239 ProtocolHandlerRegistry* local_registry_;
194 bool called_; 240 bool called_;
195 content::NotificationRegistrar notification_registrar_; 241 content::NotificationRegistrar notification_registrar_;
196 }; 242 };
197 243
244 // URLRequest DCHECKS that the current MessageLoop is IO. It does this because
245 // it can't check the thread id (since net can't depend on content.) We want
246 // to harness our tests so all threads use the same loop allowing us to
247 // guarantee all messages are processed.) By overriding the IsType method
248 // we basically ignore the supplied message loop type, and instead infer
249 // our type based on the current thread. GO DEPENDENCY INJECTION!
250 class TestMessageLoop : public MessageLoop {
251 public:
252 TestMessageLoop() : MessageLoop(MessageLoop::TYPE_DEFAULT) {}
253 ~TestMessageLoop() {}
254 virtual bool IsType(MessageLoop::Type type) const OVERRIDE {
255 switch (type) {
256 case MessageLoop::TYPE_UI:
257 return BrowserThread::CurrentlyOn(BrowserThread::UI);
258 case MessageLoop::TYPE_IO:
259 return BrowserThread::CurrentlyOn(BrowserThread::IO);
260 case MessageLoop::TYPE_DEFAULT:
261 return !BrowserThread::CurrentlyOn(BrowserThread::UI) &&
262 !BrowserThread::CurrentlyOn(BrowserThread::IO);
263 }
264 return false;
265 }
266 };
267
198 } // namespace 268 } // namespace
199 269
200 class ProtocolHandlerRegistryTest : public testing::Test { 270 class ProtocolHandlerRegistryTest : public testing::Test {
201 protected: 271 protected:
202 ProtocolHandlerRegistryTest() 272 ProtocolHandlerRegistryTest()
203 : test_protocol_handler_(CreateProtocolHandler("test", "test")) {} 273 : ui_thread_(BrowserThread::UI, &loop_),
274 file_thread_(BrowserThread::FILE, &loop_),
275 io_thread_(BrowserThread::IO, &loop_),
276 test_protocol_handler_(CreateProtocolHandler("test", "test")) {}
204 277
205 FakeDelegate* delegate() const { return delegate_; } 278 FakeDelegate* delegate() const { return delegate_; }
279 ProtocolHandlerRegistry* registry() { return registry_.get(); }
206 TestingProfile* profile() const { return profile_.get(); } 280 TestingProfile* profile() const { return profile_.get(); }
207 PrefService* pref_service() const { return profile_->GetPrefs(); } 281 PrefService* pref_service() const { return profile_->GetPrefs(); }
208 ProtocolHandlerRegistry* registry() const { return registry_.get(); }
209 const ProtocolHandler& test_protocol_handler() const { 282 const ProtocolHandler& test_protocol_handler() const {
210 return test_protocol_handler_; 283 return test_protocol_handler_;
211 } 284 }
212 285
213 ProtocolHandler CreateProtocolHandler(const std::string& protocol, 286 ProtocolHandler CreateProtocolHandler(const std::string& protocol,
214 const GURL& url, 287 const GURL& url,
215 const std::string& title) { 288 const std::string& title) {
216 return ProtocolHandler::CreateProtocolHandler(protocol, url, 289 return ProtocolHandler::CreateProtocolHandler(protocol, url,
217 UTF8ToUTF16(title)); 290 UTF8ToUTF16(title));
218 } 291 }
219 292
220 ProtocolHandler CreateProtocolHandler(const std::string& protocol, 293 ProtocolHandler CreateProtocolHandler(const std::string& protocol,
221 const std::string& name) { 294 const std::string& name) {
222 return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s"), 295 return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s"),
223 name); 296 name);
224 } 297 }
225 298
226 void ReloadProtocolHandlerRegistry() { 299 void RecreateRegistry(bool initilized) {
227 delegate_ = new FakeDelegate(); 300 TeadDownRegistry();
228 registry_->Finalize(); 301 SetUpRegistry(initilized);
229 registry_ = NULL;
230 registry_ = new ProtocolHandlerRegistry(profile(), delegate());
231 registry_->Load();
232 } 302 }
233 303
234 void ReloadProtocolHandlerRegistryAndInstallDefaultHandler() { 304 // Returns a new registry. Caller assumes ownership for the object
305 void SetUpRegistry(bool initilized) {
235 delegate_ = new FakeDelegate(); 306 delegate_ = new FakeDelegate();
236 registry_->Finalize(); 307 registry_.reset(new ProtocolHandlerRegistry(profile(), delegate()));
237 registry_ = NULL; 308 if (initilized) registry_->InitProtocolSettings();
238 registry_ = new ProtocolHandlerRegistry(profile(), delegate()); 309 }
239 registry_->AddPredefinedHandler(CreateProtocolHandler( 310
240 "test", GURL("http://test.com/%s"), "Test")); 311 void TeadDownRegistry() {
241 registry_->Load(); 312 registry_->Shutdown();
313 delete registry_.release();
314 // Registry owns the delegate_ it handles deletion of that object.
242 } 315 }
243 316
244 virtual void SetUp() { 317 virtual void SetUp() {
245 ui_message_loop_.reset(new MessageLoopForUI());
246 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI,
247 MessageLoop::current()));
248 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO));
249 io_thread_->StartIOThread();
250
251 file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE));
252 file_thread_->Start();
253
254 profile_.reset(new TestingProfile()); 318 profile_.reset(new TestingProfile());
255 profile_->SetPrefService(new TestingPrefService()); 319 profile_->SetPrefService(new TestingPrefService());
256 delegate_ = new FakeDelegate(); 320 SetUpRegistry(true);
257 registry_ = new ProtocolHandlerRegistry(profile(), delegate());
258 registry_->Load();
259 test_protocol_handler_ = 321 test_protocol_handler_ =
260 CreateProtocolHandler("test", GURL("http://test.com/%s"), "Test"); 322 CreateProtocolHandler("test", GURL("http://test.com/%s"), "Test");
261
262 ProtocolHandlerRegistry::RegisterPrefs(pref_service()); 323 ProtocolHandlerRegistry::RegisterPrefs(pref_service());
263 } 324 }
264 325
265 virtual void TearDown() { 326 virtual void TearDown() {
266 registry_->Finalize(); 327 TeadDownRegistry();
267 registry_ = NULL;
268 io_thread_->Stop();
269 io_thread_.reset(NULL);
270 file_thread_->Stop();
271 file_thread_.reset(NULL);
272 ui_thread_.reset(NULL);
273 ui_message_loop_.reset(NULL);
274 } 328 }
275 329
276 bool enabled_io() { 330 TestMessageLoop loop_;
277 return registry()->enabled_io_;
278 }
279 331
280 scoped_ptr<MessageLoopForUI> ui_message_loop_; 332 private:
281 scoped_ptr<content::TestBrowserThread> ui_thread_; 333 content::TestBrowserThread ui_thread_;
282 scoped_ptr<content::TestBrowserThread> io_thread_; 334 content::TestBrowserThread file_thread_;
283 scoped_ptr<content::TestBrowserThread> file_thread_; 335 content::TestBrowserThread io_thread_;
284 336
285 FakeDelegate* delegate_;
286 scoped_ptr<TestingProfile> profile_; 337 scoped_ptr<TestingProfile> profile_;
287 scoped_refptr<ProtocolHandlerRegistry> registry_; 338 FakeDelegate* delegate_; // Registry assumes ownership of delegate_.
339 scoped_ptr<ProtocolHandlerRegistry> registry_;
288 ProtocolHandler test_protocol_handler_; 340 ProtocolHandler test_protocol_handler_;
289 }; 341 };
290 342
291 // ProtocolHandlerRegistryTest tests are flaky on Linux & ChromeOS. 343 // ProtocolHandlerRegistryTest tests are flaky on Linux & ChromeOS.
292 // http://crbug.com/133023 344 // http://crbug.com/133023
293 #if defined(OS_LINUX) || defined(OS_CHROMEOS) 345 #if defined(OS_LINUX) || defined(OS_CHROMEOS)
294 #define MAYBE_AcceptProtocolHandlerHandlesProtocol \ 346 #define MAYBE_AcceptProtocolHandlerHandlesProtocol \
295 DISABLED_AcceptProtocolHandlerHandlesProtocol 347 DISABLED_AcceptProtocolHandlerHandlesProtocol
296 #define MAYBE_DeniedProtocolIsntHandledUntilAccepted \ 348 #define MAYBE_DeniedProtocolIsntHandledUntilAccepted \
297 DISABLED_DeniedProtocolIsntHandledUntilAccepted 349 DISABLED_DeniedProtocolIsntHandledUntilAccepted
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 428
377 TEST_F(ProtocolHandlerRegistryTest, SaveAndLoad) { 429 TEST_F(ProtocolHandlerRegistryTest, SaveAndLoad) {
378 ProtocolHandler stuff_protocol_handler( 430 ProtocolHandler stuff_protocol_handler(
379 CreateProtocolHandler("stuff", "stuff")); 431 CreateProtocolHandler("stuff", "stuff"));
380 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); 432 registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler());
381 registry()->OnIgnoreRegisterProtocolHandler(stuff_protocol_handler); 433 registry()->OnIgnoreRegisterProtocolHandler(stuff_protocol_handler);
382 434
383 ASSERT_TRUE(registry()->IsHandledProtocol("test")); 435 ASSERT_TRUE(registry()->IsHandledProtocol("test"));
384 ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler)); 436 ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler));
385 delegate()->Reset(); 437 delegate()->Reset();
386 ReloadProtocolHandlerRegistry(); 438 RecreateRegistry(true);
387 ASSERT_TRUE(registry()->IsHandledProtocol("test")); 439 ASSERT_TRUE(registry()->IsHandledProtocol("test"));
388 ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler)); 440 ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler));
389 } 441 }
390 442
391 TEST_F(ProtocolHandlerRegistryTest, TestEnabledDisabled) { 443 TEST_F(ProtocolHandlerRegistryTest, TestEnabledDisabled) {
392 registry()->Disable(); 444 registry()->Disable();
393 ASSERT_FALSE(registry()->enabled()); 445 ASSERT_FALSE(registry()->enabled());
394 registry()->Enable(); 446 registry()->Enable();
395 ASSERT_TRUE(registry()->enabled()); 447 ASSERT_TRUE(registry()->enabled());
396 } 448 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 514
463 TEST_F(ProtocolHandlerRegistryTest, TestDefaultSaveLoad) { 515 TEST_F(ProtocolHandlerRegistryTest, TestDefaultSaveLoad) {
464 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); 516 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
465 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); 517 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2");
466 registry()->OnDenyRegisterProtocolHandler(ph1); 518 registry()->OnDenyRegisterProtocolHandler(ph1);
467 registry()->OnDenyRegisterProtocolHandler(ph2); 519 registry()->OnDenyRegisterProtocolHandler(ph2);
468 520
469 registry()->OnAcceptRegisterProtocolHandler(ph2); 521 registry()->OnAcceptRegisterProtocolHandler(ph2);
470 registry()->Disable(); 522 registry()->Disable();
471 523
472 ReloadProtocolHandlerRegistry(); 524 RecreateRegistry(true);
473 525
474 ASSERT_FALSE(registry()->enabled()); 526 ASSERT_FALSE(registry()->enabled());
475 registry()->Enable(); 527 registry()->Enable();
476 ASSERT_FALSE(registry()->IsDefault(ph1)); 528 ASSERT_FALSE(registry()->IsDefault(ph1));
477 ASSERT_TRUE(registry()->IsDefault(ph2)); 529 ASSERT_TRUE(registry()->IsDefault(ph2));
478 530
479 ReloadProtocolHandlerRegistry(); 531 RecreateRegistry(true);
480 ASSERT_TRUE(registry()->enabled()); 532 ASSERT_TRUE(registry()->enabled());
481 } 533 }
482 534
483 TEST_F(ProtocolHandlerRegistryTest, TestRemoveHandler) { 535 TEST_F(ProtocolHandlerRegistryTest, TestRemoveHandler) {
484 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); 536 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
485 registry()->OnAcceptRegisterProtocolHandler(ph1); 537 registry()->OnAcceptRegisterProtocolHandler(ph1);
486 registry()->OnAcceptRegisterProtocolHandler(ph1); 538 registry()->OnAcceptRegisterProtocolHandler(ph1);
487 539
488 registry()->RemoveHandler(ph1); 540 registry()->RemoveHandler(ph1);
489 ASSERT_FALSE(registry()->IsRegistered(ph1)); 541 ASSERT_FALSE(registry()->IsRegistered(ph1));
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 } 681 }
630 682
631 TEST_F(ProtocolHandlerRegistryTest, TestDisablePreventsHandling) { 683 TEST_F(ProtocolHandlerRegistryTest, TestDisablePreventsHandling) {
632 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); 684 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1");
633 registry()->OnAcceptRegisterProtocolHandler(ph1); 685 registry()->OnAcceptRegisterProtocolHandler(ph1);
634 ASSERT_TRUE(registry()->IsHandledProtocol("test")); 686 ASSERT_TRUE(registry()->IsHandledProtocol("test"));
635 registry()->Disable(); 687 registry()->Disable();
636 ASSERT_FALSE(registry()->IsHandledProtocol("test")); 688 ASSERT_FALSE(registry()->IsHandledProtocol("test"));
637 } 689 }
638 690
691 // TODO(smckay): This is much more appropriately an integration
692 // test. Make that so, then update the
693 // ShellIntegretion{Delegate,Observer,Worker} test classes we use to fully
694 // isolate this test from the FILE thread.
639 TEST_F(ProtocolHandlerRegistryTest, TestOSRegistration) { 695 TEST_F(ProtocolHandlerRegistryTest, TestOSRegistration) {
640 ProtocolHandler ph_do1 = CreateProtocolHandler("do", "test1"); 696 ProtocolHandler ph_do1 = CreateProtocolHandler("do", "test1");
641 ProtocolHandler ph_do2 = CreateProtocolHandler("do", "test2"); 697 ProtocolHandler ph_do2 = CreateProtocolHandler("do", "test2");
642 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test"); 698 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test");
643 699
644 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("do")); 700 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("do"));
645 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont")); 701 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont"));
646 702
647 registry()->OnAcceptRegisterProtocolHandler(ph_do1); 703 registry()->OnAcceptRegisterProtocolHandler(ph_do1);
648 registry()->OnDenyRegisterProtocolHandler(ph_dont); 704 registry()->OnDenyRegisterProtocolHandler(ph_dont);
649 MessageLoop::current()->Run(); 705 MessageLoop::current()->Run(); // FILE thread needs to run.
650 ASSERT_TRUE(delegate()->IsFakeRegisteredWithOS("do")); 706 ASSERT_TRUE(delegate()->IsFakeRegisteredWithOS("do"));
651 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont")); 707 ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont"));
652 708
653 // This should not register with the OS, if it does the delegate 709 // This should not register with the OS, if it does the delegate
654 // will assert for us. We don't need to wait for the message loop 710 // will assert for us. We don't need to wait for the message loop
655 // as it should not go through to the shell worker. 711 // as it should not go through to the shell worker.
656 registry()->OnAcceptRegisterProtocolHandler(ph_do2); 712 registry()->OnAcceptRegisterProtocolHandler(ph_do2);
657 } 713 }
658 714
659 #if defined(OS_LINUX) 715 #if defined(OS_LINUX)
660 // TODO(benwells): When Linux support is more reliable and 716 // TODO(benwells): When Linux support is more reliable and
661 // http://crbut.com/88255 is fixed this test will pass. 717 // http://crbut.com/88255 is fixed this test will pass.
662 #define MAYBE_TestOSRegistrationFailure FAILS_TestOSRegistrationFailure 718 #define MAYBE_TestOSRegistrationFailure FAILS_TestOSRegistrationFailure
663 #else 719 #else
664 #define MAYBE_TestOSRegistrationFailure TestOSRegistrationFailure 720 #define MAYBE_TestOSRegistrationFailure TestOSRegistrationFailure
665 #endif 721 #endif
666 722
723 // TODO(smckay): This is much more appropriately an integration
724 // test. Make that so, then update the
725 // ShellIntegretion{Delegate,Observer,Worker} test classes we use to fully
726 // isolate this test from the FILE thread.
667 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestOSRegistrationFailure) { 727 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestOSRegistrationFailure) {
668 ProtocolHandler ph_do = CreateProtocolHandler("do", "test1"); 728 ProtocolHandler ph_do = CreateProtocolHandler("do", "test1");
669 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test"); 729 ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test");
670 730
671 ASSERT_FALSE(registry()->IsHandledProtocol("do")); 731 ASSERT_FALSE(registry()->IsHandledProtocol("do"));
672 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); 732 ASSERT_FALSE(registry()->IsHandledProtocol("dont"));
673 733
674 registry()->OnAcceptRegisterProtocolHandler(ph_do); 734 registry()->OnAcceptRegisterProtocolHandler(ph_do);
675 MessageLoop::current()->Run(); 735 MessageLoop::current()->Run(); // FILE thread needs to run.
676 delegate()->set_force_os_failure(true); 736 delegate()->set_force_os_failure(true);
677 registry()->OnAcceptRegisterProtocolHandler(ph_dont); 737 registry()->OnAcceptRegisterProtocolHandler(ph_dont);
678 MessageLoop::current()->Run(); 738 MessageLoop::current()->Run(); // FILE thread needs to run.
679 ASSERT_TRUE(registry()->IsHandledProtocol("do")); 739 ASSERT_TRUE(registry()->IsHandledProtocol("do"));
680 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size()); 740 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size());
681 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); 741 ASSERT_FALSE(registry()->IsHandledProtocol("dont"));
682 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size()); 742 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size());
683 } 743 }
684 744
685 static void MakeRequest(const GURL& url, ProtocolHandlerRegistry* registry) {
686 net::URLRequestContext context;
687 net::URLRequest request(url, NULL, &context);
688 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
689 MessageLoop::QuitClosure());
690 scoped_refptr<net::URLRequestJob> job(registry->MaybeCreateJob(&request));
691 ASSERT_TRUE(job.get() != NULL);
692 }
693
694 TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) { 745 TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) {
695 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); 746 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1");
696 registry()->OnAcceptRegisterProtocolHandler(ph1); 747 registry()->OnAcceptRegisterProtocolHandler(ph1);
697 GURL url("mailto:someone@something.com"); 748 GURL url("mailto:someone@something.com");
698 scoped_refptr<ProtocolHandlerRegistry> r(registry()); 749 net::URLRequestJobFactory::Interceptor* interceptor =
699 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 750 registry()->CreateURLInterceptor();
700 base::Bind(MakeRequest, url, r));
701 MessageLoop::current()->Run();
702 }
703 751
704 static void CheckIsHandled(const std::string& scheme, bool expected, 752 AssertIntercepted(url, interceptor);
705 ProtocolHandlerRegistry* registry) {
706 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
707 MessageLoop::QuitClosure());
708 ASSERT_EQ(expected, registry->IsHandledProtocolIO(scheme));
709 } 753 }
710 754
711 TEST_F(ProtocolHandlerRegistryTest, 755 TEST_F(ProtocolHandlerRegistryTest,
712 MAYBE_TestIsHandledProtocolWorksOnIOThread) { 756 MAYBE_TestIsHandledProtocolWorksOnIOThread) {
713 std::string scheme("mailto"); 757 std::string scheme("mailto");
714 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); 758 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1");
715 registry()->OnAcceptRegisterProtocolHandler(ph1); 759 registry()->OnAcceptRegisterProtocolHandler(ph1);
716 scoped_refptr<ProtocolHandlerRegistry> r(registry()); 760 net::URLRequestJobFactory::Interceptor* interceptor =
717 BrowserThread::PostTask( 761 registry()->CreateURLInterceptor();
718 BrowserThread::IO, 762
719 FROM_HERE, 763 AssertWillHandle(scheme, true, interceptor);
720 base::Bind(CheckIsHandled, scheme, true, r));
721 } 764 }
722 765
723 TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultFallsBackToOldDefault) { 766 TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultFallsBackToOldDefault) {
724 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); 767 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1");
725 ProtocolHandler ph2 = CreateProtocolHandler("mailto", "test2"); 768 ProtocolHandler ph2 = CreateProtocolHandler("mailto", "test2");
726 ProtocolHandler ph3 = CreateProtocolHandler("mailto", "test3"); 769 ProtocolHandler ph3 = CreateProtocolHandler("mailto", "test3");
727 registry()->OnAcceptRegisterProtocolHandler(ph1); 770 registry()->OnAcceptRegisterProtocolHandler(ph1);
728 registry()->OnAcceptRegisterProtocolHandler(ph2); 771 registry()->OnAcceptRegisterProtocolHandler(ph2);
729 registry()->OnAcceptRegisterProtocolHandler(ph3); 772 registry()->OnAcceptRegisterProtocolHandler(ph3);
730 773
(...skipping 23 matching lines...) Expand all
754 797
755 ASSERT_EQ(ph2, handlers[0]); 798 ASSERT_EQ(ph2, handlers[0]);
756 ASSERT_EQ(ph1, handlers[1]); 799 ASSERT_EQ(ph1, handlers[1]);
757 } 800 }
758 801
759 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestClearDefaultGetsPropagatedToIO) { 802 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestClearDefaultGetsPropagatedToIO) {
760 std::string scheme("mailto"); 803 std::string scheme("mailto");
761 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); 804 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1");
762 registry()->OnAcceptRegisterProtocolHandler(ph1); 805 registry()->OnAcceptRegisterProtocolHandler(ph1);
763 registry()->ClearDefault(scheme); 806 registry()->ClearDefault(scheme);
764 scoped_refptr<ProtocolHandlerRegistry> r(registry()); 807 net::URLRequestJobFactory::Interceptor* interceptor =
808 registry()->CreateURLInterceptor();
765 809
766 BrowserThread::PostTask( 810 AssertWillHandle(scheme, false, interceptor);
767 BrowserThread::IO,
768 FROM_HERE,
769 base::Bind(CheckIsHandled, scheme, false, r));
770 }
771
772 static void QuitUILoop() {
773 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
774 MessageLoop::QuitClosure());
775 } 811 }
776 812
777 TEST_F(ProtocolHandlerRegistryTest, TestLoadEnabledGetsPropogatedToIO) { 813 TEST_F(ProtocolHandlerRegistryTest, TestLoadEnabledGetsPropogatedToIO) {
814 std::string mailto("mailto");
815 ProtocolHandler ph1 = CreateProtocolHandler(mailto, "MailtoHandler");
816 registry()->OnAcceptRegisterProtocolHandler(ph1);
817
818 AssertWillHandle(mailto, true, registry()->CreateURLInterceptor());
778 registry()->Disable(); 819 registry()->Disable();
779 ReloadProtocolHandlerRegistry(); 820 AssertWillHandle(mailto, false, registry()->CreateURLInterceptor());
780 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
781 base::Bind(QuitUILoop));
782 MessageLoop::current()->Run();
783 ASSERT_FALSE(enabled_io());
784 } 821 }
785 822
786 TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) { 823 TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) {
787 ProtocolHandler ph1 = CreateProtocolHandler("mailto", 824 ProtocolHandler ph1 = CreateProtocolHandler("mailto",
788 GURL("http://test.com/%s"), "test1"); 825 GURL("http://test.com/%s"), "test1");
789 ProtocolHandler ph2 = CreateProtocolHandler("mailto", 826 ProtocolHandler ph2 = CreateProtocolHandler("mailto",
790 GURL("http://test.com/updated-url/%s"), "test2"); 827 GURL("http://test.com/updated-url/%s"), "test2");
791 registry()->OnAcceptRegisterProtocolHandler(ph1); 828 registry()->OnAcceptRegisterProtocolHandler(ph1);
792 ASSERT_TRUE(registry()->AttemptReplace(ph2)); 829 ASSERT_TRUE(registry()->AttemptReplace(ph2));
793 const ProtocolHandler& handler(registry()->GetHandlerFor("mailto")); 830 const ProtocolHandler& handler(registry()->GetHandlerFor("mailto"));
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
837 ph1.IsSameOrigin(ph2)); 874 ph1.IsSameOrigin(ph2));
838 ASSERT_EQ(ph1.url().GetOrigin() == ph2.url().GetOrigin(), 875 ASSERT_EQ(ph1.url().GetOrigin() == ph2.url().GetOrigin(),
839 ph2.IsSameOrigin(ph1)); 876 ph2.IsSameOrigin(ph1));
840 ASSERT_EQ(ph2.url().GetOrigin() == ph3.url().GetOrigin(), 877 ASSERT_EQ(ph2.url().GetOrigin() == ph3.url().GetOrigin(),
841 ph2.IsSameOrigin(ph3)); 878 ph2.IsSameOrigin(ph3));
842 ASSERT_EQ(ph3.url().GetOrigin() == ph2.url().GetOrigin(), 879 ASSERT_EQ(ph3.url().GetOrigin() == ph2.url().GetOrigin(),
843 ph3.IsSameOrigin(ph2)); 880 ph3.IsSameOrigin(ph2));
844 } 881 }
845 882
846 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { 883 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) {
847 ReloadProtocolHandlerRegistryAndInstallDefaultHandler(); 884 RecreateRegistry(false);
885 registry()->AddPredefinedHandler(CreateProtocolHandler(
886 "test", GURL("http://test.com/%s"), "Test"));
887 registry()->InitProtocolSettings();
848 std::vector<std::string> protocols; 888 std::vector<std::string> protocols;
849 registry()->GetRegisteredProtocols(&protocols); 889 registry()->GetRegisteredProtocols(&protocols);
850 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); 890 ASSERT_EQ(static_cast<size_t>(1), protocols.size());
851 } 891 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698