Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(694)

Side by Side Diff: services/service_manager/tests/service_manager/service_manager_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698