David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 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 | |
| 5 | #include "base/cancelable_callback.h" |
| 6 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/bind_helpers.h" |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 11 | #include "base/location.h" |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 13 | #include "base/message_loop/message_loop.h" |
| 14 | #include "base/run_loop.h" |
| 15 | #include "base/single_thread_task_runner.h" |
| 16 | #include "base/threading/thread_task_runner_handle.h" |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
| 19 | namespace base { |
| 20 | namespace { |
| 21 | |
| 22 | class TestRefCounted : public RefCountedThreadSafe<TestRefCounted> { |
| 23 | private: |
| 24 | friend class RefCountedThreadSafe<TestRefCounted>; |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 25 | ~TestRefCounted() = default; |
| 26 | ; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | void Increment(int* count) { (*count)++; } |
| 30 | void IncrementBy(int* count, int n) { (*count) += n; } |
| 31 | void RefCountedParam(const scoped_refptr<TestRefCounted>& ref_counted) {} |
| 32 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 33 | void OnMoveOnlyReceived(int* value, std::unique_ptr<int> result) { |
| 34 | *value = *result; |
| 35 | } |
| 36 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 37 | // Cancel(). |
| 38 | // - Callback can be run multiple times. |
| 39 | // - After Cancel(), Run() completes but has no effect. |
| 40 | TEST(CancelableCallbackTest, Cancel) { |
| 41 | int count = 0; |
| 42 | CancelableClosure cancelable( |
| 43 | base::Bind(&Increment, base::Unretained(&count))); |
| 44 | |
| 45 | base::Closure callback = cancelable.callback(); |
| 46 | callback.Run(); |
| 47 | EXPECT_EQ(1, count); |
| 48 | |
| 49 | callback.Run(); |
| 50 | EXPECT_EQ(2, count); |
| 51 | |
| 52 | cancelable.Cancel(); |
| 53 | callback.Run(); |
| 54 | EXPECT_EQ(2, count); |
| 55 | } |
| 56 | |
| 57 | // Cancel() called multiple times. |
| 58 | // - Cancel() cancels all copies of the wrapped callback. |
| 59 | // - Calling Cancel() more than once has no effect. |
| 60 | // - After Cancel(), callback() returns a null callback. |
| 61 | TEST(CancelableCallbackTest, MultipleCancel) { |
| 62 | int count = 0; |
| 63 | CancelableClosure cancelable( |
| 64 | base::Bind(&Increment, base::Unretained(&count))); |
| 65 | |
| 66 | base::Closure callback1 = cancelable.callback(); |
| 67 | base::Closure callback2 = cancelable.callback(); |
| 68 | cancelable.Cancel(); |
| 69 | |
| 70 | callback1.Run(); |
| 71 | EXPECT_EQ(0, count); |
| 72 | |
| 73 | callback2.Run(); |
| 74 | EXPECT_EQ(0, count); |
| 75 | |
| 76 | // Calling Cancel() again has no effect. |
| 77 | cancelable.Cancel(); |
| 78 | |
| 79 | // callback() of a cancelled callback is null. |
| 80 | base::Closure callback3 = cancelable.callback(); |
| 81 | EXPECT_TRUE(callback3.is_null()); |
| 82 | } |
| 83 | |
| 84 | // CancelableCallback destroyed before callback is run. |
| 85 | // - Destruction of CancelableCallback cancels outstanding callbacks. |
| 86 | TEST(CancelableCallbackTest, CallbackCanceledOnDestruction) { |
| 87 | int count = 0; |
| 88 | base::Closure callback; |
| 89 | |
| 90 | { |
| 91 | CancelableClosure cancelable( |
| 92 | base::Bind(&Increment, base::Unretained(&count))); |
| 93 | |
| 94 | callback = cancelable.callback(); |
| 95 | callback.Run(); |
| 96 | EXPECT_EQ(1, count); |
| 97 | } |
| 98 | |
| 99 | callback.Run(); |
| 100 | EXPECT_EQ(1, count); |
| 101 | } |
| 102 | |
| 103 | // Cancel() called on bound closure with a RefCounted parameter. |
| 104 | // - Cancel drops wrapped callback (and, implicitly, its bound arguments). |
| 105 | TEST(CancelableCallbackTest, CancelDropsCallback) { |
| 106 | scoped_refptr<TestRefCounted> ref_counted = new TestRefCounted; |
| 107 | EXPECT_TRUE(ref_counted->HasOneRef()); |
| 108 | |
| 109 | CancelableClosure cancelable(base::Bind(RefCountedParam, ref_counted)); |
| 110 | EXPECT_FALSE(cancelable.IsCancelled()); |
| 111 | EXPECT_TRUE(ref_counted.get()); |
| 112 | EXPECT_FALSE(ref_counted->HasOneRef()); |
| 113 | |
| 114 | // There is only one reference to |ref_counted| after the Cancel(). |
| 115 | cancelable.Cancel(); |
| 116 | EXPECT_TRUE(cancelable.IsCancelled()); |
| 117 | EXPECT_TRUE(ref_counted.get()); |
| 118 | EXPECT_TRUE(ref_counted->HasOneRef()); |
| 119 | } |
| 120 | |
| 121 | // Reset(). |
| 122 | // - Reset() replaces the existing wrapped callback with a new callback. |
| 123 | // - Reset() deactivates outstanding callbacks. |
| 124 | TEST(CancelableCallbackTest, Reset) { |
| 125 | int count = 0; |
| 126 | CancelableClosure cancelable( |
| 127 | base::Bind(&Increment, base::Unretained(&count))); |
| 128 | |
| 129 | base::Closure callback = cancelable.callback(); |
| 130 | callback.Run(); |
| 131 | EXPECT_EQ(1, count); |
| 132 | |
| 133 | callback.Run(); |
| 134 | EXPECT_EQ(2, count); |
| 135 | |
| 136 | cancelable.Reset( |
| 137 | base::Bind(&IncrementBy, base::Unretained(&count), 3)); |
| 138 | EXPECT_FALSE(cancelable.IsCancelled()); |
| 139 | |
| 140 | // The stale copy of the cancelable callback is non-null. |
| 141 | ASSERT_FALSE(callback.is_null()); |
| 142 | |
| 143 | // The stale copy of the cancelable callback is no longer active. |
| 144 | callback.Run(); |
| 145 | EXPECT_EQ(2, count); |
| 146 | |
| 147 | base::Closure callback2 = cancelable.callback(); |
| 148 | ASSERT_FALSE(callback2.is_null()); |
| 149 | |
| 150 | callback2.Run(); |
| 151 | EXPECT_EQ(5, count); |
| 152 | } |
| 153 | |
| 154 | // IsCanceled(). |
| 155 | // - Cancel() transforms the CancelableCallback into a cancelled state. |
| 156 | TEST(CancelableCallbackTest, IsNull) { |
| 157 | CancelableClosure cancelable; |
| 158 | EXPECT_TRUE(cancelable.IsCancelled()); |
| 159 | |
| 160 | int count = 0; |
| 161 | cancelable.Reset(base::Bind(&Increment, |
| 162 | base::Unretained(&count))); |
| 163 | EXPECT_FALSE(cancelable.IsCancelled()); |
| 164 | |
| 165 | cancelable.Cancel(); |
| 166 | EXPECT_TRUE(cancelable.IsCancelled()); |
| 167 | } |
| 168 | |
| 169 | // CancelableCallback posted to a MessageLoop with PostTask. |
| 170 | // - Callbacks posted to a MessageLoop can be cancelled. |
| 171 | TEST(CancelableCallbackTest, PostTask) { |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 172 | MessageLoop loop; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 173 | |
| 174 | int count = 0; |
| 175 | CancelableClosure cancelable(base::Bind(&Increment, |
| 176 | base::Unretained(&count))); |
| 177 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 178 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback()); |
| 179 | RunLoop().RunUntilIdle(); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 180 | |
| 181 | EXPECT_EQ(1, count); |
| 182 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 183 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, cancelable.callback()); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 184 | |
| 185 | // Cancel before running the message loop. |
| 186 | cancelable.Cancel(); |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 187 | RunLoop().RunUntilIdle(); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 188 | |
| 189 | // Callback never ran due to cancellation; count is the same. |
| 190 | EXPECT_EQ(1, count); |
| 191 | } |
| 192 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 193 | // CancelableCallback can be used with move-only types. |
| 194 | TEST(CancelableCallbackTest, MoveOnlyType) { |
| 195 | const int kExpectedResult = 42; |
| 196 | |
| 197 | int result = 0; |
| 198 | CancelableCallback<void(std::unique_ptr<int>)> cb( |
| 199 | base::Bind(&OnMoveOnlyReceived, base::Unretained(&result))); |
Kaido Kert | f9bc013 | 2023-12-04 11:52:41 -0800 | [diff] [blame] | 200 | cb.callback().Run(std::make_unique<int>(kExpectedResult)); |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 201 | |
| 202 | EXPECT_EQ(kExpectedResult, result); |
| 203 | } |
| 204 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 205 | } // namespace |
| 206 | } // namespace base |