mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
[testing] Make vsync waiters pluggable in shell_unittests (#14463)
This makes it so that the platform views can be passed an arbitraty CreateVsyncWaiter callback that lets us inject a vsync waiter other than just the simulated monotonic vsync waiter that currently exists.
This commit is contained in:
parent
181ad4e18f
commit
e0e0ac0a68
@ -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<Shell> ShellTest::CreateShell(Settings settings,
|
||||
std::unique_ptr<Shell> ShellTest::CreateShell(Settings settings,
|
||||
TaskRunners task_runners,
|
||||
bool simulate_vsync) {
|
||||
const auto vsync_clock = std::make_shared<ShellTestVsyncClock>();
|
||||
CreateVsyncWaiter create_vsync_waiter = [&]() {
|
||||
if (simulate_vsync) {
|
||||
return static_cast<std::unique_ptr<VsyncWaiter>>(
|
||||
std::make_unique<ShellTestVsyncWaiter>(task_runners, vsync_clock));
|
||||
} else {
|
||||
return static_cast<std::unique_ptr<VsyncWaiter>>(
|
||||
std::make_unique<VsyncWaiterFallback>(task_runners));
|
||||
}
|
||||
};
|
||||
return Shell::Create(
|
||||
task_runners, settings,
|
||||
[simulate_vsync](Shell& shell) {
|
||||
[vsync_clock, &create_vsync_waiter](Shell& shell) {
|
||||
return std::make_unique<ShellTestPlatformView>(
|
||||
shell, shell.GetTaskRunners(), simulate_vsync);
|
||||
shell, shell.GetTaskRunners(), vsync_clock,
|
||||
std::move(create_vsync_waiter));
|
||||
},
|
||||
[](Shell& shell) {
|
||||
return std::make_unique<Rasterizer>(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<ShellTestVsyncClock> 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<VsyncWaiter> ShellTestPlatformView::CreateVSyncWaiter() {
|
||||
return simulate_vsync_ ? std::make_unique<ShellTestVsyncWaiter>(task_runners_,
|
||||
vsync_clock_)
|
||||
: PlatformView::CreateVSyncWaiter();
|
||||
return create_vsync_waiter_();
|
||||
}
|
||||
|
||||
void ShellTestPlatformView::SimulateVSync() {
|
||||
vsync_clock_.SimulateVSync();
|
||||
vsync_clock_->SimulateVSync();
|
||||
}
|
||||
|
||||
// |PlatformView|
|
||||
|
||||
@ -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<ShellTestVsyncClock> 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<ShellTestVsyncClock> vsync_clock_;
|
||||
|
||||
// |PlatformView|
|
||||
std::unique_ptr<Surface> CreateRenderingSurface() override;
|
||||
|
||||
@ -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<ShellTestPlatformView>(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<ShellTestVsyncClock>();
|
||||
return std::make_unique<ShellTestPlatformView>(
|
||||
shell, shell.GetTaskRunners(), vsync_clock,
|
||||
[task_runners = shell.GetTaskRunners()]() {
|
||||
return static_cast<std::unique_ptr<VsyncWaiter>>(
|
||||
std::make_unique<VsyncWaiterFallback>(task_runners));
|
||||
});
|
||||
},
|
||||
[](Shell& shell) {
|
||||
return std::make_unique<Rasterizer>(shell, shell.GetTaskRunners());
|
||||
|
||||
@ -35,7 +35,7 @@ std::future<int> 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();
|
||||
|
||||
@ -12,6 +12,8 @@
|
||||
namespace flutter {
|
||||
namespace testing {
|
||||
|
||||
using CreateVsyncWaiter = std::function<std::unique_ptr<VsyncWaiter>()>;
|
||||
|
||||
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<ShellTestVsyncClock> clock)
|
||||
: VsyncWaiter(std::move(task_runners)), clock_(clock) {}
|
||||
|
||||
protected:
|
||||
void AwaitVSync() override;
|
||||
|
||||
private:
|
||||
ShellTestVsyncClock& clock_;
|
||||
std::shared_ptr<ShellTestVsyncClock> clock_;
|
||||
};
|
||||
|
||||
} // namespace testing
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user