Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions lib/src/golden_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:io';
import 'dart:ui' as ui;

import 'package:alchemist/alchemist.dart';
Expand All @@ -10,6 +11,7 @@ import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meta/meta.dart';
import 'package:yaml/yaml.dart';

/// Default golden test runner which uses the flutter test framework.
const defaultGoldenTestRunner = FlutterGoldenTestRunner();
Expand Down Expand Up @@ -37,26 +39,58 @@ Future<void> _setUpGoldenTests() async {
@visibleForTesting
Future<void> loadFonts() async {
final bundle = rootBundle;
final packageName = _getPackageName();

final fontManifestString = await bundle.loadString('FontManifest.json');
final fontManifest = (json.decode(fontManifestString) as List<dynamic>)
.map((dynamic x) => x as Map<String, dynamic>);

for (final entry in fontManifest) {
final family =
(entry['family'] as String).stripFontFamilyAlchemistPackageName();
final baseFamily = entry['family'] as String;
final family = baseFamily.stripFontFamilyAlchemistPackageName();

final fontAssets = [
for (final fontAssetEntry in entry['fonts'] as List<dynamic>)
(fontAssetEntry as Map<String, dynamic>)['asset'] as String,
];

final loader = FontLoader(family);
for (final fontAsset in fontAssets) {
loader.addFont(bundle.load(fontAsset));
await _loadFontFamily(family, fontAssets);

// Check if the font is a custom user-specified font.
// `MaterialIcons` is a special case, added by specifying
// `flutter` -> `uses-material-design: true` in `pubspec.yaml`.
final isCustomFont =
!baseFamily.startsWith('packages/') && family != 'MaterialIcons';

// Register same-package custom fonts under an alias.
if (isCustomFont && packageName != null) {
final aliasFamily = 'packages/$packageName/$baseFamily';
await _loadFontFamily(aliasFamily, fontAssets);
}
}
}

await loader.load();
/// Returns the name of the package from which the golden test is being run.
/// `null` if the package name cannot be determined.
String? _getPackageName() {
final pubspecFile = File('pubspec.yaml');
if (!pubspecFile.existsSync()) {
return null;
}

final pubspec = loadYaml(pubspecFile.readAsStringSync()) as YamlMap;
return pubspec['name'] as String?;
}

/// Loads fonts from a font family for use in golden tests.
Future<void> _loadFontFamily(String family, List<String> fontAssets) async {
final bundle = rootBundle;
final loader = FontLoader(family);
for (final fontAsset in fontAssets) {
loader.addFont(bundle.load(fontAsset));
}

await loader.load();
}

/// Performs a Flutter widget test that compares against golden image.
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies:
flutter_test:
sdk: flutter
meta: ^1.7.0
yaml: ^3.1.2

dev_dependencies:
mocktail: ^0.3.0
Expand Down
30 changes: 30 additions & 0 deletions test/smoke_tests/text_package_smoke_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:alchemist/src/golden_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('smoke test', () {
goldenTest(
'succeeds without a package name',
fileName: 'text_package_smoke_test_no_package',
builder: () => const Text(
'Hello, world!',
style: TextStyle(
fontFamily: 'Roboto',
),
),
);

goldenTest(
'succeeds with a package name',
fileName: 'text_package_smoke_test_specified',
builder: () => const Text(
'Hello, world!',
style: TextStyle(
fontFamily: 'Roboto',
package: 'alchemist',
),
),
);
});
}
Loading