未验证 提交 bf5df7f2 编写于 作者: F Ferhat 提交者: GitHub

Fix incorrect platformchannel response when clipboard getData is not supported (#25743)

上级 31d31c05
...@@ -45,12 +45,29 @@ class ClipboardMessageHandler { ...@@ -45,12 +45,29 @@ class ClipboardMessageHandler {
final Map<String, dynamic> map = <String, dynamic>{'text': data}; final Map<String, dynamic> map = <String, dynamic>{'text': data};
callback!(codec.encodeSuccessEnvelope(map)); callback!(codec.encodeSuccessEnvelope(map));
}).catchError((dynamic error) { }).catchError((dynamic error) {
print('Could not get text from clipboard: $error'); if (error is UnimplementedError) {
callback!(codec.encodeErrorEnvelope( // Clipboard.getData not supported.
code: 'paste_fail', message: 'Clipboard.getData failed')); // Passing [null] to [callback] indicates that the platform message isn't
// implemented. Look at [MethodChannel.invokeMethod] to see how [null] is
// handled.
Future<void>.delayed(Duration.zero).then((_) {
if (callback != null) {
callback(null);
}
});
return;
}
_reportGetDataFailure(callback, codec, error);
}); });
} }
void _reportGetDataFailure(ui.PlatformMessageResponseCallback? callback,
MethodCodec codec, dynamic error) {
print('Could not get text from clipboard: $error');
callback!(codec.encodeErrorEnvelope(
code: 'paste_fail', message: 'Clipboard.getData failed'));
}
/// Methods used by tests. /// Methods used by tests.
set pasteFromClipboardStrategy(PasteFromClipboardStrategy strategy) { set pasteFromClipboardStrategy(PasteFromClipboardStrategy strategy) {
_pasteFromClipboardStrategy = strategy; _pasteFromClipboardStrategy = strategy;
...@@ -185,6 +202,7 @@ class ExecCommandPasteStrategy implements PasteFromClipboardStrategy { ...@@ -185,6 +202,7 @@ class ExecCommandPasteStrategy implements PasteFromClipboardStrategy {
@override @override
Future<String> getData() { Future<String> getData() {
// TODO(nurhan): https://github.com/flutter/flutter/issues/48581 // TODO(nurhan): https://github.com/flutter/flutter/issues/48581
throw UnimplementedError('Paste is not implemented for this browser.'); return Future.error(
UnimplementedError('Paste is not implemented for this browser.'));
} }
} }
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async'; import 'dart:async';
import 'dart:html' as html;
import 'dart:js_util' as js_util;
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:ui/src/engine.dart'; import 'package:ui/src/engine.dart';
...@@ -55,5 +57,31 @@ void testMain() { ...@@ -55,5 +57,31 @@ void testMain() {
true, true,
); );
}); });
test('responds correctly to flutter/platform Clipboard.getData failure',
() async {
// Patch browser so that clipboard api is not available.
dynamic originalClipboard =
js_util.getProperty(html.window.navigator, 'clipboard');
js_util.setProperty(html.window.navigator, 'clipboard', null);
const MethodCodec codec = JSONMethodCodec();
final Completer<ByteData?> completer = Completer<ByteData?>();
ui.PlatformDispatcher.instance.sendPlatformMessage(
'flutter/platform',
codec.encodeMethodCall(MethodCall(
'Clipboard.getData',
)),
completer.complete,
);
final ByteData? response = await completer.future;
if (response != null) {
expect(
() => codec.decodeEnvelope(response),
throwsA(isA<PlatformException>()),
);
}
js_util.setProperty(
html.window.navigator, 'clipboard', originalClipboard);
});
}); });
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册