Add tests for FlMouseCursorHandler by robert-ancell · Pull Request #188554 · flutter/flutter · GitHub
Skip to content

Add tests for FlMouseCursorHandler#188554

Open
robert-ancell wants to merge 3 commits into
flutter:masterfrom
robert-ancell:linux-mouse-cursor-handler-tests
Open

Add tests for FlMouseCursorHandler#188554
robert-ancell wants to merge 3 commits into
flutter:masterfrom
robert-ancell:linux-mouse-cursor-handler-tests

Conversation

@robert-ancell

Copy link
Copy Markdown
Contributor

Add unit tests covering cursor kind mapping, the fallback to the default cursor, the cursor-changed signal, and method call error handling for the mouse cursor handler.

Add unit tests covering cursor kind mapping, the fallback to the default
cursor, the cursor-changed signal, and method call error handling for the
mouse cursor handler.
An activateSystemCursor method call whose argument map omits the "kind"
key (or has a non-string value) passes a null kind to the handler, which
was passed straight to g_hash_table_lookup and crashed in g_str_hash.
Guard against a null kind and fall back to the default cursor.

Also guard the channel's fl_value_get_type lookup against a missing
"kind" key, which otherwise triggered a CRITICAL assertion warning.

Add a regression test covering a call with a missing kind.
@robert-ancell robert-ancell requested a review from a team as a code owner June 25, 2026 06:17
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Jun 25, 2026
@github-actions github-actions Bot added engine flutter/engine related. See also e: labels. platform-linux Building on or for Linux specifically a: desktop Running on desktop team-linux Owned by the Linux platform team labels Jun 25, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds unit tests for FlMouseCursorHandler and introduces null checks in fl_mouse_cursor_channel.cc and fl_mouse_cursor_handler.cc to prevent potential crashes. The review feedback highlights a critical double-free or use-after-free issue in the new test file, where fl_value_set_string takes ownership of kind while it is also managed by g_autoptr. The reviewer suggests refactoring activate_system_cursor to accept a const gchar* and simplifying the test cases to resolve this issue.

Comment on lines +14 to +32

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The fl_value_set_string function takes ownership of the value passed to it (transfer full). Passing kind directly to fl_value_set_string transfers its ownership to args. Since kind is also managed by a g_autoptr in the calling test cases, this leads to a double-free / use-after-free bug when args is destroyed at the end of this function.

To fix this and simplify the test cases, we can change activate_system_cursor to accept a const gchar* kind string instead of an FlValue*.

static gboolean activate_system_cursor(FlMockBinaryMessenger* messenger,
                                       const gchar* kind) {
  g_autoptr(FlValue) args = fl_value_new_map();
  fl_value_set_string(args, "kind", fl_value_new_string(kind));

  gboolean called = FALSE;
  fl_mock_binary_messenger_invoke_standard_method(
      messenger, "flutter/mousecursor", "activateSystemCursor", args,
      [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
         gpointer user_data) {
        gboolean* called = static_cast<gboolean*>(user_data);
        *called = TRUE;

        EXPECT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
      },
      &called);

  return called;
}

Comment on lines +49 to +50
g_autoptr(FlValue) kind = fl_value_new_string("click");
EXPECT_TRUE(activate_system_cursor(messenger, kind));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Simplify the test case by passing the string directly to activate_system_cursor, which avoids the double-free of kind.

  EXPECT_TRUE(activate_system_cursor(messenger, "click"));

Comment on lines +61 to +62
g_autoptr(FlValue) kind = fl_value_new_string("basic");
EXPECT_TRUE(activate_system_cursor(messenger, kind));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Simplify the test case by passing the string directly to activate_system_cursor, which avoids the double-free of kind.

  EXPECT_TRUE(activate_system_cursor(messenger, "basic"));

Comment on lines +73 to +74
g_autoptr(FlValue) kind = fl_value_new_string("madeUpCursorKind");
EXPECT_TRUE(activate_system_cursor(messenger, kind));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Simplify the test case by passing the string directly to activate_system_cursor, which avoids the double-free of kind.

  EXPECT_TRUE(activate_system_cursor(messenger, "madeUpCursorKind"));

Comment on lines +115 to +116

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Simplify the test case by passing the string directly to activate_system_cursor, which avoids the double-free of kind.

  EXPECT_TRUE(activate_system_cursor(messenger, "grab"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: desktop Running on desktop CICD Run CI/CD engine flutter/engine related. See also e: labels. platform-linux Building on or for Linux specifically team-linux Owned by the Linux platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants