Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
type_conversions_unittests.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/runtime/dart_vm_lifecycle.h"
6#include "flutter/testing/dart_isolate_runner.h"
7#include "flutter/testing/fixture_test.h"
8#include "flutter/testing/testing.h"
9#include "flutter/third_party/tonic/converter/dart_converter.h"
10
11// CREATE_NATIVE_ENTRY is leaky by design
12// NOLINTBEGIN(clang-analyzer-core.StackAddressEscape)
13
14namespace flutter {
15namespace testing {
16
18 public:
20 : settings_(CreateSettingsForFixture()),
21 vm_(DartVMRef::Create(settings_)) {}
22
24
25 [[nodiscard]] bool RunWithEntrypoint(const std::string& entrypoint) {
26 if (running_isolate_) {
27 return false;
28 }
29 auto thread = CreateNewThread();
30 TaskRunners single_threaded_task_runner(GetCurrentTestName(), thread,
31 thread, thread, thread);
32 auto isolate =
33 RunDartCodeInIsolate(vm_, settings_, single_threaded_task_runner,
34 entrypoint, {}, GetDefaultKernelFilePath());
35 if (!isolate || isolate->get()->GetPhase() != DartIsolate::Phase::Running) {
36 return false;
37 }
38
39 running_isolate_ = std::move(isolate);
40 return true;
41 }
42
43 private:
44 Settings settings_;
45 DartVMRef vm_;
46 std::unique_ptr<AutoIsolateShutdown> running_isolate_;
48};
49
51 ASSERT_TRUE(RunWithEntrypoint("main"));
52}
53
54TEST_F(TypeConversionsTest, CanConvertEmptyList) {
56 AddNativeCallback(
57 "NotifySuccess", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
58 auto bool_handle = Dart_GetNativeArgument(args, 0);
59 ASSERT_FALSE(tonic::CheckAndHandleError(bool_handle));
60 ASSERT_TRUE(tonic::DartConverter<bool>::FromDart(bool_handle));
61 event.Signal();
62 }));
63 AddNativeCallback(
64 "NotifyNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) {
65 std::vector<int64_t> items;
66 auto items_handle = tonic::ToDart(items);
67 ASSERT_FALSE(tonic::CheckAndHandleError(items_handle));
68 tonic::DartInvokeField(::Dart_RootLibrary(), "testCanConvertEmptyList",
69 {items_handle});
70 }));
71 ASSERT_TRUE(RunWithEntrypoint("trampoline"));
72 event.Wait();
73}
74
75TEST_F(TypeConversionsTest, CanConvertListOfStrings) {
77 AddNativeCallback(
78 "NotifySuccess", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
79 auto bool_handle = Dart_GetNativeArgument(args, 0);
80 ASSERT_FALSE(tonic::CheckAndHandleError(bool_handle));
81 ASSERT_TRUE(tonic::DartConverter<bool>::FromDart(bool_handle));
82 event.Signal();
83 }));
84 AddNativeCallback(
85 "NotifyNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) {
86 std::vector<std::string> items;
87 items.push_back("tinker");
88 items.push_back("tailor");
89 items.push_back("soldier");
90 items.push_back("sailor");
91 auto items_handle = tonic::ToDart(items);
92 ASSERT_FALSE(tonic::CheckAndHandleError(items_handle));
94 "testCanConvertListOfStrings", {items_handle});
95 }));
96 ASSERT_TRUE(RunWithEntrypoint("trampoline"));
97 event.Wait();
98}
99
100TEST_F(TypeConversionsTest, CanConvertListOfDoubles) {
102 AddNativeCallback(
103 "NotifySuccess", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
104 auto bool_handle = Dart_GetNativeArgument(args, 0);
105 ASSERT_FALSE(tonic::CheckAndHandleError(bool_handle));
106 ASSERT_TRUE(tonic::DartConverter<bool>::FromDart(bool_handle));
107 event.Signal();
108 }));
109 AddNativeCallback(
110 "NotifyNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) {
111 std::vector<double> items;
112 items.push_back(1.0);
113 items.push_back(2.0);
114 items.push_back(3.0);
115 items.push_back(4.0);
116 auto items_handle = tonic::ToDart(items);
117 ASSERT_FALSE(tonic::CheckAndHandleError(items_handle));
119 "testCanConvertListOfDoubles", {items_handle});
120 }));
121 ASSERT_TRUE(RunWithEntrypoint("trampoline"));
122 event.Wait();
123}
124
125TEST_F(TypeConversionsTest, CanConvertListOfInts) {
127 AddNativeCallback(
128 "NotifySuccess", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
129 auto bool_handle = Dart_GetNativeArgument(args, 0);
130 ASSERT_FALSE(tonic::CheckAndHandleError(bool_handle));
131 ASSERT_TRUE(tonic::DartConverter<bool>::FromDart(bool_handle));
132 event.Signal();
133 }));
134 AddNativeCallback(
135 "NotifyNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) {
136 std::vector<int32_t> items;
137 items.push_back(1);
138 items.push_back(2);
139 items.push_back(3);
140 items.push_back(4);
141 auto items_handle = tonic::ToDart(items);
142 ASSERT_FALSE(tonic::CheckAndHandleError(items_handle));
143 tonic::DartInvokeField(::Dart_RootLibrary(), "testCanConvertListOfInts",
144 {items_handle});
145 }));
146 ASSERT_TRUE(RunWithEntrypoint("trampoline"));
147 event.Wait();
148}
149
150} // namespace testing
151} // namespace flutter
152
153// NOLINTEND(clang-analyzer-core.StackAddressEscape)
static sk_sp< Effect > Create()
virtual Settings CreateSettingsForFixture()
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...
bool RunWithEntrypoint(const std::string &entrypoint)
DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, int index)
struct _Dart_NativeArguments * Dart_NativeArguments
Definition dart_api.h:3010
DART_EXPORT Dart_Handle Dart_RootLibrary(void)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
FlKeyEvent * event
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
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)
Dart_Handle ToDart(const T &object)
bool CheckAndHandleError(Dart_Handle handle)
Definition dart_error.cc:33
Dart_Handle DartInvokeField(Dart_Handle target, const char *name, std::initializer_list< Dart_Handle > args)
#define CREATE_NATIVE_ENTRY(native_entry)