[sql] Record memory usage at various periods after startup.
[chromium-blink-merge.git] / mojo / shell / application_manager_unittest.cc
blobe4bfb3759589ac91ae0c6fc14aeb410e341bf8f9
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"
6 #include "base/bind.h"
7 #include "base/macros.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/message_loop/message_loop.h"
10 #include "mojo/application/public/cpp/application_connection.h"
11 #include "mojo/application/public/cpp/application_delegate.h"
12 #include "mojo/application/public/cpp/application_impl.h"
13 #include "mojo/application/public/cpp/interface_factory.h"
14 #include "mojo/application/public/interfaces/service_provider.mojom.h"
15 #include "mojo/public/cpp/bindings/strong_binding.h"
16 #include "mojo/shell/application_loader.h"
17 #include "mojo/shell/application_manager.h"
18 #include "mojo/shell/fetcher.h"
19 #include "mojo/shell/test.mojom.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace mojo {
23 namespace shell {
24 namespace {
26 const char kTestURLString[] = "test:testService";
27 const char kTestAURLString[] = "test:TestA";
28 const char kTestBURLString[] = "test:TestB";
30 const char kTestMimeType[] = "test/mime-type";
32 class TestMimeTypeFetcher : public Fetcher {
33 public:
34 explicit TestMimeTypeFetcher(const FetchCallback& fetch_callback)
35 : Fetcher(fetch_callback), url_("xxx") {
36 loader_callback_.Run(make_scoped_ptr(this));
38 ~TestMimeTypeFetcher() override {}
40 // Fetcher:
41 const GURL& GetURL() const override { return url_; }
42 GURL GetRedirectURL() const override { return GURL("yyy"); }
43 GURL GetRedirectReferer() const override { return GURL(); }
44 URLResponsePtr AsURLResponse(base::TaskRunner* task_runner,
45 uint32_t skip) override {
46 return URLResponse::New().Pass();
48 void AsPath(
49 base::TaskRunner* task_runner,
50 base::Callback<void(const base::FilePath&, bool)> callback) override {}
51 std::string MimeType() override { return kTestMimeType; }
52 bool HasMojoMagic() override { return false; }
53 bool PeekFirstLine(std::string* line) override { return false; }
55 private:
56 const GURL url_;
58 DISALLOW_COPY_AND_ASSIGN(TestMimeTypeFetcher);
61 struct TestContext {
62 TestContext() : num_impls(0), num_loader_deletes(0) {}
63 std::string last_test_string;
64 int num_impls;
65 int num_loader_deletes;
68 void QuitClosure(bool* value) {
69 *value = true;
70 base::MessageLoop::current()->QuitWhenIdle();
73 class TestServiceImpl : public TestService {
74 public:
75 TestServiceImpl(TestContext* context, InterfaceRequest<TestService> request)
76 : context_(context), binding_(this, request.Pass()) {
77 ++context_->num_impls;
80 ~TestServiceImpl() override {
81 --context_->num_impls;
82 if (!base::MessageLoop::current()->is_running())
83 return;
84 base::MessageLoop::current()->Quit();
87 // TestService implementation:
88 void Test(const String& test_string,
89 const Callback<void()>& callback) override {
90 context_->last_test_string = test_string;
91 callback.Run();
94 private:
95 TestContext* context_;
96 StrongBinding<TestService> binding_;
99 class TestClient {
100 public:
101 explicit TestClient(TestServicePtr service)
102 : service_(service.Pass()), quit_after_ack_(false) {}
104 void AckTest() {
105 if (quit_after_ack_)
106 base::MessageLoop::current()->Quit();
109 void Test(const std::string& test_string) {
110 quit_after_ack_ = true;
111 service_->Test(test_string,
112 base::Bind(&TestClient::AckTest, base::Unretained(this)));
115 private:
116 TestServicePtr service_;
117 bool quit_after_ack_;
118 DISALLOW_COPY_AND_ASSIGN(TestClient);
121 class TestApplicationLoader : public ApplicationLoader,
122 public ApplicationDelegate,
123 public InterfaceFactory<TestService> {
124 public:
125 TestApplicationLoader() : context_(nullptr), num_loads_(0) {}
127 ~TestApplicationLoader() override {
128 if (context_)
129 ++context_->num_loader_deletes;
130 test_app_.reset();
133 void set_context(TestContext* context) { context_ = context; }
134 int num_loads() const { return num_loads_; }
135 const GURL& last_requestor_url() const { return last_requestor_url_; }
137 private:
138 // ApplicationLoader implementation.
139 void Load(const GURL& url,
140 InterfaceRequest<Application> application_request) override {
141 ++num_loads_;
142 test_app_.reset(new ApplicationImpl(this, application_request.Pass()));
145 // ApplicationDelegate implementation.
146 bool ConfigureIncomingConnection(ApplicationConnection* connection) override {
147 connection->AddService(this);
148 last_requestor_url_ = GURL(connection->GetRemoteApplicationURL());
149 return true;
152 // InterfaceFactory implementation.
153 void Create(ApplicationConnection* connection,
154 InterfaceRequest<TestService> request) override {
155 new TestServiceImpl(context_, request.Pass());
158 scoped_ptr<ApplicationImpl> test_app_;
159 TestContext* context_;
160 int num_loads_;
161 GURL last_requestor_url_;
163 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader);
166 class ClosingApplicationLoader : public ApplicationLoader {
167 private:
168 // ApplicationLoader implementation.
169 void Load(const GURL& url,
170 InterfaceRequest<Application> application_request) override {}
173 class TesterContext {
174 public:
175 explicit TesterContext(base::MessageLoop* loop)
176 : num_b_calls_(0),
177 num_c_calls_(0),
178 num_a_deletes_(0),
179 num_b_deletes_(0),
180 num_c_deletes_(0),
181 tester_called_quit_(false),
182 a_called_quit_(false),
183 loop_(loop) {}
185 void IncrementNumBCalls() {
186 base::AutoLock lock(lock_);
187 num_b_calls_++;
190 void IncrementNumCCalls() {
191 base::AutoLock lock(lock_);
192 num_c_calls_++;
195 void IncrementNumADeletes() {
196 base::AutoLock lock(lock_);
197 num_a_deletes_++;
200 void IncrementNumBDeletes() {
201 base::AutoLock lock(lock_);
202 num_b_deletes_++;
205 void IncrementNumCDeletes() {
206 base::AutoLock lock(lock_);
207 num_c_deletes_++;
210 void set_tester_called_quit() {
211 base::AutoLock lock(lock_);
212 tester_called_quit_ = true;
215 void set_a_called_quit() {
216 base::AutoLock lock(lock_);
217 a_called_quit_ = true;
220 int num_b_calls() {
221 base::AutoLock lock(lock_);
222 return num_b_calls_;
224 int num_c_calls() {
225 base::AutoLock lock(lock_);
226 return num_c_calls_;
228 int num_a_deletes() {
229 base::AutoLock lock(lock_);
230 return num_a_deletes_;
232 int num_b_deletes() {
233 base::AutoLock lock(lock_);
234 return num_b_deletes_;
236 int num_c_deletes() {
237 base::AutoLock lock(lock_);
238 return num_c_deletes_;
240 bool tester_called_quit() {
241 base::AutoLock lock(lock_);
242 return tester_called_quit_;
244 bool a_called_quit() {
245 base::AutoLock lock(lock_);
246 return a_called_quit_;
249 void QuitSoon() {
250 loop_->PostTask(FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
253 private:
254 // lock_ protects all members except for loop_ which must be unchanged for the
255 // lifetime of this class.
256 base::Lock lock_;
257 int num_b_calls_;
258 int num_c_calls_;
259 int num_a_deletes_;
260 int num_b_deletes_;
261 int num_c_deletes_;
262 bool tester_called_quit_;
263 bool a_called_quit_;
265 base::MessageLoop* loop_;
268 // Used to test that the requestor url will be correctly passed.
269 class TestAImpl : public TestA {
270 public:
271 TestAImpl(ApplicationImpl* app_impl,
272 TesterContext* test_context,
273 InterfaceRequest<TestA> request)
274 : test_context_(test_context), binding_(this, request.Pass()) {
275 mojo::URLRequestPtr request2(mojo::URLRequest::New());
276 request2->url = mojo::String::From(kTestBURLString);
277 connection_ = app_impl->ConnectToApplication(request2.Pass());
278 connection_->ConnectToService(&b_);
281 ~TestAImpl() override {
282 test_context_->IncrementNumADeletes();
283 if (base::MessageLoop::current()->is_running())
284 Quit();
287 private:
288 void CallB() override {
289 b_->B(base::Bind(&TestAImpl::Quit, base::Unretained(this)));
292 void CallCFromB() override {
293 b_->CallC(base::Bind(&TestAImpl::Quit, base::Unretained(this)));
296 void Quit() {
297 base::MessageLoop::current()->Quit();
298 test_context_->set_a_called_quit();
299 test_context_->QuitSoon();
302 scoped_ptr<ApplicationConnection> connection_;
303 TesterContext* test_context_;
304 TestBPtr b_;
305 StrongBinding<TestA> binding_;
308 class TestBImpl : public TestB {
309 public:
310 TestBImpl(ApplicationConnection* connection,
311 TesterContext* test_context,
312 InterfaceRequest<TestB> request)
313 : test_context_(test_context), binding_(this, request.Pass()) {
314 connection->ConnectToService(&c_);
317 ~TestBImpl() override {
318 test_context_->IncrementNumBDeletes();
319 if (base::MessageLoop::current()->is_running())
320 base::MessageLoop::current()->Quit();
321 test_context_->QuitSoon();
324 private:
325 void B(const Callback<void()>& callback) override {
326 test_context_->IncrementNumBCalls();
327 callback.Run();
330 void CallC(const Callback<void()>& callback) override {
331 test_context_->IncrementNumBCalls();
332 c_->C(callback);
335 TesterContext* test_context_;
336 TestCPtr c_;
337 StrongBinding<TestB> binding_;
340 class TestCImpl : public TestC {
341 public:
342 TestCImpl(ApplicationConnection* connection,
343 TesterContext* test_context,
344 InterfaceRequest<TestC> request)
345 : test_context_(test_context), binding_(this, request.Pass()) {}
347 ~TestCImpl() override { test_context_->IncrementNumCDeletes(); }
349 private:
350 void C(const Callback<void()>& callback) override {
351 test_context_->IncrementNumCCalls();
352 callback.Run();
355 TesterContext* test_context_;
356 StrongBinding<TestC> binding_;
359 class Tester : public ApplicationDelegate,
360 public ApplicationLoader,
361 public InterfaceFactory<TestA>,
362 public InterfaceFactory<TestB>,
363 public InterfaceFactory<TestC> {
364 public:
365 Tester(TesterContext* context, const std::string& requestor_url)
366 : context_(context), requestor_url_(requestor_url) {}
367 ~Tester() override {}
369 private:
370 void Load(const GURL& url,
371 InterfaceRequest<Application> application_request) override {
372 app_.reset(new ApplicationImpl(this, application_request.Pass()));
375 bool ConfigureIncomingConnection(ApplicationConnection* connection) override {
376 if (!requestor_url_.empty() &&
377 requestor_url_ != connection->GetRemoteApplicationURL()) {
378 context_->set_tester_called_quit();
379 context_->QuitSoon();
380 base::MessageLoop::current()->Quit();
381 return false;
383 // If we're coming from A, then add B, otherwise A.
384 if (connection->GetRemoteApplicationURL() == kTestAURLString)
385 connection->AddService<TestB>(this);
386 else
387 connection->AddService<TestA>(this);
388 return true;
391 bool ConfigureOutgoingConnection(ApplicationConnection* connection) override {
392 // If we're connecting to B, then add C.
393 if (connection->GetRemoteApplicationURL() == kTestBURLString)
394 connection->AddService<TestC>(this);
395 return true;
398 void Create(ApplicationConnection* connection,
399 InterfaceRequest<TestA> request) override {
400 a_bindings_.push_back(new TestAImpl(app_.get(), context_, request.Pass()));
403 void Create(ApplicationConnection* connection,
404 InterfaceRequest<TestB> request) override {
405 new TestBImpl(connection, context_, request.Pass());
408 void Create(ApplicationConnection* connection,
409 InterfaceRequest<TestC> request) override {
410 new TestCImpl(connection, context_, request.Pass());
413 TesterContext* context_;
414 scoped_ptr<ApplicationImpl> app_;
415 std::string requestor_url_;
416 ScopedVector<TestAImpl> a_bindings_;
419 class TestDelegate : public ApplicationManager::Delegate {
420 public:
421 TestDelegate() : create_test_fetcher_(false) {}
422 ~TestDelegate() override {}
424 void AddMapping(const GURL& from, const GURL& to) { mappings_[from] = to; }
426 void set_create_test_fetcher(bool create_test_fetcher) {
427 create_test_fetcher_ = create_test_fetcher;
430 // ApplicationManager::Delegate
431 GURL ResolveMappings(const GURL& url) override {
432 auto it = mappings_.find(url);
433 if (it != mappings_.end())
434 return it->second;
435 return url;
437 GURL ResolveMojoURL(const GURL& url) override {
438 GURL mapped_url = ResolveMappings(url);
439 // The shell automatically map mojo URLs.
440 if (mapped_url.scheme() == "mojo") {
441 url::Replacements<char> replacements;
442 replacements.SetScheme("file", url::Component(0, 4));
443 mapped_url = mapped_url.ReplaceComponents(replacements);
445 return mapped_url;
447 bool CreateFetcher(const GURL& url,
448 const Fetcher::FetchCallback& loader_callback) override {
449 if (!create_test_fetcher_)
450 return false;
451 new TestMimeTypeFetcher(loader_callback);
452 return true;
455 private:
456 std::map<GURL, GURL> mappings_;
457 bool create_test_fetcher_;
459 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
462 class ApplicationManagerTest : public testing::Test {
463 public:
464 ApplicationManagerTest() : tester_context_(&loop_) {}
466 ~ApplicationManagerTest() override {}
468 void SetUp() override {
469 application_manager_.reset(new ApplicationManager(&test_delegate_));
470 test_loader_ = new TestApplicationLoader;
471 test_loader_->set_context(&context_);
472 application_manager_->set_default_loader(
473 scoped_ptr<ApplicationLoader>(test_loader_));
475 TestServicePtr service_proxy;
476 application_manager_->ConnectToService(GURL(kTestURLString),
477 &service_proxy);
478 test_client_.reset(new TestClient(service_proxy.Pass()));
481 void TearDown() override {
482 test_client_.reset();
483 application_manager_.reset();
486 void AddLoaderForURL(const GURL& url, const std::string& requestor_url) {
487 application_manager_->SetLoaderForURL(
488 make_scoped_ptr(new Tester(&tester_context_, requestor_url)), url);
491 bool HasRunningInstanceForURL(const GURL& url) {
492 ApplicationManager::TestAPI manager_test_api(application_manager_.get());
493 return manager_test_api.HasRunningInstanceForURL(url);
496 protected:
497 base::ShadowingAtExitManager at_exit_;
498 TestDelegate test_delegate_;
499 TestApplicationLoader* test_loader_;
500 TesterContext tester_context_;
501 TestContext context_;
502 base::MessageLoop loop_;
503 scoped_ptr<TestClient> test_client_;
504 scoped_ptr<ApplicationManager> application_manager_;
505 DISALLOW_COPY_AND_ASSIGN(ApplicationManagerTest);
508 TEST_F(ApplicationManagerTest, Basic) {
509 test_client_->Test("test");
510 loop_.Run();
511 EXPECT_EQ(std::string("test"), context_.last_test_string);
514 // Confirm that url mappings are respected.
515 TEST_F(ApplicationManagerTest, URLMapping) {
516 ApplicationManager am(&test_delegate_);
517 GURL test_url("test:test");
518 GURL test_url2("test:test2");
519 test_delegate_.AddMapping(test_url, test_url2);
520 TestApplicationLoader* loader = new TestApplicationLoader;
521 loader->set_context(&context_);
522 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url2);
524 // Connext to the mapped url
525 TestServicePtr test_service;
526 am.ConnectToService(test_url, &test_service);
527 TestClient test_client(test_service.Pass());
528 test_client.Test("test");
529 loop_.Run();
532 // Connext to the target url
533 TestServicePtr test_service;
534 am.ConnectToService(test_url2, &test_service);
535 TestClient test_client(test_service.Pass());
536 test_client.Test("test");
537 loop_.Run();
541 TEST_F(ApplicationManagerTest, ClientError) {
542 test_client_->Test("test");
543 EXPECT_TRUE(HasRunningInstanceForURL(GURL(kTestURLString)));
544 loop_.Run();
545 EXPECT_EQ(1, context_.num_impls);
546 test_client_.reset();
547 loop_.Run();
548 EXPECT_EQ(0, context_.num_impls);
549 EXPECT_TRUE(HasRunningInstanceForURL(GURL(kTestURLString)));
552 TEST_F(ApplicationManagerTest, Deletes) {
554 ApplicationManager am(&test_delegate_);
555 TestApplicationLoader* default_loader = new TestApplicationLoader;
556 default_loader->set_context(&context_);
557 TestApplicationLoader* url_loader1 = new TestApplicationLoader;
558 TestApplicationLoader* url_loader2 = new TestApplicationLoader;
559 url_loader1->set_context(&context_);
560 url_loader2->set_context(&context_);
561 TestApplicationLoader* scheme_loader1 = new TestApplicationLoader;
562 TestApplicationLoader* scheme_loader2 = new TestApplicationLoader;
563 scheme_loader1->set_context(&context_);
564 scheme_loader2->set_context(&context_);
565 am.set_default_loader(scoped_ptr<ApplicationLoader>(default_loader));
566 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(url_loader1),
567 GURL("test:test1"));
568 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(url_loader2),
569 GURL("test:test1"));
570 am.SetLoaderForScheme(scoped_ptr<ApplicationLoader>(scheme_loader1),
571 "test");
572 am.SetLoaderForScheme(scoped_ptr<ApplicationLoader>(scheme_loader2),
573 "test");
575 EXPECT_EQ(5, context_.num_loader_deletes);
578 // Confirm that both urls and schemes can have their loaders explicitly set.
579 TEST_F(ApplicationManagerTest, SetLoaders) {
580 TestApplicationLoader* default_loader = new TestApplicationLoader;
581 TestApplicationLoader* url_loader = new TestApplicationLoader;
582 TestApplicationLoader* scheme_loader = new TestApplicationLoader;
583 application_manager_->set_default_loader(
584 scoped_ptr<ApplicationLoader>(default_loader));
585 application_manager_->SetLoaderForURL(
586 scoped_ptr<ApplicationLoader>(url_loader), GURL("test:test1"));
587 application_manager_->SetLoaderForScheme(
588 scoped_ptr<ApplicationLoader>(scheme_loader), "test");
590 // test::test1 should go to url_loader.
591 TestServicePtr test_service;
592 application_manager_->ConnectToService(GURL("test:test1"), &test_service);
593 EXPECT_EQ(1, url_loader->num_loads());
594 EXPECT_EQ(0, scheme_loader->num_loads());
595 EXPECT_EQ(0, default_loader->num_loads());
597 // test::test2 should go to scheme loader.
598 application_manager_->ConnectToService(GURL("test:test2"), &test_service);
599 EXPECT_EQ(1, url_loader->num_loads());
600 EXPECT_EQ(1, scheme_loader->num_loads());
601 EXPECT_EQ(0, default_loader->num_loads());
603 // http::test1 should go to default loader.
604 application_manager_->ConnectToService(GURL("http:test1"), &test_service);
605 EXPECT_EQ(1, url_loader->num_loads());
606 EXPECT_EQ(1, scheme_loader->num_loads());
607 EXPECT_EQ(1, default_loader->num_loads());
610 // Confirm that the url of a service is correctly passed to another service that
611 // it loads.
612 TEST_F(ApplicationManagerTest, ACallB) {
613 // Any url can load a.
614 AddLoaderForURL(GURL(kTestAURLString), std::string());
616 // Only a can load b.
617 AddLoaderForURL(GURL(kTestBURLString), kTestAURLString);
619 TestAPtr a;
620 application_manager_->ConnectToService(GURL(kTestAURLString), &a);
621 a->CallB();
622 loop_.Run();
623 EXPECT_EQ(1, tester_context_.num_b_calls());
624 EXPECT_TRUE(tester_context_.a_called_quit());
627 // A calls B which calls C.
628 TEST_F(ApplicationManagerTest, BCallC) {
629 // Any url can load a.
630 AddLoaderForURL(GURL(kTestAURLString), std::string());
632 // Only a can load b.
633 AddLoaderForURL(GURL(kTestBURLString), kTestAURLString);
635 TestAPtr a;
636 application_manager_->ConnectToService(GURL(kTestAURLString), &a);
637 a->CallCFromB();
638 loop_.Run();
640 EXPECT_EQ(1, tester_context_.num_b_calls());
641 EXPECT_EQ(1, tester_context_.num_c_calls());
642 EXPECT_TRUE(tester_context_.a_called_quit());
645 // Confirm that a service impl will be deleted if the app that connected to
646 // it goes away.
647 TEST_F(ApplicationManagerTest, BDeleted) {
648 AddLoaderForURL(GURL(kTestAURLString), std::string());
649 AddLoaderForURL(GURL(kTestBURLString), std::string());
651 TestAPtr a;
652 application_manager_->ConnectToService(GURL(kTestAURLString), &a);
654 a->CallB();
655 loop_.Run();
657 // Kills the a app.
658 application_manager_->SetLoaderForURL(scoped_ptr<ApplicationLoader>(),
659 GURL(kTestAURLString));
660 loop_.Run();
662 EXPECT_EQ(1, tester_context_.num_b_deletes());
665 // Confirm that the url of a service is correctly passed to another service that
666 // it loads, and that it can be rejected.
667 TEST_F(ApplicationManagerTest, ANoLoadB) {
668 // Any url can load a.
669 AddLoaderForURL(GURL(kTestAURLString), std::string());
671 // Only c can load b, so this will fail.
672 AddLoaderForURL(GURL(kTestBURLString), "test:TestC");
674 TestAPtr a;
675 application_manager_->ConnectToService(GURL(kTestAURLString), &a);
676 a->CallB();
677 loop_.Run();
678 EXPECT_EQ(0, tester_context_.num_b_calls());
680 EXPECT_FALSE(tester_context_.a_called_quit());
681 EXPECT_TRUE(tester_context_.tester_called_quit());
684 TEST_F(ApplicationManagerTest, NoServiceNoLoad) {
685 AddLoaderForURL(GURL(kTestAURLString), std::string());
687 // There is no TestC service implementation registered with
688 // ApplicationManager, so this cannot succeed (but also shouldn't crash).
689 TestCPtr c;
690 application_manager_->ConnectToService(GURL(kTestAURLString), &c);
691 c.set_connection_error_handler(
692 []() { base::MessageLoop::current()->QuitWhenIdle(); });
694 loop_.Run();
695 EXPECT_TRUE(c.encountered_error());
698 TEST_F(ApplicationManagerTest, MappedURLsShouldNotCauseDuplicateLoad) {
699 test_delegate_.AddMapping(GURL("foo:foo2"), GURL("foo:foo"));
700 // 1 because ApplicationManagerTest connects once at startup.
701 EXPECT_EQ(1, test_loader_->num_loads());
703 TestServicePtr test_service;
704 application_manager_->ConnectToService(GURL("foo:foo"), &test_service);
705 EXPECT_EQ(2, test_loader_->num_loads());
707 TestServicePtr test_service2;
708 application_manager_->ConnectToService(GURL("foo:foo2"), &test_service2);
709 EXPECT_EQ(2, test_loader_->num_loads());
711 TestServicePtr test_service3;
712 application_manager_->ConnectToService(GURL("bar:bar"), &test_service2);
713 EXPECT_EQ(3, test_loader_->num_loads());
716 TEST_F(ApplicationManagerTest, MappedURLsShouldWorkWithLoaders) {
717 TestApplicationLoader* custom_loader = new TestApplicationLoader;
718 TestContext context;
719 custom_loader->set_context(&context);
720 application_manager_->SetLoaderForURL(make_scoped_ptr(custom_loader),
721 GURL("mojo:foo"));
722 test_delegate_.AddMapping(GURL("mojo:foo2"), GURL("mojo:foo"));
724 TestServicePtr test_service;
725 application_manager_->ConnectToService(GURL("mojo:foo2"), &test_service);
726 EXPECT_EQ(1, custom_loader->num_loads());
727 custom_loader->set_context(nullptr);
729 EXPECT_TRUE(HasRunningInstanceForURL(GURL("mojo:foo2")));
730 EXPECT_FALSE(HasRunningInstanceForURL(GURL("mojo:foo")));
733 TEST_F(ApplicationManagerTest, TestQueryWithLoaders) {
734 TestApplicationLoader* url_loader = new TestApplicationLoader;
735 TestApplicationLoader* scheme_loader = new TestApplicationLoader;
736 application_manager_->SetLoaderForURL(
737 scoped_ptr<ApplicationLoader>(url_loader), GURL("test:test1"));
738 application_manager_->SetLoaderForScheme(
739 scoped_ptr<ApplicationLoader>(scheme_loader), "test");
741 // test::test1 should go to url_loader.
742 TestServicePtr test_service;
743 application_manager_->ConnectToService(GURL("test:test1?foo=bar"),
744 &test_service);
745 EXPECT_EQ(1, url_loader->num_loads());
746 EXPECT_EQ(0, scheme_loader->num_loads());
748 // test::test2 should go to scheme loader.
749 application_manager_->ConnectToService(GURL("test:test2?foo=bar"),
750 &test_service);
751 EXPECT_EQ(1, url_loader->num_loads());
752 EXPECT_EQ(1, scheme_loader->num_loads());
755 TEST_F(ApplicationManagerTest, TestEndApplicationClosure) {
756 ClosingApplicationLoader* loader = new ClosingApplicationLoader();
757 application_manager_->SetLoaderForScheme(
758 scoped_ptr<ApplicationLoader>(loader), "test");
760 bool called = false;
761 mojo::URLRequestPtr request(mojo::URLRequest::New());
762 request->url = mojo::String::From("test:test");
763 application_manager_->ConnectToApplication(
764 nullptr, request.Pass(), std::string(), GURL(), nullptr, nullptr,
765 GetPermissiveCapabilityFilter(),
766 base::Bind(&QuitClosure, base::Unretained(&called)));
767 loop_.Run();
768 EXPECT_TRUE(called);
771 TEST(ApplicationManagerTest2, ContentHandlerConnectionGetsRequestorURL) {
772 const GURL content_handler_url("http://test.content.handler");
773 const GURL requestor_url("http://requestor.url");
774 TestContext test_context;
775 base::MessageLoop loop;
776 TestDelegate test_delegate;
777 test_delegate.set_create_test_fetcher(true);
778 ApplicationManager application_manager(&test_delegate);
779 application_manager.set_default_loader(nullptr);
780 application_manager.RegisterContentHandler(kTestMimeType,
781 content_handler_url);
783 TestApplicationLoader* loader = new TestApplicationLoader;
784 loader->set_context(&test_context);
785 application_manager.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader),
786 content_handler_url);
788 bool called = false;
789 mojo::URLRequestPtr request(mojo::URLRequest::New());
790 request->url = mojo::String::From("test:test");
791 application_manager.ConnectToApplication(
792 nullptr, request.Pass(), std::string(), requestor_url, nullptr, nullptr,
793 GetPermissiveCapabilityFilter(),
794 base::Bind(&QuitClosure, base::Unretained(&called)));
795 loop.Run();
796 EXPECT_TRUE(called);
798 ASSERT_EQ(1, loader->num_loads());
799 EXPECT_EQ(requestor_url, loader->last_requestor_url());
802 } // namespace
803 } // namespace shell
804 } // namespace mojo