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 "mojo/public/bindings/allocation_scope.h"
6 #include "mojo/public/bindings/remote_ptr.h"
7 #include "mojo/public/environment/environment.h"
8 #include "mojo/public/shell/application.h"
9 #include "mojo/public/utility/run_loop.h"
10 #include "mojo/shell/service_manager.h"
11 #include "mojom/shell.h"
12 #include "mojom/test.h"
13 #include "testing/gtest/include/gtest/gtest.h"
19 const char kTestURLString
[] = "test:testService";
22 TestContext() : num_impls(0) {}
23 std::string last_test_string
;
27 class TestServiceImpl
:
28 public Service
<TestService
, TestServiceImpl
, TestContext
> {
32 virtual ~TestServiceImpl() {
33 --context()->num_impls
;
36 virtual void Test(const mojo::String
& test_string
) OVERRIDE
{
37 context()->last_test_string
= test_string
.To
<std::string
>();
41 void Initialize(ServiceFactory
<TestServiceImpl
, TestContext
>* service_factory
,
42 ScopedMessagePipeHandle client_handle
) {
43 Service
<TestService
, TestServiceImpl
, TestContext
>::Initialize(
44 service_factory
, client_handle
.Pass());
45 ++context()->num_impls
;
49 class TestClientImpl
: public TestClient
{
51 explicit TestClientImpl(ScopedTestServiceHandle service_handle
)
52 : service_(service_handle
.Pass(), this),
53 quit_after_ack_(false) {
56 virtual ~TestClientImpl() {}
58 virtual void AckTest() OVERRIDE
{
60 mojo::RunLoop::current()->Quit();
63 void Test(std::string test_string
) {
64 AllocationScope scope
;
65 quit_after_ack_
= true;
66 service_
->Test(mojo::String(test_string
));
70 RemotePtr
<TestService
> service_
;
72 DISALLOW_COPY_AND_ASSIGN(TestClientImpl
);
76 class ServiceManagerTest
: public testing::Test
,
77 public ServiceManager::Loader
{
79 ServiceManagerTest() {}
81 virtual ~ServiceManagerTest() {}
83 virtual void SetUp() OVERRIDE
{
84 GURL
test_url(kTestURLString
);
85 service_manager_
.reset(new ServiceManager
);
86 service_manager_
->SetLoaderForURL(this, test_url
);
88 InterfacePipe
<TestService
, AnyInterface
> pipe
;
89 test_client_
.reset(new TestClientImpl(pipe
.handle_to_self
.Pass()));
90 service_manager_
->Connect(test_url
, pipe
.handle_to_peer
.Pass());
93 virtual void TearDown() OVERRIDE
{
94 test_client_
.reset(NULL
);
95 test_app_
.reset(NULL
);
96 service_manager_
.reset(NULL
);
99 virtual void Load(const GURL
& url
,
100 ScopedShellHandle shell_handle
) OVERRIDE
{
101 test_app_
.reset(new Application(shell_handle
.Pass()));
102 test_app_
->AddServiceFactory(
103 new ServiceFactory
<TestServiceImpl
, TestContext
>(&context_
));
106 bool HasFactoryForTestURL() {
107 ServiceManager::TestAPI
manager_test_api(service_manager_
.get());
108 return manager_test_api
.HasFactoryForURL(GURL(kTestURLString
));
112 mojo::Environment env_
;
114 TestContext context_
;
115 scoped_ptr
<Application
> test_app_
;
116 scoped_ptr
<TestClientImpl
> test_client_
;
117 scoped_ptr
<ServiceManager
> service_manager_
;
118 DISALLOW_COPY_AND_ASSIGN(ServiceManagerTest
);
121 TEST_F(ServiceManagerTest
, Basic
) {
122 test_client_
->Test("test");
124 EXPECT_EQ(std::string("test"), context_
.last_test_string
);
127 TEST_F(ServiceManagerTest
, ClientError
) {
128 test_client_
->Test("test");
129 EXPECT_TRUE(HasFactoryForTestURL());
131 EXPECT_EQ(1, context_
.num_impls
);
132 test_client_
.reset(NULL
);
134 EXPECT_EQ(0, context_
.num_impls
);
135 EXPECT_FALSE(HasFactoryForTestURL());