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

Commit c241538

Browse files
committed
Refactor Shell unit-tests to allow runtime selection of client rendering API.
Adds support for Metal rasterizers in the unit-test harness.
1 parent 2c0eee4 commit c241538

22 files changed

+662
-221
lines changed

shell/common/BUILD.gn

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Use of this source code is governed by a BSD-style license that can be
33
# found in the LICENSE file.
44

5+
import("$flutter_root/shell/config.gni")
56
import("$flutter_root/shell/gpu/gpu.gni")
67
import("$flutter_root/testing/testing.gni")
78

@@ -158,14 +159,30 @@ if (current_toolchain == host_toolchain) {
158159
}
159160

160161
shell_host_executable("shell_unittests") {
162+
defines = []
163+
164+
if (shell_enable_metal) {
165+
defines += [ "SHELL_ENABLE_METAL" ]
166+
}
167+
161168
sources = [
162-
"canvas_spy_unittests.cc",
163-
"input_events_unittests.cc",
164-
"persistent_cache_unittests.cc",
165-
"pipeline_unittests.cc",
166-
"shell_test.cc",
167-
"shell_test.h",
168-
"shell_unittests.cc",
169+
"tests/canvas_spy_unittests.cc",
170+
"tests/input_events_unittests.cc",
171+
"tests/persistent_cache_unittests.cc",
172+
"tests/pipeline_unittests.cc",
173+
"tests/shell_test.cc",
174+
"tests/shell_test.h",
175+
"tests/shell_test_platform_view.cc",
176+
"tests/shell_test_platform_view.h",
177+
"tests/shell_test_surface.cc",
178+
"tests/shell_test_surface.h",
179+
"tests/shell_test_surface_gl.cc",
180+
"tests/shell_test_surface_gl.h",
181+
"tests/shell_test_surface_metal.cc",
182+
"tests/shell_test_surface_metal.h",
183+
"tests/shell_test_vsync_waiter.cc",
184+
"tests/shell_test_vsync_waiter.h",
185+
"tests/shell_unittests.cc",
169186
]
170187

171188
deps = [
@@ -177,8 +194,25 @@ if (current_toolchain == host_toolchain) {
177194
"$flutter_root/lib/ui:ui",
178195
"$flutter_root/shell",
179196
"$flutter_root/testing:dart",
197+
"$flutter_root/testing:metal",
180198
"$flutter_root/testing:opengl",
181199
]
200+
201+
libs = []
202+
203+
if (shell_enable_metal) {
204+
sources += [
205+
"tests/shell_test_surface_metal_impl.h",
206+
"tests/shell_test_surface_metal_impl.mm",
207+
]
208+
209+
deps += [ "//flutter/shell/gpu:gpu_surface_metal" ]
210+
211+
libs += [
212+
"Foundation.framework",
213+
"QuartzCore.framework",
214+
]
215+
}
182216
}
183217

184218
shell_host_executable("shell_benchmarks") {

shell/common/canvas_spy_unittests.cc renamed to shell/common/tests/canvas_spy_unittests.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
#include "canvas_spy.h"
5+
#include "flutter/shell/common/canvas_spy.h"
66
#include "gtest/gtest.h"
77
#include "third_party/skia/include/core/SkPictureRecorder.h"
88
#include "third_party/skia/include/core/SkSurface.h"

shell/common/input_events_unittests.cc renamed to shell/common/tests/input_events_unittests.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
#include "flutter/shell/common/shell_test.h"
5+
#include "flutter/shell/common/tests/shell_test.h"
66
#include "flutter/testing/testing.h"
77

88
namespace flutter {

shell/common/persistent_cache_unittests.cc renamed to shell/common/tests/persistent_cache_unittests.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include "flutter/fml/file.h"
1313
#include "flutter/fml/unique_fd.h"
1414
#include "flutter/shell/common/persistent_cache.h"
15-
#include "flutter/shell/common/shell_test.h"
1615
#include "flutter/shell/common/switches.h"
16+
#include "flutter/shell/common/tests/shell_test.h"
1717
#include "flutter/testing/testing.h"
1818
#include "include/core/SkPicture.h"
1919

File renamed without changes.

shell/common/shell_test.cc renamed to shell/common/tests/shell_test.cc

Lines changed: 34 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,10 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
#include <future>
6-
#define FML_USED_ON_EMBEDDER
5+
#include "flutter/shell/common/tests/shell_test.h"
76

8-
#include "flutter/shell/common/shell_test.h"
9-
10-
#include "flutter/flow/layers/layer_tree.h"
117
#include "flutter/flow/layers/transform_layer.h"
12-
#include "flutter/fml/make_copyable.h"
13-
#include "flutter/fml/mapping.h"
14-
#include "flutter/runtime/dart_vm.h"
15-
#include "flutter/shell/gpu/gpu_surface_gl.h"
16-
#include "flutter/testing/testing.h"
8+
#include "flutter/shell/common/tests/shell_test_platform_view.h"
179

1810
namespace flutter {
1911
namespace testing {
@@ -23,6 +15,24 @@ ShellTest::ShellTest()
2315

2416
ShellTest::~ShellTest() = default;
2517

18+
// |testing::ThreadTest|
19+
void ShellTest::SetUp() {
20+
ThreadTest::SetUp();
21+
assets_dir_ =
22+
fml::OpenDirectory(GetFixturesPath(), false, fml::FilePermission::kRead);
23+
thread_host_ = std::make_unique<ThreadHost>(
24+
"io.flutter.test." + GetCurrentTestName() + ".",
25+
ThreadHost::Type::Platform | ThreadHost::Type::IO | ThreadHost::Type::UI |
26+
ThreadHost::Type::GPU);
27+
}
28+
29+
// |testing::ThreadTest|
30+
void ShellTest::TearDown() {
31+
ThreadTest::TearDown();
32+
assets_dir_.reset();
33+
thread_host_.reset();
34+
}
35+
2636
void ShellTest::SendEnginePlatformMessage(
2737
Shell* shell,
2838
fml::RefPtr<PlatformMessage> message) {
@@ -232,6 +242,20 @@ TaskRunners ShellTest::GetTaskRunnersForFixture() {
232242
};
233243
}
234244

245+
std::unique_ptr<Shell> ShellTest::CreateShell(
246+
Settings settings,
247+
ShellTestSurface::ClientRenderingAPI api) {
248+
return Shell::Create(
249+
GetTaskRunnersForFixture(), settings,
250+
[api](Shell& shell) {
251+
return std::make_unique<ShellTestPlatformView>(
252+
shell, shell.GetTaskRunners(), api);
253+
},
254+
[](Shell& shell) {
255+
return std::make_unique<Rasterizer>(shell, shell.GetTaskRunners());
256+
});
257+
}
258+
235259
std::unique_ptr<Shell> ShellTest::CreateShell(Settings settings,
236260
bool simulate_vsync) {
237261
return CreateShell(std::move(settings), GetTaskRunnersForFixture(),
@@ -267,132 +291,10 @@ void ShellTest::DestroyShell(std::unique_ptr<Shell> shell,
267291
latch.Wait();
268292
}
269293

270-
// |testing::ThreadTest|
271-
void ShellTest::SetUp() {
272-
ThreadTest::SetUp();
273-
assets_dir_ =
274-
fml::OpenDirectory(GetFixturesPath(), false, fml::FilePermission::kRead);
275-
thread_host_ = std::make_unique<ThreadHost>(
276-
"io.flutter.test." + GetCurrentTestName() + ".",
277-
ThreadHost::Type::Platform | ThreadHost::Type::IO | ThreadHost::Type::UI |
278-
ThreadHost::Type::GPU);
279-
}
280-
281-
// |testing::ThreadTest|
282-
void ShellTest::TearDown() {
283-
ThreadTest::TearDown();
284-
assets_dir_.reset();
285-
thread_host_.reset();
286-
}
287-
288294
void ShellTest::AddNativeCallback(std::string name,
289295
Dart_NativeFunction callback) {
290296
native_resolver_->AddNativeCallback(std::move(name), callback);
291297
}
292298

293-
void ShellTestVsyncClock::SimulateVSync() {
294-
std::scoped_lock lock(mutex_);
295-
if (vsync_issued_ >= vsync_promised_.size()) {
296-
vsync_promised_.emplace_back();
297-
}
298-
FML_CHECK(vsync_issued_ < vsync_promised_.size());
299-
vsync_promised_[vsync_issued_].set_value(vsync_issued_);
300-
vsync_issued_ += 1;
301-
}
302-
303-
std::future<int> ShellTestVsyncClock::NextVSync() {
304-
std::scoped_lock lock(mutex_);
305-
vsync_promised_.emplace_back();
306-
return vsync_promised_.back().get_future();
307-
}
308-
309-
void ShellTestVsyncWaiter::AwaitVSync() {
310-
FML_DCHECK(task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread());
311-
auto vsync_future = clock_.NextVSync();
312-
313-
auto async_wait = std::async([&vsync_future, this]() {
314-
vsync_future.wait();
315-
316-
// Post the `FireCallback` to the Platform thread so earlier Platform tasks
317-
// (specifically, the `VSyncFlush` call) will be finished before
318-
// `FireCallback` is executed. This is only needed for our unit tests.
319-
//
320-
// Without this, the repeated VSYNC signals in `VSyncFlush` may start both
321-
// the current frame in the UI thread and the next frame in the secondary
322-
// callback (both of them are waiting for VSYNCs). That breaks the unit
323-
// test's assumption that each frame's VSYNC must be issued by different
324-
// `VSyncFlush` call (which resets the `will_draw_new_frame` bit).
325-
//
326-
// For example, HandlesActualIphoneXsInputEvents will fail without this.
327-
task_runners_.GetPlatformTaskRunner()->PostTask([this]() {
328-
FireCallback(fml::TimePoint::Now(), fml::TimePoint::Now());
329-
});
330-
});
331-
}
332-
333-
ShellTestPlatformView::ShellTestPlatformView(PlatformView::Delegate& delegate,
334-
TaskRunners task_runners,
335-
bool simulate_vsync)
336-
: PlatformView(delegate, std::move(task_runners)),
337-
gl_surface_(SkISize::Make(800, 600)),
338-
simulate_vsync_(simulate_vsync) {}
339-
340-
ShellTestPlatformView::~ShellTestPlatformView() = default;
341-
342-
std::unique_ptr<VsyncWaiter> ShellTestPlatformView::CreateVSyncWaiter() {
343-
return simulate_vsync_ ? std::make_unique<ShellTestVsyncWaiter>(task_runners_,
344-
vsync_clock_)
345-
: PlatformView::CreateVSyncWaiter();
346-
}
347-
348-
void ShellTestPlatformView::SimulateVSync() {
349-
vsync_clock_.SimulateVSync();
350-
}
351-
352-
// |PlatformView|
353-
std::unique_ptr<Surface> ShellTestPlatformView::CreateRenderingSurface() {
354-
return std::make_unique<GPUSurfaceGL>(this, true);
355-
}
356-
357-
// |PlatformView|
358-
PointerDataDispatcherMaker ShellTestPlatformView::GetDispatcherMaker() {
359-
return [](DefaultPointerDataDispatcher::Delegate& delegate) {
360-
return std::make_unique<SmoothPointerDataDispatcher>(delegate);
361-
};
362-
}
363-
364-
// |GPUSurfaceGLDelegate|
365-
bool ShellTestPlatformView::GLContextMakeCurrent() {
366-
return gl_surface_.MakeCurrent();
367-
}
368-
369-
// |GPUSurfaceGLDelegate|
370-
bool ShellTestPlatformView::GLContextClearCurrent() {
371-
return gl_surface_.ClearCurrent();
372-
}
373-
374-
// |GPUSurfaceGLDelegate|
375-
bool ShellTestPlatformView::GLContextPresent() {
376-
return gl_surface_.Present();
377-
}
378-
379-
// |GPUSurfaceGLDelegate|
380-
intptr_t ShellTestPlatformView::GLContextFBO() const {
381-
return gl_surface_.GetFramebuffer();
382-
}
383-
384-
// |GPUSurfaceGLDelegate|
385-
GPUSurfaceGLDelegate::GLProcResolver ShellTestPlatformView::GetGLProcResolver()
386-
const {
387-
return [surface = &gl_surface_](const char* name) -> void* {
388-
return surface->GetProcAddress(name);
389-
};
390-
}
391-
392-
// |GPUSurfaceGLDelegate|
393-
ExternalViewEmbedder* ShellTestPlatformView::GetExternalViewEmbedder() {
394-
return nullptr;
395-
}
396-
397299
} // namespace testing
398300
} // namespace flutter

0 commit comments

Comments
 (0)