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
32class ShellTest : public FixtureTest {
33 public:
34 struct Config {
35 // Required.
36 const Settings& settings;
37 // Defaults to GetTaskRunnersForFixture().
38 std::optional<TaskRunners> task_runners = {};
39 bool is_gpu_disabled = false;
40 // Defaults to calling ShellTestPlatformView::Create with the provided
41 // arguments.
42 Shell::CreateCallback<PlatformView> platform_view_create_callback;
43 };
44
45 ShellTest();
46
47 Settings CreateSettingsForFixture() override;
48 std::unique_ptr<Shell> CreateShell(
49 const Settings& settings,
50 std::optional<TaskRunners> task_runners = {});
51 std::unique_ptr<Shell> CreateShell(const Config& config);
52 void DestroyShell(std::unique_ptr<Shell> shell);
53 void DestroyShell(std::unique_ptr<Shell> shell,
54 const TaskRunners& task_runners);
55 TaskRunners GetTaskRunnersForFixture();
56
57 fml::TimePoint GetLatestFrameTargetTime(Shell* shell) const;
58
59 void SendPlatformMessage(Shell* shell,
60 std::unique_ptr<PlatformMessage> message);
61
62 void SendEnginePlatformMessage(Shell* shell,
63 std::unique_ptr<PlatformMessage> message);
64
65 static void PlatformViewNotifyCreated(
66 Shell* shell); // This creates the surface
67 static void PlatformViewNotifyDestroyed(
68 Shell* shell); // This destroys the surface
69 static void RunEngine(Shell* shell, RunConfiguration configuration);
70 static void RestartEngine(Shell* shell, RunConfiguration configuration);
71
72 /// Issue as many VSYNC as needed to flush the UI tasks so far, and reset
73 /// the `will_draw_new_frame` to true.
74 static void VSyncFlush(Shell* shell, bool& will_draw_new_frame);
75
76 /// Given the root layer, this callback builds the layer tree to be rasterized
77 /// in PumpOneFrame.
78 using LayerTreeBuilder =
79 std::function<void(std::shared_ptr<ContainerLayer> root)>;
80
81 static void SetViewportMetrics(Shell* shell, double width, double height);
82 static void NotifyIdle(Shell* shell, fml::TimeDelta deadline);
83
84 static void PumpOneFrame(Shell* shell,
85 double width = 1,
86 double height = 1,
87 LayerTreeBuilder = {});
88 static void PumpOneFrame(Shell* shell,
89 const flutter::ViewportMetrics& viewport_metrics,
90 LayerTreeBuilder = {});
91 static void DispatchFakePointerData(Shell* shell);
92 static void DispatchPointerData(Shell* shell,
93 std::unique_ptr<PointerDataPacket> packet);
94 // Declare |UnreportedTimingsCount|, |GetNeedsReportTimings| and
95 // |SetNeedsReportTimings| inside |ShellTest| mainly for easier friend class
96 // declarations as shell unit tests and Shell are in different name spaces.
97
98 static bool GetNeedsReportTimings(Shell* shell);
99 static void SetNeedsReportTimings(Shell* shell, bool value);
100
101 // Declare |StorePersistentCache| inside |ShellTest| so |PersistentCache| can
102 // friend |ShellTest| and allow us to call private |PersistentCache::store| in
103 // unit tests.
104 static void StorePersistentCache(PersistentCache* cache,
105 const SkData& key,
106 const SkData& value);
107
108 static bool IsAnimatorRunning(Shell* shell);
109
110 enum ServiceProtocolEnum {
111 kGetSkSLs,
112 kEstimateRasterCacheMemory,
113 kSetAssetBundlePath,
114 kRunInView,
115 kRenderFrameWithRasterStats,
116 };
117
118 // Helper method to test private method Shell::OnServiceProtocolGetSkSLs.
119 // (ShellTest is a friend class of Shell.) We'll also make sure that it is
120 // running on the correct task_runner.
121 static void OnServiceProtocol(
122 Shell* shell,
123 ServiceProtocolEnum some_protocol,
124 const fml::RefPtr<fml::TaskRunner>& task_runner,
125 const ServiceProtocol::Handler::ServiceProtocolMap& params,
126 rapidjson::Document* response);
127
128 std::shared_ptr<txt::FontCollection> GetFontCollection(Shell* shell);
129
130 // Do not assert |UnreportedTimingsCount| to be positive in any tests.
131 // Otherwise those tests will be flaky as the clearing of unreported timings
132 // is unpredictive.
133 static int UnreportedTimingsCount(Shell* shell);
134
135 static size_t GetLiveTrackedPathCount(
136 const std::shared_ptr<VolatilePathTracker>& tracker);
137
138 private:
139 ThreadHost thread_host_;
140
141 FML_DISALLOW_COPY_AND_ASSIGN(ShellTest);
142};
143
144} // namespace testing
145} // namespace flutter
146
147#endif // FLUTTER_SHELL_COMMON_SHELL_TEST_H_
148

source code of flutter_engine/flutter/shell/common/shell_test.h