diff --git a/shell/common/shell_test.cc b/shell/common/shell_test.cc index 1cfd393f086f2..2557d8ca73bed 100644 --- a/shell/common/shell_test.cc +++ b/shell/common/shell_test.cc @@ -11,6 +11,7 @@ #include "flutter/fml/make_copyable.h" #include "flutter/fml/mapping.h" #include "flutter/runtime/dart_vm.h" +#include "flutter/shell/common/vsync_waiter_fallback.h" #include "flutter/shell/gpu/gpu_surface_gl.h" #include "flutter/testing/testing.h" @@ -258,11 +259,22 @@ std::unique_ptr ShellTest::CreateShell(Settings settings, std::unique_ptr ShellTest::CreateShell(Settings settings, TaskRunners task_runners, bool simulate_vsync) { + const auto vsync_clock = std::make_shared(); + CreateVsyncWaiter create_vsync_waiter = [&]() { + if (simulate_vsync) { + return static_cast>( + std::make_unique(task_runners, vsync_clock)); + } else { + return static_cast>( + std::make_unique(task_runners)); + } + }; return Shell::Create( task_runners, settings, - [simulate_vsync](Shell& shell) { + [vsync_clock, &create_vsync_waiter](Shell& shell) { return std::make_unique( - shell, shell.GetTaskRunners(), simulate_vsync); + shell, shell.GetTaskRunners(), vsync_clock, + std::move(create_vsync_waiter)); }, [](Shell& shell) { return std::make_unique(shell, shell.GetTaskRunners()); @@ -289,23 +301,24 @@ void ShellTest::AddNativeCallback(std::string name, native_resolver_->AddNativeCallback(std::move(name), callback); } -ShellTestPlatformView::ShellTestPlatformView(PlatformView::Delegate& delegate, - TaskRunners task_runners, - bool simulate_vsync) +ShellTestPlatformView::ShellTestPlatformView( + PlatformView::Delegate& delegate, + TaskRunners task_runners, + std::shared_ptr vsync_clock, + CreateVsyncWaiter create_vsync_waiter) : PlatformView(delegate, std::move(task_runners)), gl_surface_(SkISize::Make(800, 600)), - simulate_vsync_(simulate_vsync) {} + create_vsync_waiter_(std::move(create_vsync_waiter)), + vsync_clock_(vsync_clock) {} ShellTestPlatformView::~ShellTestPlatformView() = default; std::unique_ptr ShellTestPlatformView::CreateVSyncWaiter() { - return simulate_vsync_ ? std::make_unique(task_runners_, - vsync_clock_) - : PlatformView::CreateVSyncWaiter(); + return create_vsync_waiter_(); } void ShellTestPlatformView::SimulateVSync() { - vsync_clock_.SimulateVSync(); + vsync_clock_->SimulateVSync(); } // |PlatformView| diff --git a/shell/common/shell_test.h b/shell/common/shell_test.h index 7368eb4f2d4d2..dc32896ac7bee 100644 --- a/shell/common/shell_test.h +++ b/shell/common/shell_test.h @@ -93,7 +93,8 @@ class ShellTestPlatformView : public PlatformView, public GPUSurfaceGLDelegate { public: ShellTestPlatformView(PlatformView::Delegate& delegate, TaskRunners task_runners, - bool simulate_vsync = false); + std::shared_ptr vsync_clock, + CreateVsyncWaiter create_vsync_waiter); ~ShellTestPlatformView() override; @@ -102,8 +103,9 @@ class ShellTestPlatformView : public PlatformView, public GPUSurfaceGLDelegate { private: TestGLSurface gl_surface_; - bool simulate_vsync_ = false; - ShellTestVsyncClock vsync_clock_; + CreateVsyncWaiter create_vsync_waiter_; + + std::shared_ptr vsync_clock_; // |PlatformView| std::unique_ptr CreateRenderingSurface() override; diff --git a/shell/common/shell_unittests.cc b/shell/common/shell_unittests.cc index 3074e71aaf4df..8b7763bb896d5 100644 --- a/shell/common/shell_unittests.cc +++ b/shell/common/shell_unittests.cc @@ -24,6 +24,7 @@ #include "flutter/shell/common/shell_test.h" #include "flutter/shell/common/switches.h" #include "flutter/shell/common/thread_host.h" +#include "flutter/shell/common/vsync_waiter_fallback.h" #include "flutter/testing/testing.h" #include "third_party/tonic/converter/dart_converter.h" @@ -125,8 +126,15 @@ TEST_F(ShellTest, auto shell = Shell::Create( std::move(task_runners), settings, [](Shell& shell) { - return std::make_unique(shell, - shell.GetTaskRunners()); + // This is unused in the platform view as we are not using the simulated + // vsync mechanism. We should have better DI in the tests. + const auto vsync_clock = std::make_shared(); + return std::make_unique( + shell, shell.GetTaskRunners(), vsync_clock, + [task_runners = shell.GetTaskRunners()]() { + return static_cast>( + std::make_unique(task_runners)); + }); }, [](Shell& shell) { return std::make_unique(shell, shell.GetTaskRunners()); diff --git a/shell/common/test_vsync_waiters.cc b/shell/common/test_vsync_waiters.cc index af80d76380b94..9f5f2de3289d8 100644 --- a/shell/common/test_vsync_waiters.cc +++ b/shell/common/test_vsync_waiters.cc @@ -35,7 +35,7 @@ std::future ShellTestVsyncClock::NextVSync() { void ShellTestVsyncWaiter::AwaitVSync() { FML_DCHECK(task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread()); - auto vsync_future = clock_.NextVSync(); + auto vsync_future = clock_->NextVSync(); auto async_wait = std::async([&vsync_future, this]() { vsync_future.wait(); diff --git a/shell/common/test_vsync_waiters.h b/shell/common/test_vsync_waiters.h index f2ac486df907e..861f517f3a514 100644 --- a/shell/common/test_vsync_waiters.h +++ b/shell/common/test_vsync_waiters.h @@ -12,6 +12,8 @@ namespace flutter { namespace testing { +using CreateVsyncWaiter = std::function()>; + class ShellTestVsyncClock { public: /// Simulate that a vsync signal is triggered. @@ -28,14 +30,15 @@ class ShellTestVsyncClock { class ShellTestVsyncWaiter : public VsyncWaiter { public: - ShellTestVsyncWaiter(TaskRunners task_runners, ShellTestVsyncClock& clock) + ShellTestVsyncWaiter(TaskRunners task_runners, + std::shared_ptr clock) : VsyncWaiter(std::move(task_runners)), clock_(clock) {} protected: void AwaitVSync() override; private: - ShellTestVsyncClock& clock_; + std::shared_ptr clock_; }; } // namespace testing