binding_test.dart 3.43 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 'dart:ui' as ui;
6

Dan Field's avatar
Dan Field committed
7
import 'package:flutter/foundation.dart';
8
import 'package:flutter/painting.dart';
9
import 'package:flutter/scheduler.dart';
Dan Field's avatar
Dan Field committed
10
import 'package:flutter/services.dart';
11
import 'package:flutter/widgets.dart';
12 13 14 15
import 'package:flutter_test/flutter_test.dart';

Future<void> main() async {
  final ui.Image image = await createTestImage();
16

17
  testWidgets('didHaveMemoryPressure clears imageCache', (WidgetTester tester) async {
18
    imageCache.putIfAbsent(1, () => OneFrameImageStreamCompleter(
19
      Future<ImageInfo>.value(ImageInfo(
20
        image: image,
21 22
      )),
    ));
23 24

    await tester.idle();
25
    expect(imageCache.currentSize, 1);
26
    final ByteData message = const JSONMessageCodec().encodeMessage(<String, dynamic>{'type': 'memoryPressure'})!;
27 28
    await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage('flutter/system', message, (_) { });
    expect(imageCache.currentSize, 0);
29
  });
Dan Field's avatar
Dan Field committed
30 31 32 33 34 35 36 37 38 39

  test('evict clears live references', () async {
    final TestPaintingBinding binding = TestPaintingBinding();
    expect(binding.imageCache.clearCount, 0);
    expect(binding.imageCache.liveClearCount, 0);

    binding.evict('/path/to/asset.png');
    expect(binding.imageCache.clearCount, 1);
    expect(binding.imageCache.liveClearCount, 1);
  });
40 41 42 43

  test('ShaderWarmUp is null by default', () {
    expect(PaintingBinding.shaderWarmUp, null);
  });
44
}
Dan Field's avatar
Dan Field committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

class TestBindingBase implements BindingBase {
  @override
  void initInstances() {}

  @override
  void initServiceExtensions() {}

  @override
  Future<void> lockEvents(Future<void> Function() callback) async {}

  @override
  bool get locked => throw UnimplementedError();

  @override
  Future<void> performReassemble() {
    throw UnimplementedError();
  }

  @override
  void postEvent(String eventKind, Map<String, dynamic> eventData) {}

  @override
  Future<void> reassembleApplication() {
    throw UnimplementedError();
  }

  @override
73
  void registerBoolServiceExtension({required String name, required AsyncValueGetter<bool> getter, required AsyncValueSetter<bool> setter}) {}
Dan Field's avatar
Dan Field committed
74 75

  @override
76
  void registerNumericServiceExtension({required String name, required AsyncValueGetter<double> getter, required AsyncValueSetter<double> setter}) {}
Dan Field's avatar
Dan Field committed
77 78

  @override
79
  void registerServiceExtension({required String name, required ServiceExtensionCallback callback}) {}
Dan Field's avatar
Dan Field committed
80 81

  @override
82
  void registerSignalServiceExtension({required String name, required AsyncCallback callback}) {}
Dan Field's avatar
Dan Field committed
83 84

  @override
85
  void registerStringServiceExtension({required String name, required AsyncValueGetter<String> getter, required AsyncValueSetter<String> setter}) {}
Dan Field's avatar
Dan Field committed
86 87 88 89 90

  @override
  void unlocked() {}

  @override
91 92 93 94
  ui.SingletonFlutterWindow get window => throw UnimplementedError();

  @override
  ui.PlatformDispatcher get platformDispatcher => throw UnimplementedError();
Dan Field's avatar
Dan Field committed
95 96
}

97
class TestPaintingBinding extends TestBindingBase with SchedulerBinding, ServicesBinding, PaintingBinding {
Dan Field's avatar
Dan Field committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  @override
  final FakeImageCache imageCache = FakeImageCache();

  @override
  ImageCache createImageCache() => imageCache;
}

class FakeImageCache extends ImageCache {
  int clearCount = 0;
  int liveClearCount = 0;

  @override
  void clear() {
    clearCount += 1;
    super.clear();
  }

  @override
  void clearLiveImages() {
    liveClearCount += 1;
    super.clearLiveImages();
  }
120
}