Flutter Engine
The Flutter Engine
shell_test.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_COMMON_SHELL_TEST_H_
6#define FLUTTER_SHELL_COMMON_SHELL_TEST_H_
7
8#include "flutter/shell/common/shell.h"
9
10#include <memory>
11
12#include "flutter/common/graphics/persistent_cache.h"
13#include "flutter/common/settings.h"
14#include "flutter/flow/layers/container_layer.h"
15#include "flutter/fml/build_config.h"
16#include "flutter/fml/macros.h"
17#include "flutter/fml/time/time_point.h"
18#include "flutter/lib/ui/volatile_path_tracker.h"
19#include "flutter/lib/ui/window/platform_message.h"
20#include "flutter/shell/common/run_configuration.h"
21#include "flutter/shell/common/shell_test_external_view_embedder.h"
22#include "flutter/shell/common/shell_test_platform_view.h"
23#include "flutter/shell/common/thread_host.h"
24#include "flutter/shell/common/vsync_waiters_test.h"
25#include "flutter/testing/elf_loader.h"
26#include "flutter/testing/fixture_test.h"
27#include "flutter/testing/test_dart_native_resolver.h"
28
29namespace flutter {
30namespace testing {
31
32// The signature of ViewContent::builder.
34 std::function<void(std::shared_ptr<ContainerLayer> root)>;
35struct ViewContent;
36// Defines the content to be rendered to all views of a frame in PumpOneFrame.
37using FrameContent = std::map<int64_t, ViewContent>;
38// Defines the content to be rendered to a view in PumpOneFrame.
41 // Given the root layer, this callback builds the layer tree to be rasterized
42 // in PumpOneFrame.
44
45 // Build a frame with no views. This is useful when PumpOneFrame is used just
46 // to schedule the frame while the frame content is defined by other means.
47 static FrameContent NoViews();
48
49 // Build a frame with a single implicit view with the specific size and no
50 // content.
51 static FrameContent DummyView(double width = 1, double height = 1);
52
53 // Build a frame with a single implicit view with the specific viewport
54 // metrics and no content.
56
57 // Build a frame with a single implicit view with the specific size and
58 // content.
59 static FrameContent ImplicitView(double width,
60 double height,
62};
63
64class ShellTest : public FixtureTest {
65 public:
66 struct Config {
67 // Required.
69 // Defaults to GetTaskRunnersForFixture().
70 std::optional<TaskRunners> task_runners = {};
71 bool is_gpu_disabled = false;
72 // Defaults to calling ShellTestPlatformView::Create with the provided
73 // arguments.
75 };
76
77 ShellTest();
78
80 std::unique_ptr<Shell> CreateShell(
81 const Settings& settings,
82 std::optional<TaskRunners> task_runners = {});
83 std::unique_ptr<Shell> CreateShell(const Config& config);
84 void DestroyShell(std::unique_ptr<Shell> shell);
85 void DestroyShell(std::unique_ptr<Shell> shell,
86 const TaskRunners& task_runners);
88
90
92 std::unique_ptr<PlatformMessage> message);
93
95 std::unique_ptr<PlatformMessage> message);
96
97 static void PlatformViewNotifyCreated(
98 Shell* shell); // This creates the surface
100 Shell* shell); // This destroys the surface
101 static void RunEngine(Shell* shell, RunConfiguration configuration);
102 static void RestartEngine(Shell* shell, RunConfiguration configuration);
103
104 /// Issue as many VSYNC as needed to flush the UI tasks so far, and reset
105 /// the content of `will_draw_new_frame` to true if it's not nullptr.
106 static void VSyncFlush(Shell* shell, bool* will_draw_new_frame = nullptr);
107
108 static void SetViewportMetrics(Shell* shell, double width, double height);
109 static void NotifyIdle(Shell* shell, fml::TimeDelta deadline);
110
111 static void PumpOneFrame(Shell* shell);
112 static void PumpOneFrame(Shell* shell, FrameContent frame_content);
113 // Dispatch a PointerHoverEvent with the specified `x` as the pointer
114 // position.
115 static void DispatchFakePointerData(Shell* shell, double x);
116 static void DispatchPointerData(Shell* shell,
117 std::unique_ptr<PointerDataPacket> packet);
118 // Declare |UnreportedTimingsCount|, |GetNeedsReportTimings| and
119 // |SetNeedsReportTimings| inside |ShellTest| mainly for easier friend class
120 // declarations as shell unit tests and Shell are in different name spaces.
121
122 static bool GetNeedsReportTimings(Shell* shell);
123 static void SetNeedsReportTimings(Shell* shell, bool value);
124
125 // Declare |StorePersistentCache| inside |ShellTest| so |PersistentCache| can
126 // friend |ShellTest| and allow us to call private |PersistentCache::store| in
127 // unit tests.
129 const SkData& key,
130 const SkData& value);
131
133
140 };
141
142 // Helper method to test private method Shell::OnServiceProtocolGetSkSLs.
143 // (ShellTest is a friend class of Shell.) We'll also make sure that it is
144 // running on the correct task_runner.
145 static void OnServiceProtocol(
146 Shell* shell,
147 ServiceProtocolEnum some_protocol,
148 const fml::RefPtr<fml::TaskRunner>& task_runner,
150 rapidjson::Document* response);
151
152 std::shared_ptr<txt::FontCollection> GetFontCollection(Shell* shell);
153
154 // Do not assert |UnreportedTimingsCount| to be positive in any tests.
155 // Otherwise those tests will be flaky as the clearing of unreported timings
156 // is unpredictive.
158
159 static size_t GetLiveTrackedPathCount(
160 const std::shared_ptr<VolatilePathTracker>& tracker);
161
162 static void TurnOffGPU(Shell* shell, bool value);
163
164 private:
165 ThreadHost thread_host_;
166
167 FML_DISALLOW_COPY_AND_ASSIGN(ShellTest);
168};
169
170} // namespace testing
171} // namespace flutter
172
173#endif // FLUTTER_SHELL_COMMON_SHELL_TEST_H_
Definition: SkData.h:25
Specifies all the configuration required by the runtime library to launch the root isolate....
std::map< std::string_view, std::string_view > ServiceProtocolMap
std::function< std::unique_ptr< T >(Shell &)> CreateCallback
Definition: shell.h:120
static void PlatformViewNotifyDestroyed(Shell *shell)
Definition: shell_test.cc:94
void SendPlatformMessage(Shell *shell, std::unique_ptr< PlatformMessage > message)
Definition: shell_test.cc:63
static void PlatformViewNotifyCreated(Shell *shell)
Definition: shell_test.cc:84
static void PumpOneFrame(Shell *shell)
Definition: shell_test.cc:211
static bool IsAnimatorRunning(Shell *shell)
static void TurnOffGPU(Shell *shell, bool value)
Definition: shell_test.cc:424
static int UnreportedTimingsCount(Shell *shell)
Definition: shell_test.cc:282
static void RunEngine(Shell *shell, RunConfiguration configuration)
Definition: shell_test.cc:104
std::shared_ptr< txt::FontCollection > GetFontCollection(Shell *shell)
Definition: shell_test.cc:331
Settings CreateSettingsForFixture() override
Definition: shell_test.cc:336
TaskRunners GetTaskRunnersForFixture()
Definition: shell_test.cc:355
static void VSyncFlush(Shell *shell, bool *will_draw_new_frame=nullptr)
Definition: shell_test.cc:128
std::unique_ptr< Shell > CreateShell(const Settings &settings, std::optional< TaskRunners > task_runners={})
Definition: shell_test.cc:369
static void DispatchPointerData(Shell *shell, std::unique_ptr< PointerDataPacket > packet)
Definition: shell_test.cc:270
static bool GetNeedsReportTimings(Shell *shell)
Definition: shell_test.cc:290
static void RestartEngine(Shell *shell, RunConfiguration configuration)
Definition: shell_test.cc:118
void SendEnginePlatformMessage(Shell *shell, std::unique_ptr< PlatformMessage > message)
Definition: shell_test.cc:68
static void NotifyIdle(Shell *shell, fml::TimeDelta deadline)
Definition: shell_test.cc:199
fml::TimePoint GetLatestFrameTargetTime(Shell *shell) const
Definition: shell_test.cc:365
static void SetNeedsReportTimings(Shell *shell, bool value)
Definition: shell_test.cc:286
static size_t GetLiveTrackedPathCount(const std::shared_ptr< VolatilePathTracker > &tracker)
Definition: shell_test.cc:415
static void StorePersistentCache(PersistentCache *cache, const SkData &key, const SkData &value)
Definition: shell_test.cc:294
static void SetViewportMetrics(Shell *shell, double width, double height)
Definition: shell_test.cc:154
void DestroyShell(std::unique_ptr< Shell > shell)
Definition: shell_test.cc:400
static void DispatchFakePointerData(Shell *shell, double x)
Definition: shell_test.cc:261
static void OnServiceProtocol(Shell *shell, ServiceProtocolEnum some_protocol, const fml::RefPtr< fml::TaskRunner > &task_runner, const ServiceProtocol::Handler::ServiceProtocolMap &params, rapidjson::Document *response)
Definition: shell_test.cc:300
const EmbeddedViewParams * params
uint8_t value
Dart_NativeFunction function
Definition: fuchsia.cc:51
Win32Message message
double x
std::function< void(std::shared_ptr< ContainerLayer > root)> LayerTreeBuilder
Definition: shell_test.h:34
std::map< int64_t, ViewContent > FrameContent
Definition: shell_test.h:37
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace Enable an endless trace buffer The default is a ring buffer This is useful when very old events need to viewed For during application launch Memory usage will continue to grow indefinitely however Start app with an specific route defined on the framework flutter assets Path to the Flutter assets directory enable service port Allow the VM service to fallback to automatic port selection if binding to a specified port fails trace Trace early application lifecycle Automatically switches to an endless trace buffer trace skia Filters out all Skia trace event categories except those that are specified in this comma separated list dump skp on shader Automatically dump the skp that triggers new shader compilations This is useful for writing custom ShaderWarmUp to reduce jank By this is not enabled to reduce the overhead purge persistent cache
Definition: switches.h:191
string root
Definition: scale_cpu.py:20
int32_t height
int32_t width
The collection of all the threads used by the engine.
Definition: thread_host.h:21
Shell::CreateCallback< PlatformView > platform_view_create_callback
Definition: shell_test.h:74
std::optional< TaskRunners > task_runners
Definition: shell_test.h:70
static FrameContent ImplicitView(double width, double height, LayerTreeBuilder builder)
Definition: shell_test.cc:47
static FrameContent DummyView(double width=1, double height=1)
Definition: shell_test.cc:29
LayerTreeBuilder builder
Definition: shell_test.h:43
flutter::ViewportMetrics viewport_metrics
Definition: shell_test.h:40
static FrameContent NoViews()
Definition: shell_test.cc:25