import 'package:cutdex/features/projects/project_tabs_bar.dart'; import 'package:cutdex/l10n/app_localizations.dart'; import 'package:cutdex/workspace/project_state.dart'; import 'package:cutdex_ui/theme.dart'; import 'package:cutdex_native/cutdex_native.dart'; import 'package:flutter/material.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { const recent = [ RecentProject(id: 'a', name: '旅行短片', path: '/Projects/A.cutdex'), RecentProject( id: 'b', name: '这是一个很长的中文项目名称用于验证窗口标签不会溢出', path: '/Projects/B.cutdex', ), RecentProject(id: 'c', name: '尚未打开的项目', path: '/Projects/C.cutdex'), ]; Future pumpTabs( WidgetTester tester, { required Widget tabs, double width = 720, }) => tester.pumpWidget( MaterialApp( theme: AppTheme.desktopWorkspaceTheme( AppTheme.forPalette(AppPalette.graphite, Brightness.dark), ), locale: const Locale('zh'), localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, home: Scaffold( body: SizedBox(width: width, height: 44, child: tabs), ), ), ); testWidgets( 'plus follows the last tab while the menu leaves empty drag space', (tester) async { var created = 0; for (final opened in [recent.take(2).toList(), []]) { await pumpTabs( tester, tabs: ProjectTabsBar( projects: opened, recent: recent, selectedId: opened.firstOrNull?.id, onSelect: (_) {}, onClose: (_) {}, onCreate: () => created++, onOpen: () {}, onRename: (_) {}, ), ); await tester.pumpAndSettle(); final entry = tester.getRect( find.byKey(const Key('project-tabs-projects')), ); final plus = tester.getRect(find.byKey(const Key('project-tabs-add'))); final list = tester.getRect(find.byKey(const Key('project-tabs-list'))); expect(entry.left, 4); expect(list.right, 716); if (opened.isNotEmpty) { final last = tester.getRect( find.byKey(ValueKey('project-tab-surface-${opened.last.id}')), ); expect(plus.left - last.right, 4); } else { expect(plus.left - entry.right, 4); } final dragPoint = Offset((plus.right + list.left) / 2, 22); expect(list.left - plus.right, greaterThan(100)); expect( find .byType(HeaderControlRegion) .evaluate() .any( (element) => tester .getRect(find.byWidget(element.widget)) .contains(dragPoint), ), isFalse, ); await tester.tap(find.byKey(const Key('project-tabs-add'))); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('project-menu-a')), findsNothing); expect(tester.takeException(), isNull); } expect(created, 2); }, ); testWidgets('header menus do not swallow tab, create or other menu clicks', ( tester, ) async { final selected = []; var created = 0; await pumpTabs( tester, tabs: ProjectTabsBar( projects: recent.take(2).toList(), recent: recent, selectedId: 'a', onSelect: (project) => selected.add(project.id), onClose: (_) {}, onCreate: () => created++, onOpen: () {}, onRename: (_) {}, ), ); await tester.pumpAndSettle(); final projects = find.byKey(const Key('project-tabs-projects')); final list = find.byKey(const Key('project-tabs-list')); Future click(Finder target) async { await tester.tap(target, kind: PointerDeviceKind.mouse); await tester.pumpAndSettle(); } await click(projects); expect(find.byKey(const ValueKey('project-menu-a')), findsOneWidget); await click(find.byKey(const ValueKey('project-tab-b'))); expect(selected, ['b']); expect(find.byKey(const ValueKey('project-menu-a')), findsNothing); await click(projects); await click(find.byKey(const Key('project-tabs-add'))); expect(created, 1); expect(find.byKey(const ValueKey('project-menu-a')), findsNothing); await click(projects); await click(list); expect(find.byKey(const ValueKey('project-menu-a')), findsNothing); expect(find.byKey(const ValueKey('project-tabs-menu-a')), findsOneWidget); await click(projects); expect(find.byKey(const ValueKey('project-tabs-menu-a')), findsNothing); expect(find.byKey(const ValueKey('project-menu-a')), findsOneWidget); await click(projects); expect(find.byKey(const ValueKey('project-menu-a')), findsNothing); expect(tester.takeException(), isNull); }); testWidgets('recent menu manages records without closing opened tabs', ( tester, ) async { var history = recent.toList(); final renamed = []; final closed = []; await pumpTabs( tester, tabs: StatefulBuilder( builder: (context, setState) => ProjectTabsBar( projects: recent.take(2).toList(), recent: history, selectedId: 'a', onSelect: (_) {}, onClose: (project) => closed.add(project.id), onCreate: () {}, onOpen: () {}, onRename: (project) => renamed.add(project.id), onRemoveRecent: (project) => setState(() { history = history.where((item) => item.id != project.id).toList(); }), ), ), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('project-tabs-projects'))); await tester.pumpAndSettle(); await tester.tap( find.byKey(const ValueKey('project-menu-b')), buttons: kSecondaryMouseButton, ); await tester.pumpAndSettle(); await tester.tap(find.text('重命名')); await tester.pumpAndSettle(); expect(renamed, ['b']); // Reopen the recent menu if the context action dismissed it. if (find.byKey(const ValueKey('project-menu-b')).evaluate().isEmpty) { await tester.tap(find.byKey(const Key('project-tabs-projects'))); await tester.pumpAndSettle(); } await tester.tap( find.byKey(const ValueKey('project-menu-b')), buttons: kSecondaryMouseButton, ); await tester.pumpAndSettle(); await tester.tap(find.text('从最近项目移除')); await tester.pumpAndSettle(); expect(history.map((item) => item.id), ['a', 'c']); expect(find.byKey(const ValueKey('project-tab-b')), findsOneWidget); expect(closed, isEmpty); expect(tester.takeException(), isNull); }); testWidgets( 'hover and keyboard focus reveal close without moving the label', (tester) async { var closed = 0; await pumpTabs( tester, tabs: ProjectTabsBar( projects: recent.take(2).toList(), recent: recent, selectedId: 'a', onSelect: (_) {}, onClose: (_) => closed++, onCreate: () {}, onOpen: () {}, onRename: (_) {}, ), ); await tester.pumpAndSettle(); final close = find.byKey(const ValueKey('project-tab-close-b')); final label = find.text(recent[1].name); final surface = find.byKey(const ValueKey('project-tab-surface-b')); final labelBefore = tester.getRect(label); final surfaceBefore = tester.getRect(surface); double opacity() => tester .widget( find.ancestor(of: close, matching: find.byType(Opacity)).first, ) .opacity; expect(opacity(), 0); await tester.tap(close, warnIfMissed: false); expect(closed, 0); final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse); await mouse.addPointer(location: const Offset(700, 80)); await mouse.moveTo(tester.getCenter(label)); await tester.pumpAndSettle(); expect(opacity(), 1); expect(tester.getRect(label), labelBefore); expect(tester.getRect(surface), surfaceBefore); await mouse.moveTo(const Offset(700, 80)); await tester.pumpAndSettle(); expect(opacity(), 0); Focus.of(tester.element(label)).requestFocus(); await tester.pumpAndSettle(); expect(opacity(), 1); expect(tester.getRect(label), labelBefore); await tester.tap(close); expect(closed, 1); await mouse.removePointer(); expect(tester.takeException(), isNull); }, ); testWidgets( 'opened project menu reveals a clipped tab and excludes recent files', (tester) async { var selected = 'a'; await pumpTabs( tester, width: 240, tabs: StatefulBuilder( builder: (context, setState) => ProjectTabsBar( projects: recent.take(2).toList(), recent: recent, selectedId: selected, onSelect: (project) => setState(() => selected = project.id), onClose: (_) {}, onCreate: () {}, onOpen: () {}, onRename: (_) {}, ), ), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('project-tabs-list'))); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('project-tabs-menu-a')), findsOneWidget); expect(find.byKey(const ValueKey('project-tabs-menu-b')), findsOneWidget); expect(find.byKey(const ValueKey('project-tabs-menu-c')), findsNothing); await tester.tap(find.byKey(const ValueKey('project-tabs-menu-b'))); await tester.pumpAndSettle(); expect(selected, 'b'); final tab = tester.getRect( find.byKey(const ValueKey('project-tab-surface-b')), ); expect(tab.left, greaterThanOrEqualTo(36)); expect( tab.right, lessThanOrEqualTo( tester.getRect(find.byKey(const Key('project-tabs-list'))).left - AppProjectTabMetrics.gap, ), ); final plus = tester.getRect(find.byKey(const Key('project-tabs-add'))); expect(plus.left - tab.right, AppProjectTabMetrics.gap); expect( plus.right, lessThanOrEqualTo( tester.getRect(find.byKey(const Key('project-tabs-list'))).left - AppProjectTabMetrics.gap, ), ); await tester.tap(find.byKey(const ValueKey('project-tab-b'))); expect(tester.takeException(), isNull); }, ); testWidgets('selection keeps geometry and navigation controls available', ( tester, ) async { var selected = 'a'; late StateSetter update; await pumpTabs( tester, tabs: StatefulBuilder( builder: (context, setState) { update = setState; return ProjectTabsBar( projects: recent.take(2).toList(), recent: recent, selectedId: selected, onSelect: (project) => setState(() => selected = project.id), onClose: (_) {}, onCreate: () {}, onOpen: () {}, onRename: (_) {}, ); }, ), ); await tester.pumpAndSettle(); final surface = find.byKey(const ValueKey('project-tab-surface-a')); final before = tester.getRect(surface); update(() => selected = 'b'); await tester.pumpAndSettle(); expect(tester.getRect(surface), before); expect(find.byType(CircularProgressIndicator), findsNothing); expect( tester .widget(find.byKey(const ValueKey('project-tab-a'))) .onPressed, isNotNull, ); for (final key in [ 'project-tab-close-b', 'project-tabs-add', 'project-tabs-list', 'project-tabs-projects', ]) { expect( tester.widget(find.byKey(ValueKey(key))).onPressed, isNotNull, ); } await tester.tap(find.byKey(const ValueKey('project-tab-a'))); await tester.pumpAndSettle(); expect(selected, 'a'); expect(tester.takeException(), isNull); }); testWidgets('empty project bar keeps creation available', (tester) async { await pumpTabs( tester, width: 240, tabs: ProjectTabsBar( projects: const [], recent: recent, selectedId: null, onSelect: (_) {}, onClose: (_) {}, onCreate: () {}, onOpen: () {}, onRename: (_) {}, ), ); await tester.pumpAndSettle(); expect( tester .widget(find.byKey(const Key('project-tabs-add'))) .onPressed, isNotNull, ); expect( tester .widget(find.byKey(const Key('project-tabs-list'))) .onPressed, isNull, ); expect(tester.takeException(), isNull); }); testWidgets('open tabs, recent menu and close actions stay separate', ( tester, ) async { var selected = 'a'; var opened = recent.take(2).toList(); var created = 0; var picked = 0; late StateSetter update; Widget content() => StatefulBuilder( builder: (context, setState) { update = setState; return ProjectTabsBar( projects: opened, recent: recent, selectedId: selected, onSelect: (project) => setState(() { selected = project.id; if (!opened.contains(project)) opened = [...opened, project]; }), onClose: (project) => setState( () => opened = opened.where((item) => item != project).toList(), ), onCreate: () => created++, onOpen: () => picked++, onRename: (_) {}, ); }, ); await tester.pumpWidget( MaterialApp( theme: AppTheme.desktopWorkspaceTheme( AppTheme.forPalette(AppPalette.graphite, Brightness.dark), ), locale: const Locale('zh'), localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, home: Scaffold( body: SizedBox(width: 320, height: 44, child: content()), ), ), ); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('project-tab-c')), findsNothing); await tester.tap(find.byKey(const Key('project-tabs-projects'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const ValueKey('project-menu-c'))); await tester.pumpAndSettle(); expect(selected, 'c'); expect(find.byKey(const ValueKey('project-tab-c')), findsOneWidget); expect( tester.getRect(find.byKey(const ValueKey('project-tab-c'))).right, lessThanOrEqualTo(320), ); await tester.tap(find.byKey(const ValueKey('project-tab-close-c'))); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('project-tab-c')), findsNothing); expect(recent.length, 3); update(() => selected = 'a'); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('project-tabs-projects'))); await tester.pumpAndSettle(); await tester.tap(find.text('新建项目')); await tester.pumpAndSettle(); expect(created, 1); await tester.tap(find.byKey(const Key('project-tabs-projects'))); await tester.pumpAndSettle(); await tester.tap(find.text('打开项目…')); await tester.pumpAndSettle(); expect(picked, 1); expect(tester.takeException(), isNull); }); testWidgets('project tabs fit six themes, long names and scaled text', ( tester, ) async { for (final palette in AppPalette.values) { for (final brightness in Brightness.values) { for (final width in [240.0, 320.0, 720.0]) { await tester.pumpWidget( MaterialApp( theme: AppTheme.desktopWorkspaceTheme( AppTheme.forPalette(palette, brightness), ), locale: const Locale('zh'), localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, home: MediaQuery( data: const MediaQueryData(textScaler: TextScaler.linear(1.5)), child: Scaffold( body: SizedBox( width: width, height: 44, child: ProjectTabsBar( projects: recent, recent: recent, selectedId: 'b', onSelect: (_) {}, onClose: (_) {}, onCreate: () {}, onOpen: () {}, onRename: (_) {}, ), ), ), ), ), ); await tester.pumpAndSettle(); final tab = tester.getRect( find.byKey(const ValueKey('project-tab-b')), ); final close = tester.getRect( find.byKey(const ValueKey('project-tab-close-b')), ); final add = tester.getRect(find.byKey(const Key('project-tabs-add'))); expect(tab.height, AppProjectTabMetrics.height); expect(close.height, AppProjectTabMetrics.close); expect(add.height, tab.height); expect(close.center.dy, tab.center.dy); expect(add.center.dy, tab.center.dy); expect(tab.center.dy, 22); final list = tester.getRect( find.byKey(const Key('project-tabs-list')), ); expect(list.height, tab.height); expect(list.center.dy, tab.center.dy); expect(width - list.right, AppProjectTabMetrics.gap); expect( close.right, lessThanOrEqualTo(list.left - AppProjectTabMetrics.gap), ); expect(tab.right, lessThanOrEqualTo(width)); expect(tester.takeException(), isNull); } } } }); }