Files
SpotiFLAC-Mobile/test/audio_analysis_spectrogram_export_test.dart
T
zarzet 0afdacae76 feat(analysis): save the spectrogram as a PNG image
The spectrogram card gets a Save Spectrogram button. It renders the plot
as the card shows it (frequency and time axes, grid and dBFS legend)
under the track name and sample rate, then opens the system save dialog:
the save panel on desktop, the document picker on Android and iOS.
Files are named after the track, or the audio file when no name is
given, with the channel appended when one is selected.
2026-10-22 15:31:45 +07:00

82 lines
2.4 KiB
Dart

import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mornye/widgets/audio_analysis_widget.dart';
Future<ui.Image> _plotImage() async {
final recorder = ui.PictureRecorder();
Canvas(recorder).drawRect(
const Rect.fromLTWH(0, 0, 160, 80),
Paint()..color = Colors.orange,
);
final picture = recorder.endRecording();
final image = await picture.toImage(160, 80);
picture.dispose();
return image;
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('spectrogram export name', () {
test('prefers the given name and strips characters files cannot hold', () {
expect(
audioSpectrogramExportBaseName(
'/music/a.flac',
exportName: 'Artist: A/B - Title?',
),
'Artist_ A_B - Title_',
);
});
test('falls back to the file name without its extension', () {
expect(audioSpectrogramExportBaseName('/music/Song.flac'), 'Song');
expect(
audioSpectrogramExportBaseName(r'C:\Music\Song.flac', exportName: ' '),
'Song',
);
expect(
audioSpectrogramExportBaseName('smb://nas/share/Album/Song%201.m4a'),
'Song 1',
);
});
test('uses the document name of a content URI', () {
expect(
audioSpectrogramExportBaseName(
'content://com.android.externalstorage.documents/tree/'
'primary%3AMusic/document/primary%3AMusic%2FAlbum%2FSong.flac',
),
'Song',
);
});
test('never returns an empty name', () {
expect(audioSpectrogramExportBaseName('/music/'), 'audio');
expect(audioSpectrogramExportBaseName('/music/...'), 'audio');
});
});
test('renders the plot with axes and legend as a PNG', () async {
final plot = await _plotImage();
addTearDown(plot.dispose);
final bytes = await renderAudioSpectrogramPng(
image: plot,
title: 'Artist - Title',
sampleRate: 44100,
duration: 200,
details: const ['Sample Rate: 44100 Hz'],
);
expect(bytes.sublist(0, 4), [0x89, 0x50, 0x4e, 0x47]);
final codec = await ui.instantiateImageCodec(bytes);
final frame = await codec.getNextFrame();
addTearDown(frame.image.dispose);
// 16 + 34 + 800 + 16 logical pixels across, at the default 2x scale.
expect(frame.image.width, 1732);
expect(frame.image.height, greaterThan(800 * 1.1));
});
}