blob: c8e87e5a86c76b29913fd7918351734c823a40bd [file] [log] [blame]
henrike@webrtc.org4e5f65a2014-06-05 20:40:11 +00001/*
2 * Copyright 2014 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/sigslottester.h"
henrike@webrtc.org4e5f65a2014-06-05 20:40:11 +000012
13#include <string>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/gunit.h"
16#include "rtc_base/sigslot.h"
henrike@webrtc.org4e5f65a2014-06-05 20:40:11 +000017
18namespace rtc {
19
20TEST(SigslotTester, TestSignal1Arg) {
21 sigslot::signal1<int> source1;
22 int capture1;
23 SigslotTester1<int, int> slot1(&source1, &capture1);
24 EXPECT_EQ(0, slot1.callback_count());
25
26 source1.emit(10);
27 EXPECT_EQ(1, slot1.callback_count());
28 EXPECT_EQ(10, capture1);
29
30 source1.emit(20);
31 EXPECT_EQ(2, slot1.callback_count());
32 EXPECT_EQ(20, capture1);
33}
34
35TEST(SigslotTester, TestSignal2Args) {
36 sigslot::signal2<int, char> source2;
37 int capture1;
38 char capture2;
39 SigslotTester2<int, char, int, char> slot2(&source2, &capture1, &capture2);
40 EXPECT_EQ(0, slot2.callback_count());
41
42 source2.emit(10, 'x');
43 EXPECT_EQ(1, slot2.callback_count());
44 EXPECT_EQ(10, capture1);
45 EXPECT_EQ('x', capture2);
46
47 source2.emit(20, 'y');
48 EXPECT_EQ(2, slot2.callback_count());
49 EXPECT_EQ(20, capture1);
50 EXPECT_EQ('y', capture2);
51}
52
53// Since it applies for 1 and 2 args, we assume it will work for up to 5 args.
54
55TEST(SigslotTester, TestSignalWithConstReferenceArgs) {
56 sigslot::signal1<const std::string&> source1;
57 std::string capture1;
58 SigslotTester1<const std::string&, std::string> slot1(&source1, &capture1);
59 EXPECT_EQ(0, slot1.callback_count());
60 source1.emit("hello");
61 EXPECT_EQ(1, slot1.callback_count());
62 EXPECT_EQ("hello", capture1);
63}
64
65TEST(SigslotTester, TestSignalWithPointerToConstArgs) {
66 sigslot::signal1<const std::string*> source1;
67 const std::string* capture1;
68 SigslotTester1<const std::string*, const std::string*> slot1(&source1,
69 &capture1);
70 EXPECT_EQ(0, slot1.callback_count());
deadbeef37f5ecf2017-02-27 14:06:41 -080071 source1.emit(nullptr);
henrike@webrtc.org4e5f65a2014-06-05 20:40:11 +000072 EXPECT_EQ(1, slot1.callback_count());
deadbeef37f5ecf2017-02-27 14:06:41 -080073 EXPECT_EQ(nullptr, capture1);
henrike@webrtc.org4e5f65a2014-06-05 20:40:11 +000074}
75
76TEST(SigslotTester, TestSignalWithConstPointerArgs) {
77 sigslot::signal1<std::string* const> source1;
78 std::string* capture1;
79 SigslotTester1<std::string* const, std::string*> slot1(&source1, &capture1);
80 EXPECT_EQ(0, slot1.callback_count());
deadbeef37f5ecf2017-02-27 14:06:41 -080081 source1.emit(nullptr);
henrike@webrtc.org4e5f65a2014-06-05 20:40:11 +000082 EXPECT_EQ(1, slot1.callback_count());
deadbeef37f5ecf2017-02-27 14:06:41 -080083 EXPECT_EQ(nullptr, capture1);
henrike@webrtc.org4e5f65a2014-06-05 20:40:11 +000084}
85
86} // namespace rtc