Add the Flutter video editing application with project management, media import, timeline editing, preview, AI assistance, diagnostics and settings. Include shared UI and native host packages, platform runners, application tests and release tooling. Provide usage and contribution guides, the MIT license and third-party notices. Exclude local credentials, build outputs and validation data.
309 lines
10 KiB
Dart
309 lines
10 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:cutdex/features/timeline/timeline_drop_preparation.dart';
|
|
import 'package:cutdex/workspace/project_state.dart';
|
|
import 'package:cutdex/workspace/preview_target.dart';
|
|
import 'package:cutdex/workspace/projects.dart';
|
|
import 'package:cutdex/workspace/timeline_session.dart';
|
|
import 'package:cutdex_sdk/project.dart';
|
|
import 'package:cutdex_sdk/runtime.dart';
|
|
import 'package:cutdex_sdk/editor.dart';
|
|
import 'package:cutdex_sdk/timeline/model.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import '../../support/preview_fixture.dart';
|
|
|
|
AssetDocument _image(String id) => AssetDocument(
|
|
id: id,
|
|
name: id,
|
|
storage: AssetStorage.external,
|
|
location: Uri.file('/$id.png').toString(),
|
|
metadata: const {'kind': 'image'},
|
|
);
|
|
|
|
class _Projects extends Projects {
|
|
_Projects(this._session);
|
|
final EditorTimelineSession _session;
|
|
Completer<MediaReference>? _gate;
|
|
Completer<void>? _acquiring;
|
|
@override
|
|
Future<ProjectState> build() async => ProjectState(
|
|
activeProject: const RecentProject(
|
|
id: 'project',
|
|
name: 'Project',
|
|
path: '/project',
|
|
),
|
|
timeline: _session.document,
|
|
);
|
|
@override
|
|
Future<T> withAssetMedia<T>(
|
|
String? projectId,
|
|
AssetDocument asset,
|
|
Future<T> Function(MediaReference) action,
|
|
) async {
|
|
if (_acquiring?.isCompleted == false) _acquiring!.complete();
|
|
final media = _gate == null
|
|
? MediaReference(id: asset.id, uri: Uri.parse(asset.location))
|
|
: await _gate!.future;
|
|
return action(media);
|
|
}
|
|
|
|
void leave() =>
|
|
state = AsyncData(state.requireValue.copyWith(activeProject: null));
|
|
}
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
test(
|
|
'probe prepares validated metadata before placement or commit',
|
|
() async {
|
|
final fixture = await createPreviewFixture(Uri.file('/source.mpg'));
|
|
final projects = _Projects(fixture.timeline);
|
|
const channel = MethodChannel('prepared-drop-test');
|
|
final messenger =
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
|
var probes = 0;
|
|
var response = <String, Object?>{
|
|
'duration': {'numerator': '64737467444', 'denominator': 10000000},
|
|
'videoTrackCount': 1,
|
|
'audioTrackCount': 0,
|
|
'videoTrackIds': [0],
|
|
'audioTrackIds': <int>[],
|
|
'audioTracks': <Object?>[],
|
|
'isProtected': false,
|
|
};
|
|
messenger.setMockMethodCallHandler(channel, (call) async {
|
|
if (call.method == 'request') {
|
|
final request = jsonDecode(
|
|
(call.arguments as Map)['request'] as String,
|
|
);
|
|
expect(request['method'], 'probe');
|
|
} else {
|
|
expect(call.method, 'probe');
|
|
}
|
|
probes++;
|
|
return call.method == 'request'
|
|
? jsonEncode({'value': response})
|
|
: response;
|
|
});
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
projectsProvider.overrideWith(() => projects),
|
|
runtimeConnectionProvider.overrideWithValue(
|
|
CutdexRuntime(channel: channel),
|
|
),
|
|
],
|
|
);
|
|
addTearDown(() async {
|
|
container.dispose();
|
|
messenger.setMockMethodCallHandler(channel, null);
|
|
await fixture.workspace.close();
|
|
await fixture.directory.delete(recursive: true);
|
|
});
|
|
await container.read(projectsProvider.future);
|
|
final provider = timelineDropPreparationProvider(
|
|
'project',
|
|
fixture.timeline,
|
|
fixture.timeline.activeSequenceId!,
|
|
);
|
|
container.listen(provider, (_, _) {});
|
|
final controller = container.read(provider.notifier);
|
|
final source = TimelineDropSource.asset(
|
|
AssetDocument(
|
|
id: 'source',
|
|
name: 'Source',
|
|
storage: AssetStorage.external,
|
|
location: Uri.file('/source.mpg').toString(),
|
|
),
|
|
);
|
|
await controller.probe(source);
|
|
final prepared = container.read(provider).preparation.requireValue!;
|
|
expect(prepared.metadata.kind, EditorMediaKind.video);
|
|
expect(prepared.metadata.hasAudio, isFalse);
|
|
expect(
|
|
TimelineClock.standard.toTicks(prepared.metadata.duration),
|
|
9135751405697,
|
|
);
|
|
await controller.commit((_, _) async {}, isCurrent: () => true);
|
|
expect(probes, 1);
|
|
|
|
// Unusable metadata must never reach placement validation or insertion.
|
|
for (final changes in <Map<String, Object?>>[
|
|
{
|
|
'duration': {'numerator': '1', 'denominator': 0xffffffff},
|
|
},
|
|
{
|
|
'duration': {'numerator': '9223372036854775807', 'denominator': 1},
|
|
},
|
|
{'isProtected': true},
|
|
{'videoTrackCount': 0, 'videoTrackIds': <int>[]},
|
|
]) {
|
|
response = {...response, ...changes};
|
|
await controller.probe(source);
|
|
expect(container.read(provider).preparation.hasError, isTrue);
|
|
expect(container.read(provider).preparation.value, isNull);
|
|
final preparationError = container.read(provider).preparation.error;
|
|
var validations = 0;
|
|
var inserts = 0;
|
|
await expectLater(
|
|
controller.commit(
|
|
(_, _) async {
|
|
inserts++;
|
|
},
|
|
isCurrent: () => true,
|
|
validatePlacement: () {
|
|
validations++;
|
|
},
|
|
),
|
|
throwsA(same(preparationError)),
|
|
);
|
|
expect(validations, 0);
|
|
expect(inserts, 0);
|
|
expect(fixture.timeline.dirty, isFalse);
|
|
response = {
|
|
...response,
|
|
'duration': {'numerator': '64737467444', 'denominator': 10000000},
|
|
'videoTrackCount': 1,
|
|
'videoTrackIds': [0],
|
|
'isProtected': false,
|
|
};
|
|
}
|
|
},
|
|
);
|
|
|
|
test(
|
|
'a late probe cannot replace or delay a newer incoming source',
|
|
() async {
|
|
final fixture = await createPreviewFixture(Uri.file('/unused.mov'));
|
|
final projects = _Projects(fixture.timeline);
|
|
final container = ProviderContainer(
|
|
overrides: [projectsProvider.overrideWith(() => projects)],
|
|
);
|
|
addTearDown(() async {
|
|
container.dispose();
|
|
await fixture.workspace.close();
|
|
await fixture.directory.delete(recursive: true);
|
|
});
|
|
await container.read(projectsProvider.future);
|
|
final provider = timelineDropPreparationProvider(
|
|
'project',
|
|
fixture.timeline,
|
|
fixture.timeline.activeSequenceId!,
|
|
);
|
|
container.listen(provider, (_, _) {});
|
|
final controller = container.read(provider.notifier);
|
|
final delayed = projects._gate = Completer<MediaReference>();
|
|
final old = controller.probe(TimelineDropSource.asset(_image('old')));
|
|
var oldFinished = false;
|
|
final oldCompletion = controller.waitForCompletion().then((_) {
|
|
oldFinished = true;
|
|
});
|
|
projects._gate = null;
|
|
await controller.probe(TimelineDropSource.asset(_image('new')));
|
|
await controller.waitForCompletion();
|
|
expect(oldFinished, isFalse);
|
|
delayed.complete(MediaReference(id: 'old', uri: Uri.file('/old.png')));
|
|
await old;
|
|
await oldCompletion;
|
|
expect(oldFinished, isTrue);
|
|
expect(
|
|
container.read(provider).preparation.requireValue!.media.id,
|
|
'new',
|
|
);
|
|
},
|
|
);
|
|
|
|
test('a drop waits for its probe and ignores a duplicate commit', () async {
|
|
final fixture = await createPreviewFixture(Uri.file('/unused.mov'));
|
|
final projects = _Projects(fixture.timeline);
|
|
final container = ProviderContainer(
|
|
overrides: [projectsProvider.overrideWith(() => projects)],
|
|
);
|
|
addTearDown(() async {
|
|
container.dispose();
|
|
await fixture.workspace.close();
|
|
await fixture.directory.delete(recursive: true);
|
|
});
|
|
await container.read(projectsProvider.future);
|
|
final provider = timelineDropPreparationProvider(
|
|
'project',
|
|
fixture.timeline,
|
|
fixture.timeline.activeSequenceId!,
|
|
);
|
|
container.listen(provider, (_, _) {});
|
|
final controller = container.read(provider.notifier);
|
|
final delayed = projects._gate = Completer<MediaReference>();
|
|
final probe = controller.probe(TimelineDropSource.asset(_image('source')));
|
|
var inserts = 0;
|
|
var validations = 0;
|
|
final commit = controller.commit(
|
|
(_, _) async {
|
|
inserts++;
|
|
},
|
|
isCurrent: () => true,
|
|
validatePlacement: () {
|
|
validations++;
|
|
},
|
|
);
|
|
expect(container.read(provider).commit.isLoading, isTrue);
|
|
expect(inserts, 0);
|
|
await controller.commit((_, _) async {
|
|
inserts++;
|
|
}, isCurrent: () => true);
|
|
expect(inserts, 0);
|
|
projects._gate = null;
|
|
delayed.complete(
|
|
MediaReference(id: 'source', uri: Uri.file('/source.png')),
|
|
);
|
|
await probe;
|
|
await commit;
|
|
expect(validations, 1);
|
|
expect(inserts, 1);
|
|
expect(container.read(provider).commit.hasValue, isTrue);
|
|
expect(container.read(provider).commit.isLoading, isFalse);
|
|
});
|
|
|
|
test(
|
|
'a project switch during media acquisition prevents committing a drag',
|
|
() async {
|
|
final fixture = await createPreviewFixture(Uri.file('/unused.mov'));
|
|
final projects = _Projects(fixture.timeline);
|
|
final container = ProviderContainer(
|
|
overrides: [projectsProvider.overrideWith(() => projects)],
|
|
);
|
|
addTearDown(() async {
|
|
container.dispose();
|
|
await fixture.workspace.close();
|
|
await fixture.directory.delete(recursive: true);
|
|
});
|
|
await container.read(projectsProvider.future);
|
|
final provider = timelineDropPreparationProvider(
|
|
'project',
|
|
fixture.timeline,
|
|
fixture.timeline.activeSequenceId!,
|
|
);
|
|
container.listen(provider, (_, _) {});
|
|
final controller = container.read(provider.notifier);
|
|
await controller.probe(TimelineDropSource.asset(_image('source')));
|
|
final delayed = projects._gate = Completer<MediaReference>();
|
|
final acquiring = projects._acquiring = Completer<void>();
|
|
var inserted = false;
|
|
final pending = controller.commit((_, _) async {
|
|
inserted = true;
|
|
}, isCurrent: () => true);
|
|
await acquiring.future;
|
|
projects.leave();
|
|
delayed.complete(
|
|
MediaReference(id: 'source', uri: Uri.file('/source.png')),
|
|
);
|
|
await pending;
|
|
expect(inserted, isFalse);
|
|
expect(fixture.timeline.dirty, isFalse);
|
|
},
|
|
);
|
|
}
|