mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Fix incorrect platformchannel response when clipboard getData is not supported (flutter/engine#25743)
This commit is contained in:
parent
9c2c4dd009
commit
39caf3500e
@ -45,12 +45,29 @@ class ClipboardMessageHandler {
|
||||
final Map<String, dynamic> map = <String, dynamic>{'text': data};
|
||||
callback!(codec.encodeSuccessEnvelope(map));
|
||||
}).catchError((dynamic error) {
|
||||
print('Could not get text from clipboard: $error');
|
||||
callback!(codec.encodeErrorEnvelope(
|
||||
code: 'paste_fail', message: 'Clipboard.getData failed'));
|
||||
if (error is UnimplementedError) {
|
||||
// Clipboard.getData not supported.
|
||||
// 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.
|
||||
set pasteFromClipboardStrategy(PasteFromClipboardStrategy strategy) {
|
||||
_pasteFromClipboardStrategy = strategy;
|
||||
@ -185,6 +202,7 @@ class ExecCommandPasteStrategy implements PasteFromClipboardStrategy {
|
||||
@override
|
||||
Future<String> getData() {
|
||||
// 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 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:html' as html;
|
||||
import 'dart:js_util' as js_util;
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:ui/src/engine.dart';
|
||||
@ -55,5 +57,31 @@ void testMain() {
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user