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

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

Issue 294833002: Mojo: more idiomatic C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more 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"
6 #include "mojo/public/cpp/environment/environment.h" 5 #include "mojo/public/cpp/environment/environment.h"
7 #include "mojo/public/cpp/test_support/test_utils.h" 6 #include "mojo/public/cpp/test_support/test_utils.h"
8 #include "mojo/public/cpp/utility/run_loop.h" 7 #include "mojo/public/cpp/utility/run_loop.h"
9 #include "mojo/public/interfaces/bindings/tests/sample_import.mojom.h" 8 #include "mojo/public/interfaces/bindings/tests/sample_import.mojom.h"
10 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" 9 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h"
11 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
12 11
13 namespace mojo { 12 namespace mojo {
14 namespace test { 13 namespace test {
15 namespace { 14 namespace {
16 15
17 class ProviderImpl : public InterfaceImpl<sample::Provider> { 16 class ProviderImpl : public InterfaceImpl<sample::Provider> {
18 public: 17 public:
19 virtual void OnConnectionError() MOJO_OVERRIDE { 18 virtual void OnConnectionError() MOJO_OVERRIDE {
20 delete this; 19 delete this;
21 } 20 }
22 21
23 virtual void EchoString( 22 virtual void EchoString(
24 const String& a, 23 String a,
25 const Callback<void(String)>& callback) MOJO_OVERRIDE { 24 const Callback<void(String)>& callback) MOJO_OVERRIDE {
26 AllocationScope scope;
27 Callback<void(String)> callback_copy; 25 Callback<void(String)> callback_copy;
28 // Make sure operator= is used. 26 // Make sure operator= is used.
29 callback_copy = callback; 27 callback_copy = callback;
30 callback_copy.Run(a); 28 callback_copy.Run(a.Pass());
31 } 29 }
32 30
33 virtual void EchoStrings( 31 virtual void EchoStrings(
34 const String& a, 32 String a,
35 const String& b, 33 String b,
36 const Callback<void(String, String)>& callback) MOJO_OVERRIDE { 34 const Callback<void(String, String)>& callback) MOJO_OVERRIDE {
37 AllocationScope scope; 35 callback.Run(a.Pass(), b.Pass());
38 callback.Run(a, b);
39 } 36 }
40 37
41 virtual void EchoMessagePipeHandle( 38 virtual void EchoMessagePipeHandle(
42 ScopedMessagePipeHandle a, 39 ScopedMessagePipeHandle a,
43 const Callback<void(ScopedMessagePipeHandle)>& callback) MOJO_OVERRIDE { 40 const Callback<void(ScopedMessagePipeHandle)>& callback) MOJO_OVERRIDE {
44 AllocationScope scope;
45 callback.Run(a.Pass()); 41 callback.Run(a.Pass());
46 } 42 }
47 43
48 virtual void EchoEnum(sample::Enum a, 44 virtual void EchoEnum(sample::Enum a,
49 const Callback<void(sample::Enum)>& callback) 45 const Callback<void(sample::Enum)>& callback)
50 MOJO_OVERRIDE { 46 MOJO_OVERRIDE {
51 callback.Run(a); 47 callback.Run(a);
52 } 48 }
53 }; 49 };
54 50
55 class StringRecorder { 51 class StringRecorder {
56 public: 52 public:
57 StringRecorder(std::string* buf) : buf_(buf) { 53 StringRecorder(std::string* buf) : buf_(buf) {
58 } 54 }
59 void Run(const String& a) const { 55 void Run(String a) const {
60 *buf_ = a.To<std::string>(); 56 *buf_ = a.To<std::string>();
61 } 57 }
62 void Run(const String& a, const String& b) const { 58 void Run(String a, String b) const {
63 *buf_ = a.To<std::string>() + b.To<std::string>(); 59 *buf_ = a.get() + b.get();
64 } 60 }
65 private: 61 private:
66 std::string* buf_; 62 std::string* buf_;
67 }; 63 };
68 64
69 class EnumRecorder { 65 class EnumRecorder {
70 public: 66 public:
71 EnumRecorder(sample::Enum* value) : value_(value) { 67 EnumRecorder(sample::Enum* value) : value_(value) {
72 } 68 }
73 void Run(sample::Enum a) const { 69 void Run(sample::Enum a) const {
(...skipping 27 matching lines...) Expand all
101 private: 97 private:
102 Environment env_; 98 Environment env_;
103 RunLoop loop_; 99 RunLoop loop_;
104 }; 100 };
105 101
106 TEST_F(RequestResponseTest, EchoString) { 102 TEST_F(RequestResponseTest, EchoString) {
107 sample::ProviderPtr provider; 103 sample::ProviderPtr provider;
108 BindToProxy(new ProviderImpl(), &provider); 104 BindToProxy(new ProviderImpl(), &provider);
109 105
110 std::string buf; 106 std::string buf;
111 { 107 provider->EchoString(String::From("hello"), StringRecorder(&buf));
112 AllocationScope scope;
113 provider->EchoString("hello", StringRecorder(&buf));
114 }
115 108
116 PumpMessages(); 109 PumpMessages();
117 110
118 EXPECT_EQ(std::string("hello"), buf); 111 EXPECT_EQ(std::string("hello"), buf);
119 } 112 }
120 113
121 TEST_F(RequestResponseTest, EchoStrings) { 114 TEST_F(RequestResponseTest, EchoStrings) {
122 sample::ProviderPtr provider; 115 sample::ProviderPtr provider;
123 BindToProxy(new ProviderImpl(), &provider); 116 BindToProxy(new ProviderImpl(), &provider);
124 117
125 std::string buf; 118 std::string buf;
126 { 119 provider->EchoStrings(
127 AllocationScope scope; 120 String::From("hello"), String::From(" world"), StringRecorder(&buf));
128 provider->EchoStrings("hello", " world", StringRecorder(&buf));
129 }
130 121
131 PumpMessages(); 122 PumpMessages();
132 123
133 EXPECT_EQ(std::string("hello world"), buf); 124 EXPECT_EQ(std::string("hello world"), buf);
134 } 125 }
135 126
136 TEST_F(RequestResponseTest, EchoMessagePipeHandle) { 127 TEST_F(RequestResponseTest, EchoMessagePipeHandle) {
137 sample::ProviderPtr provider; 128 sample::ProviderPtr provider;
138 BindToProxy(new ProviderImpl(), &provider); 129 BindToProxy(new ProviderImpl(), &provider);
139 130
140 MessagePipe pipe2; 131 MessagePipe pipe2;
141 { 132 provider->EchoMessagePipeHandle(pipe2.handle1.Pass(),
142 AllocationScope scope; 133 MessagePipeWriter("hello"));
143 provider->EchoMessagePipeHandle(pipe2.handle1.Pass(),
144 MessagePipeWriter("hello"));
145 }
146 134
147 PumpMessages(); 135 PumpMessages();
148 136
149 std::string value; 137 std::string value;
150 ReadTextMessage(pipe2.handle0.get(), &value); 138 ReadTextMessage(pipe2.handle0.get(), &value);
151 139
152 EXPECT_EQ(std::string("hello"), value); 140 EXPECT_EQ(std::string("hello"), value);
153 } 141 }
154 142
155 TEST_F(RequestResponseTest, EchoEnum) { 143 TEST_F(RequestResponseTest, EchoEnum) {
156 sample::ProviderPtr provider; 144 sample::ProviderPtr provider;
157 BindToProxy(new ProviderImpl(), &provider); 145 BindToProxy(new ProviderImpl(), &provider);
158 146
159 sample::Enum value; 147 sample::Enum value;
160 { 148 provider->EchoEnum(sample::ENUM_VALUE, EnumRecorder(&value));
161 AllocationScope scope;
162 provider->EchoEnum(sample::ENUM_VALUE, EnumRecorder(&value));
163 }
164 149
165 PumpMessages(); 150 PumpMessages();
166 151
167 EXPECT_EQ(sample::ENUM_VALUE, value); 152 EXPECT_EQ(sample::ENUM_VALUE, value);
168 } 153 }
169 154
170 } // namespace 155 } // namespace
171 } // namespace test 156 } // namespace test
172 } // namespace mojo 157 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698