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

Side by Side Diff: services/shell/tests/loader_unittest.cc

Issue 1882423004: Move shell service to toplevel shell namespace (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 8 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 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 <memory>
5 #include <utility> 6 #include <utility>
6 7
7 #include "base/at_exit.h" 8 #include "base/at_exit.h"
8 #include "base/bind.h" 9 #include "base/bind.h"
9 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/memory/ptr_util.h"
10 #include "base/memory/scoped_vector.h" 12 #include "base/memory/scoped_vector.h"
11 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h" 14 #include "base/run_loop.h"
13 #include "mojo/public/cpp/bindings/strong_binding.h" 15 #include "mojo/public/cpp/bindings/strong_binding.h"
14 #include "services/catalog/factory.h" 16 #include "services/catalog/factory.h"
15 #include "services/catalog/store.h" 17 #include "services/catalog/store.h"
16 #include "services/shell/connect_util.h" 18 #include "services/shell/connect_util.h"
17 #include "services/shell/loader.h" 19 #include "services/shell/loader.h"
18 #include "services/shell/public/cpp/connector.h" 20 #include "services/shell/public/cpp/connector.h"
19 #include "services/shell/public/cpp/interface_factory.h" 21 #include "services/shell/public/cpp/interface_factory.h"
20 #include "services/shell/public/cpp/shell_client.h" 22 #include "services/shell/public/cpp/shell_client.h"
21 #include "services/shell/public/cpp/shell_connection.h" 23 #include "services/shell/public/cpp/shell_connection.h"
22 #include "services/shell/public/interfaces/interface_provider.mojom.h" 24 #include "services/shell/public/interfaces/interface_provider.mojom.h"
23 #include "services/shell/shell.h" 25 #include "services/shell/shell.h"
24 #include "services/shell/tests/test.mojom.h" 26 #include "services/shell/tests/test.mojom.h"
25 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
26 28
27 namespace mojo {
28 namespace shell { 29 namespace shell {
29 namespace test { 30 namespace test {
30 31
31 const char kTestURLString[] = "test:testService"; 32 const char kTestURLString[] = "test:testService";
32 const char kTestAURLString[] = "test:TestA"; 33 const char kTestAURLString[] = "test:TestA";
33 const char kTestBURLString[] = "test:TestB"; 34 const char kTestBURLString[] = "test:TestB";
34 35
35 struct TestContext { 36 struct TestContext {
36 TestContext() : num_impls(0), num_loader_deletes(0) {} 37 TestContext() : num_impls(0), num_loader_deletes(0) {}
37 std::string last_test_string; 38 std::string last_test_string;
38 int num_impls; 39 int num_impls;
39 int num_loader_deletes; 40 int num_loader_deletes;
40 }; 41 };
41 42
42 void QuitClosure(const Identity& expected, 43 void QuitClosure(const Identity& expected,
43 bool* value, 44 bool* value,
44 const Identity& actual) { 45 const Identity& actual) {
45 if (expected == actual) { 46 if (expected == actual) {
46 *value = true; 47 *value = true;
47 base::MessageLoop::current()->QuitWhenIdle(); 48 base::MessageLoop::current()->QuitWhenIdle();
48 } 49 }
49 } 50 }
50 51
51 class TestServiceImpl : public TestService { 52 class TestServiceImpl : public TestService {
52 public: 53 public:
53 TestServiceImpl(TestContext* context, InterfaceRequest<TestService> request) 54 TestServiceImpl(TestContext* context, TestServiceRequest request)
54 : context_(context), binding_(this, std::move(request)) { 55 : context_(context), binding_(this, std::move(request)) {
55 ++context_->num_impls; 56 ++context_->num_impls;
56 } 57 }
57 58
58 ~TestServiceImpl() override { 59 ~TestServiceImpl() override {
59 --context_->num_impls; 60 --context_->num_impls;
60 if (!base::MessageLoop::current()->is_running()) 61 if (!base::MessageLoop::current()->is_running())
61 return; 62 return;
62 base::MessageLoop::current()->QuitWhenIdle(); 63 base::MessageLoop::current()->QuitWhenIdle();
63 } 64 }
64 65
65 // TestService implementation: 66 // TestService implementation:
66 void Test(const String& test_string, 67 void Test(const mojo::String& test_string,
67 const Callback<void()>& callback) override { 68 const mojo::Closure& callback) override {
68 context_->last_test_string = test_string; 69 context_->last_test_string = test_string;
69 callback.Run(); 70 callback.Run();
70 } 71 }
71 72
72 private: 73 private:
73 TestContext* context_; 74 TestContext* context_;
74 StrongBinding<TestService> binding_; 75 mojo::StrongBinding<TestService> binding_;
75 }; 76 };
76 77
77 class TestClient { 78 class TestClient {
78 public: 79 public:
79 explicit TestClient(TestServicePtr service) 80 explicit TestClient(TestServicePtr service)
80 : service_(std::move(service)), quit_after_ack_(false) {} 81 : service_(std::move(service)), quit_after_ack_(false) {}
81 82
82 void AckTest() { 83 void AckTest() {
83 if (quit_after_ack_) 84 if (quit_after_ack_)
84 base::MessageLoop::current()->QuitWhenIdle(); 85 base::MessageLoop::current()->QuitWhenIdle();
(...skipping 29 matching lines...) Expand all
114 } 115 }
115 116
116 private: 117 private:
117 // Loader implementation. 118 // Loader implementation.
118 void Load(const std::string& name, 119 void Load(const std::string& name,
119 mojom::ShellClientRequest request) override { 120 mojom::ShellClientRequest request) override {
120 ++num_loads_; 121 ++num_loads_;
121 shell_connection_.reset(new ShellConnection(this, std::move(request))); 122 shell_connection_.reset(new ShellConnection(this, std::move(request)));
122 } 123 }
123 124
124 // mojo::ShellClient implementation. 125 // ShellClient implementation.
125 bool AcceptConnection(Connection* connection) override { 126 bool AcceptConnection(Connection* connection) override {
126 connection->AddInterface<TestService>(this); 127 connection->AddInterface<TestService>(this);
127 last_requestor_name_ = connection->GetRemoteIdentity().name(); 128 last_requestor_name_ = connection->GetRemoteIdentity().name();
128 return true; 129 return true;
129 } 130 }
130 131
131 // InterfaceFactory<TestService> implementation. 132 // InterfaceFactory<TestService> implementation.
132 void Create(Connection* connection, 133 void Create(Connection* connection, TestServiceRequest request) override {
133 InterfaceRequest<TestService> request) override {
134 new TestServiceImpl(context_, std::move(request)); 134 new TestServiceImpl(context_, std::move(request));
135 } 135 }
136 136
137 scoped_ptr<ShellConnection> shell_connection_; 137 std::unique_ptr<ShellConnection> shell_connection_;
138 TestContext* context_; 138 TestContext* context_;
139 int num_loads_; 139 int num_loads_;
140 std::string last_requestor_name_; 140 std::string last_requestor_name_;
141 141
142 DISALLOW_COPY_AND_ASSIGN(TestLoader); 142 DISALLOW_COPY_AND_ASSIGN(TestLoader);
143 }; 143 };
144 144
145 class ClosingLoader : public Loader { 145 class ClosingLoader : public Loader {
146 private: 146 private:
147 // Loader implementation. 147 // Loader implementation.
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 bool a_called_quit_; 243 bool a_called_quit_;
244 244
245 base::MessageLoop* loop_; 245 base::MessageLoop* loop_;
246 }; 246 };
247 247
248 // Used to test that the requestor name will be correctly passed. 248 // Used to test that the requestor name will be correctly passed.
249 class TestAImpl : public TestA { 249 class TestAImpl : public TestA {
250 public: 250 public:
251 TestAImpl(Connector* connector, 251 TestAImpl(Connector* connector,
252 TesterContext* test_context, 252 TesterContext* test_context,
253 InterfaceRequest<TestA> request, 253 TestARequest request,
254 InterfaceFactory<TestC>* factory) 254 InterfaceFactory<TestC>* factory)
255 : test_context_(test_context), binding_(this, std::move(request)) { 255 : test_context_(test_context), binding_(this, std::move(request)) {
256 connection_ = connector->Connect(kTestBURLString); 256 connection_ = connector->Connect(kTestBURLString);
257 connection_->AddInterface<TestC>(factory); 257 connection_->AddInterface<TestC>(factory);
258 connection_->GetInterface(&b_); 258 connection_->GetInterface(&b_);
259 } 259 }
260 260
261 ~TestAImpl() override { 261 ~TestAImpl() override {
262 test_context_->IncrementNumADeletes(); 262 test_context_->IncrementNumADeletes();
263 if (base::MessageLoop::current()->is_running()) 263 if (base::MessageLoop::current()->is_running())
264 Quit(); 264 Quit();
265 } 265 }
266 266
267 private: 267 private:
268 void CallB() override { 268 void CallB() override {
269 b_->B(base::Bind(&TestAImpl::Quit, base::Unretained(this))); 269 b_->B(base::Bind(&TestAImpl::Quit, base::Unretained(this)));
270 } 270 }
271 271
272 void CallCFromB() override { 272 void CallCFromB() override {
273 b_->CallC(base::Bind(&TestAImpl::Quit, base::Unretained(this))); 273 b_->CallC(base::Bind(&TestAImpl::Quit, base::Unretained(this)));
274 } 274 }
275 275
276 void Quit() { 276 void Quit() {
277 base::MessageLoop::current()->QuitWhenIdle(); 277 base::MessageLoop::current()->QuitWhenIdle();
278 test_context_->set_a_called_quit(); 278 test_context_->set_a_called_quit();
279 test_context_->QuitSoon(); 279 test_context_->QuitSoon();
280 } 280 }
281 281
282 scoped_ptr<Connection> connection_; 282 std::unique_ptr<Connection> connection_;
283 TesterContext* test_context_; 283 TesterContext* test_context_;
284 TestBPtr b_; 284 TestBPtr b_;
285 StrongBinding<TestA> binding_; 285 mojo::StrongBinding<TestA> binding_;
286 }; 286 };
287 287
288 class TestBImpl : public TestB { 288 class TestBImpl : public TestB {
289 public: 289 public:
290 TestBImpl(Connection* connection, 290 TestBImpl(Connection* connection,
291 TesterContext* test_context, 291 TesterContext* test_context,
292 InterfaceRequest<TestB> request) 292 TestBRequest request)
293 : test_context_(test_context), binding_(this, std::move(request)) { 293 : test_context_(test_context), binding_(this, std::move(request)) {
294 connection->GetInterface(&c_); 294 connection->GetInterface(&c_);
295 } 295 }
296 296
297 ~TestBImpl() override { 297 ~TestBImpl() override {
298 test_context_->IncrementNumBDeletes(); 298 test_context_->IncrementNumBDeletes();
299 if (base::MessageLoop::current()->is_running()) 299 if (base::MessageLoop::current()->is_running())
300 base::MessageLoop::current()->QuitWhenIdle(); 300 base::MessageLoop::current()->QuitWhenIdle();
301 test_context_->QuitSoon(); 301 test_context_->QuitSoon();
302 } 302 }
303 303
304 private: 304 private:
305 void B(const Callback<void()>& callback) override { 305 void B(const mojo::Closure& callback) override {
306 test_context_->IncrementNumBCalls(); 306 test_context_->IncrementNumBCalls();
307 callback.Run(); 307 callback.Run();
308 } 308 }
309 309
310 void CallC(const Callback<void()>& callback) override { 310 void CallC(const mojo::Closure& callback) override {
311 test_context_->IncrementNumBCalls(); 311 test_context_->IncrementNumBCalls();
312 c_->C(callback); 312 c_->C(callback);
313 } 313 }
314 314
315 TesterContext* test_context_; 315 TesterContext* test_context_;
316 TestCPtr c_; 316 TestCPtr c_;
317 StrongBinding<TestB> binding_; 317 mojo::StrongBinding<TestB> binding_;
318 }; 318 };
319 319
320 class TestCImpl : public TestC { 320 class TestCImpl : public TestC {
321 public: 321 public:
322 TestCImpl(Connection* connection, 322 TestCImpl(Connection* connection,
323 TesterContext* test_context, 323 TesterContext* test_context,
324 InterfaceRequest<TestC> request) 324 TestCRequest request)
325 : test_context_(test_context), binding_(this, std::move(request)) {} 325 : test_context_(test_context), binding_(this, std::move(request)) {}
326 326
327 ~TestCImpl() override { test_context_->IncrementNumCDeletes(); } 327 ~TestCImpl() override { test_context_->IncrementNumCDeletes(); }
328 328
329 private: 329 private:
330 void C(const Callback<void()>& callback) override { 330 void C(const mojo::Closure& callback) override {
331 test_context_->IncrementNumCCalls(); 331 test_context_->IncrementNumCCalls();
332 callback.Run(); 332 callback.Run();
333 } 333 }
334 334
335 TesterContext* test_context_; 335 TesterContext* test_context_;
336 StrongBinding<TestC> binding_; 336 mojo::StrongBinding<TestC> binding_;
337 }; 337 };
338 338
339 class Tester : public ShellClient, 339 class Tester : public ShellClient,
340 public Loader, 340 public Loader,
341 public InterfaceFactory<TestA>, 341 public InterfaceFactory<TestA>,
342 public InterfaceFactory<TestB>, 342 public InterfaceFactory<TestB>,
343 public InterfaceFactory<TestC> { 343 public InterfaceFactory<TestC> {
344 public: 344 public:
345 Tester(TesterContext* context, const std::string& requestor_name) 345 Tester(TesterContext* context, const std::string& requestor_name)
346 : context_(context), requestor_name_(requestor_name) {} 346 : context_(context), requestor_name_(requestor_name) {}
347 ~Tester() override {} 347 ~Tester() override {}
348 348
349 private: 349 private:
350 void Load(const std::string& name, 350 void Load(const std::string& name,
351 InterfaceRequest<mojom::ShellClient> request) override { 351 mojom::ShellClientRequest request) override {
352 app_.reset(new ShellConnection(this, std::move(request))); 352 app_.reset(new ShellConnection(this, std::move(request)));
353 } 353 }
354 354
355 bool AcceptConnection(Connection* connection) override { 355 bool AcceptConnection(Connection* connection) override {
356 if (!requestor_name_.empty() && 356 if (!requestor_name_.empty() &&
357 requestor_name_ != connection->GetRemoteIdentity().name()) { 357 requestor_name_ != connection->GetRemoteIdentity().name()) {
358 context_->set_tester_called_quit(); 358 context_->set_tester_called_quit();
359 context_->QuitSoon(); 359 context_->QuitSoon();
360 base::MessageLoop::current()->QuitWhenIdle(); 360 base::MessageLoop::current()->QuitWhenIdle();
361 return false; 361 return false;
362 } 362 }
363 // If we're coming from A, then add B, otherwise A. 363 // If we're coming from A, then add B, otherwise A.
364 if (connection->GetRemoteIdentity().name() == kTestAURLString) 364 if (connection->GetRemoteIdentity().name() == kTestAURLString)
365 connection->AddInterface<TestB>(this); 365 connection->AddInterface<TestB>(this);
366 else 366 else
367 connection->AddInterface<TestA>(this); 367 connection->AddInterface<TestA>(this);
368 return true; 368 return true;
369 } 369 }
370 370
371 void Create(Connection* connection, 371 void Create(Connection* connection, TestARequest request) override {
372 InterfaceRequest<TestA> request) override {
373 a_bindings_.push_back( 372 a_bindings_.push_back(
374 new TestAImpl(app_->connector(), context_, std::move(request), this)); 373 new TestAImpl(app_->connector(), context_, std::move(request), this));
375 } 374 }
376 375
377 void Create(Connection* connection, 376 void Create(Connection* connection, TestBRequest request) override {
378 InterfaceRequest<TestB> request) override {
379 new TestBImpl(connection, context_, std::move(request)); 377 new TestBImpl(connection, context_, std::move(request));
380 } 378 }
381 379
382 void Create(Connection* connection, 380 void Create(Connection* connection, TestCRequest request) override {
383 InterfaceRequest<TestC> request) override {
384 new TestCImpl(connection, context_, std::move(request)); 381 new TestCImpl(connection, context_, std::move(request));
385 } 382 }
386 383
387 TesterContext* context_; 384 TesterContext* context_;
388 scoped_ptr<ShellConnection> app_; 385 std::unique_ptr<ShellConnection> app_;
389 std::string requestor_name_; 386 std::string requestor_name_;
390 ScopedVector<TestAImpl> a_bindings_; 387 ScopedVector<TestAImpl> a_bindings_;
391 }; 388 };
392 389
393 void OnConnect(base::RunLoop* loop, mojom::ConnectResult result, 390 void OnConnect(base::RunLoop* loop,
394 const String& user_id, uint32_t instance_id) { 391 mojom::ConnectResult result,
392 const mojo::String& user_id,
393 uint32_t instance_id) {
395 loop->Quit(); 394 loop->Quit();
396 } 395 }
397 396
398 class LoaderTest : public testing::Test { 397 class LoaderTest : public testing::Test {
399 public: 398 public:
400 LoaderTest() : tester_context_(&loop_) {} 399 LoaderTest() : tester_context_(&loop_) {}
401 ~LoaderTest() override {} 400 ~LoaderTest() override {}
402 401
403 void SetUp() override { 402 void SetUp() override {
404 blocking_pool_ = new base::SequencedWorkerPool(3, "blocking_pool"); 403 blocking_pool_ = new base::SequencedWorkerPool(3, "blocking_pool");
405 catalog_.reset( 404 catalog_.reset(
406 new catalog::Factory(blocking_pool_.get(), nullptr, nullptr)); 405 new catalog::Factory(blocking_pool_.get(), nullptr, nullptr));
407 shell_.reset(new Shell(nullptr, catalog_->TakeShellClient())); 406 shell_.reset(new Shell(nullptr, catalog_->TakeShellClient()));
408 test_loader_ = new TestLoader(&context_); 407 test_loader_ = new TestLoader(&context_);
409 shell_->set_default_loader(scoped_ptr<Loader>(test_loader_)); 408 shell_->set_default_loader(std::unique_ptr<Loader>(test_loader_));
410 409
411 TestServicePtr service_proxy; 410 TestServicePtr service_proxy;
412 ConnectToInterface(kTestURLString, &service_proxy); 411 ConnectToInterface(kTestURLString, &service_proxy);
413 test_client_.reset(new TestClient(std::move(service_proxy))); 412 test_client_.reset(new TestClient(std::move(service_proxy)));
414 } 413 }
415 414
416 void TearDown() override { 415 void TearDown() override {
417 test_client_.reset(); 416 test_client_.reset();
418 shell_.reset(); 417 shell_.reset();
419 blocking_pool_->Shutdown(); 418 blocking_pool_->Shutdown();
420 } 419 }
421 420
422 void AddLoaderForName(const std::string& name, 421 void AddLoaderForName(const std::string& name,
423 const std::string& requestor_name) { 422 const std::string& requestor_name) {
424 shell_->SetLoaderForName( 423 shell_->SetLoaderForName(
425 make_scoped_ptr(new Tester(&tester_context_, requestor_name)), name); 424 base::WrapUnique(new Tester(&tester_context_, requestor_name)), name);
426 } 425 }
427 426
428 bool HasRunningInstanceForName(const std::string& name) { 427 bool HasRunningInstanceForName(const std::string& name) {
429 Shell::TestAPI test_api(shell_.get()); 428 Shell::TestAPI test_api(shell_.get());
430 return test_api.HasRunningInstanceForName(name); 429 return test_api.HasRunningInstanceForName(name);
431 } 430 }
432 431
433 protected: 432 protected:
434 template <typename Interface> 433 template <typename Interface>
435 void ConnectToInterface(const std::string& name, 434 void ConnectToInterface(const std::string& name,
436 InterfacePtr<Interface>* ptr) { 435 mojo::InterfacePtr<Interface>* ptr) {
437 base::RunLoop loop; 436 base::RunLoop loop;
438 mojom::InterfaceProviderPtr remote_interfaces; 437 mojom::InterfaceProviderPtr remote_interfaces;
439 scoped_ptr<ConnectParams> params(new ConnectParams); 438 std::unique_ptr<ConnectParams> params(new ConnectParams);
440 params->set_source(CreateShellIdentity()); 439 params->set_source(CreateShellIdentity());
441 params->set_target(Identity(name, mojom::kRootUserID)); 440 params->set_target(Identity(name, mojom::kRootUserID));
442 params->set_remote_interfaces(GetProxy(&remote_interfaces)); 441 params->set_remote_interfaces(mojo::GetProxy(&remote_interfaces));
443 params->set_connect_callback( 442 params->set_connect_callback(
444 base::Bind(&OnConnect, base::Unretained(&loop))); 443 base::Bind(&OnConnect, base::Unretained(&loop)));
445 shell_->Connect(std::move(params)); 444 shell_->Connect(std::move(params));
446 loop.Run(); 445 loop.Run();
447 446
448 mojo::GetInterface(remote_interfaces.get(), ptr); 447 GetInterface(remote_interfaces.get(), ptr);
449 } 448 }
450 449
451 base::ShadowingAtExitManager at_exit_; 450 base::ShadowingAtExitManager at_exit_;
452 TestLoader* test_loader_; 451 TestLoader* test_loader_;
453 TesterContext tester_context_; 452 TesterContext tester_context_;
454 TestContext context_; 453 TestContext context_;
455 base::MessageLoop loop_; 454 base::MessageLoop loop_;
456 scoped_ptr<TestClient> test_client_; 455 std::unique_ptr<TestClient> test_client_;
457 scoped_ptr<catalog::Factory> catalog_; 456 std::unique_ptr<catalog::Factory> catalog_;
458 scoped_refptr<base::SequencedWorkerPool> blocking_pool_; 457 scoped_refptr<base::SequencedWorkerPool> blocking_pool_;
459 scoped_ptr<Shell> shell_; 458 std::unique_ptr<Shell> shell_;
459
460 DISALLOW_COPY_AND_ASSIGN(LoaderTest); 460 DISALLOW_COPY_AND_ASSIGN(LoaderTest);
461 }; 461 };
462 462
463 TEST_F(LoaderTest, Basic) { 463 TEST_F(LoaderTest, Basic) {
464 test_client_->Test("test"); 464 test_client_->Test("test");
465 loop_.Run(); 465 loop_.Run();
466 EXPECT_EQ(std::string("test"), context_.last_test_string); 466 EXPECT_EQ(std::string("test"), context_.last_test_string);
467 } 467 }
468 468
469 TEST_F(LoaderTest, ClientError) { 469 TEST_F(LoaderTest, ClientError) {
470 test_client_->Test("test"); 470 test_client_->Test("test");
471 EXPECT_TRUE(HasRunningInstanceForName(kTestURLString)); 471 EXPECT_TRUE(HasRunningInstanceForName(kTestURLString));
472 loop_.Run(); 472 loop_.Run();
473 EXPECT_EQ(1, context_.num_impls); 473 EXPECT_EQ(1, context_.num_impls);
474 test_client_.reset(); 474 test_client_.reset();
475 loop_.Run(); 475 loop_.Run();
476 EXPECT_EQ(0, context_.num_impls); 476 EXPECT_EQ(0, context_.num_impls);
477 EXPECT_TRUE(HasRunningInstanceForName(kTestURLString)); 477 EXPECT_TRUE(HasRunningInstanceForName(kTestURLString));
478 } 478 }
479 479
480 TEST_F(LoaderTest, Deletes) { 480 TEST_F(LoaderTest, Deletes) {
481 { 481 {
482 catalog::Factory catalog(blocking_pool_.get(), nullptr, nullptr); 482 catalog::Factory catalog(blocking_pool_.get(), nullptr, nullptr);
483 Shell shell(nullptr, catalog.TakeShellClient()); 483 Shell shell(nullptr, catalog.TakeShellClient());
484 TestLoader* default_loader = new TestLoader(&context_); 484 TestLoader* default_loader = new TestLoader(&context_);
485 TestLoader* name_loader1 = new TestLoader(&context_); 485 TestLoader* name_loader1 = new TestLoader(&context_);
486 TestLoader* name_loader2 = new TestLoader(&context_); 486 TestLoader* name_loader2 = new TestLoader(&context_);
487 shell.set_default_loader(scoped_ptr<Loader>(default_loader)); 487 shell.set_default_loader(std::unique_ptr<Loader>(default_loader));
488 shell.SetLoaderForName(scoped_ptr<Loader>(name_loader1), "test:test1"); 488 shell.SetLoaderForName(std::unique_ptr<Loader>(name_loader1), "test:test1");
489 shell.SetLoaderForName(scoped_ptr<Loader>(name_loader2), "test:test1"); 489 shell.SetLoaderForName(std::unique_ptr<Loader>(name_loader2), "test:test1");
490 } 490 }
491 EXPECT_EQ(3, context_.num_loader_deletes); 491 EXPECT_EQ(3, context_.num_loader_deletes);
492 } 492 }
493 493
494 // Test for SetLoaderForName() & set_default_loader(). 494 // Test for SetLoaderForName() & set_default_loader().
495 TEST_F(LoaderTest, SetLoaders) { 495 TEST_F(LoaderTest, SetLoaders) {
496 TestLoader* default_loader = new TestLoader(&context_); 496 TestLoader* default_loader = new TestLoader(&context_);
497 TestLoader* name_loader = new TestLoader(&context_); 497 TestLoader* name_loader = new TestLoader(&context_);
498 shell_->set_default_loader(scoped_ptr<Loader>(default_loader)); 498 shell_->set_default_loader(std::unique_ptr<Loader>(default_loader));
499 shell_->SetLoaderForName(scoped_ptr<Loader>(name_loader), "test:test1"); 499 shell_->SetLoaderForName(std::unique_ptr<Loader>(name_loader), "test:test1");
500 500
501 // test::test1 should go to name_loader. 501 // test::test1 should go to name_loader.
502 TestServicePtr test_service; 502 TestServicePtr test_service;
503 ConnectToInterface("test:test1", &test_service); 503 ConnectToInterface("test:test1", &test_service);
504 EXPECT_EQ(1, name_loader->num_loads()); 504 EXPECT_EQ(1, name_loader->num_loads());
505 EXPECT_EQ(0, default_loader->num_loads()); 505 EXPECT_EQ(0, default_loader->num_loads());
506 506
507 // http::test1 should go to default loader. 507 // http::test1 should go to default loader.
508 ConnectToInterface("http:test1", &test_service); 508 ConnectToInterface("http:test1", &test_service);
509 EXPECT_EQ(1, name_loader->num_loads()); 509 EXPECT_EQ(1, name_loader->num_loads());
510 EXPECT_EQ(1, default_loader->num_loads()); 510 EXPECT_EQ(1, default_loader->num_loads());
511 } 511 }
512 512
513 TEST_F(LoaderTest, NoServiceNoLoad) { 513 TEST_F(LoaderTest, NoServiceNoLoad) {
514 AddLoaderForName(kTestAURLString, std::string()); 514 AddLoaderForName(kTestAURLString, std::string());
515 515
516 // There is no TestC service implementation registered with the Shell, so this 516 // There is no TestC service implementation registered with the Shell, so this
517 // cannot succeed (but also shouldn't crash). 517 // cannot succeed (but also shouldn't crash).
518 TestCPtr c; 518 TestCPtr c;
519 ConnectToInterface(kTestAURLString, &c); 519 ConnectToInterface(kTestAURLString, &c);
520 c.set_connection_error_handler( 520 c.set_connection_error_handler(
521 []() { base::MessageLoop::current()->QuitWhenIdle(); }); 521 []() { base::MessageLoop::current()->QuitWhenIdle(); });
522 522
523 loop_.Run(); 523 loop_.Run();
524 EXPECT_TRUE(c.encountered_error()); 524 EXPECT_TRUE(c.encountered_error());
525 } 525 }
526 526
527 TEST_F(LoaderTest, TestEndApplicationClosure) { 527 TEST_F(LoaderTest, TestEndApplicationClosure) {
528 ClosingLoader* loader = new ClosingLoader(); 528 ClosingLoader* loader = new ClosingLoader();
529 shell_->SetLoaderForName(scoped_ptr<Loader>(loader), "test:test"); 529 shell_->SetLoaderForName(std::unique_ptr<Loader>(loader), "test:test");
530 530
531 bool called = false; 531 bool called = false;
532 scoped_ptr<ConnectParams> params(new ConnectParams); 532 std::unique_ptr<ConnectParams> params(new ConnectParams);
533 params->set_source(CreateShellIdentity()); 533 params->set_source(CreateShellIdentity());
534 params->set_target(Identity("test:test", mojom::kRootUserID)); 534 params->set_target(Identity("test:test", mojom::kRootUserID));
535 shell_->SetInstanceQuitCallback( 535 shell_->SetInstanceQuitCallback(
536 base::Bind(&QuitClosure, params->target(), &called)); 536 base::Bind(&QuitClosure, params->target(), &called));
537 shell_->Connect(std::move(params)); 537 shell_->Connect(std::move(params));
538 loop_.Run(); 538 loop_.Run();
539 EXPECT_TRUE(called); 539 EXPECT_TRUE(called);
540 } 540 }
541 541
542 TEST_F(LoaderTest, SameIdentityShouldNotCauseDuplicateLoad) { 542 TEST_F(LoaderTest, SameIdentityShouldNotCauseDuplicateLoad) {
543 // 1 because LoaderTest connects once at startup. 543 // 1 because LoaderTest connects once at startup.
544 EXPECT_EQ(1, test_loader_->num_loads()); 544 EXPECT_EQ(1, test_loader_->num_loads());
545 545
546 TestServicePtr test_service; 546 TestServicePtr test_service;
547 ConnectToInterface("test:foo", &test_service); 547 ConnectToInterface("test:foo", &test_service);
548 EXPECT_EQ(2, test_loader_->num_loads()); 548 EXPECT_EQ(2, test_loader_->num_loads());
549 549
550 // Exactly the same name as above. 550 // Exactly the same name as above.
551 ConnectToInterface("test:foo", &test_service); 551 ConnectToInterface("test:foo", &test_service);
552 EXPECT_EQ(2, test_loader_->num_loads()); 552 EXPECT_EQ(2, test_loader_->num_loads());
553 553
554 // A different identity because the domain is different. 554 // A different identity because the domain is different.
555 ConnectToInterface("test:bar", &test_service); 555 ConnectToInterface("test:bar", &test_service);
556 EXPECT_EQ(3, test_loader_->num_loads()); 556 EXPECT_EQ(3, test_loader_->num_loads());
557 } 557 }
558 558
559 } // namespace test 559 } // namespace test
560 } // namespace shell 560 } // namespace shell
561 } // namespace mojo
OLDNEW
« no previous file with comments | « services/shell/tests/lifecycle/parent_manifest.json ('k') | services/shell/tests/shell/driver.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698