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_ 7 8 #include "base/compiler_specific.h" 9 #include "base/macros.h" 10 #include "base/memory/ref_counted.h" 11 #include "base/sequenced_task_runner.h" 12 #include "base/synchronization/waitable_event.h" 13 14 namespace base { 15 16 // Helper class that executes code on a given target sequence/thread while 17 // blocking on the invoking sequence/thread. To use, derive from this class and 18 // overwrite RunTest. An alternative use of this class is to use it directly. It 19 // will then block until all pending tasks on a given sequence/thread have been 20 // executed. 21 class ThreadTestHelper : public RefCountedThreadSafe<ThreadTestHelper> { 22 public: 23 explicit ThreadTestHelper(scoped_refptr<SequencedTaskRunner> target_sequence); 24 25 // True if RunTest() was successfully executed on the target sequence. 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(). set_test_result(bool test_result)36 void set_test_result(bool test_result) { test_result_ = test_result; } 37 38 private: 39 void RunOnSequence(); 40 41 bool test_result_; 42 scoped_refptr<SequencedTaskRunner> target_sequence_; 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_ 51