cancelable_callback_unittest.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/cancelable_callback.h"
  5. #include <memory>
  6. #include "base/bind.h"
  7. #include "base/callback_helpers.h"
  8. #include "base/location.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/run_loop.h"
  11. #include "base/task/single_thread_task_runner.h"
  12. #include "base/test/task_environment.h"
  13. #include "base/threading/thread_task_runner_handle.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. namespace base {
  16. namespace {
  17. class TestRefCounted : public RefCountedThreadSafe<TestRefCounted> {
  18. private:
  19. friend class RefCountedThreadSafe<TestRefCounted>;
  20. ~TestRefCounted() = default;
  21. };
  22. void Increment(int* count) { (*count)++; }
  23. void IncrementBy(int* count, int n) { (*count) += n; }
  24. void RefCountedParam(const scoped_refptr<TestRefCounted>& ref_counted) {}
  25. void OnMoveOnlyReceived(int* value, std::unique_ptr<int> result) {
  26. *value = *result;
  27. }
  28. // Cancel().
  29. // - Callback can be run multiple times.
  30. // - After Cancel(), Run() completes but has no effect.
  31. TEST(CancelableCallbackTest, Cancel) {
  32. int count = 0;
  33. CancelableRepeatingClosure cancelable(
  34. base::BindRepeating(&Increment, base::Unretained(&count)));
  35. base::RepeatingClosure callback = cancelable.callback();
  36. callback.Run();
  37. EXPECT_EQ(1, count);
  38. callback.Run();
  39. EXPECT_EQ(2, count);
  40. cancelable.Cancel();
  41. callback.Run();
  42. EXPECT_EQ(2, count);
  43. }
  44. // Cancel() called multiple times.
  45. // - Cancel() cancels all copies of the wrapped callback.
  46. // - Calling Cancel() more than once has no effect.
  47. // - After Cancel(), callback() returns a null callback.
  48. TEST(CancelableCallbackTest, MultipleCancel) {
  49. int count = 0;
  50. CancelableRepeatingClosure cancelable(
  51. base::BindRepeating(&Increment, base::Unretained(&count)));
  52. base::RepeatingClosure callback1 = cancelable.callback();
  53. base::RepeatingClosure callback2 = cancelable.callback();
  54. cancelable.Cancel();
  55. callback1.Run();
  56. EXPECT_EQ(0, count);
  57. callback2.Run();
  58. EXPECT_EQ(0, count);
  59. // Calling Cancel() again has no effect.
  60. cancelable.Cancel();
  61. // callback() of a cancelled callback is null.
  62. base::RepeatingClosure callback3 = cancelable.callback();
  63. EXPECT_TRUE(callback3.is_null());
  64. }
  65. // CancelableRepeatingCallback destroyed before callback is run.
  66. // - Destruction of CancelableRepeatingCallback cancels outstanding callbacks.
  67. TEST(CancelableCallbackTest, CallbackCanceledOnDestruction) {
  68. int count = 0;
  69. base::RepeatingClosure callback;
  70. {
  71. CancelableRepeatingClosure cancelable(
  72. base::BindRepeating(&Increment, base::Unretained(&count)));
  73. callback = cancelable.callback();
  74. callback.Run();
  75. EXPECT_EQ(1, count);
  76. }
  77. callback.Run();
  78. EXPECT_EQ(1, count);
  79. }
  80. // Cancel() called on bound closure with a RefCounted parameter.
  81. // - Cancel drops wrapped callback (and, implicitly, its bound arguments).
  82. TEST(CancelableCallbackTest, CancelDropsCallback) {
  83. scoped_refptr<TestRefCounted> ref_counted = new TestRefCounted;
  84. EXPECT_TRUE(ref_counted->HasOneRef());
  85. CancelableOnceClosure cancelable(
  86. base::BindOnce(RefCountedParam, ref_counted));
  87. EXPECT_FALSE(cancelable.IsCancelled());
  88. EXPECT_TRUE(ref_counted.get());
  89. EXPECT_FALSE(ref_counted->HasOneRef());
  90. // There is only one reference to |ref_counted| after the Cancel().
  91. cancelable.Cancel();
  92. EXPECT_TRUE(cancelable.IsCancelled());
  93. EXPECT_TRUE(ref_counted.get());
  94. EXPECT_TRUE(ref_counted->HasOneRef());
  95. }
  96. // Reset().
  97. // - Reset() replaces the existing wrapped callback with a new callback.
  98. // - Reset() deactivates outstanding callbacks.
  99. TEST(CancelableCallbackTest, Reset) {
  100. int count = 0;
  101. CancelableRepeatingClosure cancelable(
  102. base::BindRepeating(&Increment, base::Unretained(&count)));
  103. base::RepeatingClosure callback = cancelable.callback();
  104. callback.Run();
  105. EXPECT_EQ(1, count);
  106. callback.Run();
  107. EXPECT_EQ(2, count);
  108. cancelable.Reset(
  109. base::BindRepeating(&IncrementBy, base::Unretained(&count), 3));
  110. EXPECT_FALSE(cancelable.IsCancelled());
  111. // The stale copy of the cancelable callback is non-null.
  112. ASSERT_FALSE(callback.is_null());
  113. // The stale copy of the cancelable callback is no longer active.
  114. callback.Run();
  115. EXPECT_EQ(2, count);
  116. base::RepeatingClosure callback2 = cancelable.callback();
  117. ASSERT_FALSE(callback2.is_null());
  118. callback2.Run();
  119. EXPECT_EQ(5, count);
  120. }
  121. // IsCanceled().
  122. // - Cancel() transforms the CancelableOnceCallback into a cancelled state.
  123. TEST(CancelableCallbackTest, IsNull) {
  124. CancelableOnceClosure cancelable;
  125. EXPECT_TRUE(cancelable.IsCancelled());
  126. int count = 0;
  127. cancelable.Reset(base::BindOnce(&Increment, base::Unretained(&count)));
  128. EXPECT_FALSE(cancelable.IsCancelled());
  129. cancelable.Cancel();
  130. EXPECT_TRUE(cancelable.IsCancelled());
  131. }
  132. // CancelableRepeatingCallback posted to a task environment with PostTask.
  133. // - Posted callbacks can be cancelled.
  134. TEST(CancelableCallbackTest, PostTask) {
  135. test::TaskEnvironment task_environment;
  136. int count = 0;
  137. CancelableRepeatingClosure cancelable(
  138. base::BindRepeating(&Increment, base::Unretained(&count)));
  139. ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback());
  140. RunLoop().RunUntilIdle();
  141. EXPECT_EQ(1, count);
  142. ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback());
  143. // Cancel before running the tasks.
  144. cancelable.Cancel();
  145. RunLoop().RunUntilIdle();
  146. // Callback never ran due to cancellation; count is the same.
  147. EXPECT_EQ(1, count);
  148. }
  149. // CancelableRepeatingCallback can be used with move-only types.
  150. TEST(CancelableCallbackTest, MoveOnlyType) {
  151. const int kExpectedResult = 42;
  152. int result = 0;
  153. CancelableRepeatingCallback<void(std::unique_ptr<int>)> cb(
  154. base::BindRepeating(&OnMoveOnlyReceived, base::Unretained(&result)));
  155. cb.callback().Run(std::make_unique<int>(kExpectedResult));
  156. EXPECT_EQ(kExpectedResult, result);
  157. }
  158. } // namespace
  159. } // namespace base