1*6777b538SAndroid Build Coastguard Worker // Copyright 2011 The Chromium Authors 2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard Worker #include "base/test/thread_test_helper.h" 6*6777b538SAndroid Build Coastguard Worker 7*6777b538SAndroid Build Coastguard Worker #include <utility> 8*6777b538SAndroid Build Coastguard Worker 9*6777b538SAndroid Build Coastguard Worker #include "base/functional/bind.h" 10*6777b538SAndroid Build Coastguard Worker #include "base/location.h" 11*6777b538SAndroid Build Coastguard Worker #include "base/threading/thread_restrictions.h" 12*6777b538SAndroid Build Coastguard Worker 13*6777b538SAndroid Build Coastguard Worker namespace base { 14*6777b538SAndroid Build Coastguard Worker ThreadTestHelper(scoped_refptr<SequencedTaskRunner> target_sequence)15*6777b538SAndroid Build Coastguard WorkerThreadTestHelper::ThreadTestHelper( 16*6777b538SAndroid Build Coastguard Worker scoped_refptr<SequencedTaskRunner> target_sequence) 17*6777b538SAndroid Build Coastguard Worker : test_result_(false), 18*6777b538SAndroid Build Coastguard Worker target_sequence_(std::move(target_sequence)), 19*6777b538SAndroid Build Coastguard Worker done_event_(WaitableEvent::ResetPolicy::AUTOMATIC, 20*6777b538SAndroid Build Coastguard Worker WaitableEvent::InitialState::NOT_SIGNALED) {} 21*6777b538SAndroid Build Coastguard Worker Run()22*6777b538SAndroid Build Coastguard Workerbool ThreadTestHelper::Run() { 23*6777b538SAndroid Build Coastguard Worker if (!target_sequence_->PostTask( 24*6777b538SAndroid Build Coastguard Worker FROM_HERE, base::BindOnce(&ThreadTestHelper::RunOnSequence, this))) { 25*6777b538SAndroid Build Coastguard Worker return false; 26*6777b538SAndroid Build Coastguard Worker } 27*6777b538SAndroid Build Coastguard Worker base::ScopedAllowBaseSyncPrimitivesForTesting allow_wait; 28*6777b538SAndroid Build Coastguard Worker done_event_.Wait(); 29*6777b538SAndroid Build Coastguard Worker return test_result_; 30*6777b538SAndroid Build Coastguard Worker } 31*6777b538SAndroid Build Coastguard Worker RunTest()32*6777b538SAndroid Build Coastguard Workervoid ThreadTestHelper::RunTest() { set_test_result(true); } 33*6777b538SAndroid Build Coastguard Worker 34*6777b538SAndroid Build Coastguard Worker ThreadTestHelper::~ThreadTestHelper() = default; 35*6777b538SAndroid Build Coastguard Worker RunOnSequence()36*6777b538SAndroid Build Coastguard Workervoid ThreadTestHelper::RunOnSequence() { 37*6777b538SAndroid Build Coastguard Worker RunTest(); 38*6777b538SAndroid Build Coastguard Worker done_event_.Signal(); 39*6777b538SAndroid Build Coastguard Worker } 40*6777b538SAndroid Build Coastguard Worker 41*6777b538SAndroid Build Coastguard Worker } // namespace base 42