• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/runtime_test.h"
6 
7 #include "flutter/runtime/dart_vm.h"
8 #include "flutter/testing/testing.h"
9 
10 namespace flutter {
11 namespace testing {
12 
RuntimeTest()13 RuntimeTest::RuntimeTest()
14     : native_resolver_(std::make_shared<TestDartNativeResolver>()) {}
15 
16 RuntimeTest::~RuntimeTest() = default;
17 
SetSnapshotsAndAssets(Settings & settings)18 void RuntimeTest::SetSnapshotsAndAssets(Settings& settings) {
19   if (!assets_dir_.is_valid()) {
20     return;
21   }
22 
23   settings.assets_dir = assets_dir_.get();
24 
25   // In JIT execution, all snapshots are present within the binary itself and
26   // don't need to be explicitly suppiled by the embedder.
27   if (DartVM::IsRunningPrecompiledCode()) {
28     settings.vm_snapshot_data = [this]() {
29       return fml::FileMapping::CreateReadOnly(assets_dir_, "vm_snapshot_data");
30     };
31 
32     settings.isolate_snapshot_data = [this]() {
33       return fml::FileMapping::CreateReadOnly(assets_dir_,
34                                               "isolate_snapshot_data");
35     };
36 
37     if (DartVM::IsRunningPrecompiledCode()) {
38       settings.vm_snapshot_instr = [this]() {
39         return fml::FileMapping::CreateReadExecute(assets_dir_,
40                                                    "vm_snapshot_instr");
41       };
42 
43       settings.isolate_snapshot_instr = [this]() {
44         return fml::FileMapping::CreateReadExecute(assets_dir_,
45                                                    "isolate_snapshot_instr");
46       };
47     }
48   } else {
49     settings.application_kernels = [this]() {
50       std::vector<std::unique_ptr<const fml::Mapping>> kernel_mappings;
51       kernel_mappings.emplace_back(
52           fml::FileMapping::CreateReadOnly(assets_dir_, "kernel_blob.bin"));
53       return kernel_mappings;
54     };
55   }
56 }
57 
CreateSettingsForFixture()58 Settings RuntimeTest::CreateSettingsForFixture() {
59   Settings settings;
60   settings.leak_vm = false;
61   settings.task_observer_add = [](intptr_t, fml::closure) {};
62   settings.task_observer_remove = [](intptr_t) {};
63   settings.isolate_create_callback = [this]() {
64     native_resolver_->SetNativeResolverForIsolate();
65   };
66   SetSnapshotsAndAssets(settings);
67   return settings;
68 }
69 
70 // |testing::ThreadTest|
SetUp()71 void RuntimeTest::SetUp() {
72   assets_dir_ =
73       fml::OpenDirectory(GetFixturesPath(), false, fml::FilePermission::kRead);
74   ThreadTest::SetUp();
75 }
76 
77 // |testing::ThreadTest|
TearDown()78 void RuntimeTest::TearDown() {
79   ThreadTest::TearDown();
80   assets_dir_.reset();
81 }
82 
AddNativeCallback(std::string name,Dart_NativeFunction callback)83 void RuntimeTest::AddNativeCallback(std::string name,
84                                     Dart_NativeFunction callback) {
85   native_resolver_->AddNativeCallback(std::move(name), callback);
86 }
87 
88 }  // namespace testing
89 }  // namespace flutter
90