Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions shell/common/shell_test.cc
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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