Skip to content

Commit a83a572

Browse files
committed
Eliminate ShellTestPlatformView::BackendType::kDefaultBackendType
`kDefaultBackendType` is intended to make life easier for authors of tests, but in any switch statement where it's used (currently just a single location), we rely on ordering it first and `#ifdef`ing out all backends that aren't available. Instead, we define a static function that returns the default that callers can invoke instead. This avoids relinace on case ordering and fallthrough. In flutter#56722 we split backends out into separate translation units, and remove the `#ifdef`s which means we can't rely on this trick anymore.
1 parent 03eb3db commit a83a572

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

shell/common/animator_unittests.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ TEST_F(ShellTest, VSyncTargetTime) {
8888
flutter::PlatformData(), task_runners, settings,
8989
[vsync_clock, &create_vsync_waiter](Shell& shell) {
9090
return ShellTestPlatformView::Create(
91-
shell, shell.GetTaskRunners(), vsync_clock, create_vsync_waiter,
92-
ShellTestPlatformView::BackendType::kDefaultBackend, nullptr,
91+
ShellTestPlatformView::DefaultBackendType(), shell,
92+
shell.GetTaskRunners(), vsync_clock, create_vsync_waiter, nullptr,
9393
shell.GetIsGpuDisabledSyncSwitch());
9494
},
9595
[](Shell& shell) { return std::make_unique<Rasterizer>(shell); });

shell/common/shell_test_platform_view.cc

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,44 @@ namespace flutter {
2020
namespace testing {
2121

2222
std::unique_ptr<ShellTestPlatformView> ShellTestPlatformView::Create(
23+
BackendType backend,
2324
PlatformView::Delegate& delegate,
2425
const TaskRunners& task_runners,
2526
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
2627
const CreateVsyncWaiter& create_vsync_waiter,
27-
BackendType backend,
2828
const std::shared_ptr<ShellTestExternalViewEmbedder>&
2929
shell_test_external_view_embedder,
3030
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
3131
// TODO(gw280): https://github.com/flutter/flutter/issues/50298
3232
// Make this fully runtime configurable
3333
switch (backend) {
34-
case BackendType::kDefaultBackend:
35-
#ifdef SHELL_ENABLE_GL
3634
case BackendType::kGLBackend:
35+
#ifdef SHELL_ENABLE_GL
3736
return std::make_unique<ShellTestPlatformViewGL>(
3837
delegate, task_runners, vsync_clock, create_vsync_waiter,
3938
shell_test_external_view_embedder);
39+
#else
40+
FML_LOG(FATAL) << "OpenGL not enabled in this build";
41+
return nullptr;
4042
#endif // SHELL_ENABLE_GL
41-
#ifdef SHELL_ENABLE_VULKAN
4243
case BackendType::kVulkanBackend:
44+
#ifdef SHELL_ENABLE_VULKAN
4345
return std::make_unique<ShellTestPlatformViewVulkan>(
4446
delegate, task_runners, vsync_clock, create_vsync_waiter,
4547
shell_test_external_view_embedder);
48+
#else
49+
FML_LOG(FATAL) << "Vulkan not enabled in this build";
50+
return nullptr;
4651
#endif // SHELL_ENABLE_VULKAN
47-
#ifdef SHELL_ENABLE_METAL
4852
case BackendType::kMetalBackend:
53+
#ifdef SHELL_ENABLE_METAL
4954
return std::make_unique<ShellTestPlatformViewMetal>(
5055
delegate, task_runners, vsync_clock, create_vsync_waiter,
5156
shell_test_external_view_embedder, is_gpu_disabled_sync_switch);
52-
#endif // SHELL_ENABLE_METAL
53-
54-
default:
55-
FML_LOG(FATAL) << "No backends supported for ShellTestPlatformView";
57+
#else
58+
FML_LOG(FATAL) << "Metal not enabled in this build";
5659
return nullptr;
60+
#endif // SHELL_ENABLE_METAL
5761
}
5862
}
5963

@@ -76,11 +80,11 @@ std::unique_ptr<PlatformView> ShellTestPlatformViewBuilder::operator()(
7680
}
7781
};
7882
return ShellTestPlatformView::Create(
83+
config_.rendering_backend, //
7984
shell, //
8085
task_runners, //
8186
vsync_clock, //
8287
create_vsync_waiter, //
83-
config_.rendering_backend, //
8488
config_.shell_test_external_view_embedder, //
8589
shell.GetIsGpuDisabledSyncSwitch() //
8690
);

shell/common/shell_test_platform_view.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef FLUTTER_SHELL_COMMON_SHELL_TEST_PLATFORM_VIEW_H_
66
#define FLUTTER_SHELL_COMMON_SHELL_TEST_PLATFORM_VIEW_H_
77

8+
#include <exception>
9+
810
#include "flutter/shell/common/platform_view.h"
911
#include "flutter/shell/common/shell_test_external_view_embedder.h"
1012
#include "flutter/shell/common/vsync_waiters_test.h"
@@ -15,18 +17,30 @@ namespace testing {
1517
class ShellTestPlatformView : public PlatformView {
1618
public:
1719
enum class BackendType {
18-
kDefaultBackend = 0,
1920
kGLBackend,
2021
kVulkanBackend,
2122
kMetalBackend,
2223
};
2324

25+
static BackendType DefaultBackendType() {
26+
#if defined(SHELL_ENABLE_GL)
27+
return BackendType::kGLBackend;
28+
#elif defined(SHELL_ENABLE_METAL)
29+
return BackendType::kMetalBackend;
30+
#elif defined(SHELL_ENABLE_VULKAN)
31+
return BackendType::kVulkanBackend;
32+
#else
33+
FML_LOG(FATAL) << "No backend is enabled in this build.";
34+
std::terminate();
35+
#endif
36+
}
37+
2438
static std::unique_ptr<ShellTestPlatformView> Create(
39+
BackendType backend,
2540
PlatformView::Delegate& delegate,
2641
const TaskRunners& task_runners,
2742
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
2843
const CreateVsyncWaiter& create_vsync_waiter,
29-
BackendType backend,
3044
const std::shared_ptr<ShellTestExternalViewEmbedder>&
3145
shell_test_external_view_embedder,
3246
const std::shared_ptr<const fml::SyncSwitch>&
@@ -50,7 +64,7 @@ class ShellTestPlatformViewBuilder {
5064
std::shared_ptr<ShellTestExternalViewEmbedder>
5165
shell_test_external_view_embedder = nullptr;
5266
ShellTestPlatformView::BackendType rendering_backend =
53-
ShellTestPlatformView::BackendType::kDefaultBackend;
67+
ShellTestPlatformView::DefaultBackendType();
5468
};
5569

5670
explicit ShellTestPlatformViewBuilder(Config config);

shell/common/shell_unittests.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,13 +500,13 @@ TEST_F(ShellTest,
500500
// vsync mechanism. We should have better DI in the tests.
501501
const auto vsync_clock = std::make_shared<ShellTestVsyncClock>();
502502
return ShellTestPlatformView::Create(
503-
shell, shell.GetTaskRunners(), vsync_clock,
503+
ShellTestPlatformView::DefaultBackendType(), shell,
504+
shell.GetTaskRunners(), vsync_clock,
504505
[task_runners = shell.GetTaskRunners()]() {
505506
return static_cast<std::unique_ptr<VsyncWaiter>>(
506507
std::make_unique<VsyncWaiterFallback>(task_runners));
507508
},
508-
ShellTestPlatformView::BackendType::kDefaultBackend, nullptr,
509-
shell.GetIsGpuDisabledSyncSwitch());
509+
nullptr, shell.GetIsGpuDisabledSyncSwitch());
510510
},
511511
[](Shell& shell) { return std::make_unique<Rasterizer>(shell); });
512512
ASSERT_TRUE(ValidateShell(shell.get()));

0 commit comments

Comments
 (0)