asset_bundle_package_fonts_test.dart 8.87 KB
Newer Older
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
// 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';

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

import 'package:test/test.dart';

import 'src/common.dart';
import 'src/context.dart';

void main() {
  void writePubspecFile(String path, String name, {String fontsSection}) {
    if (fontsSection == null) {
      fontsSection = '';
    } else {
      fontsSection = '''
flutter:
     fonts:
$fontsSection
''';
    }

    fs.file(path)
      ..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) {
49
    fs.file('.packages')
50 51 52 53 54 55 56 57 58 59
      ..createSync()
      ..writeAsStringSync(packages);
  }

  Future<Null> buildAndVerifyFonts(
    List<String> localFonts,
    List<String> packageFonts,
    List<String> packages,
    String expectedAssetManifest,
  ) async {
60
    final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
61 62 63 64 65 66 67
    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(
68
          utf8.decode(await bundle.entries[entryKey].contentsAsBytes()),
69 70 71 72 73 74 75
          packageFont,
        );
      }

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

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

  void writeFontAsset(String path, String font) {
    fs.file('$path$font')
      ..createSync(recursive: true)
      ..writeAsStringSync(font);
  }

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  // These tests do not use a memory file system because we want to ensure that
  // asset bundles work correctly on Windows and Posix systems.
  Directory tempDir;
  Directory oldCurrentDir;

  setUp(() async {
    tempDir = await fs.systemTempDirectory.createTemp('asset_bundle_tests');
    oldCurrentDir = fs.currentDirectory;
    fs.currentDirectory = tempDir;
  });

  tearDown(() {
    fs.currentDirectory = oldCurrentDir;
    try {
      tempDir?.deleteSync(recursive: true);
      tempDir = null;
    } on FileSystemException catch (e) {
      // Do nothing, windows sometimes has trouble deleting.
      print('Ignored exception during tearDown: $e');
    }
  });

116 117 118 119 120 121 122 123
  group('AssetBundle fonts from packages', () {
    testUsingContext('App includes neither font manifest nor fonts when no defines fonts', () async {
      establishFlutterRoot();

      writePubspecFile('pubspec.yaml', 'test');
      writePackagesFile('test_package:p/p/lib/');
      writePubspecFile('p/p/pubspec.yaml', 'test_package');

124
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
125
      await bundle.build(manifestPath: 'pubspec.yaml');
126 127
      expect(bundle.entries.length, 3); // LICENSE, AssetManifest, FontManifest
      expect(bundle.entries.containsKey('FontManifest.json'), isTrue);
128
    });
129 130 131 132

    testUsingContext('App font uses font file from package', () async {
      establishFlutterRoot();

133
      const String fontsSection = '''
134 135 136 137 138 139 140 141
       - 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');

142
      const String font = 'bar';
143 144
      writeFontAsset('p/p/lib/', font);

145
      const String expectedFontManifest =
146 147 148 149 150 151 152
          '[{"fonts":[{"asset":"packages/test_package/bar"}],"family":"foo"}]';
      await buildAndVerifyFonts(
        <String>[],
        <String>[font],
        <String>['test_package'],
        expectedFontManifest,
      );
153
    });
154 155 156 157

    testUsingContext('App font uses local font file and package font file', () async {
      establishFlutterRoot();

158
      const String fontsSection = '''
159 160 161 162 163 164 165 166 167
       - 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');

168
      const String packageFont = 'bar';
169
      writeFontAsset('p/p/lib/', packageFont);
170
      const String localFont = 'a/bar';
171 172
      writeFontAsset('', localFont);

173
      const String expectedFontManifest =
174 175 176 177 178 179 180 181
          '[{"fonts":[{"asset":"packages/test_package/bar"},{"asset":"a/bar"}],'
          '"family":"foo"}]';
      await buildAndVerifyFonts(
        <String>[localFont],
        <String>[packageFont],
        <String>['test_package'],
        expectedFontManifest,
      );
182
    });
183 184 185 186 187 188

    testUsingContext('App uses package font with own font file', () async {
      establishFlutterRoot();

      writePubspecFile('pubspec.yaml', 'test');
      writePackagesFile('test_package:p/p/lib/');
189
      const String fontsSection = '''
190 191 192 193 194 195 196 197 198 199
       - family: foo
         fonts:
           - asset: a/bar
''';
      writePubspecFile(
        'p/p/pubspec.yaml',
        'test_package',
        fontsSection: fontsSection,
      );

200
      const String font = 'a/bar';
201 202
      writeFontAsset('p/p/', font);

203
      const String expectedFontManifest =
204 205
          '[{"family":"packages/test_package/foo",'
          '"fonts":[{"asset":"packages/test_package/a/bar"}]}]';
206 207 208 209 210 211
      await buildAndVerifyFonts(
        <String>[],
        <String>[font],
        <String>['test_package'],
        expectedFontManifest,
      );
212
    });
213 214 215 216 217 218

    testUsingContext('App uses package font with font file from another package', () async {
      establishFlutterRoot();

      writePubspecFile('pubspec.yaml', 'test');
      writePackagesFile('test_package:p/p/lib/\ntest_package2:p2/p/lib/');
219
      const String fontsSection = '''
220 221 222 223 224 225 226 227 228 229 230
       - family: foo
         fonts:
           - asset: packages/test_package2/bar
''';
      writePubspecFile(
        'p/p/pubspec.yaml',
        'test_package',
        fontsSection: fontsSection,
      );
      writePubspecFile('p2/p/pubspec.yaml', 'test_package2');

231
      const String font = 'bar';
232 233
      writeFontAsset('p2/p/lib/', font);

234
      const String expectedFontManifest =
235 236
          '[{"family":"packages/test_package/foo",'
          '"fonts":[{"asset":"packages/test_package2/bar"}]}]';
237 238 239 240 241 242
      await buildAndVerifyFonts(
        <String>[],
        <String>[font],
        <String>['test_package2'],
        expectedFontManifest,
      );
243
    });
244 245 246 247 248 249 250

    testUsingContext('App uses package font with properties and own font file', () async {
      establishFlutterRoot();

      writePubspecFile('pubspec.yaml', 'test');
      writePackagesFile('test_package:p/p/lib/');

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

266
      const String expectedFontManifest =
267 268
          '[{"family":"packages/test_package/foo",'
          '"fonts":[{"weight":400,"style":"italic","asset":"packages/test_package/a/bar"}]}]';
269 270 271 272 273 274
      await buildAndVerifyFonts(
        <String>[],
        <String>[font],
        <String>['test_package'],
        expectedFontManifest,
      );
275
    });
276 277 278 279

    testUsingContext('App uses local font and package font with own font file.', () async {
      establishFlutterRoot();

280
      const String fontsSection = '''
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
       - 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,
      );

297
      const String font = 'a/bar';
298 299 300
      writeFontAsset('', font);
      writeFontAsset('p/p/', font);

301
      const String expectedFontManifest =
302 303 304
          '[{"fonts":[{"asset":"a/bar"}],"family":"foo"},'
          '{"family":"packages/test_package/foo",'
          '"fonts":[{"asset":"packages/test_package/a/bar"}]}]';
305 306 307 308 309 310
      await buildAndVerifyFonts(
        <String>[font],
        <String>[font],
        <String>['test_package'],
        expectedFontManifest,
      );
311
    });
312 313
  });
}