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 // For all the templates below: 44 // - A1-A5 is the type of the argument i in the callback. Signals may and often 45 // do use const-references here for efficiency. 46 // - C1-C5 is the type of the variable to capture argument i. These should be 47 // non-const value types suitable for use as lvalues. 48 49 $var n = 5 50 $range i 1..n 51 $for i [[ 52 $range j 1..i 53 54 template <$for j , [[class A$j]], $for j , [[class C$j]]> 55 class SigslotTester$i : public sigslot::has_slots<> { 56 public: 57 SigslotTester$i(sigslot::signal$i<$for j , [[A$j]]>* signal, 58 $for j , [[C$j* capture$j]]) 59 : callback_count_(0), 60 $for j , [[capture$j[[]]_(capture$j)]] { 61 signal->connect(this, &SigslotTester$i::OnSignalCallback); 62 } 63 64 int callback_count() const { return callback_count_; } 65 66 private: 67 void OnSignalCallback($for j , [[A$j arg$j]]) { 68 callback_count_++;$for j [[ 69 70 *capture$j[[]]_ = arg$j;]] 71 72 } 73 74 int callback_count_;$for j [[ 75 76 C$j* capture$j[[]]_;]] 77 78 79 DISALLOW_COPY_AND_ASSIGN(SigslotTester$i); 80 }; 81 82 ]] 83 } // namespace rtc 84 85 #endif // WEBRTC_BASE_SIGSLOTTESTER_H_ 86