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" | 5 #include "base/message_loop/message_loop.h" |
6 #include "mojo/public/bindings/allocation_scope.h" | 6 #include "mojo/public/bindings/allocation_scope.h" |
7 #include "mojo/public/bindings/remote_ptr.h" | 7 #include "mojo/public/bindings/remote_ptr.h" |
| 8 #include "mojo/public/shell/service.h" |
8 #include "mojo/shell/service_connector.h" | 9 #include "mojo/shell/service_connector.h" |
9 #include "mojom/shell.h" | 10 #include "mojom/shell.h" |
10 #include "mojom/test.h" | 11 #include "mojom/test.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 namespace mojo { | 14 namespace mojo { |
14 namespace shell { | 15 namespace shell { |
15 namespace { | 16 namespace { |
16 | 17 |
17 class TestApp : public ShellClient { | 18 struct Context { |
| 19 std::string last_test_string; |
| 20 }; |
| 21 |
| 22 class TestServiceImpl : public Service<TestService, TestServiceImpl, Context> { |
18 public: | 23 public: |
19 TestApp(ScopedMessagePipeHandle shell_handle) | 24 virtual void Test(const mojo::String& test_string) OVERRIDE { |
20 : shell_(shell_handle.Pass(), this) { | 25 context()->last_test_string = test_string.To<std::string>(); |
| 26 client()->AckTest(); |
21 } | 27 } |
22 virtual ~TestApp() { | |
23 } | |
24 virtual void AcceptConnection(ScopedMessagePipeHandle client_handle) | |
25 MOJO_OVERRIDE { | |
26 service_.reset(new TestServiceImpl(this, client_handle.Pass())); | |
27 } | |
28 std::string GetLastTestString() { | |
29 return service_->last_test_string_; | |
30 } | |
31 | |
32 private: | |
33 class TestServiceImpl : public TestService { | |
34 public: | |
35 TestServiceImpl(TestApp* service, ScopedMessagePipeHandle client_handle) | |
36 : service_(service), | |
37 client_(client_handle.Pass(), this) { | |
38 } | |
39 virtual ~TestServiceImpl() { | |
40 } | |
41 virtual void Test(const mojo::String& test_string) OVERRIDE { | |
42 last_test_string_ = test_string.To<std::string>(); | |
43 client_->AckTest(); | |
44 } | |
45 TestApp* service_; | |
46 RemotePtr<TestClient> client_; | |
47 std::string last_test_string_; | |
48 }; | |
49 RemotePtr<Shell> shell_; | |
50 scoped_ptr<TestServiceImpl> service_; | |
51 }; | 28 }; |
52 | 29 |
53 class TestClientImpl : public TestClient { | 30 class TestClientImpl : public TestClient { |
54 public: | 31 public: |
55 explicit TestClientImpl(ScopedMessagePipeHandle service_handle) | 32 explicit TestClientImpl(ScopedMessagePipeHandle service_handle) |
56 : service_(service_handle.Pass(), this), | 33 : service_(service_handle.Pass(), this), |
57 quit_after_ack_(false) { | 34 quit_after_ack_(false) { |
58 } | 35 } |
59 virtual ~TestClientImpl() { | 36 virtual ~TestClientImpl() { |
60 } | 37 } |
(...skipping 29 matching lines...) Expand all Loading... |
90 } | 67 } |
91 | 68 |
92 virtual void TearDown() OVERRIDE { | 69 virtual void TearDown() OVERRIDE { |
93 test_client_.reset(NULL); | 70 test_client_.reset(NULL); |
94 test_app_.reset(NULL); | 71 test_app_.reset(NULL); |
95 service_connector_.reset(NULL); | 72 service_connector_.reset(NULL); |
96 } | 73 } |
97 | 74 |
98 virtual void Load(const GURL& url, | 75 virtual void Load(const GURL& url, |
99 ScopedMessagePipeHandle shell_handle) OVERRIDE { | 76 ScopedMessagePipeHandle shell_handle) OVERRIDE { |
100 test_app_.reset(new TestApp(shell_handle.Pass())); | 77 test_app_.reset(new ServiceFactory<TestServiceImpl, Context>( |
| 78 shell_handle.Pass(), &context_)); |
101 } | 79 } |
102 | 80 |
103 protected: | 81 protected: |
104 base::MessageLoop loop_; | 82 base::MessageLoop loop_; |
105 scoped_ptr<TestApp> test_app_; | 83 Context context_; |
| 84 scoped_ptr<ServiceFactory<TestServiceImpl, Context> > test_app_; |
106 scoped_ptr<TestClientImpl> test_client_; | 85 scoped_ptr<TestClientImpl> test_client_; |
107 scoped_ptr<ServiceConnector> service_connector_; | 86 scoped_ptr<ServiceConnector> service_connector_; |
108 DISALLOW_COPY_AND_ASSIGN(ServiceConnectorTest); | 87 DISALLOW_COPY_AND_ASSIGN(ServiceConnectorTest); |
109 }; | 88 }; |
110 | 89 |
111 TEST_F(ServiceConnectorTest, Basic) { | 90 TEST_F(ServiceConnectorTest, Basic) { |
112 test_client_->Test("test"); | 91 test_client_->Test("test"); |
113 loop_.Run(); | 92 loop_.Run(); |
114 EXPECT_EQ(std::string("test"), test_app_->GetLastTestString()); | 93 EXPECT_EQ(std::string("test"), context_.last_test_string); |
115 } | 94 } |
116 | 95 |
117 } // namespace | 96 } // namespace |
118 } // namespace shell | 97 } // namespace shell |
119 } // namespace mojo | 98 } // namespace mojo |
OLD | NEW |