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

Side by Side Diff: mojo/apps/js/test/js_to_cpp_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 "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/files/file_path.h" 6 #include "base/files/file_path.h"
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "gin/public/isolate_holder.h" 10 #include "gin/public/isolate_holder.h"
11 #include "mojo/apps/js/mojo_runner_delegate.h" 11 #include "mojo/apps/js/mojo_runner_delegate.h"
12 #include "mojo/apps/js/test/js_to_cpp.mojom.h" 12 #include "mojo/apps/js/test/js_to_cpp.mojom.h"
13 #include "mojo/common/common_type_converters.h" 13 #include "mojo/common/common_type_converters.h"
14 #include "mojo/common/test/test_utils.h" 14 #include "mojo/common/test/test_utils.h"
15 #include "mojo/public/cpp/bindings/allocation_scope.h"
16 #include "mojo/public/cpp/environment/environment.h" 15 #include "mojo/public/cpp/environment/environment.h"
17 #include "mojo/public/cpp/system/core.h" 16 #include "mojo/public/cpp/system/core.h"
18 #include "mojo/public/cpp/system/macros.h" 17 #include "mojo/public/cpp/system/macros.h"
19 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
20 19
21 namespace mojo { 20 namespace mojo {
22 namespace js { 21 namespace js {
23 22
24 // Global value updated by some checks to prevent compilers from optimizing 23 // Global value updated by some checks to prevent compilers from optimizing
25 // reads out of existence. 24 // reads out of existence.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); 69 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer));
71 MojoResult result = MojoReadData( 70 MojoResult result = MojoReadData(
72 data_pipe_handle, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE); 71 data_pipe_handle, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE);
73 EXPECT_EQ(MOJO_RESULT_OK, result); 72 EXPECT_EQ(MOJO_RESULT_OK, result);
74 EXPECT_EQ(64u, buffer_size); 73 EXPECT_EQ(64u, buffer_size);
75 for (int i = 0; i < 64; ++i) { 74 for (int i = 0; i < 64; ++i) {
76 EXPECT_EQ(i, buffer[i]); 75 EXPECT_EQ(i, buffer[i]);
77 } 76 }
78 } 77 }
79 78
80 // NOTE: Callers will need to have established an AllocationScope, or you're 79 js_to_cpp::EchoArgsPtr BuildSampleEchoArgs() {
81 // gonna have a bad time. 80 js_to_cpp::EchoArgsPtr args;
82 js_to_cpp::EchoArgs BuildSampleEchoArgs() { 81 args->si64 = kExpectedInt64Value;
83 js_to_cpp::EchoArgs::Builder builder; 82 args->si32 = kExpectedInt32Value;
84 builder.set_si64(kExpectedInt64Value); 83 args->si16 = kExpectedInt16Value;
85 builder.set_si32(kExpectedInt32Value); 84 args->si8 = kExpectedInt8Value;
86 builder.set_si16(kExpectedInt16Value); 85 args->ui64 = kExpectedUInt64Value;
87 builder.set_si8(kExpectedInt8Value); 86 args->ui32 = kExpectedUInt32Value;
88 builder.set_ui64(kExpectedUInt64Value); 87 args->ui16 = kExpectedUInt16Value;
89 builder.set_ui32(kExpectedUInt32Value); 88 args->ui8 = kExpectedUInt8Value;
90 builder.set_ui16(kExpectedUInt16Value); 89 args->float_val = kExpectedFloatVal;
91 builder.set_ui8(kExpectedUInt8Value); 90 args->float_inf = kExpectedFloatInf;
92 builder.set_float_val(kExpectedFloatVal); 91 args->float_nan = kExpectedFloatNan;
93 builder.set_float_inf(kExpectedFloatInf); 92 args->double_val = kExpectedDoubleVal;
94 builder.set_float_nan(kExpectedFloatNan); 93 args->double_inf = kExpectedDoubleInf;
95 builder.set_double_val(kExpectedDoubleVal); 94 args->double_nan = kExpectedDoubleNan;
96 builder.set_double_inf(kExpectedDoubleInf); 95 args->name = String::From("coming");
97 builder.set_double_nan(kExpectedDoubleNan); 96 args->string_array = Array<String>::New(3);
98 builder.set_name("coming"); 97 args->string_array[0] = String::From("one");
99 mojo::Array<mojo::String>::Builder string_array(3); 98 args->string_array[1] = String::From("two");
100 string_array[0] = "one"; 99 args->string_array[2] = String::From("three");
101 string_array[1] = "two"; 100 return args.Pass();
102 string_array[2] = "three";
103 builder.set_string_array(string_array.Finish());
104 return builder.Finish();
105 } 101 }
106 102
107 void CheckSampleEchoArgs(const js_to_cpp::EchoArgs& arg) { 103 void CheckSampleEchoArgs(const js_to_cpp::EchoArgs& arg) {
108 EXPECT_EQ(kExpectedInt64Value, arg.si64()); 104 EXPECT_EQ(kExpectedInt64Value, arg.si64);
109 EXPECT_EQ(kExpectedInt32Value, arg.si32()); 105 EXPECT_EQ(kExpectedInt32Value, arg.si32);
110 EXPECT_EQ(kExpectedInt16Value, arg.si16()); 106 EXPECT_EQ(kExpectedInt16Value, arg.si16);
111 EXPECT_EQ(kExpectedInt8Value, arg.si8()); 107 EXPECT_EQ(kExpectedInt8Value, arg.si8);
112 EXPECT_EQ(kExpectedUInt64Value, arg.ui64()); 108 EXPECT_EQ(kExpectedUInt64Value, arg.ui64);
113 EXPECT_EQ(kExpectedUInt32Value, arg.ui32()); 109 EXPECT_EQ(kExpectedUInt32Value, arg.ui32);
114 EXPECT_EQ(kExpectedUInt16Value, arg.ui16()); 110 EXPECT_EQ(kExpectedUInt16Value, arg.ui16);
115 EXPECT_EQ(kExpectedUInt8Value, arg.ui8()); 111 EXPECT_EQ(kExpectedUInt8Value, arg.ui8);
116 EXPECT_EQ(kExpectedFloatVal, arg.float_val()); 112 EXPECT_EQ(kExpectedFloatVal, arg.float_val);
117 EXPECT_EQ(kExpectedFloatInf, arg.float_inf()); 113 EXPECT_EQ(kExpectedFloatInf, arg.float_inf);
118 EXPECT_NAN(arg.float_nan()); 114 EXPECT_NAN(arg.float_nan);
119 EXPECT_EQ(kExpectedDoubleVal, arg.double_val()); 115 EXPECT_EQ(kExpectedDoubleVal, arg.double_val);
120 EXPECT_EQ(kExpectedDoubleInf, arg.double_inf()); 116 EXPECT_EQ(kExpectedDoubleInf, arg.double_inf);
121 EXPECT_NAN(arg.double_nan()); 117 EXPECT_NAN(arg.double_nan);
122 EXPECT_EQ(std::string("coming"), arg.name().To<std::string>()); 118 EXPECT_EQ(std::string("coming"), arg.name.To<std::string>());
123 EXPECT_EQ(std::string("one"), arg.string_array()[0].To<std::string>()); 119 EXPECT_EQ(std::string("one"), arg.string_array[0].To<std::string>());
124 EXPECT_EQ(std::string("two"), arg.string_array()[1].To<std::string>()); 120 EXPECT_EQ(std::string("two"), arg.string_array[1].To<std::string>());
125 EXPECT_EQ(std::string("three"), arg.string_array()[2].To<std::string>()); 121 EXPECT_EQ(std::string("three"), arg.string_array[2].To<std::string>());
126 CheckDataPipe(arg.data_handle().get().value()); 122 CheckDataPipe(arg.data_handle.get().value());
127 } 123 }
128 124
129 js_to_cpp::EchoArgsList BuildSampleEchoArgsList() { 125 js_to_cpp::EchoArgsListPtr BuildSampleEchoArgsList() {
130 js_to_cpp::EchoArgsList::Builder builder; 126 js_to_cpp::EchoArgsListPtr args_list(js_to_cpp::EchoArgsList::New());
131 builder.set_item(BuildSampleEchoArgs()); 127 args_list->item = BuildSampleEchoArgs();
132 return builder.Finish(); 128 return args_list.Pass();
133 } 129 }
134 130
135 void CheckSampleEchoArgsList(const js_to_cpp::EchoArgsList& list) { 131 void CheckSampleEchoArgsList(const js_to_cpp::EchoArgsListPtr& list) {
136 if (list.is_null()) 132 if (list.is_null())
137 return; 133 return;
138 CheckSampleEchoArgs(list.item()); 134 CheckSampleEchoArgs(*list->item);
139 CheckSampleEchoArgsList(list.next()); 135 CheckSampleEchoArgsList(list->next);
140 } 136 }
141 137
142 void CheckCorruptedString(const mojo::String& arg) { 138 void CheckCorruptedString(const mojo::String& arg) {
143 // The values don't matter so long as all accesses are within bounds. 139 // The values don't matter so long as all accesses are within bounds.
144 if (arg.is_null()) 140 if (arg.is_null())
145 return; 141 return;
146 for (size_t i = 0; i < arg.size(); ++i) 142 for (size_t i = 0; i < arg.size(); ++i)
147 g_waste_accumulator += arg[i]; 143 g_waste_accumulator += arg[i];
148 } 144 }
149 145
(...skipping 25 matching lines...) Expand all
175 } 171 }
176 172
177 virtual void TestFinished() OVERRIDE { 173 virtual void TestFinished() OVERRIDE {
178 NOTREACHED(); 174 NOTREACHED();
179 } 175 }
180 176
181 virtual void PingResponse() OVERRIDE { 177 virtual void PingResponse() OVERRIDE {
182 NOTREACHED(); 178 NOTREACHED();
183 } 179 }
184 180
185 virtual void EchoResponse(const js_to_cpp::EchoArgsList& list) OVERRIDE { 181 virtual void EchoResponse(js_to_cpp::EchoArgsListPtr list) OVERRIDE {
186 NOTREACHED(); 182 NOTREACHED();
187 } 183 }
188 184
189 virtual void BitFlipResponse(const js_to_cpp::EchoArgs& arg1) OVERRIDE { 185 virtual void BitFlipResponse(js_to_cpp::EchoArgsPtr arg1) OVERRIDE {
190 NOTREACHED(); 186 NOTREACHED();
191 } 187 }
192 188
193 protected: 189 protected:
194 base::RunLoop* run_loop_; 190 base::RunLoop* run_loop_;
195 js_to_cpp::JsSide* js_side_; 191 js_to_cpp::JsSide* js_side_;
196 192
197 private: 193 private:
198 DISALLOW_COPY_AND_ASSIGN(CppSideConnection); 194 DISALLOW_COPY_AND_ASSIGN(CppSideConnection);
199 }; 195 };
(...skipping 27 matching lines...) Expand all
227 class EchoCppSideConnection : public CppSideConnection { 223 class EchoCppSideConnection : public CppSideConnection {
228 public: 224 public:
229 explicit EchoCppSideConnection() : 225 explicit EchoCppSideConnection() :
230 message_count_(0), 226 message_count_(0),
231 termination_seen_(false) { 227 termination_seen_(false) {
232 } 228 }
233 virtual ~EchoCppSideConnection() {} 229 virtual ~EchoCppSideConnection() {}
234 230
235 // js_to_cpp::CppSide: 231 // js_to_cpp::CppSide:
236 virtual void StartTest() OVERRIDE { 232 virtual void StartTest() OVERRIDE {
237 AllocationScope scope;
238 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgsList()); 233 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgsList());
239 } 234 }
240 235
241 virtual void EchoResponse(const js_to_cpp::EchoArgsList& list) OVERRIDE { 236 virtual void EchoResponse(js_to_cpp::EchoArgsListPtr list) OVERRIDE {
242 const js_to_cpp::EchoArgs& special_arg = list.item(); 237 const js_to_cpp::EchoArgsPtr& special_arg = list->item;
243 message_count_ += 1; 238 message_count_ += 1;
244 EXPECT_EQ(-1, special_arg.si64()); 239 EXPECT_EQ(-1, special_arg->si64);
245 EXPECT_EQ(-1, special_arg.si32()); 240 EXPECT_EQ(-1, special_arg->si32);
246 EXPECT_EQ(-1, special_arg.si16()); 241 EXPECT_EQ(-1, special_arg->si16);
247 EXPECT_EQ(-1, special_arg.si8()); 242 EXPECT_EQ(-1, special_arg->si8);
248 EXPECT_EQ(std::string("going"), special_arg.name().To<std::string>()); 243 EXPECT_EQ(std::string("going"), special_arg->name.To<std::string>());
249 CheckSampleEchoArgsList(list.next()); 244 CheckSampleEchoArgsList(list->next);
250 } 245 }
251 246
252 virtual void TestFinished() OVERRIDE { 247 virtual void TestFinished() OVERRIDE {
253 termination_seen_ = true; 248 termination_seen_ = true;
254 run_loop()->Quit(); 249 run_loop()->Quit();
255 } 250 }
256 251
257 bool DidSucceed() { 252 bool DidSucceed() {
258 return termination_seen_ && message_count_ == kExpectedMessageCount; 253 return termination_seen_ && message_count_ == kExpectedMessageCount;
259 } 254 }
260 255
261 private: 256 private:
262 static const int kExpectedMessageCount = 100; 257 static const int kExpectedMessageCount = 100;
263 int message_count_; 258 int message_count_;
264 bool termination_seen_; 259 bool termination_seen_;
265 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection); 260 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection);
266 }; 261 };
267 262
268 // Test that corrupted messages don't wreak havoc. 263 // Test that corrupted messages don't wreak havoc.
269 class BitFlipCppSideConnection : public CppSideConnection { 264 class BitFlipCppSideConnection : public CppSideConnection {
270 public: 265 public:
271 explicit BitFlipCppSideConnection() : termination_seen_(false) {} 266 explicit BitFlipCppSideConnection() : termination_seen_(false) {}
272 virtual ~BitFlipCppSideConnection() {} 267 virtual ~BitFlipCppSideConnection() {}
273 268
274 // js_to_cpp::CppSide: 269 // js_to_cpp::CppSide:
275 virtual void StartTest() OVERRIDE { 270 virtual void StartTest() OVERRIDE {
276 AllocationScope scope;
277 js_side_->BitFlip(BuildSampleEchoArgs()); 271 js_side_->BitFlip(BuildSampleEchoArgs());
278 } 272 }
279 273
280 virtual void BitFlipResponse(const js_to_cpp::EchoArgs& arg) OVERRIDE { 274 virtual void BitFlipResponse(js_to_cpp::EchoArgsPtr arg) OVERRIDE {
281 if (arg.is_null()) 275 if (arg.is_null())
282 return; 276 return;
283 CheckCorruptedString(arg.name()); 277 CheckCorruptedString(arg->name);
284 CheckCorruptedStringArray(arg.string_array()); 278 CheckCorruptedStringArray(arg->string_array);
285 if (arg.data_handle().is_valid()) 279 if (arg->data_handle.is_valid())
286 CheckDataPipe(arg.data_handle().get().value()); 280 CheckDataPipe(arg->data_handle.get().value());
287 } 281 }
288 282
289 virtual void TestFinished() OVERRIDE { 283 virtual void TestFinished() OVERRIDE {
290 termination_seen_ = true; 284 termination_seen_ = true;
291 run_loop()->Quit(); 285 run_loop()->Quit();
292 } 286 }
293 287
294 bool DidSucceed() { 288 bool DidSucceed() {
295 return termination_seen_; 289 return termination_seen_;
296 } 290 }
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 if (IsRunningOnIsolatedBot()) 352 if (IsRunningOnIsolatedBot())
359 return; 353 return;
360 354
361 BitFlipCppSideConnection cpp_side_connection; 355 BitFlipCppSideConnection cpp_side_connection;
362 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); 356 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection);
363 EXPECT_TRUE(cpp_side_connection.DidSucceed()); 357 EXPECT_TRUE(cpp_side_connection.DidSucceed());
364 } 358 }
365 359
366 } // namespace js 360 } // namespace js
367 } // namespace mojo 361 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698