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)),
21  native_resolver_(std::make_shared<TestDartNativeResolver>()) {
24  isolate_create_callbacks_.push_back(
25  [weak_resolver =
26  std::weak_ptr<TestDartNativeResolver>{native_resolver_}]() {
27  if (auto resolver = weak_resolver.lock()) {
28  resolver->SetNativeResolverForIsolate();
29  }
30  });
31 }
32 
34 
37  return;
38  }
40  std::make_unique<fml::NonOwnedMapping>(aot_symbols_.vm_snapshot_data, 0u);
41  vm_snapshot_instructions_ = std::make_unique<fml::NonOwnedMapping>(
44  std::make_unique<fml::NonOwnedMapping>(aot_symbols_.vm_isolate_data, 0u);
45  isolate_snapshot_instructions_ = std::make_unique<fml::NonOwnedMapping>(
47 }
48 
51  return;
52  }
53  FlutterEngineAOTDataSource data_in = {};
54  FlutterEngineAOTData data_out = nullptr;
55 
56  const auto elf_path =
58 
60  data_in.elf_path = elf_path.c_str();
61 
62  ASSERT_EQ(FlutterEngineCreateAOTData(&data_in, &data_out), kSuccess);
63 
64  aot_data_.reset(data_out);
65 }
66 
67 const std::string& EmbedderTestContext::GetAssetsPath() const {
68  return assets_path_;
69 }
70 
72  return vm_snapshot_data_.get();
73 }
74 
76  return vm_snapshot_instructions_.get();
77 }
78 
80  return isolate_snapshot_data_.get();
81 }
82 
84  const {
85  return isolate_snapshot_instructions_.get();
86 }
87 
89  return aot_data_.get();
90 }
91 
94 }
95 
97  if (closure) {
98  isolate_create_callbacks_.push_back(closure);
99  }
100 }
101 
103  return [](void* user_data) {
104  reinterpret_cast<EmbedderTestContext*>(user_data)
106  };
107 }
108 
110  for (auto closure : isolate_create_callbacks_) {
111  closure();
112  }
113 }
114 
116  Dart_NativeFunction function) {
117  native_resolver_->AddNativeCallback({name}, function);
118 }
119 
121  const SemanticsNodeCallback& update_semantics_node_callback) {
122  update_semantics_node_callback_ = update_semantics_node_callback;
123 }
124 
126  const SemanticsActionCallback& update_semantics_custom_action_callback) {
128  update_semantics_custom_action_callback;
129 }
130 
132  const std::function<void(const FlutterPlatformMessage*)>& callback) {
133  platform_message_callback_ = callback;
134 }
135 
137  const FlutterPlatformMessage* message) {
140  }
141 }
142 
145  return [](const FlutterSemanticsNode* semantics_node, void* user_data) {
146  auto context = reinterpret_cast<EmbedderTestContext*>(user_data);
147  if (auto callback = context->update_semantics_node_callback_) {
148  callback(semantics_node);
149  }
150  };
151 }
152 
155  return [](const FlutterSemanticsCustomAction* action, void* user_data) {
156  auto context = reinterpret_cast<EmbedderTestContext*>(user_data);
157  if (auto callback = context->update_semantics_custom_action_callback_) {
158  callback(action);
159  }
160  };
161 }
162 
165  return [](const FlutterLocale** supported_locales,
166  size_t length) -> const FlutterLocale* {
167  return supported_locales[0];
168  };
169 }
170 
173 }
174 
177  << "Accessed the compositor on a context where one was not setup. Use "
178  "the config builder to setup a context with a custom compositor.";
179  return *compositor_;
180 }
181 
183  const NextSceneCallback& next_scene_callback) {
184  if (compositor_) {
185  compositor_->SetNextSceneCallback(next_scene_callback);
186  return;
187  }
188  next_scene_callback_ = next_scene_callback;
189 }
190 
191 std::future<sk_sp<SkImage>> EmbedderTestContext::GetNextSceneImage() {
192  std::promise<sk_sp<SkImage>> promise;
193  auto future = promise.get_future();
195  fml::MakeCopyable([promise = std::move(promise)](auto image) mutable {
196  promise.set_value(image);
197  }));
198  return future;
199 }
200 
201 /// @note Procedure doesn't copy all closures.
203  const std::function<sk_sp<SkImage>(void)>& image_callback) {
204  if (!next_scene_callback_) {
205  return;
206  }
207  auto callback = next_scene_callback_;
208  next_scene_callback_ = nullptr;
209  callback(image_callback());
210 }
211 
212 } // namespace testing
213 } // namespace flutter
const uint8_t * vm_snapshot_instrs
Definition: elf_loader.h:28
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:29
std::function< void(const FlutterPlatformMessage *)> platform_message_callback_
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_
FlutterTransformation GetRootSurfaceTransformation()
void(* FlutterUpdateSemanticsNodeCallback)(const FlutterSemanticsNode *, void *)
Definition: embedder.h:692
EmbedderTestContext(std::string assets_path="")
static FlutterUpdateSemanticsNodeCallback GetUpdateSemanticsNodeCallbackHook()
void(* FlutterUpdateSemanticsCustomActionCallback)(const FlutterSemanticsCustomAction *, void *)
Definition: embedder.h:696
std::unique_ptr< fml::Mapping > isolate_snapshot_instructions_
const fml::Mapping * GetVMSnapshotInstructions() const
const FlutterLocale *(* FlutterComputePlatformResolvedLocaleCallback)(const FlutterLocale **, size_t)
Definition: embedder.h:962
const fml::Mapping * GetVMSnapshotData() const
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_
size_t length
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:198
static FlutterUpdateSemanticsCustomActionCallback GetUpdateSemanticsCustomActionCallbackHook()
const uint8_t * vm_isolate_instrs
Definition: elf_loader.h:30
G_BEGIN_DECLS FlMethodCall gpointer user_data
void PlatformMessageCallback(const FlutterPlatformMessage *message)
std::unique_ptr< fml::Mapping > vm_snapshot_data_
void AddNativeCallback(const char *name, Dart_NativeFunction function)
const char * name
Definition: fuchsia.cc:50
#define FML_CHECK(condition)
Definition: logging.h:68
const uint8_t * vm_snapshot_data
Definition: elf_loader.h:27
const std::string & GetAssetsPath() const
void(* VoidCallback)(void *)
Definition: embedder.h:243
FlutterEngineAOTDataSourceType type
Definition: embedder.h:1122
static FlutterComputePlatformResolvedLocaleCallback GetComputePlatformResolvedLocaleCallbackHook()
const fml::Mapping * GetIsolateSnapshotInstructions() const
std::unique_ptr< fml::Mapping > isolate_snapshot_data_
constexpr const char * kAOTAppELFFileName
Definition: elf_loader.h:17
std::future< sk_sp< SkImage > > GetNextSceneImage()
ELFAOTSymbols LoadELFSymbolFromFixturesIfNeccessary()
Attempts to resolve AOT symbols from the portable ELF loader. This location is automatically resolved...
Definition: elf_loader.cc:15
const char * elf_path
Absolute path to an ELF library file.
Definition: embedder.h:1125
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:597