import 'dart:async'; import 'package:cutdex/app/editor_theme.dart'; import 'package:cutdex/l10n/app_localizations.dart'; import 'package:cutdex_ui/theme.dart'; import 'package:cutdex/features/projects/widgets/name_dialog.dart'; import 'package:flutter/material.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { testWidgets('name collision keeps the dialog open and allows correction', ( tester, ) async { final validation = Completer(); final submitted = []; String? result; await tester.pumpWidget( MaterialApp( theme: EditorTheme.penDark, localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, locale: const Locale('zh'), home: Scaffold( body: Builder( builder: (context) => TextButton( onPressed: () async { result = await askName( context, title: '新建项目', label: '项目名称', validateName: (name) async { submitted.add(name); return name == 'Existing' ? validation.future : null; }, ); }, child: const Text('Open'), ), ), ), ), ); await tester.tap(find.text('Open')); await tester.pumpAndSettle(); await tester.enterText(find.byType(TextFormField), 'Existing'); await tester.tap(find.byType(FilledButton)); await tester.pump(); expect( tester.widget(find.byType(FilledButton)).onPressed, isNull, ); await tester.testTextInput.receiveAction(TextInputAction.done); expect(submitted, ['Existing']); final error = lookupAppLocalizations(const Locale('zh')) .projectAlreadyExists; validation.complete(error); await tester.pumpAndSettle(); expect(find.text(error), findsOneWidget); expect(find.byType(TextFormField), findsOneWidget); expect( tester.widget(find.byType(EditableText)).controller.text, 'Existing', ); expect(result, isNull); expect(tester.takeException(), isNull); await tester.enterText(find.byType(TextFormField), 'Another'); await tester.pump(); expect(find.text(error), findsNothing); await tester.tap(find.byType(FilledButton)); await tester.pumpAndSettle(); expect(result, 'Another'); expect(submitted, ['Existing', 'Another']); expect(find.byType(TextFormField), findsNothing); }); testWidgets('cancel during validation does not pop the underlying page', ( tester, ) async { final validation = Completer(); await tester.pumpWidget( MaterialApp( theme: EditorTheme.penDark, localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, home: Builder( builder: (context) => TextButton( onPressed: () => askName( context, title: 'New project', label: 'Name', validateName: (_) => validation.future, ), child: const Text('Open'), ), ), ), ); await tester.tap(find.text('Open')); await tester.pumpAndSettle(); await tester.enterText(find.byType(TextFormField), 'Name'); await tester.tap(find.byType(FilledButton)); await tester.pump(); await tester.tap(find.text('Cancel')); validation.complete(null); await tester.pumpAndSettle(); expect(find.text('Open'), findsOneWidget); expect(find.byType(TextFormField), findsNothing); expect(tester.takeException(), isNull); }); testWidgets('rename hint, text line and caret share the vertical center', ( tester, ) async { addTearDown(() => debugDefaultTargetPlatformOverride = null); for (final platform in [TargetPlatform.macOS, TargetPlatform.windows]) { debugDefaultTargetPlatformOverride = platform; for (final brightness in Brightness.values) { await tester.pumpWidget( MaterialApp( theme: EditorTheme.desktop( EditorTheme.forPalette(AppPalette.graphite, brightness), ), localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, locale: const Locale('zh'), home: Scaffold( body: Builder( builder: (context) => TextButton( onPressed: () => askName( context, title: '重命名', label: '项目名称', submitLabel: '重命名', ), child: const Text('Open'), ), ), ), ), ); await tester.tap(find.text('Open')); await tester.pumpAndSettle(); final field = find.byType(TextFormField); final editable = find.byType(EditableText); final input = tester.widget(editable); final decoration = tester .widget(find.byType(TextField)) .decoration! .applyDefaults( Theme.of(tester.element(field)).inputDecorationTheme, ); expect(decoration.hintStyle!.fontSize, input.style.fontSize); expect(decoration.hintStyle!.height, input.style.height); final hint = tester.renderObject(find.text('项目名称')); final hintBox = hint .getBoxesForSelection( const TextSelection(baseOffset: 0, extentOffset: 4), ) .first .toRect(); expect( hint.localToGlobal(hintBox.center).dy, closeTo(tester.getCenter(field).dy, 1), ); for (final text in ['', '剪辑项目', 'Cutdex project']) { await tester.enterText(field, text); await tester.pump(); final render = tester .state(editable) .renderEditable; final caret = render.getLocalRectForCaret( TextPosition(offset: text.length), ); expect( render.localToGlobal(caret.center).dy, closeTo(tester.getCenter(field).dy, 1), reason: '$platform / $brightness / "$text"', ); expect( render.localToGlobal(render.size.center(Offset.zero)).dy, closeTo(tester.getCenter(field).dy, 1), ); expect(tester.getSize(field).height, 36); } await tester.tap(find.text('取消')); await tester.pumpAndSettle(); await tester.pumpWidget(const SizedBox()); } } debugDefaultTargetPlatformOverride = null; }); testWidgets('rename chat uses the same dialog and compact input', ( tester, ) async { await tester.pumpWidget( MaterialApp( theme: EditorTheme.penDark, localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, locale: const Locale('zh'), home: Scaffold( body: Builder( builder: (context) => TextButton( onPressed: () => askName( context, title: '重命名聊天', description: '保持简短且易于识别', label: '名称', initialValue: '实现图片页面并添加跳转', submitLabel: '保存', ), child: const Text('Open'), ), ), ), ), ); await tester.tap(find.text('Open')); await tester.pumpAndSettle(); final frame = tester.getRect(find.byKey(const Key('app-dialog-frame'))); final field = tester.getRect(find.byType(TextFormField)); final save = tester.getRect(find.widgetWithText(FilledButton, '保存')); expect(frame.width, 420); expect(find.text('重命名聊天'), findsOneWidget); expect(find.text('保持简短且易于识别'), findsOneWidget); expect(field.left - frame.left, AppSpacing.large); expect(frame.right - field.right, AppSpacing.large); expect(field.height, 36); expect(save.top - field.bottom, AppSpacing.compact); expect( tester .widget( find.descendant( of: find.byType(TextFormField), matching: find.byType(EditableText), ), ) .style .fontSize, 14, ); expect( (Theme.of(tester.element(find.byType(TextFormField))) .inputDecorationTheme .focusedBorder! as OutlineInputBorder) .borderSide .color, const Color(0xFF6084AF), ); }); }