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

Side by Side Diff: mojo/service_manager/service_manager_unittest.cc

Issue 380413003: Mojo: Use InterfaceFactory<Interface> for service registration (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix network_service_loader Created 6 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « mojo/service_manager/service_manager.cc ('k') | mojo/services/dbus_echo/dbus_echo_service.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/at_exit.h" 5 #include "base/at_exit.h"
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "mojo/public/cpp/application/application_connection.h" 8 #include "mojo/public/cpp/application/application_connection.h"
9 #include "mojo/public/cpp/application/application_delegate.h" 9 #include "mojo/public/cpp/application/application_delegate.h"
10 #include "mojo/public/cpp/application/application_impl.h" 10 #include "mojo/public/cpp/application/application_impl.h"
11 #include "mojo/public/cpp/application/interface_factory.h"
11 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" 12 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h"
12 #include "mojo/service_manager/service_loader.h" 13 #include "mojo/service_manager/service_loader.h"
13 #include "mojo/service_manager/service_manager.h" 14 #include "mojo/service_manager/service_manager.h"
14 #include "mojo/service_manager/test.mojom.h" 15 #include "mojo/service_manager/test.mojom.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 17
17 namespace mojo { 18 namespace mojo {
18 namespace { 19 namespace {
19 20
20 const char kTestURLString[] = "test:testService"; 21 const char kTestURLString[] = "test:testService";
(...skipping 16 matching lines...) Expand all
37 virtual void OnConnectionError() OVERRIDE { 38 virtual void OnConnectionError() OVERRIDE {
38 base::MessageLoop::current()->QuitWhenIdle(); 39 base::MessageLoop::current()->QuitWhenIdle();
39 } 40 }
40 41
41 private: 42 private:
42 DISALLOW_COPY_AND_ASSIGN(QuitMessageLoopErrorHandler); 43 DISALLOW_COPY_AND_ASSIGN(QuitMessageLoopErrorHandler);
43 }; 44 };
44 45
45 class TestServiceImpl : public InterfaceImpl<TestService> { 46 class TestServiceImpl : public InterfaceImpl<TestService> {
46 public: 47 public:
47 explicit TestServiceImpl(ApplicationConnection* connection, 48 explicit TestServiceImpl(TestContext* context) : context_(context) {
48 TestContext* context) : context_(context) {
49 ++context_->num_impls; 49 ++context_->num_impls;
50 } 50 }
51 51
52 virtual ~TestServiceImpl() { 52 virtual ~TestServiceImpl() {
53 --context_->num_impls; 53 --context_->num_impls;
54 } 54 }
55 55
56 virtual void OnConnectionError() OVERRIDE { 56 virtual void OnConnectionError() OVERRIDE {
57 if (!base::MessageLoop::current()->is_running()) 57 if (!base::MessageLoop::current()->is_running())
58 return; 58 return;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 service_->Test(test_string); 91 service_->Test(test_string);
92 } 92 }
93 93
94 private: 94 private:
95 TestServicePtr service_; 95 TestServicePtr service_;
96 bool quit_after_ack_; 96 bool quit_after_ack_;
97 DISALLOW_COPY_AND_ASSIGN(TestClientImpl); 97 DISALLOW_COPY_AND_ASSIGN(TestClientImpl);
98 }; 98 };
99 99
100 class TestServiceLoader : public ServiceLoader, 100 class TestServiceLoader : public ServiceLoader,
101 public ApplicationDelegate { 101 public ApplicationDelegate,
102 public InterfaceFactory<TestService> {
102 public: 103 public:
103 TestServiceLoader() 104 TestServiceLoader()
104 : context_(NULL), 105 : context_(NULL),
105 num_loads_(0) { 106 num_loads_(0) {
106 } 107 }
107 108
108 virtual ~TestServiceLoader() { 109 virtual ~TestServiceLoader() {
109 if (context_) 110 if (context_)
110 ++context_->num_loader_deletes; 111 ++context_->num_loader_deletes;
111 test_app_.reset(NULL); 112 test_app_.reset(NULL);
112 } 113 }
113 114
114 void set_context(TestContext* context) { context_ = context; } 115 void set_context(TestContext* context) { context_ = context; }
115 int num_loads() const { return num_loads_; } 116 int num_loads() const { return num_loads_; }
116 117
117 private: 118 private:
119 // ServiceLoader implementation.
118 virtual void LoadService( 120 virtual void LoadService(
119 ServiceManager* manager, 121 ServiceManager* manager,
120 const GURL& url, 122 const GURL& url,
121 ScopedMessagePipeHandle service_provider_handle) OVERRIDE { 123 ScopedMessagePipeHandle service_provider_handle) OVERRIDE {
122 ++num_loads_; 124 ++num_loads_;
123 test_app_.reset(new ApplicationImpl(this, service_provider_handle.Pass())); 125 test_app_.reset(new ApplicationImpl(this, service_provider_handle.Pass()));
124 } 126 }
125 127
126 virtual void OnServiceError(ServiceManager* manager, 128 virtual void OnServiceError(ServiceManager* manager,
127 const GURL& url) OVERRIDE { 129 const GURL& url) OVERRIDE {
128 } 130 }
129 131
132 // ApplicationDelegate implementation.
130 virtual bool ConfigureIncomingConnection( 133 virtual bool ConfigureIncomingConnection(
131 ApplicationConnection* connection) OVERRIDE { 134 ApplicationConnection* connection) OVERRIDE {
132 connection->AddService<TestServiceImpl>(context_); 135 connection->AddService(this);
133 return true; 136 return true;
134 } 137 }
135 138
139 // InterfaceFactory implementation.
140 virtual void Create(ApplicationConnection* connection,
141 InterfaceRequest<TestService> request) OVERRIDE {
142 BindToRequest(new TestServiceImpl(context_), &request);
143 }
144
136 scoped_ptr<ApplicationImpl> test_app_; 145 scoped_ptr<ApplicationImpl> test_app_;
137 TestContext* context_; 146 TestContext* context_;
138 int num_loads_; 147 int num_loads_;
139 DISALLOW_COPY_AND_ASSIGN(TestServiceLoader); 148 DISALLOW_COPY_AND_ASSIGN(TestServiceLoader);
140 }; 149 };
141 150
142 struct TesterContext { 151 struct TesterContext {
143 TesterContext() 152 TesterContext()
144 : num_b_calls(0), 153 : num_b_calls(0),
145 num_c_calls(0), 154 num_c_calls(0),
146 num_a_deletes(0), 155 num_a_deletes(0),
147 num_b_deletes(0), 156 num_b_deletes(0),
148 num_c_deletes(0), 157 num_c_deletes(0),
149 tester_called_quit(false), 158 tester_called_quit(false),
150 a_called_quit(false) {} 159 a_called_quit(false) {}
151 int num_b_calls; 160 int num_b_calls;
152 int num_c_calls; 161 int num_c_calls;
153 int num_a_deletes; 162 int num_a_deletes;
154 int num_b_deletes; 163 int num_b_deletes;
155 int num_c_deletes; 164 int num_c_deletes;
156 bool tester_called_quit; 165 bool tester_called_quit;
157 bool a_called_quit; 166 bool a_called_quit;
158 }; 167 };
159 168
160 // Used to test that the requestor url will be correctly passed. 169 // Used to test that the requestor url will be correctly passed.
161 class TestAImpl : public InterfaceImpl<TestA> { 170 class TestAImpl : public InterfaceImpl<TestA> {
162 public: 171 public:
163 explicit TestAImpl(ApplicationConnection* connection, 172 TestAImpl(ApplicationConnection* connection, TesterContext* test_context)
164 TesterContext* test_context)
165 : test_context_(test_context) { 173 : test_context_(test_context) {
166 connection->ConnectToApplication(kTestBURLString)->ConnectToService(&b_); 174 connection->ConnectToApplication(kTestBURLString)->ConnectToService(&b_);
167 } 175 }
168 virtual ~TestAImpl() { test_context_->num_a_deletes++; } 176 virtual ~TestAImpl() { test_context_->num_a_deletes++; }
169 177
170 private: 178 private:
171 virtual void CallB() OVERRIDE { 179 virtual void CallB() OVERRIDE {
172 b_->B(base::Bind(&TestAImpl::Quit, base::Unretained(this))); 180 b_->B(base::Bind(&TestAImpl::Quit, base::Unretained(this)));
173 } 181 }
174 182
175 virtual void CallCFromB() OVERRIDE { 183 virtual void CallCFromB() OVERRIDE {
176 b_->CallC(base::Bind(&TestAImpl::Quit, base::Unretained(this))); 184 b_->CallC(base::Bind(&TestAImpl::Quit, base::Unretained(this)));
177 } 185 }
178 186
179 void Quit() { 187 void Quit() {
180 test_context_->a_called_quit = true; 188 test_context_->a_called_quit = true;
181 base::MessageLoop::current()->Quit(); 189 base::MessageLoop::current()->Quit();
182 } 190 }
183 191
184 TesterContext* test_context_; 192 TesterContext* test_context_;
185 TestBPtr b_; 193 TestBPtr b_;
186 }; 194 };
187 195
188 class TestBImpl : public InterfaceImpl<TestB> { 196 class TestBImpl : public InterfaceImpl<TestB> {
189 public: 197 public:
190 explicit TestBImpl(ApplicationConnection* connection, 198 TestBImpl(ApplicationConnection* connection, TesterContext* test_context)
191 TesterContext* test_context)
192 : test_context_(test_context) { 199 : test_context_(test_context) {
193 connection->ConnectToService(&c_); 200 connection->ConnectToService(&c_);
194 } 201 }
195 202
196 virtual ~TestBImpl() { 203 virtual ~TestBImpl() {
197 test_context_->num_b_deletes++; 204 test_context_->num_b_deletes++;
198 if (!base::MessageLoop::current()->is_running()) 205 if (!base::MessageLoop::current()->is_running())
199 return; 206 return;
200 base::MessageLoop::current()->Quit(); 207 base::MessageLoop::current()->Quit();
201 } 208 }
202 209
203 private: 210 private:
204 virtual void B(const mojo::Callback<void()>& callback) OVERRIDE { 211 virtual void B(const mojo::Callback<void()>& callback) OVERRIDE {
205 ++test_context_->num_b_calls; 212 ++test_context_->num_b_calls;
206 callback.Run(); 213 callback.Run();
207 } 214 }
208 215
209 virtual void CallC(const mojo::Callback<void()>& callback) OVERRIDE { 216 virtual void CallC(const mojo::Callback<void()>& callback) OVERRIDE {
210 ++test_context_->num_b_calls; 217 ++test_context_->num_b_calls;
211 c_->C(callback); 218 c_->C(callback);
212 } 219 }
213 220
214 TesterContext* test_context_; 221 TesterContext* test_context_;
215 TestCPtr c_; 222 TestCPtr c_;
216 }; 223 };
217 224
218 class TestCImpl : public InterfaceImpl<TestC> { 225 class TestCImpl : public InterfaceImpl<TestC> {
219 public: 226 public:
220 explicit TestCImpl(ApplicationConnection* connection, 227 TestCImpl(ApplicationConnection* connection, TesterContext* test_context)
221 TesterContext* test_context) 228 : test_context_(test_context) {}
222 : test_context_(test_context) {
223 }
224 229
225 virtual ~TestCImpl() { test_context_->num_c_deletes++; } 230 virtual ~TestCImpl() { test_context_->num_c_deletes++; }
226 231
227 private: 232 private:
228 virtual void C(const mojo::Callback<void()>& callback) OVERRIDE { 233 virtual void C(const mojo::Callback<void()>& callback) OVERRIDE {
229 ++test_context_->num_c_calls; 234 ++test_context_->num_c_calls;
230 callback.Run(); 235 callback.Run();
231 } 236 }
232 TesterContext* test_context_; 237 TesterContext* test_context_;
233 }; 238 };
234 239
235 class Tester : public ApplicationDelegate, public ServiceLoader { 240 class Tester : public ApplicationDelegate,
241 public ServiceLoader,
242 public InterfaceFactory<TestA>,
243 public InterfaceFactory<TestB>,
244 public InterfaceFactory<TestC> {
236 public: 245 public:
237 Tester(TesterContext* context, const std::string& requestor_url) 246 Tester(TesterContext* context, const std::string& requestor_url)
238 : context_(context), 247 : context_(context),
239 requestor_url_(requestor_url) {} 248 requestor_url_(requestor_url) {}
240 virtual ~Tester() {} 249 virtual ~Tester() {}
241 250
242 private: 251 private:
243 virtual void LoadService( 252 virtual void LoadService(
244 ServiceManager* manager, 253 ServiceManager* manager,
245 const GURL& url, 254 const GURL& url,
246 ScopedMessagePipeHandle shell_handle) OVERRIDE { 255 ScopedMessagePipeHandle shell_handle) OVERRIDE {
247 app_.reset(new ApplicationImpl(this, shell_handle.Pass())); 256 app_.reset(new ApplicationImpl(this, shell_handle.Pass()));
248 } 257 }
249 258
250 virtual void OnServiceError(ServiceManager* manager, 259 virtual void OnServiceError(ServiceManager* manager,
251 const GURL& url) OVERRIDE {} 260 const GURL& url) OVERRIDE {}
252 261
253 virtual bool ConfigureIncomingConnection( 262 virtual bool ConfigureIncomingConnection(
254 ApplicationConnection* connection) OVERRIDE { 263 ApplicationConnection* connection) OVERRIDE {
255 if (!requestor_url_.empty() && 264 if (!requestor_url_.empty() &&
256 requestor_url_ != connection->GetRemoteApplicationURL()) { 265 requestor_url_ != connection->GetRemoteApplicationURL()) {
257 context_->tester_called_quit = true; 266 context_->tester_called_quit = true;
258 base::MessageLoop::current()->Quit(); 267 base::MessageLoop::current()->Quit();
259 return false; 268 return false;
260 } 269 }
261 // If we're coming from A, then add B, otherwise A. 270 // If we're coming from A, then add B, otherwise A.
262 if (connection->GetRemoteApplicationURL() == kTestAURLString) 271 if (connection->GetRemoteApplicationURL() == kTestAURLString)
263 connection->AddService<TestBImpl>(context_); 272 connection->AddService<TestB>(this);
264 else 273 else
265 connection->AddService<TestAImpl>(context_); 274 connection->AddService<TestA>(this);
266 return true; 275 return true;
267 } 276 }
268 277
269 virtual bool ConfigureOutgoingConnection( 278 virtual bool ConfigureOutgoingConnection(
270 ApplicationConnection* connection) OVERRIDE { 279 ApplicationConnection* connection) OVERRIDE {
271 // If we're connecting to B, then add C. 280 // If we're connecting to B, then add C.
272 if (connection->GetRemoteApplicationURL() == kTestBURLString) 281 if (connection->GetRemoteApplicationURL() == kTestBURLString)
273 connection->AddService<TestCImpl>(context_); 282 connection->AddService<TestC>(this);
274 return true; 283 return true;
275 } 284 }
276 285
286 virtual void Create(ApplicationConnection* connection,
287 InterfaceRequest<TestA> request) OVERRIDE {
288 BindToRequest(new TestAImpl(connection, context_), &request);
289 }
290
291 virtual void Create(ApplicationConnection* connection,
292 InterfaceRequest<TestB> request) OVERRIDE {
293 BindToRequest(new TestBImpl(connection, context_), &request);
294 }
295
296 virtual void Create(ApplicationConnection* connection,
297 InterfaceRequest<TestC> request) OVERRIDE {
298 BindToRequest(new TestCImpl(connection, context_), &request);
299 }
300
277 TesterContext* context_; 301 TesterContext* context_;
278 scoped_ptr<ApplicationImpl> app_; 302 scoped_ptr<ApplicationImpl> app_;
279 std::string requestor_url_; 303 std::string requestor_url_;
280 }; 304 };
281 305
282 class TestServiceInterceptor : public ServiceManager::Interceptor { 306 class TestServiceInterceptor : public ServiceManager::Interceptor {
283 public: 307 public:
284 TestServiceInterceptor() : call_count_(0) {} 308 TestServiceInterceptor() : call_count_(0) {}
285 309
286 virtual ServiceProviderPtr OnConnectToClient( 310 virtual ServiceProviderPtr OnConnectToClient(
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 435
412 // http::test1 should go to default loader. 436 // http::test1 should go to default loader.
413 sm.ConnectToService(GURL("http:test1"), &test_service); 437 sm.ConnectToService(GURL("http:test1"), &test_service);
414 EXPECT_EQ(1, url_loader->num_loads()); 438 EXPECT_EQ(1, url_loader->num_loads());
415 EXPECT_EQ(1, scheme_loader->num_loads()); 439 EXPECT_EQ(1, scheme_loader->num_loads());
416 EXPECT_EQ(1, default_loader->num_loads()); 440 EXPECT_EQ(1, default_loader->num_loads());
417 } 441 }
418 442
419 // Confirm that the url of a service is correctly passed to another service that 443 // Confirm that the url of a service is correctly passed to another service that
420 // it loads. 444 // it loads.
421 TEST_F(ServiceManagerTest, ACallB) { 445 // http://crbug.com/396300
446 TEST_F(ServiceManagerTest, DISABLED_ACallB) {
422 TesterContext context; 447 TesterContext context;
423 ServiceManager sm; 448 ServiceManager sm;
424 449
425 // Any url can load a. 450 // Any url can load a.
426 sm.SetLoaderForURL( 451 sm.SetLoaderForURL(
427 scoped_ptr<ServiceLoader>(new Tester(&context, std::string())), 452 scoped_ptr<ServiceLoader>(new Tester(&context, std::string())),
428 GURL(kTestAURLString)); 453 GURL(kTestAURLString));
429 454
430 // Only a can load b. 455 // Only a can load b.
431 sm.SetLoaderForURL( 456 sm.SetLoaderForURL(
432 scoped_ptr<ServiceLoader>( 457 scoped_ptr<ServiceLoader>(
433 new Tester(&context, kTestAURLString)), 458 new Tester(&context, kTestAURLString)),
434 GURL(kTestBURLString)); 459 GURL(kTestBURLString));
435 460
436 TestAPtr a; 461 TestAPtr a;
437 sm.ConnectToService(GURL(kTestAURLString), &a); 462 sm.ConnectToService(GURL(kTestAURLString), &a);
438 a->CallB(); 463 a->CallB();
439 loop_.Run(); 464 loop_.Run();
440 EXPECT_EQ(1, context.num_b_calls); 465 EXPECT_EQ(1, context.num_b_calls);
441 EXPECT_TRUE(context.a_called_quit); 466 EXPECT_TRUE(context.a_called_quit);
442 } 467 }
443 468
444 // A calls B which calls C. 469 // A calls B which calls C.
445 TEST_F(ServiceManagerTest, BCallC) { 470 // http://crbug.com/396300
471 TEST_F(ServiceManagerTest, DISABLED_BCallC) {
446 TesterContext context; 472 TesterContext context;
447 ServiceManager sm; 473 ServiceManager sm;
448 474
449 // Any url can load a. 475 // Any url can load a.
450 sm.SetLoaderForURL( 476 sm.SetLoaderForURL(
451 scoped_ptr<ServiceLoader>(new Tester(&context, std::string())), 477 scoped_ptr<ServiceLoader>(new Tester(&context, std::string())),
452 GURL(kTestAURLString)); 478 GURL(kTestAURLString));
453 479
454 // Only a can load b. 480 // Only a can load b.
455 sm.SetLoaderForURL( 481 sm.SetLoaderForURL(
456 scoped_ptr<ServiceLoader>( 482 scoped_ptr<ServiceLoader>(
457 new Tester(&context, kTestAURLString)), 483 new Tester(&context, kTestAURLString)),
458 GURL(kTestBURLString)); 484 GURL(kTestBURLString));
459 485
460 TestAPtr a; 486 TestAPtr a;
461 sm.ConnectToService(GURL(kTestAURLString), &a); 487 sm.ConnectToService(GURL(kTestAURLString), &a);
462 a->CallCFromB(); 488 a->CallCFromB();
463 loop_.Run(); 489 loop_.Run();
464 490
465 EXPECT_EQ(1, context.num_b_calls); 491 EXPECT_EQ(1, context.num_b_calls);
466 EXPECT_EQ(1, context.num_c_calls); 492 EXPECT_EQ(1, context.num_c_calls);
467 EXPECT_TRUE(context.a_called_quit); 493 EXPECT_TRUE(context.a_called_quit);
468 } 494 }
469 495
470 // Confirm that a service impl will be deleted if the app that connected to 496 // Confirm that a service impl will be deleted if the app that connected to
471 // it goes away. 497 // it goes away.
472 TEST_F(ServiceManagerTest, BDeleted) { 498 // http://crbug.com/396300
499 TEST_F(ServiceManagerTest, DISABLED_BDeleted) {
473 TesterContext context; 500 TesterContext context;
474 ServiceManager sm; 501 ServiceManager sm;
475 502
476 sm.SetLoaderForURL( 503 sm.SetLoaderForURL(
477 scoped_ptr<ServiceLoader>(new Tester(&context, std::string())), 504 scoped_ptr<ServiceLoader>(new Tester(&context, std::string())),
478 GURL(kTestAURLString)); 505 GURL(kTestAURLString));
479 506
480 sm.SetLoaderForURL( 507 sm.SetLoaderForURL(
481 scoped_ptr<ServiceLoader>( new Tester(&context, std::string())), 508 scoped_ptr<ServiceLoader>( new Tester(&context, std::string())),
482 GURL(kTestBURLString)); 509 GURL(kTestBURLString));
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 std::string url("test:test3"); 573 std::string url("test:test3");
547 TestServicePtr test_service; 574 TestServicePtr test_service;
548 sm.ConnectToService(GURL(url), &test_service); 575 sm.ConnectToService(GURL(url), &test_service);
549 576
550 EXPECT_EQ(1, interceptor.call_count()); 577 EXPECT_EQ(1, interceptor.call_count());
551 EXPECT_EQ(url, interceptor.url_spec()); 578 EXPECT_EQ(url, interceptor.url_spec());
552 EXPECT_EQ(1, default_loader->num_loads()); 579 EXPECT_EQ(1, default_loader->num_loads());
553 } 580 }
554 581
555 } // namespace mojo 582 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/service_manager/service_manager.cc ('k') | mojo/services/dbus_echo/dbus_echo_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698