image_provider_test.dart 2.09 KB
Newer Older
1 2 3 4
// Copyright 2017 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.

5 6 7
import 'dart:async';
import 'dart:typed_data';

8
import 'package:flutter/painting.dart';
9 10
import 'package:flutter_test/flutter_test.dart';

11 12 13
import '../rendering/rendering_tester.dart';
import 'image_data.dart';

14
void main() {
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  new TestRenderingFlutterBinding(); // initializes the imageCache
  group(ImageProvider, () {
    tearDown(() {
      imageCache.clear();
    });

    test('NetworkImage non-null url test', () {
      expect(() {
        new NetworkImage(nonconst(null));
      }, throwsAssertionError);
    });

    test('ImageProvider can evict images', () async {
      final Uint8List bytes = new Uint8List.fromList(kTransparentImage);
      final MemoryImage imageProvider = new MemoryImage(bytes);
      final ImageStream stream = imageProvider.resolve(ImageConfiguration.empty);
      final Completer<void> completer = new Completer<void>();
      stream.addListener((ImageInfo info, bool syncCall) => completer.complete());
      await completer.future;

      expect(imageCache.currentSize, 1);
      expect(await new MemoryImage(bytes).evict(), true);
      expect(imageCache.currentSize, 0);
    });

    test('ImageProvider.evict respects the provided ImageCache', () async {
      final ImageCache otherCache = new ImageCache();
      final Uint8List bytes = new Uint8List.fromList(kTransparentImage);
      final MemoryImage imageProvider = new MemoryImage(bytes);
      otherCache.putIfAbsent(imageProvider, () => imageProvider.load(imageProvider));
      final ImageStream stream = imageProvider.resolve(ImageConfiguration.empty);
      final Completer<void> completer = new Completer<void>();
      stream.addListener((ImageInfo info, bool syncCall) => completer.complete());
      await completer.future;

      expect(otherCache.currentSize, 1);
      expect(imageCache.currentSize, 1);
      expect(await imageProvider.evict(cache: otherCache), true);
      expect(otherCache.currentSize, 0);
      expect(imageCache.currentSize, 1);
    });
56 57
  });
}