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.

147 lines
3.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 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. #include "rtc_base/callback.h"
  11. #include "rtc_base/bind.h"
  12. #include "rtc_base/keep_ref_until_done.h"
  13. #include "rtc_base/ref_count.h"
  14. #include "test/gtest.h"
  15. namespace rtc {
  16. namespace {
  17. void f() {}
  18. int g() {
  19. return 42;
  20. }
  21. int h(int x) {
  22. return x * x;
  23. }
  24. void i(int& x) {
  25. x *= x;
  26. } // NOLINT: Testing refs
  27. struct BindTester {
  28. int a() { return 24; }
  29. int b(int x) const { return x * x; }
  30. };
  31. class RefCountedBindTester : public RefCountInterface {
  32. public:
  33. RefCountedBindTester() : count_(0) {}
  34. void AddRef() const override { ++count_; }
  35. RefCountReleaseStatus Release() const override {
  36. --count_;
  37. return count_ == 0 ? RefCountReleaseStatus::kDroppedLastRef
  38. : RefCountReleaseStatus::kOtherRefsRemained;
  39. }
  40. int RefCount() const { return count_; }
  41. private:
  42. mutable int count_;
  43. };
  44. } // namespace
  45. TEST(CallbackTest, VoidReturn) {
  46. Callback0<void> cb;
  47. EXPECT_TRUE(cb.empty());
  48. cb(); // Executing an empty callback should not crash.
  49. cb = Callback0<void>(&f);
  50. EXPECT_FALSE(cb.empty());
  51. cb();
  52. }
  53. TEST(CallbackTest, IntReturn) {
  54. Callback0<int> cb;
  55. EXPECT_TRUE(cb.empty());
  56. cb = Callback0<int>(&g);
  57. EXPECT_FALSE(cb.empty());
  58. EXPECT_EQ(42, cb());
  59. EXPECT_EQ(42, cb());
  60. }
  61. TEST(CallbackTest, OneParam) {
  62. Callback1<int, int> cb1(&h);
  63. EXPECT_FALSE(cb1.empty());
  64. EXPECT_EQ(9, cb1(-3));
  65. EXPECT_EQ(100, cb1(10));
  66. // Try clearing a callback.
  67. cb1 = Callback1<int, int>();
  68. EXPECT_TRUE(cb1.empty());
  69. // Try a callback with a ref parameter.
  70. Callback1<void, int&> cb2(&i);
  71. int x = 3;
  72. cb2(x);
  73. EXPECT_EQ(9, x);
  74. cb2(x);
  75. EXPECT_EQ(81, x);
  76. }
  77. TEST(CallbackTest, WithBind) {
  78. BindTester t;
  79. Callback0<int> cb1 = Bind(&BindTester::a, &t);
  80. EXPECT_EQ(24, cb1());
  81. EXPECT_EQ(24, cb1());
  82. cb1 = Bind(&BindTester::b, &t, 10);
  83. EXPECT_EQ(100, cb1());
  84. EXPECT_EQ(100, cb1());
  85. cb1 = Bind(&BindTester::b, &t, 5);
  86. EXPECT_EQ(25, cb1());
  87. EXPECT_EQ(25, cb1());
  88. }
  89. TEST(KeepRefUntilDoneTest, simple) {
  90. RefCountedBindTester t;
  91. EXPECT_EQ(0, t.RefCount());
  92. {
  93. Callback0<void> cb = KeepRefUntilDone(&t);
  94. EXPECT_EQ(1, t.RefCount());
  95. cb();
  96. EXPECT_EQ(1, t.RefCount());
  97. cb();
  98. EXPECT_EQ(1, t.RefCount());
  99. }
  100. EXPECT_EQ(0, t.RefCount());
  101. }
  102. TEST(KeepRefUntilDoneTest, copy) {
  103. RefCountedBindTester t;
  104. EXPECT_EQ(0, t.RefCount());
  105. Callback0<void> cb2;
  106. {
  107. Callback0<void> cb = KeepRefUntilDone(&t);
  108. EXPECT_EQ(1, t.RefCount());
  109. cb2 = cb;
  110. }
  111. EXPECT_EQ(1, t.RefCount());
  112. cb2 = Callback0<void>();
  113. EXPECT_EQ(0, t.RefCount());
  114. }
  115. TEST(KeepRefUntilDoneTest, scopedref) {
  116. RefCountedBindTester t;
  117. EXPECT_EQ(0, t.RefCount());
  118. {
  119. scoped_refptr<RefCountedBindTester> t_scoped_ref(&t);
  120. Callback0<void> cb = KeepRefUntilDone(t_scoped_ref);
  121. t_scoped_ref = nullptr;
  122. EXPECT_EQ(1, t.RefCount());
  123. cb();
  124. EXPECT_EQ(1, t.RefCount());
  125. }
  126. EXPECT_EQ(0, t.RefCount());
  127. }
  128. } // namespace rtc