Flutter Engine
Loading...
Searching...
No Matches
thread_test.h
Go to the documentation of this file.
1
// Copyright 2013 The Flutter 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 FLUTTER_TESTING_THREAD_TEST_H_
6
#define FLUTTER_TESTING_THREAD_TEST_H_
7
8
#include <memory>
9
#include <string>
10
11
#include "
flutter/fml/macros.h
"
12
#include "
flutter/fml/message_loop.h
"
13
#include "
flutter/fml/task_runner.h
"
14
#include "
flutter/fml/thread.h
"
15
#include "gtest/gtest.h"
16
17
namespace
flutter::testing
{
18
19
//------------------------------------------------------------------------------
20
/// @brief A fixture that creates threads with running message loops that
21
/// are terminated when the test is done (the threads are joined
22
/// then as well). While this fixture may be used on it's own, it is
23
/// often sub-classed by other fixtures whose functioning requires
24
/// threads to be created as necessary.
25
///
26
class
ThreadTest
:
public
::testing::Test {
27
public
:
28
ThreadTest
();
29
30
//----------------------------------------------------------------------------
31
/// @brief Get the task runner for the thread that the current unit-test
32
/// is running on. This creates a message loop as necessary.
33
///
34
/// @attention Unlike all other threads and task runners, this task runner is
35
/// shared by all tests running in the process. Tests must ensure
36
/// that all tasks posted to this task runner are executed before
37
/// the test ends to prevent the task from one test being executed
38
/// while another test is running. When in doubt, just create a
39
/// bespoke thread and task running. These cannot be seen by other
40
/// tests in the process.
41
///
42
/// @see `GetThreadTaskRunner`, `CreateNewThread`.
43
///
44
/// @return The task runner for the thread the test is running on.
45
///
46
fml::RefPtr<fml::TaskRunner>
GetCurrentTaskRunner
();
47
48
//----------------------------------------------------------------------------
49
/// @brief Creates a new thread, initializes a message loop on it, and,
50
/// returns its task runner to the unit-test. The message loop is
51
/// terminated (and its thread joined) when the test ends. This
52
/// allows tests to create multiple named threads as necessary.
53
///
54
/// @param[in] name The name of the OS thread created.
55
///
56
/// @return The task runner for the newly created thread.
57
///
58
fml::RefPtr<fml::TaskRunner>
CreateNewThread
(
const
std::string&
name
=
""
);
59
60
private
:
61
fml::RefPtr<fml::TaskRunner>
current_task_runner_;
62
std::vector<std::unique_ptr<fml::Thread>> extra_threads_;
63
64
FML_DISALLOW_COPY_AND_ASSIGN
(
ThreadTest
);
65
};
66
67
}
// namespace flutter::testing
68
69
#endif
// FLUTTER_TESTING_THREAD_TEST_H_
flutter::testing::ThreadTest
A fixture that creates threads with running message loops that are terminated when the test is done (...
Definition
thread_test.h:26
flutter::testing::ThreadTest::GetCurrentTaskRunner
fml::RefPtr< fml::TaskRunner > GetCurrentTaskRunner()
Get the task runner for the thread that the current unit-test is running on. This creates a message l...
Definition
thread_test.cc:22
flutter::testing::ThreadTest::ThreadTest
ThreadTest()
Definition
thread_test.cc:20
flutter::testing::ThreadTest::CreateNewThread
fml::RefPtr< fml::TaskRunner > CreateNewThread(const std::string &name="")
Creates a new thread, initializes a message loop on it, and, returns its task runner to the unit-test...
Definition
thread_test.cc:26
fml::RefPtr
Definition
ref_ptr.h:65
macros.h
FML_DISALLOW_COPY_AND_ASSIGN
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition
macros.h:27
task_runner.h
thread.h
message_loop.h
flutter::testing
Definition
native_assets_unittests.cc:10
flutter::name
DEF_SWITCHES_START aot vmservice shared library name
Definition
switch_defs.h:27
testing
thread_test.h
Generated on Thu Nov 6 2025 16:11:29 for Flutter Engine by
1.9.8