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
CreateRect()16 RectPtr CreateRect() {
17 return Rect::New(1, 2, 3, 4);
18 }
19
20 using EqualsTest = testing::Test;
21
22 } // namespace
23
TEST_F(EqualsTest,NullStruct)24 TEST_F(EqualsTest, NullStruct) {
25 RectPtr r1;
26 RectPtr r2;
27 EXPECT_TRUE(r1.Equals(r2));
28 EXPECT_TRUE(r2.Equals(r1));
29
30 r1 = CreateRect();
31 EXPECT_FALSE(r1.Equals(r2));
32 EXPECT_FALSE(r2.Equals(r1));
33 }
34
TEST_F(EqualsTest,Struct)35 TEST_F(EqualsTest, Struct) {
36 RectPtr r1(CreateRect());
37 RectPtr r2(r1.Clone());
38 EXPECT_TRUE(r1.Equals(r2));
39 r2->y = 1;
40 EXPECT_FALSE(r1.Equals(r2));
41 r2.reset();
42 EXPECT_FALSE(r1.Equals(r2));
43 }
44
TEST_F(EqualsTest,StructNested)45 TEST_F(EqualsTest, StructNested) {
46 RectPairPtr p1(RectPair::New(CreateRect(), CreateRect()));
47 RectPairPtr p2(p1.Clone());
48 EXPECT_TRUE(p1.Equals(p2));
49 p2->second->width = 0;
50 EXPECT_FALSE(p1.Equals(p2));
51 p2->second.reset();
52 EXPECT_FALSE(p1.Equals(p2));
53 }
54
TEST_F(EqualsTest,Array)55 TEST_F(EqualsTest, Array) {
56 std::vector<RectPtr> rects;
57 rects.push_back(CreateRect());
58 NamedRegionPtr n1(NamedRegion::New(std::string("n1"), std::move(rects)));
59 NamedRegionPtr n2(n1.Clone());
60 EXPECT_TRUE(n1.Equals(n2));
61
62 n2->rects = base::nullopt;
63 EXPECT_FALSE(n1.Equals(n2));
64 n2->rects.emplace();
65 EXPECT_FALSE(n1.Equals(n2));
66
67 n2->rects->push_back(CreateRect());
68 n2->rects->push_back(CreateRect());
69 EXPECT_FALSE(n1.Equals(n2));
70
71 n2->rects->resize(1);
72 (*n2->rects)[0]->width = 0;
73 EXPECT_FALSE(n1.Equals(n2));
74
75 (*n2->rects)[0] = CreateRect();
76 EXPECT_TRUE(n1.Equals(n2));
77 }
78
TEST_F(EqualsTest,InterfacePtr)79 TEST_F(EqualsTest, InterfacePtr) {
80 base::MessageLoop message_loop;
81
82 SomeInterfacePtr inf1;
83 SomeInterfacePtr inf2;
84
85 EXPECT_TRUE(inf1.Equals(inf1));
86 EXPECT_TRUE(inf1.Equals(inf2));
87
88 auto inf1_request = MakeRequest(&inf1);
89 ALLOW_UNUSED_LOCAL(inf1_request);
90
91 EXPECT_TRUE(inf1.Equals(inf1));
92 EXPECT_FALSE(inf1.Equals(inf2));
93
94 auto inf2_request = MakeRequest(&inf2);
95 ALLOW_UNUSED_LOCAL(inf2_request);
96
97 EXPECT_FALSE(inf1.Equals(inf2));
98 }
99
TEST_F(EqualsTest,InterfaceRequest)100 TEST_F(EqualsTest, InterfaceRequest) {
101 base::MessageLoop message_loop;
102
103 InterfaceRequest<SomeInterface> req1;
104 InterfaceRequest<SomeInterface> req2;
105
106 EXPECT_TRUE(req1.Equals(req1));
107 EXPECT_TRUE(req1.Equals(req2));
108
109 SomeInterfacePtr inf1;
110 req1 = MakeRequest(&inf1);
111
112 EXPECT_TRUE(req1.Equals(req1));
113 EXPECT_FALSE(req1.Equals(req2));
114
115 SomeInterfacePtr inf2;
116 req2 = MakeRequest(&inf2);
117
118 EXPECT_FALSE(req1.Equals(req2));
119 }
120
121 } // test
122 } // mojo
123