Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(223)

Side by Side Diff: mojo/public/cpp/bindings/tests/request_response_unittest.cc

Issue 265793015: Mojo: Replace RemotePtr with InterfacePtr and InterfaceImpl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "mojo/public/cpp/bindings/allocation_scope.h" 5 #include "mojo/public/cpp/bindings/allocation_scope.h"
6 #include "mojo/public/cpp/bindings/remote_ptr.h"
7 #include "mojo/public/cpp/environment/environment.h" 6 #include "mojo/public/cpp/environment/environment.h"
8 #include "mojo/public/cpp/test_support/test_utils.h" 7 #include "mojo/public/cpp/test_support/test_utils.h"
9 #include "mojo/public/cpp/utility/run_loop.h" 8 #include "mojo/public/cpp/utility/run_loop.h"
10 #include "mojo/public/interfaces/bindings/tests/sample_import.mojom.h" 9 #include "mojo/public/interfaces/bindings/tests/sample_import.mojom.h"
11 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" 10 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h"
12 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
13 12
14 namespace mojo { 13 namespace mojo {
15 namespace test { 14 namespace test {
16 namespace { 15 namespace {
17 16
18 class ProviderImpl : public sample::Provider { 17 class ProviderImpl : public InterfaceImpl<sample::Provider> {
19 public: 18 public:
20 explicit ProviderImpl(sample::ScopedProviderClientHandle handle) 19 virtual void OnConnectionError() MOJO_OVERRIDE {
21 : client_(handle.Pass(), this) { 20 delete this;
21 }
22
23 virtual void SetClient(sample::ProviderClient* client) MOJO_OVERRIDE {
24 // Ignored. TODO(darin): Eliminate ProviderClient.
22 } 25 }
23 26
24 virtual void EchoString( 27 virtual void EchoString(
25 const String& a, 28 const String& a,
26 const Callback<void(String)>& callback) MOJO_OVERRIDE { 29 const Callback<void(String)>& callback) MOJO_OVERRIDE {
27 AllocationScope scope; 30 AllocationScope scope;
28 Callback<void(String)> callback_copy; 31 Callback<void(String)> callback_copy;
29 // Make sure operator= is used. 32 // Make sure operator= is used.
30 callback_copy = callback; 33 callback_copy = callback;
31 callback_copy.Run(a); 34 callback_copy.Run(a);
(...skipping 12 matching lines...) Expand all
44 const Callback<void(ScopedMessagePipeHandle)>& callback) MOJO_OVERRIDE { 47 const Callback<void(ScopedMessagePipeHandle)>& callback) MOJO_OVERRIDE {
45 AllocationScope scope; 48 AllocationScope scope;
46 callback.Run(a.Pass()); 49 callback.Run(a.Pass());
47 } 50 }
48 51
49 virtual void EchoEnum(sample::Enum a, 52 virtual void EchoEnum(sample::Enum a,
50 const Callback<void(sample::Enum)>& callback) 53 const Callback<void(sample::Enum)>& callback)
51 MOJO_OVERRIDE { 54 MOJO_OVERRIDE {
52 callback.Run(a); 55 callback.Run(a);
53 } 56 }
54
55 private:
56 RemotePtr<sample::ProviderClient> client_;
57 }; 57 };
58 58
59 class StringRecorder { 59 class StringRecorder {
60 public: 60 public:
61 StringRecorder(std::string* buf) : buf_(buf) { 61 StringRecorder(std::string* buf) : buf_(buf) {
62 } 62 }
63 void Run(const String& a) const { 63 void Run(const String& a) const {
64 *buf_ = a.To<std::string>(); 64 *buf_ = a.To<std::string>();
65 } 65 }
66 void Run(const String& a, const String& b) const { 66 void Run(const String& a, const String& b) const {
(...skipping 20 matching lines...) Expand all
87 } 87 }
88 void Run(ScopedMessagePipeHandle handle) const { 88 void Run(ScopedMessagePipeHandle handle) const {
89 WriteTextMessage(handle.get(), text_); 89 WriteTextMessage(handle.get(), text_);
90 } 90 }
91 private: 91 private:
92 std::string text_; 92 std::string text_;
93 }; 93 };
94 94
95 class RequestResponseTest : public testing::Test { 95 class RequestResponseTest : public testing::Test {
96 public: 96 public:
97 virtual ~RequestResponseTest() {
98 loop_.RunUntilIdle();
99 }
100
97 void PumpMessages() { 101 void PumpMessages() {
98 loop_.RunUntilIdle(); 102 loop_.RunUntilIdle();
99 } 103 }
100 104
101 private: 105 private:
102 Environment env_; 106 Environment env_;
103 RunLoop loop_; 107 RunLoop loop_;
104 }; 108 };
105 109
106 TEST_F(RequestResponseTest, EchoString) { 110 TEST_F(RequestResponseTest, EchoString) {
107 InterfacePipe<sample::Provider> pipe; 111 sample::ProviderPtr provider;
108 ProviderImpl provider_impl(pipe.handle_to_peer.Pass()); 112 BindToProxy(new ProviderImpl(), &provider);
109 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass(), NULL);
110 113
111 std::string buf; 114 std::string buf;
112 { 115 {
113 AllocationScope scope; 116 AllocationScope scope;
114 provider->EchoString("hello", StringRecorder(&buf)); 117 provider->EchoString("hello", StringRecorder(&buf));
115 } 118 }
116 119
117 PumpMessages(); 120 PumpMessages();
118 121
119 EXPECT_EQ(std::string("hello"), buf); 122 EXPECT_EQ(std::string("hello"), buf);
120 } 123 }
121 124
122 TEST_F(RequestResponseTest, EchoStrings) { 125 TEST_F(RequestResponseTest, EchoStrings) {
123 InterfacePipe<sample::Provider> pipe; 126 sample::ProviderPtr provider;
124 ProviderImpl provider_impl(pipe.handle_to_peer.Pass()); 127 BindToProxy(new ProviderImpl(), &provider);
125 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass(), NULL);
126 128
127 std::string buf; 129 std::string buf;
128 { 130 {
129 AllocationScope scope; 131 AllocationScope scope;
130 provider->EchoStrings("hello", " world", StringRecorder(&buf)); 132 provider->EchoStrings("hello", " world", StringRecorder(&buf));
131 } 133 }
132 134
133 PumpMessages(); 135 PumpMessages();
134 136
135 EXPECT_EQ(std::string("hello world"), buf); 137 EXPECT_EQ(std::string("hello world"), buf);
136 } 138 }
137 139
138 TEST_F(RequestResponseTest, EchoMessagePipeHandle) { 140 TEST_F(RequestResponseTest, EchoMessagePipeHandle) {
139 InterfacePipe<sample::Provider> pipe; 141 sample::ProviderPtr provider;
140 ProviderImpl provider_impl(pipe.handle_to_peer.Pass()); 142 BindToProxy(new ProviderImpl(), &provider);
141 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass(), NULL);
142 143
143 MessagePipe pipe2; 144 MessagePipe pipe2;
144 { 145 {
145 AllocationScope scope; 146 AllocationScope scope;
146 provider->EchoMessagePipeHandle(pipe2.handle1.Pass(), 147 provider->EchoMessagePipeHandle(pipe2.handle1.Pass(),
147 MessagePipeWriter("hello")); 148 MessagePipeWriter("hello"));
148 } 149 }
149 150
150 PumpMessages(); 151 PumpMessages();
151 152
152 std::string value; 153 std::string value;
153 ReadTextMessage(pipe2.handle0.get(), &value); 154 ReadTextMessage(pipe2.handle0.get(), &value);
154 155
155 EXPECT_EQ(std::string("hello"), value); 156 EXPECT_EQ(std::string("hello"), value);
156 } 157 }
157 158
158 TEST_F(RequestResponseTest, EchoEnum) { 159 TEST_F(RequestResponseTest, EchoEnum) {
159 InterfacePipe<sample::Provider> pipe; 160 sample::ProviderPtr provider;
160 ProviderImpl provider_impl(pipe.handle_to_peer.Pass()); 161 BindToProxy(new ProviderImpl(), &provider);
161 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass(), NULL);
162 162
163 sample::Enum value; 163 sample::Enum value;
164 { 164 {
165 AllocationScope scope; 165 AllocationScope scope;
166 provider->EchoEnum(sample::ENUM_VALUE, EnumRecorder(&value)); 166 provider->EchoEnum(sample::ENUM_VALUE, EnumRecorder(&value));
167 } 167 }
168 168
169 PumpMessages(); 169 PumpMessages();
170 170
171 EXPECT_EQ(sample::ENUM_VALUE, value); 171 EXPECT_EQ(sample::ENUM_VALUE, value);
172 } 172 }
173 173
174 } // namespace 174 } // namespace
175 } // namespace test 175 } // namespace test
176 } // namespace mojo 176 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698