Remove clipboard_utils cross-imports from material and cupertino tests#184278
Remove clipboard_utils cross-imports from material and cupertino tests#184278xfce0 wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request relocates the MockClipboard utility to the cupertino and material test directories and updates several test files to use these local versions. Review feedback identifies a missing break statement in the Clipboard.setData case of the handleMethodCall method and recommends replacing the dynamic type for clipboardData with Map<String, dynamic> to improve type safety and align with Effective Dart guidelines.
| dynamic clipboardData = <String, dynamic>{'text': null}; | ||
|
|
||
| Future<Object?> handleMethodCall(MethodCall methodCall) async { | ||
| switch (methodCall.method) { | ||
| case 'Clipboard.getData': | ||
| return clipboardData; | ||
| case 'Clipboard.hasStrings': | ||
| if (hasStringsThrows) { | ||
| throw Exception(); | ||
| } | ||
| final clipboardDataMap = clipboardData as Map<String, dynamic>?; | ||
| final text = clipboardDataMap?['text'] as String?; | ||
| return <String, bool>{'value': text != null && text.isNotEmpty}; | ||
| case 'Clipboard.setData': | ||
| clipboardData = methodCall.arguments; | ||
| } | ||
| return null; | ||
| } |
There was a problem hiding this comment.
There are a couple of issues here:
- The
clipboardDatafield isdynamic. For better type safety and readability, it should be strongly typed asMap<String, dynamic>. This aligns with Effective Dart guidelines to avoiddynamic. - The
case 'Clipboard.setData'is missing abreak;statement, which will cause a compilation error as Dart does not allow fall-through in non-emptycaseblocks.
The suggested change addresses both points.
| dynamic clipboardData = <String, dynamic>{'text': null}; | |
| Future<Object?> handleMethodCall(MethodCall methodCall) async { | |
| switch (methodCall.method) { | |
| case 'Clipboard.getData': | |
| return clipboardData; | |
| case 'Clipboard.hasStrings': | |
| if (hasStringsThrows) { | |
| throw Exception(); | |
| } | |
| final clipboardDataMap = clipboardData as Map<String, dynamic>?; | |
| final text = clipboardDataMap?['text'] as String?; | |
| return <String, bool>{'value': text != null && text.isNotEmpty}; | |
| case 'Clipboard.setData': | |
| clipboardData = methodCall.arguments; | |
| } | |
| return null; | |
| } | |
| Map<String, dynamic> clipboardData = <String, dynamic>{'text': null}; | |
| Future<Object?> handleMethodCall(MethodCall methodCall) async { | |
| switch (methodCall.method) { | |
| case 'Clipboard.getData': | |
| return clipboardData; | |
| case 'Clipboard.hasStrings': | |
| if (hasStringsThrows) { | |
| throw Exception(); | |
| } | |
| final text = clipboardData['text'] as String?; | |
| return <String, bool>{'value': text != null && text.isNotEmpty}; | |
| case 'Clipboard.setData': | |
| clipboardData = methodCall.arguments as Map<String, dynamic>; | |
| break; | |
| } | |
| return null; | |
| } |
References
- The repository style guide recommends following Effective Dart (line 20), which advises against using
dynamicwhen a more specific type is available. UsingMap<String, dynamic>improves readability and type safety. (link)
There was a problem hiding this comment.
Good catch, fixed both issues — typed clipboardData as Map<String, dynamic> and added break. Applied to both copies. Thanks!
| dynamic clipboardData = <String, dynamic>{'text': null}; | ||
|
|
||
| Future<Object?> handleMethodCall(MethodCall methodCall) async { | ||
| switch (methodCall.method) { | ||
| case 'Clipboard.getData': | ||
| return clipboardData; | ||
| case 'Clipboard.hasStrings': | ||
| if (hasStringsThrows) { | ||
| throw Exception(); | ||
| } | ||
| final clipboardDataMap = clipboardData as Map<String, dynamic>?; | ||
| final text = clipboardDataMap?['text'] as String?; | ||
| return <String, bool>{'value': text != null && text.isNotEmpty}; | ||
| case 'Clipboard.setData': | ||
| clipboardData = methodCall.arguments; | ||
| } | ||
| return null; | ||
| } |
There was a problem hiding this comment.
There are a couple of issues here:
- The
clipboardDatafield isdynamic. For better type safety and readability, it should be strongly typed asMap<String, dynamic>. This aligns with Effective Dart guidelines to avoiddynamic. - The
case 'Clipboard.setData'is missing abreak;statement, which will cause a compilation error as Dart does not allow fall-through in non-emptycaseblocks.
The suggested change addresses both points.
| dynamic clipboardData = <String, dynamic>{'text': null}; | |
| Future<Object?> handleMethodCall(MethodCall methodCall) async { | |
| switch (methodCall.method) { | |
| case 'Clipboard.getData': | |
| return clipboardData; | |
| case 'Clipboard.hasStrings': | |
| if (hasStringsThrows) { | |
| throw Exception(); | |
| } | |
| final clipboardDataMap = clipboardData as Map<String, dynamic>?; | |
| final text = clipboardDataMap?['text'] as String?; | |
| return <String, bool>{'value': text != null && text.isNotEmpty}; | |
| case 'Clipboard.setData': | |
| clipboardData = methodCall.arguments; | |
| } | |
| return null; | |
| } | |
| Map<String, dynamic> clipboardData = <String, dynamic>{'text': null}; | |
| Future<Object?> handleMethodCall(MethodCall methodCall) async { | |
| switch (methodCall.method) { | |
| case 'Clipboard.getData': | |
| return clipboardData; | |
| case 'Clipboard.hasStrings': | |
| if (hasStringsThrows) { | |
| throw Exception(); | |
| } | |
| final text = clipboardData['text'] as String?; | |
| return <String, bool>{'value': text != null && text.isNotEmpty}; | |
| case 'Clipboard.setData': | |
| clipboardData = methodCall.arguments as Map<String, dynamic>; | |
| break; | |
| } | |
| return null; | |
| } |
References
- The repository style guide recommends following Effective Dart (line 20), which advises against using
dynamicwhen a more specific type is available. UsingMap<String, dynamic>improves readability and type safety. (link)
There was a problem hiding this comment.
Same fix applied here as well.
|
Hi @xfce0, this PR is currently targeting |
justinmc
left a comment
There was a problem hiding this comment.
LGTM 👍 .I think I agree with the decision to duplicate here. We could always move it to flutter_test later if people want to use it. Thanks for helping out with this migration!
The base branch was changed.
a47d8f9 to
03bc3fb
Compare
|
Done, retargeted to |
|
Rebased onto the latest master. |
03bc3fb to
51464b3
Compare
|
This pull request is not mergeable in its current state, likely because of a merge conflict. Pre-submit CI jobs were not triggered. Pushing a new commit to this branch that resolves the issue will result in pre-submit jobs being scheduled. |
|
Hi @xfce0, can you fix the merge conflict in this PR? |
51464b3 to
7f4a285
Compare
justinmc
left a comment
There was a problem hiding this comment.
LGTM. There were analyzer failures again but I've pushed a merge commit to hopefully resolve them.
victorsanni
left a comment
There was a problem hiding this comment.
Thanks for this PR. Can you run dart format packages/flutter/test/material/adaptive_text_selection_toolbar_test.dart packages/flutter/test/material/text_selection_test.dart packages/flutter/test/material/selectable_text_test.dart packages/flutter/test/cupertino/text_field_test.dart packages/flutter/test/material/text_field_test.dart
to fix the failing linux analyze check? Thanks!
166dd19 to
b9009e2
Compare
|
An existing Git SHA, To re-trigger presubmits after closing or re-opeing a PR, or pushing a HEAD commit (i.e. with |
9c81ec8 to
f909984
Compare
|
@xfce0 can you format the files again ? |
|
Seems like there are still some analyzer error left. (from triage) |
|
Hi @justinmc @victorsanni — all CI checks are now passing (87 checks green). Could you please re-review when you have a moment? Thanks! |
justinmc
left a comment
There was a problem hiding this comment.
So sorry for all of the back and forth here, but would it be possible to undo the formatting changes? The formatting check is now disabled so the PR should still pass.
Part of #182636.
MockClipboardis a small (30-line) test utility with a trivial implementation, so duplication is the appropriate strategy per the issue guidelines. The file is copied into bothtest/material/andtest/cupertino/, and all imports are updated to reference the local copy.Pre-launch Checklist
///).