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 "base/run_loop.h"
11 #include "mojo/application/public/cpp/application_connection.h"
12 #include "mojo/application/public/cpp/application_delegate.h"
13 #include "mojo/application/public/cpp/application_impl.h"
14 #include "mojo/application/public/cpp/interface_factory.h"
15 #include "mojo/application/public/interfaces/content_handler.mojom.h"
16 #include "mojo/application/public/interfaces/service_provider.mojom.h"
17 #include "mojo/public/cpp/bindings/strong_binding.h"
18 #include "mojo/shell/application_loader.h"
19 #include "mojo/shell/application_manager.h"
20 #include "mojo/shell/connect_util.h"
21 #include "mojo/shell/fetcher.h"
22 #include "mojo/shell/test.mojom.h"
23 #include "mojo/shell/test_package_manager.h"
24 #include "testing/gtest/include/gtest/gtest.h"
30 const char kTestURLString
[] = "test:testService";
31 const char kTestAURLString
[] = "test:TestA";
32 const char kTestBURLString
[] = "test:TestB";
34 const char kTestMimeType
[] = "test/mime-type";
36 class TestMimeTypeFetcher
: public Fetcher
{
38 TestMimeTypeFetcher(const FetchCallback
& fetch_callback
,
40 const std::string
& mime_type
)
41 : Fetcher(fetch_callback
), url_(url
), mime_type_(mime_type
) {
42 loader_callback_
.Run(make_scoped_ptr(this));
44 ~TestMimeTypeFetcher() override
{}
47 const GURL
& GetURL() const override
{ return url_
; }
48 GURL
GetRedirectURL() const override
{ return GURL("yyy"); }
49 GURL
GetRedirectReferer() const override
{ return GURL(); }
50 URLResponsePtr
AsURLResponse(base::TaskRunner
* task_runner
,
51 uint32_t skip
) override
{
52 return URLResponse::New().Pass();
55 base::TaskRunner
* task_runner
,
56 base::Callback
<void(const base::FilePath
&, bool)> callback
) override
{}
57 std::string
MimeType() override
{ return mime_type_
; }
58 bool HasMojoMagic() override
{ return false; }
59 bool PeekFirstLine(std::string
* line
) override
{ return false; }
63 const std::string mime_type_
;
65 DISALLOW_COPY_AND_ASSIGN(TestMimeTypeFetcher
);
69 TestContext() : num_impls(0), num_loader_deletes(0) {}
70 std::string last_test_string
;
72 int num_loader_deletes
;
75 void QuitClosure(bool* value
) {
77 base::MessageLoop::current()->QuitWhenIdle();
80 class TestServiceImpl
: public TestService
{
82 TestServiceImpl(TestContext
* context
, InterfaceRequest
<TestService
> request
)
83 : context_(context
), binding_(this, request
.Pass()) {
84 ++context_
->num_impls
;
87 ~TestServiceImpl() override
{
88 --context_
->num_impls
;
89 if (!base::MessageLoop::current()->is_running())
91 base::MessageLoop::current()->Quit();
94 // TestService implementation:
95 void Test(const String
& test_string
,
96 const Callback
<void()>& callback
) override
{
97 context_
->last_test_string
= test_string
;
102 TestContext
* context_
;
103 StrongBinding
<TestService
> binding_
;
108 explicit TestClient(TestServicePtr service
)
109 : service_(service
.Pass()), quit_after_ack_(false) {}
113 base::MessageLoop::current()->Quit();
116 void Test(const std::string
& test_string
) {
117 quit_after_ack_
= true;
118 service_
->Test(test_string
,
119 base::Bind(&TestClient::AckTest
, base::Unretained(this)));
123 TestServicePtr service_
;
124 bool quit_after_ack_
;
125 DISALLOW_COPY_AND_ASSIGN(TestClient
);
128 class TestContentHandler
: public ContentHandler
, public ApplicationDelegate
{
130 TestContentHandler(ApplicationConnection
* connection
,
131 InterfaceRequest
<ContentHandler
> request
)
132 : binding_(this, request
.Pass()) {}
135 void StartApplication(InterfaceRequest
<Application
> application_request
,
136 URLResponsePtr response
) override
{
137 apps_
.push_back(new ApplicationImpl(this, application_request
.Pass()));
141 StrongBinding
<ContentHandler
> binding_
;
142 ScopedVector
<ApplicationImpl
> apps_
;
144 DISALLOW_COPY_AND_ASSIGN(TestContentHandler
);
147 class TestApplicationLoader
: public ApplicationLoader
,
148 public ApplicationDelegate
,
149 public InterfaceFactory
<TestService
>,
150 public InterfaceFactory
<ContentHandler
> {
152 TestApplicationLoader()
153 : context_(nullptr), num_loads_(0), create_content_handler_(false) {}
155 ~TestApplicationLoader() override
{
157 ++context_
->num_loader_deletes
;
161 void set_create_content_handler(bool value
) {
162 create_content_handler_
= true;
165 void set_context(TestContext
* context
) { context_
= context
; }
166 int num_loads() const { return num_loads_
; }
167 const GURL
& last_requestor_url() const { return last_requestor_url_
; }
170 // ApplicationLoader implementation.
171 void Load(const GURL
& url
,
172 InterfaceRequest
<Application
> application_request
) override
{
174 test_app_
.reset(new ApplicationImpl(this, application_request
.Pass()));
177 // ApplicationDelegate implementation.
178 bool ConfigureIncomingConnection(ApplicationConnection
* connection
) override
{
179 connection
->AddService
<TestService
>(this);
180 if (create_content_handler_
)
181 connection
->AddService
<ContentHandler
>(this);
182 last_requestor_url_
= GURL(connection
->GetRemoteApplicationURL());
186 // InterfaceFactory<TestService> implementation.
187 void Create(ApplicationConnection
* connection
,
188 InterfaceRequest
<TestService
> request
) override
{
189 new TestServiceImpl(context_
, request
.Pass());
192 // InterfaceFactory<ContentHandler> implementation.
193 void Create(ApplicationConnection
* connection
,
194 InterfaceRequest
<ContentHandler
> request
) override
{
195 new TestContentHandler(connection
, request
.Pass());
198 scoped_ptr
<ApplicationImpl
> test_app_
;
199 TestContext
* context_
;
201 GURL last_requestor_url_
;
202 bool create_content_handler_
;
204 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader
);
207 class ClosingApplicationLoader
: public ApplicationLoader
{
209 // ApplicationLoader implementation.
210 void Load(const GURL
& url
,
211 InterfaceRequest
<Application
> application_request
) override
{}
214 class TesterContext
{
216 explicit TesterContext(base::MessageLoop
* loop
)
222 tester_called_quit_(false),
223 a_called_quit_(false),
226 void IncrementNumBCalls() {
227 base::AutoLock
lock(lock_
);
231 void IncrementNumCCalls() {
232 base::AutoLock
lock(lock_
);
236 void IncrementNumADeletes() {
237 base::AutoLock
lock(lock_
);
241 void IncrementNumBDeletes() {
242 base::AutoLock
lock(lock_
);
246 void IncrementNumCDeletes() {
247 base::AutoLock
lock(lock_
);
251 void set_tester_called_quit() {
252 base::AutoLock
lock(lock_
);
253 tester_called_quit_
= true;
256 void set_a_called_quit() {
257 base::AutoLock
lock(lock_
);
258 a_called_quit_
= true;
262 base::AutoLock
lock(lock_
);
266 base::AutoLock
lock(lock_
);
269 int num_a_deletes() {
270 base::AutoLock
lock(lock_
);
271 return num_a_deletes_
;
273 int num_b_deletes() {
274 base::AutoLock
lock(lock_
);
275 return num_b_deletes_
;
277 int num_c_deletes() {
278 base::AutoLock
lock(lock_
);
279 return num_c_deletes_
;
281 bool tester_called_quit() {
282 base::AutoLock
lock(lock_
);
283 return tester_called_quit_
;
285 bool a_called_quit() {
286 base::AutoLock
lock(lock_
);
287 return a_called_quit_
;
291 loop_
->PostTask(FROM_HERE
, base::MessageLoop::QuitWhenIdleClosure());
295 // lock_ protects all members except for loop_ which must be unchanged for the
296 // lifetime of this class.
303 bool tester_called_quit_
;
306 base::MessageLoop
* loop_
;
309 // Used to test that the requestor url will be correctly passed.
310 class TestAImpl
: public TestA
{
312 TestAImpl(ApplicationImpl
* app_impl
,
313 TesterContext
* test_context
,
314 InterfaceRequest
<TestA
> request
)
315 : test_context_(test_context
), binding_(this, request
.Pass()) {
316 mojo::URLRequestPtr
request2(mojo::URLRequest::New());
317 request2
->url
= mojo::String::From(kTestBURLString
);
318 connection_
= app_impl
->ConnectToApplication(request2
.Pass());
319 connection_
->ConnectToService(&b_
);
322 ~TestAImpl() override
{
323 test_context_
->IncrementNumADeletes();
324 if (base::MessageLoop::current()->is_running())
329 void CallB() override
{
330 b_
->B(base::Bind(&TestAImpl::Quit
, base::Unretained(this)));
333 void CallCFromB() override
{
334 b_
->CallC(base::Bind(&TestAImpl::Quit
, base::Unretained(this)));
338 base::MessageLoop::current()->Quit();
339 test_context_
->set_a_called_quit();
340 test_context_
->QuitSoon();
343 scoped_ptr
<ApplicationConnection
> connection_
;
344 TesterContext
* test_context_
;
346 StrongBinding
<TestA
> binding_
;
349 class TestBImpl
: public TestB
{
351 TestBImpl(ApplicationConnection
* connection
,
352 TesterContext
* test_context
,
353 InterfaceRequest
<TestB
> request
)
354 : test_context_(test_context
), binding_(this, request
.Pass()) {
355 connection
->ConnectToService(&c_
);
358 ~TestBImpl() override
{
359 test_context_
->IncrementNumBDeletes();
360 if (base::MessageLoop::current()->is_running())
361 base::MessageLoop::current()->Quit();
362 test_context_
->QuitSoon();
366 void B(const Callback
<void()>& callback
) override
{
367 test_context_
->IncrementNumBCalls();
371 void CallC(const Callback
<void()>& callback
) override
{
372 test_context_
->IncrementNumBCalls();
376 TesterContext
* test_context_
;
378 StrongBinding
<TestB
> binding_
;
381 class TestCImpl
: public TestC
{
383 TestCImpl(ApplicationConnection
* connection
,
384 TesterContext
* test_context
,
385 InterfaceRequest
<TestC
> request
)
386 : test_context_(test_context
), binding_(this, request
.Pass()) {}
388 ~TestCImpl() override
{ test_context_
->IncrementNumCDeletes(); }
391 void C(const Callback
<void()>& callback
) override
{
392 test_context_
->IncrementNumCCalls();
396 TesterContext
* test_context_
;
397 StrongBinding
<TestC
> binding_
;
400 class Tester
: public ApplicationDelegate
,
401 public ApplicationLoader
,
402 public InterfaceFactory
<TestA
>,
403 public InterfaceFactory
<TestB
>,
404 public InterfaceFactory
<TestC
> {
406 Tester(TesterContext
* context
, const std::string
& requestor_url
)
407 : context_(context
), requestor_url_(requestor_url
) {}
408 ~Tester() override
{}
411 void Load(const GURL
& url
,
412 InterfaceRequest
<Application
> application_request
) override
{
413 app_
.reset(new ApplicationImpl(this, application_request
.Pass()));
416 bool ConfigureIncomingConnection(ApplicationConnection
* connection
) override
{
417 if (!requestor_url_
.empty() &&
418 requestor_url_
!= connection
->GetRemoteApplicationURL()) {
419 context_
->set_tester_called_quit();
420 context_
->QuitSoon();
421 base::MessageLoop::current()->Quit();
424 // If we're coming from A, then add B, otherwise A.
425 if (connection
->GetRemoteApplicationURL() == kTestAURLString
)
426 connection
->AddService
<TestB
>(this);
428 connection
->AddService
<TestA
>(this);
432 bool ConfigureOutgoingConnection(ApplicationConnection
* connection
) override
{
433 // If we're connecting to B, then add C.
434 if (connection
->GetRemoteApplicationURL() == kTestBURLString
)
435 connection
->AddService
<TestC
>(this);
439 void Create(ApplicationConnection
* connection
,
440 InterfaceRequest
<TestA
> request
) override
{
441 a_bindings_
.push_back(new TestAImpl(app_
.get(), context_
, request
.Pass()));
444 void Create(ApplicationConnection
* connection
,
445 InterfaceRequest
<TestB
> request
) override
{
446 new TestBImpl(connection
, context_
, request
.Pass());
449 void Create(ApplicationConnection
* connection
,
450 InterfaceRequest
<TestC
> request
) override
{
451 new TestCImpl(connection
, context_
, request
.Pass());
454 TesterContext
* context_
;
455 scoped_ptr
<ApplicationImpl
> app_
;
456 std::string requestor_url_
;
457 ScopedVector
<TestAImpl
> a_bindings_
;
460 class AMTestPackageManager
: public TestPackageManager
{
462 AMTestPackageManager()
463 : create_test_fetcher_(false),
465 mime_type_(kTestMimeType
) {}
466 ~AMTestPackageManager() override
{}
468 void set_create_test_fetcher(bool create_test_fetcher
) {
469 create_test_fetcher_
= create_test_fetcher
;
472 void set_fetcher_url(const GURL
& url
) { fetcher_url_
= url
; }
474 void set_mime_type(const std::string
& mime_type
) { mime_type_
= mime_type
; }
476 // TestPackageManager:
477 void FetchRequest(URLRequestPtr request
,
478 const Fetcher::FetchCallback
& loader_callback
) override
{
479 if (create_test_fetcher_
)
480 new TestMimeTypeFetcher(loader_callback
, fetcher_url_
, mime_type_
);
484 bool create_test_fetcher_
;
486 std::string mime_type_
;
488 DISALLOW_COPY_AND_ASSIGN(AMTestPackageManager
);
491 class ApplicationManagerTest
: public testing::Test
{
493 ApplicationManagerTest() : tester_context_(&loop_
) {}
495 ~ApplicationManagerTest() override
{}
497 void SetUp() override
{
498 application_manager_
.reset(new ApplicationManager(
499 make_scoped_ptr(new AMTestPackageManager
)));
500 test_loader_
= new TestApplicationLoader
;
501 test_loader_
->set_context(&context_
);
502 application_manager_
->set_default_loader(
503 scoped_ptr
<ApplicationLoader
>(test_loader_
));
505 TestServicePtr service_proxy
;
506 ConnectToService(application_manager_
.get(), GURL(kTestURLString
),
508 test_client_
.reset(new TestClient(service_proxy
.Pass()));
511 void TearDown() override
{
512 test_client_
.reset();
513 application_manager_
.reset();
516 void AddLoaderForURL(const GURL
& url
, const std::string
& requestor_url
) {
517 application_manager_
->SetLoaderForURL(
518 make_scoped_ptr(new Tester(&tester_context_
, requestor_url
)), url
);
521 bool HasRunningInstanceForURL(const GURL
& url
) {
522 ApplicationManager::TestAPI
manager_test_api(application_manager_
.get());
523 return manager_test_api
.HasRunningInstanceForURL(url
);
527 base::ShadowingAtExitManager at_exit_
;
528 TestApplicationLoader
* test_loader_
;
529 TesterContext tester_context_
;
530 TestContext context_
;
531 base::MessageLoop loop_
;
532 scoped_ptr
<TestClient
> test_client_
;
533 scoped_ptr
<ApplicationManager
> application_manager_
;
534 DISALLOW_COPY_AND_ASSIGN(ApplicationManagerTest
);
537 TEST_F(ApplicationManagerTest
, Basic
) {
538 test_client_
->Test("test");
540 EXPECT_EQ(std::string("test"), context_
.last_test_string
);
543 TEST_F(ApplicationManagerTest
, ClientError
) {
544 test_client_
->Test("test");
545 EXPECT_TRUE(HasRunningInstanceForURL(GURL(kTestURLString
)));
547 EXPECT_EQ(1, context_
.num_impls
);
548 test_client_
.reset();
550 EXPECT_EQ(0, context_
.num_impls
);
551 EXPECT_TRUE(HasRunningInstanceForURL(GURL(kTestURLString
)));
554 TEST_F(ApplicationManagerTest
, Deletes
) {
556 ApplicationManager
am(make_scoped_ptr(new AMTestPackageManager
));
557 TestApplicationLoader
* default_loader
= new TestApplicationLoader
;
558 default_loader
->set_context(&context_
);
559 TestApplicationLoader
* url_loader1
= new TestApplicationLoader
;
560 TestApplicationLoader
* url_loader2
= new TestApplicationLoader
;
561 url_loader1
->set_context(&context_
);
562 url_loader2
->set_context(&context_
);
563 am
.set_default_loader(scoped_ptr
<ApplicationLoader
>(default_loader
));
564 am
.SetLoaderForURL(scoped_ptr
<ApplicationLoader
>(url_loader1
),
566 am
.SetLoaderForURL(scoped_ptr
<ApplicationLoader
>(url_loader2
),
569 EXPECT_EQ(3, context_
.num_loader_deletes
);
572 // Test for SetLoaderForURL() & set_default_loader().
573 TEST_F(ApplicationManagerTest
, SetLoaders
) {
574 TestApplicationLoader
* default_loader
= new TestApplicationLoader
;
575 TestApplicationLoader
* url_loader
= new TestApplicationLoader
;
576 application_manager_
->set_default_loader(
577 scoped_ptr
<ApplicationLoader
>(default_loader
));
578 application_manager_
->SetLoaderForURL(
579 scoped_ptr
<ApplicationLoader
>(url_loader
), GURL("test:test1"));
581 // test::test1 should go to url_loader.
582 TestServicePtr test_service
;
583 ConnectToService(application_manager_
.get(), GURL("test:test1"),
585 EXPECT_EQ(1, url_loader
->num_loads());
586 EXPECT_EQ(0, default_loader
->num_loads());
588 // http::test1 should go to default loader.
589 ConnectToService(application_manager_
.get(), GURL("http:test1"),
591 EXPECT_EQ(1, url_loader
->num_loads());
592 EXPECT_EQ(1, default_loader
->num_loads());
595 // Confirm that the url of a service is correctly passed to another service that
597 TEST_F(ApplicationManagerTest
, ACallB
) {
598 // Any url can load a.
599 AddLoaderForURL(GURL(kTestAURLString
), std::string());
601 // Only a can load b.
602 AddLoaderForURL(GURL(kTestBURLString
), kTestAURLString
);
605 ConnectToService(application_manager_
.get(), GURL(kTestAURLString
), &a
);
608 EXPECT_EQ(1, tester_context_
.num_b_calls());
609 EXPECT_TRUE(tester_context_
.a_called_quit());
612 // A calls B which calls C.
613 TEST_F(ApplicationManagerTest
, BCallC
) {
614 // Any url can load a.
615 AddLoaderForURL(GURL(kTestAURLString
), std::string());
617 // Only a can load b.
618 AddLoaderForURL(GURL(kTestBURLString
), kTestAURLString
);
621 ConnectToService(application_manager_
.get(), GURL(kTestAURLString
), &a
);
625 EXPECT_EQ(1, tester_context_
.num_b_calls());
626 EXPECT_EQ(1, tester_context_
.num_c_calls());
627 EXPECT_TRUE(tester_context_
.a_called_quit());
630 // Confirm that a service impl will be deleted if the app that connected to
632 TEST_F(ApplicationManagerTest
, BDeleted
) {
633 AddLoaderForURL(GURL(kTestAURLString
), std::string());
634 AddLoaderForURL(GURL(kTestBURLString
), std::string());
637 ConnectToService(application_manager_
.get(), GURL(kTestAURLString
), &a
);
643 application_manager_
->SetLoaderForURL(scoped_ptr
<ApplicationLoader
>(),
644 GURL(kTestAURLString
));
647 EXPECT_EQ(1, tester_context_
.num_b_deletes());
650 // Confirm that the url of a service is correctly passed to another service that
651 // it loads, and that it can be rejected.
652 TEST_F(ApplicationManagerTest
, ANoLoadB
) {
653 // Any url can load a.
654 AddLoaderForURL(GURL(kTestAURLString
), std::string());
656 // Only c can load b, so this will fail.
657 AddLoaderForURL(GURL(kTestBURLString
), "test:TestC");
660 ConnectToService(application_manager_
.get(), GURL(kTestAURLString
), &a
);
663 EXPECT_EQ(0, tester_context_
.num_b_calls());
665 EXPECT_FALSE(tester_context_
.a_called_quit());
666 EXPECT_TRUE(tester_context_
.tester_called_quit());
669 TEST_F(ApplicationManagerTest
, NoServiceNoLoad
) {
670 AddLoaderForURL(GURL(kTestAURLString
), std::string());
672 // There is no TestC service implementation registered with
673 // ApplicationManager, so this cannot succeed (but also shouldn't crash).
675 ConnectToService(application_manager_
.get(), GURL(kTestAURLString
), &c
);
676 c
.set_connection_error_handler(
677 []() { base::MessageLoop::current()->QuitWhenIdle(); });
680 EXPECT_TRUE(c
.encountered_error());
683 TEST_F(ApplicationManagerTest
, TestEndApplicationClosure
) {
684 ClosingApplicationLoader
* loader
= new ClosingApplicationLoader();
685 application_manager_
->SetLoaderForURL(
686 scoped_ptr
<ApplicationLoader
>(loader
), GURL("test:test"));
689 scoped_ptr
<ConnectToApplicationParams
> params(new ConnectToApplicationParams
);
690 params
->SetTargetURL(GURL("test:test"));
691 params
->set_on_application_end(
692 base::Bind(&QuitClosure
, base::Unretained(&called
)));
693 application_manager_
->ConnectToApplication(params
.Pass());
698 TEST(ApplicationManagerTest2
, ContentHandlerConnectionGetsRequestorURL
) {
699 const GURL
content_handler_url("http://test.content.handler");
700 const GURL
requestor_url("http://requestor.url");
701 TestContext test_context
;
702 base::MessageLoop loop
;
703 scoped_ptr
<AMTestPackageManager
> test_package_manager(
704 new AMTestPackageManager
);
705 test_package_manager
->set_create_test_fetcher(true);
706 test_package_manager
->RegisterContentHandler(kTestMimeType
,
707 content_handler_url
);
708 ApplicationManager
application_manager(test_package_manager
.Pass());
709 application_manager
.set_default_loader(nullptr);
711 TestApplicationLoader
* loader
= new TestApplicationLoader
;
712 loader
->set_context(&test_context
);
713 application_manager
.SetLoaderForURL(scoped_ptr
<ApplicationLoader
>(loader
),
714 content_handler_url
);
717 scoped_ptr
<ConnectToApplicationParams
> params(new ConnectToApplicationParams
);
718 params
->set_source(Identity(requestor_url
));
719 params
->SetTargetURL(GURL("test:test"));
720 params
->set_on_application_end(
721 base::Bind(&QuitClosure
, base::Unretained(&called
)));
722 application_manager
.ConnectToApplication(params
.Pass());
726 ASSERT_EQ(1, loader
->num_loads());
727 EXPECT_EQ(requestor_url
, loader
->last_requestor_url());
730 TEST_F(ApplicationManagerTest
, SameIdentityShouldNotCauseDuplicateLoad
) {
731 // 1 because ApplicationManagerTest connects once at startup.
732 EXPECT_EQ(1, test_loader_
->num_loads());
734 TestServicePtr test_service
;
735 ConnectToService(application_manager_
.get(),
736 GURL("http://www.example.org/abc?def"), &test_service
);
737 EXPECT_EQ(2, test_loader_
->num_loads());
739 // Exactly the same URL as above.
740 ConnectToService(application_manager_
.get(),
741 GURL("http://www.example.org/abc?def"), &test_service
);
742 EXPECT_EQ(2, test_loader_
->num_loads());
744 // The same identity as the one above because only the query string is
746 ConnectToService(application_manager_
.get(),
747 GURL("http://www.example.org/abc"), &test_service
);
748 EXPECT_EQ(2, test_loader_
->num_loads());
750 // A different identity because the path is different.
751 ConnectToService(application_manager_
.get(),
752 GURL("http://www.example.org/another_path"), &test_service
);
753 EXPECT_EQ(3, test_loader_
->num_loads());
755 // A different identity because the domain is different.
756 ConnectToService(application_manager_
.get(),
757 GURL("http://www.another_domain.org/abc"), &test_service
);
758 EXPECT_EQ(4, test_loader_
->num_loads());
761 TEST(ApplicationManagerTest2
,
762 MultipleConnectionsToContentHandlerGetSameContentHandlerId
) {
763 base::MessageLoop loop
;
764 const GURL
content_handler_url("http://test.content.handler");
765 const GURL
requestor_url("http://requestor.url");
766 TestContext test_context
;
767 scoped_ptr
<AMTestPackageManager
> test_package_manager(
768 new AMTestPackageManager
);
769 test_package_manager
->set_fetcher_url(GURL("test:test"));
770 test_package_manager
->set_create_test_fetcher(true);
771 test_package_manager
->RegisterContentHandler(kTestMimeType
,
772 content_handler_url
);
773 ApplicationManager
application_manager(test_package_manager
.Pass());
774 application_manager
.set_default_loader(nullptr);
776 TestApplicationLoader
* content_handler_loader
= new TestApplicationLoader
;
777 content_handler_loader
->set_create_content_handler(true);
778 content_handler_loader
->set_context(&test_context
);
779 application_manager
.SetLoaderForURL(
780 scoped_ptr
<ApplicationLoader
>(content_handler_loader
),
781 content_handler_url
);
783 uint32_t content_handler_id
;
785 base::RunLoop run_loop
;
786 scoped_ptr
<ConnectToApplicationParams
> params(
787 new ConnectToApplicationParams
);
788 params
->set_source(Identity(requestor_url
));
789 params
->SetTargetURL(GURL("test:test"));
790 params
->set_connect_callback([&content_handler_id
, &run_loop
](uint32_t t
) {
791 content_handler_id
= t
;
794 application_manager
.ConnectToApplication(params
.Pass());
796 EXPECT_NE(Shell::kInvalidContentHandlerID
, content_handler_id
);
799 uint32_t content_handler_id2
;
801 base::RunLoop run_loop
;
802 scoped_ptr
<ConnectToApplicationParams
> params(
803 new ConnectToApplicationParams
);
804 params
->set_source(Identity(requestor_url
));
805 params
->SetTargetURL(GURL("test:test"));
806 params
->set_connect_callback([&content_handler_id2
, &run_loop
](uint32_t t
) {
807 content_handler_id2
= t
;
810 application_manager
.ConnectToApplication(params
.Pass());
812 EXPECT_NE(Shell::kInvalidContentHandlerID
, content_handler_id2
);
814 EXPECT_EQ(content_handler_id
, content_handler_id2
);
817 TEST(ApplicationManagerTest2
, DifferedContentHandlersGetDifferentIDs
) {
818 base::MessageLoop loop
;
819 const GURL
content_handler_url("http://test.content.handler");
820 const GURL
requestor_url("http://requestor.url");
821 TestContext test_context
;
822 AMTestPackageManager
* test_package_manager
= new AMTestPackageManager
;
823 test_package_manager
->set_fetcher_url(GURL("test:test"));
824 test_package_manager
->set_create_test_fetcher(true);
825 test_package_manager
->RegisterContentHandler(kTestMimeType
,
826 content_handler_url
);
827 ApplicationManager
application_manager(make_scoped_ptr(test_package_manager
));
828 application_manager
.set_default_loader(nullptr);
830 TestApplicationLoader
* content_handler_loader
= new TestApplicationLoader
;
831 content_handler_loader
->set_create_content_handler(true);
832 content_handler_loader
->set_context(&test_context
);
833 application_manager
.SetLoaderForURL(
834 scoped_ptr
<ApplicationLoader
>(content_handler_loader
),
835 content_handler_url
);
837 uint32_t content_handler_id
;
839 base::RunLoop run_loop
;
840 scoped_ptr
<ConnectToApplicationParams
> params(
841 new ConnectToApplicationParams
);
842 params
->set_source(Identity(requestor_url
));
843 params
->SetTargetURL(GURL("test:test"));
844 params
->set_connect_callback([&content_handler_id
, &run_loop
](uint32_t t
) {
845 content_handler_id
= t
;
848 application_manager
.ConnectToApplication(params
.Pass());
850 EXPECT_NE(Shell::kInvalidContentHandlerID
, content_handler_id
);
853 const std::string
mime_type2("test/mime-type2");
854 const GURL
content_handler_url2("http://test.content2.handler");
855 test_package_manager
->set_fetcher_url(GURL("test2:test2"));
856 test_package_manager
->set_mime_type(mime_type2
);
857 test_package_manager
->RegisterContentHandler(mime_type2
,
858 content_handler_url2
);
860 TestApplicationLoader
* content_handler_loader2
= new TestApplicationLoader
;
861 content_handler_loader
->set_create_content_handler(true);
862 content_handler_loader
->set_context(&test_context
);
863 application_manager
.SetLoaderForURL(
864 scoped_ptr
<ApplicationLoader
>(content_handler_loader2
),
865 content_handler_url2
);
867 uint32_t content_handler_id2
;
869 base::RunLoop run_loop
;
870 scoped_ptr
<ConnectToApplicationParams
> params(
871 new ConnectToApplicationParams
);
872 params
->set_source(Identity(requestor_url
));
873 params
->SetTargetURL(GURL("test2:test2"));
874 params
->set_connect_callback([&content_handler_id2
, &run_loop
](uint32_t t
) {
875 content_handler_id2
= t
;
878 application_manager
.ConnectToApplication(params
.Pass());
880 EXPECT_NE(Shell::kInvalidContentHandlerID
, content_handler_id2
);
882 EXPECT_NE(content_handler_id
, content_handler_id2
);
885 TEST_F(ApplicationManagerTest
,
886 ConnectWithNoContentHandlerGetsInvalidContentHandlerId
) {
887 application_manager_
->SetLoaderForURL(
888 scoped_ptr
<ApplicationLoader
>(new TestApplicationLoader
),
891 uint32_t content_handler_id
= 1u;
892 scoped_ptr
<ConnectToApplicationParams
> params(new ConnectToApplicationParams
);
893 params
->SetTargetURL(GURL("test:test"));
894 params
->set_connect_callback(
895 [&content_handler_id
](uint32_t t
) { content_handler_id
= t
; });
896 application_manager_
->ConnectToApplication(params
.Pass());
897 EXPECT_EQ(0u, content_handler_id
);