Flutter Engine
The Flutter Engine
hooks_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 <memory>
6
7#include "flutter/common/task_runners.h"
8#include "flutter/fml/synchronization/waitable_event.h"
9#include "flutter/runtime/dart_vm.h"
10#include "flutter/shell/common/shell_test.h"
11#include "flutter/shell/common/thread_host.h"
12#include "flutter/testing/testing.h"
13#include "third_party/dart/runtime/include/dart_api.h"
14
15// CREATE_NATIVE_ENTRY is leaky by design
16// NOLINTBEGIN(clang-analyzer-core.StackAddressEscape)
17
18namespace flutter {
19namespace testing {
20
22
23#define CHECK_DART_ERROR(name) \
24 EXPECT_FALSE(Dart_IsError(name)) << Dart_GetError(name)
25
26TEST_F(HooksTest, HooksUnitTests) {
27 auto settings = CreateSettingsForFixture();
28
29 TaskRunners task_runners(GetCurrentTestName(), // label
30 GetCurrentTaskRunner(), // platform
31 CreateNewThread("raster"), // raster
32 CreateNewThread("ui"), // ui
33 CreateNewThread("io") // io
34 );
35
36 auto message_latch = std::make_shared<fml::AutoResetWaitableEvent>();
37
38 std::unique_ptr<Shell> shell = CreateShell(settings, task_runners);
39 ASSERT_TRUE(shell->IsSetup());
40
41 auto call_hook = [](Dart_NativeArguments args) {
43 CHECK_DART_ERROR(hook_name);
44
45 Dart_Handle ui_library = Dart_LookupLibrary(tonic::ToDart("dart:ui"));
46 CHECK_DART_ERROR(ui_library);
47
48 Dart_Handle hook = Dart_GetField(ui_library, hook_name);
49 CHECK_DART_ERROR(hook);
50
51 Dart_Handle arg_count_handle = Dart_GetNativeArgument(args, 1);
52 CHECK_DART_ERROR(arg_count_handle);
53
54 int64_t arg_count;
55 Dart_IntegerToInt64(arg_count_handle, &arg_count);
56
57 std::vector<Dart_Handle> hook_args;
58 for (int i = 0; i < static_cast<int>(arg_count); i++) {
59 hook_args.push_back(Dart_GetNativeArgument(args, 2 + i));
60 CHECK_DART_ERROR(hook_args.back());
61 }
62
63 Dart_Handle hook_result =
64 Dart_InvokeClosure(hook, hook_args.size(), hook_args.data());
65 CHECK_DART_ERROR(hook_result);
66 };
67
68 auto finished = [&message_latch](Dart_NativeArguments args) {
69 message_latch->Signal();
70 };
71 AddNativeCallback("CallHook", CREATE_NATIVE_ENTRY(call_hook));
72 AddNativeCallback("Finish", CREATE_NATIVE_ENTRY(finished));
73
75 configuration.SetEntrypoint("hooksTests");
76
77 shell->RunEngine(std::move(configuration), [](auto result) {
79 });
80
81 message_latch->Wait();
82 DestroyShell(std::move(shell), task_runners);
83}
84
85} // namespace testing
86} // namespace flutter
87
88// NOLINTEND(clang-analyzer-core.StackAddressEscape)
static RunConfiguration InferFromSettings(const Settings &settings, const fml::RefPtr< fml::TaskRunner > &io_worker=nullptr, IsolateLaunchType launch_type=IsolateLaunchType::kNewGroup)
Attempts to infer a run configuration from the settings object. This tries to create a run configurat...
DART_EXPORT Dart_Handle Dart_IntegerToInt64(Dart_Handle integer, int64_t *value)
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, int index)
DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_InvokeClosure(Dart_Handle closure, int number_of_arguments, Dart_Handle *arguments)
DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url)
struct _Dart_NativeArguments * Dart_NativeArguments
Definition: dart_api.h:3019
DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_GetField(Dart_Handle container, Dart_Handle name)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GAsyncResult * result
#define CHECK_DART_ERROR(name)
fml::RefPtr< fml::TaskRunner > CreateNewThread(const std::string &name)
fml::RefPtr< fml::TaskRunner > GetCurrentTaskRunner()
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)
Dart_Handle ToDart(const T &object)
#define CREATE_NATIVE_ENTRY(native_entry)