Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Phone: paste a picture into the message box (Dee) (#2470)

0xken15b95a1a28050e
diff --git a/mobile/lib/swarm/swarm_conversation_page.dart b/mobile/lib/swarm/swarm_conversation_page.dart
index 0bf785b45..500a205e1 100644
--- a/mobile/lib/swarm/swarm_conversation_page.dart
+++ b/mobile/lib/swarm/swarm_conversation_page.dart
@@ -36,6 +36,7 @@ import 'swarm_message_link.dart';
import 'swarm_message_receipt.dart';
import 'swarm_message_row.dart';
import 'swarm_need_rows.dart';
+import 'swarm_paste.dart';
import 'swarm_profile_sheet.dart';
import 'swarm_project_page.dart';
import 'swarm_reactions.dart';
@@ -710,6 +711,16 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>
}
}
+ /// A picture pasted into the composer ([pasteSwarmImage]), into [c]'s
+ /// files, as the + puts a photo there.
+ Future<void> _pasteImage(SwarmConversation c, SwarmPastedImage image) =>
+ pasteSwarmImage(
+ c.files,
+ image,
+ stillWanted: () => mounted && _conversation == c,
+ note: (note) => _attachNote.value = note,
+ );
+
/// How far up the chat the person reads back, past which a message that
/// arrives waits below instead of pushing up what they read.
static const _readingBack = 48.0;
@@ -1958,6 +1969,12 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>
controller: _text,
focus: _focus,
hint: hint,
+ imagePaste: swarmSendsFiles
+ ? SwarmImagePaste(
+ enabled: !c.uploading && c.files.room > 0,
+ onImage: (image) => unawaited(_pasteImage(c, image)),
+ )
+ : null,
),
),
if (_voice case final voice?)
@@ -2984,12 +3001,17 @@ class SwarmMessageField extends StatelessWidget {
required this.focus,
required this.hint,
this.fieldKey = const ValueKey('swarm-field'),
+ this.imagePaste,
});
final TextEditingController controller;
final FocusNode focus;
final String hint;
+ /// Where a picture pasted into the field goes ([swarmFieldMenu]); none
+ /// takes text alone.
+ final SwarmImagePaste? imagePaste;
+
/// The pill's key: the chat's and a thread's box are told apart by it.
final Key fieldKey;
@@ -3035,6 +3057,16 @@ class SwarmMessageField extends StatelessWidget {
enableSuggestions: ComposingKeyboard.enableSuggestions,
smartDashesType: SmartDashesType.disabled,
smartQuotesType: SmartQuotesType.disabled,
+ contextMenuBuilder: (context, field) =>
+ swarmFieldMenu(context, field, imagePaste),
+ contentInsertionConfiguration: switch (imagePaste) {
+ final paste? => ContentInsertionConfiguration(
+ allowedMimeTypes: swarmInsertedImageTypes,
+ onContentInserted: (content) =>
+ paste.onImage(SwarmPastedImage.inserted(content)),
+ ),
+ null => null,
+ },
cursorColor: AppPalette.accentOnSurface,
cursorWidth: 2,
style: swarmChatText(16, height: 22 / 16),
diff --git a/mobile/lib/swarm/swarm_new_message_sheet.dart b/mobile/lib/swarm/swarm_new_message_sheet.dart
index 24a506be2..4b6dd7256 100644
--- a/mobile/lib/swarm/swarm_new_message_sheet.dart
+++ b/mobile/lib/swarm/swarm_new_message_sheet.dart
@@ -28,6 +28,7 @@ import 'swarm_conversation_page.dart'
attachSwarmFiles;
import 'swarm_inbox.dart';
import 'swarm_outgoing_files.dart';
+import 'swarm_paste.dart';
import 'swarm_reader.dart';
import 'swarm_row.dart';
import 'swarm_session.dart';
@@ -444,6 +445,17 @@ class _SwarmNewMessageSheetState extends State<SwarmNewMessageSheet>
}
}
+ /// A picture pasted into the box ([pasteSwarmImage]), into [files], as
+ /// the + puts a photo there.
+ Future<void> _pasteImage(SwarmPastedImage image) => pasteSwarmImage(
+ files,
+ image,
+ stillWanted: () => mounted && !_closing && !_locked,
+ note: (note) {
+ if (mounted && !_closing) setState(() => _note = note);
+ },
+ );
+
/// Posts the text and the files to the chat picked, through that chat's
/// own send; or, while its post is not confirmed, sends that one again.
/// Taken, the person has read that chat through it, as posting in the chat
@@ -743,6 +755,12 @@ class _SwarmNewMessageSheetState extends State<SwarmNewMessageSheet>
? 'Write a message…'
: 'Message ${picked.title}',
fieldKey: const ValueKey('swarm-new-message-field'),
+ imagePaste: swarmSendsFiles
+ ? SwarmImagePaste(
+ enabled: !locked && files.room > 0,
+ onImage: (image) => unawaited(_pasteImage(image)),
+ )
+ : null,
),
),
),
diff --git a/mobile/lib/swarm/swarm_paste.dart b/mobile/lib/swarm/swarm_paste.dart
new file mode 100644
index 000000000..f7723f1ae
--- /dev/null
+++ b/mobile/lib/swarm/swarm_paste.dart
@@ -0,0 +1,225 @@
+import 'dart:async';
+
+import 'package:flutter/foundation.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
+
+import 'package:harness_mobile/clipboard/native_clipboard.dart';
+
+import 'swarm_outgoing_files.dart';
+
+/// What a picture pasted into a message box is called: a clipboard holds
+/// pixels, not a file (the desktop's `kPastedImageName`).
+const swarmPastedImageName = 'pasted-image.png';
+
+/// Whether the clipboard holds a picture, asked without reading it, so iOS
+/// shows no "Allow Paste" prompt and Android no toast. Replaced in tests.
+@visibleForTesting
+Future<bool> Function() swarmClipboardHasImage = NativeClipboard.hasImage;
+
+/// The clipboard's picture as PNG: null when there is none, empty when there
+/// is one the phone cannot read. Replaced in tests.
+@visibleForTesting
+Future<Uint8List?> Function() readSwarmClipboardImage =
+ NativeClipboard.readImagePng;
+
+/// How long the box's menu waits to learn whether a picture is on the
+/// clipboard before it opens without Paste for it.
+const _askLimit = Duration(milliseconds: 400);
+
+/// A picture pasted into a message box: its [name] and how to [read] its
+/// bytes. Read only once the person chose Paste.
+class SwarmPastedImage {
+ const SwarmPastedImage(this.name, this.read);
+
+ /// The clipboard's picture, from the box's Paste.
+ factory SwarmPastedImage.clipboard() =>
+ SwarmPastedImage(swarmPastedImageName, () => readSwarmClipboardImage());
+
+ /// A picture the keyboard put in — Gboard's clipboard, a sticker, a GIF.
+ factory SwarmPastedImage.inserted(KeyboardInsertedContent content) {
+ final data = content.data;
+ final extension = switch (content.mimeType) {
+ 'image/gif' => 'gif',
+ 'image/jpeg' || 'image/jpg' => 'jpg',
+ 'image/webp' => 'webp',
+ _ => 'png',
+ };
+ // No bytes is a picture the keyboard sent that could not be read.
+ return SwarmPastedImage(
+ 'pasted-image.$extension',
+ () async => data ?? Uint8List(0),
+ );
+ }
+
+ final String name;
+ final Future<Uint8List?> Function() read;
+}
+
+/// A message box that takes pasted pictures: [onImage] gets each, and the
+/// box's menu offers Paste for one only while [enabled] — when the + would
+/// take files.
+class SwarmImagePaste {
+ const SwarmImagePaste({required this.enabled, required this.onImage});
+
+ final bool enabled;
+ final ValueChanged<SwarmPastedImage> onImage;
+}
+
+/// The kinds of picture a keyboard may put in a box: those a chat shows.
+const swarmInsertedImageTypes = [
+ 'image/png',
+ 'image/jpeg',
+ 'image/jpg',
+ 'image/gif',
+ 'image/webp',
+];
+
+/// Adds [image] to [files] beside what is there, numbered as photos are
+/// (`pasted-image-2.png`…), by the same limits: ten files a message, 256 MB
+/// each. What to say under the box goes to [note]; null clears it. Nothing
+/// is added, or said, once [stillWanted] fails — the chat closed, was
+/// replaced, or access to it was lost while the clipboard was read. The
+/// bytes stay in memory only.
+Future<void> pasteSwarmImage(
+ SwarmOutgoingFiles files,
+ SwarmPastedImage image, {
+ required bool Function() stillWanted,
+ required ValueChanged<String?> note,
+}) async {
+ if (files.room <= 0) {
+ note('Attach at most $swarmAttachmentMaxFiles files.');
+ return;
+ }
+ Uint8List? bytes;
+ try {
+ bytes = await image.read();
+ } catch (_) {
+ bytes = Uint8List(0);
+ }
+ if (!stillWanted()) return;
+ if (bytes == null) {
+ note('There is no picture on the clipboard.');
+ return;
+ }
+ if (bytes.isEmpty) {
+ note("That picture isn't one this phone can read.");
+ return;
+ }
+ note(files.addNumbered([SwarmOutgoingFile(image.name, bytes)]));
+}
+
+/// The message box's long-press menu: the field's own — text Paste
+/// unchanged — and, while [paste] takes pictures and one is on the
+/// clipboard, Paste for it ("Paste image" beside a text Paste).
+Widget swarmFieldMenu(
+ BuildContext context,
+ EditableTextState field,
+ SwarmImagePaste? paste,
+) {
+ if (paste == null || !paste.enabled || field.widget.readOnly) {
+ return _fieldMenu(field, null);
+ }
+ return _ImagePasteMenu(field: field, paste: paste);
+}
+
+Widget _fieldMenu(EditableTextState field, VoidCallback? pasteImage) {
+ final textPaste = field.contextMenuButtonItems.any(
+ (item) => item.type == ContextMenuButtonType.paste,
+ );
+ final label = textPaste ? 'Paste image' : 'Paste';
+ if (SystemContextMenu.isSupportedByField(field)) {
+ final items = SystemContextMenu.getDefaultItems(field);
+ if (pasteImage != null) {
+ items.insert(
+ _afterEditing(items, (item) => item is IOSSystemContextMenuItemPaste, (
+ item,
+ ) {
+ return item is IOSSystemContextMenuItemCut ||
+ item is IOSSystemContextMenuItemCopy;
+ }),
+ IOSSystemContextMenuItemCustom(title: label, onPressed: pasteImage),
+ );
+ }
+ return SystemContextMenu.editableText(
+ editableTextState: field,
+ items: items,
+ );
+ }
+ final buttons = [...field.contextMenuButtonItems];
+ if (pasteImage != null) {
+ buttons.insert(
+ _afterEditing(
+ buttons,
+ (item) => item.type == ContextMenuButtonType.paste,
+ (item) =>
+ item.type == ContextMenuButtonType.cut ||
+ item.type == ContextMenuButtonType.copy,
+ ),
+ ContextMenuButtonItem(label: label, onPressed: pasteImage),
+ );
+ }
+ return AdaptiveTextSelectionToolbar.buttonItems(
+ anchors: field.contextMenuAnchors,
+ buttonItems: buttons,
+ );
+}
+
+/// Where a picture's Paste goes in [items]: after the text Paste, else after
+/// Cut and Copy, else first.
+int _afterEditing<T>(
+ List<T> items,
+ bool Function(T) isPaste,
+ bool Function(T) isCutOrCopy,
+) {
+ final paste = items.indexWhere(isPaste);
+ if (paste >= 0) return paste + 1;
+ return items.lastIndexWhere(isCutOrCopy) + 1;
+}
+
+/// The menu once the clipboard has said whether it holds a picture — asked as
+/// the menu opens, and for no more than [_askLimit].
+class _ImagePasteMenu extends StatefulWidget {
+ const _ImagePasteMenu({required this.field, required this.paste});
+
+ final EditableTextState field;
+ final SwarmImagePaste paste;
+
+ @override
+ State<_ImagePasteMenu> createState() => _ImagePasteMenuState();
+}
+
+class _ImagePasteMenuState extends State<_ImagePasteMenu> {
+ bool? _hasImage;
+
+ @override
+ void initState() {
+ super.initState();
+ unawaited(_ask());
+ }
+
+ Future<void> _ask() async {
+ bool has;
+ try {
+ has = await swarmClipboardHasImage().timeout(
+ _askLimit,
+ onTimeout: () => false,
+ );
+ } catch (_) {
+ has = false;
+ }
+ if (mounted) setState(() => _hasImage = has);
+ }
+
+ void _pasteImage() {
+ widget.field.hideToolbar();
+ widget.paste.onImage(SwarmPastedImage.clipboard());
+ }
+
+ @override
+ Widget build(BuildContext context) => switch (_hasImage) {
+ null => const SizedBox.shrink(),
+ true => _fieldMenu(widget.field, _pasteImage),
+ false => _fieldMenu(widget.field, null),
+ };
+}
diff --git a/mobile/lib/swarm/swarm_thread_page.dart b/mobile/lib/swarm/swarm_thread_page.dart
index 86eb37fca..1950283e3 100644
--- a/mobile/lib/swarm/swarm_thread_page.dart
+++ b/mobile/lib/swarm/swarm_thread_page.dart
@@ -16,6 +16,7 @@ import 'swarm_choices.dart';
import 'swarm_conversation.dart';
import 'swarm_conversation_page.dart';
import 'swarm_message_row.dart';
+import 'swarm_paste.dart';
import 'swarm_session.dart';
import 'swarm_thread.dart';
@@ -186,6 +187,17 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {
}
}
+ /// A picture pasted into the box ([pasteSwarmImage]), into the thread's
+ /// files, as the + puts a photo there.
+ Future<void> _pasteImage(SwarmPastedImage image) => pasteSwarmImage(
+ _thread.files,
+ image,
+ // Nothing goes into a thread the person can no longer reply in.
+ stillWanted: () =>
+ mounted && _thread.state != SwarmChatState.lost && _thread.canPost,
+ note: (note) => _attachNote.value = note,
+ );
+
/// Whether the foot is more than a screen below ([_farBack]), on every
/// scroll and whenever the thread's length changes.
bool _measured(Notification n) {
@@ -530,6 +542,12 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {
focus: _focus,
hint: 'Reply',
fieldKey: const ValueKey('swarm-thread-field'),
+ imagePaste: swarmSendsFiles
+ ? SwarmImagePaste(
+ enabled: canAttach,
+ onImage: (image) => unawaited(_pasteImage(image)),
+ )
+ : null,
),
Row(
children: [
diff --git a/mobile/test/swarm/swarm_chat_test.dart b/mobile/test/swarm/swarm_chat_test.dart
index 2051464ca..adabdb3dd 100644
--- a/mobile/test/swarm/swarm_chat_test.dart
+++ b/mobile/test/swarm/swarm_chat_test.dart
@@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:math' as math;
import 'package:dio/dio.dart' show CancelToken;
+import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
@@ -21,6 +22,7 @@ import 'package:lucide_icons_flutter/lucide_icons.dart';
import '../theme/sheet_shape.dart';
import '../voice_fakes.dart';
import 'swarm_fixture.dart';
+import 'swarm_paste_fakes.dart';
/// A Swarm chat on the phone, as Tony's prototype has it: who wrote each
/// message and in what role, follow-ups grouped, a reply's quote that brings
@@ -179,13 +181,24 @@ void main() {
String? decisionId,
bool settle = true,
bool homeC = false,
+ bool systemMenu = false,
}) async {
await tester.pumpWidget(
// The app's own theme, as the shell sets it: its field defaults are
// what the composer has to hold back.
MaterialApp(
theme: grid.buildAppTheme(brightness: Brightness.dark),
- builder: (context, child) => SwarmScope(notifier: host, child: child!),
+ builder: (context, child) {
+ final scoped = SwarmScope(notifier: host, child: child!);
+ // iOS's own edit menu, which the field shows on a phone that has it.
+ return systemMenu
+ ? MediaQuery(
+ data: MediaQuery.of(context)
+ .copyWith(supportsShowingSystemContextMenu: true),
+ child: scoped,
+ )
+ : scoped;
+ },
home: SwarmConversationPage(
swarmId: swarmId,
conversationId: conversation,
@@ -212,6 +225,7 @@ void main() {
bool settle = true,
bool homeC = false,
SwarmUpload upload = refuseSwarmUpload,
+ bool systemMenu = false,
}) async {
features = ExperimentalFeaturesStore(storage: MemoryKeyValueStore())
..value = {PhoneExperiment.swarm};
@@ -235,6 +249,7 @@ void main() {
messageId: messageId,
decisionId: decisionId,
homeC: homeC,
+ systemMenu: systemMenu,
);
return host;
}
@@ -2566,6 +2581,33 @@ void main() {
);
});
+ testWidgets('Paste in the reply box attaches the clipboard\'s picture '
+ 'to the reply, never to the chat\'s own box', (tester) async {
+ FakeSwarmClipboard(image: pngBytes()).install(tester);
+ final fake = threaded();
+ await pump(tester, fake, upload: held);
+ await tester.tap(key('swarm-thread-line-m1'));
+ await tester.pumpAndSettle();
+
+ await openFieldMenu(tester, key('swarm-thread-field'));
+ await tester.tap(
+ find.widgetWithText(TextSelectionToolbarTextButton, 'Paste'),
+ );
+ await tester.pumpAndSettle();
+ expect(inBox(key('swarm-outgoing-pasted-image.png')), findsOneWidget);
+
+ await tester.tap(key('swarm-thread-send'));
+ await tester.pump();
+ expect(uploads.single.name, 'pasted-image.png');
+ uploads.single.answer.complete(attachment('f1'));
+ await tester.pumpAndSettle();
+
+ expect(fake.commands.single['replyTo'], 'm1');
+ expect(fake.commands.single['attachmentIds'], ['f1']);
+ await back(tester);
+ expect(key('swarm-outgoing-pasted-image.png'), findsNothing);
+ });
+
testWidgets('a reply of files alone shows them while it is sent', (
tester,
) async {
@@ -3701,6 +3743,299 @@ void main() {
expect(key('swarm-read-only'), findsOneWidget);
expect(key('swarm-attach'), findsNothing);
});
+
+ group('pasting a picture', () {
+ Finder field() => find.byType(TextField);
+ Finder menuItem(String label) =>
+ find.widgetWithText(TextSelectionToolbarTextButton, label);
+
+ testWidgets('long-press Paste attaches the clipboard\'s picture as a '
+ 'chip, as a photo from the library', (tester) async {
+ FakeSwarmClipboard(image: pngBytes(2048)).install(tester);
+ await pump(tester, backend([message('m1', 1)]));
+
+ await openFieldMenu(tester, field());
+ await tester.tap(menuItem('Paste'));
+ await tester.pumpAndSettle();
+
+ expect(key('swarm-outgoing-pasted-image.png'), findsOneWidget);
+ expect(find.text('2 KB'), findsOneWidget);
+ expect(key('swarm-attach-note'), findsNothing);
+ expect(menuItem('Paste'), findsNothing, reason: 'the menu closed');
+ });
+
+ testWidgets('a Buddy chat\'s box takes a pasted picture too', (
+ tester,
+ ) async {
+ FakeSwarmClipboard(image: pngBytes()).install(tester);
+ await pump(
+ tester,
+ backend(
+ [message('m1', 1)],
+ g1: {'kind': 'buddy', 'buddyOwnerId': me},
+ ),
+ );
+
+ await openFieldMenu(tester, field());
+ await tester.tap(menuItem('Paste'));
+ await tester.pumpAndSettle();
+
+ expect(key('swarm-outgoing-pasted-image.png'), findsOneWidget);
+ });
+
+ testWidgets('several pastes are numbered beside the photos, never '
+ 'swapped in', (tester) async {
+ FakeSwarmClipboard(image: pngBytes()).install(tester);
+ await pump(tester, backend([message('m1', 1)]));
+
+ for (var i = 0; i < 3; i++) {
+ await openFieldMenu(tester, field());
+ await tester.tap(menuItem('Paste'));
+ await tester.pumpAndSettle();
+ }
+
+ expect(key('swarm-outgoing-pasted-image.png'), findsOneWidget);
+ expect(key('swarm-outgoing-pasted-image-2.png'), findsOneWidget);
+ expect(key('swarm-outgoing-pasted-image-3.png'), findsOneWidget);
+ });
+
+ testWidgets('a picture over 256 MB says why and is not attached', (
+ tester,
+ ) async {
+ FakeSwarmClipboard(image: Uint8List(256 * 1024 * 1024 + 1))
+ .install(tester);
+ await pump(tester, backend([message('m1', 1)]));
+
+ await openFieldMenu(tester, field());
+ await tester.tap(menuItem('Paste'));
+ await tester.pumpAndSettle();
+
+ expect(find.text('pasted-image.png is over 256 MB.'), findsOneWidget);
+ expect(key('swarm-outgoing-pasted-image.png'), findsNothing);
+ });
+
+ testWidgets('a picture the phone cannot read says so', (tester) async {
+ FakeSwarmClipboard(image: Uint8List(0)).install(tester);
+ await pump(tester, backend([message('m1', 1)]));
+
+ await openFieldMenu(tester, field());
+ await tester.tap(menuItem('Paste'));
+ await tester.pumpAndSettle();
+
+ expect(
+ find.text("That picture isn't one this phone can read."),
+ findsOneWidget,
+ );
+ expect(key('swarm-outgoing-pasted-image.png'), findsNothing);
+ });
+
+ testWidgets('text on the clipboard pastes as before, and the picture '
+ 'is never read', (tester) async {
+ final clipboard = FakeSwarmClipboard(text: 'Ship it')..install(tester);
+ await pump(tester, backend([message('m1', 1)]));
+
+ await openFieldMenu(tester, field());
+ expect(menuItem('Paste image'), findsNothing);
+ await tester.tap(menuItem('Paste'));
+ await tester.pumpAndSettle();
+
+ expect(tester.widget<TextField>(field()).controller!.text, 'Ship it');
+ expect(clipboard.reads, 0);
+ expect(find.byKey(const ValueKey('swarm-outgoing')), findsNothing);
+ });
+
+ testWidgets('text and a picture both there: Paste is the text, Paste '
+ 'image beside it the picture', (tester) async {
+ final clipboard = FakeSwarmClipboard(image: pngBytes(), text: 'Hi')
+ ..install(tester);
+ await pump(tester, backend([message('m1', 1)]));
+
+ await openFieldMenu(tester, field());
+ await tester.tap(menuItem('Paste'));
+ await tester.pumpAndSettle();
+ expect(tester.widget<TextField>(field()).controller!.text, 'Hi');
+ expect(clipboard.reads, 0);
+
+ await openFieldMenu(tester, field());
+ await tester.tap(menuItem('Paste image'));
+ await tester.pumpAndSettle();
+ expect(key('swarm-outgoing-pasted-image.png'), findsOneWidget);
+ expect(tester.widget<TextField>(field()).controller!.text, 'Hi');
+ });
+
+ testWidgets('nothing but text is offered while no picture is there', (
+ tester,
+ ) async {
+ final clipboard = FakeSwarmClipboard()..install(tester);
+ await pump(tester, backend([message('m1', 1)]));
+ await tester.enterText(field(), 'draft');
+ await tester.pump();
+
+ await openFieldMenu(tester, field());
+
+ expect(menuItem('Paste'), findsNothing);
+ expect(menuItem('Paste image'), findsNothing);
+ expect(clipboard.reads, 0);
+ });
+
+ testWidgets('with ten files waiting, Paste offers no picture', (
+ tester,
+ ) async {
+ pickSwarmFiles = () async => [
+ for (var i = 0; i < 10; i++) picked('f$i.txt', 3),
+ ];
+ final clipboard = FakeSwarmClipboard(image: pngBytes())
+ ..install(tester);
+ await pump(tester, backend([message('m1', 1)]));
+ await attach(tester, 'swarm-attach-files');
+
+ await openFieldMenu(tester, field());
+
+ expect(menuItem('Paste'), findsNothing);
+ expect(clipboard.reads, 0);
+ });
+
+ testWidgets('a paste while the files go up waits for the next message: '
+ 'the post carries only its own files', (tester) async {
+ pickSwarmFiles = () async => [picked('crash.log', 12)];
+ final clipboard = FakeSwarmClipboard(image: pngBytes())
+ ..hold = true
+ ..install(tester);
+ final fake = backend([message('m1', 1)]);
+ await pump(tester, fake, upload: held);
+ await attach(tester, 'swarm-attach-files');
+
+ // Paste tapped; the clipboard answers only once the upload is on.
+ await openFieldMenu(tester, field());
+ await tester.tap(menuItem('Paste'));
+ await tester.pump();
+ await tester.tap(key('swarm-send'));
+ await tester.pump();
+ expect(uploads.single.name, 'crash.log');
+
+ await openFieldMenu(tester, field());
+ expect(
+ menuItem('Paste'),
+ findsNothing,
+ reason: 'off while files go up, as the + is',
+ );
+ clipboard.release();
+ await tester.pump();
+ await tester.pump();
+ expect(key('swarm-outgoing-pasted-image.png'), findsOneWidget);
+
+ uploads.single.answer.complete(attachment('f1'));
+ await tester.pumpAndSettle();
+
+ expect(uploads, hasLength(1), reason: 'the picture did not go up');
+ expect(fake.commands.single['attachmentIds'], ['f1']);
+ expect(key('swarm-outgoing-crash.log'), findsNothing);
+ expect(key('swarm-outgoing-pasted-image.png'), findsOneWidget);
+ });
+
+ testWidgets('signed out while the clipboard is read, nothing is added '
+ 'to any chat', (tester) async {
+ final clipboard = FakeSwarmClipboard(image: pngBytes())
+ ..hold = true
+ ..install(tester);
+ final host = await pump(tester, backend([message('m1', 1)]));
+
+ await openFieldMenu(tester, field());
+ await tester.tap(menuItem('Paste'));
+ await tester.pump();
+ account.value = null;
+ await frames(tester);
+ account.value = me;
+ await host.session!.inbox.refresh();
+ await tester.pumpAndSettle();
+ clipboard.release();
+ await tester.pumpAndSettle();
+
+ expect(tester.takeException(), isNull);
+ expect(key('swarm-outgoing-pasted-image.png'), findsNothing);
+ expect(key('swarm-attach-note'), findsNothing);
+ });
+
+ testWidgets('a picture the keyboard puts in is attached the same way', (
+ tester,
+ ) async {
+ FakeSwarmClipboard().install(tester);
+ await pump(tester, backend([message('m1', 1)]));
+ await tester.tap(field());
+ await tester.pump();
+
+ final editable = tester.state<EditableTextState>(
+ find.byType(EditableText),
+ );
+ expect(
+ editable.textInputConfiguration.allowedMimeTypes,
+ contains('image/gif'),
+ );
+ editable.insertContent(
+ KeyboardInsertedContent(
+ mimeType: 'image/gif',
+ uri: 'content://keyboard/1',
+ data: pngBytes(),
+ ),
+ );
+ await tester.pumpAndSettle();
+
+ expect(key('swarm-outgoing-pasted-image.gif'), findsOneWidget);
+ });
+
+ testWidgets('on iOS the system menu carries Paste for the picture', (
+ tester,
+ ) async {
+ debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
+ final shown = <List<Object?>>[];
+ FakeSwarmClipboard(image: pngBytes()).install(tester);
+ final messenger = tester.binding.defaultBinaryMessenger;
+ messenger.setMockMethodCallHandler(SystemChannels.platform, (
+ call,
+ ) async {
+ if (call.method == 'ContextMenu.showSystemContextMenu') {
+ shown.add(
+ ((call.arguments as Map)['items'] as List).cast<Object?>(),
+ );
+ }
+ return null;
+ });
+ try {
+ await pump(tester, backend([message('m1', 1)]), systemMenu: true);
+ await tester.tap(field());
+ await tester.pump();
+ tester
+ .state<EditableTextState>(find.byType(EditableText))
+ .showToolbar();
+ await tester.pumpAndSettle();
+
+ final menu = tester.widget<SystemContextMenu>(
+ find.byType(SystemContextMenu),
+ );
+ final paste = menu.items
+ .whereType<IOSSystemContextMenuItemCustom>()
+ .single;
+ expect(paste.title, 'Paste');
+ expect(shown, isNotEmpty);
+
+ await messenger.handlePlatformMessage(
+ 'flutter/platform',
+ const JSONMethodCodec().encodeMethodCall(
+ MethodCall('ContextMenu.onPerformCustomAction', [
+ 0,
+ paste.hashCode.toString(),
+ ]),
+ ),
+ (_) {},
+ );
+ await tester.pumpAndSettle();
+
+ expect(key('swarm-outgoing-pasted-image.png'), findsOneWidget);
+ } finally {
+ debugDefaultTargetPlatformOverride = null;
+ }
+ });
+ });
});
group('the delivery line under the newest own message', () {
final queued = {'id': 'd1', 'agentId': 'agent-lead', 'status': 'queued'};
diff --git a/mobile/test/swarm/swarm_new_message_test.dart b/mobile/test/swarm/swarm_new_message_test.dart
index 68d6282dc..d716ac9d5 100644
--- a/mobile/test/swarm/swarm_new_message_test.dart
+++ b/mobile/test/swarm/swarm_new_message_test.dart
@@ -24,6 +24,7 @@ import 'package:harness_mobile/swarm/swarm_session.dart';
import '../voice_fakes.dart';
import 'swarm_fixture.dart';
+import 'swarm_paste_fakes.dart';
/// New message on Swarm Home (Tony, 10-30: as Slack Mobile starts one): the
/// header's compose is gone, a round + floats at Home's bottom right, and it
@@ -766,6 +767,33 @@ void main() {
expectHomeCProject('Web');
});
+ testWidgets('Paste attaches the clipboard\'s picture, which goes with '
+ 'the message to the chat picked', (tester) async {
+ FakeSwarmClipboard(image: pngBytes()).install(tester);
+ final uploads = FakeUploads();
+ final fake = backend();
+ await pump(tester, fake, upload: uploads.upload);
+ await open(tester);
+ await tester.tap(row(webLead));
+ await tester.pumpAndSettle();
+
+ await openFieldMenu(tester, key('swarm-new-message-field'));
+ await tester.tap(
+ find.widgetWithText(TextSelectionToolbarTextButton, 'Paste'),
+ );
+ await tester.pumpAndSettle();
+ expect(key('swarm-outgoing-pasted-image.png'), findsOneWidget);
+
+ await tester.tap(key('swarm-new-message-send'));
+ await tester.pumpAndSettle();
+
+ expect(uploads.names, ['pasted-image.png']);
+ expect(uploads.asked.single.conversationId, webLead);
+ expect(sends(fake).single['command']['attachmentIds'], [
+ 'id-pasted-image.png',
+ ]);
+ });
+
testWidgets('+ attaches files, which go up to the chat picked and with '
'its message', (tester) async {
final uploads = FakeUploads();
diff --git a/mobile/test/swarm/swarm_paste_fakes.dart b/mobile/test/swarm/swarm_paste_fakes.dart
new file mode 100644
index 000000000..73059ebc8
--- /dev/null
+++ b/mobile/test/swarm/swarm_paste_fakes.dart
@@ -0,0 +1,67 @@
+import 'dart:async';
+
+import 'package:flutter/services.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/swarm/swarm_paste.dart';
+
+/// The phone's clipboard as a message box's Paste sees it, for one test: a
+/// picture ([image], none when null) beside [text]. With [hold] set, a read
+/// of the picture waits until [release].
+class FakeSwarmClipboard {
+ FakeSwarmClipboard({this.image, this.text});
+
+ Uint8List? image;
+ String? text;
+ bool hold = false;
+
+ /// How many times the picture was read (a read is what asks iOS's "Allow
+ /// Paste").
+ int reads = 0;
+ final _held = <Completer<Uint8List?>>[];
+
+ /// Answers the reads waiting, with what [image] is now.
+ void release() {
+ for (final read in _held) {
+ read.complete(image);
+ }
+ _held.clear();
+ }
+
+ /// Puts this clipboard in place of the phone's until the test ends.
+ void install(WidgetTester tester) {
+ final hasImage = swarmClipboardHasImage;
+ final readImage = readSwarmClipboardImage;
+ swarmClipboardHasImage = () async => image != null;
+ readSwarmClipboardImage = () {
+ reads++;
+ if (!hold) return Future.value(image);
+ final read = Completer<Uint8List?>();
+ _held.add(read);
+ return read.future;
+ };
+ final messenger = tester.binding.defaultBinaryMessenger;
+ messenger.setMockMethodCallHandler(SystemChannels.platform, (call) async {
+ switch (call.method) {
+ case 'Clipboard.hasStrings':
+ return {'value': text != null && text!.isNotEmpty};
+ case 'Clipboard.getData':
+ return text == null ? null : {'text': text};
+ }
+ return null;
+ });
+ addTearDown(() {
+ swarmClipboardHasImage = hasImage;
+ readSwarmClipboardImage = readImage;
+ messenger.setMockMethodCallHandler(SystemChannels.platform, null);
+ });
+ }
+}
+
+/// A few bytes that stand for a PNG: nothing here decodes them.
+Uint8List pngBytes([int size = 64]) => Uint8List(size)..[0] = 0x89;
+
+/// Long-presses [field] and lets its menu ask the clipboard and open.
+Future<void> openFieldMenu(WidgetTester tester, Finder field) async {
+ await tester.longPress(field);
+ await tester.pumpAndSettle();
+}
diff --git a/mobile/test/swarm/swarm_paste_test.dart b/mobile/test/swarm/swarm_paste_test.dart
new file mode 100644
index 000000000..8df88dc93
--- /dev/null
+++ b/mobile/test/swarm/swarm_paste_test.dart
@@ -0,0 +1,161 @@
+import 'dart:async';
+
+import 'package:flutter/services.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/swarm/swarm_outgoing_files.dart';
+import 'package:harness_mobile/swarm/swarm_paste.dart';
+
+/// A picture pasted into a message box goes into its files by the photos'
+/// own limits, and only into the box it was pasted in.
+void main() {
+ Uint8List bytes(int size) => Uint8List(size);
+
+ Future<String?> paste(
+ SwarmOutgoingFiles files,
+ SwarmPastedImage image, {
+ bool Function()? stillWanted,
+ }) async {
+ String? said = 'never said';
+ await pasteSwarmImage(
+ files,
+ image,
+ stillWanted: stillWanted ?? () => true,
+ note: (note) => said = note,
+ );
+ return said;
+ }
+
+ test('a box closed while the clipboard was read takes nothing and says '
+ 'nothing', () async {
+ final files = SwarmOutgoingFiles();
+ final answer = Completer<Uint8List?>();
+ var wanted = true;
+ final done = paste(
+ files,
+ SwarmPastedImage('pasted-image.png', () => answer.future),
+ stillWanted: () => wanted,
+ );
+ wanted = false;
+ answer.complete(bytes(10));
+
+ expect(await done, 'never said');
+ expect(files.files, isEmpty);
+ });
+
+ test('a box with ten files says so and never reads the clipboard', () async {
+ final files = SwarmOutgoingFiles()
+ ..add([for (var i = 0; i < 10; i++) SwarmOutgoingFile('f$i', bytes(1))]);
+ var reads = 0;
+
+ final said = await paste(
+ files,
+ SwarmPastedImage('pasted-image.png', () async {
+ reads++;
+ return bytes(10);
+ }),
+ );
+
+ expect(said, 'Attach at most 10 files.');
+ expect(reads, 0);
+ expect(files.files, hasLength(10));
+ });
+
+ test('a box filled while the clipboard was read keeps its ten', () async {
+ final files = SwarmOutgoingFiles();
+ final answer = Completer<Uint8List?>();
+ final done = paste(
+ files,
+ SwarmPastedImage('pasted-image.png', () => answer.future),
+ );
+ files.add([
+ for (var i = 0; i < 10; i++) SwarmOutgoingFile('f$i', bytes(1)),
+ ]);
+ answer.complete(bytes(10));
+
+ expect(await done, 'Attach at most 10 files.');
+ expect(files.files.map((f) => f.name), isNot(contains('pasted-image.png')));
+ });
+
+ test(
+ 'a picture at exactly 256 MB is attached; one byte more is not',
+ () async {
+ final files = SwarmOutgoingFiles();
+ expect(
+ await paste(
+ files,
+ SwarmPastedImage(
+ 'pasted-image.png',
+ () async => bytes(swarmAttachmentMaxBytes),
+ ),
+ ),
+ isNull,
+ );
+ expect(
+ await paste(
+ files,
+ SwarmPastedImage(
+ 'pasted-image.png',
+ () async => bytes(swarmAttachmentMaxBytes + 1),
+ ),
+ ),
+ 'pasted-image-2.png is over 256 MB.',
+ );
+ expect(files.files.map((f) => f.name), ['pasted-image.png']);
+ },
+ );
+
+ test('a read that fails says the picture cannot be read', () async {
+ final files = SwarmOutgoingFiles();
+ final said = await paste(
+ files,
+ SwarmPastedImage(
+ 'pasted-image.png',
+ () async => throw PlatformException(code: 'gone'),
+ ),
+ );
+ expect(said, "That picture isn't one this phone can read.");
+ expect(files.files, isEmpty);
+ });
+
+ test('nothing on the clipboard by the time it is read says so', () async {
+ final files = SwarmOutgoingFiles();
+ final said = await paste(
+ files,
+ SwarmPastedImage('pasted-image.png', () async => null),
+ );
+ expect(said, 'There is no picture on the clipboard.');
+ });
+
+ test('a keyboard\'s picture keeps its kind in its name; one without bytes '
+ 'cannot be read', () async {
+ KeyboardInsertedContent inserted(String mime, [Uint8List? data]) =>
+ KeyboardInsertedContent(
+ mimeType: mime,
+ uri: 'content://k/1',
+ data: data,
+ );
+ expect(
+ SwarmPastedImage.inserted(inserted('image/gif')).name,
+ 'pasted-image.gif',
+ );
+ expect(
+ SwarmPastedImage.inserted(inserted('image/jpeg')).name,
+ 'pasted-image.jpg',
+ );
+ expect(
+ SwarmPastedImage.inserted(inserted('image/webp')).name,
+ 'pasted-image.webp',
+ );
+ expect(
+ SwarmPastedImage.inserted(inserted('image/png')).name,
+ 'pasted-image.png',
+ );
+
+ final files = SwarmOutgoingFiles();
+ expect(
+ await paste(files, SwarmPastedImage.inserted(inserted('image/png'))),
+ "That picture isn't one this phone can read.",
+ );
+ expect(files.files, isEmpty);
+ });
+}