asset_bundle_test.dart 2.7 KB
Newer Older
1 2 3 4 5 6
// 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:flutter_tools/src/asset.dart';
7
import 'package:flutter_tools/src/base/file_system.dart';
8
import 'package:flutter_tools/src/devfs.dart';
9 10 11 12
import 'package:test/test.dart';

void main()  {
  // Create a temporary directory and write a single file into it.
13 14
  FileSystem fs = new LocalFileSystem();
  Directory tempDir = fs.systemTempDirectory.createTempSync();
15 16 17
  String projectRoot = tempDir.path;
  String assetPath = 'banana.txt';
  String assetContents = 'banana';
18
  File tempFile = fs.file(fs.path.join(projectRoot, assetPath));
19 20 21 22 23 24 25 26 27 28 29 30 31
  tempFile.parent.createSync(recursive: true);
  tempFile.writeAsBytesSync(UTF8.encode(assetContents));

  // Fixed asset bundle tests.
  group('AssetBundle.fixed', () {
    test('empty strings', () async {
      expect(new AssetBundle.fixed(null, null), isNotNull);
      expect(new AssetBundle.fixed('', ''), isNotNull);
      expect(new AssetBundle.fixed(null, null).entries, isEmpty);
    });
    test('does not need a rebuild', () async {
      expect(new AssetBundle.fixed(null, null).needsBuild(), isFalse);
    });
32 33 34 35
    test('empty string', () async {
      AssetBundle ab = new AssetBundle.fixed('', '');
      expect(ab.entries, isEmpty);
    });
36 37 38 39
    test('single entry', () async {
      AssetBundle ab = new AssetBundle.fixed('', 'apple.txt');
      expect(ab.entries, isNotEmpty);
      expect(ab.entries.length, 1);
40 41 42
      String archivePath = ab.entries.keys.first;
      expect(archivePath, isNotNull);
      expect(archivePath, 'apple.txt');
43 44 45 46 47
    });
    test('two entries', () async {
      AssetBundle ab = new AssetBundle.fixed('', 'apple.txt,packages/flutter_gallery_assets/shrine/products/heels.png');
      expect(ab.entries, isNotEmpty);
      expect(ab.entries.length, 2);
48 49 50
      List<String> archivePaths = ab.entries.keys.toList()..sort();
      expect(archivePaths[0], 'apple.txt');
      expect(archivePaths[1], 'packages/flutter_gallery_assets/shrine/products/heels.png');
51 52 53 54 55
    });
    test('file contents', () async {
      AssetBundle ab = new AssetBundle.fixed(projectRoot, assetPath);
      expect(ab.entries, isNotEmpty);
      expect(ab.entries.length, 1);
56 57 58 59
      String archivePath = ab.entries.keys.first;
      DevFSContent content = ab.entries[archivePath];
      expect(archivePath, assetPath);
      expect(assetContents, UTF8.decode(await content.contentsAsBytes()));
60 61
    });
  });
62 63 64 65 66 67 68 69

  group('AssetBundle.build', () {
    test('nonempty', () async {
      AssetBundle ab = new AssetBundle();
      expect(await ab.build(), 0);
      expect(ab.entries.length, greaterThan(0));
    });
  });
70
}