1 // Copyright 2023 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_TEST_RUN_UNTIL_H_ 6 #define BASE_TEST_RUN_UNTIL_H_ 7 8 #include "base/functional/function_ref.h" 9 10 namespace base::test { 11 12 // Waits until `condition` evaluates to `true`, by evaluating `condition` 13 // whenever the current task becomes idle (while allowing the message loop of 14 // the calling thread to spin). 15 // 16 // Returns true if `condition` became true, or false if a timeout happens. 17 // 18 // Example usage: 19 // 20 // ChangeColorAsyncTo(object_under_tests, Color::Red); 21 // 22 // // Waits until the color is red, or aborts the tests otherwise. 23 // ASSERT_TRUE(RunUntil([&](){ 24 // return object_under_test.Color() == Color::Red; 25 // })) << "Timeout waiting for the color to turn red"; 26 // 27 // // When we come here `Color()` is guaranteed to be `Color::Red`. 28 // 29 [[nodiscard]] bool RunUntil(base::FunctionRef<bool(void)> condition); 30 31 } // namespace base::test 32 33 #endif // BASE_TEST_RUN_UNTIL_H_ 34