| Flutter Linux Embedder |

fl_touch_manager_test.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "flutter/shell/platform/linux/fl_touch_manager.h"
6 #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
7 #include "flutter/shell/platform/linux/fl_engine_private.h"
8
9 #include <cstring>
10 #include <vector>
11
12 #include "gtest/gtest.h"
13
14 struct _FakeGdkDevice {
15 GObject parent_instance;
16 gchar* name;
17 GdkInputSource source;
18 };
19 static GdkDevice* makeFakeDevice(GdkInputSource source) {
20 _FakeGdkDevice* device =
21 static_cast<_FakeGdkDevice*>(g_malloc0(sizeof(_FakeGdkDevice)));
22 device->source = source;
23 // Bully the type checker
24 (reinterpret_cast<GTypeInstance*>(device))->g_class =
25 static_cast<GTypeClass*>(g_malloc0(sizeof(GTypeClass)));
26 (reinterpret_cast<GTypeInstance*>(device))->g_class->g_type = GDK_TYPE_DEVICE;
27 return reinterpret_cast<GdkDevice*>(device);
28 }
29
30 TEST(FlTouchManagerTest, TouchEvents) {
31 g_autoptr(FlDartProject) project = fl_dart_project_new();
32 g_autoptr(FlEngine) engine = fl_engine_new(project);
33
34 g_autoptr(GError) error = nullptr;
35 EXPECT_TRUE(fl_engine_start(engine, &error));
36 EXPECT_EQ(error, nullptr);
37
38 std::vector<FlutterPointerEvent> pointer_events;
39 fl_engine_get_embedder_api(engine)->SendPointerEvent = MOCK_ENGINE_PROC(
40 SendPointerEvent,
41 ([&pointer_events](auto engine, const FlutterPointerEvent* events,
42 size_t events_count) {
43 for (size_t i = 0; i < events_count; i++) {
44 pointer_events.push_back(events[i]);
45 }
46
47 return kSuccess;
48 }));
49
50 g_autoptr(FlTouchManager) manager = fl_touch_manager_new(engine, 0);
51
52 GdkDevice* touchscreen = makeFakeDevice(GDK_SOURCE_TOUCHSCREEN);
53 GdkEventTouch* event =
54 reinterpret_cast<GdkEventTouch*>(gdk_event_new(GDK_TOUCH_BEGIN));
55 event->time = 1;
56 event->x = 4.0;
57 event->y = 8.0;
58 event->device = touchscreen;
59 fl_touch_manager_handle_touch_event(manager, event, 1.0);
60 EXPECT_EQ(pointer_events.size(), 2u);
61 EXPECT_EQ(pointer_events[0].x, 4.0);
62 EXPECT_EQ(pointer_events[0].y, 8.0);
63 EXPECT_EQ(pointer_events[0].device_kind, kFlutterPointerDeviceKindTouch);
64 EXPECT_EQ(pointer_events[0].timestamp,
65 1000lu); // Milliseconds -> Microseconds
66 EXPECT_EQ(pointer_events[0].phase, kAdd);
67
68 EXPECT_EQ(pointer_events[1].x, 4.0);
69 EXPECT_EQ(pointer_events[1].y, 8.0);
70 EXPECT_EQ(pointer_events[1].device_kind, kFlutterPointerDeviceKindTouch);
71 EXPECT_EQ(pointer_events[1].timestamp,
72 1000lu); // Milliseconds -> Microseconds
73 EXPECT_EQ(pointer_events[1].phase, kDown);
74
75 event->type = GDK_TOUCH_UPDATE;
76 fl_touch_manager_handle_touch_event(manager, event, 1.0);
77 EXPECT_EQ(pointer_events.size(), 3u);
78 EXPECT_EQ(pointer_events[2].x, 4.0);
79 EXPECT_EQ(pointer_events[2].y, 8.0);
80 EXPECT_EQ(pointer_events[2].device_kind, kFlutterPointerDeviceKindTouch);
81 EXPECT_EQ(pointer_events[2].timestamp,
82 1000lu); // Milliseconds -> Microseconds
83 EXPECT_EQ(pointer_events[2].phase, kMove);
84
85 event->type = GDK_TOUCH_END;
86 fl_touch_manager_handle_touch_event(manager, event, 1.0);
87 EXPECT_EQ(pointer_events.size(), 5u);
88 EXPECT_EQ(pointer_events[3].x, 4.0);
89 EXPECT_EQ(pointer_events[3].y, 8.0);
90 EXPECT_EQ(pointer_events[3].device_kind, kFlutterPointerDeviceKindTouch);
91 EXPECT_EQ(pointer_events[3].timestamp,
92 1000lu); // Milliseconds -> Microseconds
93 EXPECT_EQ(pointer_events[3].phase, kUp);
94
95 EXPECT_EQ(pointer_events[4].x, 4.0);
96 EXPECT_EQ(pointer_events[4].y, 8.0);
97 EXPECT_EQ(pointer_events[4].device_kind, kFlutterPointerDeviceKindTouch);
98 EXPECT_EQ(pointer_events[4].timestamp,
99 1000lu); // Milliseconds -> Microseconds
100 EXPECT_EQ(pointer_events[4].phase, kRemove);
101 }
fl_dart_project_new
G_MODULE_EXPORT FlDartProject * fl_dart_project_new()
Definition: fl_dart_project.cc:50
fl_engine_get_embedder_api
FlutterEngineProcTable * fl_engine_get_embedder_api(FlEngine *self)
Definition: fl_engine.cc:661
fl_engine_start
gboolean fl_engine_start(FlEngine *self, GError **error)
Definition: fl_engine.cc:544
fl_engine_new
G_MODULE_EXPORT FlEngine * fl_engine_new(FlDartProject *project)
Definition: fl_engine.cc:524
fl_engine_private.h
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
i
int i
Definition: fl_socket_accessible.cc:18
fl_touch_manager_handle_touch_event
void fl_touch_manager_handle_touch_event(FlTouchManager *self, GdkEventTouch *touch_event, gint scale_factor)
Definition: fl_touch_manager.cc:131
fl_touch_manager_new
FlTouchManager * fl_touch_manager_new(FlEngine *engine, FlutterViewId view_id)
Definition: fl_touch_manager.cc:48
fl_touch_manager.h
makeFakeDevice
static GdkDevice * makeFakeDevice(GdkInputSource source)
Definition: fl_touch_manager_test.cc:19
TEST
TEST(FlTouchManagerTest, TouchEvents)
Definition: fl_touch_manager_test.cc:30
_FakeGdkDevice
Definition: fl_scrolling_manager_test.cc:15
_FakeGdkDevice::name
gchar * name
Definition: fl_scrolling_manager_test.cc:17
_FakeGdkDevice::source
GdkInputSource source
Definition: fl_scrolling_manager_test.cc:18
_FakeGdkDevice::parent_instance
GObject parent_instance
Definition: fl_scrolling_manager_test.cc:16