asset_test.dart 4.82 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
import 'package:file/memory.dart';
6
import 'package:flutter_tools/src/asset.dart';
7
import 'package:flutter_tools/src/base/file_system.dart';
8
import 'package:flutter_tools/src/build_info.dart';
9
import 'package:flutter_tools/src/cache.dart';
10
import 'package:flutter_tools/src/globals.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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

const String packageConfig = '''
{
  "configVersion": 2,
  "packages":[
    {
      "name": "my_package",
      "rootUri": "file:///",
      "packageUri": "lib/",
      "languageVersion": "2.17"
    }
  ]
}
''';

const String pubspecDotYaml = '''
name: my_package
''';

  testUsingContext('Bundles material shaders on non-web platforms', () async {
    final String shaderPath = globals.fs.path.join(
      Cache.flutterRoot!,
      'packages', 'flutter', 'lib', 'src', 'material', 'shaders', 'ink_sparkle.frag'
    );
    globals.fs.file(shaderPath).createSync(recursive: true);
    globals.fs.file('.dart_tool/package_config.json')
      ..createSync(recursive: true)
      ..writeAsStringSync(packageConfig);
    globals.fs.file('pubspec.yaml').writeAsStringSync(pubspecDotYaml);
    final AssetBundle asset = AssetBundleFactory.instance.createBundle();

    await asset.build(packagesPath: '.packages', targetPlatform: TargetPlatform.android_arm);

    expect(asset.entries.keys, contains('shaders/ink_sparkle.frag'));
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem.test(),
    ProcessManager: () => FakeProcessManager.empty(),
  });

125
  testUsingContext('Does bundle material shaders on web platforms', () async {
126 127 128 129 130 131 132 133 134 135 136 137 138
    final String shaderPath = globals.fs.path.join(
      Cache.flutterRoot!,
      'packages', 'flutter', 'lib', 'src', 'material', 'shaders', 'ink_sparkle.frag'
    );
    globals.fs.file(shaderPath).createSync(recursive: true);
    globals.fs.file('.dart_tool/package_config.json')
      ..createSync(recursive: true)
      ..writeAsStringSync(packageConfig);
    globals.fs.file('pubspec.yaml').writeAsStringSync(pubspecDotYaml);
    final AssetBundle asset = AssetBundleFactory.instance.createBundle();

    await asset.build(packagesPath: '.packages', targetPlatform: TargetPlatform.web_javascript);

139
    expect(asset.entries.keys, contains('shaders/ink_sparkle.frag'));
140 141 142 143
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem.test(),
    ProcessManager: () => FakeProcessManager.empty(),
  });
144 145 146
}

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