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

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

Issue 2076313002: Mojo C++ bindings: expose serialization public API for mojo structs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 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
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"
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 NativeStructPtr output_native; 483 NativeStructPtr output_native;
484 mojo::internal::Deserialize<NativeStructPtr>(data, &output_native, nullptr); 484 mojo::internal::Deserialize<NativeStructPtr>(data, &output_native, nullptr);
485 EXPECT_FALSE(output_native.is_null()); 485 EXPECT_FALSE(output_native.is_null());
486 EXPECT_FALSE(output_native->data.is_null()); 486 EXPECT_FALSE(output_native->data.is_null());
487 EXPECT_EQ(2u, output_native->data.size()); 487 EXPECT_EQ(2u, output_native->data.size());
488 EXPECT_EQ('X', output_native->data[0]); 488 EXPECT_EQ('X', output_native->data[0]);
489 EXPECT_EQ('Y', output_native->data[1]); 489 EXPECT_EQ('Y', output_native->data[1]);
490 } 490 }
491 } 491 }
492 492
493 TEST_F(StructTest, Serialization_PublicAPI) {
494 {
495 // A null struct pointer.
496 RectPtr null_struct;
497 mojo::Array<uint8_t> data = Rect::Serialize(&null_struct);
498 EXPECT_TRUE(data.empty());
499
500 // Initialize it to non-null.
501 RectPtr output(Rect::New());
502 ASSERT_TRUE(Rect::Deserialize(std::move(data), &output));
503 EXPECT_TRUE(output.is_null());
504 }
505
506 {
507 // A struct with no fields.
508 EmptyStructPtr empty_struct(EmptyStruct::New());
509 mojo::Array<uint8_t> data = EmptyStruct::Serialize(&empty_struct);
510 EXPECT_FALSE(data.empty());
511
512 EmptyStructPtr output;
513 ASSERT_TRUE(EmptyStruct::Deserialize(std::move(data), &output));
514 EXPECT_FALSE(output.is_null());
515 }
516
517 {
518 // A simple struct.
519 RectPtr rect = MakeRect();
520 RectPtr cloned_rect = rect.Clone();
521 mojo::Array<uint8_t> data = Rect::Serialize(&rect);
522
523 RectPtr output;
524 ASSERT_TRUE(Rect::Deserialize(std::move(data), &output));
525 EXPECT_TRUE(output.Equals(cloned_rect));
526 }
527
528 {
529 // A struct containing other objects.
530 NamedRegionPtr region(NamedRegion::New());
531 region->name = "region";
532 region->rects = Array<RectPtr>::New(4);
533 for (size_t i = 0; i < region->rects.size(); ++i)
534 region->rects[i] = MakeRect(static_cast<int32_t>(i) + 1);
535
536 NamedRegionPtr cloned_region = region.Clone();
537 mojo::Array<uint8_t> data = NamedRegion::Serialize(&region);
538
539 // Make sure that the serialized result gets pointers encoded properly.
540 mojo::Array<uint8_t> cloned_data = data.Clone();
541 NamedRegionPtr output;
542 ASSERT_TRUE(NamedRegion::Deserialize(std::move(cloned_data), &output));
543 EXPECT_TRUE(output.Equals(cloned_region));
544 }
545
546 {
547 // Deserialization failure.
548 RectPtr rect = MakeRect();
549 mojo::Array<uint8_t> data = Rect::Serialize(&rect);
550
551 NamedRegionPtr output;
552 EXPECT_FALSE(NamedRegion::Deserialize(std::move(data), &output));
553 }
554 }
555
493 } // namespace test 556 } // namespace test
494 } // namespace mojo 557 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/struct_traits_unittest.cc ('k') | mojo/public/cpp/bindings/tests/wtf_types_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698