asset_bundle_test.dart 4.77 KB
Newer Older
1 2 3 4 5
// Copyright 2016 The Chromium Authors. All rights reserved.
// 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/file.dart';
8
import 'package:file/memory.dart';
9

10
import 'package:flutter_tools/src/asset.dart';
11
import 'package:flutter_tools/src/base/file_system.dart';
12
import 'package:flutter_tools/src/base/platform.dart';
13
import 'package:flutter_tools/src/cache.dart';
14

15
import 'src/common.dart';
16
import 'src/context.dart';
17

18
void main() {
19 20 21 22
  setUpAll(() {
    Cache.flutterRoot = getFlutterRoot();
  });

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

    setUp(() async {
27
      testFileSystem = MemoryFileSystem(
28 29 30 31 32
        style: platform.isWindows
          ? 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 38
      expect(await ab.build(), 0);
      expect(ab.entries.length, greaterThan(0));
39 40
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
41
    });
42 43 44 45 46 47

    testUsingContext('empty pubspec', () async {
      fs.file('pubspec.yaml')
        ..createSync()
        ..writeAsStringSync('');

48
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
49 50
      await bundle.build(manifestPath: 'pubspec.yaml');
      expect(bundle.entries.length, 1);
51
      const String expectedAssetManifest = '{}';
52
      expect(
53
        utf8.decode(await bundle.entries['AssetManifest.json'].contentsAsBytes()),
54 55
        expectedAssetManifest,
      );
56 57
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
58
    });
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

    testUsingContext('wildcard directories are updated when filesystem changes', () async {
      fs.file('.packages').createSync();
      fs.file(fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
      fs.file('pubspec.yaml')
        ..createSync()
        ..writeAsStringSync(r'''
name: example
flutter:
  assets:
    - assets/foo/
''');
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
      await bundle.build(manifestPath: 'pubspec.yaml');
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      expect(bundle.entries.length, 4);
      expect(bundle.needsBuild(manifestPath: 'pubspec.yaml'), false);

      // Adding a file should update the stat of the directory, but instead
      // we need to fully recreate it.
      fs.directory(fs.path.join('assets', 'foo')).deleteSync(recursive: true);
      fs.file(fs.path.join('assets', 'foo', 'fizz.txt')).createSync(recursive: true);
      fs.file(fs.path.join('assets', 'foo', 'bar.txt')).createSync();

      expect(bundle.needsBuild(manifestPath: 'pubspec.yaml'), true);
      await bundle.build(manifestPath: 'pubspec.yaml');
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      //  - assets/foo/fizz.txt
      expect(bundle.entries.length, 5);
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
    });
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

    testUsingContext('handle removal of wildcard directories', () async {
      fs.file('.packages').createSync();
      fs.file(fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
      fs.file('pubspec.yaml')
        ..createSync()
        ..writeAsStringSync(r'''
name: example
flutter:
  assets:
    - assets/foo/
''');
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
      await bundle.build(manifestPath: 'pubspec.yaml');
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      expect(bundle.entries.length, 4);
      expect(bundle.needsBuild(manifestPath: 'pubspec.yaml'), false);

      // Delete the wildcard directory and update pubspec file.
      fs.directory(fs.path.join('assets', 'foo')).deleteSync(recursive: true);
      fs.file('pubspec.yaml')
        ..createSync()
        ..writeAsStringSync(r'''
name: example''');

      // 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.
      expect(bundle.needsBuild(manifestPath: 'pubspec.yaml'), true);
      await bundle.build(manifestPath: 'pubspec.yaml');
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      expect(bundle.entries.length, 4);
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
    });
142
  });
143

144
}