| 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 }; |
| 50 | 74 |
| 51 class ServiceConnectorTest : public testing::Test, | 75 class ServiceConnectorTest : public testing::Test, |
| 52 public ServiceConnector::Loader { | 76 public ServiceConnector::Loader { |
| 53 public: | 77 public: |
| 54 ServiceConnectorTest() { | 78 ServiceConnectorTest() {} |
| 55 } | |
| 56 | 79 |
| 57 virtual ~ServiceConnectorTest() { | 80 virtual ~ServiceConnectorTest() {} |
| 58 } | |
| 59 | 81 |
| 60 virtual void SetUp() OVERRIDE { | 82 virtual void SetUp() OVERRIDE { |
| 61 GURL test_url("test:testService"); | 83 GURL test_url(kTestURLString); |
| 62 service_connector_.reset(new ServiceConnector); | 84 service_connector_.reset(new ServiceConnector); |
| 63 service_connector_->SetLoaderForURL(this, test_url); | 85 service_connector_->SetLoaderForURL(this, test_url); |
| 64 MessagePipe pipe; | 86 MessagePipe pipe; |
| 65 test_client_.reset(new TestClientImpl(pipe.handle0.Pass())); | 87 test_client_.reset(new TestClientImpl(pipe.handle0.Pass())); |
| 66 service_connector_->Connect(test_url, pipe.handle1.Pass()); | 88 service_connector_->Connect(test_url, pipe.handle1.Pass()); |
| 67 } | 89 } |
| 68 | 90 |
| 69 virtual void TearDown() OVERRIDE { | 91 virtual void TearDown() OVERRIDE { |
| 70 test_client_.reset(NULL); | 92 test_client_.reset(NULL); |
| 71 test_app_.reset(NULL); | 93 test_app_.reset(NULL); |
| 72 service_connector_.reset(NULL); | 94 service_connector_.reset(NULL); |
| 73 } | 95 } |
| 74 | 96 |
| 75 virtual void Load(const GURL& url, | 97 virtual void Load(const GURL& url, |
| 76 ScopedMessagePipeHandle shell_handle) OVERRIDE { | 98 ScopedMessagePipeHandle shell_handle) OVERRIDE { |
| 77 test_app_.reset(new ServiceFactory<TestServiceImpl, Context>( | 99 test_app_.reset(new ServiceFactory<TestServiceImpl, TestContext>( |
| 78 shell_handle.Pass(), &context_)); | 100 shell_handle.Pass(), &context_)); |
| 79 } | 101 } |
| 80 | 102 |
| 81 protected: | 103 protected: |
| 82 base::MessageLoop loop_; | 104 mojo::Environment env_; |
| 83 Context context_; | 105 mojo::RunLoop loop_; |
| 84 scoped_ptr<ServiceFactory<TestServiceImpl, Context> > test_app_; | 106 TestContext context_; |
| 107 scoped_ptr<ServiceFactory<TestServiceImpl, TestContext> > test_app_; |
| 85 scoped_ptr<TestClientImpl> test_client_; | 108 scoped_ptr<TestClientImpl> test_client_; |
| 86 scoped_ptr<ServiceConnector> service_connector_; | 109 scoped_ptr<ServiceConnector> service_connector_; |
| 87 DISALLOW_COPY_AND_ASSIGN(ServiceConnectorTest); | 110 DISALLOW_COPY_AND_ASSIGN(ServiceConnectorTest); |
| 88 }; | 111 }; |
| 89 | 112 |
| 90 TEST_F(ServiceConnectorTest, Basic) { | 113 TEST_F(ServiceConnectorTest, Basic) { |
| 91 test_client_->Test("test"); | 114 test_client_->Test("test"); |
| 92 loop_.Run(); | 115 loop_.Run(); |
| 93 EXPECT_EQ(std::string("test"), context_.last_test_string); | 116 EXPECT_EQ(std::string("test"), context_.last_test_string); |
| 94 } | 117 } |
| 95 | 118 |
| 119 TEST_F(ServiceConnectorTest, ClientError) { |
| 120 test_client_->Test("test"); |
| 121 EXPECT_TRUE(service_connector_->HasFactoryForURL(GURL(kTestURLString))); |
| 122 loop_.Run(); |
| 123 EXPECT_EQ(1, context_.num_impls); |
| 124 test_client_.reset(NULL); |
| 125 loop_.Run(); |
| 126 EXPECT_EQ(0, context_.num_impls); |
| 127 EXPECT_FALSE(service_connector_->HasFactoryForURL(GURL(kTestURLString))); |
| 128 } |
| 96 } // namespace | 129 } // namespace |
| 97 } // namespace shell | 130 } // namespace shell |
| 98 } // namespace mojo | 131 } // namespace mojo |
| OLD | NEW |