diff --git a/packages/flutter/lib/src/material/text_selection.dart b/packages/flutter/lib/src/material/text_selection.dart index bb02b6cbca..dce1d19749 100644 --- a/packages/flutter/lib/src/material/text_selection.dart +++ b/packages/flutter/lib/src/material/text_selection.dart @@ -50,7 +50,7 @@ class _TextSelectionToolbar extends StatelessWidget { } void _handleCut() { - Clipboard.setClipboardData(new ClipboardData()..text = value.selection.textInside(value.text)); + Clipboard.setData(new ClipboardData(text: value.selection.textInside(value.text))); delegate.inputValue = new InputValue( text: value.selection.textBefore(value.text) + value.selection.textAfter(value.text), selection: new TextSelection.collapsed(offset: value.selection.start) @@ -59,7 +59,7 @@ class _TextSelectionToolbar extends StatelessWidget { } void _handleCopy() { - Clipboard.setClipboardData(new ClipboardData()..text = value.selection.textInside(value.text)); + Clipboard.setData(new ClipboardData(text: value.selection.textInside(value.text))); delegate.inputValue = new InputValue( text: value.text, selection: new TextSelection.collapsed(offset: value.selection.end) @@ -69,11 +69,11 @@ class _TextSelectionToolbar extends StatelessWidget { Future _handlePaste() async { InputValue value = this.value; // Snapshot the input before using `await`. - ClipboardData clip = await Clipboard.getClipboardData(Clipboard.kTextPlain); - if (clip != null) { + ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain); + if (data != null) { delegate.inputValue = new InputValue( - text: value.selection.textBefore(value.text) + clip.text + value.selection.textAfter(value.text), - selection: new TextSelection.collapsed(offset: value.selection.start + clip.text.length) + text: value.selection.textBefore(value.text) + data.text + value.selection.textAfter(value.text), + selection: new TextSelection.collapsed(offset: value.selection.start + data.text.length) ); } delegate.hideToolbar(); diff --git a/packages/flutter/lib/src/services/clipboard.dart b/packages/flutter/lib/src/services/clipboard.dart index 4f86b27f13..c780eba82f 100644 --- a/packages/flutter/lib/src/services/clipboard.dart +++ b/packages/flutter/lib/src/services/clipboard.dart @@ -4,38 +4,48 @@ import 'dart:async'; -import 'package:flutter_services/editing.dart' as mojom; +import 'platform_messages.dart'; -import 'shell.dart'; +/// Data stored on the system clip board. +/// +/// The system clip board can contain data of various media types. This data +/// structure currently supports only plain text data in the [text] property. +class ClipboardData { + /// Creates data for the system clipboard. + const ClipboardData({ this.text }); -export 'package:flutter_services/editing.dart' show ClipboardData; - -mojom.ClipboardProxy _initClipboardProxy() { - return shell.connectToApplicationService('mojo:clipboard', mojom.Clipboard.connectToService); + /// Plain text data on the clip board. + final String text; } -final mojom.ClipboardProxy _clipboardProxy = _initClipboardProxy(); - -/// An interface to the system's clipboard. Wraps the mojo interface. +/// An interface to the system's clipboard. class Clipboard { - /// Constants for common [getClipboardData] [format] types. + /// Constants for common [getData] [format] types. static final String kTextPlain = 'text/plain'; Clipboard._(); /// Stores the given clipboard data on the clipboard. - static void setClipboardData(mojom.ClipboardData clip) { - _clipboardProxy.setClipboardData(clip); + static Future setData(ClipboardData data) async { + await PlatformMessages.sendJSON('flutter/platform', { + 'method': 'Clipboard.setData', + 'args': >[{ + 'text': data.text, + }], + }); } /// Retrieves data from the clipboard that matches the given format. /// /// * `format` is a media type, such as `text/plain`. - static Future getClipboardData(String format) { - Completer completer = new Completer(); - _clipboardProxy.getClipboardData(format, (mojom.ClipboardData clip) { - completer.complete(clip); + static Future getData(String format) async { + Map result = + await PlatformMessages.sendJSON('flutter/platform', { + 'method': 'Clipboard.getData', + 'args': [format], }); - return completer.future; + if (result == null) + return null; + return new ClipboardData(text: result['text']); } } diff --git a/packages/flutter/lib/src/services/platform_messages.dart b/packages/flutter/lib/src/services/platform_messages.dart index 698792df26..0d74656715 100644 --- a/packages/flutter/lib/src/services/platform_messages.dart +++ b/packages/flutter/lib/src/services/platform_messages.dart @@ -28,29 +28,35 @@ dynamic _decodeJSON(String message) { return message != null ? JSON.decode(message) : null; } -void _sendString(String name, String message, void callback(String reply)) { - Uint8List encoded = UTF8.encoder.convert(message); - ui.window.sendPlatformMessage(name, encoded.buffer.asByteData(), (ByteData reply) { - try { - callback(_decodeUTF8(reply)); - } catch (exception, stack) { - FlutterError.reportError(new FlutterErrorDetails( - exception: exception, - stack: stack, - library: 'services library', - context: 'during a platform message response callback', - )); - } - }); -} - typedef Future _PlatformMessageHandler(ByteData message); /// Sends message to and receives messages from the underlying platform. class PlatformMessages { + /// Handlers for incoming platform messages. static final Map _handlers = {}; + /// Mock handlers that intercept and respond to outgoing messages. + static final Map _mockHandlers = + {}; + + static Future _sendPlatformMessage(String name, ByteData message) { + final Completer completer = new Completer(); + ui.window.sendPlatformMessage(name, message, (ByteData reply) { + try { + completer.complete(reply); + } catch (exception, stack) { + FlutterError.reportError(new FlutterErrorDetails( + exception: exception, + stack: stack, + library: 'services library', + context: 'during a platform message response callback', + )); + } + }); + return completer.future; + } + /// Calls the handler registered for the given name. /// /// Typically called by [ServicesBinding] to handle platform messages received @@ -77,22 +83,22 @@ class PlatformMessages { } } + /// Send a binary message to the host application. + static Future sendBinary(String name, ByteData message) { + final _PlatformMessageHandler handler = _mockHandlers[name]; + if (handler != null) + return handler(message); + return _sendPlatformMessage(name, message); + } + /// Send a string message to the host application. - static Future sendString(String name, String message) { - Completer completer = new Completer(); - _sendString(name, message, (String reply) { - completer.complete(reply); - }); - return completer.future; + static Future sendString(String name, String message) async { + return _decodeUTF8(await sendBinary(name, _encodeUTF8(message))); } /// Sends a JSON-encoded message to the host application and JSON-decodes the response. - static Future sendJSON(String name, dynamic json) { - Completer completer = new Completer(); - _sendString(name, JSON.encode(json), (String reply) { - completer.complete(_decodeJSON(reply)); - }); - return completer.future; + static Future sendJSON(String name, dynamic json) async { + return _decodeJSON(await sendString(name, _encodeJSON(json))); } /// Set a callback for receiving binary messages from the platform. @@ -123,4 +129,35 @@ class PlatformMessages { return _encodeJSON(await handler(_decodeJSON(message))); }); } + + /// Sets a message handler that intercepts outgoing messages in binary form. + /// + /// The given callback will replace the currently registered callback (if any). + /// To remove the mock handler, pass `null` as the `handler` argument. + static void setMockBinaryMessageHandler(String name, Future handler(ByteData message)) { + if (handler == null) + _mockHandlers.remove(handler); + else + _mockHandlers[name] = handler; + } + + /// Sets a message handler that intercepts outgoing messages in string form. + /// + /// The given callback will replace the currently registered callback (if any). + /// To remove the mock handler, pass `null` as the `handler` argument. + static void setMockStringMessageHandler(String name, Future handler(String message)) { + setMockBinaryMessageHandler(name, (ByteData message) async { + return _encodeUTF8(await handler(_decodeUTF8(message))); + }); + } + + /// Sets a message handler that intercepts outgoing messages in JSON form. + /// + /// The given callback will replace the currently registered callback (if any). + /// To remove the mock handler, pass `null` as the `handler` argument. + static void setMockJSONMessageHandler(String name, Future handler(dynamic message)) { + setMockStringMessageHandler(name, (String message) async { + return _encodeJSON(await handler(_decodeJSON(message))); + }); + } } diff --git a/packages/flutter/test/widget/input_test.dart b/packages/flutter/test/widget/input_test.dart index 09dd082015..d7e1b2678f 100644 --- a/packages/flutter/test/widget/input_test.dart +++ b/packages/flutter/test/widget/input_test.dart @@ -7,6 +7,7 @@ import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_services/editing.dart' as mojom; import 'package:meta/meta.dart'; @@ -30,29 +31,30 @@ class MockKeyboard extends mojom.KeyboardProxy { void setEditingState(mojom.EditingState state) {} } -class MockClipboard extends mojom.ClipboardProxy { - MockClipboard() : super.unbound(); +class MockClipboard { + Object _clipboardData = { + 'text': null + }; - mojom.ClipboardData _clip; - - @override - void setClipboardData(mojom.ClipboardData clip) { - _clip = clip; - } - - @override - void getClipboardData(String format, void callback(mojom.ClipboardData clip)) { - scheduleMicrotask(() { - callback(_clip); - }); + Future handleJSONMessage(dynamic json) async { + final String method = json['method']; + final List args= json['args']; + switch (method) { + case 'Clipboard.getData': + return _clipboardData; + case 'Clipboard.setData': + _clipboardData = args[0]; + break; + } } } void main() { MockKeyboard mockKeyboard = new MockKeyboard(); serviceMocker.registerMockService(mockKeyboard); + MockClipboard mockClipboard = new MockClipboard(); - serviceMocker.registerMockService(mockClipboard); + PlatformMessages.setMockJSONMessageHandler('flutter/platform', mockClipboard.handleJSONMessage); const String kThreeLines = 'First line of text is here abcdef ghijkl mnopqrst. ' +