22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5- #include < future>
6- #define FML_USED_ON_EMBEDDER
5+ #include " flutter/shell/common/tests/shell_test.h"
76
8- #include " flutter/shell/common/shell_test.h"
9-
10- #include " flutter/flow/layers/layer_tree.h"
117#include " flutter/flow/layers/transform_layer.h"
12- #include " flutter/fml/make_copyable.h"
13- #include " flutter/fml/mapping.h"
14- #include " flutter/runtime/dart_vm.h"
15- #include " flutter/shell/gpu/gpu_surface_gl.h"
16- #include " flutter/testing/testing.h"
8+ #include " flutter/shell/common/tests/shell_test_platform_view.h"
179
1810namespace flutter {
1911namespace testing {
@@ -23,6 +15,24 @@ ShellTest::ShellTest()
2315
2416ShellTest::~ShellTest () = default ;
2517
18+ // |testing::ThreadTest|
19+ void ShellTest::SetUp () {
20+ ThreadTest::SetUp ();
21+ assets_dir_ =
22+ fml::OpenDirectory (GetFixturesPath (), false , fml::FilePermission::kRead );
23+ thread_host_ = std::make_unique<ThreadHost>(
24+ " io.flutter.test." + GetCurrentTestName () + " ." ,
25+ ThreadHost::Type::Platform | ThreadHost::Type::IO | ThreadHost::Type::UI |
26+ ThreadHost::Type::GPU);
27+ }
28+
29+ // |testing::ThreadTest|
30+ void ShellTest::TearDown () {
31+ ThreadTest::TearDown ();
32+ assets_dir_.reset ();
33+ thread_host_.reset ();
34+ }
35+
2636void ShellTest::SendEnginePlatformMessage (
2737 Shell* shell,
2838 fml::RefPtr<PlatformMessage> message) {
@@ -232,6 +242,20 @@ TaskRunners ShellTest::GetTaskRunnersForFixture() {
232242 };
233243}
234244
245+ std::unique_ptr<Shell> ShellTest::CreateShell (
246+ Settings settings,
247+ ShellTestSurface::ClientRenderingAPI api) {
248+ return Shell::Create (
249+ GetTaskRunnersForFixture (), settings,
250+ [api](Shell& shell) {
251+ return std::make_unique<ShellTestPlatformView>(
252+ shell, shell.GetTaskRunners (), api);
253+ },
254+ [](Shell& shell) {
255+ return std::make_unique<Rasterizer>(shell, shell.GetTaskRunners ());
256+ });
257+ }
258+
235259std::unique_ptr<Shell> ShellTest::CreateShell (Settings settings,
236260 bool simulate_vsync) {
237261 return CreateShell (std::move (settings), GetTaskRunnersForFixture (),
@@ -267,132 +291,10 @@ void ShellTest::DestroyShell(std::unique_ptr<Shell> shell,
267291 latch.Wait ();
268292}
269293
270- // |testing::ThreadTest|
271- void ShellTest::SetUp () {
272- ThreadTest::SetUp ();
273- assets_dir_ =
274- fml::OpenDirectory (GetFixturesPath (), false , fml::FilePermission::kRead );
275- thread_host_ = std::make_unique<ThreadHost>(
276- " io.flutter.test." + GetCurrentTestName () + " ." ,
277- ThreadHost::Type::Platform | ThreadHost::Type::IO | ThreadHost::Type::UI |
278- ThreadHost::Type::GPU);
279- }
280-
281- // |testing::ThreadTest|
282- void ShellTest::TearDown () {
283- ThreadTest::TearDown ();
284- assets_dir_.reset ();
285- thread_host_.reset ();
286- }
287-
288294void ShellTest::AddNativeCallback (std::string name,
289295 Dart_NativeFunction callback) {
290296 native_resolver_->AddNativeCallback (std::move (name), callback);
291297}
292298
293- void ShellTestVsyncClock::SimulateVSync () {
294- std::scoped_lock lock (mutex_);
295- if (vsync_issued_ >= vsync_promised_.size ()) {
296- vsync_promised_.emplace_back ();
297- }
298- FML_CHECK (vsync_issued_ < vsync_promised_.size ());
299- vsync_promised_[vsync_issued_].set_value (vsync_issued_);
300- vsync_issued_ += 1 ;
301- }
302-
303- std::future<int > ShellTestVsyncClock::NextVSync () {
304- std::scoped_lock lock (mutex_);
305- vsync_promised_.emplace_back ();
306- return vsync_promised_.back ().get_future ();
307- }
308-
309- void ShellTestVsyncWaiter::AwaitVSync () {
310- FML_DCHECK (task_runners_.GetUITaskRunner ()->RunsTasksOnCurrentThread ());
311- auto vsync_future = clock_.NextVSync ();
312-
313- auto async_wait = std::async ([&vsync_future, this ]() {
314- vsync_future.wait ();
315-
316- // Post the `FireCallback` to the Platform thread so earlier Platform tasks
317- // (specifically, the `VSyncFlush` call) will be finished before
318- // `FireCallback` is executed. This is only needed for our unit tests.
319- //
320- // Without this, the repeated VSYNC signals in `VSyncFlush` may start both
321- // the current frame in the UI thread and the next frame in the secondary
322- // callback (both of them are waiting for VSYNCs). That breaks the unit
323- // test's assumption that each frame's VSYNC must be issued by different
324- // `VSyncFlush` call (which resets the `will_draw_new_frame` bit).
325- //
326- // For example, HandlesActualIphoneXsInputEvents will fail without this.
327- task_runners_.GetPlatformTaskRunner ()->PostTask ([this ]() {
328- FireCallback (fml::TimePoint::Now (), fml::TimePoint::Now ());
329- });
330- });
331- }
332-
333- ShellTestPlatformView::ShellTestPlatformView (PlatformView::Delegate& delegate,
334- TaskRunners task_runners,
335- bool simulate_vsync)
336- : PlatformView(delegate, std::move(task_runners)),
337- gl_surface_ (SkISize::Make(800 , 600 )),
338- simulate_vsync_(simulate_vsync) {}
339-
340- ShellTestPlatformView::~ShellTestPlatformView () = default ;
341-
342- std::unique_ptr<VsyncWaiter> ShellTestPlatformView::CreateVSyncWaiter () {
343- return simulate_vsync_ ? std::make_unique<ShellTestVsyncWaiter>(task_runners_,
344- vsync_clock_)
345- : PlatformView::CreateVSyncWaiter ();
346- }
347-
348- void ShellTestPlatformView::SimulateVSync () {
349- vsync_clock_.SimulateVSync ();
350- }
351-
352- // |PlatformView|
353- std::unique_ptr<Surface> ShellTestPlatformView::CreateRenderingSurface () {
354- return std::make_unique<GPUSurfaceGL>(this , true );
355- }
356-
357- // |PlatformView|
358- PointerDataDispatcherMaker ShellTestPlatformView::GetDispatcherMaker () {
359- return [](DefaultPointerDataDispatcher::Delegate& delegate) {
360- return std::make_unique<SmoothPointerDataDispatcher>(delegate);
361- };
362- }
363-
364- // |GPUSurfaceGLDelegate|
365- bool ShellTestPlatformView::GLContextMakeCurrent () {
366- return gl_surface_.MakeCurrent ();
367- }
368-
369- // |GPUSurfaceGLDelegate|
370- bool ShellTestPlatformView::GLContextClearCurrent () {
371- return gl_surface_.ClearCurrent ();
372- }
373-
374- // |GPUSurfaceGLDelegate|
375- bool ShellTestPlatformView::GLContextPresent () {
376- return gl_surface_.Present ();
377- }
378-
379- // |GPUSurfaceGLDelegate|
380- intptr_t ShellTestPlatformView::GLContextFBO () const {
381- return gl_surface_.GetFramebuffer ();
382- }
383-
384- // |GPUSurfaceGLDelegate|
385- GPUSurfaceGLDelegate::GLProcResolver ShellTestPlatformView::GetGLProcResolver ()
386- const {
387- return [surface = &gl_surface_](const char * name) -> void * {
388- return surface->GetProcAddress (name);
389- };
390- }
391-
392- // |GPUSurfaceGLDelegate|
393- ExternalViewEmbedder* ShellTestPlatformView::GetExternalViewEmbedder () {
394- return nullptr ;
395- }
396-
397299} // namespace testing
398300} // namespace flutter
0 commit comments