Skip to content

Make sure that a DesktopTextSelectionToolbar doesn't crash in 0x0 env… #173928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ahmedsameha1
Copy link
Contributor

This is my attempt to handle #6537 for the DesktopTextSelectionToolbar UI control.

@github-actions github-actions bot added a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Aug 17, 2025
Copy link
Contributor

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

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 a test case to ensure that DesktopTextSelectionToolbar does not crash when rendered in a zero-sized environment, which is a good addition to prevent regressions. My feedback focuses on improving the new test by adding an explicit assertion to make its intent clearer and more robust.

Comment on lines +31 to +42
testWidgets('DesktopTextSelectionToolbar renders at zero area', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: SizedBox.shrink(
child: DesktopTextSelectionToolbar(
anchor: const Offset(10, 10),
children: const <Widget>[Text('X')],
),
),
),
);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

While this test correctly checks that pumping the widget in a zero-sized area doesn't cause a crash, it would be more robust and clearer to add an assertion to verify that the widget is actually present in the widget tree.1

Adding an expect call ensures that the test is not passing simply because the widget failed to render silently, making the test's purpose more explicit.

  testWidgets('DesktopTextSelectionToolbar renders at zero area', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: SizedBox.shrink(
          child: DesktopTextSelectionToolbar(
            anchor: const Offset(10, 10),
            children: const <Widget>[Text('X')],
          ),
        ),
      ),
    );

    expect(find.byType(DesktopTextSelectionToolbar), findsOneWidget);
  });

Style Guide References

Footnotes

  1. Tests should follow the guidance in the 'writing effective tests guide'. Tests are generally expected to have assertions to verify behavior, not just the absence of crashes. (link)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is fine either way.

Copy link
Contributor

@justinmc justinmc Aug 21, 2025

Choose a reason for hiding this comment

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

Actually, in all of these PRs, we should have an a expect that checks that the relevant widget (DesktopTextSelectionToolbar here) has a size of 0x0. Can you add that @ahmedsameha1?

Thanks @LongCatIsLooong for flagging this.

Copy link
Contributor

@dkwingsmt dkwingsmt Aug 22, 2025

Choose a reason for hiding this comment

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

@justinmc We don't need any expect. The original issue only wants to make sure that it doesn't crash, not even needing the actual size to be zero.

Copy link
Contributor

@justinmc justinmc left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@dkwingsmt During triage you mentioned you were concerned about this test, were you wondering if the toolbar would end up in an Overlay or what was it?

Comment on lines +31 to +42
testWidgets('DesktopTextSelectionToolbar renders at zero area', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: SizedBox.shrink(
child: DesktopTextSelectionToolbar(
anchor: const Offset(10, 10),
children: const <Widget>[Text('X')],
),
),
),
);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is fine either way.

@dkwingsmt
Copy link
Contributor

Yeah, I was saying I wanted someone familiar with this to verify its validity, such as making sure it's not laying out overlay.

@justinmc
Copy link
Contributor

In that case we are good, the Overlay thing is done in text_selection.dart, not as a part of DesktopTextSelectionToolbar itself 👍 . Thanks for clarifying!

Comment on lines +31 to +42
testWidgets('DesktopTextSelectionToolbar renders at zero area', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: SizedBox.shrink(
child: DesktopTextSelectionToolbar(
anchor: const Offset(10, 10),
children: const <Widget>[Text('X')],
),
),
),
);
});
Copy link
Contributor

@justinmc justinmc Aug 21, 2025

Choose a reason for hiding this comment

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

Actually, in all of these PRs, we should have an a expect that checks that the relevant widget (DesktopTextSelectionToolbar here) has a size of 0x0. Can you add that @ahmedsameha1?

Thanks @LongCatIsLooong for flagging this.

@ahmedsameha1
Copy link
Contributor Author

ahmedsameha1 commented Aug 21, 2025

@justinmc Could you resolve this in #6537 because, according to #6537 (comment), there is no need to add that expectation.
Please consider this: #172074 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: text input Entering text in a text field or keyboard related problems f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants