feat: Add tests for Wolf3dGuiApp and refactor game data picker management

- Introduced unit tests for the Wolf3dGuiApp to ensure proper directory configuration and audio management.
- Refactored the GameDataPickerManager to streamline directory and file selection processes.
- Removed obsolete GameDataPickerManager and Wolf3dAppManager classes to simplify the architecture.
- Enhanced the Wolf3dFlutterEngine to improve game version handling during save loading.
- Updated existing tests to reflect changes in the game data management structure.
- Removed unnecessary dependencies and cleaned up the codebase for better maintainability.

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-24 14:35:20 +01:00
parent b980174905
commit ce4dd8d61d
18 changed files with 1919 additions and 810 deletions
@@ -0,0 +1,316 @@
import 'dart:io';
import 'dart:typed_data';
import 'package:file_selector/file_selector.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart' as path;
import 'package:wolf_3d_dart/wolf_3d_data_types.dart';
import 'package:wolf_3d_dart/wolf_3d_engine.dart';
import 'package:wolf_3d_flutter/wolf_3d_flutter.dart';
import 'package:wolf_3d_gui/game_data_picker_manager.dart';
class _NoopAudio implements EngineAudio {
@override
WolfensteinData? activeGame;
@override
Future<void> debugSoundTest() async {}
@override
Future<void> init() async {}
@override
void playLevelMusic(Music music) {}
@override
void playMenuMusic() {}
@override
void playSoundEffect(SoundEffect effect) {}
@override
void playSoundEffectId(int sfxId) {}
@override
Future<void> stopAllAudio() async {}
@override
void stopMusic() {}
@override
void dispose() {}
}
class _RecordingEngine extends Wolf3dFlutterEngine {
_RecordingEngine({required _NoopAudio audio}) : super(audioBackend: audio);
int initCallCount = 0;
String? lastDirectory;
List<String> lastAdditionalDirectories = <String>[];
List<WolfensteinData> discoveredGames = <WolfensteinData>[];
@override
Future<Wolf3dFlutterEngine> init({
String? directory,
Iterable<String>? additionalDirectories,
}) async {
initCallCount++;
lastDirectory = directory;
lastAdditionalDirectories =
additionalDirectories?.toList(growable: false) ?? <String>[];
availableGames
..clear()
..addAll(discoveredGames);
return this;
}
}
void main() {
group('GameDataPickerManager', () {
test(
'pickGameDataDirectory scans selected directory and exposes ready version',
() async {
final engine = _RecordingEngine(audio: _NoopAudio());
int notifyCount = 0;
final Directory tempDir = await Directory.systemTemp.createTemp(
'wolf3d_scan_',
);
addTearDown(() async => tempDir.delete(recursive: true));
await _writeRetailFiles(tempDir.path, useMapTempAlias: false);
final manager = GameDataPickerManager(
engine: engine,
pickDirectory: ({String? confirmButtonText}) async =>
' ${tempDir.path} ',
computeChecksum: (_) async => DataVersion.version14Retail.checksum,
);
await manager.pickGameDataDirectory(
notifyChanged: () => notifyCount++,
);
expect(notifyCount, 3);
expect(manager.isLoadingGameData, isFalse);
expect(manager.pickerError, isNull);
expect(engine.initCallCount, 0);
expect(manager.scanResult, isNotNull);
expect(manager.scanResult!.readyVersions, hasLength(1));
expect(manager.selectedReadyVersion, GameVersion.retail);
},
);
test(
'pickGameDataFiles records checksum warnings for unknown builds',
() async {
final engine = _RecordingEngine(audio: _NoopAudio());
final Directory tempDir = await Directory.systemTemp.createTemp(
'wolf3d_warn_',
);
addTearDown(() async => tempDir.delete(recursive: true));
await _writeRetailFiles(tempDir.path, useMapTempAlias: false);
final manager = GameDataPickerManager(
engine: engine,
pickFiles: () async => <XFile>[
XFile(path.join(tempDir.path, 'VSWAP.WL6')),
XFile(path.join(tempDir.path, 'MAPHEAD.WL6')),
],
computeChecksum: (_) async => 'unknown-checksum',
);
await manager.pickGameDataFiles(notifyChanged: () {});
expect(engine.initCallCount, 0);
expect(manager.pickerError, isNull);
expect(manager.selectedReadyVersion, isNull);
expect(
manager.scanResult!.versions
.singleWhere(
(analysis) => analysis.version == GameVersion.retail,
)
.state,
GameDataVersionState.checksumWarning,
);
},
);
test('pickGameDataFiles reports missing directory paths', () async {
final engine = _RecordingEngine(audio: _NoopAudio());
final manager = GameDataPickerManager(
engine: engine,
pickFiles: () async => <XFile>[XFile('VSWAP.WL6')],
);
await manager.pickGameDataFiles(notifyChanged: () {});
expect(engine.initCallCount, 0);
expect(
manager.pickerError,
'Selected files do not expose local filesystem paths.',
);
});
test(
'useSelectedData reloads selected directories for the ready version',
() async {
final engine = _RecordingEngine(audio: _NoopAudio());
engine.discoveredGames = <WolfensteinData>[
_buildTestData(GameVersion.retail),
_buildTestData(GameVersion.shareware),
];
final Directory one = await Directory.systemTemp.createTemp(
'wolf3d_one_',
);
final Directory two = await Directory.systemTemp.createTemp(
'wolf3d_two_',
);
addTearDown(() async => one.delete(recursive: true));
addTearDown(() async => two.delete(recursive: true));
await _writeRetailFiles(one.path, useMapTempAlias: false);
await File(
path.join(two.path, 'VSWAP.WL1'),
).writeAsBytes(<int>[1, 2, 3]);
final manager = GameDataPickerManager(
engine: engine,
pickFiles: () async => <XFile>[
XFile(path.join(one.path, 'VSWAP.WL6')),
XFile(path.join(one.path, 'MAPHEAD.WL6')),
XFile(path.join(two.path, 'VSWAP.WL1')),
],
computeChecksum: (filePath) async => filePath.endsWith('WL6')
? DataVersion.version14Retail.checksum
: 'unknown-checksum',
);
await manager.pickGameDataFiles(notifyChanged: () {});
await manager.useSelectedData(notifyChanged: () {});
expect(engine.initCallCount, 1);
expect(engine.lastDirectory, one.path);
expect(engine.lastAdditionalDirectories, <String>[two.path]);
expect(engine.availableGames, hasLength(1));
expect(engine.availableGames.single.version, GameVersion.retail);
expect(engine.maybeActiveGame?.version, GameVersion.retail);
},
);
test(
'importSelectedData copies canonical files into config game_data folder',
() async {
final Directory sourceDir = await Directory.systemTemp.createTemp(
'wolf3d_source_',
);
final Directory importRoot = await Directory.systemTemp.createTemp(
'wolf3d_import_',
);
addTearDown(() async => sourceDir.delete(recursive: true));
addTearDown(() async => importRoot.delete(recursive: true));
await _writeRetailFiles(sourceDir.path, useMapTempAlias: true);
final engine = _RecordingEngine(
audio: _NoopAudio(),
);
final manager = GameDataPickerManager(
engine: engine,
pickDirectory: ({String? confirmButtonText}) async => sourceDir.path,
computeChecksum: (_) async => DataVersion.version10Retail.checksum,
importRootDirectory: importRoot.path,
);
await manager.pickGameDataDirectory(notifyChanged: () {});
await manager.importSelectedData(notifyChanged: () {});
final String importedDirectory = path.join(
importRoot.path,
'game_data',
'wl6',
);
expect(engine.initCallCount, 1);
expect(engine.lastDirectory, importedDirectory);
expect(
File(path.join(importedDirectory, 'GAMEMAPS.WL6')).existsSync(),
isTrue,
);
expect(
File(path.join(importedDirectory, 'MAPTEMP.WL6')).existsSync(),
isFalse,
);
expect(
File(path.join(importedDirectory, 'VSWAP.WL6')).existsSync(),
isTrue,
);
},
);
});
}
WolfensteinData _buildTestData(GameVersion version) {
final wallGrid = List.generate(64, (_) => List.filled(64, 0));
final objectGrid = List.generate(64, (_) => List.filled(64, 0));
for (int i = 0; i < 64; i++) {
wallGrid[0][i] = 2;
wallGrid[63][i] = 2;
wallGrid[i][0] = 2;
wallGrid[i][63] = 2;
}
objectGrid[2][2] = MapObject.playerEast;
return WolfensteinData(
version: version,
dataVersion: DataVersion.unknown,
registry: version == GameVersion.retail
? RetailAssetRegistry()
: SharewareAssetRegistry(),
walls: <Sprite>[_sprite(1), _sprite(1), _sprite(2), _sprite(2)],
sprites: List<Sprite>.generate(436, (_) => _sprite(255)),
sounds: List<PcmSound>.generate(200, (_) => PcmSound(Uint8List(1))),
adLibSounds: const <PcmSound>[],
music: const <ImfMusic>[],
vgaImages: const <VgaImage>[],
episodes: <Episode>[
Episode(
name: 'Test Episode',
levels: <WolfLevel>[
WolfLevel(
name: 'Test Level',
wallGrid: wallGrid,
areaGrid: List.generate(64, (_) => List.filled(64, -1)),
objectGrid: objectGrid,
music: Music.level01,
),
],
),
],
);
}
Sprite _sprite(int color) =>
Sprite(Uint8List.fromList(List<int>.filled(64 * 64, color)));
Future<void> _writeRetailFiles(
String directoryPath, {
required bool useMapTempAlias,
}) async {
final Map<String, List<int>> files = <String, List<int>>{
'VSWAP.WL6': <int>[1, 2, 3, 4],
'MAPHEAD.WL6': <int>[5],
...(useMapTempAlias
? <String, List<int>>{
'MAPTEMP.WL6': <int>[6],
}
: <String, List<int>>{
'GAMEMAPS.WL6': <int>[6],
}),
'VGADICT.WL6': <int>[7],
'VGAHEAD.WL6': <int>[8],
'VGAGRAPH.WL6': <int>[9],
'AUDIOHED.WL6': <int>[10],
'AUDIOT.WL6': <int>[11],
};
for (final MapEntry<String, List<int>> entry in files.entries) {
await File(path.join(directoryPath, entry.key)).writeAsBytes(entry.value);
}
}
@@ -0,0 +1,213 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:wolf_3d_dart/wolf_3d_data_types.dart';
import 'package:wolf_3d_dart/wolf_3d_engine.dart';
import 'package:wolf_3d_flutter/wolf_3d_flutter.dart';
import 'package:wolf_3d_gui/game_data_picker_manager.dart';
import 'package:wolf_3d_gui/wolf3d_gui_app.dart';
class _CountingAudio implements EngineAudio {
@override
WolfensteinData? activeGame;
int stopAllAudioCallCount = 0;
int disposeCallCount = 0;
@override
Future<void> debugSoundTest() async {}
@override
Future<void> init() async {}
@override
void playLevelMusic(Music music) {}
@override
void playMenuMusic() {}
@override
void playSoundEffect(SoundEffect effect) {}
@override
void playSoundEffectId(int sfxId) {}
@override
Future<void> stopAllAudio() async {
stopAllAudioCallCount++;
await Future<void>.delayed(const Duration(milliseconds: 1));
}
@override
void stopMusic() {}
@override
void dispose() {
disposeCallCount++;
}
}
class _NoopAudio implements EngineAudio {
@override
WolfensteinData? activeGame;
@override
Future<void> debugSoundTest() async {}
@override
Future<void> init() async {}
@override
void playLevelMusic(Music music) {}
@override
void playMenuMusic() {}
@override
void playSoundEffect(SoundEffect effect) {}
@override
void playSoundEffectId(int sfxId) {}
@override
Future<void> stopAllAudio() async {}
@override
void stopMusic() {}
@override
void dispose() {}
}
class _RecordingEngine extends Wolf3dFlutterEngine {
_RecordingEngine({required _NoopAudio audio}) : super(audioBackend: audio);
int initCallCount = 0;
List<WolfensteinData> discoveredGames = <WolfensteinData>[];
@override
Future<Wolf3dFlutterEngine> init({
String? directory,
Iterable<String>? additionalDirectories,
}) async {
initCallCount++;
availableGames
..clear()
..addAll(discoveredGames);
return this;
}
}
class _AutoUsePickerManager extends GameDataPickerManager {
_AutoUsePickerManager({required super.engine});
int pickDirectoryCallCount = 0;
int useSelectedDataCallCount = 0;
GameVersion? _selectedReadyVersion;
static final GameDataScanResult _singleReadyScan = GameDataScanResult(
scannedDirectories: const <String>['/tmp/wolf'],
versions: <GameDataVersionAnalysis>[
GameDataVersionAnalysis(
version: GameVersion.retail,
state: GameDataVersionState.ready,
files: GameFile.values
.map(
(GameFile file) => GameDataFileAnalysis(
file: file,
expectedName: '${file.baseName}.WL6',
state: GameDataFileState.ready,
sourcePath: '/tmp/wolf/${file.baseName}.WL6',
sourceName: '${file.baseName}.WL6',
),
)
.toList(growable: false),
dataVersion: DataVersion.version14Retail,
vswapChecksum: DataVersion.version14Retail.checksum,
),
],
);
@override
GameDataScanResult? get scanResult => _singleReadyScan;
@override
GameVersion? get selectedReadyVersion => _selectedReadyVersion;
@override
Future<void> pickGameDataDirectory({
required void Function() notifyChanged,
}) async {
pickDirectoryCallCount++;
notifyChanged();
}
@override
void selectReadyVersion(GameVersion version) {
_selectedReadyVersion = version;
}
@override
Future<void> useSelectedData({
required void Function() notifyChanged,
}) async {
useSelectedDataCallCount++;
notifyChanged();
}
}
void main() {
testWidgets('Wolf3dGuiApp forwards configured directory to no-data screen', (
tester,
) async {
final audio = _NoopAudio();
final wolf3d = Wolf3dFlutterEngine(audioBackend: audio)
..configuredDataDirectory = '/tmp/wolf-data';
await tester.pumpWidget(
MaterialApp(
home: Wolf3dGuiApp(engine: wolf3d),
),
);
expect(find.textContaining('Configured data directory:'), findsOneWidget);
expect(find.textContaining('/tmp/wolf-data'), findsOneWidget);
});
testWidgets('Wolf3dGuiApp dispose path shuts down audio', (tester) async {
final audio = _CountingAudio();
final wolf3d = Wolf3dFlutterEngine(audioBackend: audio);
await tester.pumpWidget(
MaterialApp(
home: Wolf3dGuiApp(engine: wolf3d),
),
);
await tester.pumpWidget(const MaterialApp(home: SizedBox.shrink()));
await tester.pump(const Duration(milliseconds: 10));
expect(audio.stopAllAudioCallCount, 1);
expect(audio.disposeCallCount, 1);
});
testWidgets('Wolf3dGuiApp auto-uses an unambiguous ready scan', (
tester,
) async {
final engine = _RecordingEngine(audio: _NoopAudio());
final manager = _AutoUsePickerManager(engine: engine);
await tester.pumpWidget(
MaterialApp(
home: Wolf3dGuiApp(engine: engine, pickerManager: manager),
),
);
await tester.tap(find.text('Select data directory'));
await tester.pump();
await tester.pump();
expect(manager.pickDirectoryCallCount, 1);
expect(manager.selectedReadyVersion, GameVersion.retail);
expect(manager.useSelectedDataCallCount, 1);
});
}