diet-guard/app/test/screens/body_screen_support.dart
Krzysztof kuhy Rudnicki 49144a2c49 Move the app to material_ui for Flutter 3.50
Flutter 3.50 deprecates package:flutter/material.dart; 73 deprecation
infos failed flutter analyze --fatal-infos, and four Dart pins were
behind (crdt_sync_flutter 0.8.2, flutter_local_notifications 23.0.2,
github_device_auth 0.5.0, sync_settings_ui 0.8.2). Those UI packages
now sit on material_ui, so the app has to as well or their widgets lose
MaterialLocalizations.

Imports swap one-for-one and dart fix re-sorted the directives; no
behaviour change. analyze is clean and all six test shards pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWWfb63jncj9Lca2jeX
2026-11-16 09:25:34 +01:00

127 lines
4.1 KiB
Dart

// Shared setup for the Body screen test files (split for the 250-line cap).
import 'dart:convert';
import 'package:diet_guard_app/models/local_time.dart';
import 'package:diet_guard_app/screens/body_screen.dart';
import 'package:diet_guard_app/services/app_settings_service.dart';
import 'package:diet_guard_app/services/body_service.dart';
import 'package:diet_guard_app/services/log_storage_service.dart';
import 'package:diet_guard_app/widgets/body/body_tile_grid.dart';
import 'package:material_ui/material_ui.dart';
import 'package:flutter_test/flutter_test.dart';
import '../label_finders.dart';
import '../services/body_test_support.dart';
export '../services/body_test_support.dart';
/// Points every service the screen reads at [store]; returns a teardown.
Future<void> setUpBodyServices(MemoryDocStore store) async {
LogStorageService.resetForTesting(store: store);
AppSettingsService.resetForTesting(store: store);
await BodyService.initForTesting(store);
}
/// Undoes [setUpBodyServices].
void tearDownBodyServices() {
BodyService.resetForTesting();
LogStorageService.resetForTesting();
AppSettingsService.resetForTesting();
}
/// Pumps the Body screen on a view tall enough to lay out every card.
Future<void> pumpBody(WidgetTester tester) async {
tester.view.physicalSize = const Size(1200, 12000);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
await tester.pumpWidget(const MaterialApp(home: BodyScreen()));
await tester.pumpAndSettle();
}
/// Yesterday's local date key.
String yesterdayKey() =>
localDateKey(DateTime.now().subtract(const Duration(days: 1)));
/// An 80 kg, 180 cm, 30-year-old man with a run, a lift and 9000 steps
/// yesterday; optionally a saved [goal].
Future<void> seedPerson(MemoryDocStore store, {Object? goal}) async {
final today = DateTime.now();
const t = '2026-09-01T10:00:00+02:00';
store.documents[BodyService.documentName] = jsonEncode({
'profile': {
'birth': {'v': localDateKey(DateTime(today.year - 30, 1, 1)), 't': t},
'height_cm': {'v': 180, 't': t},
'sex': {'v': 'm', 't': t},
if (goal != null) 'goal': {'v': goal, 't': t},
},
'weights': {
'2026-09-20': {'kg': 81, 'src': 'phone', 't': '2026-09-20T07:00:00Z'},
yesterdayKey(): {'kg': 80, 'src': 'manual', 't': '2026-09-20T07:00:00Z'},
},
'steps': {
yesterdayKey(): {'n': 9000, 't': t},
},
'activity': {
't': t,
'sessions': [
{
'day': yesterdayKey(),
'kind': 'run',
'min': 60,
'km': 10,
'label': 'Running 10.0 km',
'src': 'runnerup',
},
{
'day': yesterdayKey(),
'kind': 'strength',
'min': 80,
'label': 'StrongLifts workout A (80 min, all succeeded)',
'src': 'screen-locker',
},
{
'day': yesterdayKey(),
'kind': 'walk',
'min': 30,
'km': 2,
'label': 'walk to the shop',
'src': 'manual',
},
],
},
});
await BodyService.initForTesting(store);
}
/// The top edge of [finder]'s first match.
double topOf(WidgetTester tester, Finder finder) =>
tester.getTopLeft(finder.first).dy;
/// The overview tile spoken as [title].
Finder tile(String title) =>
find.descendant(of: find.byType(BodyTileGrid), matching: bySemLabel(title));
/// Opens the sheet behind the tile spoken as [title].
Future<void> openTile(WidgetTester tester, String title) async {
await tester.tap(tile(title));
await tester.pumpAndSettle();
}
/// Dismisses the open sheet by tapping its scrim.
Future<void> closeSheet(WidgetTester tester) async {
await tester.tapAt(const Offset(5, 5));
await tester.pumpAndSettle();
}
/// [finder] restricted to the open sheet (the tiles stay mounted behind it).
Finder inSheet(Finder finder) =>
find.descendant(of: find.byType(BottomSheet), matching: finder);
/// Spoken names of the overview tiles, in display order.
List<String> tileTitles(WidgetTester tester) => tester
.widget<BodyTileGrid>(find.byType(BodyTileGrid))
.tiles
.map((t) => t.semanticLabel)
.toList();