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