1 // Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights 2 // reserved. Use of this source code is governed by a BSD-style license that 3 // can be found in the LICENSE file. 4 5 #ifndef CEF_TESTS_CEFTESTS_THREAD_HELPER_H_ 6 #define CEF_TESTS_CEFTESTS_THREAD_HELPER_H_ 7 #pragma once 8 9 #include "include/base/cef_callback.h" 10 #include "include/cef_task.h" 11 #include "include/cef_waitable_event.h" 12 13 // Helper for signaling |event|. 14 void SignalEvent(CefRefPtr<CefWaitableEvent> event); 15 16 // Post a task to the specified thread and wait for the task to execute as 17 // indication that all previously pending tasks on that thread have completed. 18 void WaitForThread(CefThreadId thread_id, int64 delay_ms = 0); 19 void WaitForThread(CefRefPtr<CefTaskRunner> task_runner, int64 delay_ms = 0); 20 21 #define WaitForIOThread() WaitForThread(TID_IO) 22 #define WaitForUIThread() WaitForThread(TID_UI) 23 #define WaitForFILEThread() WaitForThread(TID_FILE_USER_VISIBLE) 24 #define WaitForIOThreadWithDelay(delay_ms) WaitForThread(TID_IO, delay_ms) 25 #define WaitForUIThreadWithDelay(delay_ms) WaitForThread(TID_UI, delay_ms) 26 #define WaitForFILEThreadWithDelay(delay_ms) \ 27 WaitForThread(TID_FILE_USER_VISIBLE, delay_ms) 28 29 // Assert that execution is occuring on the named thread. 30 #define EXPECT_UI_THREAD() EXPECT_TRUE(CefCurrentlyOn(TID_UI)); 31 #define EXPECT_IO_THREAD() EXPECT_TRUE(CefCurrentlyOn(TID_IO)); 32 #define EXPECT_FILE_THREAD() EXPECT_TRUE(CefCurrentlyOn(TID_FILE_USER_VISIBLE)); 33 #define EXPECT_RENDERER_THREAD() EXPECT_TRUE(CefCurrentlyOn(TID_RENDERER)); 34 35 // Executes |test_impl| on the specified |thread_id|. |event| will be signaled 36 // once execution is complete. 37 void RunOnThread(CefThreadId thread_id, 38 base::OnceClosure test_impl, 39 CefRefPtr<CefWaitableEvent> event); 40 41 #define NAMED_THREAD_TEST(thread_id, test_case_name, test_name) \ 42 TEST(test_case_name, test_name) { \ 43 CefRefPtr<CefWaitableEvent> event = \ 44 CefWaitableEvent::CreateWaitableEvent(true, false); \ 45 RunOnThread(thread_id, base::BindOnce(test_name##Impl), event); \ 46 event->Wait(); \ 47 } 48 49 // Execute "test_case_name.test_name" test on the named thread. The test 50 // implementation is "void test_nameImpl()". 51 #define UI_THREAD_TEST(test_case_name, test_name) \ 52 NAMED_THREAD_TEST(TID_UI, test_case_name, test_name) 53 54 // Like RunOnThread() but |test_impl| is responsible for signaling |event|. 55 void RunOnThreadAsync( 56 CefThreadId thread_id, 57 base::OnceCallback<void(CefRefPtr<CefWaitableEvent>)> test_impl, 58 CefRefPtr<CefWaitableEvent>); 59 60 #define NAMED_THREAD_TEST_ASYNC(thread_id, test_case_name, test_name) \ 61 TEST(test_case_name, test_name) { \ 62 CefRefPtr<CefWaitableEvent> event = \ 63 CefWaitableEvent::CreateWaitableEvent(true, false); \ 64 RunOnThreadAsync(thread_id, base::BindOnce(test_name##Impl), event); \ 65 event->Wait(); \ 66 } 67 68 // Execute "test_case_name.test_name" test on the named thread. The test 69 // implementation is "void test_nameImpl(CefRefPtr<CefWaitableEvent> event)". 70 #define UI_THREAD_TEST_ASYNC(test_case_name, test_name) \ 71 NAMED_THREAD_TEST_ASYNC(TID_UI, test_case_name, test_name) 72 73 #endif // CEF_TESTS_CEFTESTS_THREAD_HELPER_H_ 74