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 thread becomes idle. 14 // 15 // Note: "something" (e.g. a task) must wake the current thread once the 16 // condition is true. As such testing global conditions which won't wake the 17 // current thread is flaky. 18 // 19 // Returns true if `condition` became true, or false if a timeout happens. 20 // 21 // Example usage: 22 // 23 // ChangeColorAsyncTo(object_under_tests, Color::Red); 24 // 25 // // Waits until the color is red, or aborts the tests otherwise. 26 // ASSERT_TRUE(RunUntil([&](){ 27 // return object_under_test.Color() == Color::Red; 28 // })) << "Timeout waiting for the color to turn red"; 29 // 30 // // When we come here `Color()` is guaranteed to be `Color::Red`. 31 // 32 // TODO (crbug.com/376085325): Implement timeout handling for MOCK_TIME to 33 // manage unmet conditions. 34 [[nodiscard]] bool RunUntil(base::FunctionRef<bool(void)> condition); 35 36 } // namespace base::test 37 38 #endif // BASE_TEST_RUN_UNTIL_H_ 39