| 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_fixture.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | #include "flutter/fml/paths.h" |
| 9 | |
| 10 | namespace flutter::testing { |
| 11 | |
| 12 | DartFixture::DartFixture() |
| 13 | : DartFixture("kernel_blob.bin" , |
| 14 | kDefaultAOTAppELFFileName, |
| 15 | kDefaultAOTAppELFSplitFileName) {} |
| 16 | |
| 17 | DartFixture::DartFixture(std::string kernel_filename, |
| 18 | std::string elf_filename, |
| 19 | std::string elf_split_filename) |
| 20 | : native_resolver_(std::make_shared<TestDartNativeResolver>()), |
| 21 | split_aot_symbols_(LoadELFSplitSymbolFromFixturesIfNeccessary( |
| 22 | elf_split_filename: std::move(elf_split_filename))), |
| 23 | kernel_filename_(std::move(kernel_filename)), |
| 24 | assets_dir_(fml::OpenDirectory(path: GetFixturesPath(), |
| 25 | create_if_necessary: false, |
| 26 | permission: fml::FilePermission::kRead)), |
| 27 | aot_symbols_( |
| 28 | LoadELFSymbolFromFixturesIfNeccessary(elf_filename: std::move(elf_filename))) {} |
| 29 | |
| 30 | Settings DartFixture::CreateSettingsForFixture() { |
| 31 | Settings settings; |
| 32 | settings.leak_vm = false; |
| 33 | settings.task_observer_add = [](intptr_t, const fml::closure&) {}; |
| 34 | settings.task_observer_remove = [](intptr_t) {}; |
| 35 | settings.isolate_create_callback = [this]() { |
| 36 | native_resolver_->SetNativeResolverForIsolate(); |
| 37 | }; |
| 38 | settings.enable_vm_service = false; |
| 39 | SetSnapshotsAndAssets(settings); |
| 40 | return settings; |
| 41 | } |
| 42 | |
| 43 | void DartFixture::SetSnapshotsAndAssets(Settings& settings) { |
| 44 | if (!assets_dir_.is_valid()) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | settings.assets_dir = assets_dir_.get(); |
| 49 | |
| 50 | // In JIT execution, all snapshots are present within the binary itself and |
| 51 | // don't need to be explicitly supplied by the embedder. In AOT, these |
| 52 | // snapshots will be present in the application AOT dylib. |
| 53 | if (DartVM::IsRunningPrecompiledCode()) { |
| 54 | FML_CHECK(PrepareSettingsForAOTWithSymbols(settings, aot_symbols_)); |
| 55 | #if FML_OS_LINUX |
| 56 | settings.vmservice_snapshot_library_path.emplace_back(args: fml::paths::JoinPaths( |
| 57 | components: {GetTestingAssetsPath(), "libvmservice_snapshot.so" })); |
| 58 | #endif // FML_OS_LINUX |
| 59 | } else { |
| 60 | settings.application_kernels = [this]() -> Mappings { |
| 61 | std::vector<std::unique_ptr<const fml::Mapping>> kernel_mappings; |
| 62 | auto kernel_mapping = |
| 63 | fml::FileMapping::CreateReadOnly(base_fd: assets_dir_, sub_path: kernel_filename_); |
| 64 | if (!kernel_mapping || !kernel_mapping->IsValid()) { |
| 65 | FML_LOG(ERROR) << "Could not find kernel blob for test fixture not " |
| 66 | "running in precompiled mode." ; |
| 67 | return kernel_mappings; |
| 68 | } |
| 69 | kernel_mappings.emplace_back(args: std::move(kernel_mapping)); |
| 70 | return kernel_mappings; |
| 71 | }; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void DartFixture::AddNativeCallback(const std::string& name, |
| 76 | Dart_NativeFunction callback) { |
| 77 | native_resolver_->AddNativeCallback(name, callback); |
| 78 | } |
| 79 | |
| 80 | void DartFixture::AddFfiNativeCallback(const std::string& name, |
| 81 | void* callback_ptr) { |
| 82 | native_resolver_->AddFfiNativeCallback(name, callback_ptr); |
| 83 | } |
| 84 | |
| 85 | } // namespace flutter::testing |
| 86 | |