Flutter Linux Embedder
fl_view_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 
6 #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
8 #include "flutter/shell/platform/linux/testing/fl_test.h"
9 #include "flutter/shell/platform/linux/testing/fl_test_gtk_logs.h"
10 #include "flutter/shell/platform/linux/testing/mock_window.h"
11 
12 #include "gtest/gtest.h"
13 
14 static void first_frame_cb(FlView* view, gboolean* first_frame_emitted) {
15  *first_frame_emitted = TRUE;
16 }
17 
18 TEST(FlViewTest, GetEngine) {
19  flutter::testing::fl_ensure_gtk_init();
20  g_autoptr(FlDartProject) project = fl_dart_project_new();
21  FlView* view = fl_view_new(project);
22 
23  // Check the engine is immediately available (i.e. before the widget is
24  // realized).
25  FlEngine* engine = fl_view_get_engine(view);
26  EXPECT_NE(engine, nullptr);
27 }
28 
29 TEST(FlViewTest, StateUpdateDoesNotHappenInInit) {
30  flutter::testing::fl_ensure_gtk_init();
31  g_autoptr(FlDartProject) project = fl_dart_project_new();
32  FlView* view = fl_view_new(project);
33  // Check that creating a view doesn't try to query the window state in
34  // initialization, causing a critical log to be issued.
35  EXPECT_EQ(
36  flutter::testing::fl_get_received_gtk_log_levels() & G_LOG_LEVEL_CRITICAL,
37  (GLogLevelFlags)0x0);
38 
39  (void)view;
40 }
41 
42 TEST(FlViewTest, FirstFrameSignal) {
43  flutter::testing::fl_ensure_gtk_init();
44 
45  g_autoptr(FlDartProject) project = fl_dart_project_new();
46  FlView* view = fl_view_new(project);
47  gboolean first_frame_emitted = FALSE;
48  g_signal_connect(view, "first-frame", G_CALLBACK(first_frame_cb),
49  &first_frame_emitted);
50 
51  EXPECT_FALSE(first_frame_emitted);
52 
53  fl_renderable_redraw(FL_RENDERABLE(view));
54 
55  // Signal is emitted in idle, clear the main loop.
56  while (g_main_context_iteration(g_main_context_default(), FALSE)) {
57  // Repeat until nothing to iterate on.
58  }
59 
60  // Check view has detected frame.
61  EXPECT_TRUE(first_frame_emitted);
62 }
63 
64 // Check secondary view is registered with engine.
65 TEST(FlViewTest, SecondaryView) {
66  flutter::testing::fl_ensure_gtk_init();
67 
68  g_autoptr(FlDartProject) project = fl_dart_project_new();
69  FlView* implicit_view = fl_view_new(project);
70 
71  FlEngine* engine = fl_view_get_engine(implicit_view);
72 
73  FlutterViewId view_id = -1;
74  fl_engine_get_embedder_api(engine)->AddView = MOCK_ENGINE_PROC(
75  AddView, ([&view_id](auto engine, const FlutterAddViewInfo* info) {
76  view_id = info->view_id;
77  FlutterAddViewResult result = {
78  .struct_size = sizeof(FlutterAddViewResult),
79  .added = true,
80  .user_data = info->user_data};
81  info->add_view_callback(&result);
82  return kSuccess;
83  }));
84 
85  g_autoptr(GError) error = nullptr;
86  EXPECT_TRUE(fl_engine_start(engine, &error));
87 
88  FlView* secondary_view = fl_view_new_for_engine(engine);
89  EXPECT_EQ(view_id, fl_view_get_id(secondary_view));
90 }
91 
92 // Check secondary view that fails registration.
93 TEST(FlViewTest, SecondaryViewError) {
94  flutter::testing::fl_ensure_gtk_init();
95 
96  g_autoptr(FlDartProject) project = fl_dart_project_new();
97  FlView* implicit_view = fl_view_new(project);
98 
99  FlEngine* engine = fl_view_get_engine(implicit_view);
100 
101  FlutterViewId view_id = -1;
102  fl_engine_get_embedder_api(engine)->AddView = MOCK_ENGINE_PROC(
103  AddView, ([&view_id](auto engine, const FlutterAddViewInfo* info) {
104  view_id = info->view_id;
105  return kInvalidArguments;
106  }));
107 
108  g_autoptr(GError) error = nullptr;
109  EXPECT_TRUE(fl_engine_start(engine, &error));
110 
111  FlView* secondary_view = fl_view_new_for_engine(engine);
112  EXPECT_EQ(view_id, fl_view_get_id(secondary_view));
113 }
114 
115 // Check views are deregistered on destruction.
116 TEST(FlViewTest, ViewDestroy) {
117  flutter::testing::fl_ensure_gtk_init();
118 
119  g_autoptr(FlDartProject) project = fl_dart_project_new();
120  FlView* implicit_view = fl_view_new(project);
121 
122  FlEngine* engine = fl_view_get_engine(implicit_view);
123 
124  g_autoptr(GPtrArray) removed_views = g_ptr_array_new();
125  fl_engine_get_embedder_api(engine)->RemoveView = MOCK_ENGINE_PROC(
126  RemoveView,
127  ([removed_views](auto engine, const FlutterRemoveViewInfo* info) {
128  g_ptr_array_add(removed_views, GINT_TO_POINTER(info->view_id));
129  return kSuccess;
130  }));
131 
132  g_autoptr(GError) error = nullptr;
133  EXPECT_TRUE(fl_engine_start(engine, &error));
134 
135  FlView* secondary_view = fl_view_new_for_engine(engine);
136 
137  int64_t implicit_view_id = fl_view_get_id(implicit_view);
138  int64_t secondary_view_id = fl_view_get_id(secondary_view);
139 
140  fl_gtk_widget_destroy(GTK_WIDGET(secondary_view));
141  fl_gtk_widget_destroy(GTK_WIDGET(implicit_view));
142 
143  EXPECT_EQ(removed_views->len, 2u);
144  EXPECT_EQ(GPOINTER_TO_INT(g_ptr_array_index(removed_views, 0)),
145  secondary_view_id);
146  EXPECT_EQ(GPOINTER_TO_INT(g_ptr_array_index(removed_views, 1)),
147  implicit_view_id);
148 }
149 
150 // Check views deregistered with errors works.
151 TEST(FlViewTest, ViewDestroyError) {
152  flutter::testing::fl_ensure_gtk_init();
153 
154  g_autoptr(FlDartProject) project = fl_dart_project_new();
155  FlView* implicit_view = fl_view_new(project);
156 
157  FlEngine* engine = fl_view_get_engine(implicit_view);
158 
159  fl_engine_get_embedder_api(engine)->RemoveView = MOCK_ENGINE_PROC(
160  RemoveView, ([](auto engine, const FlutterRemoveViewInfo* info) {
161  return kInvalidArguments;
162  }));
163 
164  g_autoptr(GError) error = nullptr;
165  EXPECT_TRUE(fl_engine_start(engine, &error));
166 
167  FlView* secondary_view = fl_view_new_for_engine(engine);
168 
169  fl_gtk_widget_destroy(GTK_WIDGET(secondary_view));
170  fl_gtk_widget_destroy(GTK_WIDGET(implicit_view));
171 }
fl_engine_get_embedder_api
FlutterEngineProcTable * fl_engine_get_embedder_api(FlEngine *self)
Definition: fl_engine.cc:721
fl_view_get_id
G_MODULE_EXPORT int64_t fl_view_get_id(FlView *self)
Definition: fl_view.cc:795
fl_view_new_for_engine
G_MODULE_EXPORT FlView * fl_view_new_for_engine(FlEngine *engine)
Definition: fl_view.cc:764
fl_dart_project_new
G_MODULE_EXPORT FlDartProject * fl_dart_project_new()
Definition: fl_dart_project.cc:50
fl_view_new
G_MODULE_EXPORT FlView * fl_view_new(FlDartProject *project)
Definition: fl_view.cc:736
user_data
G_BEGIN_DECLS G_MODULE_EXPORT FlValue gpointer user_data
Definition: fl_event_channel.h:90
fl_engine_private.h
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
fl_view_get_engine
G_MODULE_EXPORT FlEngine * fl_view_get_engine(FlView *self)
Definition: fl_view.cc:789
fl_view.h
view
FlView * view
Definition: fl_application.cc:36
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
fl_renderable_redraw
void fl_renderable_redraw(FlRenderable *self)
Definition: fl_renderable.cc:11
fl_engine_start
gboolean fl_engine_start(FlEngine *self, GError **error)
Definition: fl_engine.cc:607
TEST
TEST(FlViewTest, GetEngine)
Definition: fl_view_test.cc:18
first_frame_cb
static void first_frame_cb(FlView *view, gboolean *first_frame_emitted)
Definition: fl_view_test.cc:14