Flutter Engine
embedder_unittests_util.h
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 #ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_UNITTESTS_UTIL_H_
6 #define FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_UNITTESTS_UTIL_H_
7 
8 #define FML_USED_ON_EMBEDDER
9 
10 #include <future>
11 
12 #include "embedder.h"
13 #include "flutter/fml/mapping.h"
14 #include "flutter/fml/message_loop.h"
15 #include "flutter/fml/paths.h"
16 #include "flutter/shell/platform/embedder/tests/embedder_assertions.h"
17 #include "flutter/shell/platform/embedder/tests/embedder_config_builder.h"
18 #include "flutter/shell/platform/embedder/tests/embedder_test.h"
19 #include "third_party/skia/include/core/SkSurface.h"
20 
21 namespace flutter {
22 namespace testing {
23 
24 sk_sp<SkSurface> CreateRenderSurface(const FlutterLayer& layer,
25  GrDirectContext* context);
26 
27 bool RasterImagesAreSame(sk_sp<SkImage> a, sk_sp<SkImage> b);
28 
29 bool WriteImageToDisk(const fml::UniqueFD& directory,
30  const std::string& name,
31  sk_sp<SkImage> image);
32 
33 bool ImageMatchesFixture(const std::string& fixture_file_name,
34  sk_sp<SkImage> scene_image);
35 
36 bool ImageMatchesFixture(const std::string& fixture_file_name,
37  std::future<sk_sp<SkImage>>& scene_image);
38 
40  const FlutterPlatformViewMutation** mutations,
41  size_t count,
43  std::function<void(const FlutterPlatformViewMutation& mutation)> handler);
44 
46  const FlutterPlatformView* view,
48  std::function<void(const FlutterPlatformViewMutation& mutation)> handler);
49 
51  const FlutterPlatformViewMutation** mutations,
52  size_t count);
53 
55 
56 //------------------------------------------------------------------------------
57 /// @brief A task runner that we expect the embedder to provide but whose
58 /// implementation is a real FML task runner.
59 ///
61  public:
62  using TaskExpiryCallback = std::function<void(FlutterTask)>;
64  TaskExpiryCallback on_task_expired)
65  : identifier_(++sEmbedderTaskRunnerIdentifiers),
66  real_task_runner_(real_task_runner),
67  on_task_expired_(on_task_expired) {
68  FML_CHECK(real_task_runner_);
69  FML_CHECK(on_task_expired_);
70 
71  task_runner_description_.struct_size = sizeof(FlutterTaskRunnerDescription);
72  task_runner_description_.user_data = this;
73  task_runner_description_.runs_task_on_current_thread_callback =
74  [](void* user_data) -> bool {
75  return reinterpret_cast<EmbedderTestTaskRunner*>(user_data)
76  ->real_task_runner_->RunsTasksOnCurrentThread();
77  };
78  task_runner_description_.post_task_callback = [](FlutterTask task,
79  uint64_t target_time_nanos,
80  void* user_data) -> void {
81  auto thiz = reinterpret_cast<EmbedderTestTaskRunner*>(user_data);
82 
83  auto target_time = fml::TimePoint::FromEpochDelta(
84  fml::TimeDelta::FromNanoseconds(target_time_nanos));
85  auto on_task_expired = thiz->on_task_expired_;
86  auto invoke_task = [task, on_task_expired]() { on_task_expired(task); };
87  auto real_task_runner = thiz->real_task_runner_;
88 
89  real_task_runner->PostTaskForTime(invoke_task, target_time);
90  };
91  task_runner_description_.identifier = identifier_;
92  }
93 
95  return task_runner_description_;
96  }
97 
98  private:
99  static std::atomic_size_t sEmbedderTaskRunnerIdentifiers;
100  const size_t identifier_;
101  fml::RefPtr<fml::TaskRunner> real_task_runner_;
102  TaskExpiryCallback on_task_expired_;
103  FlutterTaskRunnerDescription task_runner_description_ = {};
104 
105  FML_DISALLOW_COPY_AND_ASSIGN(EmbedderTestTaskRunner);
106 };
107 
108 } // namespace testing
109 } // namespace flutter
110 
111 #endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_UNITTESTS_UTIL_H_
SkMatrix GetTotalMutationTransformationMatrix(const FlutterPlatformViewMutation **mutations, size_t count)
FlutterPlatformViewMutationType
Definition: embedder.h:786
void FilterMutationsByType(const FlutterPlatformViewMutation **mutations, size_t count, FlutterPlatformViewMutationType type, std::function< void(const FlutterPlatformViewMutation &mutation)> handler)
static constexpr TimeDelta FromNanoseconds(int64_t nanos)
Definition: time_delta.h:40
size_t struct_size
The size of this struct. Must be sizeof(FlutterTaskRunnerDescription).
Definition: embedder.h:718
sk_sp< SkSurface > CreateRenderSurface(const FlutterLayer &layer, GrDirectContext *context)
Dart_NativeFunction function
Definition: fuchsia.cc:51
A task runner that we expect the embedder to provide but whose implementation is a real FML task runn...
std::function< void(FlutterTask)> TaskExpiryCallback
static constexpr TimePoint FromEpochDelta(TimeDelta ticks)
Definition: time_point.h:36
virtual void PostTaskForTime(const fml::closure &task, fml::TimePoint target_time)
Definition: task_runner.cc:28
bool RasterImagesAreSame(sk_sp< SkImage > a, sk_sp< SkImage > b)
G_BEGIN_DECLS FlMethodCall gpointer user_data
const FlutterTaskRunnerDescription & GetFlutterTaskRunnerDescription()
const char * name
Definition: fuchsia.cc:50
#define FML_CHECK(condition)
Definition: logging.h:68
FlutterTaskRunnerPostTaskCallback post_task_callback
Definition: embedder.h:735
virtual bool RunsTasksOnCurrentThread()
Definition: task_runner.cc:43
bool WriteImageToDisk(const fml::UniqueFD &directory, const std::string &name, sk_sp< SkImage > image)
bool ImageMatchesFixture(const std::string &fixture_file_name, sk_sp< SkImage > scene_image)
BoolCallback runs_task_on_current_thread_callback
Definition: embedder.h:724
EmbedderTestTaskRunner(fml::RefPtr< fml::TaskRunner > real_task_runner, TaskExpiryCallback on_task_expired)