Files
BadNote/test/pen_slots_test.dart

76 lines
2.3 KiB
Dart
Raw Normal View History

// Unit tests for PenSlot / PenSlotsController persistence and independence.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:badnote/editor/engine/brush.dart';
import 'package:badnote/editor/input/pen_slots.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
setUp(() {
SharedPreferences.setMockInitialValues({});
});
group('PenSlot toJson / fromJson', () {
test('round-trips defaults', () {
for (final slot in kDefaultPenSlots()) {
final restored = PenSlot.fromJson(slot.toJson());
expect(restored, slot);
}
});
});
group('PenSlotsController', () {
test('seeds three default pens on first load', () async {
final c = await PenSlotsController.load();
expect(c.slots.length, 3);
expect(c.active.brush, BrushKind.fountainPen);
expect(c.active.color, Colors.black);
expect(c.active.width, 0.006);
expect(c.slots[1].brush, BrushKind.ballpoint);
expect(c.slots[1].width, 0.0022);
expect(c.slots[2].brush, BrushKind.pencil);
expect(c.slots[2].width, 0.003);
c.dispose();
});
test('select restores brush + color + width as a unit', () async {
final c = await PenSlotsController.load();
await c.select('slot_1');
expect(c.active.brush, BrushKind.ballpoint);
expect(c.active.color, Colors.blue);
expect(c.active.width, 0.0022);
await c.setActiveColor(Colors.red);
await c.setActiveWidth(0.01);
await c.select('slot_0');
expect(c.active.color, Colors.black);
expect(c.active.width, 0.006);
await c.select('slot_1');
expect(c.active.color, Colors.red);
expect(c.active.width, 0.01);
c.dispose();
});
test('persists across load()', () async {
final first = await PenSlotsController.load();
await first.select('slot_2');
await first.setActiveColor(Colors.purple);
await first.setActiveWidth(0.008);
first.dispose();
final second = await PenSlotsController.load();
expect(second.activeId, 'slot_2');
expect(second.active.brush, BrushKind.pencil);
expect(second.active.color.toARGB32(), Colors.purple.toARGB32());
expect(second.active.width, 0.008);
second.dispose();
});
});
}