blob: 829a6197eab88edce2bb53d9bf804481537ce1db [file] [log] [blame]
[email protected]8aaae7b2011-06-28 06:43:201// 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_
[email protected]8aaae7b2011-06-28 06:43:207
8#include "base/compiler_specific.h"
avid351e5a2015-12-24 03:28:029#include "base/macros.h"
[email protected]8aaae7b2011-06-28 06:43:2010#include "base/memory/ref_counted.h"
skyostil054861d2015-04-30 19:06:1511#include "base/single_thread_task_runner.h"
[email protected]8aaae7b2011-06-28 06:43:2012#include "base/synchronization/waitable_event.h"
13
14namespace base {
15
16// Helper class that executes code on a given thread while blocking on the
17// invoking thread. To use, derive from this class and overwrite RunTest. An
18// alternative use of this class is to use it directly. It will then block
19// until all pending tasks on a given thread have been executed.
20class ThreadTestHelper : public RefCountedThreadSafe<ThreadTestHelper> {
21 public:
dcheng077d1b22014-08-28 18:47:3822 explicit ThreadTestHelper(
skyostil054861d2015-04-30 19:06:1523 scoped_refptr<SingleThreadTaskRunner> target_thread);
[email protected]8aaae7b2011-06-28 06:43:2024
25 // True if RunTest() was successfully executed on the target thread.
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().
36 void set_test_result(bool test_result) { test_result_ = test_result; }
37
38 private:
39 void RunInThread();
40
41 bool test_result_;
skyostil054861d2015-04-30 19:06:1542 scoped_refptr<SingleThreadTaskRunner> target_thread_;
[email protected]8aaae7b2011-06-28 06:43:2043 WaitableEvent done_event_;
44
45 DISALLOW_COPY_AND_ASSIGN(ThreadTestHelper);
46};
47
48} // namespace base
49
50#endif // BASE_TEST_THREAD_TEST_HELPER_H_