blob: c3d2d6e99b6d4129b22dde40dc64b89ff1e389b5 [file] [log] [blame]
henrike@webrtc.org4e5f65a2014-06-05 20:40:111/*
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 04:47:3111#ifndef RTC_BASE_SIGSLOTTESTER_H_
12#define RTC_BASE_SIGSLOTTESTER_H_
henrike@webrtc.org4e5f65a2014-06-05 20:40:1113
14// To generate sigslottester.h from sigslottester.h.pump, execute:
15// /home/build/google3/third_party/gtest/scripts/pump.py sigslottester.h.pump
16
17
18// SigslotTester(s) are utility classes to check if signals owned by an
19// object are being invoked at the right time and with the right arguments.
20// They are meant to be used in tests. Tests must provide "capture" pointers
21// (i.e. address of variables) where the arguments from the signal callback
22// can be stored.
23//
24// Example:
25// /* Some signal */
26// sigslot::signal1<const std::string&> foo;
27//
28// /* We want to monitor foo in some test. Note how signal argument is
29// const std::string&, but capture-type is std::string. Capture type
30// must be type that can be assigned to. */
31// std::string capture;
32// SigslotTester1<const std::string&, std::string> slot(&foo, &capture);
33// foo.emit("hello");
34// EXPECT_EQ(1, slot.callback_count());
35// EXPECT_EQ("hello", capture);
36// /* See unit-tests for more examples */
37
Artem Titove41c4332018-07-25 13:04:2838#include "rtc_base/third_party/sigslot/sigslot.h"
henrike@webrtc.org4e5f65a2014-06-05 20:40:1139
40namespace rtc {
41
honghaiz4deba9a2016-06-14 19:49:4942// Base version for testing signals that passes no arguments.
43class SigslotTester0 : public sigslot::has_slots<> {
44 public:
45 explicit SigslotTester0(sigslot::signal0<>* signal) : callback_count_(0) {
46 signal->connect(this, &SigslotTester0::OnSignalCallback);
47 }
48
Artem Titov6cae2d52022-01-26 15:01:1049 SigslotTester0(const SigslotTester0&) = delete;
50 SigslotTester0& operator=(const SigslotTester0&) = delete;
51
honghaiz4deba9a2016-06-14 19:49:4952 int callback_count() const { return callback_count_; }
53
54 private:
55 void OnSignalCallback() { callback_count_++; }
56 int callback_count_;
honghaiz4deba9a2016-06-14 19:49:4957};
58
59// Versions below are for testing signals that pass arguments. For all the
60// templates below:
henrike@webrtc.org4e5f65a2014-06-05 20:40:1161// - A1-A5 is the type of the argument i in the callback. Signals may and often
62// do use const-references here for efficiency.
63// - C1-C5 is the type of the variable to capture argument i. These should be
64// non-const value types suitable for use as lvalues.
65
66$var n = 5
67$range i 1..n
68$for i [[
69$range j 1..i
70
71template <$for j , [[class A$j]], $for j , [[class C$j]]>
72class SigslotTester$i : public sigslot::has_slots<> {
73 public:
74 SigslotTester$i(sigslot::signal$i<$for j , [[A$j]]>* signal,
75 $for j , [[C$j* capture$j]])
76 : callback_count_(0),
77 $for j , [[capture$j[[]]_(capture$j)]] {
78 signal->connect(this, &SigslotTester$i::OnSignalCallback);
79 }
80
Artem Titov6cae2d52022-01-26 15:01:1081 SigslotTester$i(const SigslotTester$i&) = delete;
82 SigslotTester$i& operator=(const SigslotTester$i&) = delete;
83
henrike@webrtc.org4e5f65a2014-06-05 20:40:1184 int callback_count() const { return callback_count_; }
85
86 private:
87 void OnSignalCallback($for j , [[A$j arg$j]]) {
88 callback_count_++;$for j [[
89
90 *capture$j[[]]_ = arg$j;]]
91
92 }
93
94 int callback_count_;$for j [[
95
96 C$j* capture$j[[]]_;]]
henrike@webrtc.org4e5f65a2014-06-05 20:40:1197};
98
99]]
100} // namespace rtc
101
Mirko Bonadei92ea95e2017-09-15 04:47:31102#endif // RTC_BASE_SIGSLOTTESTER_H_