2017-09-19 12:04:51 +00:00
|
|
|
// Copyright 2017 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:file/file.dart';
|
2018-09-04 16:12:24 +00:00
|
|
|
import 'package:file/memory.dart';
|
2017-09-19 12:04:51 +00:00
|
|
|
|
|
|
|
import 'package:flutter_tools/src/asset.dart';
|
|
|
|
import 'package:flutter_tools/src/base/file_system.dart';
|
2018-09-04 16:12:24 +00:00
|
|
|
import 'package:flutter_tools/src/base/platform.dart';
|
2017-09-19 12:04:51 +00:00
|
|
|
import 'package:flutter_tools/src/cache.dart';
|
|
|
|
|
|
|
|
import 'src/common.dart';
|
|
|
|
import 'src/context.dart';
|
2018-09-04 16:12:24 +00:00
|
|
|
import 'src/pubspec_schema.dart';
|
2017-09-19 12:04:51 +00:00
|
|
|
|
|
|
|
void main() {
|
2018-09-04 16:12:24 +00:00
|
|
|
String fixPath(String path) {
|
|
|
|
// The in-memory file system is strict about slashes on Windows being the
|
|
|
|
// correct way so until https://github.com/google/file.dart/issues/112 is
|
|
|
|
// fixed we fix them here.
|
|
|
|
// TODO(dantup): Remove this function once the above issue is fixed and
|
|
|
|
// rolls into Flutter.
|
|
|
|
return path?.replaceAll('/', fs.path.separator);
|
|
|
|
}
|
2017-09-19 12:04:51 +00:00
|
|
|
void writePubspecFile(String path, String name, {String fontsSection}) {
|
|
|
|
if (fontsSection == null) {
|
|
|
|
fontsSection = '';
|
|
|
|
} else {
|
|
|
|
fontsSection = '''
|
|
|
|
flutter:
|
|
|
|
fonts:
|
|
|
|
$fontsSection
|
|
|
|
''';
|
|
|
|
}
|
|
|
|
|
2018-09-04 16:12:24 +00:00
|
|
|
fs.file(fixPath(path))
|
2017-09-19 12:04:51 +00:00
|
|
|
..createSync(recursive: true)
|
|
|
|
..writeAsStringSync('''
|
|
|
|
name: $name
|
|
|
|
dependencies:
|
|
|
|
flutter:
|
|
|
|
sdk: flutter
|
|
|
|
$fontsSection
|
|
|
|
''');
|
|
|
|
}
|
|
|
|
|
|
|
|
void establishFlutterRoot() {
|
|
|
|
// Setting flutterRoot here so that it picks up the MemoryFileSystem's
|
|
|
|
// path separator.
|
|
|
|
Cache.flutterRoot = getFlutterRoot();
|
|
|
|
}
|
|
|
|
|
|
|
|
void writePackagesFile(String packages) {
|
2017-10-22 16:11:36 +00:00
|
|
|
fs.file('.packages')
|
2017-09-19 12:04:51 +00:00
|
|
|
..createSync()
|
|
|
|
..writeAsStringSync(packages);
|
|
|
|
}
|
|
|
|
|
2018-10-05 05:54:56 +00:00
|
|
|
Future<void> buildAndVerifyFonts(
|
2017-09-19 12:04:51 +00:00
|
|
|
List<String> localFonts,
|
|
|
|
List<String> packageFonts,
|
|
|
|
List<String> packages,
|
|
|
|
String expectedAssetManifest,
|
|
|
|
) async {
|
2018-01-29 17:40:28 +00:00
|
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
2017-09-19 12:04:51 +00:00
|
|
|
await bundle.build(manifestPath: 'pubspec.yaml');
|
|
|
|
|
|
|
|
for (String packageName in packages) {
|
|
|
|
for (String packageFont in packageFonts) {
|
|
|
|
final String entryKey = 'packages/$packageName/$packageFont';
|
|
|
|
expect(bundle.entries.containsKey(entryKey), true);
|
|
|
|
expect(
|
2018-03-12 18:06:32 +00:00
|
|
|
utf8.decode(await bundle.entries[entryKey].contentsAsBytes()),
|
2017-09-19 12:04:51 +00:00
|
|
|
packageFont,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String localFont in localFonts) {
|
|
|
|
expect(bundle.entries.containsKey(localFont), true);
|
|
|
|
expect(
|
2018-03-12 18:06:32 +00:00
|
|
|
utf8.decode(await bundle.entries[localFont].contentsAsBytes()),
|
2017-09-19 12:04:51 +00:00
|
|
|
localFont,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(
|
2018-10-01 21:14:48 +00:00
|
|
|
json.decode(utf8.decode(await bundle.entries['FontManifest.json'].contentsAsBytes())),
|
|
|
|
json.decode(expectedAssetManifest),
|
2017-09-19 12:04:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeFontAsset(String path, String font) {
|
2018-09-04 16:12:24 +00:00
|
|
|
fs.file(fixPath('$path$font'))
|
2017-09-19 12:04:51 +00:00
|
|
|
..createSync(recursive: true)
|
|
|
|
..writeAsStringSync(font);
|
|
|
|
}
|
|
|
|
|
2018-09-04 16:12:24 +00:00
|
|
|
group('AssetBundle fonts from packages', () {
|
|
|
|
FileSystem testFileSystem;
|
2018-01-24 21:16:23 +00:00
|
|
|
|
2018-09-04 16:12:24 +00:00
|
|
|
setUp(() async {
|
2018-09-12 06:29:29 +00:00
|
|
|
testFileSystem = MemoryFileSystem(
|
2018-09-04 16:12:24 +00:00
|
|
|
style: platform.isWindows
|
|
|
|
? FileSystemStyle.windows
|
|
|
|
: FileSystemStyle.posix,
|
|
|
|
);
|
|
|
|
testFileSystem.currentDirectory = testFileSystem.systemTempDirectory.createTempSync('flutter_asset_bundle_test.');
|
|
|
|
});
|
2018-01-24 21:16:23 +00:00
|
|
|
|
2017-09-19 12:04:51 +00:00
|
|
|
testUsingContext('App includes neither font manifest nor fonts when no defines fonts', () async {
|
|
|
|
establishFlutterRoot();
|
2018-09-04 16:12:24 +00:00
|
|
|
writeEmptySchemaFile(fs);
|
2017-09-19 12:04:51 +00:00
|
|
|
|
|
|
|
writePubspecFile('pubspec.yaml', 'test');
|
|
|
|
writePackagesFile('test_package:p/p/lib/');
|
|
|
|
writePubspecFile('p/p/pubspec.yaml', 'test_package');
|
|
|
|
|
2018-01-29 17:40:28 +00:00
|
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
2017-09-19 12:04:51 +00:00
|
|
|
await bundle.build(manifestPath: 'pubspec.yaml');
|
2018-04-17 04:48:21 +00:00
|
|
|
expect(bundle.entries.length, 3); // LICENSE, AssetManifest, FontManifest
|
|
|
|
expect(bundle.entries.containsKey('FontManifest.json'), isTrue);
|
2018-09-04 16:12:24 +00:00
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
FileSystem: () => testFileSystem,
|
2018-01-24 21:16:23 +00:00
|
|
|
});
|
2017-09-19 12:04:51 +00:00
|
|
|
|
|
|
|
testUsingContext('App font uses font file from package', () async {
|
|
|
|
establishFlutterRoot();
|
2018-09-04 16:12:24 +00:00
|
|
|
writeEmptySchemaFile(fs);
|
2017-09-19 12:04:51 +00:00
|
|
|
|
2018-02-01 06:51:26 +00:00
|
|
|
const String fontsSection = '''
|
2017-09-19 12:04:51 +00:00
|
|
|
- family: foo
|
|
|
|
fonts:
|
|
|
|
- asset: packages/test_package/bar
|
|
|
|
''';
|
|
|
|
writePubspecFile('pubspec.yaml', 'test', fontsSection: fontsSection);
|
|
|
|
writePackagesFile('test_package:p/p/lib/');
|
|
|
|
writePubspecFile('p/p/pubspec.yaml', 'test_package');
|
|
|
|
|
2018-02-01 06:51:26 +00:00
|
|
|
const String font = 'bar';
|
2017-09-19 12:04:51 +00:00
|
|
|
writeFontAsset('p/p/lib/', font);
|
|
|
|
|
2018-02-01 06:51:26 +00:00
|
|
|
const String expectedFontManifest =
|
2017-09-19 12:04:51 +00:00
|
|
|
'[{"fonts":[{"asset":"packages/test_package/bar"}],"family":"foo"}]';
|
|
|
|
await buildAndVerifyFonts(
|
|
|
|
<String>[],
|
|
|
|
<String>[font],
|
|
|
|
<String>['test_package'],
|
|
|
|
expectedFontManifest,
|
|
|
|
);
|
2018-09-04 16:12:24 +00:00
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
FileSystem: () => testFileSystem,
|
2018-01-24 21:16:23 +00:00
|
|
|
});
|
2017-09-19 12:04:51 +00:00
|
|
|
|
|
|
|
testUsingContext('App font uses local font file and package font file', () async {
|
|
|
|
establishFlutterRoot();
|
2018-09-04 16:12:24 +00:00
|
|
|
writeEmptySchemaFile(fs);
|
2017-09-19 12:04:51 +00:00
|
|
|
|
2018-02-01 06:51:26 +00:00
|
|
|
const String fontsSection = '''
|
2017-09-19 12:04:51 +00:00
|
|
|
- family: foo
|
|
|
|
fonts:
|
|
|
|
- asset: packages/test_package/bar
|
|
|
|
- asset: a/bar
|
|
|
|
''';
|
|
|
|
writePubspecFile('pubspec.yaml', 'test', fontsSection: fontsSection);
|
|
|
|
writePackagesFile('test_package:p/p/lib/');
|
|
|
|
writePubspecFile('p/p/pubspec.yaml', 'test_package');
|
|
|
|
|
2018-02-01 06:51:26 +00:00
|
|
|
const String packageFont = 'bar';
|
2017-09-19 12:04:51 +00:00
|
|
|
writeFontAsset('p/p/lib/', packageFont);
|
2018-02-01 06:51:26 +00:00
|
|
|
const String localFont = 'a/bar';
|
2017-09-19 12:04:51 +00:00
|
|
|
writeFontAsset('', localFont);
|
|
|
|
|
2018-03-14 05:24:49 +00:00
|
|
|
const String expectedFontManifest =
|
2017-09-19 12:04:51 +00:00
|
|
|
'[{"fonts":[{"asset":"packages/test_package/bar"},{"asset":"a/bar"}],'
|
|
|
|
'"family":"foo"}]';
|
|
|
|
await buildAndVerifyFonts(
|
|
|
|
<String>[localFont],
|
|
|
|
<String>[packageFont],
|
|
|
|
<String>['test_package'],
|
|
|
|
expectedFontManifest,
|
|
|
|
);
|
2018-09-04 16:12:24 +00:00
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
FileSystem: () => testFileSystem,
|
2018-01-24 21:16:23 +00:00
|
|
|
});
|
2017-09-19 12:04:51 +00:00
|
|
|
|
|
|
|
testUsingContext('App uses package font with own font file', () async {
|
|
|
|
establishFlutterRoot();
|
2018-09-04 16:12:24 +00:00
|
|
|
writeEmptySchemaFile(fs);
|
2017-09-19 12:04:51 +00:00
|
|
|
|
|
|
|
writePubspecFile('pubspec.yaml', 'test');
|
|
|
|
writePackagesFile('test_package:p/p/lib/');
|
2018-02-01 06:51:26 +00:00
|
|
|
const String fontsSection = '''
|
2017-09-19 12:04:51 +00:00
|
|
|
- family: foo
|
|
|
|
fonts:
|
|
|
|
- asset: a/bar
|
|
|
|
''';
|
|
|
|
writePubspecFile(
|
|
|
|
'p/p/pubspec.yaml',
|
|
|
|
'test_package',
|
|
|
|
fontsSection: fontsSection,
|
|
|
|
);
|
|
|
|
|
2018-02-01 06:51:26 +00:00
|
|
|
const String font = 'a/bar';
|
2017-09-19 12:04:51 +00:00
|
|
|
writeFontAsset('p/p/', font);
|
|
|
|
|
2018-03-14 05:24:49 +00:00
|
|
|
const String expectedFontManifest =
|
2017-09-26 12:48:52 +00:00
|
|
|
'[{"family":"packages/test_package/foo",'
|
|
|
|
'"fonts":[{"asset":"packages/test_package/a/bar"}]}]';
|
2017-09-19 12:04:51 +00:00
|
|
|
await buildAndVerifyFonts(
|
|
|
|
<String>[],
|
|
|
|
<String>[font],
|
|
|
|
<String>['test_package'],
|
|
|
|
expectedFontManifest,
|
|
|
|
);
|
2018-09-04 16:12:24 +00:00
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
FileSystem: () => testFileSystem,
|
2018-01-24 21:16:23 +00:00
|
|
|
});
|
2017-09-19 12:04:51 +00:00
|
|
|
|
|
|
|
testUsingContext('App uses package font with font file from another package', () async {
|
|
|
|
establishFlutterRoot();
|
2018-09-04 16:12:24 +00:00
|
|
|
writeEmptySchemaFile(fs);
|
2017-09-19 12:04:51 +00:00
|
|
|
|
|
|
|
writePubspecFile('pubspec.yaml', 'test');
|
|
|
|
writePackagesFile('test_package:p/p/lib/\ntest_package2:p2/p/lib/');
|
2018-02-01 06:51:26 +00:00
|
|
|
const String fontsSection = '''
|
2017-09-19 12:04:51 +00:00
|
|
|
- family: foo
|
|
|
|
fonts:
|
|
|
|
- asset: packages/test_package2/bar
|
|
|
|
''';
|
|
|
|
writePubspecFile(
|
|
|
|
'p/p/pubspec.yaml',
|
|
|
|
'test_package',
|
|
|
|
fontsSection: fontsSection,
|
|
|
|
);
|
|
|
|
writePubspecFile('p2/p/pubspec.yaml', 'test_package2');
|
|
|
|
|
2018-02-01 06:51:26 +00:00
|
|
|
const String font = 'bar';
|
2017-09-19 12:04:51 +00:00
|
|
|
writeFontAsset('p2/p/lib/', font);
|
|
|
|
|
2018-03-14 05:24:49 +00:00
|
|
|
const String expectedFontManifest =
|
2017-09-26 12:48:52 +00:00
|
|
|
'[{"family":"packages/test_package/foo",'
|
|
|
|
'"fonts":[{"asset":"packages/test_package2/bar"}]}]';
|
2017-09-19 12:04:51 +00:00
|
|
|
await buildAndVerifyFonts(
|
|
|
|
<String>[],
|
|
|
|
<String>[font],
|
|
|
|
<String>['test_package2'],
|
|
|
|
expectedFontManifest,
|
|
|
|
);
|
2018-09-04 16:12:24 +00:00
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
FileSystem: () => testFileSystem,
|
2018-01-24 21:16:23 +00:00
|
|
|
});
|
2017-09-19 12:04:51 +00:00
|
|
|
|
|
|
|
testUsingContext('App uses package font with properties and own font file', () async {
|
|
|
|
establishFlutterRoot();
|
2018-09-04 16:12:24 +00:00
|
|
|
writeEmptySchemaFile(fs);
|
2017-09-19 12:04:51 +00:00
|
|
|
|
|
|
|
writePubspecFile('pubspec.yaml', 'test');
|
|
|
|
writePackagesFile('test_package:p/p/lib/');
|
|
|
|
|
2018-02-01 06:51:26 +00:00
|
|
|
const String pubspec = '''
|
2017-09-19 12:04:51 +00:00
|
|
|
- family: foo
|
|
|
|
fonts:
|
|
|
|
- style: italic
|
|
|
|
weight: 400
|
|
|
|
asset: a/bar
|
|
|
|
''';
|
|
|
|
writePubspecFile(
|
|
|
|
'p/p/pubspec.yaml',
|
|
|
|
'test_package',
|
|
|
|
fontsSection: pubspec,
|
|
|
|
);
|
2018-02-01 06:51:26 +00:00
|
|
|
const String font = 'a/bar';
|
2017-09-19 12:04:51 +00:00
|
|
|
writeFontAsset('p/p/', font);
|
|
|
|
|
2018-03-14 05:24:49 +00:00
|
|
|
const String expectedFontManifest =
|
2017-09-26 12:48:52 +00:00
|
|
|
'[{"family":"packages/test_package/foo",'
|
|
|
|
'"fonts":[{"weight":400,"style":"italic","asset":"packages/test_package/a/bar"}]}]';
|
2017-09-19 12:04:51 +00:00
|
|
|
await buildAndVerifyFonts(
|
|
|
|
<String>[],
|
|
|
|
<String>[font],
|
|
|
|
<String>['test_package'],
|
|
|
|
expectedFontManifest,
|
|
|
|
);
|
2018-09-04 16:12:24 +00:00
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
FileSystem: () => testFileSystem,
|
2018-01-24 21:16:23 +00:00
|
|
|
});
|
2017-09-19 12:04:51 +00:00
|
|
|
|
|
|
|
testUsingContext('App uses local font and package font with own font file.', () async {
|
|
|
|
establishFlutterRoot();
|
2018-09-04 16:12:24 +00:00
|
|
|
writeEmptySchemaFile(fs);
|
2017-09-19 12:04:51 +00:00
|
|
|
|
2018-02-01 06:51:26 +00:00
|
|
|
const String fontsSection = '''
|
2017-09-19 12:04:51 +00:00
|
|
|
- family: foo
|
|
|
|
fonts:
|
|
|
|
- asset: a/bar
|
|
|
|
''';
|
|
|
|
writePubspecFile(
|
|
|
|
'pubspec.yaml',
|
|
|
|
'test',
|
|
|
|
fontsSection: fontsSection,
|
|
|
|
);
|
|
|
|
writePackagesFile('test_package:p/p/lib/');
|
|
|
|
writePubspecFile(
|
|
|
|
'p/p/pubspec.yaml',
|
|
|
|
'test_package',
|
|
|
|
fontsSection: fontsSection,
|
|
|
|
);
|
|
|
|
|
2018-02-01 06:51:26 +00:00
|
|
|
const String font = 'a/bar';
|
2017-09-19 12:04:51 +00:00
|
|
|
writeFontAsset('', font);
|
|
|
|
writeFontAsset('p/p/', font);
|
|
|
|
|
2018-03-14 05:24:49 +00:00
|
|
|
const String expectedFontManifest =
|
2017-09-26 12:48:52 +00:00
|
|
|
'[{"fonts":[{"asset":"a/bar"}],"family":"foo"},'
|
|
|
|
'{"family":"packages/test_package/foo",'
|
|
|
|
'"fonts":[{"asset":"packages/test_package/a/bar"}]}]';
|
2017-09-19 12:04:51 +00:00
|
|
|
await buildAndVerifyFonts(
|
|
|
|
<String>[font],
|
|
|
|
<String>[font],
|
|
|
|
<String>['test_package'],
|
|
|
|
expectedFontManifest,
|
|
|
|
);
|
2018-09-04 16:12:24 +00:00
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
FileSystem: () => testFileSystem,
|
2018-01-24 21:16:23 +00:00
|
|
|
});
|
2017-09-19 12:04:51 +00:00
|
|
|
});
|
|
|
|
}
|