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

Side by Side Diff: mojo/apps/js/test/js_to_cpp_unittest.cc

Issue 292243002: Add mojo bindings backpointer tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove stray "explicit" keywords. 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
« no previous file with comments | « mojo/apps/js/test/js_to_cpp.mojom ('k') | mojo/apps/js/test/js_to_cpp_unittest.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 EXPECT_EQ(kExpectedDoubleVal, arg.double_val()); 119 EXPECT_EQ(kExpectedDoubleVal, arg.double_val());
120 EXPECT_EQ(kExpectedDoubleInf, arg.double_inf()); 120 EXPECT_EQ(kExpectedDoubleInf, arg.double_inf());
121 EXPECT_NAN(arg.double_nan()); 121 EXPECT_NAN(arg.double_nan());
122 EXPECT_EQ(std::string("coming"), arg.name().To<std::string>()); 122 EXPECT_EQ(std::string("coming"), arg.name().To<std::string>());
123 EXPECT_EQ(std::string("one"), arg.string_array()[0].To<std::string>()); 123 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>()); 124 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>()); 125 EXPECT_EQ(std::string("three"), arg.string_array()[2].To<std::string>());
126 CheckDataPipe(arg.data_handle().get().value()); 126 CheckDataPipe(arg.data_handle().get().value());
127 } 127 }
128 128
129 js_to_cpp::EchoArgsList BuildSampleEchoArgsList() {
130 js_to_cpp::EchoArgsList::Builder builder;
131 builder.set_item(BuildSampleEchoArgs());
132 return builder.Finish();
133 }
134
135 void CheckSampleEchoArgsList(const js_to_cpp::EchoArgsList& list) { 129 void CheckSampleEchoArgsList(const js_to_cpp::EchoArgsList& list) {
136 if (list.is_null()) 130 if (list.is_null())
137 return; 131 return;
138 CheckSampleEchoArgs(list.item()); 132 CheckSampleEchoArgs(list.item());
139 CheckSampleEchoArgsList(list.next()); 133 CheckSampleEchoArgsList(list.next());
140 } 134 }
141 135
142 void CheckCorruptedString(const mojo::String& arg) { 136 void CheckCorruptedString(const mojo::String& arg) {
143 // The values don't matter so long as all accesses are within bounds. 137 // The values don't matter so long as all accesses are within bounds.
144 if (arg.is_null()) 138 if (arg.is_null())
145 return; 139 return;
146 for (size_t i = 0; i < arg.size(); ++i) 140 for (size_t i = 0; i < arg.size(); ++i)
147 g_waste_accumulator += arg[i]; 141 g_waste_accumulator += arg[i];
148 } 142 }
149 143
150 void CheckCorruptedStringArray(const mojo::Array<mojo::String>& string_array) { 144 void CheckCorruptedStringArray(const mojo::Array<mojo::String>& string_array) {
151 if (string_array.is_null()) 145 if (string_array.is_null())
152 return; 146 return;
153 for (size_t i = 0; i < string_array.size(); ++i) 147 for (size_t i = 0; i < string_array.size(); ++i)
154 CheckCorruptedString(string_array[i]); 148 CheckCorruptedString(string_array[i]);
155 } 149 }
156 150
151 void CheckCorruptedEchoArgs(const js_to_cpp::EchoArgs& arg) {
152 if (arg.is_null())
153 return;
154 CheckCorruptedString(arg.name());
155 CheckCorruptedStringArray(arg.string_array());
156 if (arg.data_handle().is_valid())
157 CheckDataPipe(arg.data_handle().get().value());
158 }
159
160 void CheckCorruptedEchoArgsList(const js_to_cpp::EchoArgsList& list) {
161 if (list.is_null())
162 return;
163 CheckCorruptedEchoArgs(list.item());
164 CheckCorruptedEchoArgsList(list.next());
165 }
166
157 // Base Provider implementation class. It's expected that tests subclass and 167 // Base Provider implementation class. It's expected that tests subclass and
158 // override the appropriate Provider functions. When test is done quit the 168 // override the appropriate Provider functions. When test is done quit the
159 // run_loop(). 169 // run_loop().
160 class CppSideConnection : public js_to_cpp::CppSide { 170 class CppSideConnection : public js_to_cpp::CppSide {
161 public: 171 public:
162 CppSideConnection() : run_loop_(NULL), js_side_(NULL) { 172 CppSideConnection() : run_loop_(NULL), js_side_(NULL) {
163 } 173 }
164 virtual ~CppSideConnection() {} 174 virtual ~CppSideConnection() {}
165 175
166 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } 176 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; }
(...skipping 12 matching lines...) Expand all
179 } 189 }
180 190
181 virtual void PingResponse() OVERRIDE { 191 virtual void PingResponse() OVERRIDE {
182 NOTREACHED(); 192 NOTREACHED();
183 } 193 }
184 194
185 virtual void EchoResponse(const js_to_cpp::EchoArgsList& list) OVERRIDE { 195 virtual void EchoResponse(const js_to_cpp::EchoArgsList& list) OVERRIDE {
186 NOTREACHED(); 196 NOTREACHED();
187 } 197 }
188 198
189 virtual void BitFlipResponse(const js_to_cpp::EchoArgs& arg1) OVERRIDE { 199 virtual void BitFlipResponse(const js_to_cpp::EchoArgsList& list) OVERRIDE {
190 NOTREACHED(); 200 NOTREACHED();
191 } 201 }
192 202
203 virtual void BackPointerResponse(
204 const js_to_cpp::EchoArgsList& list) OVERRIDE {
205 NOTREACHED();
206 }
193 protected: 207 protected:
194 base::RunLoop* run_loop_; 208 base::RunLoop* run_loop_;
195 js_to_cpp::JsSide* js_side_; 209 js_to_cpp::JsSide* js_side_;
196 210
197 private: 211 private:
198 DISALLOW_COPY_AND_ASSIGN(CppSideConnection); 212 DISALLOW_COPY_AND_ASSIGN(CppSideConnection);
199 }; 213 };
200 214
201 // Trivial test to verify a message sent from JS is received. 215 // Trivial test to verify a message sent from JS is received.
202 class PingCppSideConnection : public CppSideConnection { 216 class PingCppSideConnection : public CppSideConnection {
203 public: 217 public:
204 explicit PingCppSideConnection() : got_message_(false) {} 218 PingCppSideConnection() : got_message_(false) {}
205 virtual ~PingCppSideConnection() {} 219 virtual ~PingCppSideConnection() {}
206 220
207 // js_to_cpp::CppSide: 221 // js_to_cpp::CppSide:
208 virtual void StartTest() OVERRIDE { 222 virtual void StartTest() OVERRIDE {
209 js_side_->Ping(); 223 js_side_->Ping();
210 } 224 }
211 225
212 virtual void PingResponse() OVERRIDE { 226 virtual void PingResponse() OVERRIDE {
213 got_message_ = true; 227 got_message_ = true;
214 run_loop()->Quit(); 228 run_loop()->Quit();
215 } 229 }
216 230
217 bool DidSucceed() { 231 bool DidSucceed() {
218 return got_message_; 232 return got_message_;
219 } 233 }
220 234
221 private: 235 private:
222 bool got_message_; 236 bool got_message_;
223 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection); 237 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection);
224 }; 238 };
225 239
226 // Test that parameters are passed with correct values. 240 // Test that parameters are passed with correct values.
227 class EchoCppSideConnection : public CppSideConnection { 241 class EchoCppSideConnection : public CppSideConnection {
228 public: 242 public:
229 explicit EchoCppSideConnection() : 243 EchoCppSideConnection() :
230 message_count_(0), 244 message_count_(0),
231 termination_seen_(false) { 245 termination_seen_(false) {
232 } 246 }
233 virtual ~EchoCppSideConnection() {} 247 virtual ~EchoCppSideConnection() {}
234 248
235 // js_to_cpp::CppSide: 249 // js_to_cpp::CppSide:
236 virtual void StartTest() OVERRIDE { 250 virtual void StartTest() OVERRIDE {
237 AllocationScope scope; 251 AllocationScope scope;
238 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgsList()); 252 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgs());
239 } 253 }
240 254
241 virtual void EchoResponse(const js_to_cpp::EchoArgsList& list) OVERRIDE { 255 virtual void EchoResponse(const js_to_cpp::EchoArgsList& list) OVERRIDE {
242 const js_to_cpp::EchoArgs& special_arg = list.item(); 256 const js_to_cpp::EchoArgs& special_arg = list.item();
243 message_count_ += 1; 257 message_count_ += 1;
244 EXPECT_EQ(-1, special_arg.si64()); 258 EXPECT_EQ(-1, special_arg.si64());
245 EXPECT_EQ(-1, special_arg.si32()); 259 EXPECT_EQ(-1, special_arg.si32());
246 EXPECT_EQ(-1, special_arg.si16()); 260 EXPECT_EQ(-1, special_arg.si16());
247 EXPECT_EQ(-1, special_arg.si8()); 261 EXPECT_EQ(-1, special_arg.si8());
248 EXPECT_EQ(std::string("going"), special_arg.name().To<std::string>()); 262 EXPECT_EQ(std::string("going"), special_arg.name().To<std::string>());
249 CheckSampleEchoArgsList(list.next()); 263 CheckSampleEchoArgsList(list.next());
250 } 264 }
251 265
252 virtual void TestFinished() OVERRIDE { 266 virtual void TestFinished() OVERRIDE {
253 termination_seen_ = true; 267 termination_seen_ = true;
254 run_loop()->Quit(); 268 run_loop()->Quit();
255 } 269 }
256 270
257 bool DidSucceed() { 271 bool DidSucceed() {
258 return termination_seen_ && message_count_ == kExpectedMessageCount; 272 return termination_seen_ && message_count_ == kExpectedMessageCount;
259 } 273 }
260 274
261 private: 275 private:
262 static const int kExpectedMessageCount = 100; 276 static const int kExpectedMessageCount = 10;
263 int message_count_; 277 int message_count_;
264 bool termination_seen_; 278 bool termination_seen_;
265 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection); 279 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection);
266 }; 280 };
267 281
268 // Test that corrupted messages don't wreak havoc. 282 // Test that corrupted messages don't wreak havoc.
269 class BitFlipCppSideConnection : public CppSideConnection { 283 class BitFlipCppSideConnection : public CppSideConnection {
270 public: 284 public:
271 explicit BitFlipCppSideConnection() : termination_seen_(false) {} 285 BitFlipCppSideConnection() : termination_seen_(false) {}
272 virtual ~BitFlipCppSideConnection() {} 286 virtual ~BitFlipCppSideConnection() {}
273 287
274 // js_to_cpp::CppSide: 288 // js_to_cpp::CppSide:
275 virtual void StartTest() OVERRIDE { 289 virtual void StartTest() OVERRIDE {
276 AllocationScope scope; 290 AllocationScope scope;
277 js_side_->BitFlip(BuildSampleEchoArgs()); 291 js_side_->BitFlip(BuildSampleEchoArgs());
278 } 292 }
279 293
280 virtual void BitFlipResponse(const js_to_cpp::EchoArgs& arg) OVERRIDE { 294 virtual void BitFlipResponse(const js_to_cpp::EchoArgsList& list) OVERRIDE {
281 if (arg.is_null()) 295 CheckCorruptedEchoArgsList(list);
282 return;
283 CheckCorruptedString(arg.name());
284 CheckCorruptedStringArray(arg.string_array());
285 if (arg.data_handle().is_valid())
286 CheckDataPipe(arg.data_handle().get().value());
287 } 296 }
288 297
289 virtual void TestFinished() OVERRIDE { 298 virtual void TestFinished() OVERRIDE {
290 termination_seen_ = true; 299 termination_seen_ = true;
291 run_loop()->Quit(); 300 run_loop()->Quit();
292 } 301 }
293 302
294 bool DidSucceed() { 303 bool DidSucceed() {
295 return termination_seen_; 304 return termination_seen_;
296 } 305 }
297 306
298 private: 307 private:
299 bool termination_seen_; 308 bool termination_seen_;
300 DISALLOW_COPY_AND_ASSIGN(BitFlipCppSideConnection); 309 DISALLOW_COPY_AND_ASSIGN(BitFlipCppSideConnection);
301 }; 310 };
302 311
312 // Test that severely random messages don't wreak havoc.
313 class BackPointerCppSideConnection : public CppSideConnection {
314 public:
315 BackPointerCppSideConnection() : termination_seen_(false) {}
316 virtual ~BackPointerCppSideConnection() {}
317
318 // js_to_cpp::CppSide:
319 virtual void StartTest() OVERRIDE {
320 AllocationScope scope;
321 js_side_->BackPointer(BuildSampleEchoArgs());
322 }
323
324 virtual void BackPointerResponse(
325 const js_to_cpp::EchoArgsList& list) OVERRIDE {
326 CheckCorruptedEchoArgsList(list);
327 }
328
329 virtual void TestFinished() OVERRIDE {
330 termination_seen_ = true;
331 run_loop()->Quit();
332 }
333
334 bool DidSucceed() {
335 return termination_seen_;
336 }
337
338 private:
339 bool termination_seen_;
340 DISALLOW_COPY_AND_ASSIGN(BackPointerCppSideConnection);
341 };
342
303 } // namespace 343 } // namespace
304 344
305 class JsToCppTest : public testing::Test { 345 class JsToCppTest : public testing::Test {
306 public: 346 public:
307 JsToCppTest() {} 347 JsToCppTest() {}
308 348
309 void RunTest(const std::string& test, CppSideConnection* cpp_side) { 349 void RunTest(const std::string& test, CppSideConnection* cpp_side) {
310 cpp_side->set_run_loop(&run_loop_); 350 cpp_side->set_run_loop(&run_loop_);
311 351
312 MessagePipe pipe; 352 MessagePipe pipe;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 // TODO(tsepez): Disabled due to http://crbug.com/366797. 396 // TODO(tsepez): Disabled due to http://crbug.com/366797.
357 TEST_F(JsToCppTest, DISABLED_BitFlip) { 397 TEST_F(JsToCppTest, DISABLED_BitFlip) {
358 if (IsRunningOnIsolatedBot()) 398 if (IsRunningOnIsolatedBot())
359 return; 399 return;
360 400
361 BitFlipCppSideConnection cpp_side_connection; 401 BitFlipCppSideConnection cpp_side_connection;
362 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); 402 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection);
363 EXPECT_TRUE(cpp_side_connection.DidSucceed()); 403 EXPECT_TRUE(cpp_side_connection.DidSucceed());
364 } 404 }
365 405
406 // TODO(tsepez): Disabled due to http://crbug.com/366797.
407 TEST_F(JsToCppTest, DISABLED_BackPointer) {
408 if (IsRunningOnIsolatedBot())
409 return;
410
411 BackPointerCppSideConnection cpp_side_connection;
412 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection);
413 EXPECT_TRUE(cpp_side_connection.DidSucceed());
414 }
415
366 } // namespace js 416 } // namespace js
367 } // namespace mojo 417 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/apps/js/test/js_to_cpp.mojom ('k') | mojo/apps/js/test/js_to_cpp_unittest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698