OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/process/process_handle.h" | 13 #include "base/process/process_handle.h" |
| 14 #include "mojo/common/weak_binding_set.h" |
14 #include "mojo/converters/network/network_type_converters.h" | 15 #include "mojo/converters/network/network_type_converters.h" |
15 #include "mojo/shell/application_manager_apptests.mojom.h" | 16 #include "mojo/shell/application_manager_apptests.mojom.h" |
16 #include "mojo/shell/public/cpp/application_impl.h" | 17 #include "mojo/shell/public/cpp/application_impl.h" |
17 #include "mojo/shell/public/cpp/application_test_base.h" | 18 #include "mojo/shell/public/cpp/application_test_base.h" |
18 #include "mojo/shell/public/cpp/interface_factory.h" | 19 #include "mojo/shell/public/cpp/interface_factory.h" |
19 #include "mojo/shell/public/interfaces/application_manager.mojom.h" | 20 #include "mojo/shell/public/interfaces/application_manager.mojom.h" |
20 | 21 |
21 using mojo::shell::test::mojom::CreateInstanceForHandleTest; | 22 using mojo::shell::test::mojom::CreateInstanceForHandleTest; |
22 | 23 |
23 namespace mojo { | 24 namespace mojo { |
24 namespace shell { | 25 namespace shell { |
25 namespace { | 26 namespace { |
26 | 27 |
27 class ApplicationManagerAppTestDelegate | 28 class ApplicationManagerAppTestDelegate |
28 : public ApplicationDelegate, | 29 : public ApplicationDelegate, |
29 public InterfaceFactory<CreateInstanceForHandleTest>, | 30 public InterfaceFactory<CreateInstanceForHandleTest>, |
30 public CreateInstanceForHandleTest { | 31 public CreateInstanceForHandleTest { |
31 public: | 32 public: |
32 ApplicationManagerAppTestDelegate() : binding_(this) {} | 33 ApplicationManagerAppTestDelegate() |
| 34 : target_id_(Shell::kInvalidApplicationID), |
| 35 binding_(this) {} |
33 ~ApplicationManagerAppTestDelegate() override {} | 36 ~ApplicationManagerAppTestDelegate() override {} |
34 | 37 |
35 const std::string& data() const { return data_; } | 38 uint32_t target_id() const { return target_id_; } |
36 | 39 |
37 private: | 40 private: |
38 // ApplicationDelegate: | 41 // ApplicationDelegate: |
39 void Initialize(ApplicationImpl* app) override {} | 42 void Initialize(ApplicationImpl* app) override {} |
40 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | 43 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { |
41 connection->AddService<CreateInstanceForHandleTest>(this); | 44 connection->AddService<CreateInstanceForHandleTest>(this); |
42 return true; | 45 return true; |
43 } | 46 } |
44 | 47 |
45 // InterfaceFactory<CreateInstanceForHandleTest>: | 48 // InterfaceFactory<CreateInstanceForHandleTest>: |
46 void Create( | 49 void Create( |
47 ApplicationConnection* connection, | 50 ApplicationConnection* connection, |
48 InterfaceRequest<CreateInstanceForHandleTest> request) override { | 51 InterfaceRequest<CreateInstanceForHandleTest> request) override { |
49 binding_.Bind(std::move(request)); | 52 binding_.Bind(std::move(request)); |
50 } | 53 } |
51 | 54 |
52 // CreateInstanceForHandleTest: | 55 // CreateInstanceForHandleTest: |
53 void Ping(const String& data) override { | 56 void SetTargetID(uint32_t target_id) override { |
54 data_ = data; | 57 target_id_ = target_id; |
55 base::MessageLoop::current()->QuitWhenIdle(); | 58 base::MessageLoop::current()->QuitWhenIdle(); |
56 } | 59 } |
57 | 60 |
58 std::string data_; | 61 uint32_t target_id_; |
59 | 62 |
60 Binding<CreateInstanceForHandleTest> binding_; | 63 Binding<CreateInstanceForHandleTest> binding_; |
61 | 64 |
62 DISALLOW_COPY_AND_ASSIGN(ApplicationManagerAppTestDelegate); | 65 DISALLOW_COPY_AND_ASSIGN(ApplicationManagerAppTestDelegate); |
63 }; | 66 }; |
64 | 67 |
65 } // namespace | 68 } // namespace |
66 | 69 |
67 class ApplicationManagerAppTest : public mojo::test::ApplicationTestBase, | 70 class ApplicationManagerAppTest : public mojo::test::ApplicationTestBase, |
68 public mojom::ApplicationManagerListener { | 71 public mojom::ApplicationManagerListener { |
(...skipping 20 matching lines...) Expand all Loading... |
89 application_impl()->ConnectToService("mojo:shell", &application_manager); | 92 application_impl()->ConnectToService("mojo:shell", &application_manager); |
90 | 93 |
91 mojom::ApplicationManagerListenerPtr listener; | 94 mojom::ApplicationManagerListenerPtr listener; |
92 InterfaceRequest<mojom::ApplicationManagerListener> request = | 95 InterfaceRequest<mojom::ApplicationManagerListener> request = |
93 GetProxy(&listener); | 96 GetProxy(&listener); |
94 application_manager->AddListener(std::move(listener)); | 97 application_manager->AddListener(std::move(listener)); |
95 binding_.Bind(std::move(request)); | 98 binding_.Bind(std::move(request)); |
96 binding_.WaitForIncomingMethodCall(); | 99 binding_.WaitForIncomingMethodCall(); |
97 } | 100 } |
98 | 101 |
99 const std::string& data() const { | 102 uint32_t target_id() const { |
100 DCHECK(delegate_); | 103 DCHECK(delegate_); |
101 return delegate_->data(); | 104 return delegate_->target_id(); |
102 } | 105 } |
103 | 106 |
104 const std::vector<ApplicationInfo>& applications() const { | 107 const std::vector<ApplicationInfo>& applications() const { |
105 return applications_; | 108 return applications_; |
106 } | 109 } |
107 | 110 |
108 ApplicationManagerAppTestDelegate* delegate() { return delegate_; } | 111 ApplicationManagerAppTestDelegate* delegate() { return delegate_; } |
109 | 112 |
110 private: | 113 private: |
111 // test::ApplicationTestBase: | 114 // test::ApplicationTestBase: |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 | 148 |
146 DISALLOW_COPY_AND_ASSIGN(ApplicationManagerAppTest); | 149 DISALLOW_COPY_AND_ASSIGN(ApplicationManagerAppTest); |
147 }; | 150 }; |
148 | 151 |
149 TEST_F(ApplicationManagerAppTest, CreateInstanceForHandle) { | 152 TEST_F(ApplicationManagerAppTest, CreateInstanceForHandle) { |
150 AddListenerAndWaitForApplications(); | 153 AddListenerAndWaitForApplications(); |
151 | 154 |
152 // 1. Launch a process. (Actually, have the runner launch a process that | 155 // 1. Launch a process. (Actually, have the runner launch a process that |
153 // launches a process. #becauselinkerrors). | 156 // launches a process. #becauselinkerrors). |
154 mojo::shell::test::mojom::DriverPtr driver; | 157 mojo::shell::test::mojom::DriverPtr driver; |
155 application_impl()->ConnectToService("exe:application_manager_apptest_driver", | 158 scoped_ptr<ApplicationConnection> connection = |
156 &driver); | 159 application_impl()->ConnectToApplication( |
| 160 "exe:application_manager_apptest_driver"); |
| 161 connection->ConnectToService(&driver); |
157 | 162 |
158 // 2. Wait for the target to connect to us. (via | 163 // 2. Wait for the target to connect to us. (via |
159 // mojo:application_manager_apptests) | 164 // mojo:application_manager_apptests) |
160 base::MessageLoop::current()->Run(); | 165 base::MessageLoop::current()->Run(); |
161 | 166 |
162 // 3.1. Validate that we got the ping from the target process... | 167 uint32_t remote_id = Shell::kInvalidApplicationID; |
163 EXPECT_EQ("From Target", data()); | 168 EXPECT_TRUE(connection->GetRemoteApplicationID(&remote_id)); |
| 169 EXPECT_NE(Shell::kInvalidApplicationID, remote_id); |
164 | 170 |
165 // 3.2. ... and that the right applications/processes were created. | 171 // 2. Validate that the right applications/processes were created. |
166 // Note that the target process will be created even if the tests are | 172 // Note that the target process will be created even if the tests are |
167 // run with --single-process. | 173 // run with --single-process. |
168 EXPECT_EQ(2u, applications().size()); | 174 EXPECT_EQ(2u, applications().size()); |
169 { | 175 { |
170 auto& application = applications().front(); | 176 auto& application = applications().front(); |
| 177 EXPECT_EQ(remote_id, application.id); |
171 EXPECT_EQ("exe://application_manager_apptest_driver/", application.url); | 178 EXPECT_EQ("exe://application_manager_apptest_driver/", application.url); |
172 EXPECT_NE(base::kNullProcessId, application.pid); | 179 EXPECT_NE(base::kNullProcessId, application.pid); |
173 } | 180 } |
174 { | 181 { |
175 auto& application = applications().back(); | 182 auto& application = applications().back(); |
| 183 // We learn about the target process id via a ping from it. |
| 184 EXPECT_EQ(target_id(), application.id); |
176 EXPECT_EQ("exe://application_manager_apptest_target/", application.url); | 185 EXPECT_EQ("exe://application_manager_apptest_target/", application.url); |
177 EXPECT_NE(base::kNullProcessId, application.pid); | 186 EXPECT_NE(base::kNullProcessId, application.pid); |
178 } | 187 } |
179 | 188 |
180 driver.set_connection_error_handler( | 189 driver.set_connection_error_handler( |
181 base::Bind(&ApplicationManagerAppTest::OnDriverQuit, | 190 base::Bind(&ApplicationManagerAppTest::OnDriverQuit, |
182 base::Unretained(this))); | 191 base::Unretained(this))); |
183 driver->QuitDriver(); | 192 driver->QuitDriver(); |
184 base::MessageLoop::current()->Run(); | 193 base::MessageLoop::current()->Run(); |
185 } | 194 } |
186 | 195 |
187 } // namespace shell | 196 } // namespace shell |
188 } // namespace mojo | 197 } // namespace mojo |
OLD | NEW |