Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit e0e0ac0

Browse files
authored
[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.
1 parent 181ad4e commit e0e0ac0

File tree

5 files changed

+44
-18
lines changed

5 files changed

+44
-18
lines changed

shell/common/shell_test.cc

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "flutter/fml/make_copyable.h"
1212
#include "flutter/fml/mapping.h"
1313
#include "flutter/runtime/dart_vm.h"
14+
#include "flutter/shell/common/vsync_waiter_fallback.h"
1415
#include "flutter/shell/gpu/gpu_surface_gl.h"
1516
#include "flutter/testing/testing.h"
1617

@@ -258,11 +259,22 @@ std::unique_ptr<Shell> ShellTest::CreateShell(Settings settings,
258259
std::unique_ptr<Shell> ShellTest::CreateShell(Settings settings,
259260
TaskRunners task_runners,
260261
bool simulate_vsync) {
262+
const auto vsync_clock = std::make_shared<ShellTestVsyncClock>();
263+
CreateVsyncWaiter create_vsync_waiter = [&]() {
264+
if (simulate_vsync) {
265+
return static_cast<std::unique_ptr<VsyncWaiter>>(
266+
std::make_unique<ShellTestVsyncWaiter>(task_runners, vsync_clock));
267+
} else {
268+
return static_cast<std::unique_ptr<VsyncWaiter>>(
269+
std::make_unique<VsyncWaiterFallback>(task_runners));
270+
}
271+
};
261272
return Shell::Create(
262273
task_runners, settings,
263-
[simulate_vsync](Shell& shell) {
274+
[vsync_clock, &create_vsync_waiter](Shell& shell) {
264275
return std::make_unique<ShellTestPlatformView>(
265-
shell, shell.GetTaskRunners(), simulate_vsync);
276+
shell, shell.GetTaskRunners(), vsync_clock,
277+
std::move(create_vsync_waiter));
266278
},
267279
[](Shell& shell) {
268280
return std::make_unique<Rasterizer>(shell, shell.GetTaskRunners());
@@ -289,23 +301,24 @@ void ShellTest::AddNativeCallback(std::string name,
289301
native_resolver_->AddNativeCallback(std::move(name), callback);
290302
}
291303

292-
ShellTestPlatformView::ShellTestPlatformView(PlatformView::Delegate& delegate,
293-
TaskRunners task_runners,
294-
bool simulate_vsync)
304+
ShellTestPlatformView::ShellTestPlatformView(
305+
PlatformView::Delegate& delegate,
306+
TaskRunners task_runners,
307+
std::shared_ptr<ShellTestVsyncClock> vsync_clock,
308+
CreateVsyncWaiter create_vsync_waiter)
295309
: PlatformView(delegate, std::move(task_runners)),
296310
gl_surface_(SkISize::Make(800, 600)),
297-
simulate_vsync_(simulate_vsync) {}
311+
create_vsync_waiter_(std::move(create_vsync_waiter)),
312+
vsync_clock_(vsync_clock) {}
298313

299314
ShellTestPlatformView::~ShellTestPlatformView() = default;
300315

301316
std::unique_ptr<VsyncWaiter> ShellTestPlatformView::CreateVSyncWaiter() {
302-
return simulate_vsync_ ? std::make_unique<ShellTestVsyncWaiter>(task_runners_,
303-
vsync_clock_)
304-
: PlatformView::CreateVSyncWaiter();
317+
return create_vsync_waiter_();
305318
}
306319

307320
void ShellTestPlatformView::SimulateVSync() {
308-
vsync_clock_.SimulateVSync();
321+
vsync_clock_->SimulateVSync();
309322
}
310323

311324
// |PlatformView|

shell/common/shell_test.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ class ShellTestPlatformView : public PlatformView, public GPUSurfaceGLDelegate {
9393
public:
9494
ShellTestPlatformView(PlatformView::Delegate& delegate,
9595
TaskRunners task_runners,
96-
bool simulate_vsync = false);
96+
std::shared_ptr<ShellTestVsyncClock> vsync_clock,
97+
CreateVsyncWaiter create_vsync_waiter);
9798

9899
~ShellTestPlatformView() override;
99100

@@ -102,8 +103,9 @@ class ShellTestPlatformView : public PlatformView, public GPUSurfaceGLDelegate {
102103
private:
103104
TestGLSurface gl_surface_;
104105

105-
bool simulate_vsync_ = false;
106-
ShellTestVsyncClock vsync_clock_;
106+
CreateVsyncWaiter create_vsync_waiter_;
107+
108+
std::shared_ptr<ShellTestVsyncClock> vsync_clock_;
107109

108110
// |PlatformView|
109111
std::unique_ptr<Surface> CreateRenderingSurface() override;

shell/common/shell_unittests.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "flutter/shell/common/shell_test.h"
2525
#include "flutter/shell/common/switches.h"
2626
#include "flutter/shell/common/thread_host.h"
27+
#include "flutter/shell/common/vsync_waiter_fallback.h"
2728
#include "flutter/testing/testing.h"
2829
#include "third_party/tonic/converter/dart_converter.h"
2930

@@ -125,8 +126,15 @@ TEST_F(ShellTest,
125126
auto shell = Shell::Create(
126127
std::move(task_runners), settings,
127128
[](Shell& shell) {
128-
return std::make_unique<ShellTestPlatformView>(shell,
129-
shell.GetTaskRunners());
129+
// This is unused in the platform view as we are not using the simulated
130+
// vsync mechanism. We should have better DI in the tests.
131+
const auto vsync_clock = std::make_shared<ShellTestVsyncClock>();
132+
return std::make_unique<ShellTestPlatformView>(
133+
shell, shell.GetTaskRunners(), vsync_clock,
134+
[task_runners = shell.GetTaskRunners()]() {
135+
return static_cast<std::unique_ptr<VsyncWaiter>>(
136+
std::make_unique<VsyncWaiterFallback>(task_runners));
137+
});
130138
},
131139
[](Shell& shell) {
132140
return std::make_unique<Rasterizer>(shell, shell.GetTaskRunners());

shell/common/test_vsync_waiters.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ std::future<int> ShellTestVsyncClock::NextVSync() {
3535

3636
void ShellTestVsyncWaiter::AwaitVSync() {
3737
FML_DCHECK(task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread());
38-
auto vsync_future = clock_.NextVSync();
38+
auto vsync_future = clock_->NextVSync();
3939

4040
auto async_wait = std::async([&vsync_future, this]() {
4141
vsync_future.wait();

shell/common/test_vsync_waiters.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace flutter {
1313
namespace testing {
1414

15+
using CreateVsyncWaiter = std::function<std::unique_ptr<VsyncWaiter>()>;
16+
1517
class ShellTestVsyncClock {
1618
public:
1719
/// Simulate that a vsync signal is triggered.
@@ -28,14 +30,15 @@ class ShellTestVsyncClock {
2830

2931
class ShellTestVsyncWaiter : public VsyncWaiter {
3032
public:
31-
ShellTestVsyncWaiter(TaskRunners task_runners, ShellTestVsyncClock& clock)
33+
ShellTestVsyncWaiter(TaskRunners task_runners,
34+
std::shared_ptr<ShellTestVsyncClock> clock)
3235
: VsyncWaiter(std::move(task_runners)), clock_(clock) {}
3336

3437
protected:
3538
void AwaitVSync() override;
3639

3740
private:
38-
ShellTestVsyncClock& clock_;
41+
std::shared_ptr<ShellTestVsyncClock> clock_;
3942
};
4043

4144
} // namespace testing

0 commit comments

Comments
 (0)