muzzle-velocity/test/note_list_harness.dart

56 lines
1.9 KiB
Dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:muzzle_velocity/note_list.dart';
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'package:shared_preferences/shared_preferences.dart';
class _FakePathProvider extends PathProviderPlatform
with MockPlatformInterfaceMixin {
_FakePathProvider(this.path);
final String path;
@override
Future<String?> getApplicationDocumentsPath() async => path;
}
/// Creates a temporary notes folder with [files] (relative path to content)
/// and makes it the app's default notes directory.
Directory setUpNotes(Map<String, String> files) {
final notes = Directory.systemTemp.createTempSync('mv-list');
files.forEach((name, content) {
File('${notes.path}/$name')
..createSync(recursive: true)
..writeAsStringSync(content);
});
PathProviderPlatform.instance = _FakePathProvider(notes.path);
SharedPreferences.setMockInitialValues({'initialized': true});
return notes;
}
/// Pumps the note list, letting its real file I/O finish.
Future<void> pumpList(
WidgetTester tester, {
void Function(bool)? toggleTheme,
VoidCallback? toggleTags,
}) async {
await tester.runAsync(() async {
await tester.pumpWidget(MaterialApp(
home: NoteListScreen(
toggleTheme: toggleTheme ?? (_) {},
toggleTags: toggleTags ?? () {},
isDarkMode: false,
showTags: true,
),
));
await Future<void>.delayed(const Duration(milliseconds: 200));
});
await tester.pumpAndSettle();
}
/// Titles of the notes currently listed.
List<String> listedNotes(WidgetTester tester) => [
for (final tile in tester.widgetList<ListTile>(find.byType(ListTile)))
(tile.title as Text).data!,
];