import 'dart:ui' as ui; import 'package:cutdex/app/adaptive/desktop_workspace_controller.dart'; import 'package:cutdex/shared/layout/workspace_layout_metrics.dart'; import 'package:cutdex/app/shells/desktop_workspace_shell.dart'; import 'package:cutdex/workspace/workspace_mode.dart'; import 'package:cutdex/features/editor/layouts/expanded_editor_layout.dart'; import 'package:flutter/material.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { testWidgets( 'junctions paint both connected boundaries during hover and drag', (tester) async { tester.view.physicalSize = const Size(1500, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final controller = DesktopWorkspaceController(); addTearDown(controller.dispose); const accent = Color(0xFF0D99FF); final repaintKey = GlobalKey(); final scaffoldKey = GlobalKey(); var sidebarWidth = 240.0; var mediaWidth = 260.0; var timelineHeight = 280.0; var showMedia = true; var sidebarCollapsed = false; late StateSetter update; await tester.pumpWidget( MaterialApp( theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: accent) .copyWith(primary: accent), ), home: RepaintBoundary( key: repaintKey, child: StatefulBuilder( builder: (context, setState) { update = setState; return DesktopWorkspaceShell( projectTabsLayout: true, mode: WorkspaceMode.cut, controller: controller, sidebarCollapsed: sidebarCollapsed, sidebarWidth: sidebarWidth, onSidebarCollapsedChanged: (collapsed) => setState(() => sidebarCollapsed = collapsed), onSidebarWidthChanged: (width) => setState(() => sidebarWidth = width), timelineHeight: timelineHeight, onTimelineHeightChanged: (height) => setState(() => timelineHeight = height), sidebarBuilder: (_, _) => const SizedBox.expand(), agentBuilder: (_, _) => const SizedBox.expand(), scaffoldKey: scaffoldKey, navigationDrawer: const SizedBox.shrink(), editor: ExpandedEditorLayout( preview: const SizedBox.expand(), header: const SizedBox(height: 40), properties: const SizedBox.expand(), media: const SizedBox.expand(), showMedia: showMedia, showProperties: false, mediaWidth: mediaWidth, onMediaWidthChanged: (width) => setState(() => mediaWidth = width), onMediaWidthChangeEnd: () {}, timelineHeight: timelineHeight, onTimelineHeightChanged: (height) => setState(() => timelineHeight = height), onTimelineHeightChangeEnd: () {}, onJunctionResizeEnd: () {}, timeline: const SizedBox.expand(), ), ); }, ), ), ), ); const boundaries = { 'sidebar': 'project-sidebar-resize', 'media': 'editor-media-resize', 'timeline': 'editor-timeline-resize', }; Future expectPainted(Set active) async { final render = repaintKey.currentContext!.findRenderObject()! as RenderRepaintBoundary; final image = (await tester.runAsync(render.toImage))!; final data = (await tester.runAsync( () => image.toByteData(format: ui.ImageByteFormat.rawRgba), ))!; Color pixel(Offset global) { final point = render.globalToLocal(global); final offset = (point.dy.floor() * image.width + point.dx.floor()) * 4; return Color.fromARGB( data.getUint8(offset + 3), data.getUint8(offset), data.getUint8(offset + 1), data.getUint8(offset + 2), ); } for (final entry in boundaries.entries) { final handle = find.byKey(Key(entry.value)); if (handle.evaluate().isEmpty) continue; final rect = tester.getRect(handle); final paint = tester.getRect( find.descendant(of: handle, matching: find.byType(DecoratedBox)), ); final color = pixel(paint.center); expect( color, active.contains(entry.key) ? accent : isNot(accent), reason: entry.key, ); if (active.contains(entry.key)) { final vertical = entry.key != 'timeline'; expect(vertical ? rect.width : rect.height, 10); expect(vertical ? paint.width : paint.height, 4); } } if (active.containsAll({'sidebar', 'timeline'})) { final sidebar = tester.getRect( find.byKey(const Key('project-sidebar-resize')), ); final timeline = tester.getRect( find.byKey(const Key('editor-timeline-resize')), ); if (sidebarCollapsed) { expect(sidebar.left, 0); expect(sidebar.width, 10); final verticalPaint = tester.getRect( find.descendant( of: find.byKey(const Key('project-sidebar-resize')), matching: find.byType(DecoratedBox), ), ); expect(verticalPaint.center.dx, timeline.left); expect( pixel(Offset(sidebar.center.dx, timeline.center.dy - 20)), isNot(accent), reason: 'Collapsed feedback sits at the edge, not the hit center', ); expect( pixel(Offset(timeline.left, timeline.center.dy - 20)), accent, ); } if (showMedia) { final media = tester.getRect( find.byKey(const Key('editor-media-resize')), ); expect( pixel(Offset(media.center.dx, timeline.center.dy)), accent, reason: 'The original media/preview/timeline T stays continuous', ); } } image.dispose(); } final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse); await mouse.addPointer(location: const Offset(1400, 50)); await tester.pumpAndSettle(); await expectPainted({}); await mouse.moveTo( tester.getCenter(find.byKey(const Key('editor-media-resize'))), ); await tester.pumpAndSettle(); await expectPainted({'media'}); await mouse.moveTo( tester.getCenter( find.byKey(const Key('editor-media-timeline-junction')), ), ); await tester.pumpAndSettle(); await expectPainted({'media', 'timeline'}); // Removing the junction leaves the pointer hovering over the timeline. update(() => showMedia = false); await tester.pumpAndSettle(); await expectPainted({'timeline'}); await mouse.moveTo(const Offset(1450, 50)); await tester.pumpAndSettle(); await expectPainted({}); update(() => showMedia = true); await tester.pumpAndSettle(); final junction = find.byKey(const Key('desktop-chat-timeline-junction')); await mouse.moveTo(tester.getCenter(junction)); await tester.pumpAndSettle(); await expectPainted({'sidebar', 'timeline'}); await mouse.down(tester.getCenter(junction)); await mouse.moveBy(const Offset(60, -40)); await tester.pumpAndSettle(); await mouse.moveTo(const Offset(1450, 50)); await tester.pumpAndSettle(); await expectPainted({'sidebar', 'timeline'}); await mouse.up(); await tester.pumpAndSettle(); await expectPainted({}); await mouse.moveTo(tester.getCenter(junction)); await mouse.down(tester.getCenter(junction)); await mouse.moveBy(const Offset(-300, 20)); await tester.pumpAndSettle(); expect(sidebarCollapsed, isTrue); await expectPainted({'sidebar', 'timeline'}); final node = tester.getRect( find.descendant(of: junction, matching: find.byType(DecoratedBox)), ); expect(node.center.dx, 0); expect(tester.getSize(junction).width, 10); // Reverse the same drag: feedback returns to the expanded boundary. await mouse.moveBy(const Offset(150, -20)); await tester.pumpAndSettle(); expect(sidebarCollapsed, isFalse); await expectPainted({'sidebar', 'timeline'}); final expandedNode = tester.getRect( find.descendant(of: junction, matching: find.byType(DecoratedBox)), ); expect(expandedNode.center.dx, tester.getCenter(junction).dx); await mouse.up(); await tester.pumpAndSettle(); await mouse.removePointer(); expect(tester.takeException(), isNull); }, ); testWidgets( 'media/timeline junction drags both splits and clamps each axis', (tester) async { tester.view.physicalSize = const Size(1100, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); var mediaWidth = 260.0; var timelineHeight = 280.0; var showMedia = true; var saves = 0; var singleAxisEnds = 0; late StateSetter update; await tester.pumpWidget( MaterialApp( home: StatefulBuilder( builder: (context, setState) { update = setState; return ExpandedEditorLayout( preview: const SizedBox.expand(), header: const SizedBox(height: 40), properties: const SizedBox.expand(), media: const SizedBox.expand(), showMedia: showMedia, showProperties: true, mediaWidth: mediaWidth, onMediaWidthChanged: (value) => setState(() => mediaWidth = value), onMediaWidthChangeEnd: () => singleAxisEnds++, timelineHeight: timelineHeight, onTimelineHeightChanged: (value) => setState(() => timelineHeight = value), onTimelineHeightChangeEnd: () => singleAxisEnds++, onJunctionResizeEnd: () => saves++, timeline: const SizedBox.expand(), ); }, ), ), ); final junction = find.byKey(const Key('editor-media-timeline-junction')); Rect card(String name) => tester.getRect(find.byKey(Key('editor-$name-card'))); final center = tester.getCenter(junction); expect(center.dx, (card('media').right + card('preview').left) / 2); expect(center.dy, (card('preview').bottom + card('timeline').top) / 2); await tester.drag(junction, const Offset(80, -60)); await tester.pumpAndSettle(); expect(card('media').width, 340); expect(card('timeline').height, 340); expect(saves, 1); expect(singleAxisEnds, 0); await tester.drag(junction, const Offset(-40, 30)); await tester.pumpAndSettle(); expect(card('media').width, 300); expect(card('timeline').height, 310); expect(saves, 2); await tester.drag(junction, const Offset(-2000, -2000)); await tester.pumpAndSettle(); expect(card('media').width, 220); expect(card('preview').height, 180); await tester.drag(junction, const Offset(80, -60)); await tester.pumpAndSettle(); expect(card('media').width, 300); expect(card('preview').height, 180); await tester.drag(junction, const Offset(2000, 2000)); await tester.pumpAndSettle(); expect(card('preview').width, 320); expect(card('timeline').height, 220); expect(saves, 5); expect(singleAxisEnds, 0); update(() => showMedia = false); await tester.pump(); expect(junction, findsNothing); expect(tester.takeException(), isNull); }, ); testWidgets('media width drags, respects other panels and survives hiding', ( tester, ) async { tester.view.physicalSize = const Size(1100, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); double? mediaWidth; var showMedia = true; var saves = 0; late StateSetter update; await tester.pumpWidget( MaterialApp( home: StatefulBuilder( builder: (context, setState) { update = setState; return ExpandedEditorLayout( preview: const SizedBox.expand(), header: const SizedBox(height: 40), properties: const SizedBox.expand(), media: const SizedBox.expand(key: Key('test-media')), showMedia: showMedia, showProperties: true, mediaWidth: mediaWidth, onMediaWidthChanged: (value) => setState(() => mediaWidth = value), onMediaWidthChangeEnd: () => saves++, timelineHeight: 220, onTimelineHeightChanged: (_) {}, onTimelineHeightChangeEnd: () {}, onJunctionResizeEnd: () {}, timeline: const SizedBox.expand(), ); }, ), ), ); Rect card(String name) => tester.getRect(find.byKey(Key('editor-$name-card'))); final resize = find.byKey(const Key('editor-media-resize')); final mediaElement = tester.element(find.byKey(const Key('test-media'))); expect(card('media').width, 260); expect(tester.getSize(resize).width, 10); expect(tester.getRect(resize).bottom, card('preview').bottom); await tester.dragFrom( tester.getCenter(resize) + const Offset(4, 0), const Offset(80, 0), ); await tester.pumpAndSettle(); expect(card('media').width, 340); expect(saves, 1); expect(card('preview').left - card('media').right, 2); expect(card('properties').width, 240); await tester.drag(resize, const Offset(2000, 0)); await tester.pumpAndSettle(); expect(card('preview').width, 320); expect(card('media').width, 536); await tester.drag(resize, const Offset(-2000, 0)); await tester.pumpAndSettle(); expect(card('media').width, 220); expect(saves, 3); update(() => mediaWidth = 400); await tester.pump(); tester.view.physicalSize = const Size(900, 800); await tester.pumpAndSettle(); expect(card('media').width, 336); expect(card('preview').width, 320); expect(mediaWidth, 400); tester.view.physicalSize = const Size(1100, 800); await tester.pumpAndSettle(); expect(card('media').width, 400); update(() => showMedia = false); await tester.pump(); expect(resize, findsNothing); update(() => showMedia = true); await tester.pump(); expect(card('media').width, 400); expect( tester.element(find.byKey(const Key('test-media'))), same(mediaElement), ); expect(saves, 3); expect(tester.takeException(), isNull); }); testWidgets('inspector has an independent frame aligned with the preview', ( tester, ) async { tester.view.physicalSize = const Size(1000, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); var showProperties = true; late StateSetter update; await tester.pumpWidget( MaterialApp( home: StatefulBuilder( builder: (context, setState) { update = setState; return ExpandedEditorLayout( preview: const SizedBox.expand(key: Key('test-preview')), header: const SizedBox(height: 40, key: Key('test-header')), properties: const SizedBox.expand(), media: const SizedBox(), showMedia: false, showProperties: showProperties, mediaWidth: null, onMediaWidthChanged: (_) {}, onMediaWidthChangeEnd: () {}, timelineHeight: 200, onTimelineHeightChanged: (_) {}, onTimelineHeightChangeEnd: () {}, onJunctionResizeEnd: () {}, timeline: const SizedBox(), ); }, ), ), ); final card = tester.getRect(find.byKey(const Key('editor-preview-card'))); final divider = tester.getRect( find.byKey(const Key('editor-properties-divider')), ); final properties = tester.getRect( find.byKey(const Key('editor-properties-region')), ); final header = tester.getRect(find.byKey(const Key('test-header'))); expect(divider.top, card.top); expect(divider.bottom, card.bottom); expect(properties.top, divider.top); expect(properties.bottom, divider.bottom); expect(header.right, divider.left - 1); expect(properties.left, divider.right); final preview = tester.element(find.byKey(const Key('test-preview'))); update(() => showProperties = false); await tester.pump(); expect(find.byKey(const Key('editor-properties-divider')), findsNothing); expect( tester.getRect(find.byKey(const Key('test-header'))).right, tester.getRect(find.byKey(const Key('editor-preview-card'))).right - 1, ); expect( tester.element(find.byKey(const Key('test-preview'))), same(preview), ); expect(tester.takeException(), isNull); }); testWidgets('preview and timeline cards resize through their narrow gap', ( tester, ) async { tester.view.physicalSize = const Size(700, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); double? timelineHeight; var saves = 0; await tester.pumpWidget( MaterialApp( home: StatefulBuilder( builder: (context, update) => ExpandedEditorLayout( preview: const Text('Preview'), header: const SizedBox(height: 46, child: Text('Preview controls')), properties: const SizedBox(), media: const SizedBox(), showMedia: false, showProperties: false, mediaWidth: null, onMediaWidthChanged: (_) {}, onMediaWidthChangeEnd: () {}, timelineHeight: timelineHeight, onTimelineHeightChanged: (value) => update(() => timelineHeight = value), onTimelineHeightChangeEnd: () => saves++, onJunctionResizeEnd: () {}, timeline: const Column( children: [ Text('Toolbar'), Expanded(child: Text('Timeline')), ], ), ), ), ), ); Rect card(String name) => tester.getRect(find.byKey(Key('editor-$name-card'))); final timelineBefore = card('timeline'); expect( timelineBefore.top - card('preview').bottom, WorkspaceLayoutMetrics.desktopSpacing, ); final resize = find.byKey(const Key('editor-timeline-resize')); expect( tester.getSize(resize).height, WorkspaceLayoutMetrics.desktopResizeHitExtent, ); expect( find.descendant( of: find.byKey(const Key('editor-timeline-card')), matching: find.text('Toolbar'), ), findsOneWidget, ); await tester.dragFrom( tester.getCenter(resize) + const Offset(0, -2), const Offset(0, -80), ); await tester.pumpAndSettle(); expect(card('timeline').height, timelineBefore.height + 80); expect( card('timeline').top - card('preview').bottom, WorkspaceLayoutMetrics.desktopSpacing, ); expect(saves, 1); await tester.dragFrom( tester.getCenter(resize) + const Offset(0, 2), const Offset(0, 20), ); await tester.pumpAndSettle(); expect(card('timeline').height, timelineBefore.height + 60); expect(saves, 2); await tester.dragFrom( tester.getCenter(resize) + const Offset(0, 6), const Offset(0, -30), ); await tester.pumpAndSettle(); expect(card('timeline').height, timelineBefore.height + 60); expect(saves, 2); expect(tester.takeException(), isNull); }); testWidgets( 'desktop cards keep their order and resize through both gutters', (tester) async { tester.view.physicalSize = const Size(1600, 900); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final controller = DesktopWorkspaceController(); addTearDown(controller.dispose); double sidebarWidth = 260; var mode = WorkspaceMode.agent; late StateSetter update; await tester.pumpWidget( MaterialApp( home: StatefulBuilder( builder: (context, setState) { update = setState; return DesktopWorkspaceShell( mode: mode, controller: controller, sidebarCollapsed: false, sidebarWidth: sidebarWidth, onSidebarCollapsedChanged: (_) {}, onSidebarWidthChanged: (value) => setState(() => sidebarWidth = value), sidebarBuilder: (_, _) => const Text('Projects'), agentBuilder: (_, _) => const Text('Agent'), editor: const Text('Editor'), scaffoldKey: GlobalKey(), navigationDrawer: const SizedBox(), ); }, ), ), ); Rect card(String name) => tester.getRect(find.byKey(Key('desktop-$name-card'))); final project = card('project'); final editor = card('editor'); final agent = card('agent'); expect( editor.left - project.right, WorkspaceLayoutMetrics.desktopSpacing, ); expect(agent.left - editor.right, WorkspaceLayoutMetrics.desktopSpacing); expect(project.top, editor.top); expect(editor.bottom, agent.bottom); final projectResize = find.byKey(const Key('project-sidebar-resize')); final agentResize = find.byKey(const Key('agent-panel-resize')); expect( tester.getSize(projectResize).width, WorkspaceLayoutMetrics.desktopResizeHitExtent, ); expect( tester.getSize(agentResize).width, WorkspaceLayoutMetrics.desktopResizeHitExtent, ); await tester.dragFrom( tester.getCenter(projectResize) + const Offset(-2, 0), const Offset(60, 0), ); await tester.pumpAndSettle(); expect(card('project').width, project.width + 60); expect(card('agent').width, agent.width); await tester.dragFrom( tester.getCenter(agentResize) + const Offset(2, 0), const Offset(-80, 0), ); await tester.pumpAndSettle(); expect(card('agent').width, agent.width + 80); expect(card('project').width, project.width + 60); expect( card('agent').left - card('editor').right, WorkspaceLayoutMetrics.desktopSpacing, ); await tester.dragFrom( tester.getCenter(projectResize) + const Offset(2, 0), const Offset(-20, 0), ); await tester.pumpAndSettle(); expect(card('project').width, project.width + 40); await tester.dragFrom( tester.getCenter(agentResize) + const Offset(-2, 0), const Offset(20, 0), ); await tester.pumpAndSettle(); expect(card('agent').width, agent.width + 60); await tester.dragFrom( tester.getCenter(projectResize) + const Offset(-6, 0), const Offset(30, 0), ); await tester.pumpAndSettle(); expect(card('project').width, project.width + 40); expect( card('editor').left - card('project').right, WorkspaceLayoutMetrics.desktopSpacing, ); controller.setAgentVisible(true); update(() => mode = WorkspaceMode.cut); await tester.pumpAndSettle(); expect(find.byKey(const Key('desktop-agent-card')), findsOneWidget); expect(card('editor').left, greaterThan(card('project').right)); expect(card('agent').left, greaterThan(card('editor').right)); final agentElement = tester.element(find.text('Agent')); await tester.drag(agentResize, const Offset(-2000, 0)); await tester.pumpAndSettle(); expect(find.text('Agent'), findsOneWidget); expect(tester.element(find.text('Agent')), same(agentElement)); expect(find.text('Editor'), findsOneWidget); expect(card('editor').width, 480); expect(agentResize, findsOneWidget); await tester.drag(agentResize, const Offset(600, 0)); await tester.pumpAndSettle(); expect(find.text('Agent'), findsOneWidget); expect(find.text('Editor'), findsOneWidget); expect(tester.takeException(), isNull); await tester.pumpWidget(const SizedBox()); }, ); }