android webview envsetup: Set android_ndk_root and android_sdk_version to dummy values.
[chromium-blink-merge.git] / mojo / shell / service_manager_unittest.cc
blobac5d1ba1dac6c3ce95cbec83aaf4016c7f0ba9dc
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"
15 namespace mojo {
16 namespace shell {
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,
77 public ServiceManager::Loader {
78 public:
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));
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 shell
138 } // namespace mojo