| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_TEST_THREAD_TEST_HELPER_H_ | 5 #ifndef BASE_TEST_THREAD_TEST_HELPER_H_ |
| 6 #define BASE_TEST_THREAD_TEST_HELPER_H_ | 6 #define BASE_TEST_THREAD_TEST_HELPER_H_ |
| 7 | 7 |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
| 12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 13 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 | 15 |
| 16 // Helper class that executes code on a given thread while blocking on the | 16 // Helper class that executes code on a given target sequence/thread while |
| 17 // invoking thread. To use, derive from this class and overwrite RunTest. An | 17 // blocking on the invoking sequence/thread. To use, derive from this class and |
| 18 // alternative use of this class is to use it directly. It will then block | 18 // overwrite RunTest. An alternative use of this class is to use it directly. It |
| 19 // until all pending tasks on a given thread have been executed. | 19 // will then block until all pending tasks on a given sequence/thread have been |
| 20 // executed. |
| 20 class ThreadTestHelper : public RefCountedThreadSafe<ThreadTestHelper> { | 21 class ThreadTestHelper : public RefCountedThreadSafe<ThreadTestHelper> { |
| 21 public: | 22 public: |
| 22 explicit ThreadTestHelper( | 23 explicit ThreadTestHelper(scoped_refptr<SequencedTaskRunner> target_sequence); |
| 23 scoped_refptr<SingleThreadTaskRunner> target_thread); | |
| 24 | 24 |
| 25 // True if RunTest() was successfully executed on the target thread. | 25 // True if RunTest() was successfully executed on the target sequence. |
| 26 bool Run() WARN_UNUSED_RESULT; | 26 bool Run() WARN_UNUSED_RESULT; |
| 27 | 27 |
| 28 virtual void RunTest(); | 28 virtual void RunTest(); |
| 29 | 29 |
| 30 protected: | 30 protected: |
| 31 friend class RefCountedThreadSafe<ThreadTestHelper>; | 31 friend class RefCountedThreadSafe<ThreadTestHelper>; |
| 32 | 32 |
| 33 virtual ~ThreadTestHelper(); | 33 virtual ~ThreadTestHelper(); |
| 34 | 34 |
| 35 // Use this method to store the result of RunTest(). | 35 // Use this method to store the result of RunTest(). |
| 36 void set_test_result(bool test_result) { test_result_ = test_result; } | 36 void set_test_result(bool test_result) { test_result_ = test_result; } |
| 37 | 37 |
| 38 private: | 38 private: |
| 39 void RunInThread(); | 39 void RunOnSequence(); |
| 40 | 40 |
| 41 bool test_result_; | 41 bool test_result_; |
| 42 scoped_refptr<SingleThreadTaskRunner> target_thread_; | 42 scoped_refptr<SequencedTaskRunner> target_sequence_; |
| 43 WaitableEvent done_event_; | 43 WaitableEvent done_event_; |
| 44 | 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(ThreadTestHelper); | 45 DISALLOW_COPY_AND_ASSIGN(ThreadTestHelper); |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 } // namespace base | 48 } // namespace base |
| 49 | 49 |
| 50 #endif // BASE_TEST_THREAD_TEST_HELPER_H_ | 50 #endif // BASE_TEST_THREAD_TEST_HELPER_H_ |
| OLD | NEW |