OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library SvgElementTest; |
| 6 import 'dart:html'; |
| 7 import 'dart:svg' as svg; |
| 8 import 'package:expect/expect.dart'; |
| 9 import 'package:unittest/html_individual_config.dart'; |
| 10 import 'package:unittest/unittest.dart'; |
| 11 |
| 12 main() { |
| 13 useHtmlIndividualConfiguration(); |
| 14 |
| 15 var isSvgSvgElement = |
| 16 predicate((x) => x is svg.SvgSvgElement, 'is a SvgSvgElement'); |
| 17 |
| 18 List<String> _nodeStrings(Iterable<Node> input) { |
| 19 final out = new List<String>(); |
| 20 for (Node n in input) { |
| 21 if (n is Element) { |
| 22 Element e = n; |
| 23 out.add(e.tagName); |
| 24 } else { |
| 25 out.add(n.text); |
| 26 } |
| 27 } |
| 28 return out; |
| 29 }; |
| 30 |
| 31 testConstructor(String tagName, Function isExpectedClass, |
| 32 [bool expectation = true, allowsInnerHtml = true]) { |
| 33 test(tagName, () { |
| 34 expect(isExpectedClass(new svg.SvgElement.tag(tagName)), expectation); |
| 35 if (allowsInnerHtml) { |
| 36 expect(isExpectedClass(new svg.SvgElement.svg('<$tagName></$tagName>')), |
| 37 expectation && allowsInnerHtml); |
| 38 } |
| 39 }); |
| 40 } |
| 41 group('additionalConstructors', () { |
| 42 test('valid', () { |
| 43 final svgContent = |
| 44 "<svg version=\"1.1\">\n" |
| 45 " <circle></circle>\n" |
| 46 " <path></path>\n" |
| 47 "</svg>"; |
| 48 final el = new svg.SvgElement.svg(svgContent); |
| 49 expect(el, isSvgSvgElement); |
| 50 expect(el.innerHtml, anyOf("<circle></circle><path></path>", '<circle ' |
| 51 'xmlns="http://www.w3.org/2000/svg" /><path ' |
| 52 'xmlns="http://www.w3.org/2000/svg" />')); |
| 53 expect(el.outerHtml, anyOf(svgContent, |
| 54 '<svg xmlns="http://www.w3.org/2000/svg" version="1.1">\n ' |
| 55 '<circle />\n <path />\n</svg>')); |
| 56 }); |
| 57 |
| 58 test('has no parent', () => |
| 59 expect(new svg.SvgElement.svg('<circle/>').parent, isNull) |
| 60 ); |
| 61 |
| 62 test('empty', () { |
| 63 expect(() => new svg.SvgElement.svg(""), throwsStateError); |
| 64 }); |
| 65 |
| 66 test('too many elements', () { |
| 67 expect(() => new svg.SvgElement.svg("<circle></circle><path></path>"), |
| 68 throwsStateError); |
| 69 }); |
| 70 }); |
| 71 |
| 72 // Unfortunately, because the filtering mechanism in unitttest is a regex done |
| 73 group('supported_animate', () { |
| 74 test('supported', () { |
| 75 expect(svg.AnimateElement.supported, true); |
| 76 }); |
| 77 }); |
| 78 |
| 79 group('supported_animateMotion', () { |
| 80 test('supported', () { |
| 81 expect(svg.AnimateMotionElement.supported, true); |
| 82 }); |
| 83 }); |
| 84 |
| 85 group('supported_animateTransform', () { |
| 86 test('supported', () { |
| 87 expect(svg.AnimateTransformElement.supported, true); |
| 88 }); |
| 89 }); |
| 90 |
| 91 group('supported_feBlend', () { |
| 92 test('supported', () { |
| 93 expect(svg.FEBlendElement.supported, true); |
| 94 }); |
| 95 }); |
| 96 |
| 97 group('supported_feColorMatrix', () { |
| 98 test('supported', () { |
| 99 expect(svg.FEColorMatrixElement.supported, true); |
| 100 }); |
| 101 }); |
| 102 |
| 103 group('supported_feComponentTransfer', () { |
| 104 test('supported', () { |
| 105 expect(svg.FEComponentTransferElement.supported, true); |
| 106 }); |
| 107 }); |
| 108 |
| 109 group('supported_feConvolveMatrix', () { |
| 110 test('supported', () { |
| 111 expect(svg.FEConvolveMatrixElement.supported, true); |
| 112 }); |
| 113 }); |
| 114 |
| 115 group('supported_feDiffuseLighting', () { |
| 116 test('supported', () { |
| 117 expect(svg.FEDiffuseLightingElement.supported, true); |
| 118 }); |
| 119 }); |
| 120 |
| 121 group('supported_feDisplacementMap', () { |
| 122 test('supported', () { |
| 123 expect(svg.FEDisplacementMapElement.supported, true); |
| 124 }); |
| 125 }); |
| 126 |
| 127 group('supported_feDistantLight', () { |
| 128 test('supported', () { |
| 129 expect(svg.FEDistantLightElement.supported, true); |
| 130 }); |
| 131 }); |
| 132 |
| 133 group('supported_feFlood', () { |
| 134 test('supported', () { |
| 135 expect(svg.FEFloodElement.supported, true); |
| 136 }); |
| 137 }); |
| 138 |
| 139 group('supported_feFuncA', () { |
| 140 test('supported', () { |
| 141 expect(svg.FEFuncAElement.supported, true); |
| 142 }); |
| 143 }); |
| 144 |
| 145 group('supported_feFuncB', () { |
| 146 test('supported', () { |
| 147 expect(svg.FEFuncBElement.supported, true); |
| 148 }); |
| 149 }); |
| 150 |
| 151 group('supported_feFuncG', () { |
| 152 test('supported', () { |
| 153 expect(svg.FEFuncGElement.supported, true); |
| 154 }); |
| 155 }); |
| 156 |
| 157 group('supported_feFuncR', () { |
| 158 test('supported', () { |
| 159 expect(svg.FEFuncRElement.supported, true); |
| 160 }); |
| 161 }); |
| 162 |
| 163 group('supported_feGaussianBlur', () { |
| 164 test('supported', () { |
| 165 expect(svg.FEGaussianBlurElement.supported, true); |
| 166 }); |
| 167 }); |
| 168 |
| 169 group('supported_feImage', () { |
| 170 test('supported', () { |
| 171 expect(svg.FEImageElement.supported, true); |
| 172 }); |
| 173 }); |
| 174 |
| 175 group('supported_feMerge', () { |
| 176 test('supported', () { |
| 177 expect(svg.FEMergeElement.supported, true); |
| 178 }); |
| 179 }); |
| 180 |
| 181 group('supported_feMergeNode', () { |
| 182 test('supported', () { |
| 183 expect(svg.FEMergeNodeElement.supported, true); |
| 184 }); |
| 185 }); |
| 186 |
| 187 group('supported_feOffset', () { |
| 188 test('supported', () { |
| 189 expect(svg.FEOffsetElement.supported, true); |
| 190 }); |
| 191 }); |
| 192 |
| 193 group('supported_feComponentTransfer', () { |
| 194 test('supported', () { |
| 195 expect(svg.FEPointLightElement.supported, true); |
| 196 }); |
| 197 }); |
| 198 |
| 199 group('supported_feSpecularLighting', () { |
| 200 test('supported', () { |
| 201 expect(svg.FESpecularLightingElement.supported, true); |
| 202 }); |
| 203 }); |
| 204 |
| 205 group('supported_feComponentTransfer', () { |
| 206 test('supported', () { |
| 207 expect(svg.FESpotLightElement.supported, true); |
| 208 }); |
| 209 }); |
| 210 |
| 211 group('supported_feTile', () { |
| 212 test('supported', () { |
| 213 expect(svg.FETileElement.supported, true); |
| 214 }); |
| 215 }); |
| 216 |
| 217 group('supported_feTurbulence', () { |
| 218 test('supported', () { |
| 219 expect(svg.FETurbulenceElement.supported, true); |
| 220 }); |
| 221 }); |
| 222 |
| 223 group('supported_filter', () { |
| 224 test('supported', () { |
| 225 expect(svg.FilterElement.supported, true); |
| 226 }); |
| 227 }); |
| 228 |
| 229 group('supported_foreignObject', () { |
| 230 test('supported', () { |
| 231 expect(svg.ForeignObjectElement.supported, true); |
| 232 }); |
| 233 }); |
| 234 |
| 235 group('supported_set', () { |
| 236 test('supported', () { |
| 237 expect(svg.SetElement.supported, true); |
| 238 }); |
| 239 }); |
| 240 |
| 241 group('constructors', () { |
| 242 testConstructor('a', (e) => e is svg.AElement); |
| 243 testConstructor('circle', (e) => e is svg.CircleElement); |
| 244 testConstructor('clipPath', (e) => e is svg.ClipPathElement); |
| 245 testConstructor('defs', (e) => e is svg.DefsElement); |
| 246 testConstructor('desc', (e) => e is svg.DescElement); |
| 247 testConstructor('ellipse', (e) => e is svg.EllipseElement); |
| 248 testConstructor('g', (e) => e is svg.GElement); |
| 249 testConstructor('image', (e) => e is svg.ImageElement); |
| 250 testConstructor('line', (e) => e is svg.LineElement); |
| 251 testConstructor('linearGradient', (e) => e is svg.LinearGradientElement); |
| 252 testConstructor('marker', (e) => e is svg.MarkerElement); |
| 253 testConstructor('mask', (e) => e is svg.MaskElement); |
| 254 testConstructor('path', (e) => e is svg.PathElement); |
| 255 testConstructor('pattern', (e) => e is svg.PatternElement); |
| 256 testConstructor('polygon', (e) => e is svg.PolygonElement); |
| 257 testConstructor('polyline', (e) => e is svg.PolylineElement); |
| 258 testConstructor('radialGradient', (e) => e is svg.RadialGradientElement); |
| 259 testConstructor('rect', (e) => e is svg.RectElement); |
| 260 test('script', () { |
| 261 expect(new svg.SvgElement.tag('script') is svg.ScriptElement, isTrue); |
| 262 }); |
| 263 testConstructor('stop', (e) => e is svg.StopElement); |
| 264 testConstructor('style', (e) => e is svg.StyleElement); |
| 265 testConstructor('switch', (e) => e is svg.SwitchElement); |
| 266 testConstructor('symbol', (e) => e is svg.SymbolElement); |
| 267 testConstructor('tspan', (e) => e is svg.TSpanElement); |
| 268 testConstructor('text', (e) => e is svg.TextElement); |
| 269 testConstructor('textPath', (e) => e is svg.TextPathElement); |
| 270 testConstructor('title', (e) => e is svg.TitleElement); |
| 271 testConstructor('use', (e) => e is svg.UseElement); |
| 272 testConstructor('view', (e) => e is svg.ViewElement); |
| 273 // TODO(alanknight): Issue 23144 |
| 274 testConstructor('animate', (e) => e is svg.AnimateElement, |
| 275 svg.AnimateElement.supported); |
| 276 testConstructor('animateMotion', (e) => e is svg.AnimateMotionElement, |
| 277 svg.AnimateMotionElement.supported); |
| 278 testConstructor('animateTransform', (e) => e is svg.AnimateTransformElemen
t, |
| 279 svg.AnimateTransformElement.supported); |
| 280 testConstructor('feBlend', (e) => e is svg.FEBlendElement, |
| 281 svg.FEBlendElement.supported); |
| 282 testConstructor('feColorMatrix', (e) => e is svg.FEColorMatrixElement, |
| 283 svg.FEColorMatrixElement.supported); |
| 284 testConstructor('feComponentTransfer', |
| 285 (e) => e is svg.FEComponentTransferElement, |
| 286 svg.FEComponentTransferElement.supported); |
| 287 testConstructor('feConvolveMatrix', |
| 288 (e) => e is svg.FEConvolveMatrixElement, |
| 289 svg.FEConvolveMatrixElement.supported); |
| 290 testConstructor('feDiffuseLighting', |
| 291 (e) => e is svg.FEDiffuseLightingElement, |
| 292 svg.FEDiffuseLightingElement.supported); |
| 293 testConstructor('feDisplacementMap', |
| 294 (e) => e is svg.FEDisplacementMapElement, |
| 295 svg.FEDisplacementMapElement.supported); |
| 296 testConstructor('feDistantLight', (e) => e is svg.FEDistantLightElement, |
| 297 svg.FEDistantLightElement.supported); |
| 298 testConstructor('feFlood', (e) => e is svg.FEFloodElement, |
| 299 svg.FEFloodElement.supported); |
| 300 testConstructor('feFuncA', (e) => e is svg.FEFuncAElement, |
| 301 svg.FEFuncAElement.supported); |
| 302 testConstructor('feFuncB', (e) => e is svg.FEFuncBElement, |
| 303 svg.FEFuncBElement.supported); |
| 304 testConstructor('feFuncG', (e) => e is svg.FEFuncGElement, |
| 305 svg.FEFuncGElement.supported); |
| 306 testConstructor('feFuncR', (e) => e is svg.FEFuncRElement, |
| 307 svg.FEFuncRElement.supported); |
| 308 testConstructor('feGaussianBlur', (e) => e is svg.FEGaussianBlurElement, |
| 309 svg.FEGaussianBlurElement.supported); |
| 310 testConstructor('feImage', (e) => e is svg.FEImageElement, |
| 311 svg.FEImageElement.supported); |
| 312 testConstructor('feMerge', (e) => e is svg.FEMergeElement, |
| 313 svg.FEMergeElement.supported); |
| 314 testConstructor('feMergeNode', (e) => e is svg.FEMergeNodeElement, |
| 315 svg.FEMergeNodeElement.supported); |
| 316 testConstructor('feOffset', (e) => e is svg.FEOffsetElement, |
| 317 svg.FEOffsetElement.supported); |
| 318 testConstructor('fePointLight', (e) => e is svg.FEPointLightElement, |
| 319 svg.FEPointLightElement.supported); |
| 320 testConstructor('feSpecularLighting', |
| 321 (e) => e is svg.FESpecularLightingElement, |
| 322 svg.FESpecularLightingElement.supported); |
| 323 testConstructor('feSpotLight', (e) => e is svg.FESpotLightElement, |
| 324 svg.FESpotLightElement.supported); |
| 325 testConstructor('feTile', (e) => e is svg.FETileElement, |
| 326 svg.FETileElement.supported); |
| 327 testConstructor('feTurbulence', (e) => e is svg.FETurbulenceElement, |
| 328 svg.FETurbulenceElement.supported); |
| 329 testConstructor('filter', (e) => e is svg.FilterElement, |
| 330 svg.FilterElement.supported); |
| 331 testConstructor('foreignObject', (e) => e is svg.ForeignObjectElement, |
| 332 svg.ForeignObjectElement.supported, false); |
| 333 testConstructor('metadata', (e) => e is svg.MetadataElement); |
| 334 testConstructor('set', (e) => e is svg.SetElement, |
| 335 svg.SetElement.supported); |
| 336 }); |
| 337 |
| 338 group('outerHtml', () { |
| 339 test('outerHtml', () { |
| 340 final el = new svg.SvgSvgElement(); |
| 341 el.children.add(new svg.CircleElement()); |
| 342 el.children.add(new svg.PathElement()); |
| 343 expect([ |
| 344 '<svg version="1.1"><circle></circle><path></path></svg>', |
| 345 '<svg xmlns="http://www.w3.org/2000/svg" version="1.1">' |
| 346 '<circle /><path /></svg>', |
| 347 ].contains(el.outerHtml), true); |
| 348 }); |
| 349 }); |
| 350 |
| 351 group('innerHtml', () { |
| 352 test('get', () { |
| 353 final el = new svg.SvgSvgElement(); |
| 354 el.children.add(new svg.CircleElement()); |
| 355 el.children.add(new svg.PathElement()); |
| 356 // Allow for odd IE serialization. |
| 357 expect([ |
| 358 '<circle></circle><path></path>', |
| 359 '<circle xmlns="http://www.w3.org/2000/svg" />' |
| 360 '<path xmlns="http://www.w3.org/2000/svg" />' |
| 361 ].contains(el.innerHtml), true); |
| 362 }); |
| 363 |
| 364 test('set', () { |
| 365 final el = new svg.SvgSvgElement(); |
| 366 el.children.add(new svg.CircleElement()); |
| 367 el.children.add(new svg.PathElement()); |
| 368 el.innerHtml = '<rect></rect><a></a>'; |
| 369 expect(_nodeStrings(el.children), ["rect", "a"]); |
| 370 }); |
| 371 }); |
| 372 |
| 373 group('elementget', () { |
| 374 test('get', () { |
| 375 final el = new svg.SvgElement.svg(""" |
| 376 <svg version="1.1"> |
| 377 <circle></circle> |
| 378 <path></path> |
| 379 text |
| 380 </svg>"""); |
| 381 expect(_nodeStrings(el.children), ["circle", "path"]); |
| 382 }); |
| 383 |
| 384 test('resize', () { |
| 385 var el = new svg.SvgSvgElement(); |
| 386 var items = [new svg.CircleElement(), new svg.RectElement()]; |
| 387 el.children = items; |
| 388 expect(el.children.length, 2); |
| 389 el.children.length = 1; |
| 390 expect(el.children.length, 1); |
| 391 expect(el.children.contains(items[0]), true); |
| 392 expect(el.children.contains(items[1]), false); |
| 393 |
| 394 el.children.length = 0; |
| 395 expect(el.children.contains(items[0]), false); |
| 396 }); |
| 397 }); |
| 398 |
| 399 group('elementset', () { |
| 400 test('set', () { |
| 401 final el = new svg.SvgSvgElement(); |
| 402 el.children = [new svg.SvgElement.tag("circle"), new svg.SvgElement.tag("p
ath")]; |
| 403 expect(el.nodes.length, 2); |
| 404 expect(el.nodes[0] is svg.CircleElement, true); |
| 405 expect(el.nodes[1] is svg.PathElement, true); |
| 406 }); |
| 407 }); |
| 408 |
| 409 group('css', () { |
| 410 test('classes', () { |
| 411 var el = new svg.CircleElement(); |
| 412 var classes = el.classes; |
| 413 expect(el.classes.length, 0); |
| 414 classes.toggle('foo'); |
| 415 expect(el.classes.length, 1); |
| 416 classes.toggle('foo'); |
| 417 expect(el.classes.length, 0); |
| 418 }); |
| 419 |
| 420 test('classes-add-bad', () { |
| 421 var el = new svg.CircleElement(); |
| 422 expect(() => el.classes.add(''), throws); |
| 423 expect(() => el.classes.add('foo bar'), throws); |
| 424 }); |
| 425 test('classes-remove-bad', () { |
| 426 var el = new svg.CircleElement(); |
| 427 expect(() => el.classes.remove(''), throws); |
| 428 expect(() => el.classes.remove('foo bar'), throws); |
| 429 }); |
| 430 test('classes-toggle-token', () { |
| 431 var el = new svg.CircleElement(); |
| 432 expect(() => el.classes.toggle(''), throws); |
| 433 expect(() => el.classes.toggle('', true), throws); |
| 434 expect(() => el.classes.toggle('', false), throws); |
| 435 expect(() => el.classes.toggle('foo bar'), throws); |
| 436 expect(() => el.classes.toggle('foo bar', true), throws); |
| 437 expect(() => el.classes.toggle('foo bar', false), throws); |
| 438 }); |
| 439 test('classes-contains-bad', () { |
| 440 var el = new svg.CircleElement(); |
| 441 // Non-strings => false, strings must be valid tokens. |
| 442 expect(el.classes.contains(1), isFalse); |
| 443 expect(() => el.classes.contains(''), throws); |
| 444 expect(() => el.classes.contains('foo bar'), throws); |
| 445 }); |
| 446 }); |
| 447 |
| 448 group('getBoundingClientRect', () { |
| 449 test('is a Rectangle', () { |
| 450 var element = new svg.RectElement(); |
| 451 element.attributes['width'] = '100'; |
| 452 element.attributes['height'] = '100'; |
| 453 var root = new svg.SvgSvgElement(); |
| 454 root.append(element); |
| 455 |
| 456 document.body.append(root); |
| 457 |
| 458 var rect = element.getBoundingClientRect(); |
| 459 expect(rect is Rectangle, isTrue); |
| 460 expect(rect.width, closeTo(100, 1)); |
| 461 expect(rect.height, closeTo(100, 1)); |
| 462 }); |
| 463 }); |
| 464 |
| 465 group('PathElement', () { |
| 466 test('pathSegList', () { |
| 467 svg.PathElement path = |
| 468 new svg.SvgElement.svg('<path d="M 100 100 L 300 100 L 200 300 z"/>'); |
| 469 for (var seg in path.pathSegList) { |
| 470 expect(seg is svg.PathSeg, isTrue); |
| 471 } |
| 472 }); |
| 473 }); |
| 474 } |
OLD | NEW |