asset_test.dart 2.75 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
import 'package:flutter_tools/src/asset.dart';
8
import 'package:flutter_tools/src/base/file_system.dart';
9
import 'package:flutter_tools/src/cache.dart';
10
import 'package:flutter_tools/src/globals_null_migrated.dart' as globals;
11

12 13
import '../src/common.dart';
import '../src/context.dart';
14

15
void main() {
16
  group('Assets', () {
17
    final String dataPath = globals.fs.path.join(
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
      getFlutterRoot(),
      'packages',
      'flutter_tools',
      'test',
      'data',
      'asset_test',
    );

    setUpAll(() {
      Cache.disableLocking();
    });

    // This test intentionally does not use a memory file system to ensure
    // that AssetBundle with fonts also works on Windows.
    testUsingContext('app font uses local font file', () async {
33
      final AssetBundle asset = AssetBundleFactory.instance.createBundle();
34 35 36 37
      final String manifestPath =
          globals.fs.path.join(dataPath, 'main', 'pubspec.yaml');
      final String packagesPath =
          globals.fs.path.join(dataPath, 'main', '.packages');
38
      await asset.build(
39 40
        manifestPath: manifestPath,
        packagesPath: packagesPath,
41 42 43 44 45 46 47
      );

      expect(asset.entries.containsKey('FontManifest.json'), isTrue);
      expect(
        await getValueAsString('FontManifest.json', asset),
        '[{"family":"packages/font/test_font","fonts":[{"asset":"packages/font/test_font_file"}]}]',
      );
48
      expect(asset.wasBuiltOnce(), true);
49 50 51 52 53 54 55 56
      expect(
        asset.inputFiles.map((File f) {
          return f.path;
        }),
        <String>[
          packagesPath,
          globals.fs.path.join(dataPath, 'font', 'pubspec.yaml'),
          manifestPath,
57
          globals.fs.path.join(dataPath, 'font', 'test_font_file'),
58 59
        ],
      );
60 61
    });

62
    testUsingContext('handles empty pubspec with .packages', () async {
63
      final String dataPath = globals.fs.path.join(
64 65 66 67 68 69 70 71 72
        getFlutterRoot(),
        'packages',
        'flutter_tools',
        'test',
        'data',
        'fuchsia_test',
      );
      final AssetBundle asset = AssetBundleFactory.instance.createBundle();
      await asset.build(
73 74
        manifestPath: globals.fs.path
            .join(dataPath, 'main', 'pubspec.yaml'), // file doesn't exist
75
        packagesPath: globals.fs.path.join(dataPath, 'main', '.packages'),
76 77
      );
      expect(asset.wasBuiltOnce(), true);
78 79 80 81 82 83
      expect(
        asset.inputFiles.map((File f) {
          return f.path;
        }),
        <String>[],
      );
84
    });
85 86 87 88
  });
}

Future<String> getValueAsString(String key, AssetBundle asset) async {
89
  return String.fromCharCodes(await asset.entries[key].contentsAsBytes());
90
}