blob: 811f85ead348e5703fae5d057174cd668d425cf4 [file] [log] [blame]
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -07001/*
2 * Copyright 2020 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10#include <string>
11#include <type_traits>
12
13#include "api/function_view.h"
14#include "rtc_base/bind.h"
Mirko Bonadei3d259352020-10-23 12:04:40 +020015#include "rtc_base/callback_list.h"
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -070016#include "test/gtest.h"
17
18namespace webrtc {
19namespace {
20
Mirko Bonadei3d259352020-10-23 12:04:40 +020021TEST(CallbackList, NoRecieverSingleMessageTest) {
22 CallbackList<std::string> c;
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -070023
24 c.Send("message");
25}
26
Mirko Bonadei3d259352020-10-23 12:04:40 +020027TEST(CallbackList, MultipleParameterMessageTest) {
28 CallbackList<const std::string&, std::string, std::string&&, int, int*,
Lahiru Ginnaliya Gamathige52fa9922020-09-21 12:28:48 -070029 std::string&>
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -070030 c;
31 std::string str = "messege";
32 int i = 10;
33
34 c.Send(str, "message1", "message0", 123, &i, str);
35}
36
Mirko Bonadei3d259352020-10-23 12:04:40 +020037TEST(CallbackList, NoParameterMessageTest) {
38 CallbackList<> c;
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -070039
40 c.Send();
41}
42
Mirko Bonadei3d259352020-10-23 12:04:40 +020043TEST(CallbackList, ReferenceTest) {
44 CallbackList<int&> c;
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -070045 int index = 1;
46
47 c.AddReceiver([](int& index) { index++; });
48 c.Send(index);
49
50 EXPECT_EQ(index, 2);
51}
52
Lahiru Ginnaliya Gamathigee99c68d2020-09-30 14:33:45 -070053enum State {
54 kNew,
55 kChecking,
56};
57
Mirko Bonadei3d259352020-10-23 12:04:40 +020058TEST(CallbackList, SingleEnumValueTest) {
59 CallbackList<State> c;
Lahiru Ginnaliya Gamathigee99c68d2020-09-30 14:33:45 -070060 State s1 = kNew;
61 int index = 0;
62
63 c.AddReceiver([&index](State s) { index++; });
64 c.Send(s1);
65
66 EXPECT_EQ(index, 1);
67}
68
Mirko Bonadei3d259352020-10-23 12:04:40 +020069TEST(CallbackList, SingleEnumReferenceTest) {
70 CallbackList<State&> c;
Lahiru Ginnaliya Gamathigee99c68d2020-09-30 14:33:45 -070071 State s = kNew;
72
73 c.AddReceiver([](State& s) { s = kChecking; });
74 c.Send(s);
75
76 EXPECT_EQ(s, kChecking);
77}
78
Mirko Bonadei3d259352020-10-23 12:04:40 +020079TEST(CallbackList, ConstReferenceTest) {
80 CallbackList<int&> c;
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -070081 int i = 0;
82 int index = 1;
83
84 c.AddReceiver([&i](const int& index) { i = index; });
85 c.Send(index);
86
87 EXPECT_EQ(i, 1);
88}
89
Mirko Bonadei3d259352020-10-23 12:04:40 +020090TEST(CallbackList, PointerTest) {
91 CallbackList<int*> c;
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -070092 int index = 1;
93
94 c.AddReceiver([](int* index) { (*index)++; });
95 c.Send(&index);
96
97 EXPECT_EQ(index, 2);
98}
99
Mirko Bonadei3d259352020-10-23 12:04:40 +0200100TEST(CallbackList, CallByValue) {
101 CallbackList<int> c;
Karl Wibergd88b1692020-09-25 06:16:12 +0200102 int x = 17;
103
104 c.AddReceiver([&x](int n) { x += n; });
105 int y = 89;
106 c.Send(y);
107
108 EXPECT_EQ(x, 106);
109}
110
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -0700111void PlusOne(int& a) {
112 a++;
113}
114
Mirko Bonadei3d259352020-10-23 12:04:40 +0200115TEST(CallbackList, FunctionPtrTest) {
116 CallbackList<int&> c;
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -0700117 int index = 1;
118
119 c.AddReceiver(PlusOne);
120 c.Send(index);
121
122 EXPECT_EQ(index, 2);
123}
124
125struct LargeNonTrivial {
126 int a[17];
127
128 LargeNonTrivial() = default;
129 LargeNonTrivial(LargeNonTrivial&& m) {}
130 ~LargeNonTrivial() = default;
131
132 void operator()(int& a) { a = 1; }
133};
134
Mirko Bonadei3d259352020-10-23 12:04:40 +0200135TEST(CallbackList, LargeNonTrivialTest) {
136 CallbackList<int&> c;
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -0700137 int i = 0;
Karl Wiberg84ba18a2020-10-06 12:10:59 +0200138 static_assert(sizeof(LargeNonTrivial) > UntypedFunction::kInlineStorageSize,
139 "");
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -0700140 c.AddReceiver(LargeNonTrivial());
141 c.Send(i);
142
143 EXPECT_EQ(i, 1);
144}
145
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -0700146struct LargeTrivial {
147 int a[17];
148 void operator()(int& x) { x = 1; }
149};
150
Mirko Bonadei3d259352020-10-23 12:04:40 +0200151TEST(CallbackList, LargeTrivial) {
152 CallbackList<int&> c;
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -0700153 LargeTrivial lt;
154 int i = 0;
155
Karl Wiberg84ba18a2020-10-06 12:10:59 +0200156 static_assert(sizeof(lt) > UntypedFunction::kInlineStorageSize, "");
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -0700157 c.AddReceiver(lt);
158 c.Send(i);
159
160 EXPECT_EQ(i, 1);
161}
162
163struct OnlyNonTriviallyConstructible {
164 OnlyNonTriviallyConstructible() = default;
165 OnlyNonTriviallyConstructible(OnlyNonTriviallyConstructible&& m) {}
166
167 void operator()(int& a) { a = 1; }
168};
169
Mirko Bonadei3d259352020-10-23 12:04:40 +0200170TEST(CallbackList, OnlyNonTriviallyMoveConstructible) {
171 CallbackList<int&> c;
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -0700172 int i = 0;
173
174 c.AddReceiver(OnlyNonTriviallyConstructible());
175 c.Send(i);
176
177 EXPECT_EQ(i, 1);
178}
179
Mirko Bonadei3d259352020-10-23 12:04:40 +0200180TEST(CallbackList, MultipleReceiverSendTest) {
181 CallbackList<int&> c;
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -0700182 std::function<void(int&)> plus = PlusOne;
183 int index = 1;
184
185 c.AddReceiver(plus);
186 c.AddReceiver([](int& i) { i--; });
187 c.AddReceiver(plus);
188 c.AddReceiver(plus);
189 c.Send(index);
190 c.Send(index);
191
192 EXPECT_EQ(index, 5);
193}
Lahiru Ginnaliya Gamathigee99c68d2020-09-30 14:33:45 -0700194
195class A {
196 public:
197 void increment(int& i) const { i++; }
198};
199
Mirko Bonadei3d259352020-10-23 12:04:40 +0200200TEST(CallbackList, MemberFunctionTest) {
201 CallbackList<int&> c;
Lahiru Ginnaliya Gamathigee99c68d2020-09-30 14:33:45 -0700202 A a;
203 int index = 1;
204
205 c.AddReceiver([&a](int& i) { a.increment(i); });
206 c.Send(index);
207
208 EXPECT_EQ(index, 2);
209}
Lahiru Ginnaliya Gamathige3e982802020-09-16 10:34:21 -0700210// todo(glahiru): Add a test case to catch some error for Karl's first fix
211// todo(glahiru): Add a test for rtc::Bind
212// which used the following code in the Send
213} // namespace
214} // namespace webrtc