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

import 'dart:async';

import 'package:flutter_tools/src/asset.dart';
import 'package:flutter_tools/src/cache.dart';
9
import 'package:flutter_tools/src/globals.dart' as globals;
10

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

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

      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"}]}]',
      );
44
      expect(asset.wasBuiltOnce(), true);
45 46
    });

47
    testUsingContext('handles empty pubspec with .packages', () async {
48
      final String dataPath = globals.fs.path.join(
49 50 51 52 53 54 55 56 57
        getFlutterRoot(),
        'packages',
        'flutter_tools',
        'test',
        'data',
        'fuchsia_test',
      );
      final AssetBundle asset = AssetBundleFactory.instance.createBundle();
      await asset.build(
58 59
        manifestPath : globals.fs.path.join(dataPath, 'main', 'pubspec.yaml'), // file doesn't exist
        packagesPath: globals.fs.path.join(dataPath, 'main', '.packages'),
60 61 62 63
        includeDefaultFonts: false,
      );
      expect(asset.wasBuiltOnce(), true);
    });
64 65 66 67
  });
}

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