OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <stddef.h> | |
6 #include <stdint.h> | |
7 | |
8 #include <memory> | |
9 #include <utility> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/message_loop/message_loop.h" | |
15 #include "base/process/process_handle.h" | |
16 #include "base/run_loop.h" | |
17 #include "mojo/public/cpp/bindings/binding_set.h" | |
18 #include "services/shell/public/cpp/interface_factory.h" | |
19 #include "services/shell/public/cpp/service.h" | |
20 #include "services/shell/public/cpp/service_test.h" | |
21 #include "services/shell/public/interfaces/service_manager.mojom.h" | |
22 #include "services/shell/tests/shell/shell_unittest.mojom.h" | |
23 | |
24 namespace shell { | |
25 | |
26 namespace { | |
27 | |
28 class ShellTestClient | |
29 : public test::ServiceTestClient, | |
30 public InterfaceFactory<test::mojom::CreateInstanceTest>, | |
31 public test::mojom::CreateInstanceTest { | |
32 public: | |
33 explicit ShellTestClient(test::ServiceTest* test) | |
34 : test::ServiceTestClient(test), | |
35 binding_(this) {} | |
36 ~ShellTestClient() override {} | |
37 | |
38 const Identity& target_identity() const { return target_identity_; } | |
39 | |
40 private: | |
41 // test::ServiceTestClient: | |
42 bool OnConnect(const Identity& remote_identity, | |
43 InterfaceRegistry* registry) override { | |
44 registry->AddInterface<test::mojom::CreateInstanceTest>(this); | |
45 return true; | |
46 } | |
47 | |
48 // InterfaceFactory<test::mojom::CreateInstanceTest>: | |
49 void Create( | |
50 const Identity& remote_identity, | |
51 test::mojom::CreateInstanceTestRequest request) override { | |
52 binding_.Bind(std::move(request)); | |
53 } | |
54 | |
55 // test::mojom::CreateInstanceTest: | |
56 void SetTargetIdentity(const shell::Identity& identity) override { | |
57 target_identity_ = identity; | |
58 base::MessageLoop::current()->QuitWhenIdle(); | |
59 } | |
60 | |
61 shell::Identity target_identity_; | |
62 | |
63 mojo::Binding<test::mojom::CreateInstanceTest> binding_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(ShellTestClient); | |
66 }; | |
67 | |
68 } // namespace | |
69 | |
70 class ShellTest : public test::ServiceTest, | |
71 public mojom::ServiceManagerListener { | |
72 public: | |
73 ShellTest() | |
74 : test::ServiceTest("service:shell_unittest"), | |
75 service_(nullptr), | |
76 binding_(this) {} | |
77 ~ShellTest() override {} | |
78 | |
79 void OnDriverQuit() { | |
80 base::MessageLoop::current()->QuitNow(); | |
81 } | |
82 | |
83 protected: | |
84 struct InstanceInfo { | |
85 explicit InstanceInfo(const Identity& identity) | |
86 : identity(identity), pid(base::kNullProcessId) {} | |
87 | |
88 Identity identity; | |
89 base::ProcessId pid; | |
90 }; | |
91 | |
92 void AddListenerAndWaitForApplications() { | |
93 mojom::ServiceManagerPtr service_manager; | |
94 connector()->ConnectToInterface("service:shell", &service_manager); | |
95 | |
96 service_manager->AddListener(binding_.CreateInterfacePtrAndBind()); | |
97 | |
98 wait_for_instances_loop_.reset(new base::RunLoop); | |
99 wait_for_instances_loop_->Run(); | |
100 } | |
101 | |
102 bool ContainsInstanceWithName(const std::string& name) const { | |
103 for (const auto& instance : initial_instances_) { | |
104 if (instance.identity.name() == name) | |
105 return true; | |
106 } | |
107 for (const auto& instance : instances_) { | |
108 if (instance.identity.name() == name) | |
109 return true; | |
110 } | |
111 return false; | |
112 } | |
113 | |
114 const Identity& target_identity() const { | |
115 DCHECK(service_); | |
116 return service_->target_identity(); | |
117 } | |
118 | |
119 const std::vector<InstanceInfo>& instances() const { | |
120 return instances_; | |
121 } | |
122 | |
123 private: | |
124 // test::ServiceTest: | |
125 std::unique_ptr<Service> CreateService() override { | |
126 service_ = new ShellTestClient(this); | |
127 return base::WrapUnique(service_); | |
128 } | |
129 | |
130 // mojom::ServiceManagerListener: | |
131 void OnInit(std::vector<mojom::ServiceInfoPtr> instances) override { | |
132 for (size_t i = 0; i < instances.size(); ++i) | |
133 initial_instances_.push_back(InstanceInfo(instances[i]->identity)); | |
134 | |
135 DCHECK(wait_for_instances_loop_); | |
136 wait_for_instances_loop_->Quit(); | |
137 } | |
138 void OnServiceCreated(mojom::ServiceInfoPtr instance) override { | |
139 instances_.push_back(InstanceInfo(instance->identity)); | |
140 } | |
141 void OnServiceStarted(const shell::Identity& identity, | |
142 uint32_t pid) override { | |
143 for (auto& instance : instances_) { | |
144 if (instance.identity == identity) { | |
145 instance.pid = pid; | |
146 break; | |
147 } | |
148 } | |
149 } | |
150 void OnServiceStopped(const shell::Identity& identity) override { | |
151 for (auto it = instances_.begin(); it != instances_.end(); ++it) { | |
152 auto& instance = *it; | |
153 if (instance.identity == identity) { | |
154 instances_.erase(it); | |
155 break; | |
156 } | |
157 } | |
158 } | |
159 | |
160 ShellTestClient* service_; | |
161 mojo::Binding<mojom::ServiceManagerListener> binding_; | |
162 std::vector<InstanceInfo> instances_; | |
163 std::vector<InstanceInfo> initial_instances_; | |
164 std::unique_ptr<base::RunLoop> wait_for_instances_loop_; | |
165 | |
166 DISALLOW_COPY_AND_ASSIGN(ShellTest); | |
167 }; | |
168 | |
169 TEST_F(ShellTest, CreateInstance) { | |
170 AddListenerAndWaitForApplications(); | |
171 | |
172 // 1. Launch a process. (Actually, have the runner launch a process that | |
173 // launches a process.) | |
174 test::mojom::DriverPtr driver; | |
175 std::unique_ptr<Connection> connection = | |
176 connector()->Connect("exe:shell_unittest_driver"); | |
177 connection->GetInterface(&driver); | |
178 | |
179 // 2. Wait for the target to connect to us. (via | |
180 // mojo:shell_unittest) | |
181 base::RunLoop().Run(); | |
182 | |
183 EXPECT_FALSE(connection->IsPending()); | |
184 Identity remote_identity = connection->GetRemoteIdentity(); | |
185 | |
186 // 3. Validate that this test suite's name was received from the application | |
187 // manager. | |
188 EXPECT_TRUE(ContainsInstanceWithName("service:shell_unittest")); | |
189 | |
190 // 4. Validate that the right applications/processes were created. | |
191 // Note that the target process will be created even if the tests are | |
192 // run with --single-process. | |
193 EXPECT_EQ(2u, instances().size()); | |
194 { | |
195 auto& instance = instances().front(); | |
196 EXPECT_EQ(remote_identity, instance.identity); | |
197 EXPECT_EQ("exe:shell_unittest_driver", instance.identity.name()); | |
198 EXPECT_NE(base::kNullProcessId, instance.pid); | |
199 } | |
200 { | |
201 auto& instance = instances().back(); | |
202 // We learn about the target process id via a ping from it. | |
203 EXPECT_EQ(target_identity(), instance.identity); | |
204 EXPECT_EQ("exe:shell_unittest_target", instance.identity.name()); | |
205 EXPECT_NE(base::kNullProcessId, instance.pid); | |
206 } | |
207 | |
208 driver.set_connection_error_handler( | |
209 base::Bind(&ShellTest::OnDriverQuit, | |
210 base::Unretained(this))); | |
211 driver->QuitDriver(); | |
212 base::RunLoop().Run(); | |
213 } | |
214 | |
215 } // namespace shell | |
OLD | NEW |