// Copyright 2011 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "base/cancelable_callback.h" #include <memory> #include <optional> #include "base/functional/bind.h" #include "base/functional/callback_helpers.h" #include "base/location.h" #include "base/memory/ref_counted.h" #include "base/run_loop.h" #include "base/task/single_thread_task_runner.h" #include "base/test/task_environment.h" #include "testing/gtest/include/gtest/gtest.h" namespace base { namespace { class TestRefCounted : public RefCountedThreadSafe<TestRefCounted> { … }; void Increment(int* count) { … } void IncrementBy(int* count, int n) { … } void RefCountedParam(const scoped_refptr<TestRefCounted>& ref_counted) { … } void OnMoveOnlyReceived(int* value, std::unique_ptr<int> result) { … } // Cancel(). // - Callback can be run multiple times. // - After Cancel(), Run() completes but has no effect. TEST(CancelableCallbackTest, Cancel) { … } // Cancel() called multiple times. // - Cancel() cancels all copies of the wrapped callback. // - Calling Cancel() more than once has no effect. // - After Cancel(), callback() returns a null callback. TEST(CancelableCallbackTest, MultipleCancel) { … } // CancelableRepeatingCallback destroyed before callback is run. // - Destruction of CancelableRepeatingCallback cancels outstanding callbacks. TEST(CancelableCallbackTest, CallbackCanceledOnDestruction) { … } // Cancel() called on bound closure with a RefCounted parameter. // - Cancel drops wrapped callback (and, implicitly, its bound arguments). TEST(CancelableCallbackTest, CancelDropsCallback) { … } // Reset(). // - Reset() replaces the existing wrapped callback with a new callback. // - Reset() deactivates outstanding callbacks. TEST(CancelableCallbackTest, Reset) { … } // IsCanceled(). // - Cancel() transforms the CancelableOnceCallback into a cancelled state. TEST(CancelableCallbackTest, IsNull) { … } // CancelableRepeatingCallback posted to a task environment with PostTask. // - Posted callbacks can be cancelled. // - Chained callbacks from `.Then()` still run on cancelled callbacks. TEST(CancelableCallbackTest, PostTask) { … } // CancelableRepeatingCallback posted to a task environment with // PostTaskAndReply. // - Posted callbacks can be cancelled. TEST(CancelableCallbackTest, PostTaskAndReply) { … } // CancelableRepeatingCallback can be used with move-only types. TEST(CancelableCallbackTest, MoveOnlyType) { … } } // namespace } // namespace base