Paste files and images into the chat composer on macOS
Native hosts reported clipboard-file paste as unsupported, and Flutter's Clipboard carries text only, so Cmd+V of a Finder file or a screenshot in the macOS app pasted nothing (or the file name). The macOS Runner now answers a pipelines/clipboard_files channel with the pasteboard's file URLs, else its image as PNG; the composer's Cmd+V reads it and attaches the files, falling back to the pasteboard text when there are none. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_composer_bar.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_composer_controller.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_clipboard_paste.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_clipboard_paste_native.dart';
|
||||
|
||||
import '../conversation/chat_composer_bar_fixtures.dart';
|
||||
|
||||
final _macOS = TargetPlatformVariant.only(TargetPlatform.macOS);
|
||||
|
||||
Finder get _field => find.byKey(const Key('chat-composer-field'));
|
||||
|
||||
void main() {
|
||||
late ComposerBarGateway gateway;
|
||||
late ChatComposerController composer;
|
||||
late Map<String, Object?> pasteboard;
|
||||
late int fileReads, textReads;
|
||||
|
||||
setUp(() {
|
||||
gateway = ComposerBarGateway();
|
||||
composer = ChatComposerController(
|
||||
gateway,
|
||||
's',
|
||||
drafts: MemoryChatDraftStore(),
|
||||
);
|
||||
pasteboard = {'files': const [], 'errors': const []};
|
||||
fileReads = 0;
|
||||
textReads = 0;
|
||||
final messenger =
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
||||
messenger.setMockMethodCallHandler(chatClipboardFilesChannel, (call) async {
|
||||
expect(call.method, 'read');
|
||||
fileReads++;
|
||||
return pasteboard;
|
||||
});
|
||||
messenger.setMockMethodCallHandler(SystemChannels.platform, (call) async {
|
||||
if (call.method == 'Clipboard.getData') {
|
||||
textReads++;
|
||||
return {'text': 'copied text'};
|
||||
}
|
||||
if (call.method == 'Clipboard.hasStrings') return {'value': true};
|
||||
return null;
|
||||
});
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
composer.dispose();
|
||||
gateway.close();
|
||||
final messenger =
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
||||
messenger.setMockMethodCallHandler(chatClipboardFilesChannel, null);
|
||||
messenger.setMockMethodCallHandler(SystemChannels.platform, null);
|
||||
});
|
||||
|
||||
// Production wiring: no injected paste, so the composer takes the host's.
|
||||
Future<void> mount(WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
materialHost(ChatComposerBar(composer: composer, running: false)),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
Future<void> paste(
|
||||
WidgetTester tester, {
|
||||
LogicalKeyboardKey modifier = LogicalKeyboardKey.metaLeft,
|
||||
}) async {
|
||||
await tester.sendKeyDownEvent(modifier);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyV);
|
||||
await tester.sendKeyUpEvent(modifier);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
Future<void> done(WidgetTester tester) => tester.pumpWidget(const SizedBox());
|
||||
|
||||
test('only the macOS host reads clipboard files natively', () {
|
||||
expect(chatHostReadsClipboardFiles(TargetPlatform.macOS), isTrue);
|
||||
for (final platform in [
|
||||
TargetPlatform.linux,
|
||||
TargetPlatform.windows,
|
||||
TargetPlatform.android,
|
||||
TargetPlatform.iOS,
|
||||
]) {
|
||||
expect(chatHostReadsClipboardFiles(platform), isFalse);
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('the macOS host supports clipboard-file paste', (tester) async {
|
||||
expect(hostChatClipboardPaste.isSupported, isTrue);
|
||||
}, variant: _macOS);
|
||||
|
||||
for (final focused in [true, false]) {
|
||||
testWidgets(
|
||||
'Cmd+V attaches a Finder file and a screenshot '
|
||||
'(${focused ? 'focused' : 'unfocused'} composer)',
|
||||
(tester) async {
|
||||
pasteboard = {
|
||||
'files': [
|
||||
{
|
||||
'name': 'report.pdf',
|
||||
'mime': 'application/pdf',
|
||||
'bytes': Uint8List.fromList([1, 2]),
|
||||
},
|
||||
{
|
||||
'name': '',
|
||||
'mime': 'image/png',
|
||||
'bytes': Uint8List.fromList([3]),
|
||||
},
|
||||
],
|
||||
'errors': const [],
|
||||
};
|
||||
await mount(tester);
|
||||
if (focused) {
|
||||
await tester.showKeyboard(_field);
|
||||
} else {
|
||||
FocusManager.instance.primaryFocus?.unfocus();
|
||||
}
|
||||
await tester.pump();
|
||||
await paste(tester);
|
||||
|
||||
expect(fileReads, 1);
|
||||
expect(find.text('report.pdf'), findsOneWidget);
|
||||
expect(find.text('pasted-1.png'), findsOneWidget);
|
||||
// The pasteboard's file-name text is not pasted alongside the files.
|
||||
expect(textReads, 0);
|
||||
expect(composer.draft, isEmpty);
|
||||
await done(tester);
|
||||
},
|
||||
variant: _macOS,
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets('Cmd+V of plain text pastes it once into the focused composer', (
|
||||
tester,
|
||||
) async {
|
||||
await mount(tester);
|
||||
await tester.showKeyboard(_field);
|
||||
await paste(tester);
|
||||
|
||||
expect(fileReads, 1);
|
||||
expect(textReads, 1);
|
||||
expect(composer.draft, 'copied text');
|
||||
await done(tester);
|
||||
}, variant: _macOS);
|
||||
|
||||
testWidgets('an unreadable pasted file is reported, not dropped silently', (
|
||||
tester,
|
||||
) async {
|
||||
pasteboard = {
|
||||
'files': const [],
|
||||
'errors': ['Downloads is a folder; paste its files instead.'],
|
||||
};
|
||||
await mount(tester);
|
||||
await tester.showKeyboard(_field);
|
||||
await paste(tester);
|
||||
|
||||
expect(
|
||||
find.text('Downloads is a folder; paste its files instead.'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(composer.draft, isEmpty);
|
||||
await done(tester);
|
||||
}, variant: _macOS);
|
||||
|
||||
testWidgets('Ctrl+V is not the macOS paste shortcut', (tester) async {
|
||||
await mount(tester);
|
||||
FocusManager.instance.primaryFocus?.unfocus();
|
||||
await tester.pump();
|
||||
await paste(tester, modifier: LogicalKeyboardKey.controlLeft);
|
||||
|
||||
expect(fileReads, 0);
|
||||
expect(composer.draft, isEmpty);
|
||||
await done(tester);
|
||||
}, variant: _macOS);
|
||||
}
|
||||
Reference in New Issue
Block a user