249 lines
9.4 KiB
Dart
249 lines
9.4 KiB
Dart
import 'package:cutdex/shared/widgets/workspace_resize_handle.dart';
|
|
import 'package:flutter/gestures.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/rendering.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
void main() {
|
|
for (final axis in [...Axis.values, null]) {
|
|
testWidgets('$axis feedback fades and expands with rounded ends', (
|
|
tester,
|
|
) async {
|
|
final handle = find.byKey(const Key('animated-handle'));
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
home: Scaffold(
|
|
body: Center(
|
|
child: SizedBox(
|
|
width: axis == Axis.vertical ? 200 : 10,
|
|
height: axis == Axis.horizontal ? 200 : 10,
|
|
child: axis == null
|
|
? WorkspaceResizeHandle.junction(
|
|
key: const Key('animated-handle'),
|
|
initialWidth: 100,
|
|
initialHeight: 100,
|
|
onWidthChanged: (_) {},
|
|
onHeightChanged: (_) {},
|
|
)
|
|
: WorkspaceResizeHandle(
|
|
key: const Key('animated-handle'),
|
|
axis: axis,
|
|
initialValue: 100,
|
|
onResize: (_) {},
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
final fade = find.descendant(of: handle, matching: find.byType(Opacity));
|
|
final paint = find.descendant(
|
|
of: handle,
|
|
matching: find.byType(DecoratedBox),
|
|
);
|
|
double opacity() => tester.widget<Opacity>(fade).opacity;
|
|
final hitSize = tester.getSize(handle);
|
|
final center = tester.getCenter(handle);
|
|
final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse);
|
|
expect(opacity(), 0);
|
|
await mouse.addPointer(location: center);
|
|
await tester.pump();
|
|
expect(opacity(), 0);
|
|
await tester.pump(const Duration(milliseconds: 60));
|
|
expect(opacity(), inExclusiveRange(0, 1));
|
|
final growing = tester.getRect(paint);
|
|
expect(
|
|
axis == Axis.vertical ? growing.height : growing.width,
|
|
inExclusiveRange(2, 4),
|
|
);
|
|
expect(tester.getSize(handle), hitSize);
|
|
await tester.pumpAndSettle();
|
|
expect(opacity(), 1);
|
|
final decoration =
|
|
tester.widget<DecoratedBox>(paint).decoration as BoxDecoration;
|
|
expect(decoration.borderRadius, BorderRadius.circular(2));
|
|
await mouse.moveTo(const Offset(10, 10));
|
|
await tester.pump();
|
|
await tester.pump(const Duration(milliseconds: 30));
|
|
final fading = opacity();
|
|
expect(fading, inExclusiveRange(0, 1));
|
|
// Reentering mid-fade continues from its current appearance.
|
|
await mouse.moveTo(center);
|
|
await tester.pump();
|
|
expect(opacity(), closeTo(fading, .001));
|
|
await tester.pumpAndSettle();
|
|
expect(opacity(), 1);
|
|
await mouse.moveTo(const Offset(10, 10));
|
|
await tester.pumpAndSettle();
|
|
expect(opacity(), 0);
|
|
await mouse.removePointer();
|
|
expect(tester.takeException(), isNull);
|
|
});
|
|
}
|
|
|
|
for (final axis in [...Axis.values, null]) {
|
|
for (final finish in ['release', 'cancel', 'unmount']) {
|
|
testWidgets(
|
|
'$axis resize cursor stays locked until $finish',
|
|
(tester) async {
|
|
final expectedCursor = axis == null
|
|
? SystemMouseCursors.grab
|
|
: axis == Axis.horizontal
|
|
? SystemMouseCursors.resizeColumn
|
|
: SystemMouseCursors.resizeRow;
|
|
var showHandle = true;
|
|
var taps = 0;
|
|
var ends = 0;
|
|
var starts = 0;
|
|
var updates = 0;
|
|
late StateSetter update;
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
home: Scaffold(
|
|
body: StatefulBuilder(
|
|
builder: (context, setState) {
|
|
update = setState;
|
|
return Stack(
|
|
children: [
|
|
Positioned(
|
|
left: 400,
|
|
top: 400,
|
|
width: 100,
|
|
height: 60,
|
|
child: MouseRegion(
|
|
cursor: SystemMouseCursors.click,
|
|
child: GestureDetector(
|
|
key: const Key('button'),
|
|
onTap: () => taps++,
|
|
child: const ColoredBox(color: Colors.blue),
|
|
),
|
|
),
|
|
),
|
|
if (showHandle)
|
|
Positioned(
|
|
left: 100,
|
|
top: 100,
|
|
width: axis == null || axis == Axis.horizontal
|
|
? 5
|
|
: 200,
|
|
height: axis == null || axis == Axis.vertical
|
|
? 5
|
|
: 200,
|
|
child: axis == null
|
|
? WorkspaceResizeHandle.junction(
|
|
key: const Key('handle'),
|
|
initialWidth: 100,
|
|
initialHeight: 100,
|
|
onStart: () => starts++,
|
|
onWidthChanged: (_) {
|
|
expect(starts, 1);
|
|
updates++;
|
|
},
|
|
onHeightChanged: (_) {
|
|
expect(starts, 1);
|
|
updates++;
|
|
},
|
|
onEnd: () => ends++,
|
|
)
|
|
: WorkspaceResizeHandle(
|
|
key: const Key('handle'),
|
|
axis: axis,
|
|
initialValue: 100,
|
|
onStart: () => starts++,
|
|
onResize: (_) {
|
|
expect(starts, 1);
|
|
updates++;
|
|
},
|
|
onEnd: () => ends++,
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
);
|
|
final mouse = await tester.createGesture(
|
|
kind: PointerDeviceKind.mouse,
|
|
);
|
|
final start = tester.getCenter(find.byKey(const Key('handle')));
|
|
final button = tester.getCenter(find.byKey(const Key('button')));
|
|
double highlightOpacity() => tester
|
|
.widget<Opacity>(
|
|
find.descendant(
|
|
of: find.byKey(const Key('handle')),
|
|
matching: find.byType(Opacity),
|
|
),
|
|
)
|
|
.opacity;
|
|
expect(highlightOpacity(), 0);
|
|
await mouse.addPointer(location: start);
|
|
await tester.pumpAndSettle();
|
|
expect(highlightOpacity(), 1);
|
|
expect(
|
|
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
|
|
expectedCursor,
|
|
);
|
|
await mouse.down(start);
|
|
await mouse.moveBy(
|
|
axis == null
|
|
? const Offset(30, 30)
|
|
: axis == Axis.horizontal
|
|
? const Offset(30, 0)
|
|
: const Offset(0, 30),
|
|
);
|
|
await tester.pump();
|
|
await mouse.moveTo(button);
|
|
await tester.pump();
|
|
expect(updates, greaterThan(0));
|
|
expect(starts, 1);
|
|
expect(highlightOpacity(), 1);
|
|
expect(
|
|
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
|
|
axis == null ? SystemMouseCursors.grabbing : expectedCursor,
|
|
);
|
|
// A second pointer still reaches the control through the cursor overlay.
|
|
await tester.tap(find.byKey(const Key('button')));
|
|
expect(taps, 1);
|
|
|
|
if (finish == 'unmount') {
|
|
update(() => showHandle = false);
|
|
await tester.pumpAndSettle();
|
|
await mouse.up();
|
|
} else if (finish == 'cancel') {
|
|
await mouse.cancel();
|
|
} else {
|
|
await mouse.up();
|
|
}
|
|
await tester.pumpAndSettle();
|
|
await mouse.moveTo(button + const Offset(1, 0));
|
|
await tester.pump();
|
|
expect(
|
|
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
|
|
SystemMouseCursors.click,
|
|
);
|
|
expect(ends, finish == 'unmount' ? 0 : 1);
|
|
if (finish != 'unmount') {
|
|
expect(highlightOpacity(), 0);
|
|
await mouse.moveTo(start);
|
|
await tester.pumpAndSettle();
|
|
expect(highlightOpacity(), 1);
|
|
expect(
|
|
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
|
|
expectedCursor,
|
|
);
|
|
}
|
|
await mouse.removePointer();
|
|
expect(tester.takeException(), isNull);
|
|
},
|
|
variant: const TargetPlatformVariant({
|
|
TargetPlatform.macOS,
|
|
TargetPlatform.windows,
|
|
TargetPlatform.linux,
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
}
|