Skip to content

Commit

Permalink
[testing] Make vsync waiters pluggable in shell_unittests (flutter#14463
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
iskakaushik committed Dec 12, 2019
1 parent 181ad4e commit e0e0ac0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
33 changes: 23 additions & 10 deletions shell/common/shell_test.cc
Expand Up @@ -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"

Expand Down Expand Up @@ -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());
Expand All @@ -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|
Expand Down
8 changes: 5 additions & 3 deletions shell/common/shell_test.h
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
12 changes: 10 additions & 2 deletions shell/common/shell_unittests.cc
Expand Up @@ -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"

Expand Down Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion shell/common/test_vsync_waiters.cc
Expand Up @@ -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();
Expand Down
7 changes: 5 additions & 2 deletions shell/common/test_vsync_waiters.h
Expand Up @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit e0e0ac0

Please sign in to comment.