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 <utility> | 5 #include <utility> |
6 | 6 |
7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "mojo/public/cpp/bindings/strong_binding.h" | 13 #include "mojo/public/cpp/bindings/strong_binding.h" |
14 #include "mojo/services/package_manager/package_manager.h" | 14 #include "mojo/services/package_manager/package_manager.h" |
15 #include "mojo/shell/application_manager.h" | |
16 #include "mojo/shell/connect_util.h" | 15 #include "mojo/shell/connect_util.h" |
17 #include "mojo/shell/loader.h" | 16 #include "mojo/shell/loader.h" |
18 #include "mojo/shell/public/cpp/connector.h" | 17 #include "mojo/shell/public/cpp/connector.h" |
19 #include "mojo/shell/public/cpp/interface_factory.h" | 18 #include "mojo/shell/public/cpp/interface_factory.h" |
20 #include "mojo/shell/public/cpp/shell_client.h" | 19 #include "mojo/shell/public/cpp/shell_client.h" |
21 #include "mojo/shell/public/cpp/shell_connection.h" | 20 #include "mojo/shell/public/cpp/shell_connection.h" |
22 #include "mojo/shell/public/interfaces/interface_provider.mojom.h" | 21 #include "mojo/shell/public/interfaces/interface_provider.mojom.h" |
| 22 #include "mojo/shell/shell.h" |
23 #include "mojo/shell/tests/test.mojom.h" | 23 #include "mojo/shell/tests/test.mojom.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
25 | 25 |
26 namespace mojo { | 26 namespace mojo { |
27 namespace shell { | 27 namespace shell { |
28 namespace test { | 28 namespace test { |
29 | 29 |
30 const char kTestURLString[] = "test:testService"; | 30 const char kTestURLString[] = "test:testService"; |
31 const char kTestAURLString[] = "test:TestA"; | 31 const char kTestAURLString[] = "test:TestA"; |
32 const char kTestBURLString[] = "test:TestB"; | 32 const char kTestBURLString[] = "test:TestB"; |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 void OnConnect(base::RunLoop* loop, uint32_t instance_id, uint32_t user_id) { | 392 void OnConnect(base::RunLoop* loop, uint32_t instance_id, uint32_t user_id) { |
393 loop->Quit(); | 393 loop->Quit(); |
394 } | 394 } |
395 | 395 |
396 class LoaderTest : public testing::Test { | 396 class LoaderTest : public testing::Test { |
397 public: | 397 public: |
398 LoaderTest() : tester_context_(&loop_) {} | 398 LoaderTest() : tester_context_(&loop_) {} |
399 ~LoaderTest() override {} | 399 ~LoaderTest() override {} |
400 | 400 |
401 void SetUp() override { | 401 void SetUp() override { |
402 application_manager_.reset( | 402 shell_.reset(new Shell(nullptr, nullptr, nullptr)); |
403 new ApplicationManager(nullptr, nullptr, nullptr)); | |
404 test_loader_ = new TestLoader(&context_); | 403 test_loader_ = new TestLoader(&context_); |
405 application_manager_->set_default_loader(scoped_ptr<Loader>(test_loader_)); | 404 shell_->set_default_loader(scoped_ptr<Loader>(test_loader_)); |
406 | 405 |
407 TestServicePtr service_proxy; | 406 TestServicePtr service_proxy; |
408 ConnectToInterface(kTestURLString, &service_proxy); | 407 ConnectToInterface(kTestURLString, &service_proxy); |
409 test_client_.reset(new TestClient(std::move(service_proxy))); | 408 test_client_.reset(new TestClient(std::move(service_proxy))); |
410 } | 409 } |
411 | 410 |
412 void TearDown() override { | 411 void TearDown() override { |
413 test_client_.reset(); | 412 test_client_.reset(); |
414 application_manager_.reset(); | 413 shell_.reset(); |
415 } | 414 } |
416 | 415 |
417 void AddLoaderForName(const std::string& name, | 416 void AddLoaderForName(const std::string& name, |
418 const std::string& requestor_name) { | 417 const std::string& requestor_name) { |
419 application_manager_->SetLoaderForName( | 418 shell_->SetLoaderForName( |
420 make_scoped_ptr(new Tester(&tester_context_, requestor_name)), name); | 419 make_scoped_ptr(new Tester(&tester_context_, requestor_name)), name); |
421 } | 420 } |
422 | 421 |
423 bool HasRunningInstanceForName(const std::string& name) { | 422 bool HasRunningInstanceForName(const std::string& name) { |
424 ApplicationManager::TestAPI manager_test_api(application_manager_.get()); | 423 Shell::TestAPI test_api(shell_.get()); |
425 return manager_test_api.HasRunningInstanceForName(name); | 424 return test_api.HasRunningInstanceForName(name); |
426 } | 425 } |
427 | 426 |
428 protected: | 427 protected: |
429 template <typename Interface> | 428 template <typename Interface> |
430 void ConnectToInterface(const std::string& name, | 429 void ConnectToInterface(const std::string& name, |
431 InterfacePtr<Interface>* ptr) { | 430 InterfacePtr<Interface>* ptr) { |
432 base::RunLoop loop; | 431 base::RunLoop loop; |
433 mojom::InterfaceProviderPtr remote_interfaces; | 432 mojom::InterfaceProviderPtr remote_interfaces; |
434 scoped_ptr<ConnectParams> params(new ConnectParams); | 433 scoped_ptr<ConnectParams> params(new ConnectParams); |
435 params->set_source(CreateShellIdentity()); | 434 params->set_source(CreateShellIdentity()); |
436 params->set_target(Identity(name)); | 435 params->set_target(Identity(name)); |
437 params->set_remote_interfaces(GetProxy(&remote_interfaces)); | 436 params->set_remote_interfaces(GetProxy(&remote_interfaces)); |
438 params->set_connect_callback( | 437 params->set_connect_callback( |
439 base::Bind(&OnConnect, base::Unretained(&loop))); | 438 base::Bind(&OnConnect, base::Unretained(&loop))); |
440 application_manager_->Connect(std::move(params)); | 439 shell_->Connect(std::move(params)); |
441 loop.Run(); | 440 loop.Run(); |
442 | 441 |
443 mojo::GetInterface(remote_interfaces.get(), ptr); | 442 mojo::GetInterface(remote_interfaces.get(), ptr); |
444 } | 443 } |
445 | 444 |
446 base::ShadowingAtExitManager at_exit_; | 445 base::ShadowingAtExitManager at_exit_; |
447 TestLoader* test_loader_; | 446 TestLoader* test_loader_; |
448 TesterContext tester_context_; | 447 TesterContext tester_context_; |
449 TestContext context_; | 448 TestContext context_; |
450 base::MessageLoop loop_; | 449 base::MessageLoop loop_; |
451 scoped_ptr<TestClient> test_client_; | 450 scoped_ptr<TestClient> test_client_; |
452 scoped_ptr<ApplicationManager> application_manager_; | 451 scoped_ptr<Shell> shell_; |
453 DISALLOW_COPY_AND_ASSIGN(LoaderTest); | 452 DISALLOW_COPY_AND_ASSIGN(LoaderTest); |
454 }; | 453 }; |
455 | 454 |
456 TEST_F(LoaderTest, Basic) { | 455 TEST_F(LoaderTest, Basic) { |
457 test_client_->Test("test"); | 456 test_client_->Test("test"); |
458 loop_.Run(); | 457 loop_.Run(); |
459 EXPECT_EQ(std::string("test"), context_.last_test_string); | 458 EXPECT_EQ(std::string("test"), context_.last_test_string); |
460 } | 459 } |
461 | 460 |
462 TEST_F(LoaderTest, ClientError) { | 461 TEST_F(LoaderTest, ClientError) { |
463 test_client_->Test("test"); | 462 test_client_->Test("test"); |
464 EXPECT_TRUE(HasRunningInstanceForName(kTestURLString)); | 463 EXPECT_TRUE(HasRunningInstanceForName(kTestURLString)); |
465 loop_.Run(); | 464 loop_.Run(); |
466 EXPECT_EQ(1, context_.num_impls); | 465 EXPECT_EQ(1, context_.num_impls); |
467 test_client_.reset(); | 466 test_client_.reset(); |
468 loop_.Run(); | 467 loop_.Run(); |
469 EXPECT_EQ(0, context_.num_impls); | 468 EXPECT_EQ(0, context_.num_impls); |
470 EXPECT_TRUE(HasRunningInstanceForName(kTestURLString)); | 469 EXPECT_TRUE(HasRunningInstanceForName(kTestURLString)); |
471 } | 470 } |
472 | 471 |
473 TEST_F(LoaderTest, Deletes) { | 472 TEST_F(LoaderTest, Deletes) { |
474 { | 473 { |
475 ApplicationManager am(nullptr, nullptr, nullptr); | 474 Shell shell(nullptr, nullptr, nullptr); |
476 TestLoader* default_loader = new TestLoader(&context_); | 475 TestLoader* default_loader = new TestLoader(&context_); |
477 TestLoader* name_loader1 = new TestLoader(&context_); | 476 TestLoader* name_loader1 = new TestLoader(&context_); |
478 TestLoader* name_loader2 = new TestLoader(&context_); | 477 TestLoader* name_loader2 = new TestLoader(&context_); |
479 am.set_default_loader(scoped_ptr<Loader>(default_loader)); | 478 shell.set_default_loader(scoped_ptr<Loader>(default_loader)); |
480 am.SetLoaderForName(scoped_ptr<Loader>(name_loader1), "test:test1"); | 479 shell.SetLoaderForName(scoped_ptr<Loader>(name_loader1), "test:test1"); |
481 am.SetLoaderForName(scoped_ptr<Loader>(name_loader2), "test:test1"); | 480 shell.SetLoaderForName(scoped_ptr<Loader>(name_loader2), "test:test1"); |
482 } | 481 } |
483 EXPECT_EQ(3, context_.num_loader_deletes); | 482 EXPECT_EQ(3, context_.num_loader_deletes); |
484 } | 483 } |
485 | 484 |
486 // Test for SetLoaderForName() & set_default_loader(). | 485 // Test for SetLoaderForName() & set_default_loader(). |
487 TEST_F(LoaderTest, SetLoaders) { | 486 TEST_F(LoaderTest, SetLoaders) { |
488 TestLoader* default_loader = new TestLoader(&context_); | 487 TestLoader* default_loader = new TestLoader(&context_); |
489 TestLoader* name_loader = new TestLoader(&context_); | 488 TestLoader* name_loader = new TestLoader(&context_); |
490 application_manager_->set_default_loader(scoped_ptr<Loader>(default_loader)); | 489 shell_->set_default_loader(scoped_ptr<Loader>(default_loader)); |
491 application_manager_->SetLoaderForName( | 490 shell_->SetLoaderForName(scoped_ptr<Loader>(name_loader), "test:test1"); |
492 scoped_ptr<Loader>(name_loader), "test:test1"); | |
493 | 491 |
494 // test::test1 should go to name_loader. | 492 // test::test1 should go to name_loader. |
495 TestServicePtr test_service; | 493 TestServicePtr test_service; |
496 ConnectToInterface("test:test1", &test_service); | 494 ConnectToInterface("test:test1", &test_service); |
497 EXPECT_EQ(1, name_loader->num_loads()); | 495 EXPECT_EQ(1, name_loader->num_loads()); |
498 EXPECT_EQ(0, default_loader->num_loads()); | 496 EXPECT_EQ(0, default_loader->num_loads()); |
499 | 497 |
500 // http::test1 should go to default loader. | 498 // http::test1 should go to default loader. |
501 ConnectToInterface("http:test1", &test_service); | 499 ConnectToInterface("http:test1", &test_service); |
502 EXPECT_EQ(1, name_loader->num_loads()); | 500 EXPECT_EQ(1, name_loader->num_loads()); |
503 EXPECT_EQ(1, default_loader->num_loads()); | 501 EXPECT_EQ(1, default_loader->num_loads()); |
504 } | 502 } |
505 | 503 |
506 TEST_F(LoaderTest, NoServiceNoLoad) { | 504 TEST_F(LoaderTest, NoServiceNoLoad) { |
507 AddLoaderForName(kTestAURLString, std::string()); | 505 AddLoaderForName(kTestAURLString, std::string()); |
508 | 506 |
509 // There is no TestC service implementation registered with | 507 // There is no TestC service implementation registered with the Shell, so this |
510 // ApplicationManager, so this cannot succeed (but also shouldn't crash). | 508 // cannot succeed (but also shouldn't crash). |
511 TestCPtr c; | 509 TestCPtr c; |
512 ConnectToInterface(kTestAURLString, &c); | 510 ConnectToInterface(kTestAURLString, &c); |
513 c.set_connection_error_handler( | 511 c.set_connection_error_handler( |
514 []() { base::MessageLoop::current()->QuitWhenIdle(); }); | 512 []() { base::MessageLoop::current()->QuitWhenIdle(); }); |
515 | 513 |
516 loop_.Run(); | 514 loop_.Run(); |
517 EXPECT_TRUE(c.encountered_error()); | 515 EXPECT_TRUE(c.encountered_error()); |
518 } | 516 } |
519 | 517 |
520 TEST_F(LoaderTest, TestEndApplicationClosure) { | 518 TEST_F(LoaderTest, TestEndApplicationClosure) { |
521 ClosingLoader* loader = new ClosingLoader(); | 519 ClosingLoader* loader = new ClosingLoader(); |
522 application_manager_->SetLoaderForName( | 520 shell_->SetLoaderForName(scoped_ptr<Loader>(loader), "test:test"); |
523 scoped_ptr<Loader>(loader), "test:test"); | |
524 | 521 |
525 bool called = false; | 522 bool called = false; |
526 scoped_ptr<ConnectParams> params(new ConnectParams); | 523 scoped_ptr<ConnectParams> params(new ConnectParams); |
527 params->set_source(CreateShellIdentity()); | 524 params->set_source(CreateShellIdentity()); |
528 params->set_target(Identity("test:test", "", mojom::Connector::kUserRoot)); | 525 params->set_target(Identity("test:test", "", mojom::Connector::kUserRoot)); |
529 application_manager_->SetInstanceQuitCallback( | 526 shell_->SetInstanceQuitCallback( |
530 base::Bind(&QuitClosure, params->target(), &called)); | 527 base::Bind(&QuitClosure, params->target(), &called)); |
531 application_manager_->Connect(std::move(params)); | 528 shell_->Connect(std::move(params)); |
532 loop_.Run(); | 529 loop_.Run(); |
533 EXPECT_TRUE(called); | 530 EXPECT_TRUE(called); |
534 } | 531 } |
535 | 532 |
536 TEST_F(LoaderTest, SameIdentityShouldNotCauseDuplicateLoad) { | 533 TEST_F(LoaderTest, SameIdentityShouldNotCauseDuplicateLoad) { |
537 // 1 because LoaderTest connects once at startup. | 534 // 1 because LoaderTest connects once at startup. |
538 EXPECT_EQ(1, test_loader_->num_loads()); | 535 EXPECT_EQ(1, test_loader_->num_loads()); |
539 | 536 |
540 TestServicePtr test_service; | 537 TestServicePtr test_service; |
541 ConnectToInterface("test:foo", &test_service); | 538 ConnectToInterface("test:foo", &test_service); |
542 EXPECT_EQ(2, test_loader_->num_loads()); | 539 EXPECT_EQ(2, test_loader_->num_loads()); |
543 | 540 |
544 // Exactly the same name as above. | 541 // Exactly the same name as above. |
545 ConnectToInterface("test:foo", &test_service); | 542 ConnectToInterface("test:foo", &test_service); |
546 EXPECT_EQ(2, test_loader_->num_loads()); | 543 EXPECT_EQ(2, test_loader_->num_loads()); |
547 | 544 |
548 // A different identity because the domain is different. | 545 // A different identity because the domain is different. |
549 ConnectToInterface("test:bar", &test_service); | 546 ConnectToInterface("test:bar", &test_service); |
550 EXPECT_EQ(3, test_loader_->num_loads()); | 547 EXPECT_EQ(3, test_loader_->num_loads()); |
551 } | 548 } |
552 | 549 |
553 } // namespace test | 550 } // namespace test |
554 } // namespace shell | 551 } // namespace shell |
555 } // namespace mojo | 552 } // namespace mojo |
OLD | NEW |