Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
dart_state_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/third_party/tonic/dart_state.h"
6#include "flutter/common/task_runners.h"
7#include "flutter/runtime/dart_vm_lifecycle.h"
8#include "flutter/runtime/isolate_configuration.h"
9#include "flutter/testing/fixture_test.h"
10
11namespace flutter {
12namespace testing {
13
15
16TEST_F(DartState, CurrentWithNullDataDoesNotSegfault) {
17 ASSERT_FALSE(DartVMRef::IsInstanceRunning());
18 auto settings = CreateSettingsForFixture();
19 auto vm_snapshot = DartSnapshot::VMSnapshotFromSettings(settings);
20 auto isolate_snapshot = DartSnapshot::IsolateSnapshotFromSettings(settings);
21 auto vm_ref = DartVMRef::Create(settings, vm_snapshot, isolate_snapshot);
22 ASSERT_TRUE(vm_ref);
23 auto vm_data = vm_ref.GetVMData();
24 ASSERT_TRUE(vm_data);
25 TaskRunners task_runners(GetCurrentTestName(), //
26 GetCurrentTaskRunner(), //
27 GetCurrentTaskRunner(), //
28 GetCurrentTaskRunner(), //
29 GetCurrentTaskRunner() //
30 );
31 auto isolate_configuration =
33 Dart_IsolateFlags isolate_flags;
34 Dart_IsolateFlagsInitialize(&isolate_flags);
35 isolate_flags.null_safety =
36 isolate_configuration->IsNullSafetyEnabled(*isolate_snapshot);
37 isolate_flags.snapshot_is_dontneed_safe = isolate_snapshot->IsDontNeedSafe();
38 char* error;
40 "main.dart", "main", vm_data->GetIsolateSnapshot()->GetDataMapping(),
41 vm_data->GetIsolateSnapshot()->GetInstructionsMapping(), &isolate_flags,
42 nullptr, nullptr, &error);
43 ASSERT_FALSE(error) << error;
44 ::free(error);
45
46 ASSERT_FALSE(tonic::DartState::Current());
47
49 ASSERT_TRUE(Dart_CurrentIsolate() == nullptr);
50}
51
52TEST_F(DartState, IsShuttingDown) {
53 ASSERT_FALSE(DartVMRef::IsInstanceRunning());
54 auto settings = CreateSettingsForFixture();
55 auto vm_ref = DartVMRef::Create(settings);
56 ASSERT_TRUE(vm_ref);
57 auto vm_data = vm_ref.GetVMData();
58 ASSERT_TRUE(vm_data);
59 TaskRunners task_runners(GetCurrentTestName(), //
60 GetCurrentTaskRunner(), //
61 GetCurrentTaskRunner(), //
62 GetCurrentTaskRunner(), //
63 GetCurrentTaskRunner() //
64 );
65 auto isolate_configuration =
67
68 UIDartState::Context context(std::move(task_runners));
69 context.advisory_script_uri = "main.dart";
70 context.advisory_script_entrypoint = "main";
71 auto weak_isolate = DartIsolate::CreateRunningRootIsolate(
72 vm_data->GetSettings(), // settings
73 vm_data->GetIsolateSnapshot(), // isolate snapshot
74 nullptr, // platform configuration
75 DartIsolate::Flags{}, // flags
76 nullptr, // root_isolate_create_callback
77 settings.isolate_create_callback, // isolate create callback
78 settings.isolate_shutdown_callback, // isolate shutdown callback
79 "main", // dart entrypoint
80 std::nullopt, // dart entrypoint library
81 {}, // dart entrypoint arguments
82 std::move(isolate_configuration), // isolate configuration
83 std::move(context) // engine context
84 );
85 auto root_isolate = weak_isolate.lock();
86 ASSERT_TRUE(root_isolate);
87
88 tonic::DartState* dart_state = root_isolate.get();
89 ASSERT_FALSE(dart_state->IsShuttingDown());
90
91 ASSERT_TRUE(root_isolate->Shutdown());
92
93 ASSERT_TRUE(dart_state->IsShuttingDown());
94}
95
96} // namespace testing
97} // namespace flutter
static std::weak_ptr< DartIsolate > CreateRunningRootIsolate(const Settings &settings, const fml::RefPtr< const DartSnapshot > &isolate_snapshot, std::unique_ptr< PlatformConfiguration > platform_configuration, Flags flags, const fml::closure &root_isolate_create_callback, const fml::closure &isolate_create_callback, const fml::closure &isolate_shutdown_callback, std::optional< std::string > dart_entrypoint, std::optional< std::string > dart_entrypoint_library, const std::vector< std::string > &dart_entrypoint_args, std::unique_ptr< IsolateConfiguration > isolate_configuration, const UIDartState::Context &context, const DartIsolate *spawning_isolate=nullptr)
Creates an instance of a root isolate and returns a weak pointer to the same. The isolate instance ma...
static fml::RefPtr< const DartSnapshot > VMSnapshotFromSettings(const Settings &settings)
From the fields present in the given settings object, infer the core snapshot.
static fml::RefPtr< const DartSnapshot > IsolateSnapshotFromSettings(const Settings &settings)
From the fields present in the given settings object, infer the isolate snapshot.
static DartVMRef Create(const Settings &settings, fml::RefPtr< const DartSnapshot > vm_snapshot=nullptr, fml::RefPtr< const DartSnapshot > isolate_snapshot=nullptr)
static bool IsInstanceRunning()
static std::unique_ptr< IsolateConfiguration > InferFromSettings(const Settings &settings, const std::shared_ptr< AssetManager > &asset_manager=nullptr, const fml::RefPtr< fml::TaskRunner > &io_worker=nullptr, IsolateLaunchType launch_type=IsolateLaunchType::kNewGroup)
Attempts to infer the isolate configuration from the Settings object. If the VM is configured for AOT...
static DartState * Current()
Definition dart_state.cc:56
bool IsShuttingDown()
Definition dart_state.h:74
DART_EXPORT Dart_Isolate Dart_CreateIsolateGroup(const char *script_uri, const char *name, const uint8_t *isolate_snapshot_data, const uint8_t *isolate_snapshot_instructions, Dart_IsolateFlags *flags, void *isolate_group_data, void *isolate_data, char **error)
DART_EXPORT void Dart_IsolateFlagsInitialize(Dart_IsolateFlags *flags)
DART_EXPORT void Dart_ShutdownIsolate(void)
DART_EXPORT Dart_Isolate Dart_CurrentIsolate(void)
const uint8_t uint32_t uint32_t GError ** error
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)
bool snapshot_is_dontneed_safe
Definition dart_api.h:595
The subset of state which is owned by the shell or engine and passed through the RuntimeController in...