import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:xterm/xterm.dart'; /// A TerminalView placed away from the window's origin, so a report that /// used window coordinates instead of the view's own would be wrong. class _Harness { _Harness(this.tester); final WidgetTester tester; final terminal = Terminal(); final controller = TerminalController(); final replies = []; Future pump() async { terminal.onOutput = replies.add; await tester.pumpWidget( MaterialApp( home: Scaffold( body: Padding( padding: const EdgeInsets.only(left: 100, top: 60), child: SizedBox( width: 400, height: 200, child: TerminalView( terminal, controller: controller, hardwareKeyboardOnly: true, ), ), ), ), ), ); } TerminalViewState get _view => tester.state(find.byType(TerminalView)); /// The global centre of zero-based screen cell ([x], [y]). Offset cell(int x, int y) { final render = _view.renderTerminal; final size = render.cellSize; return render.localToGlobal( Offset((x + 0.5) * size.width, (y + 0.5) * size.height), ); } double get lineHeight => _view.renderTerminal.lineHeight; String take() { final all = replies.join(); replies.clear(); return all; } } void main() { testWidgets('a left drag with 1002 is reported, not selected', ( tester, ) async { final h = _Harness(tester); await h.pump(); h.terminal.write('\x1b[?1002h\x1b[?1006h'); await tester.pump(); final gesture = await tester.startGesture( h.cell(1, 1), kind: PointerDeviceKind.mouse, ); await gesture.moveTo(h.cell(4, 1)); await gesture.moveTo(h.cell(6, 2)); await gesture.up(); await tester.pumpAndSettle(); expect(h.take(), '\x1b[<0;2;2M\x1b[<32;5;2M\x1b[<32;7;3M\x1b[<0;7;3m'); expect(h.controller.selection, isNull); }); testWidgets('1003 reports plain motion; 1000 does not', (tester) async { final h = _Harness(tester); await h.pump(); h.terminal.write('\x1b[?1003h\x1b[?1006h'); final gesture = await tester.createGesture(kind: PointerDeviceKind.mouse); await gesture.addPointer(location: h.cell(0, 0)); await gesture.moveTo(h.cell(3, 2)); expect(h.take(), '\x1b[<35;4;3M'); h.terminal.write('\x1b[?1003l\x1b[?1000h'); await gesture.moveTo(h.cell(5, 2)); expect(h.take(), ''); await gesture.removePointer(); }); testWidgets('a middle click is reported as middle press and release', ( tester, ) async { final h = _Harness(tester); await h.pump(); h.terminal.write('\x1b[?1000h\x1b[?1006h'); final gesture = await tester.startGesture( h.cell(2, 0), kind: PointerDeviceKind.mouse, buttons: kMiddleMouseButton, ); await gesture.up(); await tester.pumpAndSettle(); expect(h.take(), '\x1b[<1;3;1M\x1b[<1;3;1m'); }); testWidgets('Shift+drag selects text even while the program has the mouse', ( tester, ) async { final h = _Harness(tester); await h.pump(); h.terminal.write('hello world\x1b[?1002h\x1b[?1006h'); await tester.pump(); await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft); final gesture = await tester.startGesture( h.cell(0, 0), kind: PointerDeviceKind.mouse, ); await gesture.moveTo(h.cell(2, 0)); await gesture.moveTo(h.cell(4, 0)); await gesture.up(); await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft); await tester.pumpAndSettle(); expect(h.take(), ''); final selection = h.controller.selection; expect(selection, isNotNull); expect(h.terminal.buffer.getText(selection), startsWith('hell')); }); testWidgets('the wheel reports 65 down / 64 up at the view cell', ( tester, ) async { final h = _Harness(tester); await h.pump(); h.terminal.write('\x1b[?1049h\x1b[?1000h\x1b[?1006h'); await tester.pump(); final pointer = TestPointer(1, PointerDeviceKind.mouse); final at = h.cell(3, 1); await tester.sendEventToBinding(pointer.hover(at)); await tester.sendEventToBinding( pointer.scroll(Offset(0, h.lineHeight * 2)), ); await tester.pump(); expect(h.take(), '\x1b[<65;4;2M\x1b[<65;4;2M'); await tester.sendEventToBinding(pointer.scroll(Offset(0, -h.lineHeight))); await tester.pump(); expect(h.take(), '\x1b[<64;4;2M'); }); }