Files
cutdex/test/editor_window_chrome_test.dart
T
lingjhf 27e77fff39 feat: initial Cutdex application
Add the Flutter video editing application with project management, media
import, timeline editing, preview, AI assistance, diagnostics and settings.

Include shared UI and native host packages, platform runners, application
tests and release tooling.

Provide usage and contribution guides, the MIT license and third-party
notices. Exclude local credentials, build outputs and validation data.
2026-10-05 23:36:23 +08:00

504 lines
16 KiB
Dart

import 'package:cutdex_native/cutdex_native.dart';
import 'package:cutdex/l10n/app_localizations.dart';
import 'dart:async';
import 'package:cutdex/integrations/platform/editor_window_chrome.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
const channel = MethodChannel('cutdex_native/window');
const controlsChannel = MethodChannel('cutdex_native/window_controls');
final binding = TestWidgetsFlutterBinding.ensureInitialized();
tearDown(() {
debugDefaultTargetPlatformOverride = null;
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, null);
binding.defaultBinaryMessenger.setMockMethodCallHandler(
controlsChannel,
null,
);
});
Widget app(
String id, {
VoidCallback? onCloseRequested,
TargetPlatform platform = TargetPlatform.macOS,
Widget Function(BuildContext, bool)? builder,
GlobalKey<NavigatorState>? navigatorKey,
}) {
debugDefaultTargetPlatformOverride = platform;
addTearDown(() => debugDefaultTargetPlatformOverride = null);
return MaterialApp(
navigatorKey: navigatorKey,
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
locale: const Locale('zh'),
home: EditorWindowChrome(
key: ValueKey(id),
headerHeight: 40,
onCloseRequested: onCloseRequested ?? () {},
builder:
builder ??
(context, integrated) =>
Text(integrated ? 'integrated' : 'standard'),
),
);
}
Future<void> nativeCall(String method, Object arguments) async {
final reply = Completer<ByteData?>();
binding.defaultBinaryMessenger.handlePlatformMessage(
channel.name,
const StandardMethodCodec().encodeMethodCall(
MethodCall(method, arguments),
),
reply.complete,
);
expect(
const StandardMethodCodec().decodeEnvelope((await reply.future)!),
true,
);
}
testWidgets(
'Windows buttons dispatch commands and follow native window state',
(tester) async {
final calls = <MethodCall>[];
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, (
call,
) async {
calls.add(call);
return call.method == 'configureChrome'
? (call.arguments as Map)['enabled']
: null;
});
await tester.pumpWidget(app('windows', platform: TargetPlatform.windows));
await tester.pumpAndSettle();
expect(calls.first.arguments, {
'enabled': true,
'headerHeight': 40.0,
'interceptClose': true,
'headerInteractive': true,
});
expect(EditorWindowChrome.leadingWidth, 0);
await tester.tap(find.byKey(const Key('window-minimize')));
await tester.tap(find.byKey(const Key('window-maximize')));
await tester.tap(find.byKey(const Key('window-close')));
expect(calls.skip(1).map((c) => c.method), [
'minimize',
'toggleMaximize',
'close',
]);
await nativeCall('windowState', {
'maximized': true,
'maximizeHovered': true,
});
await tester.pump();
expect(find.byTooltip('还原'), findsOneWidget);
expect(find.byIcon(Icons.filter_none), findsOneWidget);
await tester.pumpWidget(const SizedBox());
await tester.pump();
expect(calls.last.arguments, {'enabled': false});
debugDefaultTargetPlatformOverride = null;
},
);
testWidgets(
'Windows headers reserve caption space only in the rightmost pane',
(tester) async {
binding.defaultBinaryMessenger.setMockMethodCallHandler(
channel,
(call) async => call.method == 'configureChrome'
? (call.arguments as Map)['enabled']
: null,
);
final width = ValueNotifier(250.0);
addTearDown(width.dispose);
await tester.pumpWidget(
app(
'layout',
platform: TargetPlatform.windows,
builder: (context, integrated) => ValueListenableBuilder<double>(
valueListenable: width,
builder: (context, sidebarWidth, _) => Row(
children: [
SizedBox(
width: sidebarWidth,
child: Column(
children: [
WindowHeaderPadding(
child: SizedBox(
height: 40,
child: Row(
children: [
const Spacer(),
TextButton(
key: const Key('left-action'),
onPressed: () {},
child: const Text('Left'),
),
],
),
),
),
],
),
),
Expanded(
child: WindowHeaderPane(
offset: Offset(sidebarWidth, 0),
child: Column(
children: [
WindowHeaderPadding(
child: SizedBox(
height: 40,
child: Row(
children: [
const Spacer(),
TextButton(
key: const Key('right-action'),
onPressed: () {},
child: const Text('Right'),
),
],
),
),
),
],
),
),
),
],
),
),
),
);
await tester.pumpAndSettle();
final controlsLeft = tester
.getTopLeft(find.byKey(const Key('window-minimize')))
.dx;
expect(tester.getTopRight(find.byKey(const Key('left-action'))).dx, 250);
expect(
tester.getTopRight(find.byKey(const Key('right-action'))).dx,
controlsLeft,
);
width.value = 700;
await tester.pump();
expect(
tester.getTopRight(find.byKey(const Key('left-action'))).dx,
controlsLeft,
);
expect(tester.takeException(), isNull);
expect(find.byKey(const Key('right-action')), findsNothing);
await tester.pumpWidget(const SizedBox());
await tester.pump();
debugDefaultTargetPlatformOverride = null;
},
);
testWidgets(
'Windows reports actual maximize bounds without rebuilding the workspace',
(tester) async {
var builds = 0;
final regions = <MethodCall>[];
binding.defaultBinaryMessenger.setMockMethodCallHandler(
channel,
(call) async => call.method == 'configureChrome'
? (call.arguments as Map)['enabled']
: null,
);
binding.defaultBinaryMessenger.setMockMethodCallHandler(controlsChannel, (
call,
) async {
regions.add(call);
return null;
});
await tester.pumpWidget(
app(
'regions',
platform: TargetPlatform.windows,
builder: (context, integrated) {
builds++;
return const SizedBox.expand();
},
),
);
await tester.pumpAndSettle();
final count = builds;
final rect = tester.getRect(find.byKey(const Key('window-maximize')));
final report = regions
.map((c) => c.arguments as Map)
.singleWhere((args) => args['role'] == 'maximize');
expect(report['rect'], {
'x': rect.left,
'y': rect.top,
'width': rect.width,
'height': rect.height,
});
await nativeCall('windowState', {
'maximized': true,
'maximizeHovered': true,
});
await tester.pump();
expect(builds, count);
expect(find.byTooltip('还原'), findsOneWidget);
await tester.pumpWidget(const SizedBox());
await tester.pump();
expect(
regions.any(
(c) =>
(c.arguments as Map)['role'] == 'maximize' &&
(c.arguments as Map)['rect'] == null,
),
true,
);
debugDefaultTargetPlatformOverride = null;
},
);
testWidgets(
'nested pane offsets reserve caption space on the first integrated frame',
(tester) async {
binding.defaultBinaryMessenger.setMockMethodCallHandler(
channel,
(call) async => call.method == 'configureChrome'
? (call.arguments as Map)['enabled']
: null,
);
await tester.pumpWidget(
app(
'nested',
platform: TargetPlatform.windows,
builder: (context, integrated) => Row(
children: [
const SizedBox(width: 200),
Expanded(
child: WindowHeaderPane(
offset: const Offset(200, 0),
child: Row(
children: [
const SizedBox(width: 100),
Expanded(
child: WindowHeaderPane(
offset: const Offset(100, 0),
child: Column(
children: [
WindowHeaderPadding(
child: SizedBox(
height: 40,
child: Row(
children: [
const Spacer(),
TextButton(
key: const Key('nested-action'),
onPressed: () {},
child: const Text('Action'),
),
],
),
),
),
],
),
),
),
],
),
),
),
],
),
),
);
await tester.pump();
expect(
tester.getTopRight(find.byKey(const Key('nested-action'))).dx,
tester.getTopLeft(find.byKey(const Key('window-minimize'))).dx,
);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox());
await tester.pump();
debugDefaultTargetPlatformOverride = null;
},
);
testWidgets(
'Windows modal routes suspend header hit testing until dismissed',
(tester) async {
final calls = <MethodCall>[];
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, (
call,
) async {
calls.add(call);
return call.method == 'configureChrome'
? (call.arguments as Map)['enabled']
: null;
});
final navigatorKey = GlobalKey<NavigatorState>();
await tester.pumpWidget(
app(
'modal',
platform: TargetPlatform.windows,
navigatorKey: navigatorKey,
),
);
await tester.pumpAndSettle();
unawaited(
showDialog<void>(
context: tester.element(find.byType(EditorWindowChrome)),
builder: (_) => const AlertDialog(content: Text('Modal')),
),
);
await tester.pumpAndSettle();
expect(
calls.where((c) => c.method == 'setHeaderInteractive').last.arguments,
false,
);
navigatorKey.currentState!.pop();
await tester.pumpAndSettle();
expect(
calls.where((c) => c.method == 'setHeaderInteractive').last.arguments,
true,
);
await tester.pumpWidget(const SizedBox());
await tester.pump();
debugDefaultTargetPlatformOverride = null;
},
);
testWidgets(
'Windows hosts without the native plugin retain the standard header',
(tester) async {
await tester.pumpWidget(
app('old-windows', platform: TargetPlatform.windows),
);
await tester.pumpAndSettle();
expect(find.text('standard'), findsOneWidget);
expect(find.byKey(const Key('window-close')), findsNothing);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox());
await tester.pump();
debugDefaultTargetPlatformOverride = null;
},
);
testWidgets('enables native chrome and restores it when leaving the editor', (
tester,
) async {
final calls = <MethodCall>[];
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, (
call,
) async {
calls.add(call);
return (call.arguments as Map)['enabled'];
});
await tester.pumpWidget(app('one'));
await tester.pumpAndSettle();
expect(find.text('integrated'), findsOneWidget);
expect(calls.single.method, 'configureChrome');
expect(calls.single.arguments, {
'enabled': true,
'headerHeight': 40.0,
'interceptClose': true,
});
await tester.pumpWidget(const SizedBox());
await tester.pump();
expect(calls.last.arguments, {'enabled': false});
debugDefaultTargetPlatformOverride = null;
});
testWidgets('forwards the native close request to the current editor', (
tester,
) async {
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, (
call,
) async {
return (call.arguments as Map)['enabled'];
});
var requests = 0;
await tester.pumpWidget(app('one', onCloseRequested: () => requests += 1));
await tester.pumpAndSettle();
final reply = Completer<ByteData?>();
binding.defaultBinaryMessenger.handlePlatformMessage(
channel.name,
const StandardMethodCodec().encodeMethodCall(
const MethodCall('requestClose'),
),
reply.complete,
);
final result = const StandardMethodCodec().decodeEnvelope(
(await reply.future)!,
);
await tester.pump();
expect(result, isTrue);
expect(requests, 1);
await tester.pumpWidget(const SizedBox());
await tester.pump();
debugDefaultTargetPlatformOverride = null;
});
testWidgets('late enable reply cannot re-enable a disposed editor', (
tester,
) async {
final enabled = Completer<bool>();
final calls = <bool>[];
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, (
call,
) async {
final enable = (call.arguments as Map)['enabled'] as bool;
calls.add(enable);
return enable ? enabled.future : false;
});
await tester.pumpWidget(app('one'));
await tester.pumpWidget(const SizedBox());
enabled.complete(true);
await tester.pumpAndSettle();
expect(calls, [true, false]);
expect(tester.takeException(), isNull);
debugDefaultTargetPlatformOverride = null;
});
testWidgets(
'replacing an editor does not let the old owner disable the new one',
(tester) async {
final calls = <bool>[];
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, (
call,
) async {
final enable = (call.arguments as Map)['enabled'] as bool;
calls.add(enable);
return enable;
});
await tester.pumpWidget(app('one'));
await tester.pumpAndSettle();
await tester.pumpWidget(app('two'));
await tester.pumpAndSettle();
expect(calls.last, isTrue);
expect(find.text('integrated'), findsOneWidget);
await tester.pumpWidget(const SizedBox());
await tester.pump();
expect(calls.last, isFalse);
debugDefaultTargetPlatformOverride = null;
},
);
testWidgets(
'an unregistered native plugin keeps the standard Flutter heading',
(tester) async {
await tester.pumpWidget(app('one'));
await tester.pumpAndSettle();
expect(find.text('standard'), findsOneWidget);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox());
await tester.pump();
debugDefaultTargetPlatformOverride = null;
},
);
}