| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_loop/message_loop.h" | |
| 6 #include "mojo/public/bindings/allocation_scope.h" | 5 #include "mojo/public/bindings/allocation_scope.h" |
| 7 #include "mojo/public/bindings/remote_ptr.h" | 6 #include "mojo/public/bindings/remote_ptr.h" |
| 7 #include "mojo/public/environment/environment.h" |
| 8 #include "mojo/public/shell/service.h" | 8 #include "mojo/public/shell/service.h" |
| 9 #include "mojo/public/utility/run_loop.h" |
| 9 #include "mojo/shell/service_connector.h" | 10 #include "mojo/shell/service_connector.h" |
| 10 #include "mojom/shell.h" | 11 #include "mojom/shell.h" |
| 11 #include "mojom/test.h" | 12 #include "mojom/test.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 namespace mojo { | 15 namespace mojo { |
| 15 namespace shell { | 16 namespace shell { |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 struct Context { | 19 const char kTestURLString[] = "test:testService"; |
| 20 |
| 21 struct TestContext { |
| 22 TestContext() : num_impls(0) {} |
| 19 std::string last_test_string; | 23 std::string last_test_string; |
| 24 int num_impls; |
| 20 }; | 25 }; |
| 21 | 26 |
| 22 class TestServiceImpl : public Service<TestService, TestServiceImpl, Context> { | 27 class TestServiceImpl : |
| 28 public Service<TestService, TestServiceImpl, TestContext> { |
| 23 public: | 29 public: |
| 30 TestServiceImpl() {} |
| 31 |
| 32 virtual ~TestServiceImpl() { |
| 33 --context()->num_impls; |
| 34 } |
| 35 |
| 24 virtual void Test(const mojo::String& test_string) OVERRIDE { | 36 virtual void Test(const mojo::String& test_string) OVERRIDE { |
| 25 context()->last_test_string = test_string.To<std::string>(); | 37 context()->last_test_string = test_string.To<std::string>(); |
| 26 client()->AckTest(); | 38 client()->AckTest(); |
| 27 } | 39 } |
| 40 |
| 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; |
| 46 } |
| 28 }; | 47 }; |
| 29 | 48 |
| 30 class TestClientImpl : public TestClient { | 49 class TestClientImpl : public TestClient { |
| 31 public: | 50 public: |
| 32 explicit TestClientImpl(ScopedMessagePipeHandle service_handle) | 51 explicit TestClientImpl(ScopedMessagePipeHandle service_handle) |
| 33 : service_(service_handle.Pass(), this), | 52 : service_(service_handle.Pass(), this), |
| 34 quit_after_ack_(false) { | 53 quit_after_ack_(false) { |
| 35 } | 54 } |
| 36 virtual ~TestClientImpl() { | 55 |
| 37 } | 56 virtual ~TestClientImpl() {} |
| 57 |
| 38 virtual void AckTest() OVERRIDE { | 58 virtual void AckTest() OVERRIDE { |
| 39 if (quit_after_ack_) | 59 if (quit_after_ack_) |
| 40 base::MessageLoop::current()->QuitNow(); | 60 mojo::RunLoop::current()->Quit(); |
| 41 } | 61 } |
| 62 |
| 42 void Test(std::string test_string) { | 63 void Test(std::string test_string) { |
| 43 AllocationScope scope; | 64 AllocationScope scope; |
| 44 quit_after_ack_ = true; | 65 quit_after_ack_ = true; |
| 45 service_->Test(mojo::String(test_string)); | 66 service_->Test(mojo::String(test_string)); |
| 46 } | 67 } |
| 68 |
| 69 private: |
| 47 RemotePtr<TestService> service_; | 70 RemotePtr<TestService> service_; |
| 48 bool quit_after_ack_; | 71 bool quit_after_ack_; |
| 72 DISALLOW_COPY_AND_ASSIGN(TestClientImpl); |
| 49 }; | 73 }; |
| 74 } // namespace |
| 50 | 75 |
| 51 class ServiceConnectorTest : public testing::Test, | 76 class ServiceConnectorTest : public testing::Test, |
| 52 public ServiceConnector::Loader { | 77 public ServiceConnector::Loader { |
| 53 public: | 78 public: |
| 54 ServiceConnectorTest() { | 79 ServiceConnectorTest() {} |
| 55 } | |
| 56 | 80 |
| 57 virtual ~ServiceConnectorTest() { | 81 virtual ~ServiceConnectorTest() {} |
| 58 } | |
| 59 | 82 |
| 60 virtual void SetUp() OVERRIDE { | 83 virtual void SetUp() OVERRIDE { |
| 61 GURL test_url("test:testService"); | 84 GURL test_url(kTestURLString); |
| 62 service_connector_.reset(new ServiceConnector); | 85 service_connector_.reset(new ServiceConnector); |
| 63 service_connector_->SetLoaderForURL(this, test_url); | 86 service_connector_->SetLoaderForURL(this, test_url); |
| 64 MessagePipe pipe; | 87 MessagePipe pipe; |
| 65 test_client_.reset(new TestClientImpl(pipe.handle0.Pass())); | 88 test_client_.reset(new TestClientImpl(pipe.handle0.Pass())); |
| 66 service_connector_->Connect(test_url, pipe.handle1.Pass()); | 89 service_connector_->Connect(test_url, pipe.handle1.Pass()); |
| 67 } | 90 } |
| 68 | 91 |
| 69 virtual void TearDown() OVERRIDE { | 92 virtual void TearDown() OVERRIDE { |
| 70 test_client_.reset(NULL); | 93 test_client_.reset(NULL); |
| 71 test_app_.reset(NULL); | 94 test_app_.reset(NULL); |
| 72 service_connector_.reset(NULL); | 95 service_connector_.reset(NULL); |
| 73 } | 96 } |
| 74 | 97 |
| 75 virtual void Load(const GURL& url, | 98 virtual void Load(const GURL& url, |
| 76 ScopedMessagePipeHandle shell_handle) OVERRIDE { | 99 ScopedMessagePipeHandle shell_handle) OVERRIDE { |
| 77 test_app_.reset(new ServiceFactory<TestServiceImpl, Context>( | 100 test_app_.reset(new ServiceFactory<TestServiceImpl, TestContext>( |
| 78 shell_handle.Pass(), &context_)); | 101 shell_handle.Pass(), &context_)); |
| 79 } | 102 } |
| 80 | 103 |
| 104 bool HasFactoryForTestURL() { |
| 105 ServiceConnector::TestAPI connector_test_api(service_connector_.get()); |
| 106 return connector_test_api.HasFactoryForURL(GURL(kTestURLString)); |
| 107 } |
| 108 |
| 81 protected: | 109 protected: |
| 82 base::MessageLoop loop_; | 110 mojo::Environment env_; |
| 83 Context context_; | 111 mojo::RunLoop loop_; |
| 84 scoped_ptr<ServiceFactory<TestServiceImpl, Context> > test_app_; | 112 TestContext context_; |
| 113 scoped_ptr<ServiceFactory<TestServiceImpl, TestContext> > test_app_; |
| 85 scoped_ptr<TestClientImpl> test_client_; | 114 scoped_ptr<TestClientImpl> test_client_; |
| 86 scoped_ptr<ServiceConnector> service_connector_; | 115 scoped_ptr<ServiceConnector> service_connector_; |
| 87 DISALLOW_COPY_AND_ASSIGN(ServiceConnectorTest); | 116 DISALLOW_COPY_AND_ASSIGN(ServiceConnectorTest); |
| 88 }; | 117 }; |
| 89 | 118 |
| 90 TEST_F(ServiceConnectorTest, Basic) { | 119 TEST_F(ServiceConnectorTest, Basic) { |
| 91 test_client_->Test("test"); | 120 test_client_->Test("test"); |
| 92 loop_.Run(); | 121 loop_.Run(); |
| 93 EXPECT_EQ(std::string("test"), context_.last_test_string); | 122 EXPECT_EQ(std::string("test"), context_.last_test_string); |
| 94 } | 123 } |
| 95 | 124 |
| 96 } // namespace | 125 TEST_F(ServiceConnectorTest, ClientError) { |
| 126 test_client_->Test("test"); |
| 127 EXPECT_TRUE(HasFactoryForTestURL()); |
| 128 loop_.Run(); |
| 129 EXPECT_EQ(1, context_.num_impls); |
| 130 test_client_.reset(NULL); |
| 131 loop_.Run(); |
| 132 EXPECT_EQ(0, context_.num_impls); |
| 133 EXPECT_FALSE(HasFactoryForTestURL()); |
| 134 } |
| 97 } // namespace shell | 135 } // namespace shell |
| 98 } // namespace mojo | 136 } // namespace mojo |
| OLD | NEW |