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
3 // found in the LICENSE file.
5 #include "base/at_exit.h"
7 #include "base/macros.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/message_loop/message_loop.h"
10 #include "mojo/public/cpp/application/application_connection.h"
11 #include "mojo/public/cpp/application/application_delegate.h"
12 #include "mojo/public/cpp/application/application_impl.h"
13 #include "mojo/public/cpp/application/interface_factory.h"
14 #include "mojo/public/cpp/bindings/strong_binding.h"
15 #include "mojo/public/interfaces/application/service_provider.mojom.h"
16 #include "mojo/shell/application_manager/application_loader.h"
17 #include "mojo/shell/application_manager/application_manager.h"
18 #include "mojo/shell/application_manager/test.mojom.h"
19 #include "testing/gtest/include/gtest/gtest.h"
25 const char kTestURLString
[] = "test:testService";
26 const char kTestAURLString
[] = "test:TestA";
27 const char kTestBURLString
[] = "test:TestB";
30 TestContext() : num_impls(0), num_loader_deletes(0) {}
31 std::string last_test_string
;
33 int num_loader_deletes
;
36 void QuitClosure(bool* value
) {
38 base::MessageLoop::current()->QuitWhenIdle();
41 class QuitMessageLoopErrorHandler
: public ErrorHandler
{
43 QuitMessageLoopErrorHandler() {}
44 ~QuitMessageLoopErrorHandler() override
{}
46 // |ErrorHandler| implementation:
47 void OnConnectionError() override
{
48 base::MessageLoop::current()->QuitWhenIdle();
52 DISALLOW_COPY_AND_ASSIGN(QuitMessageLoopErrorHandler
);
55 class TestServiceImpl
: public TestService
{
57 TestServiceImpl(TestContext
* context
, InterfaceRequest
<TestService
> request
)
58 : context_(context
), binding_(this, request
.Pass()) {
59 ++context_
->num_impls
;
62 ~TestServiceImpl() override
{
63 --context_
->num_impls
;
64 if (!base::MessageLoop::current()->is_running())
66 base::MessageLoop::current()->Quit();
69 // TestService implementation:
70 void Test(const String
& test_string
,
71 const Callback
<void()>& callback
) override
{
72 context_
->last_test_string
= test_string
;
77 TestContext
* context_
;
78 StrongBinding
<TestService
> binding_
;
83 explicit TestClient(TestServicePtr service
)
84 : service_(service
.Pass()), quit_after_ack_(false) {}
88 base::MessageLoop::current()->Quit();
91 void Test(const std::string
& test_string
) {
92 quit_after_ack_
= true;
93 service_
->Test(test_string
,
94 base::Bind(&TestClient::AckTest
, base::Unretained(this)));
98 TestServicePtr service_
;
100 DISALLOW_COPY_AND_ASSIGN(TestClient
);
103 class TestApplicationLoader
: public ApplicationLoader
,
104 public ApplicationDelegate
,
105 public InterfaceFactory
<TestService
> {
107 TestApplicationLoader() : context_(nullptr), num_loads_(0) {}
109 ~TestApplicationLoader() override
{
111 ++context_
->num_loader_deletes
;
115 void set_context(TestContext
* context
) { context_
= context
; }
116 int num_loads() const { return num_loads_
; }
117 const std::vector
<std::string
>& GetArgs() const { return test_app_
->args(); }
120 // ApplicationLoader implementation.
121 void Load(const GURL
& url
,
122 InterfaceRequest
<Application
> application_request
) override
{
124 test_app_
.reset(new ApplicationImpl(this, application_request
.Pass()));
127 // ApplicationDelegate implementation.
128 bool ConfigureIncomingConnection(ApplicationConnection
* connection
) override
{
129 connection
->AddService(this);
133 // InterfaceFactory implementation.
134 void Create(ApplicationConnection
* connection
,
135 InterfaceRequest
<TestService
> request
) override
{
136 new TestServiceImpl(context_
, request
.Pass());
139 scoped_ptr
<ApplicationImpl
> test_app_
;
140 TestContext
* context_
;
142 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader
);
145 class ClosingApplicationLoader
: public ApplicationLoader
{
147 // ApplicationLoader implementation.
148 void Load(const GURL
& url
,
149 InterfaceRequest
<Application
> application_request
) override
{}
152 class TesterContext
{
154 explicit TesterContext(base::MessageLoop
* loop
)
160 tester_called_quit_(false),
161 a_called_quit_(false),
164 void IncrementNumBCalls() {
165 base::AutoLock
lock(lock_
);
169 void IncrementNumCCalls() {
170 base::AutoLock
lock(lock_
);
174 void IncrementNumADeletes() {
175 base::AutoLock
lock(lock_
);
179 void IncrementNumBDeletes() {
180 base::AutoLock
lock(lock_
);
184 void IncrementNumCDeletes() {
185 base::AutoLock
lock(lock_
);
189 void set_tester_called_quit() {
190 base::AutoLock
lock(lock_
);
191 tester_called_quit_
= true;
194 void set_a_called_quit() {
195 base::AutoLock
lock(lock_
);
196 a_called_quit_
= true;
200 base::AutoLock
lock(lock_
);
204 base::AutoLock
lock(lock_
);
207 int num_a_deletes() {
208 base::AutoLock
lock(lock_
);
209 return num_a_deletes_
;
211 int num_b_deletes() {
212 base::AutoLock
lock(lock_
);
213 return num_b_deletes_
;
215 int num_c_deletes() {
216 base::AutoLock
lock(lock_
);
217 return num_c_deletes_
;
219 bool tester_called_quit() {
220 base::AutoLock
lock(lock_
);
221 return tester_called_quit_
;
223 bool a_called_quit() {
224 base::AutoLock
lock(lock_
);
225 return a_called_quit_
;
229 loop_
->PostTask(FROM_HERE
, base::MessageLoop::QuitWhenIdleClosure());
233 // lock_ protects all members except for loop_ which must be unchanged for the
234 // lifetime of this class.
241 bool tester_called_quit_
;
244 base::MessageLoop
* loop_
;
247 // Used to test that the requestor url will be correctly passed.
248 class TestAImpl
: public TestA
{
250 TestAImpl(ApplicationImpl
* app_impl
,
251 TesterContext
* test_context
,
252 InterfaceRequest
<TestA
> request
)
253 : test_context_(test_context
), binding_(this, request
.Pass()) {
254 app_impl
->ConnectToApplication(kTestBURLString
)->ConnectToService(&b_
);
257 ~TestAImpl() override
{
258 test_context_
->IncrementNumADeletes();
259 if (base::MessageLoop::current()->is_running())
264 void CallB() override
{
265 b_
->B(base::Bind(&TestAImpl::Quit
, base::Unretained(this)));
268 void CallCFromB() override
{
269 b_
->CallC(base::Bind(&TestAImpl::Quit
, base::Unretained(this)));
273 base::MessageLoop::current()->Quit();
274 test_context_
->set_a_called_quit();
275 test_context_
->QuitSoon();
278 TesterContext
* test_context_
;
280 StrongBinding
<TestA
> binding_
;
283 class TestBImpl
: public TestB
{
285 TestBImpl(ApplicationConnection
* connection
,
286 TesterContext
* test_context
,
287 InterfaceRequest
<TestB
> request
)
288 : test_context_(test_context
), binding_(this, request
.Pass()) {
289 connection
->ConnectToService(&c_
);
292 ~TestBImpl() override
{
293 test_context_
->IncrementNumBDeletes();
294 if (base::MessageLoop::current()->is_running())
295 base::MessageLoop::current()->Quit();
296 test_context_
->QuitSoon();
300 void B(const Callback
<void()>& callback
) override
{
301 test_context_
->IncrementNumBCalls();
305 void CallC(const Callback
<void()>& callback
) override
{
306 test_context_
->IncrementNumBCalls();
310 TesterContext
* test_context_
;
312 StrongBinding
<TestB
> binding_
;
315 class TestCImpl
: public TestC
{
317 TestCImpl(ApplicationConnection
* connection
,
318 TesterContext
* test_context
,
319 InterfaceRequest
<TestC
> request
)
320 : test_context_(test_context
), binding_(this, request
.Pass()) {}
322 ~TestCImpl() override
{ test_context_
->IncrementNumCDeletes(); }
325 void C(const Callback
<void()>& callback
) override
{
326 test_context_
->IncrementNumCCalls();
330 TesterContext
* test_context_
;
331 StrongBinding
<TestC
> binding_
;
334 class Tester
: public ApplicationDelegate
,
335 public ApplicationLoader
,
336 public InterfaceFactory
<TestA
>,
337 public InterfaceFactory
<TestB
>,
338 public InterfaceFactory
<TestC
> {
340 Tester(TesterContext
* context
, const std::string
& requestor_url
)
341 : context_(context
), requestor_url_(requestor_url
) {}
342 ~Tester() override
{}
345 void Load(const GURL
& url
,
346 InterfaceRequest
<Application
> application_request
) override
{
347 app_
.reset(new ApplicationImpl(this, application_request
.Pass()));
350 bool ConfigureIncomingConnection(ApplicationConnection
* connection
) override
{
351 if (!requestor_url_
.empty() &&
352 requestor_url_
!= connection
->GetRemoteApplicationURL()) {
353 context_
->set_tester_called_quit();
354 context_
->QuitSoon();
355 base::MessageLoop::current()->Quit();
358 // If we're coming from A, then add B, otherwise A.
359 if (connection
->GetRemoteApplicationURL() == kTestAURLString
)
360 connection
->AddService
<TestB
>(this);
362 connection
->AddService
<TestA
>(this);
366 bool ConfigureOutgoingConnection(ApplicationConnection
* connection
) override
{
367 // If we're connecting to B, then add C.
368 if (connection
->GetRemoteApplicationURL() == kTestBURLString
)
369 connection
->AddService
<TestC
>(this);
373 void Create(ApplicationConnection
* connection
,
374 InterfaceRequest
<TestA
> request
) override
{
375 a_bindings_
.push_back(new TestAImpl(app_
.get(), context_
, request
.Pass()));
378 void Create(ApplicationConnection
* connection
,
379 InterfaceRequest
<TestB
> request
) override
{
380 new TestBImpl(connection
, context_
, request
.Pass());
383 void Create(ApplicationConnection
* connection
,
384 InterfaceRequest
<TestC
> request
) override
{
385 new TestCImpl(connection
, context_
, request
.Pass());
388 TesterContext
* context_
;
389 scoped_ptr
<ApplicationImpl
> app_
;
390 std::string requestor_url_
;
391 ScopedVector
<TestAImpl
> a_bindings_
;
394 class TestDelegate
: public ApplicationManager::Delegate
{
396 void AddMapping(const GURL
& from
, const GURL
& to
) { mappings_
[from
] = to
; }
398 // ApplicationManager::Delegate
399 GURL
ResolveMappings(const GURL
& url
) override
{
400 auto it
= mappings_
.find(url
);
401 if (it
!= mappings_
.end())
406 // ApplicationManager::Delegate
407 GURL
ResolveURL(const GURL
& url
) override
{
408 GURL mapped_url
= ResolveMappings(url
);
409 // The shell automatically map mojo URLs.
410 if (mapped_url
.scheme() == "mojo") {
411 url::Replacements
<char> replacements
;
412 replacements
.SetScheme("file", url::Component(0, 4));
413 mapped_url
= mapped_url
.ReplaceComponents(replacements
);
419 std::map
<GURL
, GURL
> mappings_
;
422 class ApplicationManagerTest
: public testing::Test
{
424 ApplicationManagerTest() : tester_context_(&loop_
) {}
426 ~ApplicationManagerTest() override
{}
428 void SetUp() override
{
429 application_manager_
.reset(new ApplicationManager(&test_delegate_
));
430 test_loader_
= new TestApplicationLoader
;
431 test_loader_
->set_context(&context_
);
432 application_manager_
->set_default_loader(
433 scoped_ptr
<ApplicationLoader
>(test_loader_
));
435 TestServicePtr service_proxy
;
436 application_manager_
->ConnectToService(GURL(kTestURLString
),
438 test_client_
.reset(new TestClient(service_proxy
.Pass()));
441 void TearDown() override
{
442 test_client_
.reset();
443 application_manager_
.reset();
446 void AddLoaderForURL(const GURL
& url
, const std::string
& requestor_url
) {
447 application_manager_
->SetLoaderForURL(
448 make_scoped_ptr(new Tester(&tester_context_
, requestor_url
)), url
);
451 bool HasFactoryForTestURL() {
452 ApplicationManager::TestAPI
manager_test_api(application_manager_
.get());
453 return manager_test_api
.HasFactoryForURL(GURL(kTestURLString
));
457 base::ShadowingAtExitManager at_exit_
;
458 TestDelegate test_delegate_
;
459 TestApplicationLoader
* test_loader_
;
460 TesterContext tester_context_
;
461 TestContext context_
;
462 base::MessageLoop loop_
;
463 scoped_ptr
<TestClient
> test_client_
;
464 scoped_ptr
<ApplicationManager
> application_manager_
;
465 DISALLOW_COPY_AND_ASSIGN(ApplicationManagerTest
);
468 TEST_F(ApplicationManagerTest
, Basic
) {
469 test_client_
->Test("test");
471 EXPECT_EQ(std::string("test"), context_
.last_test_string
);
474 // Confirm that no arguments are sent to an application by default.
475 TEST_F(ApplicationManagerTest
, NoArgs
) {
476 ApplicationManager
am(&test_delegate_
);
477 GURL
test_url("test:test");
478 TestApplicationLoader
* loader
= new TestApplicationLoader
;
479 loader
->set_context(&context_
);
480 am
.SetLoaderForURL(scoped_ptr
<ApplicationLoader
>(loader
), test_url
);
481 TestServicePtr test_service
;
482 am
.ConnectToService(test_url
, &test_service
);
483 TestClient
test_client(test_service
.Pass());
484 test_client
.Test("test");
486 std::vector
<std::string
> app_args
= loader
->GetArgs();
487 EXPECT_EQ(0U, app_args
.size());
490 // Confirm that url mappings are respected.
491 TEST_F(ApplicationManagerTest
, URLMapping
) {
492 ApplicationManager
am(&test_delegate_
);
493 GURL
test_url("test:test");
494 GURL
test_url2("test:test2");
495 test_delegate_
.AddMapping(test_url
, test_url2
);
496 TestApplicationLoader
* loader
= new TestApplicationLoader
;
497 loader
->set_context(&context_
);
498 am
.SetLoaderForURL(scoped_ptr
<ApplicationLoader
>(loader
), test_url2
);
500 // Connext to the mapped url
501 TestServicePtr test_service
;
502 am
.ConnectToService(test_url
, &test_service
);
503 TestClient
test_client(test_service
.Pass());
504 test_client
.Test("test");
508 // Connext to the target url
509 TestServicePtr test_service
;
510 am
.ConnectToService(test_url2
, &test_service
);
511 TestClient
test_client(test_service
.Pass());
512 test_client
.Test("test");
517 TEST_F(ApplicationManagerTest
, ClientError
) {
518 test_client_
->Test("test");
519 EXPECT_TRUE(HasFactoryForTestURL());
521 EXPECT_EQ(1, context_
.num_impls
);
522 test_client_
.reset();
524 EXPECT_EQ(0, context_
.num_impls
);
525 EXPECT_TRUE(HasFactoryForTestURL());
528 TEST_F(ApplicationManagerTest
, Deletes
) {
530 ApplicationManager
am(&test_delegate_
);
531 TestApplicationLoader
* default_loader
= new TestApplicationLoader
;
532 default_loader
->set_context(&context_
);
533 TestApplicationLoader
* url_loader1
= new TestApplicationLoader
;
534 TestApplicationLoader
* url_loader2
= new TestApplicationLoader
;
535 url_loader1
->set_context(&context_
);
536 url_loader2
->set_context(&context_
);
537 TestApplicationLoader
* scheme_loader1
= new TestApplicationLoader
;
538 TestApplicationLoader
* scheme_loader2
= new TestApplicationLoader
;
539 scheme_loader1
->set_context(&context_
);
540 scheme_loader2
->set_context(&context_
);
541 am
.set_default_loader(scoped_ptr
<ApplicationLoader
>(default_loader
));
542 am
.SetLoaderForURL(scoped_ptr
<ApplicationLoader
>(url_loader1
),
544 am
.SetLoaderForURL(scoped_ptr
<ApplicationLoader
>(url_loader2
),
546 am
.SetLoaderForScheme(scoped_ptr
<ApplicationLoader
>(scheme_loader1
),
548 am
.SetLoaderForScheme(scoped_ptr
<ApplicationLoader
>(scheme_loader2
),
551 EXPECT_EQ(5, context_
.num_loader_deletes
);
554 // Confirm that both urls and schemes can have their loaders explicitly set.
555 TEST_F(ApplicationManagerTest
, SetLoaders
) {
556 TestApplicationLoader
* default_loader
= new TestApplicationLoader
;
557 TestApplicationLoader
* url_loader
= new TestApplicationLoader
;
558 TestApplicationLoader
* scheme_loader
= new TestApplicationLoader
;
559 application_manager_
->set_default_loader(
560 scoped_ptr
<ApplicationLoader
>(default_loader
));
561 application_manager_
->SetLoaderForURL(
562 scoped_ptr
<ApplicationLoader
>(url_loader
), GURL("test:test1"));
563 application_manager_
->SetLoaderForScheme(
564 scoped_ptr
<ApplicationLoader
>(scheme_loader
), "test");
566 // test::test1 should go to url_loader.
567 TestServicePtr test_service
;
568 application_manager_
->ConnectToService(GURL("test:test1"), &test_service
);
569 EXPECT_EQ(1, url_loader
->num_loads());
570 EXPECT_EQ(0, scheme_loader
->num_loads());
571 EXPECT_EQ(0, default_loader
->num_loads());
573 // test::test2 should go to scheme loader.
574 application_manager_
->ConnectToService(GURL("test:test2"), &test_service
);
575 EXPECT_EQ(1, url_loader
->num_loads());
576 EXPECT_EQ(1, scheme_loader
->num_loads());
577 EXPECT_EQ(0, default_loader
->num_loads());
579 // http::test1 should go to default loader.
580 application_manager_
->ConnectToService(GURL("http:test1"), &test_service
);
581 EXPECT_EQ(1, url_loader
->num_loads());
582 EXPECT_EQ(1, scheme_loader
->num_loads());
583 EXPECT_EQ(1, default_loader
->num_loads());
586 // Confirm that the url of a service is correctly passed to another service that
588 TEST_F(ApplicationManagerTest
, ACallB
) {
589 // Any url can load a.
590 AddLoaderForURL(GURL(kTestAURLString
), std::string());
592 // Only a can load b.
593 AddLoaderForURL(GURL(kTestBURLString
), kTestAURLString
);
596 application_manager_
->ConnectToService(GURL(kTestAURLString
), &a
);
599 EXPECT_EQ(1, tester_context_
.num_b_calls());
600 EXPECT_TRUE(tester_context_
.a_called_quit());
603 // A calls B which calls C.
604 TEST_F(ApplicationManagerTest
, BCallC
) {
605 // Any url can load a.
606 AddLoaderForURL(GURL(kTestAURLString
), std::string());
608 // Only a can load b.
609 AddLoaderForURL(GURL(kTestBURLString
), kTestAURLString
);
612 application_manager_
->ConnectToService(GURL(kTestAURLString
), &a
);
616 EXPECT_EQ(1, tester_context_
.num_b_calls());
617 EXPECT_EQ(1, tester_context_
.num_c_calls());
618 EXPECT_TRUE(tester_context_
.a_called_quit());
621 // Confirm that a service impl will be deleted if the app that connected to
623 TEST_F(ApplicationManagerTest
, BDeleted
) {
624 AddLoaderForURL(GURL(kTestAURLString
), std::string());
625 AddLoaderForURL(GURL(kTestBURLString
), std::string());
628 application_manager_
->ConnectToService(GURL(kTestAURLString
), &a
);
634 application_manager_
->SetLoaderForURL(scoped_ptr
<ApplicationLoader
>(),
635 GURL(kTestAURLString
));
638 EXPECT_EQ(1, tester_context_
.num_b_deletes());
641 // Confirm that the url of a service is correctly passed to another service that
642 // it loads, and that it can be rejected.
643 TEST_F(ApplicationManagerTest
, ANoLoadB
) {
644 // Any url can load a.
645 AddLoaderForURL(GURL(kTestAURLString
), std::string());
647 // Only c can load b, so this will fail.
648 AddLoaderForURL(GURL(kTestBURLString
), "test:TestC");
651 application_manager_
->ConnectToService(GURL(kTestAURLString
), &a
);
654 EXPECT_EQ(0, tester_context_
.num_b_calls());
656 EXPECT_FALSE(tester_context_
.a_called_quit());
657 EXPECT_TRUE(tester_context_
.tester_called_quit());
660 TEST_F(ApplicationManagerTest
, NoServiceNoLoad
) {
661 AddLoaderForURL(GURL(kTestAURLString
), std::string());
663 // There is no TestC service implementation registered with
664 // ApplicationManager, so this cannot succeed (but also shouldn't crash).
666 application_manager_
->ConnectToService(GURL(kTestAURLString
), &c
);
667 QuitMessageLoopErrorHandler quitter
;
668 c
.set_error_handler(&quitter
);
671 EXPECT_TRUE(c
.encountered_error());
674 TEST_F(ApplicationManagerTest
, MappedURLsShouldNotCauseDuplicateLoad
) {
675 test_delegate_
.AddMapping(GURL("foo:foo2"), GURL("foo:foo"));
676 // 1 because ApplicationManagerTest connects once at startup.
677 EXPECT_EQ(1, test_loader_
->num_loads());
679 TestServicePtr test_service
;
680 application_manager_
->ConnectToService(GURL("foo:foo"), &test_service
);
681 EXPECT_EQ(2, test_loader_
->num_loads());
683 TestServicePtr test_service2
;
684 application_manager_
->ConnectToService(GURL("foo:foo2"), &test_service2
);
685 EXPECT_EQ(2, test_loader_
->num_loads());
687 TestServicePtr test_service3
;
688 application_manager_
->ConnectToService(GURL("bar:bar"), &test_service2
);
689 EXPECT_EQ(3, test_loader_
->num_loads());
692 TEST_F(ApplicationManagerTest
, MappedURLsShouldWorkWithLoaders
) {
693 TestApplicationLoader
* custom_loader
= new TestApplicationLoader
;
695 custom_loader
->set_context(&context
);
696 application_manager_
->SetLoaderForURL(make_scoped_ptr(custom_loader
),
698 test_delegate_
.AddMapping(GURL("mojo:foo2"), GURL("mojo:foo"));
700 TestServicePtr test_service
;
701 application_manager_
->ConnectToService(GURL("mojo:foo2"), &test_service
);
702 EXPECT_EQ(1, custom_loader
->num_loads());
703 custom_loader
->set_context(nullptr);
706 TEST_F(ApplicationManagerTest
, TestQueryWithLoaders
) {
707 TestApplicationLoader
* url_loader
= new TestApplicationLoader
;
708 TestApplicationLoader
* scheme_loader
= new TestApplicationLoader
;
709 application_manager_
->SetLoaderForURL(
710 scoped_ptr
<ApplicationLoader
>(url_loader
), GURL("test:test1"));
711 application_manager_
->SetLoaderForScheme(
712 scoped_ptr
<ApplicationLoader
>(scheme_loader
), "test");
714 // test::test1 should go to url_loader.
715 TestServicePtr test_service
;
716 application_manager_
->ConnectToService(GURL("test:test1?foo=bar"),
718 EXPECT_EQ(1, url_loader
->num_loads());
719 EXPECT_EQ(0, scheme_loader
->num_loads());
721 // test::test2 should go to scheme loader.
722 application_manager_
->ConnectToService(GURL("test:test2?foo=bar"),
724 EXPECT_EQ(1, url_loader
->num_loads());
725 EXPECT_EQ(1, scheme_loader
->num_loads());
728 TEST_F(ApplicationManagerTest
, TestEndApplicationClosure
) {
729 ClosingApplicationLoader
* loader
= new ClosingApplicationLoader();
730 application_manager_
->SetLoaderForScheme(
731 scoped_ptr
<ApplicationLoader
>(loader
), "test");
734 application_manager_
->ConnectToApplication(
735 GURL("test:test"), GURL(), nullptr, nullptr,
736 base::Bind(&QuitClosure
, base::Unretained(&called
)));