import 'package:cutdex_ui/theme.dart'; import 'package:cutdex_ui/widgets.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { testWidgets('standard dialog uses Codex spacing, type and button size', ( tester, ) async { await tester.pumpWidget( MaterialApp( theme: AppTheme.penDark, home: Scaffold( body: Builder( builder: (context) => TextButton( onPressed: () => showAppDialog( context: context, builder: (_) => AppDialog( title: '下载资源', description: '下载所需模型', actions: [ TextButton(onPressed: () {}, child: const Text('取消')), FilledButton(onPressed: () {}, child: const Text('下载')), ], child: const Text('正在准备资源'), ), ), 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 title = tester.getRect(find.text('下载资源')); final description = tester.getRect(find.text('下载所需模型')); final body = tester.getRect(find.text('正在准备资源')); final close = tester.getRect( find.byKey(const Key('app-dialog-close-button')), ); final action = tester.getRect(find.widgetWithText(FilledButton, '下载')); final cancel = tester.getRect(find.widgetWithText(TextButton, '取消')); expect(title.left - frame.left, AppSpacing.large); expect(frame.width, 420); expect(title.top - frame.top, AppSpacing.large); expect(body.left - frame.left, AppSpacing.large); expect(description.top - title.bottom, AppSpacing.tiny); expect(body.top - description.bottom, AppSpacing.compact); expect(frame.right - close.right, AppSpacing.medium); expect(close.top - frame.top, AppSpacing.medium); expect(close.size, const Size.square(24)); expect( Theme.of(tester.element(find.text('下载'))) .filledButtonTheme .style! .minimumSize! .resolve({}), const Size(0, 32), ); expect(action.height, 32); expect(cancel.height, 32); expect(action.left - cancel.right, AppSpacing.compact); expect(frame.right - action.right, AppSpacing.large); expect(action.top - body.bottom, AppSpacing.compact); expect(frame.bottom - action.bottom, AppSpacing.large); expect(tester.widget(find.text('下载资源')).style?.fontSize, 20); expect( tester.widget(find.text('下载资源')).style?.fontWeight, FontWeight.w600, ); expect( DefaultTextStyle.of(tester.element(find.text('正在准备资源'))).style.fontSize, 14, ); expect( Theme.of(tester.element(find.text('正在准备资源'))).dialogTheme.backgroundColor, const Color(0xFF242424), ); final shape = AppTheme.penDark.dialogTheme.shape! as RoundedRectangleBorder; expect(shape.borderRadius, BorderRadius.circular(AppRadius.dialog)); expect(shape.side, BorderSide.none); expect( Theme.of(tester.element(find.text('取消'))) .textButtonTheme .style! .textStyle! .resolve({})! .fontSize, 14, ); }); testWidgets('selection dialog returns the chosen option', (tester) async { String? selected; await tester.pumpWidget( MaterialApp( theme: AppTheme.penDark, home: Scaffold( body: Builder( builder: (context) => TextButton( onPressed: () async { selected = await showAppDialog( context: context, builder: (dialogContext) => AppDialog( title: '选择目标片段', child: AppDialogOption( label: '片段 A', onPressed: () => Navigator.pop(dialogContext, 'a'), ), ), ); }, child: const Text('Open'), ), ), ), ), ); await tester.tap(find.text('Open')); await tester.pumpAndSettle(); expect(find.byKey(const Key('app-dialog-frame')), findsOneWidget); await tester.tap(find.text('片段 A')); await tester.pumpAndSettle(); expect(selected, 'a'); }); }