Add ServiceManager::GetInstance() and change ServiceManager::Loader to ServiceLoader
[chromium-blink-merge.git] / mojo / service_manager / service_manager_unittest.cc
blob6941be65578ae270bfc686347e8b2cc1cf14d9a4
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/shell/shell.mojom.h"
10 #include "mojo/public/utility/run_loop.h"
11 #include "mojo/service_manager/service_loader.h"
12 #include "mojo/service_manager/service_manager.h"
13 #include "mojo/service_manager/test.mojom.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace mojo {
17 namespace {
19 const char kTestURLString[] = "test:testService";
21 struct TestContext {
22 TestContext() : num_impls(0) {}
23 std::string last_test_string;
24 int num_impls;
27 class TestServiceImpl :
28 public Service<TestService, TestServiceImpl, TestContext> {
29 public:
30 TestServiceImpl() {}
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>();
38 client()->AckTest();
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 {
50 public:
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 {
59 if (quit_after_ack_)
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));
69 private:
70 RemotePtr<TestService> service_;
71 bool quit_after_ack_;
72 DISALLOW_COPY_AND_ASSIGN(TestClientImpl);
74 } // namespace
76 class ServiceManagerTest : public testing::Test, public ServiceLoader {
77 public:
78 ServiceManagerTest() {}
80 virtual ~ServiceManagerTest() {}
82 virtual void SetUp() OVERRIDE {
83 GURL test_url(kTestURLString);
84 service_manager_.reset(new ServiceManager);
85 service_manager_->SetLoaderForURL(this, test_url);
87 InterfacePipe<TestService, AnyInterface> pipe;
88 test_client_.reset(new TestClientImpl(pipe.handle_to_self.Pass()));
89 service_manager_->Connect(test_url, pipe.handle_to_peer.Pass());
92 virtual void TearDown() OVERRIDE {
93 test_client_.reset(NULL);
94 test_app_.reset(NULL);
95 service_manager_.reset(NULL);
98 virtual void LoadService(ServiceManager* manager,
99 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));
111 protected:
112 mojo::Environment env_;
113 mojo::RunLoop loop_;
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");
123 loop_.Run();
124 EXPECT_EQ(std::string("test"), context_.last_test_string);
127 TEST_F(ServiceManagerTest, ClientError) {
128 test_client_->Test("test");
129 EXPECT_TRUE(HasFactoryForTestURL());
130 loop_.Run();
131 EXPECT_EQ(1, context_.num_impls);
132 test_client_.reset(NULL);
133 loop_.Run();
134 EXPECT_EQ(0, context_.num_impls);
135 EXPECT_FALSE(HasFactoryForTestURL());
137 } // namespace mojo