Add hover copy actions for fenced and inline chat code
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart';
|
||||
|
||||
Widget _host(String text, {bool cupertino = false}) {
|
||||
final child = SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(60),
|
||||
child: ChatMarkdownView(text: text),
|
||||
),
|
||||
);
|
||||
return cupertino
|
||||
? CupertinoApp(home: CupertinoPageScaffold(child: child))
|
||||
: MaterialApp(home: Scaffold(body: child));
|
||||
}
|
||||
|
||||
Finder _paragraph(String prefix) => find.byWidgetPredicate(
|
||||
(widget) =>
|
||||
widget is RichText && widget.text.toPlainText().startsWith(prefix),
|
||||
);
|
||||
|
||||
Offset _runPoint(WidgetTester tester, String paragraph, String run) {
|
||||
final render = tester.renderObject<RenderParagraph>(
|
||||
_paragraph(paragraph).first,
|
||||
);
|
||||
final index = render.text.toPlainText().indexOf(run);
|
||||
final boxes = render.getBoxesForSelection(
|
||||
TextSelection(baseOffset: index, extentOffset: index + run.length),
|
||||
);
|
||||
return render.localToGlobal(boxes.first.toRect().center);
|
||||
}
|
||||
|
||||
void main() {
|
||||
String? copied;
|
||||
setUp(() {
|
||||
copied = null;
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(SystemChannels.platform, (call) async {
|
||||
if (call.method == 'Clipboard.setData') {
|
||||
copied = (call.arguments as Map)['text'] as String;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
});
|
||||
tearDown(
|
||||
() => TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(SystemChannels.platform, null),
|
||||
);
|
||||
|
||||
for (final cupertino in [false, true]) {
|
||||
testWidgets(
|
||||
'fenced code reveals Copy without shifting and copies exact whitespace ($cupertino)',
|
||||
(tester) async {
|
||||
const code = ' final x = 1;\n\tkeep spaces ';
|
||||
await tester.pumpWidget(
|
||||
_host('```dart\n$code\n```', cupertino: cupertino),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
final before = tester.getTopLeft(find.text(code));
|
||||
expect(find.text('Copy').hitTestable(), findsNothing);
|
||||
final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse);
|
||||
await mouse.addPointer(location: const Offset(1, 1));
|
||||
await mouse.moveTo(tester.getCenter(find.text(code)));
|
||||
await tester.pump();
|
||||
expect(find.text('Copy').hitTestable(), findsOneWidget);
|
||||
expect(tester.getTopLeft(find.text(code)), before);
|
||||
await tester.tap(find.text('Copy').hitTestable());
|
||||
await tester.pump();
|
||||
expect(copied, code);
|
||||
expect(find.text('Copied').hitTestable(), findsOneWidget);
|
||||
await mouse.moveTo(const Offset(1, 1));
|
||||
await tester.pump();
|
||||
expect(find.text('Copied').hitTestable(), findsNothing);
|
||||
await mouse.removePointer();
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets(
|
||||
'inline spans retain selection, hover transfer and copy the right raw snippet',
|
||||
(tester) async {
|
||||
const text = 'First ` alpha beta ` then `gamma` end.';
|
||||
await tester.pumpWidget(_host(text));
|
||||
await tester.pumpAndSettle();
|
||||
final rich = tester.widget<RichText>(_paragraph('First ').first);
|
||||
void assertSpans(InlineSpan span) {
|
||||
expect(span, isA<TextSpan>());
|
||||
for (final child in (span as TextSpan).children ?? <InlineSpan>[]) {
|
||||
assertSpans(child);
|
||||
}
|
||||
}
|
||||
|
||||
assertSpans(rich.text);
|
||||
final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse);
|
||||
await mouse.addPointer(location: const Offset(1, 1));
|
||||
await mouse.moveTo(_runPoint(tester, 'First ', ' alpha beta '));
|
||||
await tester.pump();
|
||||
expect(find.text('Copy').hitTestable(), findsOneWidget);
|
||||
final popup = tester.getRect(find.byType(TextButton));
|
||||
final paragraph = tester.getRect(_paragraph('First ').first);
|
||||
expect(popup.overlaps(paragraph), isFalse);
|
||||
await mouse.moveTo(tester.getCenter(find.text('Copy').hitTestable()));
|
||||
await tester.pump(const Duration(milliseconds: 250));
|
||||
expect(find.text('Copy').hitTestable(), findsOneWidget);
|
||||
await tester.tap(find.text('Copy').hitTestable());
|
||||
await tester.pump();
|
||||
expect(copied, ' alpha beta ');
|
||||
await mouse.moveTo(_runPoint(tester, 'First ', 'gamma'));
|
||||
await tester.pump();
|
||||
await tester.tap(find.text('Copy').hitTestable());
|
||||
await tester.pump();
|
||||
expect(copied, 'gamma');
|
||||
await mouse.moveTo(const Offset(1, 1));
|
||||
await tester.pump(const Duration(milliseconds: 200));
|
||||
expect(find.text('Copy'), findsNothing);
|
||||
expect(find.text('Copied'), findsNothing);
|
||||
await mouse.removePointer();
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('inline overlay disappears on replacement, scroll and disposal', (
|
||||
tester,
|
||||
) async {
|
||||
final text =
|
||||
'Before `snippet` after.\n\n${List.filled(100, 'More').join('\n\n')}';
|
||||
await tester.pumpWidget(_host(text));
|
||||
final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse);
|
||||
await mouse.addPointer(location: const Offset(1, 1));
|
||||
await mouse.moveTo(_runPoint(tester, 'Before ', 'snippet'));
|
||||
await tester.pump();
|
||||
expect(find.text('Copy'), findsOneWidget);
|
||||
final position = tester
|
||||
.state<ScrollableState>(find.byType(Scrollable).first)
|
||||
.position;
|
||||
position.jumpTo(50);
|
||||
await tester.pump();
|
||||
expect(find.text('Copy'), findsNothing);
|
||||
await mouse.moveTo(const Offset(1, 1));
|
||||
position.jumpTo(0);
|
||||
await tester.pump();
|
||||
await mouse.moveTo(_runPoint(tester, 'Before ', 'snippet'));
|
||||
await tester.pump();
|
||||
await tester.pumpWidget(_host('Replacement `new snippet`.'));
|
||||
await tester.pump();
|
||||
expect(find.text('Copy'), findsNothing);
|
||||
await mouse.moveTo(const Offset(1, 1));
|
||||
await mouse.moveTo(_runPoint(tester, 'Replacement ', 'new snippet'));
|
||||
await tester.pump();
|
||||
expect(find.text('Copy'), findsOneWidget);
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
expect(find.text('Copy'), findsNothing);
|
||||
await mouse.removePointer();
|
||||
});
|
||||
testWidgets('code in a link retains its inline hover action', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
_host('Read [the `inner` text](https://example.com).'),
|
||||
);
|
||||
final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse);
|
||||
await mouse.addPointer(location: const Offset(1, 1));
|
||||
await mouse.moveTo(_runPoint(tester, 'Read ', 'inner'));
|
||||
await tester.pump();
|
||||
expect(find.text('Copy').hitTestable(), findsOneWidget);
|
||||
await mouse.moveTo(tester.getCenter(find.text('Copy').hitTestable()));
|
||||
await tester.pump(const Duration(milliseconds: 250));
|
||||
await tester.tap(find.text('Copy').hitTestable());
|
||||
await tester.pump();
|
||||
expect(copied, 'inner');
|
||||
await mouse.removePointer();
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
});
|
||||
|
||||
testWidgets('keyboard focus exposes the reserved block action', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(_host('```text\nraw\n```'));
|
||||
expect(find.text('Copy').hitTestable(), findsNothing);
|
||||
// The selectable transcript itself is the first focus stop.
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
|
||||
await tester.pump();
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
|
||||
await tester.pump();
|
||||
expect(find.text('Copy').hitTestable(), findsOneWidget);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
await tester.pump();
|
||||
expect(copied, 'raw');
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user