Skip to content

Commit 2000a5a

Browse files
authored
Add await mechanism to setClipboard in android_semantics_integration test (flutter#185428)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> fixes flutter#184755 The failure suggest there may be race condition when setClipboard async update the OS while TextField receive canPaste signal. Adding an await mechanism to see if that eliminate the race condition. ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 708c986 commit 2000a5a

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

  • dev/integration_tests/android_semantics_testing/android/app/src/main/java/com/yourcompany/platforminteraction

dev/integration_tests/android_semantics_testing/android/app/src/main/java/com/yourcompany/platforminteraction/MainActivity.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,40 @@ public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
7272
@SuppressWarnings("unchecked")
7373
String message = (String) data.get("message");
7474
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
75+
76+
if (clipboard.hasPrimaryClip()) {
77+
ClipData currentClip = clipboard.getPrimaryClip();
78+
if (currentClip != null && currentClip.getItemCount() > 0) {
79+
CharSequence text = currentClip.getItemAt(0).getText();
80+
if (text != null && text.toString().equals(message)) {
81+
// The clipboard already has the expected value, no need to set it.
82+
result.success(null);
83+
return;
84+
}
85+
}
86+
}
87+
88+
// To avoid race conditions during integration tests, wait until the system has
89+
// completed the clipboard update before returning success to the test runner.
90+
ClipboardManager.OnPrimaryClipChangedListener listener = new ClipboardManager.OnPrimaryClipChangedListener() {
91+
@Override
92+
public void onPrimaryClipChanged() {
93+
ClipData currentClip = clipboard.getPrimaryClip();
94+
if (currentClip != null && currentClip.getItemCount() > 0) {
95+
CharSequence text = currentClip.getItemAt(0).getText();
96+
// Verify if the clipboard has the correct value before returning success to the test runner.
97+
if (text != null && text.toString().equals(message)) {
98+
clipboard.removePrimaryClipChangedListener(this);
99+
result.success(null);
100+
return;
101+
}
102+
}
103+
}
104+
};
105+
106+
clipboard.addPrimaryClipChangedListener(listener);
75107
ClipData clip = ClipData.newPlainText("message", message);
76108
clipboard.setPrimaryClip(clip);
77-
result.success(null);
78109
return;
79110
}
80111
result.notImplemented();

0 commit comments

Comments
 (0)