OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/at_exit.h" | 5 #include "base/at_exit.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "mojo/application_manager/application_loader.h" | 9 #include "mojo/application_manager/application_loader.h" |
10 #include "mojo/application_manager/application_manager.h" | 10 #include "mojo/application_manager/application_manager.h" |
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 } | 395 } |
396 | 396 |
397 int call_count() const { return call_count_; } | 397 int call_count() const { return call_count_; } |
398 | 398 |
399 private: | 399 private: |
400 int call_count_; | 400 int call_count_; |
401 GURL url_; | 401 GURL url_; |
402 DISALLOW_COPY_AND_ASSIGN(TestServiceInterceptor); | 402 DISALLOW_COPY_AND_ASSIGN(TestServiceInterceptor); |
403 }; | 403 }; |
404 | 404 |
| 405 class TestDelegate : public ApplicationManager::Delegate { |
| 406 public: |
| 407 void AddMapping(const GURL& from, const GURL& to) { |
| 408 mappings_[from] = to; |
| 409 } |
| 410 |
| 411 // ApplicationManager::Delegate |
| 412 virtual GURL ResolveURL(const GURL& url) override { |
| 413 auto it = mappings_.find(url); |
| 414 if (it != mappings_.end()) |
| 415 return it->second; |
| 416 return url; |
| 417 } |
| 418 |
| 419 virtual void OnApplicationError(const GURL& url) override { |
| 420 } |
| 421 |
| 422 private: |
| 423 std::map<GURL, GURL> mappings_; |
| 424 }; |
| 425 |
405 } // namespace | 426 } // namespace |
406 | 427 |
407 class ApplicationManagerTest : public testing::Test { | 428 class ApplicationManagerTest : public testing::Test { |
408 public: | 429 public: |
409 ApplicationManagerTest() : tester_context_(&loop_) {} | 430 ApplicationManagerTest() : tester_context_(&loop_) {} |
410 | 431 |
411 ~ApplicationManagerTest() override {} | 432 ~ApplicationManagerTest() override {} |
412 | 433 |
413 void SetUp() override { | 434 void SetUp() override { |
414 application_manager_.reset(new ApplicationManager); | 435 application_manager_.reset(new ApplicationManager(&test_delegate_)); |
415 TestApplicationLoader* default_loader = new TestApplicationLoader; | 436 test_loader_ = new TestApplicationLoader; |
416 default_loader->set_context(&context_); | 437 test_loader_->set_context(&context_); |
417 application_manager_->set_default_loader( | 438 application_manager_->set_default_loader( |
418 scoped_ptr<ApplicationLoader>(default_loader)); | 439 scoped_ptr<ApplicationLoader>(test_loader_)); |
419 | 440 |
420 TestServicePtr service_proxy; | 441 TestServicePtr service_proxy; |
421 application_manager_->ConnectToService(GURL(kTestURLString), | 442 application_manager_->ConnectToService(GURL(kTestURLString), |
422 &service_proxy); | 443 &service_proxy); |
423 test_client_.reset(new TestClientImpl(service_proxy.Pass())); | 444 test_client_.reset(new TestClientImpl(service_proxy.Pass())); |
424 } | 445 } |
425 | 446 |
426 void TearDown() override { | 447 void TearDown() override { |
427 test_client_.reset(NULL); | 448 test_client_.reset(NULL); |
428 application_manager_.reset(NULL); | 449 application_manager_.reset(NULL); |
(...skipping 14 matching lines...) Expand all Loading... |
443 application_manager_->SetLoaderForURL(MakeLoader(requestor_url), url); | 464 application_manager_->SetLoaderForURL(MakeLoader(requestor_url), url); |
444 } | 465 } |
445 | 466 |
446 bool HasFactoryForTestURL() { | 467 bool HasFactoryForTestURL() { |
447 ApplicationManager::TestAPI manager_test_api(application_manager_.get()); | 468 ApplicationManager::TestAPI manager_test_api(application_manager_.get()); |
448 return manager_test_api.HasFactoryForURL(GURL(kTestURLString)); | 469 return manager_test_api.HasFactoryForURL(GURL(kTestURLString)); |
449 } | 470 } |
450 | 471 |
451 protected: | 472 protected: |
452 base::ShadowingAtExitManager at_exit_; | 473 base::ShadowingAtExitManager at_exit_; |
| 474 TestDelegate test_delegate_; |
| 475 TestApplicationLoader* test_loader_; |
453 TesterContext tester_context_; | 476 TesterContext tester_context_; |
454 TestContext context_; | 477 TestContext context_; |
455 base::MessageLoop loop_; | 478 base::MessageLoop loop_; |
456 scoped_ptr<TestClientImpl> test_client_; | 479 scoped_ptr<TestClientImpl> test_client_; |
457 scoped_ptr<ApplicationManager> application_manager_; | 480 scoped_ptr<ApplicationManager> application_manager_; |
458 DISALLOW_COPY_AND_ASSIGN(ApplicationManagerTest); | 481 DISALLOW_COPY_AND_ASSIGN(ApplicationManagerTest); |
459 }; | 482 }; |
460 | 483 |
461 TEST_F(ApplicationManagerTest, Basic) { | 484 TEST_F(ApplicationManagerTest, Basic) { |
462 test_client_->Test("test"); | 485 test_client_->Test("test"); |
463 loop_.Run(); | 486 loop_.Run(); |
464 EXPECT_EQ(std::string("test"), context_.last_test_string); | 487 EXPECT_EQ(std::string("test"), context_.last_test_string); |
465 } | 488 } |
466 | 489 |
467 // Confirm that no arguments are sent to an application by default. | 490 // Confirm that no arguments are sent to an application by default. |
468 TEST_F(ApplicationManagerTest, NoArgs) { | 491 TEST_F(ApplicationManagerTest, NoArgs) { |
469 ApplicationManager am; | 492 ApplicationManager am(&test_delegate_); |
470 GURL test_url("test:test"); | 493 GURL test_url("test:test"); |
471 TestContext context; | 494 TestContext context; |
472 TestApplicationLoader* loader = new TestApplicationLoader; | 495 TestApplicationLoader* loader = new TestApplicationLoader; |
473 loader->set_context(&context); | 496 loader->set_context(&context); |
474 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url); | 497 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url); |
475 TestServicePtr test_service; | 498 TestServicePtr test_service; |
476 am.ConnectToService(test_url, &test_service); | 499 am.ConnectToService(test_url, &test_service); |
477 TestClientImpl test_client(test_service.Pass()); | 500 TestClientImpl test_client(test_service.Pass()); |
478 test_client.Test("test"); | 501 test_client.Test("test"); |
479 loop_.Run(); | 502 loop_.Run(); |
480 std::vector<std::string> app_args = loader->GetArgs(); | 503 std::vector<std::string> app_args = loader->GetArgs(); |
481 EXPECT_EQ(0U, app_args.size()); | 504 EXPECT_EQ(0U, app_args.size()); |
482 } | 505 } |
483 | 506 |
484 // Confirm that arguments are sent to an application. | 507 // Confirm that arguments are sent to an application. |
485 TEST_F(ApplicationManagerTest, Args) { | 508 TEST_F(ApplicationManagerTest, Args) { |
486 ApplicationManager am; | 509 ApplicationManager am(&test_delegate_); |
487 GURL test_url("test:test"); | 510 GURL test_url("test:test"); |
488 std::vector<std::string> args; | 511 std::vector<std::string> args; |
489 args.push_back("test_arg1"); | 512 args.push_back("test_arg1"); |
490 args.push_back("test_arg2"); | 513 args.push_back("test_arg2"); |
491 am.SetArgsForURL(args, test_url); | 514 am.SetArgsForURL(args, test_url); |
492 TestContext context; | 515 TestContext context; |
493 TestApplicationLoader* loader = new TestApplicationLoader; | 516 TestApplicationLoader* loader = new TestApplicationLoader; |
494 loader->set_context(&context); | 517 loader->set_context(&context); |
495 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url); | 518 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url); |
496 TestServicePtr test_service; | 519 TestServicePtr test_service; |
(...skipping 13 matching lines...) Expand all Loading... |
510 loop_.Run(); | 533 loop_.Run(); |
511 EXPECT_EQ(1, context_.num_impls); | 534 EXPECT_EQ(1, context_.num_impls); |
512 test_client_.reset(NULL); | 535 test_client_.reset(NULL); |
513 loop_.Run(); | 536 loop_.Run(); |
514 EXPECT_EQ(0, context_.num_impls); | 537 EXPECT_EQ(0, context_.num_impls); |
515 EXPECT_TRUE(HasFactoryForTestURL()); | 538 EXPECT_TRUE(HasFactoryForTestURL()); |
516 } | 539 } |
517 | 540 |
518 TEST_F(ApplicationManagerTest, Deletes) { | 541 TEST_F(ApplicationManagerTest, Deletes) { |
519 { | 542 { |
520 ApplicationManager am; | 543 ApplicationManager am(&test_delegate_); |
521 TestApplicationLoader* default_loader = new TestApplicationLoader; | 544 TestApplicationLoader* default_loader = new TestApplicationLoader; |
522 default_loader->set_context(&context_); | 545 default_loader->set_context(&context_); |
523 TestApplicationLoader* url_loader1 = new TestApplicationLoader; | 546 TestApplicationLoader* url_loader1 = new TestApplicationLoader; |
524 TestApplicationLoader* url_loader2 = new TestApplicationLoader; | 547 TestApplicationLoader* url_loader2 = new TestApplicationLoader; |
525 url_loader1->set_context(&context_); | 548 url_loader1->set_context(&context_); |
526 url_loader2->set_context(&context_); | 549 url_loader2->set_context(&context_); |
527 TestApplicationLoader* scheme_loader1 = new TestApplicationLoader; | 550 TestApplicationLoader* scheme_loader1 = new TestApplicationLoader; |
528 TestApplicationLoader* scheme_loader2 = new TestApplicationLoader; | 551 TestApplicationLoader* scheme_loader2 = new TestApplicationLoader; |
529 scheme_loader1->set_context(&context_); | 552 scheme_loader1->set_context(&context_); |
530 scheme_loader2->set_context(&context_); | 553 scheme_loader2->set_context(&context_); |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
670 | 693 |
671 std::string url("test:test3"); | 694 std::string url("test:test3"); |
672 TestServicePtr test_service; | 695 TestServicePtr test_service; |
673 application_manager_->ConnectToService(GURL(url), &test_service); | 696 application_manager_->ConnectToService(GURL(url), &test_service); |
674 | 697 |
675 EXPECT_EQ(1, interceptor.call_count()); | 698 EXPECT_EQ(1, interceptor.call_count()); |
676 EXPECT_EQ(url, interceptor.url_spec()); | 699 EXPECT_EQ(url, interceptor.url_spec()); |
677 EXPECT_EQ(1, default_loader->num_loads()); | 700 EXPECT_EQ(1, default_loader->num_loads()); |
678 } | 701 } |
679 | 702 |
| 703 TEST_F(ApplicationManagerTest, MappedURLsShouldNotCauseDuplicateLoad) { |
| 704 test_delegate_.AddMapping(GURL("foo:foo2"), GURL("foo:foo")); |
| 705 // 1 because ApplicationManagerTest connects once at startup. |
| 706 EXPECT_EQ(1, test_loader_->num_loads()); |
| 707 |
| 708 TestServicePtr test_service; |
| 709 application_manager_->ConnectToService(GURL("foo:foo"), &test_service); |
| 710 EXPECT_EQ(2, test_loader_->num_loads()); |
| 711 |
| 712 TestServicePtr test_service2; |
| 713 application_manager_->ConnectToService(GURL("foo:foo2"), &test_service2); |
| 714 EXPECT_EQ(2, test_loader_->num_loads()); |
| 715 |
| 716 TestServicePtr test_service3; |
| 717 application_manager_->ConnectToService(GURL("bar:bar"), &test_service2); |
| 718 EXPECT_EQ(3, test_loader_->num_loads()); |
| 719 } |
| 720 |
680 } // namespace mojo | 721 } // namespace mojo |
OLD | NEW |