henrike@webrtc.org | 4e5f65a | 2014-06-05 20:40:11 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_SIGSLOTTESTER_H_ |
| 12 | #define RTC_BASE_SIGSLOTTESTER_H_ |
henrike@webrtc.org | 4e5f65a | 2014-06-05 20:40:11 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 38 | #include "rtc_base/constructormagic.h" |
| 39 | #include "rtc_base/sigslot.h" |
henrike@webrtc.org | 4e5f65a | 2014-06-05 20:40:11 +0000 | [diff] [blame] | 40 | |
| 41 | namespace rtc { |
| 42 | |
honghaiz | 4deba9a | 2016-06-14 12:49:49 -0700 | [diff] [blame] | 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 | |
kjellander | 1d08100 | 2016-12-05 06:14:29 -0800 | [diff] [blame] | 56 | RTC_DISALLOW_COPY_AND_ASSIGN(SigslotTester0); |
honghaiz | 4deba9a | 2016-06-14 12:49:49 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | // Versions below are for testing signals that pass arguments. For all the |
| 60 | // templates below: |
henrike@webrtc.org | 4e5f65a | 2014-06-05 20:40:11 +0000 | [diff] [blame] | 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 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 96 | RTC_DISALLOW_COPY_AND_ASSIGN(SigslotTester$i); |
henrike@webrtc.org | 4e5f65a | 2014-06-05 20:40:11 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | ]] |
| 100 | } // namespace rtc |
| 101 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 102 | #endif // RTC_BASE_SIGSLOTTESTER_H_ |