import 'package:cutdex/app/editor_theme.dart'; import 'package:cutdex/features/properties/caption_properties.dart'; import 'package:cutdex/l10n/app_localizations.dart'; import 'package:cutdex/workspace/timeline_session.dart'; import 'package:cutdex_sdk/editor.dart'; import 'package:cutdex_sdk/timeline/model.dart'; import 'package:cutdex_ui/theme.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'support/preview_fixture.dart'; final _sequence = SequenceId('one'); void main() { for (final desktop in [false, true]) { for (final field in ['text', 'color', 'strokeColor']) { testWidgets( '$field draft cannot overwrite a newer caption edit desktop=$desktop', (tester) async { final (session, id, _) = await _fixture(tester); final host = _host(session, id, desktop: desktop); await tester.pumpWidget(host); await tester.pumpAndSettle(); final input = _input(id, field); final element = tester.element(input); await tester.enterText(input, _local(field)); await tester.runAsync(() async { session.setCaption(_sequence, _edit(id, field, _external(field))); await session.flush(); }); await tester.pump(); expect(tester.element(input), same(element)); expect( tester.widget(input).controller!.text, _external(field), ); await tester.testTextInput.receiveAction(TextInputAction.done); await tester.pump(); expect(_value(session, id, field), _external(field)); // A fresh draft in the new binding still commits normally. await tester.enterText(input, _local(field)); await tester.testTextInput.receiveAction(TextInputAction.done); await tester.pump(); expect(_value(session, id, field), _local(field)); expect(tester.takeException(), isNull); await tester.runAsync(session.flush); await tester.pumpWidget(const SizedBox()); }, ); } } testWidgets('ordinary rebuilds retain a valid unsubmitted caption draft', ( tester, ) async { final (session, id, _) = await _fixture(tester); await tester.pumpWidget(_host(session, id)); await tester.pumpAndSettle(); final input = _input(id, 'text'); await tester.enterText(input, 'Unsubmitted draft'); await tester.pumpWidget(_host(session, id)); await tester.pump(); expect( tester.widget(input).controller!.text, 'Unsubmitted draft', ); await tester.testTextInput.receiveAction(TextInputAction.done); await tester.pump(); expect(session.caption(_sequence, id).text, 'Unsubmitted draft'); await tester.runAsync(session.flush); await tester.pumpWidget(const SizedBox()); }); testWidgets('selection changes do not carry a draft into another caption', ( tester, ) async { final (session, first, second) = await _fixture(tester); await tester.pumpWidget(_host(session, first)); await tester.pumpAndSettle(); await tester.enterText(_input(first, 'text'), 'First cue draft'); await tester.pumpWidget(_host(session, second)); await tester.pumpAndSettle(); expect( tester.widget(_input(second, 'text')).controller!.text, 'Second caption', ); expect(session.caption(_sequence, second).text, 'Second caption'); expect(session.caption(_sequence, first).text, 'Original'); expect(tester.takeException(), isNull); await tester.pumpWidget(const SizedBox()); }); } Future<(EditorTimelineSession, String, String)> _fixture( WidgetTester tester, ) async { final fixture = (await tester.runAsync( () => createPreviewFixture(Uri.file('/fixture.mov')), ))!; addTearDown( () => tester.runAsync(() async { await fixture.workspace.close(); await fixture.directory.delete(recursive: true); }), ); final session = fixture.timeline; final ids = (await tester.runAsync(() async { final role = session.createCaptionTrack( _sequence, const CreateCaptionTrack(name: 'Captions', language: 'en'), )['roleId'] as String; final result = session.addCaptions( _sequence, AddCaptions( roleId: role, cues: [ CaptionCueInput( text: 'Original', start: CommandTime(0, 1), duration: CommandTime(1, 1), ), CaptionCueInput( text: 'Second caption', start: CommandTime(1, 1), duration: CommandTime(1, 1), ), ], ), ); await session.flush(); return (result['captionIds'] as List).cast(); }))!; return (session, ids[0], ids[1]); } Widget _host( EditorTimelineSession session, String id, { bool desktop = false, }) => MaterialApp( localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, theme: desktop ? AppTheme.propertiesPanelTheme(EditorTheme.desktop(EditorTheme.penDark)) : EditorTheme.penDark, home: Scaffold( body: CaptionProperties(session: session, sequence: _sequence, clipId: id), ), ); Finder _input(String id, String field) => find.descendant( of: find.byKey(ValueKey(('caption-$field', id))), matching: find.byType(TextField), ); String _local(String field) => field == 'text' ? 'Local draft' : '#123456'; String _external(String field) => field == 'text' ? 'New external edit' : '#ABCDEF'; SetCaption _edit(String id, String field, String value) => switch (field) { 'text' => SetCaption(captionId: id, text: value), 'color' => SetCaption(captionId: id, color: value), _ => SetCaption(captionId: id, strokeColor: value), }; String _value(EditorTimelineSession session, String id, String field) { final caption = session.caption(_sequence, id); return switch (field) { 'text' => caption.text, 'color' => caption.color, _ => caption.strokeColor, }; }