assets_test.dart 7.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
import 'package:collection/collection.dart' show IterableExtension;
6 7
import 'package:file/memory.dart';
import 'package:file_testing/file_testing.dart';
8
import 'package:flutter_tools/src/artifacts.dart';
9
import 'package:flutter_tools/src/base/file_system.dart';
10
import 'package:flutter_tools/src/base/logger.dart';
11
import 'package:flutter_tools/src/base/platform.dart';
12
import 'package:flutter_tools/src/build_info.dart';
13
import 'package:flutter_tools/src/build_system/build_system.dart';
14
import 'package:flutter_tools/src/build_system/depfile.dart';
15
import 'package:flutter_tools/src/build_system/targets/assets.dart';
16 17
import 'package:flutter_tools/src/convert.dart';
import 'package:flutter_tools/src/devfs.dart';
18

19
import '../../../src/common.dart';
20
import '../../../src/context.dart';
21 22

void main() {
23 24
  late Environment environment;
  late FileSystem fileSystem;
25

26
  setUp(() {
27 28 29
    fileSystem = MemoryFileSystem.test();
    environment = Environment.test(
      fileSystem.currentDirectory,
30
      processManager: FakeProcessManager.any(),
31
      artifacts: Artifacts.test(),
32 33
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
34
      platform: FakePlatform(),
35 36 37
    );
    fileSystem.file(environment.buildDir.childFile('app.dill')).createSync(recursive: true);
    fileSystem.file('packages/flutter_tools/lib/src/build_system/targets/assets.dart')
38
      .createSync(recursive: true);
39
    fileSystem.file('assets/foo/bar.png')
40
      .createSync(recursive: true);
41
    fileSystem.file('assets/wildcard/#bar.png')
42
      .createSync(recursive: true);
43
    fileSystem.file('.packages')
44
      .createSync();
45 46 47
    fileSystem.file('pubspec.yaml')
      ..createSync()
      ..writeAsStringSync('''
48 49 50 51 52
name: example

flutter:
  assets:
    - assets/foo/bar.png
53
    - assets/wildcard/
54
''');
55
  });
56

57 58 59 60 61 62 63 64 65 66 67 68 69
  testUsingContext('includes LICENSE file inputs in dependencies', () async {
    fileSystem.file('.packages')
      .writeAsStringSync('foo:file:///bar/lib');
    fileSystem.file('bar/LICENSE')
      ..createSync(recursive: true)
      ..writeAsStringSync('THIS IS A LICENSE');

    await const CopyAssets().build(environment);

    final File depfile = environment.buildDir.childFile('flutter_assets.d');

    expect(depfile, exists);

70
    final Depfile dependencies = environment.depFileService.parse(depfile);
71 72

    expect(
73
      dependencies.inputs.firstWhereOrNull((File file) => file.path == '/bar/LICENSE'),
74 75 76 77 78 79 80 81
      isNotNull,
    );
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
  });

  testUsingContext('Copies files to correct asset directory', () async {
82
    await const CopyAssets().build(environment);
83

84 85
    expect(fileSystem.file('${environment.buildDir.path}/flutter_assets/AssetManifest.json'), exists);
    expect(fileSystem.file('${environment.buildDir.path}/flutter_assets/FontManifest.json'), exists);
86
    expect(fileSystem.file('${environment.buildDir.path}/flutter_assets/NOTICES.Z'), exists);
87
    // See https://github.com/flutter/flutter/issues/35293
88
    expect(fileSystem.file('${environment.buildDir.path}/flutter_assets/assets/foo/bar.png'), exists);
89
    // See https://github.com/flutter/flutter/issues/46163
90 91 92 93 94
    expect(fileSystem.file('${environment.buildDir.path}/flutter_assets/assets/wildcard/%23bar.png'), exists);
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
  });
95 96 97 98 99 100 101 102 103 104 105 106 107

  testUsingContext('Throws exception if pubspec contains missing files', () async {
    fileSystem.file('pubspec.yaml')
      ..createSync()
      ..writeAsStringSync('''
name: example

flutter:
  assets:
    - assets/foo/bar2.png

''');

108
    expect(() async => const CopyAssets().build(environment), throwsException);
109 110 111 112
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
  });
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

  testWithoutContext('processSkSLBundle returns null if there is no path '
    'to the bundle', () {

    expect(processSkSLBundle(
      null,
      targetPlatform: TargetPlatform.android,
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
    ), isNull);
  });

  testWithoutContext('processSkSLBundle throws exception if bundle file is '
    'missing', () {

    expect(() => processSkSLBundle(
      'does_not_exist.sksl',
      targetPlatform: TargetPlatform.android,
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
133
    ), throwsException);
134 135 136 137 138 139 140 141 142 143 144 145 146 147
  });

  testWithoutContext('processSkSLBundle throws exception if the bundle is not '
    'valid JSON', () {

    final FileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();
    fileSystem.file('bundle.sksl').writeAsStringSync('{');

    expect(() => processSkSLBundle(
      'bundle.sksl',
      targetPlatform: TargetPlatform.android,
      fileSystem: fileSystem,
      logger: logger,
148
    ), throwsException);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    expect(logger.errorText, contains('was not a JSON object'));
  });

  testWithoutContext('processSkSLBundle throws exception if the bundle is not '
    'a JSON object', () {

    final FileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();
    fileSystem.file('bundle.sksl').writeAsStringSync('[]');

    expect(() => processSkSLBundle(
      'bundle.sksl',
      targetPlatform: TargetPlatform.android,
      fileSystem: fileSystem,
      logger: logger,
164
    ), throwsException);
165 166 167 168 169 170 171 172 173 174
    expect(logger.errorText, contains('was not a JSON object'));
  });

  testWithoutContext('processSkSLBundle throws an exception if the engine '
    'revision is different', () {

    final FileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();
    fileSystem.file('bundle.sksl').writeAsStringSync(json.encode(
      <String, String>{
175 176
        'engineRevision': '1',
      },
177 178 179 180 181 182 183 184
    ));

    expect(() => processSkSLBundle(
      'bundle.sksl',
      targetPlatform: TargetPlatform.android,
      fileSystem: fileSystem,
      logger: logger,
      engineVersion: '2',
185
    ), throwsException);
186 187 188 189 190 191 192 193 194 195 196
    expect(logger.errorText, contains('Expected Flutter 1, but found 2'));
  });

  testWithoutContext('processSkSLBundle warns if the bundle target platform is '
    'different from the current target', () async {

    final FileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();
    fileSystem.file('bundle.sksl').writeAsStringSync(json.encode(
      <String, Object>{
        'engineRevision': '2',
197
        'platform': 'fuchsia-arm64',
198
        'data': <String, Object>{},
199 200 201 202 203 204 205 206 207
      }
    ));

    final DevFSContent content = processSkSLBundle(
      'bundle.sksl',
      targetPlatform: TargetPlatform.android,
      fileSystem: fileSystem,
      logger: logger,
      engineVersion: '2',
208
    )!;
209 210 211 212 213 214 215 216 217 218 219 220 221

    expect(await content.contentsAsBytes(), utf8.encode('{"data":{}}'));
    expect(logger.errorText, contains('This may lead to less efficient shader caching'));
  });

  testWithoutContext('processSkSLBundle does not warn and produces bundle', () async {

    final FileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();
    fileSystem.file('bundle.sksl').writeAsStringSync(json.encode(
      <String, Object>{
        'engineRevision': '2',
        'platform': 'android',
222 223
        'data': <String, Object>{},
      },
224 225 226 227 228 229 230 231
    ));

    final DevFSContent content = processSkSLBundle(
      'bundle.sksl',
      targetPlatform: TargetPlatform.android,
      fileSystem: fileSystem,
      logger: logger,
      engineVersion: '2',
232
    )!;
233 234 235 236

    expect(await content.contentsAsBytes(), utf8.encode('{"data":{}}'));
    expect(logger.errorText, isEmpty);
  });
237
}