asset_bundle_package_fonts_test.dart 9.59 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8 9
import 'dart:convert';

import 'package:file/file.dart';
10
import 'package:file/memory.dart';
11 12 13

import 'package:flutter_tools/src/asset.dart';
import 'package:flutter_tools/src/base/file_system.dart';
14

15
import 'package:flutter_tools/src/globals.dart' as globals;
16

17 18
import '../src/common.dart';
import '../src/context.dart';
19 20

void main() {
21 22 23 24 25 26
  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.
27
    return path?.replaceAll('/', globals.fs.path.separator);
28
  }
29
  void writePubspecFile(String path, String name, { String fontsSection }) {
30 31 32 33 34 35 36 37 38 39
    if (fontsSection == null) {
      fontsSection = '';
    } else {
      fontsSection = '''
flutter:
     fonts:
$fontsSection
''';
    }

40
    globals.fs.file(fixPath(path))
41 42 43 44 45 46 47 48 49 50 51
      ..createSync(recursive: true)
      ..writeAsStringSync('''
name: $name
dependencies:
  flutter:
    sdk: flutter
$fontsSection
''');
  }

  void writePackagesFile(String packages) {
52
    globals.fs.file('.packages')
53 54 55 56
      ..createSync()
      ..writeAsStringSync(packages);
  }

57
  Future<void> buildAndVerifyFonts(
58 59 60 61 62
    List<String> localFonts,
    List<String> packageFonts,
    List<String> packages,
    String expectedAssetManifest,
  ) async {
63
    final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
64
    await bundle.build(manifestPath: 'pubspec.yaml', packagesPath: '.packages');
65

66 67
    for (final String packageName in packages) {
      for (final String packageFont in packageFonts) {
68 69 70
        final String entryKey = 'packages/$packageName/$packageFont';
        expect(bundle.entries.containsKey(entryKey), true);
        expect(
71
          utf8.decode(await bundle.entries[entryKey].contentsAsBytes()),
72 73 74 75
          packageFont,
        );
      }

76
      for (final String localFont in localFonts) {
77 78
        expect(bundle.entries.containsKey(localFont), true);
        expect(
79
          utf8.decode(await bundle.entries[localFont].contentsAsBytes()),
80 81 82 83 84 85
          localFont,
        );
      }
    }

    expect(
86
      json.decode(utf8.decode(await bundle.entries['FontManifest.json'].contentsAsBytes())),
87
      json.decode(expectedAssetManifest),
88 89 90 91
    );
  }

  void writeFontAsset(String path, String font) {
92
    globals.fs.file(fixPath('$path$font'))
93 94 95 96
      ..createSync(recursive: true)
      ..writeAsStringSync(font);
  }

97 98
  group('AssetBundle fonts from packages', () {
    FileSystem testFileSystem;
99

100
    setUp(() async {
101
      testFileSystem = MemoryFileSystem(
102
        style: globals.platform.isWindows
103 104 105 106 107
          ? FileSystemStyle.windows
          : FileSystemStyle.posix,
      );
      testFileSystem.currentDirectory = testFileSystem.systemTempDirectory.createTempSync('flutter_asset_bundle_test.');
    });
108

109 110 111 112 113
    testUsingContext('App includes neither font manifest nor fonts when no defines fonts', () async {
      writePubspecFile('pubspec.yaml', 'test');
      writePackagesFile('test_package:p/p/lib/');
      writePubspecFile('p/p/pubspec.yaml', 'test_package');

114
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
115
      await bundle.build(manifestPath: 'pubspec.yaml', packagesPath: '.packages');
116 117
      expect(bundle.entries.length, 3); // LICENSE, AssetManifest, FontManifest
      expect(bundle.entries.containsKey('FontManifest.json'), isTrue);
118 119
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
120
      ProcessManager: () => FakeProcessManager.any(),
121
    });
122 123

    testUsingContext('App font uses font file from package', () async {
124
      const String fontsSection = '''
125 126 127 128 129 130 131 132
       - 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');

133
      const String font = 'bar';
134 135
      writeFontAsset('p/p/lib/', font);

136
      const String expectedFontManifest =
137 138 139 140 141 142 143
          '[{"fonts":[{"asset":"packages/test_package/bar"}],"family":"foo"}]';
      await buildAndVerifyFonts(
        <String>[],
        <String>[font],
        <String>['test_package'],
        expectedFontManifest,
      );
144 145
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
146
      ProcessManager: () => FakeProcessManager.any(),
147
    });
148 149

    testUsingContext('App font uses local font file and package font file', () async {
150
      const String fontsSection = '''
151 152 153 154 155 156 157 158 159
       - 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');

160
      const String packageFont = 'bar';
161
      writeFontAsset('p/p/lib/', packageFont);
162
      const String localFont = 'a/bar';
163 164
      writeFontAsset('', localFont);

165
      const String expectedFontManifest =
166 167 168 169 170 171 172 173
          '[{"fonts":[{"asset":"packages/test_package/bar"},{"asset":"a/bar"}],'
          '"family":"foo"}]';
      await buildAndVerifyFonts(
        <String>[localFont],
        <String>[packageFont],
        <String>['test_package'],
        expectedFontManifest,
      );
174 175
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
176
      ProcessManager: () => FakeProcessManager.any(),
177
    });
178 179 180 181

    testUsingContext('App uses package font with own font file', () async {
      writePubspecFile('pubspec.yaml', 'test');
      writePackagesFile('test_package:p/p/lib/');
182
      const String fontsSection = '''
183 184 185 186 187 188 189 190 191 192
       - family: foo
         fonts:
           - asset: a/bar
''';
      writePubspecFile(
        'p/p/pubspec.yaml',
        'test_package',
        fontsSection: fontsSection,
      );

193
      const String font = 'a/bar';
194 195
      writeFontAsset('p/p/', font);

196
      const String expectedFontManifest =
197 198
          '[{"family":"packages/test_package/foo",'
          '"fonts":[{"asset":"packages/test_package/a/bar"}]}]';
199 200 201 202 203 204
      await buildAndVerifyFonts(
        <String>[],
        <String>[font],
        <String>['test_package'],
        expectedFontManifest,
      );
205 206
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
207
      ProcessManager: () => FakeProcessManager.any(),
208
    });
209 210 211 212

    testUsingContext('App uses package font with font file from another package', () async {
      writePubspecFile('pubspec.yaml', 'test');
      writePackagesFile('test_package:p/p/lib/\ntest_package2:p2/p/lib/');
213
      const String fontsSection = '''
214 215 216 217 218 219 220 221 222 223 224
       - family: foo
         fonts:
           - asset: packages/test_package2/bar
''';
      writePubspecFile(
        'p/p/pubspec.yaml',
        'test_package',
        fontsSection: fontsSection,
      );
      writePubspecFile('p2/p/pubspec.yaml', 'test_package2');

225
      const String font = 'bar';
226 227
      writeFontAsset('p2/p/lib/', font);

228
      const String expectedFontManifest =
229 230
          '[{"family":"packages/test_package/foo",'
          '"fonts":[{"asset":"packages/test_package2/bar"}]}]';
231 232 233 234 235 236
      await buildAndVerifyFonts(
        <String>[],
        <String>[font],
        <String>['test_package2'],
        expectedFontManifest,
      );
237 238
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
239
      ProcessManager: () => FakeProcessManager.any(),
240
    });
241 242 243 244 245

    testUsingContext('App uses package font with properties and own font file', () async {
      writePubspecFile('pubspec.yaml', 'test');
      writePackagesFile('test_package:p/p/lib/');

246
      const String pubspec = '''
247 248 249 250 251 252 253 254 255 256 257
       - family: foo
         fonts:
           - style: italic
             weight: 400
             asset: a/bar
''';
      writePubspecFile(
        'p/p/pubspec.yaml',
        'test_package',
        fontsSection: pubspec,
      );
258
      const String font = 'a/bar';
259 260
      writeFontAsset('p/p/', font);

261
      const String expectedFontManifest =
262 263
          '[{"family":"packages/test_package/foo",'
          '"fonts":[{"weight":400,"style":"italic","asset":"packages/test_package/a/bar"}]}]';
264 265 266 267 268 269
      await buildAndVerifyFonts(
        <String>[],
        <String>[font],
        <String>['test_package'],
        expectedFontManifest,
      );
270 271
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
272
      ProcessManager: () => FakeProcessManager.any(),
273
    });
274 275

    testUsingContext('App uses local font and package font with own font file.', () async {
276
      const String fontsSection = '''
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
       - 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,
      );

293
      const String font = 'a/bar';
294 295 296
      writeFontAsset('', font);
      writeFontAsset('p/p/', font);

297
      const String expectedFontManifest =
298 299 300
          '[{"fonts":[{"asset":"a/bar"}],"family":"foo"},'
          '{"family":"packages/test_package/foo",'
          '"fonts":[{"asset":"packages/test_package/a/bar"}]}]';
301 302 303 304 305 306
      await buildAndVerifyFonts(
        <String>[font],
        <String>[font],
        <String>['test_package'],
        expectedFontManifest,
      );
307 308
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
309
      ProcessManager: () => FakeProcessManager.any(),
310
    });
311 312
  });
}