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 3 // found in the LICENSE file. 4 5 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" 6 #include "mojo/public/cpp/environment/environment.h" 7 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" 8 #include "testing/gtest/include/gtest/gtest.h" 9 10 namespace mojo { 11 namespace test { 12 namespace { 13 14 RectPtr MakeRect(int32_t factor = 1) { 15 RectPtr rect(Rect::New()); 16 rect->x = 1 * factor; 17 rect->y = 2 * factor; 18 rect->width = 10 * factor; 19 rect->height = 20 * factor; 20 return rect.Pass(); 21 } 22 23 void CheckRect(const Rect& rect, int32_t factor = 1) { 24 EXPECT_EQ(1 * factor, rect.x); 25 EXPECT_EQ(2 * factor, rect.y); 26 EXPECT_EQ(10 * factor, rect.width); 27 EXPECT_EQ(20 * factor, rect.height); 28 } 29 30 class StructTest : public testing::Test { 31 public: 32 virtual ~StructTest() {} 33 34 private: 35 Environment env_; 36 }; 37 38 } // namespace 39 40 TEST_F(StructTest, Rect) { 41 RectPtr rect; 42 EXPECT_TRUE(rect.is_null()); 43 EXPECT_TRUE(!rect); 44 EXPECT_FALSE(rect); 45 46 rect = MakeRect(); 47 EXPECT_FALSE(rect.is_null()); 48 EXPECT_FALSE(!rect); 49 EXPECT_TRUE(rect); 50 51 CheckRect(*rect); 52 } 53 54 // Serialization test of a struct with no pointer or handle members. 55 TEST_F(StructTest, Serialization_Basic) { 56 RectPtr rect(MakeRect()); 57 58 size_t size = GetSerializedSize_(rect); 59 EXPECT_EQ(8U + 16U, size); 60 61 mojo::internal::FixedBuffer buf(size); 62 internal::Rect_Data* data; 63 Serialize_(rect.Pass(), &buf, &data); 64 65 RectPtr rect2; 66 Deserialize_(data, &rect2); 67 68 CheckRect(*rect2); 69 } 70 71 // Serialization test of a struct with struct pointers. 72 TEST_F(StructTest, Serialization_StructPointers) { 73 RectPairPtr pair(RectPair::New()); 74 pair->first = MakeRect(); 75 pair->second = MakeRect(); 76 77 size_t size = GetSerializedSize_(pair); 78 EXPECT_EQ(8U + 16U + 2*(8U + 16U), size); 79 80 mojo::internal::FixedBuffer buf(size); 81 internal::RectPair_Data* data; 82 Serialize_(pair.Pass(), &buf, &data); 83 84 RectPairPtr pair2; 85 Deserialize_(data, &pair2); 86 87 CheckRect(*pair2->first); 88 CheckRect(*pair2->second); 89 } 90 91 // Serialization test of a struct with an array member. 92 TEST_F(StructTest, Serialization_ArrayPointers) { 93 NamedRegionPtr region(NamedRegion::New()); 94 region->name = "region"; 95 region->rects = Array<RectPtr>::New(4); 96 for (size_t i = 0; i < region->rects.size(); ++i) 97 region->rects[i] = MakeRect(static_cast<int32_t>(i) + 1); 98 99 size_t size = GetSerializedSize_(region); 100 EXPECT_EQ(8U + // header 101 8U + // name pointer 102 8U + // rects pointer 103 8U + // name header 104 8U + // name payload (rounded up) 105 8U + // rects header 106 4*8U + // rects payload (four pointers) 107 4*(8U + // rect header 108 16U), // rect payload (four ints) 109 size); 110 111 mojo::internal::FixedBuffer buf(size); 112 internal::NamedRegion_Data* data; 113 Serialize_(region.Pass(), &buf, &data); 114 115 NamedRegionPtr region2; 116 Deserialize_(data, ®ion2); 117 118 EXPECT_EQ(String("region"), region2->name); 119 120 EXPECT_EQ(4U, region2->rects.size()); 121 for (size_t i = 0; i < region2->rects.size(); ++i) 122 CheckRect(*region2->rects[i], static_cast<int32_t>(i) + 1); 123 } 124 125 // Serialization test of a struct with null array pointers. 126 TEST_F(StructTest, Serialization_NullArrayPointers) { 127 NamedRegionPtr region(NamedRegion::New()); 128 EXPECT_TRUE(region->name.is_null()); 129 EXPECT_TRUE(region->rects.is_null()); 130 131 size_t size = GetSerializedSize_(region); 132 EXPECT_EQ(8U + // header 133 8U + // name pointer 134 8U, // rects pointer 135 size); 136 137 mojo::internal::FixedBuffer buf(size); 138 internal::NamedRegion_Data* data; 139 Serialize_(region.Pass(), &buf, &data); 140 141 NamedRegionPtr region2; 142 Deserialize_(data, ®ion2); 143 144 EXPECT_TRUE(region2->name.is_null()); 145 EXPECT_TRUE(region2->rects.is_null()); 146 } 147 148 } // namespace test 149 } // namespace mojo 150