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/window/platform_message.h"
19 #include "flutter/shell/common/run_configuration.h"
20 #include "flutter/shell/common/shell_test_external_view_embedder.h"
21 #include "flutter/shell/common/shell_test_platform_view.h"
22 #include "flutter/shell/common/thread_host.h"
23 #include "flutter/shell/common/vsync_waiters_test.h"
24 #include "flutter/testing/elf_loader.h"
25 #include "flutter/testing/fixture_test.h"
26 #include "flutter/testing/test_dart_native_resolver.h"
27 
28 namespace flutter {
29 namespace testing {
30 
31 class ShellTest : public FixtureTest {
32  public:
33  ShellTest();
34 
36  std::unique_ptr<Shell> CreateShell(Settings settings,
37  bool simulate_vsync = false);
38  std::unique_ptr<Shell> CreateShell(
39  Settings settings,
40  TaskRunners task_runners,
41  bool simulate_vsync = false,
42  std::shared_ptr<ShellTestExternalViewEmbedder>
43  shell_test_external_view_embedder = nullptr,
44  bool is_gpu_disabled = false,
45  ShellTestPlatformView::BackendType rendering_backend =
47  Shell::CreateCallback<PlatformView> platform_view_create_callback =
48  nullptr);
49  void DestroyShell(std::unique_ptr<Shell> shell);
50  void DestroyShell(std::unique_ptr<Shell> shell, TaskRunners task_runners);
52 
54 
55  void SendEnginePlatformMessage(Shell* shell,
56  std::unique_ptr<PlatformMessage> message);
57 
58  static void PlatformViewNotifyCreated(
59  Shell* shell); // This creates the surface
60  static void PlatformViewNotifyDestroyed(
61  Shell* shell); // This destroys the surface
62  static void RunEngine(Shell* shell, RunConfiguration configuration);
63  static void RestartEngine(Shell* shell, RunConfiguration configuration);
64 
65  /// Issue as many VSYNC as needed to flush the UI tasks so far, and reset
66  /// the `will_draw_new_frame` to true.
67  static void VSyncFlush(Shell* shell, bool& will_draw_new_frame);
68 
69  /// Given the root layer, this callback builds the layer tree to be rasterized
70  /// in PumpOneFrame.
71  using LayerTreeBuilder =
72  std::function<void(std::shared_ptr<ContainerLayer> root)>;
73 
74  static void SetViewportMetrics(Shell* shell, double width, double height);
75  static void NotifyIdle(Shell* shell, int64_t deadline);
76 
77  static void PumpOneFrame(Shell* shell,
78  double width = 1,
79  double height = 1,
80  LayerTreeBuilder = {});
81  static void PumpOneFrame(Shell* shell,
82  flutter::ViewportMetrics viewport_metrics,
83  LayerTreeBuilder = {});
84  static void DispatchFakePointerData(Shell* shell);
85  static void DispatchPointerData(Shell* shell,
86  std::unique_ptr<PointerDataPacket> packet);
87  // Declare |UnreportedTimingsCount|, |GetNeedsReportTimings| and
88  // |SetNeedsReportTimings| inside |ShellTest| mainly for easier friend class
89  // declarations as shell unit tests and Shell are in different name spaces.
90 
91  static bool GetNeedsReportTimings(Shell* shell);
92  static void SetNeedsReportTimings(Shell* shell, bool value);
93 
94  // Declare |StorePersistentCache| inside |ShellTest| so |PersistentCache| can
95  // friend |ShellTest| and allow us to call private |PersistentCache::store| in
96  // unit tests.
97  static void StorePersistentCache(PersistentCache* cache,
98  const SkData& key,
99  const SkData& value);
100 
101  static bool IsAnimatorRunning(Shell* shell);
102 
108  };
109 
110  // Helper method to test private method Shell::OnServiceProtocolGetSkSLs.
111  // (ShellTest is a friend class of Shell.) We'll also make sure that it is
112  // running on the correct task_runner.
113  static void OnServiceProtocol(
114  Shell* shell,
115  ServiceProtocolEnum some_protocol,
116  fml::RefPtr<fml::TaskRunner> task_runner,
118  rapidjson::Document* response);
119 
120  std::shared_ptr<txt::FontCollection> GetFontCollection(Shell* shell);
121 
122  // Do not assert |UnreportedTimingsCount| to be positive in any tests.
123  // Otherwise those tests will be flaky as the clearing of unreported timings
124  // is unpredictive.
125  static int UnreportedTimingsCount(Shell* shell);
126 
127  private:
128  ThreadHost thread_host_;
129 
130  FML_DISALLOW_COPY_AND_ASSIGN(ShellTest);
131 };
132 
133 } // namespace testing
134 } // namespace flutter
135 
136 #endif // FLUTTER_SHELL_COMMON_SHELL_TEST_H_
static void OnServiceProtocol(Shell *shell, ServiceProtocolEnum some_protocol, fml::RefPtr< fml::TaskRunner > task_runner, const ServiceProtocol::Handler::ServiceProtocolMap &params, rapidjson::Document *response)
Definition: shell_test.cc:249
static void PlatformViewNotifyCreated(Shell *shell)
Definition: shell_test.cc:44
std::function< std::unique_ptr< T >(Shell &)> CreateCallback
Definition: shell.h:114
std::map< std::string_view, std::string_view > ServiceProtocolMap
static void PumpOneFrame(Shell *shell, double width=1, double height=1, LayerTreeBuilder={})
Definition: shell_test.cc:164
static void DispatchPointerData(Shell *shell, std::unique_ptr< PointerDataPacket > packet)
Definition: shell_test.cc:219
static void StorePersistentCache(PersistentCache *cache, const SkData &key, const SkData &value)
Definition: shell_test.cc:243
static bool IsAnimatorRunning(Shell *shell)
Definition: shell_test.cc:385
std::shared_ptr< txt::FontCollection > GetFontCollection(Shell *shell)
Definition: shell_test.cc:277
fml::TimePoint GetLatestFrameTargetTime(Shell *shell) const
Definition: shell_test.cc:311
static void SetViewportMetrics(Shell *shell, double width, double height)
Definition: shell_test.cc:112
static void PlatformViewNotifyDestroyed(Shell *shell)
Definition: shell_test.cc:54
static int UnreportedTimingsCount(Shell *shell)
Definition: shell_test.cc:231
static void DispatchFakePointerData(Shell *shell)
Definition: shell_test.cc:214
uint8_t value
std::function< void(std::shared_ptr< ContainerLayer > root)> LayerTreeBuilder
Definition: shell_test.h:72
void DestroyShell(std::unique_ptr< Shell > shell)
Definition: shell_test.cc:370
static void NotifyIdle(Shell *shell, int64_t deadline)
Definition: shell_test.cc:152
Specifies all the configuration required by the runtime library to launch the root isolate...
Settings CreateSettingsForFixture() override
Definition: shell_test.cc:282
The collection of all the threads used by the engine.
Definition: thread_host.h:16
void SendEnginePlatformMessage(Shell *shell, std::unique_ptr< PlatformMessage > message)
Definition: shell_test.cc:28
int32_t width
TaskRunners GetTaskRunnersForFixture()
Definition: shell_test.cc:301
int32_t height
static void RestartEngine(Shell *shell, RunConfiguration configuration)
Definition: shell_test.cc:78
static void SetNeedsReportTimings(Shell *shell, bool value)
Definition: shell_test.cc:235
static void VSyncFlush(Shell *shell, bool &will_draw_new_frame)
Definition: shell_test.cc:88
static void RunEngine(Shell *shell, RunConfiguration configuration)
Definition: shell_test.cc:64
static bool GetNeedsReportTimings(Shell *shell)
Definition: shell_test.cc:239
std::unique_ptr< Shell > CreateShell(Settings settings, bool simulate_vsync=false)
Definition: shell_test.cc:315