Flutter Engine
The Flutter Engine
dart_weak_persistent_handle_unittest.cc
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#include "flutter/testing/dart_isolate_runner.h"
6#include "flutter/testing/fixture_test.h"
7
8namespace flutter {
9namespace testing {
10
11namespace {
12void NopFinalizer(void* isolate_callback_data, void* peer) {}
13} // namespace
14
16 public:
18 : settings_(CreateSettingsForFixture()),
19 vm_(DartVMRef::Create(settings_)) {}
20
22
23 [[nodiscard]] bool RunWithEntrypoint(const std::string& entrypoint) {
24 if (running_isolate_) {
25 return false;
26 }
27 auto thread = CreateNewThread();
28 TaskRunners single_threaded_task_runner(GetCurrentTestName(), thread,
29 thread, thread, thread);
30 auto isolate =
31 RunDartCodeInIsolate(vm_, settings_, single_threaded_task_runner,
32 entrypoint, {}, GetDefaultKernelFilePath());
33 if (!isolate || isolate->get()->GetPhase() != DartIsolate::Phase::Running) {
34 return false;
35 }
36
37 running_isolate_ = std::move(isolate);
38 return true;
39 }
40
41 [[nodiscard]] bool RunInIsolateScope(std::function<bool(void)> closure) {
42 return running_isolate_->RunInIsolateScope(closure);
43 }
44
45 private:
46 Settings settings_;
47 DartVMRef vm_;
48 std::unique_ptr<AutoIsolateShutdown> running_isolate_;
49 FML_DISALLOW_COPY_AND_ASSIGN(DartWeakPersistentHandle);
50};
51
52TEST_F(DartWeakPersistentHandle, ClearImmediately) {
53 auto weak_persistent_value = tonic::DartWeakPersistentValue();
54
56
57 AddNativeCallback(
58 "GiveObjectToNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
59 auto handle = Dart_GetNativeArgument(args, 0);
60
61 auto dart_state = tonic::DartState::Current();
62 ASSERT_TRUE(dart_state);
63 ASSERT_TRUE(tonic::DartState::Current());
64 weak_persistent_value.Set(dart_state, handle, nullptr, 0, NopFinalizer);
65
66 weak_persistent_value.Clear();
67
68 event.Signal();
69 }));
70
71 ASSERT_TRUE(RunWithEntrypoint("callGiveObjectToNative"));
72
73 event.Wait();
74}
75
77 auto weak_persistent_value = tonic::DartWeakPersistentValue();
78
80
81 AddNativeCallback(
82 "GiveObjectToNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
83 auto handle = Dart_GetNativeArgument(args, 0);
84
85 auto dart_state = tonic::DartState::Current();
86 ASSERT_TRUE(dart_state);
87 ASSERT_TRUE(tonic::DartState::Current());
88 weak_persistent_value.Set(dart_state, handle, nullptr, 0, NopFinalizer);
89
90 // Do not clear handle immediately.
91
92 event.Signal();
93 }));
94
95 ASSERT_TRUE(RunWithEntrypoint("callGiveObjectToNative"));
96
97 event.Wait();
98
99 ASSERT_TRUE(RunInIsolateScope([&weak_persistent_value]() -> bool {
100 // Clear on initiative of native.
101 weak_persistent_value.Clear();
102 return true;
103 }));
104}
105
107 auto weak_persistent_value = tonic::DartWeakPersistentValue();
108
110
111 AddNativeCallback(
112 "GiveObjectToNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
113 auto handle = Dart_GetNativeArgument(args, 0);
114
115 auto dart_state = tonic::DartState::Current();
116 ASSERT_TRUE(dart_state);
117 ASSERT_TRUE(tonic::DartState::Current());
118 weak_persistent_value.Set(dart_state, handle, nullptr, 0, NopFinalizer);
119
120 // Do not clear handle immediately.
121 }));
122
123 AddNativeCallback("SignalDone",
125 // Clear on initiative of Dart.
126 weak_persistent_value.Clear();
127
128 event.Signal();
129 }));
130
131 ASSERT_TRUE(RunWithEntrypoint("testClearLater"));
132
133 event.Wait();
134}
135
136// Handle outside the test body scope so it survives until isolate shutdown.
139
142
143 AddNativeCallback("GiveObjectToNative",
145 auto handle = Dart_GetNativeArgument(args, 0);
146
147 auto dart_state = tonic::DartState::Current();
148 ASSERT_TRUE(dart_state);
149 ASSERT_TRUE(tonic::DartState::Current());
150
151 // The test is repeated, ensure the global var is
152 // cleared before use.
154
156 dart_state, handle, nullptr, 0, NopFinalizer);
157
158 // Do not clear handle, so it is cleared on shutdown.
159
160 event.Signal();
161 }));
162
163 ASSERT_TRUE(RunWithEntrypoint("callGiveObjectToNative"));
164
165 event.Wait();
166}
167
168} // namespace testing
169} // namespace flutter
static sk_sp< Effect > Create()
Definition: RefCntTest.cpp:117
virtual Settings CreateSettingsForFixture()
Definition: dart_fixture.cc:30
bool RunInIsolateScope(std::function< bool(void)> closure)
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
static DartState * Current()
Definition: dart_state.cc:56
void Set(DartState *dart_state, Dart_Handle object, void *peer, intptr_t external_allocation_size, Dart_HandleFinalizer callback)
DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, int index)
struct _Dart_NativeArguments * Dart_NativeArguments
Definition: dart_api.h:3019
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
FlKeyEvent * event
Dart_NativeFunction function
Definition: fuchsia.cc:51
std::string GetCurrentTestName()
Gets the name of the currently running test. This is useful in generating logs or assets based on tes...
Definition: testing.cc:15
TEST_F(DisplayListTest, Defaults)
std::string GetDefaultKernelFilePath()
Returns the default path to kernel_blob.bin. This file is within the directory returned by GetFixture...
Definition: testing.cc:19
std::unique_ptr< AutoIsolateShutdown > RunDartCodeInIsolate(DartVMRef &vm_ref, const Settings &settings, const TaskRunners &task_runners, std::string entrypoint, const std::vector< std::string > &args, const std::string &kernel_file_path, fml::WeakPtr< IOManager > io_manager, std::shared_ptr< VolatilePathTracker > volatile_path_tracker, std::unique_ptr< PlatformConfiguration > platform_configuration)
tonic::DartWeakPersistentValue global_weak_persistent_value
std::function< void()> closure
Definition: closure.h:14
#define CREATE_NATIVE_ENTRY(native_entry)