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

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

Issue 2754003005: Mojo C++ Bindings: fix mojom::Foo::Deserialize() for component build. (Closed)
Patch Set: . Created 3 years, 9 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
« no previous file with comments | « mojo/public/cpp/bindings/tests/BUILD.gn ('k') | mojo/public/interfaces/bindings/tests/BUILD.gn » ('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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 #include <string.h> 7 #include <string.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" 10 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
11 #include "mojo/public/cpp/system/message_pipe.h" 11 #include "mojo/public/cpp/system/message_pipe.h"
12 #include "mojo/public/interfaces/bindings/tests/test_export2.mojom.h"
12 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" 13 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 15
15 namespace mojo { 16 namespace mojo {
16 namespace test { 17 namespace test {
17 namespace { 18 namespace {
18 19
19 RectPtr MakeRect(int32_t factor = 1) { 20 RectPtr MakeRect(int32_t factor = 1) {
20 return Rect::New(1 * factor, 2 * factor, 10 * factor, 20 * factor); 21 return Rect::New(1 * factor, 2 * factor, 10 * factor, 20 * factor);
21 } 22 }
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 429
429 TEST_F(StructTest, Serialization_PublicAPI) { 430 TEST_F(StructTest, Serialization_PublicAPI) {
430 { 431 {
431 // A null struct pointer. 432 // A null struct pointer.
432 RectPtr null_struct; 433 RectPtr null_struct;
433 auto data = Rect::Serialize(&null_struct); 434 auto data = Rect::Serialize(&null_struct);
434 EXPECT_TRUE(data.empty()); 435 EXPECT_TRUE(data.empty());
435 436
436 // Initialize it to non-null. 437 // Initialize it to non-null.
437 RectPtr output(Rect::New()); 438 RectPtr output(Rect::New());
438 ASSERT_TRUE(Rect::Deserialize(std::move(data), &output)); 439 ASSERT_TRUE(Rect::Deserialize(data, &output));
439 EXPECT_TRUE(output.is_null()); 440 EXPECT_TRUE(output.is_null());
440 } 441 }
441 442
442 { 443 {
443 // A struct with no fields. 444 // A struct with no fields.
444 EmptyStructPtr empty_struct(EmptyStruct::New()); 445 EmptyStructPtr empty_struct(EmptyStruct::New());
445 auto data = EmptyStruct::Serialize(&empty_struct); 446 auto data = EmptyStruct::Serialize(&empty_struct);
446 EXPECT_FALSE(data.empty()); 447 EXPECT_FALSE(data.empty());
447 448
448 EmptyStructPtr output; 449 EmptyStructPtr output;
449 ASSERT_TRUE(EmptyStruct::Deserialize(std::move(data), &output)); 450 ASSERT_TRUE(EmptyStruct::Deserialize(data, &output));
450 EXPECT_FALSE(output.is_null()); 451 EXPECT_FALSE(output.is_null());
451 } 452 }
452 453
453 { 454 {
454 // A simple struct. 455 // A simple struct.
455 RectPtr rect = MakeRect(); 456 RectPtr rect = MakeRect();
456 RectPtr cloned_rect = rect.Clone(); 457 RectPtr cloned_rect = rect.Clone();
457 auto data = Rect::Serialize(&rect); 458 auto data = Rect::Serialize(&rect);
458 459
459 RectPtr output; 460 RectPtr output;
460 ASSERT_TRUE(Rect::Deserialize(std::move(data), &output)); 461 ASSERT_TRUE(Rect::Deserialize(data, &output));
461 EXPECT_TRUE(output.Equals(cloned_rect)); 462 EXPECT_TRUE(output.Equals(cloned_rect));
462 } 463 }
463 464
464 { 465 {
465 // A struct containing other objects. 466 // A struct containing other objects.
466 std::vector<RectPtr> rects; 467 std::vector<RectPtr> rects;
467 for (size_t i = 0; i < 3; ++i) 468 for (size_t i = 0; i < 3; ++i)
468 rects.push_back(MakeRect(static_cast<int32_t>(i) + 1)); 469 rects.push_back(MakeRect(static_cast<int32_t>(i) + 1));
469 NamedRegionPtr region( 470 NamedRegionPtr region(
470 NamedRegion::New(std::string("region"), std::move(rects))); 471 NamedRegion::New(std::string("region"), std::move(rects)));
471 472
472 NamedRegionPtr cloned_region = region.Clone(); 473 NamedRegionPtr cloned_region = region.Clone();
473 auto data = NamedRegion::Serialize(&region); 474 auto data = NamedRegion::Serialize(&region);
474 475
475 // Make sure that the serialized result gets pointers encoded properly. 476 // Make sure that the serialized result gets pointers encoded properly.
476 auto cloned_data = data; 477 auto cloned_data = data;
477 NamedRegionPtr output; 478 NamedRegionPtr output;
478 ASSERT_TRUE(NamedRegion::Deserialize(std::move(cloned_data), &output)); 479 ASSERT_TRUE(NamedRegion::Deserialize(cloned_data, &output));
479 EXPECT_TRUE(output.Equals(cloned_region)); 480 EXPECT_TRUE(output.Equals(cloned_region));
480 } 481 }
481 482
482 { 483 {
483 // Deserialization failure. 484 // Deserialization failure.
484 RectPtr rect = MakeRect(); 485 RectPtr rect = MakeRect();
485 auto data = Rect::Serialize(&rect); 486 auto data = Rect::Serialize(&rect);
486 487
487 NamedRegionPtr output; 488 NamedRegionPtr output;
488 EXPECT_FALSE(NamedRegion::Deserialize(std::move(data), &output)); 489 EXPECT_FALSE(NamedRegion::Deserialize(data, &output));
490 }
491
492 {
493 // A struct from another component.
494 auto pair = test_export2::StringPair::New("hello", "world");
495 auto data = test_export2::StringPair::Serialize(&pair);
496
497 test_export2::StringPairPtr output;
498 ASSERT_TRUE(test_export2::StringPair::Deserialize(data, &output));
499 EXPECT_TRUE(output.Equals(pair));
489 } 500 }
490 } 501 }
491 502
492 TEST_F(StructTest, VersionedStructConstructor) { 503 TEST_F(StructTest, VersionedStructConstructor) {
493 auto reordered = ReorderedStruct::New(123, 456, 789); 504 auto reordered = ReorderedStruct::New(123, 456, 789);
494 EXPECT_EQ(123, reordered->a); 505 EXPECT_EQ(123, reordered->a);
495 EXPECT_EQ(456, reordered->b); 506 EXPECT_EQ(456, reordered->b);
496 EXPECT_EQ(789, reordered->c); 507 EXPECT_EQ(789, reordered->c);
497 508
498 reordered = ReorderedStruct::New(123, 456); 509 reordered = ReorderedStruct::New(123, 456);
499 EXPECT_EQ(123, reordered->a); 510 EXPECT_EQ(123, reordered->a);
500 EXPECT_EQ(6, reordered->b); 511 EXPECT_EQ(6, reordered->b);
501 EXPECT_EQ(456, reordered->c); 512 EXPECT_EQ(456, reordered->c);
502 513
503 reordered = ReorderedStruct::New(123); 514 reordered = ReorderedStruct::New(123);
504 EXPECT_EQ(3, reordered->a); 515 EXPECT_EQ(3, reordered->a);
505 EXPECT_EQ(6, reordered->b); 516 EXPECT_EQ(6, reordered->b);
506 EXPECT_EQ(123, reordered->c); 517 EXPECT_EQ(123, reordered->c);
507 518
508 reordered = ReorderedStruct::New(); 519 reordered = ReorderedStruct::New();
509 EXPECT_EQ(3, reordered->a); 520 EXPECT_EQ(3, reordered->a);
510 EXPECT_EQ(6, reordered->b); 521 EXPECT_EQ(6, reordered->b);
511 EXPECT_EQ(1, reordered->c); 522 EXPECT_EQ(1, reordered->c);
512 } 523 }
513 524
514 } // namespace test 525 } // namespace test
515 } // namespace mojo 526 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/BUILD.gn ('k') | mojo/public/interfaces/bindings/tests/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698