asset_bundle_test.dart 1.96 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 8

import 'package:file/file.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/cache.dart';
12

13 14
import 'package:test/test.dart';

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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    // 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');
      }
    });

46 47
    testUsingContext('nonempty', () async {
      final AssetBundle ab = AssetBundleFactory.instance.createBundle();
48 49 50
      expect(await ab.build(), 0);
      expect(ab.entries.length, greaterThan(0));
    });
51 52 53 54 55 56

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

57
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
58 59
      await bundle.build(manifestPath: 'pubspec.yaml');
      expect(bundle.entries.length, 1);
60
      const String expectedAssetManifest = '{}';
61 62 63 64
      expect(
        UTF8.decode(await bundle.entries['AssetManifest.json'].contentsAsBytes()),
        expectedAssetManifest,
      );
65
    });
66
  });
67

68
}