From 6e9b863e6d29ecd1919569f3ff1e5064022225e4 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Fri, 24 Jan 2025 03:48:01 +1300 Subject: [PATCH] Replace hacky code creating fake devices (#162056) --- .../linux/fl_scrolling_manager_test.cc | 30 +++++++------------ .../platform/linux/fl_touch_manager_test.cc | 21 +++---------- 2 files changed, 14 insertions(+), 37 deletions(-) diff --git a/engine/src/flutter/shell/platform/linux/fl_scrolling_manager_test.cc b/engine/src/flutter/shell/platform/linux/fl_scrolling_manager_test.cc index 047df48cd9..0f68a4432b 100644 --- a/engine/src/flutter/shell/platform/linux/fl_scrolling_manager_test.cc +++ b/engine/src/flutter/shell/platform/linux/fl_scrolling_manager_test.cc @@ -6,28 +6,12 @@ #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h" #include "flutter/shell/platform/linux/fl_engine_private.h" +#include #include #include #include "gtest/gtest.h" -// Disgusting hack but could not find any way to create a GdkDevice -struct _FakeGdkDevice { - GObject parent_instance; - gchar* name; - GdkInputSource source; -}; -GdkDevice* makeFakeDevice(GdkInputSource source) { - _FakeGdkDevice* device = - static_cast<_FakeGdkDevice*>(g_malloc0(sizeof(_FakeGdkDevice))); - device->source = source; - // Bully the type checker - (reinterpret_cast(device))->g_class = - static_cast(g_malloc0(sizeof(GTypeClass))); - (reinterpret_cast(device))->g_class->g_type = GDK_TYPE_DEVICE; - return reinterpret_cast(device); -} - TEST(FlScrollingManagerTest, DiscreteDirectional) { g_autoptr(FlDartProject) project = fl_dart_project_new(); g_autoptr(FlEngine) engine = fl_engine_new(project); @@ -50,7 +34,9 @@ TEST(FlScrollingManagerTest, DiscreteDirectional) { g_autoptr(FlScrollingManager) manager = fl_scrolling_manager_new(engine, 0); - GdkDevice* mouse = makeFakeDevice(GDK_SOURCE_MOUSE); + GdkDevice* mouse = + GDK_DEVICE(g_object_new(gdk_wayland_device_get_type(), "input-source", + GDK_SOURCE_MOUSE, nullptr)); GdkEventScroll* event = reinterpret_cast(gdk_event_new(GDK_SCROLL)); event->time = 1; @@ -121,7 +107,9 @@ TEST(FlScrollingManagerTest, DiscreteScrolling) { g_autoptr(FlScrollingManager) manager = fl_scrolling_manager_new(engine, 0); - GdkDevice* mouse = makeFakeDevice(GDK_SOURCE_MOUSE); + GdkDevice* mouse = + GDK_DEVICE(g_object_new(gdk_wayland_device_get_type(), "input-source", + GDK_SOURCE_MOUSE, nullptr)); GdkEventScroll* event = reinterpret_cast(gdk_event_new(GDK_SCROLL)); event->time = 1; @@ -164,7 +152,9 @@ TEST(FlScrollingManagerTest, Panning) { g_autoptr(FlScrollingManager) manager = fl_scrolling_manager_new(engine, 0); - GdkDevice* touchpad = makeFakeDevice(GDK_SOURCE_TOUCHPAD); + GdkDevice* touchpad = + GDK_DEVICE(g_object_new(gdk_wayland_device_get_type(), "input-source", + GDK_SOURCE_TOUCHPAD, nullptr)); GdkEventScroll* event = reinterpret_cast(gdk_event_new(GDK_SCROLL)); event->time = 1; diff --git a/engine/src/flutter/shell/platform/linux/fl_touch_manager_test.cc b/engine/src/flutter/shell/platform/linux/fl_touch_manager_test.cc index 1b08bfbc22..647ce20415 100644 --- a/engine/src/flutter/shell/platform/linux/fl_touch_manager_test.cc +++ b/engine/src/flutter/shell/platform/linux/fl_touch_manager_test.cc @@ -6,27 +6,12 @@ #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h" #include "flutter/shell/platform/linux/fl_engine_private.h" +#include #include #include #include "gtest/gtest.h" -struct _FakeGdkDevice { - GObject parent_instance; - gchar* name; - GdkInputSource source; -}; -static GdkDevice* makeFakeDevice(GdkInputSource source) { - _FakeGdkDevice* device = - static_cast<_FakeGdkDevice*>(g_malloc0(sizeof(_FakeGdkDevice))); - device->source = source; - // Bully the type checker - (reinterpret_cast(device))->g_class = - static_cast(g_malloc0(sizeof(GTypeClass))); - (reinterpret_cast(device))->g_class->g_type = GDK_TYPE_DEVICE; - return reinterpret_cast(device); -} - TEST(FlTouchManagerTest, TouchEvents) { g_autoptr(FlDartProject) project = fl_dart_project_new(); g_autoptr(FlEngine) engine = fl_engine_new(project); @@ -49,7 +34,9 @@ TEST(FlTouchManagerTest, TouchEvents) { g_autoptr(FlTouchManager) manager = fl_touch_manager_new(engine, 0); - GdkDevice* touchscreen = makeFakeDevice(GDK_SOURCE_TOUCHSCREEN); + GdkDevice* touchscreen = + GDK_DEVICE(g_object_new(gdk_wayland_device_get_type(), "input-source", + GDK_SOURCE_TOUCHSCREEN, nullptr)); GdkEventTouch* event = reinterpret_cast(gdk_event_new(GDK_TOUCH_BEGIN)); event->time = 1;