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 <utility> 6 7 #include "base/message_loop/message_loop.h" 8 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 namespace mojo { 12 namespace test { 13 14 namespace { 15 16 RectPtr CreateRect() { 17 RectPtr r = Rect::New(); 18 r->x = 1; 19 r->y = 2; 20 r->width = 3; 21 r->height = 4; 22 return r; 23 } 24 25 using EqualsTest = testing::Test; 26 27 } // namespace 28 29 TEST_F(EqualsTest, NullStruct) { 30 RectPtr r1; 31 RectPtr r2; 32 EXPECT_TRUE(r1.Equals(r2)); 33 EXPECT_TRUE(r2.Equals(r1)); 34 35 r1 = CreateRect(); 36 EXPECT_FALSE(r1.Equals(r2)); 37 EXPECT_FALSE(r2.Equals(r1)); 38 } 39 40 TEST_F(EqualsTest, Struct) { 41 RectPtr r1(CreateRect()); 42 RectPtr r2(r1.Clone()); 43 EXPECT_TRUE(r1.Equals(r2)); 44 r2->y = 1; 45 EXPECT_FALSE(r1.Equals(r2)); 46 r2.reset(); 47 EXPECT_FALSE(r1.Equals(r2)); 48 } 49 50 TEST_F(EqualsTest, StructNested) { 51 RectPairPtr p1(RectPair::New()); 52 p1->first = CreateRect(); 53 p1->second = CreateRect(); 54 RectPairPtr p2(p1.Clone()); 55 EXPECT_TRUE(p1.Equals(p2)); 56 p2->second->width = 0; 57 EXPECT_FALSE(p1.Equals(p2)); 58 p2->second.reset(); 59 EXPECT_FALSE(p1.Equals(p2)); 60 } 61 62 TEST_F(EqualsTest, Array) { 63 NamedRegionPtr n1(NamedRegion::New()); 64 n1->name.emplace("n1"); 65 n1->rects.emplace(); 66 n1->rects->push_back(CreateRect()); 67 NamedRegionPtr n2(n1.Clone()); 68 EXPECT_TRUE(n1.Equals(n2)); 69 70 n2->rects = base::nullopt; 71 EXPECT_FALSE(n1.Equals(n2)); 72 n2->rects.emplace(); 73 EXPECT_FALSE(n1.Equals(n2)); 74 75 n2->rects->push_back(CreateRect()); 76 n2->rects->push_back(CreateRect()); 77 EXPECT_FALSE(n1.Equals(n2)); 78 79 n2->rects->resize(1); 80 (*n2->rects)[0]->width = 0; 81 EXPECT_FALSE(n1.Equals(n2)); 82 83 (*n2->rects)[0] = CreateRect(); 84 EXPECT_TRUE(n1.Equals(n2)); 85 } 86 87 TEST_F(EqualsTest, Map) { 88 auto n1(NamedRegion::New()); 89 n1->name.emplace("foo"); 90 n1->rects.emplace(); 91 n1->rects->push_back(CreateRect()); 92 93 Map<std::string, NamedRegionPtr> m1; 94 m1.insert("foo", std::move(n1)); 95 96 decltype(m1) m2; 97 EXPECT_FALSE(m1.Equals(m2)); 98 99 m2.insert("bar", m1.at("foo").Clone()); 100 EXPECT_FALSE(m1.Equals(m2)); 101 102 m2 = m1.Clone(); 103 m2.at("foo")->name.emplace("monkey"); 104 EXPECT_FALSE(m1.Equals(m2)); 105 106 m2 = m1.Clone(); 107 m2.at("foo")->rects->push_back(Rect::New()); 108 EXPECT_FALSE(m1.Equals(m2)); 109 110 m2.at("foo")->rects->resize(1); 111 (*m2.at("foo")->rects)[0]->width = 1; 112 EXPECT_FALSE(m1.Equals(m2)); 113 114 m2 = m1.Clone(); 115 EXPECT_TRUE(m1.Equals(m2)); 116 } 117 118 TEST_F(EqualsTest, InterfacePtr) { 119 base::MessageLoop message_loop; 120 121 SomeInterfacePtr inf1; 122 SomeInterfacePtr inf2; 123 124 EXPECT_TRUE(inf1.Equals(inf1)); 125 EXPECT_TRUE(inf1.Equals(inf2)); 126 127 auto inf1_request = GetProxy(&inf1); 128 ALLOW_UNUSED_LOCAL(inf1_request); 129 130 EXPECT_TRUE(inf1.Equals(inf1)); 131 EXPECT_FALSE(inf1.Equals(inf2)); 132 133 auto inf2_request = GetProxy(&inf2); 134 ALLOW_UNUSED_LOCAL(inf2_request); 135 136 EXPECT_FALSE(inf1.Equals(inf2)); 137 } 138 139 TEST_F(EqualsTest, InterfaceRequest) { 140 base::MessageLoop message_loop; 141 142 InterfaceRequest<SomeInterface> req1; 143 InterfaceRequest<SomeInterface> req2; 144 145 EXPECT_TRUE(req1.Equals(req1)); 146 EXPECT_TRUE(req1.Equals(req2)); 147 148 SomeInterfacePtr inf1; 149 req1 = GetProxy(&inf1); 150 151 EXPECT_TRUE(req1.Equals(req1)); 152 EXPECT_FALSE(req1.Equals(req2)); 153 154 SomeInterfacePtr inf2; 155 req2 = GetProxy(&inf2); 156 157 EXPECT_FALSE(req1.Equals(req2)); 158 } 159 160 } // test 161 } // mojo 162