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