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 #include "base/test/thread_test_helper.h" 6 7 #include <utility> 8 9 #include "base/bind.h" 10 #include "base/location.h" 11 #include "base/threading/thread_restrictions.h" 12 13 namespace base { 14 ThreadTestHelper(scoped_refptr<SequencedTaskRunner> target_sequence)15ThreadTestHelper::ThreadTestHelper( 16 scoped_refptr<SequencedTaskRunner> target_sequence) 17 : test_result_(false), 18 target_sequence_(std::move(target_sequence)), 19 done_event_(WaitableEvent::ResetPolicy::AUTOMATIC, 20 WaitableEvent::InitialState::NOT_SIGNALED) {} 21 Run()22bool ThreadTestHelper::Run() { 23 if (!target_sequence_->PostTask( 24 FROM_HERE, base::BindOnce(&ThreadTestHelper::RunOnSequence, this))) { 25 return false; 26 } 27 base::ThreadRestrictions::ScopedAllowWait allow_wait; 28 done_event_.Wait(); 29 return test_result_; 30 } 31 RunTest()32void ThreadTestHelper::RunTest() { set_test_result(true); } 33 34 ThreadTestHelper::~ThreadTestHelper() = default; 35 RunOnSequence()36void ThreadTestHelper::RunOnSequence() { 37 RunTest(); 38 done_event_.Signal(); 39 } 40 41 } // namespace base 42