[email protected] | 8aaae7b | 2011-06-28 06:43:20 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 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_THREAD_TEST_HELPER_H_ |
| 6 | #define BASE_TEST_THREAD_TEST_HELPER_H_ |
[email protected] | 8aaae7b | 2011-06-28 06:43:20 | [diff] [blame] | 7 | |
| 8 | #include "base/compiler_specific.h" |
avi | d351e5a | 2015-12-24 03:28:02 | [diff] [blame] | 9 | #include "base/macros.h" |
[email protected] | 8aaae7b | 2011-06-28 06:43:20 | [diff] [blame] | 10 | #include "base/memory/ref_counted.h" |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 11 | #include "base/single_thread_task_runner.h" |
[email protected] | 8aaae7b | 2011-06-28 06:43:20 | [diff] [blame] | 12 | #include "base/synchronization/waitable_event.h" |
| 13 | |
| 14 | namespace base { |
| 15 | |
| 16 | // Helper class that executes code on a given thread while blocking on the |
| 17 | // invoking thread. To use, derive from this class and overwrite RunTest. An |
| 18 | // alternative use of this class is to use it directly. It will then block |
| 19 | // until all pending tasks on a given thread have been executed. |
| 20 | class ThreadTestHelper : public RefCountedThreadSafe<ThreadTestHelper> { |
| 21 | public: |
dcheng | 077d1b2 | 2014-08-28 18:47:38 | [diff] [blame] | 22 | explicit ThreadTestHelper( |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 23 | scoped_refptr<SingleThreadTaskRunner> target_thread); |
[email protected] | 8aaae7b | 2011-06-28 06:43:20 | [diff] [blame] | 24 | |
| 25 | // True if RunTest() was successfully executed on the target thread. |
| 26 | bool Run() WARN_UNUSED_RESULT; |
| 27 | |
| 28 | virtual void RunTest(); |
| 29 | |
| 30 | protected: |
| 31 | friend class RefCountedThreadSafe<ThreadTestHelper>; |
| 32 | |
| 33 | virtual ~ThreadTestHelper(); |
| 34 | |
| 35 | // Use this method to store the result of RunTest(). |
| 36 | void set_test_result(bool test_result) { test_result_ = test_result; } |
| 37 | |
| 38 | private: |
| 39 | void RunInThread(); |
| 40 | |
| 41 | bool test_result_; |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 42 | scoped_refptr<SingleThreadTaskRunner> target_thread_; |
[email protected] | 8aaae7b | 2011-06-28 06:43:20 | [diff] [blame] | 43 | WaitableEvent done_event_; |
| 44 | |
| 45 | DISALLOW_COPY_AND_ASSIGN(ThreadTestHelper); |
| 46 | }; |
| 47 | |
| 48 | } // namespace base |
| 49 | |
| 50 | #endif // BASE_TEST_THREAD_TEST_HELPER_H_ |