Flutter Engine
embedder_test_context.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/shell/platform/embedder/tests/embedder_test_context.h"
6 
7 #include "flutter/fml/make_copyable.h"
8 #include "flutter/fml/paths.h"
9 #include "flutter/runtime/dart_vm.h"
10 #include "flutter/shell/platform/embedder/tests/embedder_assertions.h"
11 #include "flutter/testing/testing.h"
12 #include "third_party/dart/runtime/bin/elf_loader.h"
13 #include "third_party/skia/include/core/SkSurface.h"
14 
15 namespace flutter {
16 namespace testing {
17 
19  : assets_path_(std::move(assets_path)),
20  aot_symbols_(
22  native_resolver_(std::make_shared<TestDartNativeResolver>()) {
25  isolate_create_callbacks_.push_back(
26  [weak_resolver =
27  std::weak_ptr<TestDartNativeResolver>{native_resolver_}]() {
28  if (auto resolver = weak_resolver.lock()) {
29  resolver->SetNativeResolverForIsolate();
30  }
31  });
32 }
33 
35 
38  return;
39  }
41  std::make_unique<fml::NonOwnedMapping>(aot_symbols_.vm_snapshot_data, 0u);
42  vm_snapshot_instructions_ = std::make_unique<fml::NonOwnedMapping>(
45  std::make_unique<fml::NonOwnedMapping>(aot_symbols_.vm_isolate_data, 0u);
46  isolate_snapshot_instructions_ = std::make_unique<fml::NonOwnedMapping>(
48 }
49 
52  return;
53  }
54  FlutterEngineAOTDataSource data_in = {};
55  FlutterEngineAOTData data_out = nullptr;
56 
57  const auto elf_path = fml::paths::JoinPaths(
59 
61  data_in.elf_path = elf_path.c_str();
62 
63  ASSERT_EQ(FlutterEngineCreateAOTData(&data_in, &data_out), kSuccess);
64 
65  aot_data_.reset(data_out);
66 }
67 
68 const std::string& EmbedderTestContext::GetAssetsPath() const {
69  return assets_path_;
70 }
71 
73  return vm_snapshot_data_.get();
74 }
75 
77  return vm_snapshot_instructions_.get();
78 }
79 
81  return isolate_snapshot_data_.get();
82 }
83 
85  const {
86  return isolate_snapshot_instructions_.get();
87 }
88 
90  return aot_data_.get();
91 }
92 
95 }
96 
98  if (closure) {
99  isolate_create_callbacks_.push_back(closure);
100  }
101 }
102 
104  return [](void* user_data) {
105  reinterpret_cast<EmbedderTestContext*>(user_data)
107  };
108 }
109 
111  for (auto closure : isolate_create_callbacks_) {
112  closure();
113  }
114 }
115 
117  Dart_NativeFunction function) {
118  native_resolver_->AddNativeCallback({name}, function);
119 }
120 
122  const SemanticsNodeCallback& update_semantics_node_callback) {
123  update_semantics_node_callback_ = update_semantics_node_callback;
124 }
125 
127  const SemanticsActionCallback& update_semantics_custom_action_callback) {
129  update_semantics_custom_action_callback;
130 }
131 
133  const std::function<void(const FlutterPlatformMessage*)>& callback) {
135 }
136 
138  const FlutterPlatformMessage* message) {
141  }
142 }
143 
145  const LogMessageCallback& callback) {
147 }
148 
151  return [](const FlutterSemanticsNode* semantics_node, void* user_data) {
152  auto context = reinterpret_cast<EmbedderTestContext*>(user_data);
153  if (auto callback = context->update_semantics_node_callback_) {
154  callback(semantics_node);
155  }
156  };
157 }
158 
161  return [](const FlutterSemanticsCustomAction* action, void* user_data) {
162  auto context = reinterpret_cast<EmbedderTestContext*>(user_data);
163  if (auto callback = context->update_semantics_custom_action_callback_) {
164  callback(action);
165  }
166  };
167 }
168 
170  return [](const char* tag, const char* message, void* user_data) {
171  auto context = reinterpret_cast<EmbedderTestContext*>(user_data);
172  if (auto callback = context->log_message_callback_) {
173  callback(tag, message);
174  }
175  };
176 }
177 
180  return [](const FlutterLocale** supported_locales,
181  size_t length) -> const FlutterLocale* {
182  return supported_locales[0];
183  };
184 }
185 
188 }
189 
192  << "Accessed the compositor on a context where one was not set up. Use "
193  "the config builder to set up a context with a custom compositor.";
194  return *compositor_;
195 }
196 
198  const NextSceneCallback& next_scene_callback) {
199  if (compositor_) {
200  compositor_->SetNextSceneCallback(next_scene_callback);
201  return;
202  }
203  next_scene_callback_ = next_scene_callback;
204 }
205 
206 std::future<sk_sp<SkImage>> EmbedderTestContext::GetNextSceneImage() {
207  std::promise<sk_sp<SkImage>> promise;
208  auto future = promise.get_future();
210  fml::MakeCopyable([promise = std::move(promise)](auto image) mutable {
211  promise.set_value(image);
212  }));
213  return future;
214 }
215 
216 /// @note Procedure doesn't copy all closures.
218  const std::function<sk_sp<SkImage>(void)>& image_callback) {
219  if (!next_scene_callback_) {
220  return;
221  }
223  next_scene_callback_ = nullptr;
224  callback(image_callback());
225 }
226 
227 } // namespace testing
228 } // namespace flutter
const uint8_t * vm_snapshot_instrs
Definition: elf_loader.h:36
void SetSemanticsNodeCallback(const SemanticsNodeCallback &update_semantics_node)
void AddIsolateCreateCallback(fml::closure closure)
void FireRootSurfacePresentCallbackIfPresent(const std::function< sk_sp< SkImage >(void)> &image_callback)
std::unique_ptr< fml::Mapping > vm_snapshot_instructions_
const uint8_t * vm_isolate_data
Definition: elf_loader.h:37
std::function< void(const FlutterPlatformMessage *)> platform_message_callback_
void(* FlutterLogMessageCallback)(const char *, const char *, void *)
Definition: embedder.h:1340
void * user_data
std::vector< fml::closure > isolate_create_callbacks_
Definition: ref_ptr.h:252
Dart_NativeFunction function
Definition: fuchsia.cc:51
std::shared_ptr< TestDartNativeResolver > native_resolver_
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
FlutterTransformation GetRootSurfaceTransformation()
void(* FlutterUpdateSemanticsNodeCallback)(const FlutterSemanticsNode *, void *)
Definition: embedder.h:887
EmbedderTestContext(std::string assets_path="")
ELFAOTSymbols LoadELFSymbolFromFixturesIfNeccessary(std::string elf_filename)
Attempts to resolve AOT symbols from the portable ELF loader. This location is automatically resolved...
Definition: elf_loader.cc:15
static FlutterUpdateSemanticsNodeCallback GetUpdateSemanticsNodeCallbackHook()
void(* FlutterUpdateSemanticsCustomActionCallback)(const FlutterSemanticsCustomAction *, void *)
Definition: embedder.h:891
FlKeyEvent FlKeyResponderAsyncCallback callback
std::unique_ptr< fml::Mapping > isolate_snapshot_instructions_
const fml::Mapping * GetVMSnapshotInstructions() const
const FlutterLocale *(* FlutterComputePlatformResolvedLocaleCallback)(const FlutterLocale **, size_t)
Definition: embedder.h:1182
const fml::Mapping * GetVMSnapshotData() const
void SetLogMessageCallback(const LogMessageCallback &log_message_callback)
std::function< void(const char *tag, const char *message)> LogMessageCallback
std::function< void(const FlutterSemanticsCustomAction *)> SemanticsActionCallback
std::function< void()> closure
Definition: closure.h:14
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition: make_copyable.h:57
std::string JoinPaths(std::initializer_list< std::string > components)
Definition: paths.cc:14
void SetSemanticsCustomActionCallback(const SemanticsActionCallback &semantics_custom_action)
SemanticsNodeCallback update_semantics_node_callback_
void SetNextSceneCallback(const NextSceneCallback &next_scene_callback)
void SetPlatformMessageCallback(const std::function< void(const FlutterPlatformMessage *)> &callback)
FlutterEngineAOTData GetAOTData() const
const char * GetFixturesPath()
Returns the directory containing the test fixture for the target if this target has fixtures configur...
SemanticsAction action
SemanticsActionCallback update_semantics_custom_action_callback_
std::unique_ptr< EmbedderTestCompositor > compositor_
static bool IsRunningPrecompiledCode()
Checks if VM instances in the process can run precompiled code. This call can be made at any time and...
Definition: dart_vm.cc:202
static FlutterUpdateSemanticsCustomActionCallback GetUpdateSemanticsCustomActionCallbackHook()
const uint8_t * vm_isolate_instrs
Definition: elf_loader.h:38
constexpr const char * kDefaultAOTAppELFFileName
Definition: elf_loader.h:17
void PlatformMessageCallback(const FlutterPlatformMessage *message)
std::unique_ptr< fml::Mapping > vm_snapshot_data_
size_t length
void AddNativeCallback(const char *name, Dart_NativeFunction function)
#define FML_CHECK(condition)
Definition: logging.h:68
const uint8_t * vm_snapshot_data
Definition: elf_loader.h:35
const std::string & GetAssetsPath() const
void(* VoidCallback)(void *)
Definition: embedder.h:251
FlutterEngineAOTDataSourceType type
Definition: embedder.h:1326
static FlutterComputePlatformResolvedLocaleCallback GetComputePlatformResolvedLocaleCallbackHook()
const fml::Mapping * GetIsolateSnapshotInstructions() const
std::unique_ptr< fml::Mapping > isolate_snapshot_data_
std::future< sk_sp< SkImage > > GetNextSceneImage()
static FlutterLogMessageCallback GetLogMessageCallbackHook()
const char * elf_path
Absolute path to an ELF library file.
Definition: embedder.h:1329
FlutterTransformation FlutterTransformationMake(const SkMatrix &matrix)
std::function< void(sk_sp< SkImage > image)> NextSceneCallback
const fml::Mapping * GetIsolateSnapshotData() const
std::function< void(const FlutterSemanticsNode *)> SemanticsNodeCallback
FlutterEngineResult FlutterEngineCreateAOTData(const FlutterEngineAOTDataSource *source, FlutterEngineAOTData *data_out)
Creates the necessary data structures to launch a Flutter Dart application in AOT mode...
Definition: embedder.cc:748