image_cache_resize_test.dart 3.84 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
import 'dart:ui' as ui;

9
import 'package:flutter/painting.dart';
10
import '../flutter_test_alternative.dart';
11

12
import '../rendering/rendering_tester.dart';
13 14 15
import 'mocks_for_image_cache.dart';

void main() {
16
  TestRenderingFlutterBinding();
17

18 19 20 21 22 23 24 25
  tearDown(() {
    imageCache.clear();
    imageCache.maximumSize = 1000;
    imageCache.maximumSizeBytes = 10485760;
  });

  test('Image cache resizing based on count', () async {
    imageCache.maximumSize = 2;
26

27 28 29 30
    final TestImageInfo a = await extractOneFrame(TestImageProvider(1, 1, image: await createTestImage()).resolve(ImageConfiguration.empty)) as TestImageInfo;
    final TestImageInfo b = await extractOneFrame(TestImageProvider(2, 2, image: await createTestImage()).resolve(ImageConfiguration.empty)) as TestImageInfo;
    final TestImageInfo c = await extractOneFrame(TestImageProvider(3, 3, image: await createTestImage()).resolve(ImageConfiguration.empty)) as TestImageInfo;
    final TestImageInfo d = await extractOneFrame(TestImageProvider(1, 4, image: await createTestImage()).resolve(ImageConfiguration.empty)) as TestImageInfo;
31 32 33 34
    expect(a.value, equals(1));
    expect(b.value, equals(2));
    expect(c.value, equals(3));
    expect(d.value, equals(4));
35

36
    imageCache.maximumSize = 0;
37

38
    final TestImageInfo e = await extractOneFrame(TestImageProvider(1, 5, image: await createTestImage()).resolve(ImageConfiguration.empty)) as TestImageInfo;
39
    expect(e.value, equals(5));
40

41
    final TestImageInfo f = await extractOneFrame(TestImageProvider(1, 6, image: await createTestImage()).resolve(ImageConfiguration.empty)) as TestImageInfo;
42
    expect(f.value, equals(6));
43

44
    imageCache.maximumSize = 3;
45

46
    final TestImageInfo g = await extractOneFrame(TestImageProvider(1, 7, image: await createTestImage()).resolve(ImageConfiguration.empty)) as TestImageInfo;
47
    expect(g.value, equals(7));
48

49
    final TestImageInfo h = await extractOneFrame(TestImageProvider(1, 8, image: await createTestImage()).resolve(ImageConfiguration.empty)) as TestImageInfo;
50 51
    expect(h.value, equals(7));
  });
52

53
  test('Image cache resizing based on size', () async {
54
    final ui.Image testImage = await createTestImage(width: 8, height: 8); // 256 B.
55
    imageCache.maximumSizeBytes = 256 * 2;
56

57 58 59 60
    final TestImageInfo a = await extractOneFrame(TestImageProvider(1, 1, image: testImage).resolve(ImageConfiguration.empty)) as TestImageInfo;
    final TestImageInfo b = await extractOneFrame(TestImageProvider(2, 2, image: testImage).resolve(ImageConfiguration.empty)) as TestImageInfo;
    final TestImageInfo c = await extractOneFrame(TestImageProvider(3, 3, image: testImage).resolve(ImageConfiguration.empty)) as TestImageInfo;
    final TestImageInfo d = await extractOneFrame(TestImageProvider(1, 4, image: testImage).resolve(ImageConfiguration.empty)) as TestImageInfo;
61 62 63 64
    expect(a.value, equals(1));
    expect(b.value, equals(2));
    expect(c.value, equals(3));
    expect(d.value, equals(4));
65

66
    imageCache.maximumSizeBytes = 0;
67

68
    final TestImageInfo e = await extractOneFrame(TestImageProvider(1, 5, image: testImage).resolve(ImageConfiguration.empty)) as TestImageInfo;
69
    expect(e.value, equals(5));
70

71
    final TestImageInfo f = await extractOneFrame(TestImageProvider(1, 6, image: testImage).resolve(ImageConfiguration.empty)) as TestImageInfo;
72
    expect(f.value, equals(6));
73

74
    imageCache.maximumSizeBytes = 256 * 3;
75

76
    final TestImageInfo g = await extractOneFrame(TestImageProvider(1, 7, image: testImage).resolve(ImageConfiguration.empty)) as TestImageInfo;
77
    expect(g.value, equals(7));
78

79
    final TestImageInfo h = await extractOneFrame(TestImageProvider(1, 8, image: testImage).resolve(ImageConfiguration.empty)) as TestImageInfo;
80
    expect(h.value, equals(7));
81 82
  });
}