blob: 926da730e3133f16b7bacfd8a371741643ea007d [file] [log] [blame]
jochen@chromium.orgf5593332011-06-28 15:43:20 +09001// 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_
jochen@chromium.orgf5593332011-06-28 15:43:20 +09007
8#include "base/compiler_specific.h"
9#include "base/memory/ref_counted.h"
skyostil97aefe12015-05-01 04:06:15 +090010#include "base/single_thread_task_runner.h"
jochen@chromium.orgf5593332011-06-28 15:43:20 +090011#include "base/synchronization/waitable_event.h"
12
13namespace base {
14
15// Helper class that executes code on a given thread while blocking on the
16// invoking thread. To use, derive from this class and overwrite RunTest. An
17// alternative use of this class is to use it directly. It will then block
18// until all pending tasks on a given thread have been executed.
19class ThreadTestHelper : public RefCountedThreadSafe<ThreadTestHelper> {
20 public:
dcheng8ce99a82014-08-29 03:47:38 +090021 explicit ThreadTestHelper(
skyostil97aefe12015-05-01 04:06:15 +090022 scoped_refptr<SingleThreadTaskRunner> target_thread);
jochen@chromium.orgf5593332011-06-28 15:43:20 +090023
24 // True if RunTest() was successfully executed on the target thread.
25 bool Run() WARN_UNUSED_RESULT;
26
27 virtual void RunTest();
28
29 protected:
30 friend class RefCountedThreadSafe<ThreadTestHelper>;
31
32 virtual ~ThreadTestHelper();
33
34 // Use this method to store the result of RunTest().
35 void set_test_result(bool test_result) { test_result_ = test_result; }
36
37 private:
38 void RunInThread();
39
40 bool test_result_;
skyostil97aefe12015-05-01 04:06:15 +090041 scoped_refptr<SingleThreadTaskRunner> target_thread_;
jochen@chromium.orgf5593332011-06-28 15:43:20 +090042 WaitableEvent done_event_;
43
44 DISALLOW_COPY_AND_ASSIGN(ThreadTestHelper);
45};
46
47} // namespace base
48
49#endif // BASE_TEST_THREAD_TEST_HELPER_H_