Skip to content

Commit db735ed

Browse files
committed
migration: golden_toolkit to flutter_tests
1 parent 49e8e53 commit db735ed

10 files changed

+98
-46
lines changed

pubspec.lock

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -599,14 +599,6 @@ packages:
599599
url: "https://pub.dev"
600600
source: hosted
601601
version: "2.1.3"
602-
golden_toolkit:
603-
dependency: "direct dev"
604-
description:
605-
name: golden_toolkit
606-
sha256: "8f74adab33154fe7b731395782797021f97d2edc52f7bfb85ff4f1b5c4a215f0"
607-
url: "https://pub.dev"
608-
source: hosted
609-
version: "0.15.0"
610602
graphs:
611603
dependency: transitive
612604
description:

pubspec.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ dev_dependencies:
8383
drift_dev: ^2.26.0
8484
flutter_lints: ^5.0.0
8585
freezed: ^2.5.7
86-
golden_toolkit: ^0.15.0
8786
json_serializable: ^6.9.4
8887
mockito: ^5.4.4
8988
network_image_mock: ^2.1.1
-39.9 KB
Loading
-35.9 KB
Loading
-44.5 KB
Loading

test/nutrition/nutritional_plan_screen_test.dart

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
import 'dart:io';
20-
2119
import 'package:drift/native.dart';
2220
import 'package:flutter/material.dart';
2321
import 'package:flutter_test/flutter_test.dart';
24-
import 'package:golden_toolkit/golden_toolkit.dart';
2522
import 'package:http/http.dart' as http;
2623
import 'package:mockito/annotations.dart';
2724
import 'package:provider/provider.dart';
@@ -34,6 +31,7 @@ import 'package:wger/providers/nutrition.dart';
3431
import 'package:wger/screens/nutritional_plan_screen.dart';
3532

3633
import '../../test_data/nutritional_plans.dart';
34+
import '../utils.dart';
3735
import 'nutritional_plan_screen_test.mocks.dart';
3836

3937
@GenerateMocks([WgerBaseProvider, AuthProvider, http.Client])
@@ -67,6 +65,7 @@ void main() {
6765
),
6866
],
6967
child: MaterialApp(
68+
key: GlobalKey(),
7069
locale: Locale(locale),
7170
localizationsDelegates: AppLocalizations.localizationsDelegates,
7271
supportedLocales: AppLocalizations.supportedLocales,
@@ -84,40 +83,38 @@ void main() {
8483
);
8584
}
8685

87-
testGoldens(
86+
testWidgets(
8887
'Test the widgets on the nutritional plan screen',
8988
(tester) async {
9089
await loadAppFonts();
91-
final globalKey = GlobalKey();
92-
await tester.pumpWidgetBuilder(
93-
Material(key: globalKey),
94-
wrapper: materialAppWrapper(
95-
localizations: [AppLocalizations.delegate],
96-
),
97-
surfaceSize: const Size(500, 1000),
98-
);
90+
91+
tester.view.physicalSize = const Size(500, 1000);
92+
tester.view.devicePixelRatio = 1.0; // Ensure correct pixel ratio
93+
9994
await tester.pumpWidget(createNutritionalPlan());
10095
await tester.tap(find.byType(TextButton));
10196
await tester.pumpAndSettle();
10297

103-
await screenMatchesGolden(tester, 'nutritional_plan_1_default_view', skip: !Platform.isLinux);
98+
await expectLater(find.byType(NutritionalPlanScreen),
99+
matchesGoldenFile('goldens/nutritional_plan_1_default_view.png'));
104100

105101
// Default view shows plan description, info button, and no ingredients
106102
expect(find.text('Less fat, more protein'), findsOneWidget);
107-
expect(find.byIcon(Icons.info_outline), findsNWidgets(3)); // 2 meals, 1 "other logs"
103+
expect(find.byIcon(Icons.info_outline),
104+
findsNWidgets(3)); // 2 meals, 1 "other logs"
108105
expect(find.byIcon(Icons.info), findsNothing);
109106
expect(find.text('100g Water'), findsNothing);
110107
expect(find.text('75g Burger soup'), findsNothing);
111108

112109
// tap the first info button changes it and reveals ingredients for the first meal
113110
var infoOutlineButtons = find.byIcon(Icons.info_outline);
114-
await tester.tap(infoOutlineButtons.first); // 2nd button shows up also, but is off-screen
111+
await tester.tap(infoOutlineButtons
112+
.first); // 2nd button shows up also, but is off-screen
115113
await tester.pumpAndSettle();
116-
await screenMatchesGolden(
117-
tester,
118-
'nutritional_plan_2_one_meal_with_ingredients',
119-
skip: !Platform.isLinux,
120-
);
114+
await expectLater(
115+
find.byType(NutritionalPlanScreen),
116+
matchesGoldenFile(
117+
'goldens/nutritional_plan_2_one_meal_with_ingredients.png'));
121118

122119
// Ingredients show up now
123120
expect(find.text('100g Water'), findsOneWidget);
@@ -136,30 +133,36 @@ void main() {
136133

137134
await tester.tap(infoOutlineButtons.first);
138135
await tester.pumpAndSettle();
139-
await screenMatchesGolden(
140-
tester,
141-
'nutritional_plan_3_both_meals_with_ingredients',
142-
skip: !Platform.isLinux,
143-
);
136+
await expectLater(
137+
find.byType(NutritionalPlanScreen),
138+
matchesGoldenFile(
139+
'goldens/nutritional_plan_3_both_meals_with_ingredients.png'));
140+
144141
expect(find.byIcon(Icons.info_outline), findsOneWidget);
145142
expect(find.byIcon(Icons.info), findsNWidgets(2));
146143

147144
await tester.scrollUntilVisible(find.text('300g Broccoli cake'), 30);
148145
expect(find.text('300g Broccoli cake'), findsOneWidget);
149146

150147
expect(find.byType(Card), findsNWidgets(3));
148+
149+
// Restore the original window size.
150+
tester.view.resetPhysicalSize();
151+
tester.view.resetDevicePixelRatio();
151152
},
152153
);
153154

154-
testWidgets('Tests the localization of times - EN', (WidgetTester tester) async {
155+
testWidgets('Tests the localization of times - EN',
156+
(WidgetTester tester) async {
155157
await tester.pumpWidget(createNutritionalPlan());
156158
await tester.tap(find.byType(TextButton));
157159
await tester.pumpAndSettle();
158160

159161
expect(find.textContaining('5:00 PM'), findsOneWidget);
160162
});
161163

162-
testWidgets('Tests the localization of times - DE', (WidgetTester tester) async {
164+
testWidgets('Tests the localization of times - DE',
165+
(WidgetTester tester) async {
163166
await tester.pumpWidget(createNutritionalPlan(locale: 'de'));
164167
await tester.tap(find.byType(TextButton));
165168
await tester.pumpAndSettle();

test/utils.dart

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
import 'dart:convert';
20+
21+
import 'package:flutter/services.dart';
22+
import 'package:flutter_test/flutter_test.dart';
1923
import 'package:wger/providers/auth.dart';
2024
import 'package:wger/providers/exercises.dart';
2125

@@ -30,3 +34,57 @@ final AuthProvider testAuthProvider = AuthProvider(MockClient(), false)
3034
// Test Exercises provider
3135
final mockBaseProvider = MockWgerBaseProvider();
3236
final ExercisesProvider testExercisesProvider = ExercisesProvider(mockBaseProvider);
37+
38+
// Load app fonts
39+
Future<void> loadAppFonts() async {
40+
TestWidgetsFlutterBinding.ensureInitialized();
41+
42+
String derivedFontFamily(Map<String, dynamic> fontDefinition) {
43+
const List<String> overridableFonts = <String>[
44+
'Roboto',
45+
'.SF UI Display',
46+
'.SF UI Text',
47+
'.SF Pro Text',
48+
'.SF Pro Display',
49+
];
50+
51+
if (!fontDefinition.containsKey('family')) {
52+
return '';
53+
}
54+
55+
final String fontFamily = fontDefinition['family'];
56+
57+
if (overridableFonts.contains(fontFamily)) {
58+
return fontFamily;
59+
}
60+
61+
if (fontFamily.startsWith('packages/')) {
62+
final fontFamilyName = fontFamily.split('/').last;
63+
if (overridableFonts.any((font) => font == fontFamilyName)) {
64+
return fontFamilyName;
65+
}
66+
} else {
67+
for (final Map<String, dynamic> fontType in fontDefinition['fonts']) {
68+
final String? asset = fontType['asset'];
69+
if (asset != null && asset.startsWith('packages')) {
70+
final packageName = asset.split('/')[1];
71+
return 'packages/$packageName/$fontFamily';
72+
}
73+
}
74+
}
75+
return fontFamily;
76+
}
77+
78+
final fontManifest = await rootBundle.loadStructuredData<Iterable<dynamic>>(
79+
'FontManifest.json',
80+
(string) async => json.decode(string),
81+
);
82+
83+
for (final Map<String, dynamic> font in fontManifest) {
84+
final fontLoader = FontLoader(derivedFontFamily(font));
85+
for (final Map<String, dynamic> fontType in font['fonts']) {
86+
fontLoader.addFont(rootBundle.load(fontType['asset']));
87+
}
88+
await fontLoader.load();
89+
}
90+
}
-163 KB
Loading

test/workout/routine_logs_screen_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
import 'dart:io';
20-
2119
import 'package:flutter/material.dart';
2220
import 'package:flutter_test/flutter_test.dart';
23-
import 'package:golden_toolkit/golden_toolkit.dart';
2421
import 'package:mockito/annotations.dart';
2522
import 'package:provider/provider.dart';
2623
import 'package:wger/l10n/generated/app_localizations.dart';
@@ -30,6 +27,7 @@ import 'package:wger/screens/routine_logs_screen.dart';
3027
import 'package:wger/screens/routine_screen.dart';
3128

3229
import '../../test_data/routines.dart';
30+
import '../utils.dart';
3331
import 'routine_logs_screen_test.mocks.dart';
3432

3533
@GenerateMocks([RoutinesProvider])
@@ -68,13 +66,15 @@ void main() {
6866
);
6967
}
7068

71-
testGoldens('Test the widgets on the routine logs screen', (WidgetTester tester) async {
69+
testWidgets('Test the widgets on the routine logs screen',
70+
(WidgetTester tester) async {
7271
await loadAppFonts();
7372
await tester.pumpWidget(renderWidget());
7473
await tester.tap(find.byType(TextButton));
7574
await tester.pumpAndSettle();
7675

77-
await screenMatchesGolden(tester, 'routine_logs_screen_detail', skip: !Platform.isLinux);
76+
await expectLater(find.byType(WorkoutLogsScreen),
77+
matchesGoldenFile('goldens/routine_logs_screen_detail.png'));
7878

7979
// expect(find.text('3 day workout'), findsOneWidget);
8080

test/workout/routine_screen_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
import 'dart:io';
20-
2119
import 'package:drift/native.dart';
2220
import 'package:flutter/material.dart';
2321
import 'package:flutter_test/flutter_test.dart';
24-
import 'package:golden_toolkit/golden_toolkit.dart';
2522
import 'package:mockito/annotations.dart';
2623
import 'package:provider/provider.dart';
2724
import 'package:wger/database/exercises/exercise_database.dart';
@@ -32,6 +29,7 @@ import 'package:wger/providers/routines.dart';
3229
import 'package:wger/screens/routine_screen.dart';
3330

3431
import '../../test_data/routines.dart';
32+
import '../utils.dart';
3533
import 'routine_screen_test.mocks.dart';
3634

3735
@GenerateMocks([WgerBaseProvider])
@@ -70,13 +68,15 @@ void main() {
7068
);
7169
}
7270

73-
testGoldens('Test the widgets on the routine screen', (WidgetTester tester) async {
71+
testWidgets('Test the widgets on the routine screen',
72+
(WidgetTester tester) async {
7473
await loadAppFonts();
7574
await tester.pumpWidget(renderWidget());
7675
await tester.tap(find.byType(TextButton));
7776
await tester.pumpAndSettle();
7877

79-
await screenMatchesGolden(tester, 'routine_screen_detail', skip: !Platform.isLinux);
78+
await expectLater(find.byType(RoutineScreen),
79+
matchesGoldenFile('goldens/routine_logs_screen_detail.png'));
8080

8181
expect(find.text('3 day workout'), findsOneWidget);
8282

0 commit comments

Comments
 (0)