asset_bundle_variant_test.dart 3 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 6
// @dart = 2.8

7 8 9
import 'dart:convert';

import 'package:file/file.dart';
10
import 'package:file/memory.dart';
11 12 13

import 'package:flutter_tools/src/asset.dart';
import 'package:flutter_tools/src/base/file_system.dart';
14

15
import 'package:flutter_tools/src/globals.dart' as globals;
16

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

20
void main() {
21 22 23 24 25 26
  String fixPath(String path) {
    // The in-memory file system is strict about slashes on Windows being the
    // correct way so until https://github.com/google/file.dart/issues/112 is
    // fixed we fix them here.
    // TODO(dantup): Remove this function once the above issue is fixed and
    // rolls into Flutter.
27
    return path?.replaceAll('/', globals.fs.path.separator);
28
  }
29

30
  group('AssetBundle asset variants', () {
31 32
    FileSystem testFileSystem;
    setUp(() async {
33
      testFileSystem = MemoryFileSystem(
34
        style: globals.platform.isWindows
35 36 37 38 39 40
          ? FileSystemStyle.windows
          : FileSystemStyle.posix,
      );
      testFileSystem.currentDirectory = testFileSystem.systemTempDirectory.createTempSync('flutter_asset_bundle_variant_test.');
    });

41
    testUsingContext('main asset and variants', () async {
42
      globals.fs.file('pubspec.yaml')
43 44 45 46 47 48 49 50 51 52 53 54
        ..createSync()
        ..writeAsStringSync(
'''
name: test
dependencies:
  flutter:
    sdk: flutter
flutter:
  assets:
    - a/b/c/foo
'''
      );
55
      globals.fs.file('.packages').createSync();
56 57 58 59 60 61 62

      final List<String> assets = <String>[
        'a/b/c/foo',
        'a/b/c/var1/foo',
        'a/b/c/var2/foo',
        'a/b/c/var3/foo',
      ];
63
      for (final String asset in assets) {
64
        globals.fs.file(fixPath(asset))
65 66 67 68
          ..createSync(recursive: true)
          ..writeAsStringSync(asset);
      }

69
      AssetBundle bundle = AssetBundleFactory.instance.createBundle();
70
      await bundle.build(manifestPath: 'pubspec.yaml', packagesPath: '.packages');
71 72

      // The main asset file, /a/b/c/foo, and its variants exist.
73
      for (final String asset in assets) {
74
        expect(bundle.entries.containsKey(asset), true);
75
        expect(utf8.decode(await bundle.entries[asset].contentsAsBytes()), asset);
76 77
      }

78
      globals.fs.file(fixPath('a/b/c/foo')).deleteSync();
79
      bundle = AssetBundleFactory.instance.createBundle();
80
      await bundle.build(manifestPath: 'pubspec.yaml', packagesPath: '.packages');
81 82 83

      // Now the main asset file, /a/b/c/foo, does not exist. This is OK because
      // the /a/b/c/*/foo variants do exist.
84
      expect(bundle.entries.containsKey('a/b/c/foo'), false);
85
      for (final String asset in assets.skip(1)) {
86
        expect(bundle.entries.containsKey(asset), true);
87
        expect(utf8.decode(await bundle.entries[asset].contentsAsBytes()), asset);
88
      }
89 90
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
91
      ProcessManager: () => FakeProcessManager.any(),
92 93 94
    });
  });
}