OLD | NEW |
| (Empty) |
1 /* | |
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 | |
11 #ifndef WEBRTC_BASE_SIGSLOTTESTER_H_ | |
12 #define WEBRTC_BASE_SIGSLOTTESTER_H_ | |
13 | |
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 | |
38 #include "webrtc/base/constructormagic.h" | |
39 #include "webrtc/base/sigslot.h" | |
40 | |
41 namespace rtc { | |
42 | |
43 // Base version for testing signals that passes no arguments. | |
44 class SigslotTester0 : public sigslot::has_slots<> { | |
45 public: | |
46 explicit SigslotTester0(sigslot::signal0<>* signal) : callback_count_(0) { | |
47 signal->connect(this, &SigslotTester0::OnSignalCallback); | |
48 } | |
49 | |
50 int callback_count() const { return callback_count_; } | |
51 | |
52 private: | |
53 void OnSignalCallback() { callback_count_++; } | |
54 int callback_count_; | |
55 | |
56 RTC_DISALLOW_COPY_AND_ASSIGN(SigslotTester0); | |
57 }; | |
58 | |
59 // Versions below are for testing signals that pass arguments. For all the | |
60 // templates below: | |
61 // - 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 | |
71 template <$for j , [[class A$j]], $for j , [[class C$j]]> | |
72 class 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 | |
81 int callback_count() const { return callback_count_; } | |
82 | |
83 private: | |
84 void OnSignalCallback($for j , [[A$j arg$j]]) { | |
85 callback_count_++;$for j [[ | |
86 | |
87 *capture$j[[]]_ = arg$j;]] | |
88 | |
89 } | |
90 | |
91 int callback_count_;$for j [[ | |
92 | |
93 C$j* capture$j[[]]_;]] | |
94 | |
95 | |
96 RTC_DISALLOW_COPY_AND_ASSIGN(SigslotTester$i); | |
97 }; | |
98 | |
99 ]] | |
100 } // namespace rtc | |
101 | |
102 #endif // WEBRTC_BASE_SIGSLOTTESTER_H_ | |
OLD | NEW |