askrobots / network-computer-flutter
Public
One Flutter client for network-computer: drive a remote Linux/Mac desktop from Android, iPhone, iPad, or desktop over WebRTC.
Dan Quellhorst
No colour (compare) in ⋯: the desk's picture in grey on this device
c74471a
1 month, 3 weeks ago
History
touch_pad_test.dart · 4.6 KB · 146 lines
| 1 | import 'package:flutter/gestures.dart'; |
| 2 | import 'package:flutter/material.dart'; |
| 3 | import 'package:flutter_test/flutter_test.dart'; |
| 4 | import 'package:network_computer/protocol.dart'; |
| 5 | import 'package:network_computer/touch_pad.dart'; |
| 6 | |
| 7 | // A 1024x768 view (a 4:3 iPad) showing a 1280x720 desk: the picture is |
| 8 | // 1024x576 with 96 px bars above and below. |
| 9 | Future<List<String>> run( |
| 10 | WidgetTester t, |
| 11 | Future<void> Function() gesture, |
| 12 | ) async { |
| 13 | final events = <String>[]; |
| 14 | await t.binding.setSurfaceSize(const Size(1024, 768)); |
| 15 | await t.pumpWidget( |
| 16 | MaterialApp( |
| 17 | home: TouchPad( |
| 18 | videoSize: () => const Size(1280, 720), |
| 19 | onEvent: (InputEvent e) { |
| 20 | final j = e.toJson(); |
| 21 | final t = j['t']; |
| 22 | if (t == 'mm') { |
| 23 | events.add( |
| 24 | 'mm ${(j['x'] as double).toStringAsFixed(2)},${(j['y'] as double).toStringAsFixed(2)}', |
| 25 | ); |
| 26 | } else if (t == 'md' || t == 'mu') { |
| 27 | events.add('$t ${j['b'] ?? 0}'); |
| 28 | } else { |
| 29 | events.add('$t'); |
| 30 | } |
| 31 | }, |
| 32 | child: const SizedBox.expand(), |
| 33 | ), |
| 34 | ), |
| 35 | ); |
| 36 | await gesture(); |
| 37 | await t.pumpAndSettle(const Duration(seconds: 1)); |
| 38 | return events; |
| 39 | } |
| 40 | |
| 41 | // the middle of the picture, and a quarter of the way down it |
| 42 | const mid = Offset(512, 96 + 288), quarter = Offset(512, 96 + 144); |
| 43 | |
| 44 | void main() { |
| 45 | testWidgets( |
| 46 | 'tap: left click where the finger is (measured on the picture, not the bars)', |
| 47 | (t) async { |
| 48 | final e = await run(t, () async { |
| 49 | await t.tapAt(quarter); |
| 50 | }); |
| 51 | expect(e, ['mm 0.50,0.25', 'md 0', 'mu 0']); |
| 52 | }, |
| 53 | ); |
| 54 | |
| 55 | testWidgets('double tap: a double click', (t) async { |
| 56 | final e = await run(t, () async { |
| 57 | await t.tapAt(mid); |
| 58 | await t.pump(const Duration(milliseconds: 80)); |
| 59 | await t.tapAt(mid); |
| 60 | }); |
| 61 | expect(e, ['mm 0.50,0.50', 'md 0', 'mu 0', 'md 0', 'mu 0']); |
| 62 | }); |
| 63 | |
| 64 | testWidgets('touch and hold, let go: right click', (t) async { |
| 65 | final e = await run(t, () async { |
| 66 | await t.longPressAt(mid); |
| 67 | }); |
| 68 | expect(e, ['mm 0.50,0.50', 'md 2', 'mu 2']); |
| 69 | }); |
| 70 | |
| 71 | testWidgets('touch and hold, then move: a drag', (t) async { |
| 72 | final e = await run(t, () async { |
| 73 | final g = await t.startGesture(quarter); |
| 74 | await t.pump(const Duration(milliseconds: 700)); |
| 75 | await g.moveBy(const Offset(0, 144)); |
| 76 | await t.pump(); |
| 77 | await g.up(); |
| 78 | }); |
| 79 | expect(e.first, 'mm 0.50,0.25'); |
| 80 | expect(e[1], 'md 0'); |
| 81 | expect(e.contains('mm 0.50,0.50'), isTrue); |
| 82 | expect(e.last, 'mu 0'); |
| 83 | }); |
| 84 | |
| 85 | testWidgets('two-finger tap: right click', (t) async { |
| 86 | final e = await run(t, () async { |
| 87 | final a = await t.startGesture(mid - const Offset(20, 0), pointer: 1); |
| 88 | final b = await t.startGesture(mid + const Offset(20, 0), pointer: 2); |
| 89 | await t.pump(const Duration(milliseconds: 50)); |
| 90 | await a.up(); |
| 91 | await b.up(); |
| 92 | }); |
| 93 | expect(e.where((x) => x.startsWith('md') || x.startsWith('mu')).toList(), [ |
| 94 | 'md 2', |
| 95 | 'mu 2', |
| 96 | ]); |
| 97 | }); |
| 98 | |
| 99 | testWidgets('two-finger drag: scroll, and no clicks', (t) async { |
| 100 | final e = await run(t, () async { |
| 101 | final a = await t.startGesture(mid - const Offset(20, 0), pointer: 1); |
| 102 | final b = await t.startGesture(mid + const Offset(20, 0), pointer: 2); |
| 103 | for (var i = 0; i < 5; i++) { |
| 104 | await a.moveBy(const Offset(0, -20)); |
| 105 | await b.moveBy(const Offset(0, -20)); |
| 106 | await t.pump(const Duration(milliseconds: 16)); |
| 107 | } |
| 108 | await a.up(); |
| 109 | await b.up(); |
| 110 | }); |
| 111 | expect(e.contains('wh'), isTrue); |
| 112 | expect(e.any((x) => x.startsWith('md')), isFalse); |
| 113 | }); |
| 114 | |
| 115 | testWidgets('one-finger drag: moves the pointer, no clicks', (t) async { |
| 116 | final e = await run(t, () async { |
| 117 | await t.dragFrom(mid, const Offset(100, 0)); |
| 118 | }); |
| 119 | expect(e.contains('mr'), isTrue); |
| 120 | expect(e.any((x) => x.startsWith('md')), isFalse); |
| 121 | }); |
| 122 | |
| 123 | testWidgets('a mouse right button: right click', (t) async { |
| 124 | final e = await run(t, () async { |
| 125 | await t.tapAt( |
| 126 | mid, |
| 127 | buttons: kSecondaryMouseButton, |
| 128 | kind: PointerDeviceKind.mouse, |
| 129 | ); |
| 130 | }); |
| 131 | expect(e, ['mm 0.50,0.50', 'md 2', 'mu 2']); |
| 132 | }); |
| 133 | |
| 134 | testWidgets('a mouse click after a two-finger tap still clicks', (t) async { |
| 135 | final e = await run(t, () async { |
| 136 | final a = await t.startGesture(mid, pointer: 1); |
| 137 | final b = await t.startGesture(mid + const Offset(40, 0), pointer: 2); |
| 138 | await a.up(); |
| 139 | await b.up(); |
| 140 | await t.pump(const Duration(milliseconds: 500)); |
| 141 | await t.tapAt(mid, kind: PointerDeviceKind.mouse); |
| 142 | await t.pump(const Duration(milliseconds: 500)); |
| 143 | }); |
| 144 | expect(e.where((x) => x == 'md 0').length, 1); |
| 145 | }); |
| 146 | } |