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/settings.h"
13 #include "flutter/flow/layers/container_layer.h"
14 #include "flutter/fml/build_config.h"
15 #include "flutter/fml/macros.h"
16 #include "flutter/fml/time/time_point.h"
17 #include "flutter/lib/ui/window/platform_message.h"
18 #include "flutter/shell/common/persistent_cache.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/thread_host.h"
22 #include "flutter/shell/common/vsync_waiters_test.h"
23 #include "flutter/testing/elf_loader.h"
24 #include "flutter/testing/fixture_test.h"
25 #include "flutter/testing/test_dart_native_resolver.h"
26 
27 namespace flutter {
28 namespace testing {
29 
30 class ShellTest : public FixtureTest {
31  public:
32  ShellTest();
33 
35  std::unique_ptr<Shell> CreateShell(Settings settings,
36  bool simulate_vsync = false);
37  std::unique_ptr<Shell> CreateShell(
38  Settings settings,
39  TaskRunners task_runners,
40  bool simulate_vsync = false,
41  std::shared_ptr<ShellTestExternalViewEmbedder>
42  shell_test_external_view_embedder = nullptr);
43  void DestroyShell(std::unique_ptr<Shell> shell);
44  void DestroyShell(std::unique_ptr<Shell> shell, TaskRunners task_runners);
46 
48 
49  void SendEnginePlatformMessage(Shell* shell,
51 
52  static void PlatformViewNotifyCreated(
53  Shell* shell); // This creates the surface
54  static void PlatformViewNotifyDestroyed(
55  Shell* shell); // This destroys the surface
56  static void RunEngine(Shell* shell, RunConfiguration configuration);
57  static void RestartEngine(Shell* shell, RunConfiguration configuration);
58 
59  /// Issue as many VSYNC as needed to flush the UI tasks so far, and reset
60  /// the `will_draw_new_frame` to true.
61  static void VSyncFlush(Shell* shell, bool& will_draw_new_frame);
62 
63  /// Given the root layer, this callback builds the layer tree to be rasterized
64  /// in PumpOneFrame.
65  using LayerTreeBuilder =
66  std::function<void(std::shared_ptr<ContainerLayer> root)>;
67 
68  static void SetViewportMetrics(Shell* shell, double width, double height);
69  static void NotifyIdle(Shell* shell, int64_t deadline);
70 
71  static void PumpOneFrame(Shell* shell,
72  double width = 1,
73  double height = 1,
74  LayerTreeBuilder = {});
75  static void PumpOneFrame(Shell* shell,
76  flutter::ViewportMetrics viewport_metrics,
77  LayerTreeBuilder = {});
78  static void DispatchFakePointerData(Shell* shell);
79  static void DispatchPointerData(Shell* shell,
80  std::unique_ptr<PointerDataPacket> packet);
81  // Declare |UnreportedTimingsCount|, |GetNeedsReportTimings| and
82  // |SetNeedsReportTimings| inside |ShellTest| mainly for easier friend class
83  // declarations as shell unit tests and Shell are in different name spaces.
84 
85  static bool GetNeedsReportTimings(Shell* shell);
86  static void SetNeedsReportTimings(Shell* shell, bool value);
87 
88  // Declare |StorePersistentCache| inside |ShellTest| so |PersistentCache| can
89  // friend |ShellTest| and allow us to call private |PersistentCache::store| in
90  // unit tests.
91  static void StorePersistentCache(PersistentCache* cache,
92  const SkData& key,
93  const SkData& value);
94 
100  };
101 
102  // Helper method to test private method Shell::OnServiceProtocolGetSkSLs.
103  // (ShellTest is a friend class of Shell.) We'll also make sure that it is
104  // running on the correct task_runner.
105  static void OnServiceProtocol(
106  Shell* shell,
107  ServiceProtocolEnum some_protocol,
108  fml::RefPtr<fml::TaskRunner> task_runner,
110  rapidjson::Document* response);
111 
112  std::shared_ptr<txt::FontCollection> GetFontCollection(Shell* shell);
113 
114  // Do not assert |UnreportedTimingsCount| to be positive in any tests.
115  // Otherwise those tests will be flaky as the clearing of unreported timings
116  // is unpredictive.
117  static int UnreportedTimingsCount(Shell* shell);
118 
119  private:
120  ThreadHost thread_host_;
121 
122  FML_DISALLOW_COPY_AND_ASSIGN(ShellTest);
123 };
124 
125 } // namespace testing
126 } // namespace flutter
127 
128 #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:240
static void PlatformViewNotifyCreated(Shell *shell)
Definition: shell_test.cc:42
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:158
static void DispatchPointerData(Shell *shell, std::unique_ptr< PointerDataPacket > packet)
Definition: shell_test.cc:210
static void StorePersistentCache(PersistentCache *cache, const SkData &key, const SkData &value)
Definition: shell_test.cc:234
std::shared_ptr< txt::FontCollection > GetFontCollection(Shell *shell)
Definition: shell_test.cc:268
fml::TimePoint GetLatestFrameTargetTime(Shell *shell) const
Definition: shell_test.cc:302
static void SetViewportMetrics(Shell *shell, double width, double height)
Definition: shell_test.cc:110
static void PlatformViewNotifyDestroyed(Shell *shell)
Definition: shell_test.cc:52
static int UnreportedTimingsCount(Shell *shell)
Definition: shell_test.cc:222
static void DispatchFakePointerData(Shell *shell)
Definition: shell_test.cc:205
uint8_t value
std::function< void(std::shared_ptr< ContainerLayer > root)> LayerTreeBuilder
Definition: shell_test.h:66
void DestroyShell(std::unique_ptr< Shell > shell)
Definition: shell_test.cc:340
static void NotifyIdle(Shell *shell, int64_t deadline)
Definition: shell_test.cc:146
Specifies all the configuration required by the runtime library to launch the root isolate...
int32_t height
Settings CreateSettingsForFixture() override
Definition: shell_test.cc:273
The collection of all the threads used by the engine.
Definition: thread_host.h:16
void SendEnginePlatformMessage(Shell *shell, fml::RefPtr< PlatformMessage > message)
Definition: shell_test.cc:27
int32_t width
TaskRunners GetTaskRunnersForFixture()
Definition: shell_test.cc:292
static void RestartEngine(Shell *shell, RunConfiguration configuration)
Definition: shell_test.cc:76
static void SetNeedsReportTimings(Shell *shell, bool value)
Definition: shell_test.cc:226
static void VSyncFlush(Shell *shell, bool &will_draw_new_frame)
Definition: shell_test.cc:86
static void RunEngine(Shell *shell, RunConfiguration configuration)
Definition: shell_test.cc:62
static bool GetNeedsReportTimings(Shell *shell)
Definition: shell_test.cc:230
std::unique_ptr< Shell > CreateShell(Settings settings, bool simulate_vsync=false)
Definition: shell_test.cc:306