Flutter Engine
fl_key_event_plugin_test.cc File Reference
#include "flutter/shell/platform/linux/fl_key_event_plugin.h"
#include <iostream>
#include "gtest/gtest.h"
#include "flutter/shell/platform/linux/fl_binary_messenger_private.h"
#include "flutter/shell/platform/linux/fl_engine_private.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_message_codec.h"
#include "flutter/shell/platform/linux/testing/mock_renderer.h"

Go to the source code of this file.

Functions

static FlEngine * make_mock_engine ()
 
static void echo_response_cb (GObject *object, GAsyncResult *result, gpointer user_data)
 
 TEST (FlKeyEventPluginTest, SendKeyEvent)
 
void test_lock_event (guint key_code, const char *down_expected, const char *up_expected)
 
 TEST (FlKeyEventPluginTest, SendNumLockKeyEvent)
 
 TEST (FlKeyEventPluginTest, SendCapsLockKeyEvent)
 
 TEST (FlKeyEventPluginTest, SendShiftLockKeyEvent)
 

Variables

const char * expected_value = nullptr
 

Function Documentation

◆ echo_response_cb()

static void echo_response_cb ( GObject *  object,
GAsyncResult *  result,
gpointer  user_data 
)
static

Definition at line 31 of file fl_key_event_plugin_test.cc.

References error, expected_value, fl_basic_message_channel_send_finish(), fl_value_get_type(), fl_value_to_string(), and FL_VALUE_TYPE_MAP.

Referenced by TEST(), and test_lock_event().

33  {
34  g_autoptr(GError) error = nullptr;
35  g_autoptr(FlValue) message = fl_basic_message_channel_send_finish(
36  FL_BASIC_MESSAGE_CHANNEL(object), result, &error);
37  EXPECT_NE(message, nullptr);
38  EXPECT_EQ(error, nullptr);
39 
40  EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_MAP);
41  EXPECT_STREQ(fl_value_to_string(message), expected_value);
42  g_main_loop_quit(static_cast<GMainLoop*>(user_data));
43 }
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:395
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
FlMethodResponse GError ** error
G_MODULE_EXPORT FlValue * fl_basic_message_channel_send_finish(FlBasicMessageChannel *self, GAsyncResult *result, GError **error)
G_BEGIN_DECLS FlMethodCall gpointer user_data
G_MODULE_EXPORT gchar * fl_value_to_string(FlValue *value)
Definition: fl_value.cc:723
const char * expected_value

◆ make_mock_engine()

static FlEngine* make_mock_engine ( )
static

Definition at line 17 of file fl_key_event_plugin_test.cc.

References engine, fl_dart_project_new(), fl_engine_new(), fl_engine_start(), and fl_mock_renderer_new().

Referenced by TEST(), and test_lock_event().

17  {
18  g_autoptr(FlDartProject) project = fl_dart_project_new();
19  g_autoptr(FlMockRenderer) renderer = fl_mock_renderer_new();
20  g_autoptr(FlEngine) engine = fl_engine_new(project, FL_RENDERER(renderer));
21  g_autoptr(GError) engine_error = nullptr;
22  EXPECT_TRUE(fl_engine_start(engine, &engine_error));
23  EXPECT_EQ(engine_error, nullptr);
24 
25  return static_cast<FlEngine*>(g_object_ref(engine));
26 }
FlMockRenderer * fl_mock_renderer_new()
gboolean fl_engine_start(FlEngine *self, GError **error)
Definition: fl_engine.cc:352
FlEngine * fl_engine_new(FlDartProject *project, FlRenderer *renderer)
Definition: fl_engine.cc:337
G_MODULE_EXPORT FlDartProject * fl_dart_project_new()

◆ TEST() [1/4]

TEST ( FlKeyEventPluginTest  ,
SendKeyEvent   
)

Definition at line 46 of file fl_key_event_plugin_test.cc.

References echo_response_cb(), engine, expected_value, fl_binary_messenger_new(), fl_key_event_plugin_new(), fl_key_event_plugin_send_key_event(), and make_mock_engine().

46  {
47  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
48 
49  g_autoptr(FlEngine) engine = make_mock_engine();
50  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
51  g_autoptr(FlKeyEventPlugin) plugin =
52  fl_key_event_plugin_new(messenger, echo_response_cb, "test/echo");
53 
54  char string[] = "A";
55  GdkEventKey key_event = GdkEventKey{
56  GDK_KEY_PRESS, // event type
57  nullptr, // window (not needed)
58  FALSE, // event was sent explicitly
59  12345, // time
60  0x0, // modifier state
61  GDK_KEY_A, // key code
62  1, // length of string representation
63  reinterpret_cast<gchar*>(&string[0]), // string representation
64  0x04, // scan code
65  0, // keyboard group
66  0, // is a modifier
67  };
68 
70  "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, keyCode: 65, "
71  "modifiers: 0, unicodeScalarValues: 65}";
72  fl_key_event_plugin_send_key_event(plugin, &key_event, loop);
73 
74  // Blocks here until echo_response_cb is called.
75  g_main_loop_run(loop);
76 
77  key_event = GdkEventKey{
78  GDK_KEY_RELEASE, // event type
79  nullptr, // window (not needed)
80  FALSE, // event was sent explicitly
81  12345, // time
82  0x0, // modifier state
83  GDK_KEY_A, // key code
84  1, // length of string representation
85  reinterpret_cast<gchar*>(&string[0]), // string representation
86  0x04, // scan code
87  0, // keyboard group
88  0, // is a modifier
89  };
90 
91  expected_value =
92  "{type: keyup, keymap: linux, scanCode: 4, toolkit: gtk, keyCode: 65, "
93  "modifiers: 0, unicodeScalarValues: 65}";
94  fl_key_event_plugin_send_key_event(plugin, &key_event, loop);
95 
96  // Blocks here until echo_response_cb is called.
97  g_main_loop_run(loop);
98 }
FlKeyEventPlugin * fl_key_event_plugin_new(FlBinaryMessenger *messenger, GAsyncReadyCallback response_callback, const char *channel_name)
static FlEngine * make_mock_engine()
static void echo_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
void fl_key_event_plugin_send_key_event(FlKeyEventPlugin *self, GdkEventKey *event, gpointer user_data)
const char * expected_value
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)

◆ TEST() [2/4]

TEST ( FlKeyEventPluginTest  ,
SendNumLockKeyEvent   
)

Definition at line 140 of file fl_key_event_plugin_test.cc.

References test_lock_event().

140  {
141  test_lock_event(GDK_KEY_Num_Lock,
142  "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, "
143  "keyCode: 65407, modifiers: 16}",
144  "{type: keyup, keymap: linux, scanCode: 4, toolkit: gtk, "
145  "keyCode: 65407, modifiers: 0}");
146 }
void test_lock_event(guint key_code, const char *down_expected, const char *up_expected)

◆ TEST() [3/4]

TEST ( FlKeyEventPluginTest  ,
SendCapsLockKeyEvent   
)

Definition at line 149 of file fl_key_event_plugin_test.cc.

References test_lock_event().

149  {
150  test_lock_event(GDK_KEY_Caps_Lock,
151  "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, "
152  "keyCode: 65509, modifiers: 2}",
153  "{type: keyup, keymap: linux, scanCode: 4, toolkit: gtk, "
154  "keyCode: 65509, modifiers: 0}");
155 }
void test_lock_event(guint key_code, const char *down_expected, const char *up_expected)

◆ TEST() [4/4]

TEST ( FlKeyEventPluginTest  ,
SendShiftLockKeyEvent   
)

Definition at line 158 of file fl_key_event_plugin_test.cc.

References test_lock_event().

158  {
159  test_lock_event(GDK_KEY_Shift_Lock,
160  "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, "
161  "keyCode: 65510, modifiers: 2}",
162  "{type: keyup, keymap: linux, scanCode: 4, toolkit: gtk, "
163  "keyCode: 65510, modifiers: 0}");
164 }
void test_lock_event(guint key_code, const char *down_expected, const char *up_expected)

◆ test_lock_event()

void test_lock_event ( guint  key_code,
const char *  down_expected,
const char *  up_expected 
)

Definition at line 100 of file fl_key_event_plugin_test.cc.

References echo_response_cb(), engine, expected_value, fl_binary_messenger_new(), fl_key_event_plugin_new(), fl_key_event_plugin_send_key_event(), and make_mock_engine().

Referenced by TEST().

102  {
103  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
104 
105  g_autoptr(FlEngine) engine = make_mock_engine();
106  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
107  g_autoptr(FlKeyEventPlugin) plugin =
108  fl_key_event_plugin_new(messenger, echo_response_cb, "test/echo");
109 
110  GdkEventKey key_event = GdkEventKey{
111  GDK_KEY_PRESS, // event type
112  nullptr, // window (not needed)
113  FALSE, // event was sent explicitly
114  12345, // time
115  0x10, // modifier state
116  key_code, // key code
117  1, // length of string representation
118  nullptr, // string representation
119  0x04, // scan code
120  0, // keyboard group
121  0, // is a modifier
122  };
123 
124  expected_value = down_expected;
125  fl_key_event_plugin_send_key_event(plugin, &key_event, loop);
126 
127  // Blocks here until echo_response_cb is called.
128  g_main_loop_run(loop);
129 
130  key_event.type = GDK_KEY_RELEASE;
131 
132  expected_value = up_expected;
133  fl_key_event_plugin_send_key_event(plugin, &key_event, loop);
134 
135  // Blocks here until echo_response_cb is called.
136  g_main_loop_run(loop);
137 }
FlKeyEventPlugin * fl_key_event_plugin_new(FlBinaryMessenger *messenger, GAsyncReadyCallback response_callback, const char *channel_name)
static FlEngine * make_mock_engine()
static void echo_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
void fl_key_event_plugin_send_key_event(FlKeyEventPlugin *self, GdkEventKey *event, gpointer user_data)
const char * expected_value
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)

Variable Documentation

◆ expected_value

const char* expected_value = nullptr