picture_cache_test.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import 'package:flutter/widgets.dart';
  2. import 'package:flutter_svg/flutter_svg.dart';
  3. import 'package:flutter_svg/src/picture_cache.dart';
  4. import 'package:flutter_svg/src/picture_provider.dart';
  5. import 'package:flutter_test/flutter_test.dart';
  6. import 'package:xml/xml.dart';
  7. class MockPictureStreamCompleter extends PictureStreamCompleter {}
  8. void main() {
  9. PictureCache cache;
  10. setUp(() {
  11. cache = PictureCache();
  12. });
  13. testWidgets('Precache test', (WidgetTester tester) async {
  14. const String svgString = '''<svg viewBox="0 0 10 10">
  15. <rect x="1" y="1" width="5" height="5" fill="black" />
  16. </svg>''';
  17. const String svgString2 = '''<svg viewBox="0 0 10 10">
  18. <rect x="1" y="1" width="6" height="5" fill="black" />
  19. </svg>''';
  20. await tester.pumpWidget(
  21. const Directionality(
  22. textDirection: TextDirection.ltr,
  23. child: Text('test_text'),
  24. ),
  25. );
  26. expect(PictureProvider.cacheCount, 0);
  27. await precachePicture(
  28. StringPicture(
  29. SvgPicture.svgStringDecoder,
  30. svgString,
  31. ),
  32. tester.element(find.text('test_text')),
  33. );
  34. expect(PictureProvider.cacheCount, 1);
  35. await tester.pumpWidget(
  36. Directionality(
  37. textDirection: TextDirection.ltr,
  38. child: SvgPicture.string(svgString),
  39. ),
  40. );
  41. expect(PictureProvider.cacheCount, 1);
  42. await tester.pumpWidget(
  43. Directionality(
  44. textDirection: TextDirection.ltr,
  45. child: SvgPicture.string(svgString2),
  46. ),
  47. );
  48. expect(PictureProvider.cacheCount, 2);
  49. PictureProvider.clearCache();
  50. expect(PictureProvider.cacheCount, 0);
  51. });
  52. testWidgets('Precache - null context', (WidgetTester tester) async {
  53. const String svgString = '''<svg viewBox="0 0 10 10">
  54. <rect x="1" y="1" width="5" height="5" fill="black" />
  55. </svg>''';
  56. expect(PictureProvider.cacheCount, 0);
  57. await precachePicture(
  58. StringPicture(
  59. SvgPicture.svgStringDecoder,
  60. svgString,
  61. ),
  62. null,
  63. );
  64. expect(PictureProvider.cacheCount, 1);
  65. });
  66. testWidgets('Precache with error', (WidgetTester tester) async {
  67. const String svgString = '<svg';
  68. await tester.pumpWidget(
  69. const Directionality(
  70. textDirection: TextDirection.ltr,
  71. child: Text('test_text'),
  72. ),
  73. );
  74. bool gotError = false;
  75. void errorListener(dynamic error, StackTrace stackTrace) {
  76. gotError = true;
  77. expect(error, isInstanceOf<XmlParserException>());
  78. }
  79. await precachePicture(
  80. StringPicture(
  81. SvgPicture.svgStringDecoder,
  82. svgString,
  83. ),
  84. tester.element(find.text('test_text')),
  85. onError: errorListener,
  86. );
  87. expect(tester.takeException(), isInstanceOf<XmlParserException>());
  88. expect(gotError, isTrue);
  89. });
  90. test('Cache Tests', () {
  91. expect(cache.maximumSize, equals(1000));
  92. cache.maximumSize = 1;
  93. expect(cache.maximumSize, equals(1));
  94. expect(() => cache.maximumSize = -1, throwsAssertionError);
  95. expect(() => cache.maximumSize = null, throwsAssertionError);
  96. expect(() => cache.putIfAbsent(null, null), throwsAssertionError);
  97. expect(() => cache.putIfAbsent(1, null), throwsAssertionError);
  98. final MockPictureStreamCompleter completer1 = MockPictureStreamCompleter();
  99. final MockPictureStreamCompleter completer2 = MockPictureStreamCompleter();
  100. expect(cache.putIfAbsent(1, () => completer1), completer1);
  101. expect(cache.putIfAbsent(1, () => completer1), completer1);
  102. expect(cache.putIfAbsent(2, () => completer2), completer2);
  103. cache.clear();
  104. });
  105. }