| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 | 77 |
| 78 protected: | 78 protected: |
| 79 std::unique_ptr<Connection> ConnectTo(Connector::ConnectParams* params) { | 79 std::unique_ptr<Connection> ConnectTo(Connector::ConnectParams* params) { |
| 80 std::unique_ptr<Connection> connection = connector()->Connect(params); | 80 std::unique_ptr<Connection> connection = connector()->Connect(params); |
| 81 base::RunLoop loop; | 81 base::RunLoop loop; |
| 82 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); | 82 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); |
| 83 loop.Run(); | 83 loop.Run(); |
| 84 return connection; | 84 return connection; |
| 85 } | 85 } |
| 86 | 86 |
| 87 void set_ping_callback(const base::Closure& callback) { | |
| 88 ping_callback_ = callback; | |
| 89 } | |
| 90 | |
| 91 void CompareConnectionState( | 87 void CompareConnectionState( |
| 92 const std::string& connection_local_name, | 88 const std::string& connection_local_name, |
| 93 const std::string& connection_remote_name, | 89 const std::string& connection_remote_name, |
| 94 const std::string& connection_remote_userid, | 90 const std::string& connection_remote_userid, |
| 95 const std::string& initialize_local_name, | 91 const std::string& initialize_local_name, |
| 96 const std::string& initialize_userid) { | 92 const std::string& initialize_userid) { |
| 97 EXPECT_EQ(connection_local_name, connection_state_->connection_local_name); | 93 EXPECT_EQ(connection_local_name, connection_state_->connection_local_name); |
| 98 EXPECT_EQ(connection_remote_name, | 94 EXPECT_EQ(connection_remote_name, |
| 99 connection_state_->connection_remote_name); | 95 connection_state_->connection_remote_name); |
| 100 EXPECT_EQ(connection_remote_userid, | 96 EXPECT_EQ(connection_remote_userid, |
| 101 connection_state_->connection_remote_userid); | 97 connection_state_->connection_remote_userid); |
| 102 EXPECT_EQ(initialize_local_name, connection_state_->initialize_local_name); | 98 EXPECT_EQ(initialize_local_name, connection_state_->initialize_local_name); |
| 103 EXPECT_EQ(initialize_userid, connection_state_->initialize_userid); | 99 EXPECT_EQ(initialize_userid, connection_state_->initialize_userid); |
| 104 } | 100 } |
| 105 | 101 |
| 106 private: | 102 private: |
| 103 class TestService : public test::ServiceTestClient { |
| 104 public: |
| 105 explicit TestService(ConnectTest* connect_test) |
| 106 : test::ServiceTestClient(connect_test), |
| 107 connect_test_(connect_test) {} |
| 108 ~TestService() override {} |
| 109 |
| 110 private: |
| 111 bool OnConnect(Connection* connection) override { |
| 112 connection->AddInterface<test::mojom::ExposedInterface>(connect_test_); |
| 113 return true; |
| 114 } |
| 115 |
| 116 ConnectTest* connect_test_; |
| 117 |
| 118 DISALLOW_COPY_AND_ASSIGN(TestService); |
| 119 }; |
| 120 |
| 107 // test::ServiceTest: | 121 // test::ServiceTest: |
| 108 void SetUp() override { | 122 void SetUp() override { |
| 109 test::ServiceTest::SetUp(); | 123 test::ServiceTest::SetUp(); |
| 110 // We need to connect to the package first to force the shell to read the | 124 // We need to connect to the package first to force the shell to read the |
| 111 // package app's manifest and register aliases for the applications it | 125 // package app's manifest and register aliases for the applications it |
| 112 // provides. | 126 // provides. |
| 113 test::mojom::ConnectTestServicePtr root_service; | 127 test::mojom::ConnectTestServicePtr root_service; |
| 114 std::unique_ptr<Connection> connection = | 128 std::unique_ptr<Connection> connection = |
| 115 connector()->Connect(kTestPackageName); | 129 connector()->Connect(kTestPackageName); |
| 116 connection->GetInterface(&root_service); | 130 connection->GetInterface(&root_service); |
| 117 base::RunLoop run_loop; | 131 base::RunLoop run_loop; |
| 118 std::string root_name; | 132 std::string root_name; |
| 119 root_service->GetTitle( | 133 root_service->GetTitle( |
| 120 base::Bind(&ReceiveOneString, &root_name, &run_loop)); | 134 base::Bind(&ReceiveOneString, &root_name, &run_loop)); |
| 121 run_loop.Run(); | 135 run_loop.Run(); |
| 122 } | 136 } |
| 137 std::unique_ptr<Service> CreateService() override { |
| 138 return base::WrapUnique(new TestService(this)); |
| 139 } |
| 123 | 140 |
| 124 // InterfaceFactory<test::mojom::ExposedInterface>: | 141 // InterfaceFactory<test::mojom::ExposedInterface>: |
| 125 void Create(const Identity& remote_identity, | 142 void Create(const Identity& remote_identity, |
| 126 test::mojom::ExposedInterfaceRequest request) override { | 143 test::mojom::ExposedInterfaceRequest request) override { |
| 127 bindings_.AddBinding(this, std::move(request)); | 144 bindings_.AddBinding(this, std::move(request)); |
| 128 } | 145 } |
| 129 | 146 |
| 130 void ConnectionAccepted(test::mojom::ConnectionStatePtr state) override { | 147 void ConnectionAccepted(test::mojom::ConnectionStatePtr state) override { |
| 131 connection_state_ = std::move(state); | 148 connection_state_ = std::move(state); |
| 132 ping_callback_.Run(); | |
| 133 } | 149 } |
| 134 | 150 |
| 135 base::Closure ping_callback_; | |
| 136 test::mojom::ConnectionStatePtr connection_state_; | 151 test::mojom::ConnectionStatePtr connection_state_; |
| 137 | 152 |
| 138 mojo::BindingSet<test::mojom::ExposedInterface> bindings_; | 153 mojo::BindingSet<test::mojom::ExposedInterface> bindings_; |
| 139 | 154 |
| 140 DISALLOW_COPY_AND_ASSIGN(ConnectTest); | 155 DISALLOW_COPY_AND_ASSIGN(ConnectTest); |
| 141 }; | 156 }; |
| 142 | 157 |
| 143 // Ensure the connection was properly established and that a round trip | 158 // Ensure the connection was properly established and that a round trip |
| 144 // method call/response is completed. | 159 // method call/response is completed. |
| 145 TEST_F(ConnectTest, Connect) { | 160 TEST_F(ConnectTest, Connect) { |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 { | 408 { |
| 394 base::RunLoop loop; | 409 base::RunLoop loop; |
| 395 inherit_connection->AddConnectionCompletedClosure( | 410 inherit_connection->AddConnectionCompletedClosure( |
| 396 base::Bind(&QuitLoop, &loop)); | 411 base::Bind(&QuitLoop, &loop)); |
| 397 loop.Run(); | 412 loop.Run(); |
| 398 EXPECT_EQ(inherit_connection->GetRemoteIdentity().user_id(), | 413 EXPECT_EQ(inherit_connection->GetRemoteIdentity().user_id(), |
| 399 connection->GetRemoteIdentity().user_id()); | 414 connection->GetRemoteIdentity().user_id()); |
| 400 } | 415 } |
| 401 } | 416 } |
| 402 | 417 |
| 403 // Tests that we can expose an interface to targets on outbound connections. | |
| 404 TEST_F(ConnectTest, LocalInterface) { | |
| 405 // Connect to a standalone application. | |
| 406 { | |
| 407 test::mojom::ConnectTestServicePtr service; | |
| 408 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppName); | |
| 409 connection->GetInterface(&service); | |
| 410 connection->AddInterface<test::mojom::ExposedInterface>(this); | |
| 411 | |
| 412 { | |
| 413 base::RunLoop run_loop; | |
| 414 EXPECT_TRUE(connection->IsPending()); | |
| 415 EXPECT_EQ(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); | |
| 416 connection->AddConnectionCompletedClosure( | |
| 417 base::Bind(&QuitLoop, &run_loop)); | |
| 418 run_loop.Run(); | |
| 419 EXPECT_FALSE(connection->IsPending()); | |
| 420 } | |
| 421 | |
| 422 { | |
| 423 base::RunLoop run_loop; | |
| 424 set_ping_callback(base::Bind(&QuitLoop, &run_loop)); | |
| 425 run_loop.Run(); | |
| 426 CompareConnectionState( | |
| 427 kTestAppName, test_name(), test_userid(), kTestAppName, | |
| 428 connection->GetRemoteIdentity().user_id()); | |
| 429 } | |
| 430 } | |
| 431 | |
| 432 // Connect to an application provided by a package. | |
| 433 { | |
| 434 test::mojom::ConnectTestServicePtr service_a; | |
| 435 std::unique_ptr<Connection> connection = | |
| 436 connector()->Connect(kTestAppAName); | |
| 437 connection->GetInterface(&service_a); | |
| 438 connection->AddInterface<test::mojom::ExposedInterface>(this); | |
| 439 | |
| 440 { | |
| 441 base::RunLoop run_loop; | |
| 442 EXPECT_TRUE(connection->IsPending()); | |
| 443 EXPECT_EQ(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); | |
| 444 connection->AddConnectionCompletedClosure( | |
| 445 base::Bind(&QuitLoop, &run_loop)); | |
| 446 run_loop.Run(); | |
| 447 EXPECT_FALSE(connection->IsPending()); | |
| 448 } | |
| 449 | |
| 450 { | |
| 451 base::RunLoop run_loop; | |
| 452 set_ping_callback(base::Bind(&QuitLoop, &run_loop)); | |
| 453 run_loop.Run(); | |
| 454 CompareConnectionState( | |
| 455 kTestAppAName, test_name(), test_userid(), kTestAppAName, | |
| 456 connection->GetRemoteIdentity().user_id()); | |
| 457 } | |
| 458 | |
| 459 } | |
| 460 } | |
| 461 | |
| 462 } // namespace shell | 418 } // namespace shell |
| OLD | NEW |