image_cache_test.dart 20.3 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
import 'package:flutter/painting.dart';
6
import '../flutter_test_alternative.dart';
7

8
import '../rendering/rendering_tester.dart';
9 10 11
import 'mocks_for_image_cache.dart';

void main() {
Dan Field's avatar
Dan Field committed
12
  group('ImageCache', () {
13 14 15 16
    setUpAll(() {
      TestRenderingFlutterBinding(); // initializes the imageCache
    });

17 18
    tearDown(() {
      imageCache.clear();
Dan Field's avatar
Dan Field committed
19
      imageCache.clearLiveImages();
20 21 22
      imageCache.maximumSize = 1000;
      imageCache.maximumSizeBytes = 10485760;
    });
23

24 25
    test('maintains cache size', () async {
      imageCache.maximumSize = 3;
26

27
      final TestImageInfo a = await extractOneFrame(const TestImageProvider(1, 1).resolve(ImageConfiguration.empty)) as TestImageInfo;
28
      expect(a.value, equals(1));
29
      final TestImageInfo b = await extractOneFrame(const TestImageProvider(1, 2).resolve(ImageConfiguration.empty)) as TestImageInfo;
30
      expect(b.value, equals(1));
31
      final TestImageInfo c = await extractOneFrame(const TestImageProvider(1, 3).resolve(ImageConfiguration.empty)) as TestImageInfo;
32
      expect(c.value, equals(1));
33
      final TestImageInfo d = await extractOneFrame(const TestImageProvider(1, 4).resolve(ImageConfiguration.empty)) as TestImageInfo;
34
      expect(d.value, equals(1));
35
      final TestImageInfo e = await extractOneFrame(const TestImageProvider(1, 5).resolve(ImageConfiguration.empty)) as TestImageInfo;
36
      expect(e.value, equals(1));
37
      final TestImageInfo f = await extractOneFrame(const TestImageProvider(1, 6).resolve(ImageConfiguration.empty)) as TestImageInfo;
38
      expect(f.value, equals(1));
39

40
      expect(f, equals(a));
41

42
      // cache still only has one entry in it: 1(1)
43

44
      final TestImageInfo g = await extractOneFrame(const TestImageProvider(2, 7).resolve(ImageConfiguration.empty)) as TestImageInfo;
45
      expect(g.value, equals(7));
46

47
      // cache has two entries in it: 1(1), 2(7)
48

49
      final TestImageInfo h = await extractOneFrame(const TestImageProvider(1, 8).resolve(ImageConfiguration.empty)) as TestImageInfo;
50
      expect(h.value, equals(1));
51

52
      // cache still has two entries in it: 2(7), 1(1)
53

54
      final TestImageInfo i = await extractOneFrame(const TestImageProvider(3, 9).resolve(ImageConfiguration.empty)) as TestImageInfo;
55
      expect(i.value, equals(9));
56

57
      // cache has three entries in it: 2(7), 1(1), 3(9)
58

59
      final TestImageInfo j = await extractOneFrame(const TestImageProvider(1, 10).resolve(ImageConfiguration.empty)) as TestImageInfo;
60
      expect(j.value, equals(1));
61

62
      // cache still has three entries in it: 2(7), 3(9), 1(1)
63

64
      final TestImageInfo k = await extractOneFrame(const TestImageProvider(4, 11).resolve(ImageConfiguration.empty)) as TestImageInfo;
65
      expect(k.value, equals(11));
66

67
      // cache has three entries: 3(9), 1(1), 4(11)
68

69
      final TestImageInfo l = await extractOneFrame(const TestImageProvider(1, 12).resolve(ImageConfiguration.empty)) as TestImageInfo;
70
      expect(l.value, equals(1));
71

72
      // cache has three entries: 3(9), 4(11), 1(1)
73

74
      final TestImageInfo m = await extractOneFrame(const TestImageProvider(2, 13).resolve(ImageConfiguration.empty)) as TestImageInfo;
75
      expect(m.value, equals(13));
76

77
      // cache has three entries: 4(11), 1(1), 2(13)
78

79
      final TestImageInfo n = await extractOneFrame(const TestImageProvider(3, 14).resolve(ImageConfiguration.empty)) as TestImageInfo;
80
      expect(n.value, equals(14));
81

82
      // cache has three entries: 1(1), 2(13), 3(14)
83

84
      final TestImageInfo o = await extractOneFrame(const TestImageProvider(4, 15).resolve(ImageConfiguration.empty)) as TestImageInfo;
85
      expect(o.value, equals(15));
86

87
      // cache has three entries: 2(13), 3(14), 4(15)
88

89
      final TestImageInfo p = await extractOneFrame(const TestImageProvider(1, 16).resolve(ImageConfiguration.empty)) as TestImageInfo;
90
      expect(p.value, equals(16));
91

92 93
      // cache has three entries: 3(14), 4(15), 1(16)
    });
94

95
    test('clear removes all images and resets cache size', () async {
96
      const TestImage testImage = TestImage(width: 8, height: 8);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

      expect(imageCache.currentSize, 0);
      expect(imageCache.currentSizeBytes, 0);

      await extractOneFrame(const TestImageProvider(1, 1, image: testImage).resolve(ImageConfiguration.empty));
      await extractOneFrame(const TestImageProvider(2, 2, image: testImage).resolve(ImageConfiguration.empty));

      expect(imageCache.currentSize, 2);
      expect(imageCache.currentSizeBytes, 256 * 2);

      imageCache.clear();

      expect(imageCache.currentSize, 0);
      expect(imageCache.currentSizeBytes, 0);
    });

    test('evicts individual images', () async {
114
      const TestImage testImage = TestImage(width: 8, height: 8);
115 116 117 118 119 120 121 122 123
      await extractOneFrame(const TestImageProvider(1, 1, image: testImage).resolve(ImageConfiguration.empty));
      await extractOneFrame(const TestImageProvider(2, 2, image: testImage).resolve(ImageConfiguration.empty));

      expect(imageCache.currentSize, 2);
      expect(imageCache.currentSizeBytes, 256 * 2);
      expect(imageCache.evict(1), true);
      expect(imageCache.currentSize, 1);
      expect(imageCache.currentSizeBytes, 256);
    });
124

125
    test('Do not cache large images', () async {
126
      const TestImage testImage = TestImage(width: 8, height: 8);
127 128 129

      imageCache.maximumSizeBytes = 1;
      await extractOneFrame(const TestImageProvider(1, 1, image: testImage).resolve(ImageConfiguration.empty));
130 131 132
      expect(imageCache.currentSize, 0);
      expect(imageCache.currentSizeBytes, 0);
      expect(imageCache.maximumSizeBytes, 1);
133
    });
134 135 136

    test('Returns null if an error is caught resolving an image', () {
      final ErrorImageProvider errorImage = ErrorImageProvider();
Dan Field's avatar
Dan Field committed
137
      expect(() => imageCache.putIfAbsent(errorImage, () => errorImage.load(errorImage, null)), throwsA(isA<Error>()));
138
      bool caughtError = false;
139
      final ImageStreamCompleter result = imageCache.putIfAbsent(errorImage, () => errorImage.load(errorImage, null), onError: (dynamic error, StackTrace stackTrace) {
140 141 142 143 144
        caughtError = true;
      });
      expect(result, null);
      expect(caughtError, true);
    });
145 146 147 148 149 150 151 152 153

    test('already pending image is returned when it is put into the cache again', () async {
      const TestImage testImage = TestImage(width: 8, height: 8);

      final TestImageStreamCompleter completer1 = TestImageStreamCompleter();
      final TestImageStreamCompleter completer2 = TestImageStreamCompleter();

      final TestImageStreamCompleter resultingCompleter1 = imageCache.putIfAbsent(testImage, () {
        return completer1;
154
      }) as TestImageStreamCompleter;
155 156
      final TestImageStreamCompleter resultingCompleter2 = imageCache.putIfAbsent(testImage, () {
        return completer2;
157
      }) as TestImageStreamCompleter;
158 159 160 161 162 163 164 165 166 167 168 169 170

      expect(resultingCompleter1, completer1);
      expect(resultingCompleter2, completer1);
    });

    test('pending image is removed when cache is cleared', () async {
      const TestImage testImage = TestImage(width: 8, height: 8);

      final TestImageStreamCompleter completer1 = TestImageStreamCompleter();
      final TestImageStreamCompleter completer2 = TestImageStreamCompleter();

      final TestImageStreamCompleter resultingCompleter1 = imageCache.putIfAbsent(testImage, () {
        return completer1;
171
      }) as TestImageStreamCompleter;
172

Dan Field's avatar
Dan Field committed
173 174
      expect(imageCache.statusForKey(testImage).pending, true);
      expect(imageCache.statusForKey(testImage).live, true);
175
      imageCache.clear();
Dan Field's avatar
Dan Field committed
176 177 178 179 180
      expect(imageCache.statusForKey(testImage).pending, false);
      expect(imageCache.statusForKey(testImage).live, true);
      imageCache.clearLiveImages();
      expect(imageCache.statusForKey(testImage).pending, false);
      expect(imageCache.statusForKey(testImage).live, false);
181 182 183

      final TestImageStreamCompleter resultingCompleter2 = imageCache.putIfAbsent(testImage, () {
        return completer2;
184
      }) as TestImageStreamCompleter;
185 186 187 188 189 190 191 192 193 194 195 196 197

      expect(resultingCompleter1, completer1);
      expect(resultingCompleter2, completer2);
    });

    test('pending image is removed when image is evicted', () async {
      const TestImage testImage = TestImage(width: 8, height: 8);

      final TestImageStreamCompleter completer1 = TestImageStreamCompleter();
      final TestImageStreamCompleter completer2 = TestImageStreamCompleter();

      final TestImageStreamCompleter resultingCompleter1 = imageCache.putIfAbsent(testImage, () {
        return completer1;
198
      }) as TestImageStreamCompleter;
199 200 201 202 203

      imageCache.evict(testImage);

      final TestImageStreamCompleter resultingCompleter2 = imageCache.putIfAbsent(testImage, () {
        return completer2;
204
      }) as TestImageStreamCompleter;
205 206 207 208 209

      expect(resultingCompleter1, completer1);
      expect(resultingCompleter2, completer2);
    });

210
    test("failed image can successfully be removed from the cache's pending images", () async {
211 212
      const TestImage testImage = TestImage(width: 8, height: 8);

213 214 215 216 217 218 219 220 221
      const FailingTestImageProvider(1, 1, image: testImage)
          .resolve(ImageConfiguration.empty)
          .addListener(ImageStreamListener(
            (ImageInfo image, bool synchronousCall) { },
            onError: (dynamic exception, StackTrace stackTrace) {
              final bool evicationResult = imageCache.evict(1);
              expect(evicationResult, isTrue);
            },
          ));
222
    });
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

    test('containsKey - pending', () async {
      const TestImage testImage = TestImage(width: 8, height: 8);

      final TestImageStreamCompleter completer1 = TestImageStreamCompleter();

      final TestImageStreamCompleter resultingCompleter1 = imageCache.putIfAbsent(testImage, () {
        return completer1;
      }) as TestImageStreamCompleter;

      expect(resultingCompleter1, completer1);
      expect(imageCache.containsKey(testImage), true);
    });

    test('containsKey - completed', () async {
      const TestImage testImage = TestImage(width: 8, height: 8);

      final TestImageStreamCompleter completer1 = TestImageStreamCompleter();

      final TestImageStreamCompleter resultingCompleter1 = imageCache.putIfAbsent(testImage, () {
        return completer1;
      }) as TestImageStreamCompleter;

      // Mark as complete
      completer1.testSetImage(testImage);

      expect(resultingCompleter1, completer1);
      expect(imageCache.containsKey(testImage), true);
Dan Field's avatar
Dan Field committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    });

    test('putIfAbsent updates LRU properties of a live image', () async {
      imageCache.maximumSize = 1;
      const TestImage testImage = TestImage(width: 8, height: 8);
      const TestImage testImage2 = TestImage(width: 10, height: 10);

      final TestImageStreamCompleter completer1 = TestImageStreamCompleter()..testSetImage(testImage);
      final TestImageStreamCompleter completer2 = TestImageStreamCompleter()..testSetImage(testImage2);

      completer1.addListener(ImageStreamListener((ImageInfo info, bool syncCall) {}));

      final TestImageStreamCompleter resultingCompleter1 = imageCache.putIfAbsent(testImage, () {
        return completer1;
      }) as TestImageStreamCompleter;

      expect(imageCache.statusForKey(testImage).pending, false);
      expect(imageCache.statusForKey(testImage).keepAlive, true);
      expect(imageCache.statusForKey(testImage).live, true);
      expect(imageCache.statusForKey(testImage2).untracked, true);
      final TestImageStreamCompleter resultingCompleter2 = imageCache.putIfAbsent(testImage2, () {
        return completer2;
      }) as TestImageStreamCompleter;


      expect(imageCache.statusForKey(testImage).pending, false);
      expect(imageCache.statusForKey(testImage).keepAlive, false); // evicted
      expect(imageCache.statusForKey(testImage).live, true);
      expect(imageCache.statusForKey(testImage2).pending, false);
      expect(imageCache.statusForKey(testImage2).keepAlive, true); // took the LRU spot.
      expect(imageCache.statusForKey(testImage2).live, false); // no listeners

      expect(resultingCompleter1, completer1);
      expect(resultingCompleter2, completer2);
    });

    test('Live image cache avoids leaks of unlistened streams', () async {
      imageCache.maximumSize = 3;

290 291 292 293 294 295
      const TestImageProvider(1, 1).resolve(ImageConfiguration.empty);
      const TestImageProvider(2, 2).resolve(ImageConfiguration.empty);
      const TestImageProvider(3, 3).resolve(ImageConfiguration.empty);
      const TestImageProvider(4, 4).resolve(ImageConfiguration.empty);
      const TestImageProvider(5, 5).resolve(ImageConfiguration.empty);
      const TestImageProvider(6, 6).resolve(ImageConfiguration.empty);
Dan Field's avatar
Dan Field committed
296 297 298 299 300 301 302 303 304 305 306

      // wait an event loop to let image resolution process.
      await null;

      expect(imageCache.currentSize, 3);
      expect(imageCache.liveImageCount, 0);
    });

    test('Disabled image cache does not leak live images', () async {
      imageCache.maximumSize = 0;

307 308 309 310 311 312
      const TestImageProvider(1, 1).resolve(ImageConfiguration.empty);
      const TestImageProvider(2, 2).resolve(ImageConfiguration.empty);
      const TestImageProvider(3, 3).resolve(ImageConfiguration.empty);
      const TestImageProvider(4, 4).resolve(ImageConfiguration.empty);
      const TestImageProvider(5, 5).resolve(ImageConfiguration.empty);
      const TestImageProvider(6, 6).resolve(ImageConfiguration.empty);
Dan Field's avatar
Dan Field committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

      // wait an event loop to let image resolution process.
      await null;

      expect(imageCache.currentSize, 0);
      expect(imageCache.liveImageCount, 0);
    });

    test('Evicting a pending image clears the live image by default', () async {
      const TestImage testImage = TestImage(width: 8, height: 8);

      final TestImageStreamCompleter completer1 = TestImageStreamCompleter();

      imageCache.putIfAbsent(testImage, () => completer1);
      expect(imageCache.statusForKey(testImage).pending, true);
      expect(imageCache.statusForKey(testImage).live, true);
      expect(imageCache.statusForKey(testImage).keepAlive, false);

      imageCache.evict(testImage);
      expect(imageCache.statusForKey(testImage).untracked, true);
    });

    test('Evicting a pending image does clear the live image when includeLive is false and only cache listening', () async {
      const TestImage testImage = TestImage(width: 8, height: 8);

      final TestImageStreamCompleter completer1 = TestImageStreamCompleter();

      imageCache.putIfAbsent(testImage, () => completer1);
      expect(imageCache.statusForKey(testImage).pending, true);
      expect(imageCache.statusForKey(testImage).live, true);
      expect(imageCache.statusForKey(testImage).keepAlive, false);

      imageCache.evict(testImage, includeLive: false);
      expect(imageCache.statusForKey(testImage).pending, false);
      expect(imageCache.statusForKey(testImage).live, false);
      expect(imageCache.statusForKey(testImage).keepAlive, false);
    });

    test('Evicting a pending image does clear the live image when includeLive is false and some other listener', () async {
      const TestImage testImage = TestImage(width: 8, height: 8);

      final TestImageStreamCompleter completer1 = TestImageStreamCompleter();

      imageCache.putIfAbsent(testImage, () => completer1);
      expect(imageCache.statusForKey(testImage).pending, true);
      expect(imageCache.statusForKey(testImage).live, true);
      expect(imageCache.statusForKey(testImage).keepAlive, false);

      completer1.addListener(ImageStreamListener((_, __) {}));
      imageCache.evict(testImage, includeLive: false);
      expect(imageCache.statusForKey(testImage).pending, false);
      expect(imageCache.statusForKey(testImage).live, true);
      expect(imageCache.statusForKey(testImage).keepAlive, false);
    });

    test('Evicting a completed image does clear the live image by default', () async {
      const TestImage testImage = TestImage(width: 8, height: 8);

      final TestImageStreamCompleter completer1 = TestImageStreamCompleter()
        ..testSetImage(testImage)
        ..addListener(ImageStreamListener((ImageInfo info, bool syncCall) {}));

      imageCache.putIfAbsent(testImage, () => completer1);
      expect(imageCache.statusForKey(testImage).pending, false);
      expect(imageCache.statusForKey(testImage).live, true);
      expect(imageCache.statusForKey(testImage).keepAlive, true);

      imageCache.evict(testImage);
      expect(imageCache.statusForKey(testImage).untracked, true);
    });

    test('Evicting a completed image does not clear the live image when includeLive is set to false', () async {
      const TestImage testImage = TestImage(width: 8, height: 8);

      final TestImageStreamCompleter completer1 = TestImageStreamCompleter()
        ..testSetImage(testImage)
        ..addListener(ImageStreamListener((ImageInfo info, bool syncCall) {}));

      imageCache.putIfAbsent(testImage, () => completer1);
      expect(imageCache.statusForKey(testImage).pending, false);
      expect(imageCache.statusForKey(testImage).live, true);
      expect(imageCache.statusForKey(testImage).keepAlive, true);
395

Dan Field's avatar
Dan Field committed
396 397 398 399
      imageCache.evict(testImage, includeLive: false);
      expect(imageCache.statusForKey(testImage).pending, false);
      expect(imageCache.statusForKey(testImage).live, true);
      expect(imageCache.statusForKey(testImage).keepAlive, false);
400
    });
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

    test('Clearing liveImages removes callbacks', () async {
      const TestImage testImage = TestImage(width: 8, height: 8);

      final ImageStreamListener listener = ImageStreamListener((ImageInfo info, bool syncCall) {});

      final TestImageStreamCompleter completer1 = TestImageStreamCompleter()
        ..testSetImage(testImage)
        ..addListener(listener);

      final TestImageStreamCompleter completer2 = TestImageStreamCompleter()
        ..testSetImage(testImage)
        ..addListener(listener);

      imageCache.putIfAbsent(testImage, () => completer1);
      expect(imageCache.statusForKey(testImage).pending, false);
      expect(imageCache.statusForKey(testImage).live, true);
      expect(imageCache.statusForKey(testImage).keepAlive, true);

      imageCache.clear();
      imageCache.clearLiveImages();
      expect(imageCache.statusForKey(testImage).pending, false);
      expect(imageCache.statusForKey(testImage).live, false);
      expect(imageCache.statusForKey(testImage).keepAlive, false);

      imageCache.putIfAbsent(testImage, () => completer2);
      expect(imageCache.statusForKey(testImage).pending, false);
      expect(imageCache.statusForKey(testImage).live, true);
      expect(imageCache.statusForKey(testImage).keepAlive, true);

      completer1.removeListener(listener);

      expect(imageCache.statusForKey(testImage).pending, false);
      expect(imageCache.statusForKey(testImage).live, true);
      expect(imageCache.statusForKey(testImage).keepAlive, true);
    });

    test('Live image gets size updated', () async {
      // Add an image to the cache in pending state
      // Complete it once it is in there as live
      // Evict it but leave the live one.
      // Add it again.
      // If the live image did not track the size properly, the last line of
      // this test will fail.

      const TestImage testImage = TestImage(width: 8, height: 8);
      const int testImageSize = 8 * 8 * 4;

      final ImageStreamListener listener = ImageStreamListener((ImageInfo info, bool syncCall) {});

      final TestImageStreamCompleter completer1 = TestImageStreamCompleter()
        ..addListener(listener);


      imageCache.putIfAbsent(testImage, () => completer1);
      expect(imageCache.statusForKey(testImage).pending, true);
      expect(imageCache.statusForKey(testImage).live, true);
      expect(imageCache.statusForKey(testImage).keepAlive, false);
      expect(imageCache.currentSizeBytes, 0);

      completer1.testSetImage(testImage);

      expect(imageCache.statusForKey(testImage).pending, false);
      expect(imageCache.statusForKey(testImage).live, true);
      expect(imageCache.statusForKey(testImage).keepAlive, true);
      expect(imageCache.currentSizeBytes, testImageSize);

      imageCache.evict(testImage, includeLive: false);

      expect(imageCache.statusForKey(testImage).pending, false);
      expect(imageCache.statusForKey(testImage).live, true);
      expect(imageCache.statusForKey(testImage).keepAlive, false);
      expect(imageCache.currentSizeBytes, 0);

      imageCache.putIfAbsent(testImage, () => completer1);

      expect(imageCache.statusForKey(testImage).pending, false);
      expect(imageCache.statusForKey(testImage).live, true);
      expect(imageCache.statusForKey(testImage).keepAlive, true);
      expect(imageCache.currentSizeBytes, testImageSize);
    });
482 483
  });
}