-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathflutter_test_config.dart
More file actions
109 lines (98 loc) · 4.16 KB
/
Copy pathflutter_test_config.dart
File metadata and controls
109 lines (98 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import 'dart:async';
import 'dart:io';
import 'package:alchemist/alchemist.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:material_ui/material_ui.dart';
import 'src/golden_network_image.dart';
Future<void> testExecutable(FutureOr<void> Function() testMain) async {
TestWidgetsFlutterBinding.ensureInitialized();
// Flutter tests default to the Ahem font unless real fonts are loaded. This loads
// Material/Cupertino fonts and every family listed in the merged FontManifest
// (including transitive packages such as stream_core_flutter's Stream Icons).
await loadFonts();
// Register host system fonts that loadFonts() can't see because they live
// outside the asset manifest — currently the platform emoji font (so emoji
// glyphs render instead of tofu) and macOS's San Francisco (so Material's
// iOS typography aliases resolve to authentic SF). On hosts where a file is
// missing the loader is a no-op; CI's obscured-text variant replaces every
// glyph with Ahem anyway.
await _loadHostSystemFonts();
// Pre-render the avatar fixture palette so the synchronous networkImage
// component builder (`goldenNetworkImageBuilder`) can return cached
// providers without a FutureBuilder flicker in goldens.
await GoldenAvatarFixtures.precompute();
return AlchemistConfig.runWithConfig(
config: AlchemistConfig(
goldenTestTheme: GoldenTestTheme(
backgroundColor: Colors.transparent,
borderColor: Colors.transparent,
nameTextStyle: const TextStyle(fontSize: 18),
),
// Docs snapshots only ever commit the platform variant (see .gitignore:
// `!**/goldens/macos/*`). The CI/obscured variant is never inspected
// and never compared against, so don't waste compute on it — generate
// platform goldens locally and in CI alike.
ciGoldensConfig: const CiGoldensConfig(enabled: false),
platformGoldensConfig: const PlatformGoldensConfig(enabled: true),
),
run: testMain,
);
}
/// Registers host system fonts that aren't part of any asset manifest.
///
/// Loads the platform color emoji font (so [DefaultStreamEmoji] and inline
/// emoji glyphs can render via the `fontFamilyFallback` chain) and, on macOS
/// only, San Francisco under the family aliases Material's iOS typography
/// expects (`CupertinoSystemDisplay` / `CupertinoSystemText`). Hosts where a
/// font file is missing simply skip that loader.
Future<void> _loadHostSystemFonts() async {
await _loadEmojiFont();
await _loadAppleSystemFont();
}
/// Loads the platform's color emoji font into the Flutter test renderer.
Future<void> _loadEmojiFont() async {
// Each entry: (FontLoader family name, candidate file paths).
final candidates = [
(
'Apple Color Emoji',
['/System/Library/Fonts/Apple Color Emoji.ttc'],
),
(
'Noto Color Emoji',
[
'/usr/share/fonts/truetype/noto/NotoColorEmoji.ttf',
'/usr/share/fonts/noto/NotoColorEmoji.ttf',
],
),
];
for (final (family, paths) in candidates) {
for (final path in paths) {
final file = File(path);
if (!file.existsSync()) continue;
final loader = FontLoader(family)..addFont(file.readAsBytes().then(ByteData.sublistView));
await loader.load();
return; // Stop after the first font successfully loaded.
}
}
}
/// Registers macOS's San Francisco font under the family aliases Material's
/// iOS typography expects: `CupertinoSystemDisplay` (headlines/titles) and
/// `CupertinoSystemText` (body).
Future<void> _loadAppleSystemFont() async {
if (!Platform.isMacOS) return;
const candidates = [
'/System/Library/Fonts/SFNS.ttf', // Catalina and later
'/System/Library/Fonts/SFNSDisplay.ttf', // pre-Catalina
];
for (final path in candidates) {
final file = File(path);
if (!file.existsSync()) continue;
final bytes = await file.readAsBytes();
for (final family in const ['CupertinoSystemDisplay', 'CupertinoSystemText']) {
final loader = FontLoader(family)..addFont(Future.value(ByteData.sublistView(bytes)));
await loader.load();
}
return; // Stop after the first font successfully loaded.
}
}