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

import 'package:flutter_tools/src/asset.dart';
6
import 'package:flutter_tools/src/base/file_system.dart';
7
import 'package:flutter_tools/src/cache.dart';
8
import 'package:flutter_tools/src/globals.dart' as globals;
9

10 11
import '../src/common.dart';
import '../src/context.dart';
12

13
void main() {
14
  group('Assets', () {
15
    final String dataPath = globals.fs.path.join(
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
      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 {
31
      final AssetBundle asset = AssetBundleFactory.instance.createBundle();
32 33 34 35
      final String manifestPath =
          globals.fs.path.join(dataPath, 'main', 'pubspec.yaml');
      final String packagesPath =
          globals.fs.path.join(dataPath, 'main', '.packages');
36
      await asset.build(
37 38
        manifestPath: manifestPath,
        packagesPath: packagesPath,
39 40 41 42 43 44 45
      );

      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"}]}]',
      );
46
      expect(asset.wasBuiltOnce(), true);
47 48 49 50 51 52 53 54
      expect(
        asset.inputFiles.map((File f) {
          return f.path;
        }),
        <String>[
          packagesPath,
          globals.fs.path.join(dataPath, 'font', 'pubspec.yaml'),
          manifestPath,
55
          globals.fs.path.join(dataPath, 'font', 'test_font_file'),
56 57
        ],
      );
58 59
    });

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

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