xml_svg_test.dart 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import 'dart:ui';
  2. import 'package:test/test.dart';
  3. import 'package:xml/xml_events.dart';
  4. import 'package:flutter_svg/src/svg/xml_parsers.dart';
  5. import 'package:flutter_svg/src/utilities/xml.dart';
  6. void main() {
  7. test('Xlink href tests', () {
  8. final XmlStartElementEvent el =
  9. parseEvents('<test href="http://localhost" />').first
  10. as XmlStartElementEvent;
  11. final XmlStartElementEvent elXlink =
  12. parseEvents('<test xmlns:xlink="$kXlinkNamespace" '
  13. 'xlink:href="http://localhost" />')
  14. .first as XmlStartElementEvent;
  15. expect(getHrefAttribute(el.attributes), 'http://localhost');
  16. expect(getHrefAttribute(elXlink.attributes), 'http://localhost');
  17. });
  18. test('Attribute and style tests', () {
  19. final XmlStartElementEvent el =
  20. parseEvents('<test stroke="#fff" fill="#eee" stroke-dashpattern="1 2" '
  21. 'style="stroke-opacity:1;fill-opacity:.23" />')
  22. .first as XmlStartElementEvent;
  23. expect(getAttribute(el.attributes, 'stroke'), '#fff');
  24. expect(getAttribute(el.attributes, 'fill'), '#eee');
  25. expect(getAttribute(el.attributes, 'stroke-dashpattern'), '1 2');
  26. expect(getAttribute(el.attributes, 'stroke-opacity'), '1');
  27. expect(getAttribute(el.attributes, 'stroke-another'), '');
  28. expect(getAttribute(el.attributes, 'fill-opacity'), '.23');
  29. expect(getAttribute(el.attributes, 'fill-opacity', checkStyle: false), '');
  30. expect(getAttribute(el.attributes, 'fill', checkStyle: false), '#eee');
  31. });
  32. // if the parsing logic changes, we can simplify some methods. for now assert that whitespace in attributes is preserved
  33. test('Attribute WhiteSpace test', () {
  34. final XmlStartElementEvent xd =
  35. parseEvents('<test attr=" asdf" attr2="asdf " attr3="asdf" />').first
  36. as XmlStartElementEvent;
  37. expect(
  38. xd.attributes[0].value,
  39. ' asdf',
  40. reason:
  41. 'XML Parsing implementation no longer preserves leading whitespace in attributes!',
  42. );
  43. expect(
  44. xd.attributes[1].value,
  45. 'asdf ',
  46. reason:
  47. 'XML Parsing implementation no longer preserves trailing whitespace in attributes!',
  48. );
  49. });
  50. test('viewBox tests', () {
  51. const Rect rect = Rect.fromLTWH(0.0, 0.0, 100.0, 100.0);
  52. final XmlStartElementEvent svgWithViewBox =
  53. parseEvents('<svg viewBox="0 0 100 100" />').first
  54. as XmlStartElementEvent;
  55. final XmlStartElementEvent svgWithViewBoxAndWidthHeight =
  56. parseEvents('<svg width="50px" height="50px" viewBox="0 0 100 100" />')
  57. .first as XmlStartElementEvent;
  58. final XmlStartElementEvent svgWithWidthHeight =
  59. parseEvents('<svg width="100" height="100" />').first
  60. as XmlStartElementEvent;
  61. final XmlStartElementEvent svgWithViewBoxMinXMinY =
  62. parseEvents('<svg viewBox="42 56 100 100" />').first
  63. as XmlStartElementEvent;
  64. final XmlStartElementEvent svgWithNoSizeInfo =
  65. parseEvents('<svg />').first as XmlStartElementEvent;
  66. expect(parseViewBox(svgWithViewBoxAndWidthHeight.attributes).size,
  67. const Size(50, 50));
  68. expect(parseViewBox(svgWithViewBox.attributes).viewBoxRect, rect);
  69. expect(parseViewBox(svgWithViewBox.attributes).viewBoxOffset, Offset.zero);
  70. expect(parseViewBox(svgWithViewBoxAndWidthHeight.attributes).viewBoxRect,
  71. rect);
  72. expect(parseViewBox(svgWithWidthHeight.attributes).viewBoxRect, rect);
  73. expect(parseViewBox(svgWithNoSizeInfo.attributes, nullOk: true), null);
  74. expect(() => parseViewBox(svgWithNoSizeInfo.attributes), throwsStateError);
  75. expect(parseViewBox(svgWithViewBoxMinXMinY.attributes).viewBoxRect, rect);
  76. expect(parseViewBox(svgWithViewBoxMinXMinY.attributes).viewBoxOffset,
  77. const Offset(-42.0, -56.0));
  78. });
  79. test('TileMode tests', () {
  80. final XmlStartElementEvent pad =
  81. parseEvents('<linearGradient spreadMethod="pad" />').first
  82. as XmlStartElementEvent;
  83. final XmlStartElementEvent reflect =
  84. parseEvents('<linearGradient spreadMethod="reflect" />').first
  85. as XmlStartElementEvent;
  86. final XmlStartElementEvent repeat =
  87. parseEvents('<linearGradient spreadMethod="repeat" />').first
  88. as XmlStartElementEvent;
  89. final XmlStartElementEvent invalid =
  90. parseEvents('<linearGradient spreadMethod="invalid" />').first
  91. as XmlStartElementEvent;
  92. final XmlStartElementEvent none =
  93. parseEvents('<linearGradient />').first as XmlStartElementEvent;
  94. expect(parseTileMode(pad.attributes), TileMode.clamp);
  95. expect(parseTileMode(invalid.attributes), TileMode.clamp);
  96. expect(parseTileMode(none.attributes), TileMode.clamp);
  97. expect(parseTileMode(reflect.attributes), TileMode.mirror);
  98. expect(parseTileMode(repeat.attributes), TileMode.repeated);
  99. });
  100. test('@stroke-dashoffset tests', () {
  101. final XmlStartElementEvent abs =
  102. parseEvents('<stroke stroke-dashoffset="20" />').first
  103. as XmlStartElementEvent;
  104. final XmlStartElementEvent pct =
  105. parseEvents('<stroke stroke-dashoffset="20%" />').first
  106. as XmlStartElementEvent;
  107. // TODO(dnfield): DashOffset is completely opaque right now, maybe expose the raw value?
  108. expect(parseDashOffset(abs.attributes), isNotNull);
  109. expect(parseDashOffset(pct.attributes), isNotNull);
  110. });
  111. test('font-weight tests', () {
  112. expect(parseFontWeight('100'), FontWeight.w100);
  113. expect(parseFontWeight('200'), FontWeight.w200);
  114. expect(parseFontWeight('300'), FontWeight.w300);
  115. expect(parseFontWeight('400'), FontWeight.w400);
  116. expect(parseFontWeight('500'), FontWeight.w500);
  117. expect(parseFontWeight('600'), FontWeight.w600);
  118. expect(parseFontWeight('700'), FontWeight.w700);
  119. expect(parseFontWeight('800'), FontWeight.w800);
  120. expect(parseFontWeight('900'), FontWeight.w900);
  121. expect(parseFontWeight('normal'), FontWeight.normal);
  122. expect(parseFontWeight('bold'), FontWeight.bold);
  123. expect(() => parseFontWeight('invalid'), throwsUnsupportedError);
  124. });
  125. }