|
| 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 | +// Included first as it collides with the X11 headers. |
| 6 | +#include "gtest/gtest.h" |
| 7 | + |
| 8 | +#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h" |
| 9 | +#include "flutter/shell/platform/linux/fl_engine_private.h" |
| 10 | +#include "flutter/shell/platform/linux/public/flutter_linux/fl_engine.h" |
| 11 | +#include "flutter/shell/platform/linux/testing/fl_test.h" |
| 12 | + |
| 13 | +// Checks sending window metrics events works. |
| 14 | +TEST(FlEngineTest, WindowMetrics) { |
| 15 | + g_autoptr(FlEngine) engine = make_mock_engine(); |
| 16 | + FlutterEngineProcTable* embedder_api = fl_engine_get_embedder_api(engine); |
| 17 | + |
| 18 | + bool called = false; |
| 19 | + embedder_api->SendWindowMetricsEvent = MOCK_ENGINE_PROC( |
| 20 | + SendWindowMetricsEvent, |
| 21 | + ([&called](auto engine, const FlutterWindowMetricsEvent* event) { |
| 22 | + called = true; |
| 23 | + EXPECT_EQ(event->width, static_cast<size_t>(3840)); |
| 24 | + EXPECT_EQ(event->height, static_cast<size_t>(2160)); |
| 25 | + EXPECT_EQ(event->pixel_ratio, 2.0); |
| 26 | + |
| 27 | + return kSuccess; |
| 28 | + })); |
| 29 | + |
| 30 | + g_autoptr(GError) error = nullptr; |
| 31 | + EXPECT_TRUE(fl_engine_start(engine, &error)); |
| 32 | + EXPECT_EQ(error, nullptr); |
| 33 | + fl_engine_send_window_metrics_event(engine, 3840, 2160, 2.0); |
| 34 | + |
| 35 | + EXPECT_TRUE(called); |
| 36 | +} |
| 37 | + |
| 38 | +// Checks sending mouse pointer events works. |
| 39 | +TEST(FlEngineTest, MousePointer) { |
| 40 | + g_autoptr(FlEngine) engine = make_mock_engine(); |
| 41 | + FlutterEngineProcTable* embedder_api = fl_engine_get_embedder_api(engine); |
| 42 | + |
| 43 | + bool called = false; |
| 44 | + embedder_api->SendPointerEvent = MOCK_ENGINE_PROC( |
| 45 | + SendPointerEvent, |
| 46 | + ([&called](auto engine, const FlutterPointerEvent* events, |
| 47 | + size_t events_count) { |
| 48 | + called = true; |
| 49 | + EXPECT_EQ(events_count, static_cast<size_t>(1)); |
| 50 | + EXPECT_EQ(events[0].phase, kDown); |
| 51 | + EXPECT_EQ(events[0].timestamp, static_cast<size_t>(1234567890)); |
| 52 | + EXPECT_EQ(events[0].x, 800); |
| 53 | + EXPECT_EQ(events[0].y, 600); |
| 54 | + EXPECT_EQ(events[0].device, static_cast<int32_t>(0)); |
| 55 | + EXPECT_EQ(events[0].signal_kind, kFlutterPointerSignalKindScroll); |
| 56 | + EXPECT_EQ(events[0].scroll_delta_x, 1.2); |
| 57 | + EXPECT_EQ(events[0].scroll_delta_y, -3.4); |
| 58 | + EXPECT_EQ(events[0].device_kind, kFlutterPointerDeviceKindMouse); |
| 59 | + EXPECT_EQ(events[0].buttons, kFlutterPointerButtonMouseSecondary); |
| 60 | + |
| 61 | + return kSuccess; |
| 62 | + })); |
| 63 | + |
| 64 | + g_autoptr(GError) error = nullptr; |
| 65 | + EXPECT_TRUE(fl_engine_start(engine, &error)); |
| 66 | + EXPECT_EQ(error, nullptr); |
| 67 | + fl_engine_send_mouse_pointer_event(engine, kDown, 1234567890, 800, 600, 1.2, |
| 68 | + -3.4, kFlutterPointerButtonMouseSecondary); |
| 69 | + |
| 70 | + EXPECT_TRUE(called); |
| 71 | +} |
| 72 | + |
| 73 | +// Checks sending platform messages works. |
| 74 | +TEST(FlEngineTest, PlatformMessage) { |
| 75 | + g_autoptr(FlEngine) engine = make_mock_engine(); |
| 76 | + FlutterEngineProcTable* embedder_api = fl_engine_get_embedder_api(engine); |
| 77 | + |
| 78 | + bool called = false; |
| 79 | + embedder_api->SendPlatformMessage = MOCK_ENGINE_PROC( |
| 80 | + SendPlatformMessage, |
| 81 | + ([&called](auto engine, const FlutterPlatformMessage* message) { |
| 82 | + called = true; |
| 83 | + |
| 84 | + EXPECT_STREQ(message->channel, "test"); |
| 85 | + EXPECT_EQ(message->message_size, static_cast<size_t>(4)); |
| 86 | + EXPECT_EQ(message->message[0], 't'); |
| 87 | + EXPECT_EQ(message->message[1], 'e'); |
| 88 | + EXPECT_EQ(message->message[2], 's'); |
| 89 | + EXPECT_EQ(message->message[3], 't'); |
| 90 | + |
| 91 | + return kSuccess; |
| 92 | + })); |
| 93 | + |
| 94 | + g_autoptr(GError) error = nullptr; |
| 95 | + EXPECT_TRUE(fl_engine_start(engine, &error)); |
| 96 | + EXPECT_EQ(error, nullptr); |
| 97 | + g_autoptr(GBytes) message = g_bytes_new_static("test", 4); |
| 98 | + fl_engine_send_platform_message(engine, "test", message, nullptr, nullptr, |
| 99 | + nullptr); |
| 100 | + |
| 101 | + EXPECT_TRUE(called); |
| 102 | +} |
| 103 | + |
| 104 | +// Checks sending platform message responses works. |
| 105 | +TEST(FlEngineTest, PlatformMessageResponse) { |
| 106 | + g_autoptr(FlEngine) engine = make_mock_engine(); |
| 107 | + FlutterEngineProcTable* embedder_api = fl_engine_get_embedder_api(engine); |
| 108 | + |
| 109 | + bool called = false; |
| 110 | + embedder_api->SendPlatformMessageResponse = MOCK_ENGINE_PROC( |
| 111 | + SendPlatformMessageResponse, |
| 112 | + ([&called](auto engine, |
| 113 | + const FlutterPlatformMessageResponseHandle* handle, |
| 114 | + const uint8_t* data, size_t data_length) { |
| 115 | + called = true; |
| 116 | + |
| 117 | + EXPECT_EQ( |
| 118 | + handle, |
| 119 | + reinterpret_cast<const FlutterPlatformMessageResponseHandle*>(42)); |
| 120 | + EXPECT_EQ(data_length, static_cast<size_t>(4)); |
| 121 | + EXPECT_EQ(data[0], 't'); |
| 122 | + EXPECT_EQ(data[1], 'e'); |
| 123 | + EXPECT_EQ(data[2], 's'); |
| 124 | + EXPECT_EQ(data[3], 't'); |
| 125 | + |
| 126 | + return kSuccess; |
| 127 | + })); |
| 128 | + |
| 129 | + g_autoptr(GError) error = nullptr; |
| 130 | + EXPECT_TRUE(fl_engine_start(engine, &error)); |
| 131 | + EXPECT_EQ(error, nullptr); |
| 132 | + g_autoptr(GBytes) response = g_bytes_new_static("test", 4); |
| 133 | + EXPECT_TRUE(fl_engine_send_platform_message_response( |
| 134 | + engine, reinterpret_cast<const FlutterPlatformMessageResponseHandle*>(42), |
| 135 | + response, &error)); |
| 136 | + EXPECT_EQ(error, nullptr); |
| 137 | + |
| 138 | + EXPECT_TRUE(called); |
| 139 | +} |
0 commit comments