// Copyright 2021 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef COMPONENTS_REPORTING_UTIL_TEST_SUPPORT_CALLBACKS_H_ #define COMPONENTS_REPORTING_UTIL_TEST_SUPPORT_CALLBACKS_H_ #include <tuple> #include <utility> #include "base/atomic_ref_count.h" #include "base/functional/bind.h" #include "base/functional/callback_forward.h" #include "base/functional/callback_helpers.h" #include "base/logging.h" #include "base/memory/weak_ptr.h" #include "base/run_loop.h" #include "base/synchronization/lock.h" #include "base/task/bind_post_task.h" #include "base/task/sequenced_task_runner.h" #include "base/thread_annotations.h" #include "testing/gtest/include/gtest/gtest.h" namespace reporting::test { // Usage (in tests only): // // TestMultiEvent<ResType1, Restype2, ...> e; // // ... Do some async work passing e.cb() as a completion callback of // base::OnceCallback<void(ResType1, Restype2, ...)> type which also may // perform some other action specified by |done| callback provided by the // caller. Now wait for e.cb() to be called and return the collected results. // // std::tie(res1, res2, ...) = e.result(); // template <typename... ResType> class TestMultiEvent { … }; // Usage (in tests only): // // TestEvent<ResType> e; // // ... Do some async work passing e.cb() as a completion callback of // base::OnceCallback<void(ResType res)> type which also may perform some // other action specified by |done| callback provided by the caller. // Now wait for e.cb() to be called and return the collected result. // // ... = e.result(); // template <typename ResType> class TestEvent : public TestMultiEvent<ResType> { … }; // Usage (in tests only): // // TestCallbackWaiter waiter; // ... do something // waiter.Wait(); // // or, with multithreadeded activity: // // TestCallbackWaiter waiter; // waiter.Attach(N); // N - is a number of asynchronous actions // ... // waiter.Wait(); // // And in each of N actions: waiter.Signal(); when done class TestCallbackWaiter : public TestEvent<bool> { … }; // RAII wrapper for TestCallbackWaiter. // // Usage: // { // TestCallbackAutoWaiter waiter; // Implicitly Attach(1); // ... // Launch async activity, which will eventually do waiter.Signal(); // ... // } // Here the waiter will automatically wait. class TestCallbackAutoWaiter : public TestCallbackWaiter { … }; } // namespace reporting::test #endif // COMPONENTS_REPORTING_UTIL_TEST_SUPPORT_CALLBACKS_H_