2017-06-22 23:45:57 +00:00
|
|
|
// 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';
|
|
|
|
|
|
|
|
import 'package:file/file.dart';
|
|
|
|
|
|
|
|
import 'package:flutter_tools/src/asset.dart';
|
|
|
|
import 'package:flutter_tools/src/base/file_system.dart';
|
|
|
|
import 'package:flutter_tools/src/cache.dart';
|
|
|
|
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
import 'src/common.dart';
|
|
|
|
import 'src/context.dart';
|
|
|
|
|
2018-02-02 22:27:29 +00:00
|
|
|
void main() {
|
2018-01-24 21:16:23 +00:00
|
|
|
// These tests do not use a memory file system because we want to ensure that
|
|
|
|
// asset bundles work correctly on Windows and Posix systems.
|
|
|
|
Directory tempDir;
|
|
|
|
Directory oldCurrentDir;
|
|
|
|
|
|
|
|
setUp(() async {
|
|
|
|
tempDir = await fs.systemTempDirectory.createTemp('asset_bundle_tests');
|
|
|
|
oldCurrentDir = fs.currentDirectory;
|
|
|
|
fs.currentDirectory = tempDir;
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() {
|
|
|
|
fs.currentDirectory = oldCurrentDir;
|
|
|
|
try {
|
|
|
|
tempDir?.deleteSync(recursive: true);
|
|
|
|
tempDir = null;
|
|
|
|
} on FileSystemException catch (e) {
|
|
|
|
// Do nothing, windows sometimes has trouble deleting.
|
|
|
|
print('Ignored exception during tearDown: $e');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-22 23:45:57 +00:00
|
|
|
group('AssetBundle asset variants', () {
|
|
|
|
testUsingContext('main asset and variants', () async {
|
|
|
|
// Setting flutterRoot here so that it picks up the MemoryFileSystem's
|
|
|
|
// path separator.
|
|
|
|
Cache.flutterRoot = getFlutterRoot();
|
|
|
|
|
2017-10-22 16:11:36 +00:00
|
|
|
fs.file('pubspec.yaml')
|
2017-06-22 23:45:57 +00:00
|
|
|
..createSync()
|
|
|
|
..writeAsStringSync(
|
|
|
|
'''
|
|
|
|
name: test
|
|
|
|
dependencies:
|
|
|
|
flutter:
|
|
|
|
sdk: flutter
|
|
|
|
flutter:
|
|
|
|
assets:
|
|
|
|
- a/b/c/foo
|
|
|
|
'''
|
|
|
|
);
|
2017-10-22 16:11:36 +00:00
|
|
|
fs.file('.packages')..createSync();
|
2017-06-22 23:45:57 +00:00
|
|
|
|
|
|
|
final List<String> assets = <String>[
|
|
|
|
'a/b/c/foo',
|
|
|
|
'a/b/c/var1/foo',
|
|
|
|
'a/b/c/var2/foo',
|
|
|
|
'a/b/c/var3/foo',
|
|
|
|
];
|
|
|
|
for (String asset in assets) {
|
|
|
|
fs.file(asset)
|
|
|
|
..createSync(recursive: true)
|
|
|
|
..writeAsStringSync(asset);
|
|
|
|
}
|
|
|
|
|
2018-01-29 17:40:28 +00:00
|
|
|
AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
2017-06-22 23:45:57 +00:00
|
|
|
await bundle.build(manifestPath: 'pubspec.yaml');
|
|
|
|
|
|
|
|
// The main asset file, /a/b/c/foo, and its variants exist.
|
|
|
|
for (String asset in assets) {
|
|
|
|
expect(bundle.entries.containsKey(asset), true);
|
2018-03-12 18:06:32 +00:00
|
|
|
expect(utf8.decode(await bundle.entries[asset].contentsAsBytes()), asset);
|
2017-06-22 23:45:57 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 21:16:23 +00:00
|
|
|
fs.file('a/b/c/foo').deleteSync();
|
2018-01-29 17:40:28 +00:00
|
|
|
bundle = AssetBundleFactory.instance.createBundle();
|
2017-06-22 23:45:57 +00:00
|
|
|
await bundle.build(manifestPath: 'pubspec.yaml');
|
|
|
|
|
|
|
|
// Now the main asset file, /a/b/c/foo, does not exist. This is OK because
|
|
|
|
// the /a/b/c/*/foo variants do exist.
|
2018-01-24 21:16:23 +00:00
|
|
|
expect(bundle.entries.containsKey('a/b/c/foo'), false);
|
2017-06-22 23:45:57 +00:00
|
|
|
for (String asset in assets.skip(1)) {
|
|
|
|
expect(bundle.entries.containsKey(asset), true);
|
2018-03-12 18:06:32 +00:00
|
|
|
expect(utf8.decode(await bundle.entries[asset].contentsAsBytes()), asset);
|
2017-06-22 23:45:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|