asset_bundle_test.dart 28.1 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 'dart:convert';
6

7
import 'package:file/memory.dart';
8
import 'package:flutter_tools/src/artifacts.dart';
9
import 'package:flutter_tools/src/asset.dart';
10
import 'package:flutter_tools/src/base/file_system.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/bundle_builder.dart';
14
import 'package:flutter_tools/src/devfs.dart';
15
import 'package:flutter_tools/src/globals.dart' as globals;
16

17 18
import '../src/common.dart';
import '../src/context.dart';
19

20
const String shaderLibDir = '/./shader_lib';
21

22
void main() {
23
  group('AssetBundle.build', () {
24
    late FileSystem testFileSystem;
25 26

    setUp(() async {
27
      testFileSystem = MemoryFileSystem(
28
        style: globals.platform.isWindows
29 30 31 32
          ? FileSystemStyle.windows
          : FileSystemStyle.posix,
      );
      testFileSystem.currentDirectory = testFileSystem.systemTempDirectory.createTempSync('flutter_asset_bundle_test.');
33 34
    });

35 36
    testUsingContext('nonempty', () async {
      final AssetBundle ab = AssetBundleFactory.instance.createBundle();
37
      expect(await ab.build(packagesPath: '.packages'), 0);
38
      expect(ab.entries.length, greaterThan(0));
39 40
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
41
      ProcessManager: () => FakeProcessManager.any(),
42
    });
43 44

    testUsingContext('empty pubspec', () async {
45
      globals.fs.file('pubspec.yaml')
46 47 48
        ..createSync()
        ..writeAsStringSync('');

49
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
50
      await bundle.build(packagesPath: '.packages');
51 52
      expect(bundle.entries.length, 1);
      const String expectedAssetManifest = '{}';
53
      expect(
54
        utf8.decode(await bundle.entries['AssetManifest.json']!.contentsAsBytes()),
55
        expectedAssetManifest,
56
      );
57 58
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
59
      ProcessManager: () => FakeProcessManager.any(),
60
    });
61 62

    testUsingContext('wildcard directories are updated when filesystem changes', () async {
63 64 65
      final File packageFile = globals.fs.file('.packages')..createSync();
      globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
      globals.fs.file('pubspec.yaml')
66 67 68 69 70 71 72 73
        ..createSync()
        ..writeAsStringSync(r'''
name: example
flutter:
  assets:
    - assets/foo/
''');
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
74
      await bundle.build(packagesPath: '.packages');
75 76 77 78 79 80
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      expect(bundle.entries.length, 4);
81
      expect(bundle.needsBuild(), false);
82

83
      // Simulate modifying the files by updating the filestat time manually.
84
      globals.fs.file(globals.fs.path.join('assets', 'foo', 'fizz.txt'))
85 86
        ..createSync(recursive: true)
        ..setLastModifiedSync(packageFile.lastModifiedSync().add(const Duration(hours: 1)));
87

88 89
      expect(bundle.needsBuild(), true);
      await bundle.build(packagesPath: '.packages');
90 91 92 93 94 95 96
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      //  - assets/foo/fizz.txt
      expect(bundle.entries.length, 5);
97 98
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
99
      ProcessManager: () => FakeProcessManager.any(),
100
    });
101 102

    testUsingContext('handle removal of wildcard directories', () async {
103 104
      globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
      final File pubspec = globals.fs.file('pubspec.yaml')
105 106 107 108 109 110 111
        ..createSync()
        ..writeAsStringSync(r'''
name: example
flutter:
  assets:
    - assets/foo/
''');
112
      globals.fs.file('.packages').createSync();
113
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
114
      await bundle.build(packagesPath: '.packages');
115 116 117 118 119 120
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      expect(bundle.entries.length, 4);
121
      expect(bundle.needsBuild(), false);
122 123

      // Delete the wildcard directory and update pubspec file.
124
      final DateTime modifiedTime = pubspec.lastModifiedSync().add(const Duration(hours: 1));
125 126
      globals.fs.directory(globals.fs.path.join('assets', 'foo')).deleteSync(recursive: true);
      globals.fs.file('pubspec.yaml')
127 128
        ..createSync()
        ..writeAsStringSync(r'''
129 130
name: example''')
        ..setLastModifiedSync(modifiedTime);
131

132
      // touch .packages to make sure its change time is after pubspec.yaml's
133
      globals.fs.file('.packages')
134
        .setLastModifiedSync(modifiedTime);
135

136 137 138
      // Even though the previous file was removed, it is left in the
      // asset manifest and not updated. This is due to the devfs not
      // supporting file deletion.
139 140
      expect(bundle.needsBuild(), true);
      await bundle.build(packagesPath: '.packages');
141 142 143 144 145 146
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      expect(bundle.entries.length, 4);
147 148
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
149
      ProcessManager: () => FakeProcessManager.any(),
150
    });
151 152 153

    // https://github.com/flutter/flutter/issues/42723
    testUsingContext('Test regression for mistyped file', () async {
154
      globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
155 156
      // Create a directory in the same path to test that we're only looking at File
      // objects.
157 158
      globals.fs.directory(globals.fs.path.join('assets', 'foo', 'bar')).createSync();
      globals.fs.file('pubspec.yaml')
159 160 161 162 163 164 165
        ..createSync()
        ..writeAsStringSync(r'''
name: example
flutter:
  assets:
    - assets/foo/
''');
166
      globals.fs.file('.packages').createSync();
167
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
168
      await bundle.build(packagesPath: '.packages');
169 170 171 172 173 174
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      expect(bundle.entries.length, 4);
175
      expect(bundle.needsBuild(), false);
176 177
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
178
      ProcessManager: () => FakeProcessManager.any(),
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    });

    testUsingContext('deferred assets are parsed', () async {
      globals.fs.file('.packages').createSync();
      globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
      globals.fs.file(globals.fs.path.join('assets', 'bar', 'barbie.txt')).createSync(recursive: true);
      globals.fs.file(globals.fs.path.join('assets', 'wild', 'dash.txt')).createSync(recursive: true);
      globals.fs.file('pubspec.yaml')
        ..createSync()
        ..writeAsStringSync(r'''
name: example
flutter:
  assets:
    - assets/foo/
  deferred-components:
    - name: component1
      assets:
        - assets/bar/barbie.txt
        - assets/wild/
''');
      final AssetBundle bundle = AssetBundleFactory.defaultInstance(
        logger: globals.logger,
        fileSystem: globals.fs,
        platform: globals.platform,
        splitDeferredAssets: true,
      ).createBundle();
205
      await bundle.build(packagesPath: '.packages', deferredComponentsEnabled: true);
206 207 208 209 210 211
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      expect(bundle.entries.length, 4);
212
      expect(bundle.deferredComponentsEntries.length, 1);
213 214
      expect(bundle.deferredComponentsEntries['component1']!.length, 2);
      expect(bundle.needsBuild(), false);
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
      ProcessManager: () => FakeProcessManager.any(),
    });

    testUsingContext('deferred assets are parsed regularly when splitDeferredAssets Disabled', () async {
      globals.fs.file('.packages').createSync();
      globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
      globals.fs.file(globals.fs.path.join('assets', 'bar', 'barbie.txt')).createSync(recursive: true);
      globals.fs.file(globals.fs.path.join('assets', 'wild', 'dash.txt')).createSync(recursive: true);
      globals.fs.file('pubspec.yaml')
        ..createSync()
        ..writeAsStringSync(r'''
name: example
flutter:
  assets:
    - assets/foo/
  deferred-components:
    - name: component1
      assets:
        - assets/bar/barbie.txt
        - assets/wild/
''');
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
239
      await bundle.build(packagesPath: '.packages');
240 241 242 243 244 245
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      expect(bundle.entries.length, 6);
246
      expect(bundle.deferredComponentsEntries.isEmpty, true);
247
      expect(bundle.needsBuild(), false);
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
      ProcessManager: () => FakeProcessManager.any(),
    });

    testUsingContext('deferred assets wildcard parsed', () async {
      final File packageFile = globals.fs.file('.packages')..createSync();
      globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
      globals.fs.file(globals.fs.path.join('assets', 'bar', 'barbie.txt')).createSync(recursive: true);
      globals.fs.file(globals.fs.path.join('assets', 'wild', 'dash.txt')).createSync(recursive: true);
      globals.fs.file('pubspec.yaml')
        ..createSync()
        ..writeAsStringSync(r'''
name: example
flutter:
  assets:
    - assets/foo/
  deferred-components:
    - name: component1
      assets:
        - assets/bar/barbie.txt
        - assets/wild/
''');
      final AssetBundle bundle = AssetBundleFactory.defaultInstance(
        logger: globals.logger,
        fileSystem: globals.fs,
        platform: globals.platform,
        splitDeferredAssets: true,
      ).createBundle();
277
      await bundle.build(packagesPath: '.packages', deferredComponentsEnabled: true);
278 279 280 281 282 283 284 285
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      expect(bundle.entries.length, 4);
      expect(bundle.deferredComponentsEntries.length, 1);
      expect(bundle.deferredComponentsEntries['component1']!.length, 2);
286
      expect(bundle.needsBuild(), false);
287 288 289 290 291 292

      // Simulate modifying the files by updating the filestat time manually.
      globals.fs.file(globals.fs.path.join('assets', 'wild', 'fizz.txt'))
        ..createSync(recursive: true)
        ..setLastModifiedSync(packageFile.lastModifiedSync().add(const Duration(hours: 1)));

293 294
      expect(bundle.needsBuild(), true);
      await bundle.build(packagesPath: '.packages', deferredComponentsEnabled: true);
295

296
      expect(bundle.entries.length, 4);
297
      expect(bundle.deferredComponentsEntries.length, 1);
298
      expect(bundle.deferredComponentsEntries['component1']!.length, 3);
299 300 301
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
      ProcessManager: () => FakeProcessManager.any(),
302
    });
303
  });
304

305
  testUsingContext('Failed directory delete shows message', () async {
306 307
    final FileExceptionHandler handler = FileExceptionHandler();
    final FileSystem fileSystem = MemoryFileSystem.test(opHandle: handler.opHandle);
308

309 310 311
    final Directory directory = fileSystem.directory('foo')
      ..createSync();
    handler.addError(directory, FileSystemOp.delete, const FileSystemException('Expected Error Text'));
312

313 314 315 316 317
    await writeBundle(
      directory,
      <String, DevFSContent>{},
      <String, AssetKind>{},
      loggerOverride: testLogger,
318
      targetPlatform: TargetPlatform.android,
319
    );
320

321
    expect(testLogger.warningText, contains('Expected Error Text'));
322
  });
323 324 325 326 327 328 329 330 331 332 333 334 335

  testUsingContext('does not unnecessarily recreate asset manifest, font manifest, license', () async {
    globals.fs.file('.packages').createSync();
    globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
    globals.fs.file('pubspec.yaml')
      ..createSync()
      ..writeAsStringSync(r'''
name: example
flutter:
assets:
  - assets/foo/bar.txt
''');
    final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
336
    await bundle.build(packagesPath: '.packages');
337

338
    final DevFSStringContent? assetManifest = bundle.entries['AssetManifest.json']
339 340 341 342 343
      as DevFSStringContent?;
    final DevFSStringContent? fontManifest = bundle.entries['FontManifest.json']
      as DevFSStringContent?;
    final DevFSStringContent? license = bundle.entries['NOTICES']
      as DevFSStringContent?;
344

345
    await bundle.build(packagesPath: '.packages');
346

347
    expect(assetManifest, bundle.entries['AssetManifest.json']);
348
    expect(fontManifest, bundle.entries['FontManifest.json']);
349
    expect(license, bundle.entries['NOTICES']);
350 351 352 353
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem.test(),
    ProcessManager: () => FakeProcessManager.any(),
  });
354

355 356 357 358 359 360 361 362 363 364 365 366 367 368
  testUsingContext('inserts dummy file into additionalDependencies when '
    'wildcards are used', () async {
    globals.fs.file('.packages').createSync();
    globals.fs.file(globals.fs.path.join('assets', 'bar.txt')).createSync(recursive: true);
    globals.fs.file('pubspec.yaml')
      ..createSync()
      ..writeAsStringSync(r'''
name: example
flutter:
  assets:
    - assets/
''');
    final AssetBundle bundle = AssetBundleFactory.instance.createBundle();

369
    expect(await bundle.build(packagesPath: '.packages'), 0);
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    expect(bundle.additionalDependencies.single.path, contains('DOES_NOT_EXIST_RERUN_FOR_WILDCARD'));
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem.test(),
    ProcessManager: () => FakeProcessManager.any(),
  });

  testUsingContext('Does not insert dummy file into additionalDependencies '
    'when wildcards are not used', () async {
    globals.fs.file('.packages').createSync();
    globals.fs.file(globals.fs.path.join('assets', 'bar.txt')).createSync(recursive: true);
    globals.fs.file('pubspec.yaml')
      ..createSync()
      ..writeAsStringSync(r'''
name: example
flutter:
  assets:
    - assets/bar.txt
''');
    final AssetBundle bundle = AssetBundleFactory.instance.createBundle();

390
    expect(await bundle.build(packagesPath: '.packages'), 0);
391 392 393 394 395 396
    expect(bundle.additionalDependencies, isEmpty);
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem.test(),
    ProcessManager: () => FakeProcessManager.any(),
  });

397 398

  group('Shaders: ', () {
399 400 401 402
    late MemoryFileSystem fileSystem;
    late Artifacts artifacts;
    late String impellerc;
    late Directory output;
403
    late String assetsPath;
404 405
    late String shaderPath;
    late String outputPath;
406 407 408 409 410 411 412 413 414

    setUp(() {
      artifacts = Artifacts.test();
      fileSystem = MemoryFileSystem.test();
      impellerc = artifacts.getHostArtifact(HostArtifact.impellerc).path;

      fileSystem.file(impellerc).createSync(recursive: true);

      output = fileSystem.directory('asset_output')..createSync(recursive: true);
415 416 417
      assetsPath = 'assets';
      shaderPath = fileSystem.path.join(assetsPath, 'shader.frag');
      outputPath = fileSystem.path.join(output.path, assetsPath, 'shader.frag');
418 419 420 421 422 423 424 425 426 427
      fileSystem.file(shaderPath).createSync(recursive: true);
    });

    testUsingContext('Including a shader triggers the shader compiler', () async {
      fileSystem.file('.packages').createSync();
      fileSystem.file('pubspec.yaml')
        ..createSync()
        ..writeAsStringSync(r'''
  name: example
  flutter:
428
    shaders:
429 430 431 432
      - assets/shader.frag
  ''');
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();

433
      expect(await bundle.build(packagesPath: '.packages'), 0);
434

435 436 437 438 439
      await writeBundle(
        output,
        bundle.entries,
        bundle.entryKinds,
        loggerOverride: testLogger,
440
        targetPlatform: TargetPlatform.android,
441
      );
442 443 444 445 446 447 448 449

    }, overrides: <Type, Generator>{
      Artifacts: () => artifacts,
      FileSystem: () => fileSystem,
      ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
        FakeCommand(
          command: <String>[
            impellerc,
450 451 452 453
            '--sksl',
            '--iplr',
            '--sl=$outputPath',
            '--spirv=$outputPath.spirv',
454
            '--input=/$shaderPath',
455
            '--input-type=frag',
456
            '--remap-samplers',
457
            '--include=/$assetsPath',
458
            '--include=$shaderLibDir',
459 460 461
          ],
          onRun: () {
            fileSystem.file(outputPath).createSync(recursive: true);
462
            fileSystem.file('$outputPath.spirv').createSync(recursive: true);
463 464 465 466
          },
        ),
      ]),
    });
467

468
    testUsingContext('Included shaders are compiled for the web', () async {
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
      fileSystem.file('.packages').createSync();
      fileSystem.file('pubspec.yaml')
        ..createSync()
        ..writeAsStringSync(r'''
  name: example
  flutter:
    shaders:
      - assets/shader.frag
  ''');
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();

      expect(await bundle.build(packagesPath: '.packages', targetPlatform: TargetPlatform.web_javascript), 0);

      await writeBundle(
        output,
        bundle.entries,
        bundle.entryKinds,
        loggerOverride: testLogger,
487
        targetPlatform: TargetPlatform.web_javascript,
488 489 490 491 492 493
      );

    }, overrides: <Type, Generator>{
      Artifacts: () => artifacts,
      FileSystem: () => fileSystem,
      ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
494 495 496 497 498 499 500 501 502 503 504
        FakeCommand(
          command: <String>[
            impellerc,
            '--sksl',
            '--iplr',
            '--json',
            '--sl=$outputPath',
            '--spirv=$outputPath.spirv',
            '--input=/$shaderPath',
            '--input-type=frag',
            '--include=/$assetsPath',
505
            '--include=$shaderLibDir',
506 507 508 509 510 511
          ],
          onRun: () {
            fileSystem.file(outputPath).createSync(recursive: true);
            fileSystem.file('$outputPath.spirv').createSync(recursive: true);
          },
        ),
512 513 514
      ]),
    });

515
    testUsingContext('Material shaders are compiled for the web', () async {
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
      fileSystem.file('.packages').createSync();

      final String materialIconsPath = fileSystem.path.join(
        getFlutterRoot(),
        'bin', 'cache', 'artifacts', 'material_fonts',
        'MaterialIcons-Regular.otf',
      );
      fileSystem.file(materialIconsPath).createSync(recursive: true);

      final String materialPath = fileSystem.path.join(
        getFlutterRoot(),
        'packages', 'flutter', 'lib', 'src', 'material',
      );
      final Directory materialDir = fileSystem.directory(materialPath)..createSync(recursive: true);
      for (final String shader in kMaterialShaders) {
        materialDir.childFile(shader).createSync(recursive: true);
      }

534 535 536 537 538 539 540 541 542 543 544
      (globals.processManager as FakeProcessManager)
        .addCommand(FakeCommand(
          command: <String>[
            impellerc,
            '--sksl',
            '--iplr',
            '--json',
            '--sl=${fileSystem.path.join(output.path, 'shaders', 'ink_sparkle.frag')}',
            '--spirv=${fileSystem.path.join(output.path, 'shaders', 'ink_sparkle.frag.spirv')}',
            '--input=${fileSystem.path.join(materialDir.path, 'shaders', 'ink_sparkle.frag')}',
            '--input-type=frag',
545
            '--remap-samplers',
546
            '--include=${fileSystem.path.join(materialDir.path, 'shaders')}',
547
            '--include=$shaderLibDir',
548 549 550 551 552 553 554
          ],
          onRun: () {
            fileSystem.file(outputPath).createSync(recursive: true);
            fileSystem.file('$outputPath.spirv').createSync(recursive: true);
          },
        ));

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
      fileSystem.file('pubspec.yaml')
        ..createSync()
        ..writeAsStringSync(r'''
  name: example
  flutter:
    uses-material-design: true
  ''');
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();

      expect(await bundle.build(packagesPath: '.packages', targetPlatform: TargetPlatform.web_javascript), 0);

      await writeBundle(
        output,
        bundle.entries,
        bundle.entryKinds,
        loggerOverride: testLogger,
571
        targetPlatform: TargetPlatform.web_javascript,
572 573 574 575 576
      );

    }, overrides: <Type, Generator>{
      Artifacts: () => artifacts,
      FileSystem: () => fileSystem,
577
      ProcessManager: () => FakeProcessManager.list(<FakeCommand>[]),
578
    });
579 580
  });

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
  testUsingContext('Does not insert dummy file into additionalDependencies '
    'when wildcards are used by dependencies', () async {
    globals.fs.file('.packages').writeAsStringSync(r'''
example:lib/
foo:foo/lib/
''');
    globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt'))
      .createSync(recursive: true);
    globals.fs.file('pubspec.yaml')
      ..createSync()
      ..writeAsStringSync(r'''
name: example
dependencies:
  foo: any
''');
    globals.fs.file('foo/pubspec.yaml')
      ..createSync(recursive: true)
      ..writeAsStringSync(r'''
name: foo

flutter:
  assets:
    - bar/
''');
    final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
    globals.fs.file('foo/bar/fizz.txt').createSync(recursive: true);

608
    expect(await bundle.build(packagesPath: '.packages'), 0);
609 610 611 612
    expect(bundle.additionalDependencies, isEmpty);
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem.test(),
    ProcessManager: () => FakeProcessManager.any(),
613
    Platform: () => FakePlatform(),
614 615
  });

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
  testUsingContext('does not track wildcard directories from dependencies', () async {
    globals.fs.file('.packages').writeAsStringSync(r'''
example:lib/
foo:foo/lib/
''');
    globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt'))
      .createSync(recursive: true);
    globals.fs.file('pubspec.yaml')
      ..createSync()
      ..writeAsStringSync(r'''
name: example
dependencies:
  foo: any
''');
    globals.fs.file('foo/pubspec.yaml')
      ..createSync(recursive: true)
      ..writeAsStringSync(r'''
name: foo

flutter:
  assets:
    - bar/
''');
    final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
    globals.fs.file('foo/bar/fizz.txt').createSync(recursive: true);

642
    await bundle.build(packagesPath: '.packages');
643

644
    expect(bundle.entries, hasLength(4));
645
    expect(bundle.needsBuild(), false);
646 647 648 649 650

    // Does not track dependency's wildcard directories.
    globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt'))
      .deleteSync();

651
    expect(bundle.needsBuild(), false);
652 653 654
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem.test(),
    ProcessManager: () => FakeProcessManager.any(),
655
    Platform: () => FakePlatform(),
656
  });
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683

  testUsingContext('reports package that causes asset bundle error when it is '
    'a dependency', () async {
    globals.fs.file('.packages').writeAsStringSync(r'''
example:lib/
foo:foo/lib/
''');
    globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt'))
      .createSync(recursive: true);
    globals.fs.file('pubspec.yaml')
      ..createSync()
      ..writeAsStringSync(r'''
name: example
dependencies:
  foo: any
''');
    globals.fs.file('foo/pubspec.yaml')
      ..createSync(recursive: true)
      ..writeAsStringSync(r'''
name: foo

flutter:
  assets:
    - bar.txt
''');
    final AssetBundle bundle = AssetBundleFactory.instance.createBundle();

684
    expect(await bundle.build(packagesPath: '.packages'), 1);
685 686 687 688
    expect(testLogger.errorText, contains('This asset was included from package foo'));
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem.test(),
    ProcessManager: () => FakeProcessManager.any(),
689
    Platform: () => FakePlatform(),
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
  });

  testUsingContext('does not report package that causes asset bundle error '
    'when it is from own pubspec', () async {
    globals.fs.file('.packages').writeAsStringSync(r'''
example:lib/
''');
    globals.fs.file('pubspec.yaml')
      ..createSync()
      ..writeAsStringSync(r'''
name: example
flutter:
  assets:
    - bar.txt
''');
    final AssetBundle bundle = AssetBundleFactory.instance.createBundle();

707
    expect(await bundle.build(packagesPath: '.packages'), 1);
708 709 710 711
    expect(testLogger.errorText, isNot(contains('This asset was included from')));
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem.test(),
    ProcessManager: () => FakeProcessManager.any(),
712
    Platform: () => FakePlatform(),
713
  });
714

715
  testUsingContext('does not include Material Design assets if uses-material-design: true is '
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
    'specified only by a dependency', () async {
    globals.fs.file('.packages').writeAsStringSync(r'''
example:lib/
foo:foo/lib/
''');
    globals.fs.file('pubspec.yaml')
      ..createSync()
      ..writeAsStringSync(r'''
name: example
dependencies:
  foo: any

flutter:
  uses-material-design: false
''');
    globals.fs.file('foo/pubspec.yaml')
      ..createSync(recursive: true)
      ..writeAsStringSync(r'''
name: foo

flutter:
  uses-material-design: true
''');
    final AssetBundle bundle = AssetBundleFactory.instance.createBundle();

741 742 743
    expect(await bundle.build(packagesPath: '.packages'), 0);
    expect((bundle.entries['FontManifest.json']! as DevFSStringContent).string, '[]');
    expect((bundle.entries['AssetManifest.json']! as DevFSStringContent).string, '{}');
744 745 746 747 748 749
    expect(testLogger.errorText, contains(
      'package:foo has `uses-material-design: true` set'
    ));
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem.test(),
    ProcessManager: () => FakeProcessManager.any(),
750
    Platform: () => FakePlatform(),
751
  });
752 753 754 755 756 757 758 759 760 761 762 763

  testUsingContext('does not include assets in project directories as asset variants', () async {
    globals.fs.file('.packages').writeAsStringSync(r'''
example:lib/
''');
    globals.fs.file('pubspec.yaml')
      ..createSync()
      ..writeAsStringSync(r'''
name: example

flutter:
  assets:
764
    - assets/foo.txt
765 766 767
''');
    globals.fs.file('assets/foo.txt').createSync(recursive: true);

768
    // Potential build artifacts outside of build directory.
769 770 771 772 773 774 775 776 777
    globals.fs.file('linux/flutter/foo.txt').createSync(recursive: true);
    globals.fs.file('windows/flutter/foo.txt').createSync(recursive: true);
    globals.fs.file('windows/CMakeLists.txt').createSync();
    globals.fs.file('macos/Flutter/foo.txt').createSync(recursive: true);
    globals.fs.file('ios/foo.txt').createSync(recursive: true);
    globals.fs.file('build/foo.txt').createSync(recursive: true);

    final AssetBundle bundle = AssetBundleFactory.instance.createBundle();

778
    expect(await bundle.build(packagesPath: '.packages'), 0);
779
    expect(bundle.entries.length, 4);
780 781 782
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem.test(),
    ProcessManager: () => FakeProcessManager.any(),
783
    Platform: () => FakePlatform(),
784
  });
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811

  testUsingContext('deferred and regular assets are included in manifest alphabetically', () async {
    globals.fs.file('.packages').writeAsStringSync(r'''
example:lib/
''');
    globals.fs.file('pubspec.yaml')
      ..createSync()
      ..writeAsStringSync(r'''
name: example

flutter:
  assets:
    - assets/zebra.jpg
    - assets/foo.jpg

  deferred-components:
    - name: component1
      assets:
        - assets/bar.jpg
        - assets/apple.jpg
''');
    globals.fs.file('assets/foo.jpg').createSync(recursive: true);
    globals.fs.file('assets/bar.jpg').createSync();
    globals.fs.file('assets/apple.jpg').createSync();
    globals.fs.file('assets/zebra.jpg').createSync();
    final AssetBundle bundle = AssetBundleFactory.instance.createBundle();

812 813
    expect(await bundle.build(packagesPath: '.packages'), 0);
    expect((bundle.entries['FontManifest.json']! as DevFSStringContent).string, '[]');
814 815
    // The assets from deferred components and regular assets
    // are both included in alphabetical order
816
    expect((bundle.entries['AssetManifest.json']! as DevFSStringContent).string, '{"assets/apple.jpg":["assets/apple.jpg"],"assets/bar.jpg":["assets/bar.jpg"],"assets/foo.jpg":["assets/foo.jpg"],"assets/zebra.jpg":["assets/zebra.jpg"]}');
817 818 819
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem.test(),
    ProcessManager: () => FakeProcessManager.any(),
820
    Platform: () => FakePlatform(),
821
  });
822
}