You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef RTC_BASE_SIGSLOTTESTER_H_
  11. #define RTC_BASE_SIGSLOTTESTER_H_
  12. // To generate sigslottester.h from sigslottester.h.pump, execute:
  13. // /home/build/google3/third_party/gtest/scripts/pump.py sigslottester.h.pump
  14. // SigslotTester(s) are utility classes to check if signals owned by an
  15. // object are being invoked at the right time and with the right arguments.
  16. // They are meant to be used in tests. Tests must provide "capture" pointers
  17. // (i.e. address of variables) where the arguments from the signal callback
  18. // can be stored.
  19. //
  20. // Example:
  21. // /* Some signal */
  22. // sigslot::signal1<const std::string&> foo;
  23. //
  24. // /* We want to monitor foo in some test. Note how signal argument is
  25. // const std::string&, but capture-type is std::string. Capture type
  26. // must be type that can be assigned to. */
  27. // std::string capture;
  28. // SigslotTester1<const std::string&, std::string> slot(&foo, &capture);
  29. // foo.emit("hello");
  30. // EXPECT_EQ(1, slot.callback_count());
  31. // EXPECT_EQ("hello", capture);
  32. // /* See unit-tests for more examples */
  33. #include "rtc_base/constructor_magic.h"
  34. #include "rtc_base/third_party/sigslot/sigslot.h"
  35. namespace rtc {
  36. // Base version for testing signals that passes no arguments.
  37. class SigslotTester0 : public sigslot::has_slots<> {
  38. public:
  39. explicit SigslotTester0(sigslot::signal0<>* signal) : callback_count_(0) {
  40. signal->connect(this, &SigslotTester0::OnSignalCallback);
  41. }
  42. int callback_count() const { return callback_count_; }
  43. private:
  44. void OnSignalCallback() { callback_count_++; }
  45. int callback_count_;
  46. RTC_DISALLOW_COPY_AND_ASSIGN(SigslotTester0);
  47. };
  48. // Versions below are for testing signals that pass arguments. For all the
  49. // templates below:
  50. // - A1-A5 is the type of the argument i in the callback. Signals may and often
  51. // do use const-references here for efficiency.
  52. // - C1-C5 is the type of the variable to capture argument i. These should be
  53. // non-const value types suitable for use as lvalues.
  54. $var n = 5
  55. $range i 1..n
  56. $for i [[
  57. $range j 1..i
  58. template <$for j , [[class A$j]], $for j , [[class C$j]]>
  59. class SigslotTester$i : public sigslot::has_slots<> {
  60. public:
  61. SigslotTester$i(sigslot::signal$i<$for j , [[A$j]]>* signal,
  62. $for j , [[C$j* capture$j]])
  63. : callback_count_(0),
  64. $for j , [[capture$j[[]]_(capture$j)]] {
  65. signal->connect(this, &SigslotTester$i::OnSignalCallback);
  66. }
  67. int callback_count() const { return callback_count_; }
  68. private:
  69. void OnSignalCallback($for j , [[A$j arg$j]]) {
  70. callback_count_++;$for j [[
  71. *capture$j[[]]_ = arg$j;]]
  72. }
  73. int callback_count_;$for j [[
  74. C$j* capture$j[[]]_;]]
  75. RTC_DISALLOW_COPY_AND_ASSIGN(SigslotTester$i);
  76. };
  77. ]]
  78. } // namespace rtc
  79. #endif // RTC_BASE_SIGSLOTTESTER_H_