layout/reftests/svg/smil/smil-grid.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/reftests/svg/smil/smil-grid.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,103 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 sw=2 sts=2 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/* Javascript library for dynamically generating a simple SVG/SMIL reftest
    1.11 + * with several copies of the same animation, each seeked to a different time.
    1.12 + */
    1.13 +
    1.14 +// Global variables
    1.15 +const START_TIMES = [ "4.0s",  "3.0s",  "2.7s",
    1.16 +                      "2.25s", "2.01s", "1.5s",
    1.17 +                      "1.4s",  "1.0s",  "0.5s" ];
    1.18 +
    1.19 +const X_POSNS = [ "20px",  "70px",  "120px",
    1.20 +                  "20px",  "70px",  "120px",
    1.21 +                  "20px",  "70px",  "120px" ];
    1.22 +
    1.23 +const Y_POSNS = [ "20px",  "20px",  "20px",
    1.24 +                  "70px",  "70px",  "70px",
    1.25 +                 "120px", "120px", "120px"  ];
    1.26 +
    1.27 +const DURATION = "2s";
    1.28 +const SNAPSHOT_TIME ="3";
    1.29 +const SVGNS = "http://www.w3.org/2000/svg";
    1.30 +
    1.31 +// Convenience wrapper using testAnimatedGrid to make 15pt-by-15pt rects
    1.32 +function testAnimatedRectGrid(animationTagName, animationAttrHashList) {
    1.33 +  var targetTagName = "rect";
    1.34 +  var targetAttrHash = {"width"  : "15px",
    1.35 +                        "height" : "15px" };
    1.36 +  testAnimatedGrid(targetTagName,    targetAttrHash,
    1.37 +                   animationTagName, animationAttrHashList);
    1.38 +}
    1.39 +
    1.40 +// Convenience wrapper using testAnimatedGrid to make grid of text
    1.41 +function testAnimatedTextGrid(animationTagName, animationAttrHashList) {
    1.42 +  var targetTagName = "text";
    1.43 +  var targetAttrHash = { };
    1.44 +  testAnimatedGrid(targetTagName,    targetAttrHash,
    1.45 +                   animationTagName, animationAttrHashList);
    1.46 +}
    1.47 +
    1.48 +// Generates a visual grid of elements of type "targetTagName", with the
    1.49 +// attribute values given in targetAttrHash.  Each generated element has
    1.50 +// exactly one child -- an animation element of type "animationTagName", with
    1.51 +// the attribute values given in animationAttrHash.
    1.52 +function testAnimatedGrid(targetTagName,    targetAttrHash,
    1.53 +                          animationTagName, animationAttrHashList) {
    1.54 +    // SANITY CHECK
    1.55 +  const numElementsToMake = START_TIMES.length;
    1.56 +  if (X_POSNS.length != numElementsToMake ||
    1.57 +      Y_POSNS.length != numElementsToMake) {
    1.58 +    return;
    1.59 +  }
    1.60 +  
    1.61 +  for (var i = 0; i < animationAttrHashList.length; i++) {
    1.62 +    var animationAttrHash = animationAttrHashList[i];
    1.63 +    // Default to fill="freeze" so we can test the final value of the animation
    1.64 +    if (!animationAttrHash["fill"]) {
    1.65 +      animationAttrHash["fill"] = "freeze";
    1.66 +    }
    1.67 +  }
    1.68 +
    1.69 +  // Build the grid!
    1.70 +  var svg = document.documentElement;
    1.71 +  for (var i = 0; i < numElementsToMake; i++) {
    1.72 +    // Build target & animation elements
    1.73 +    var targetElem = buildElement(targetTagName, targetAttrHash);
    1.74 +    for (var j = 0; j < animationAttrHashList.length; j++) {
    1.75 +      var animationAttrHash = animationAttrHashList[j];
    1.76 +      var animElem = buildElement(animationTagName, animationAttrHash);
    1.77 +
    1.78 +      // Customize them using global constant values
    1.79 +      targetElem.setAttribute("x", X_POSNS[i]);
    1.80 +      targetElem.setAttribute("y", Y_POSNS[i]);
    1.81 +      animElem.setAttribute("begin", START_TIMES[i]);
    1.82 +      animElem.setAttribute("dur", DURATION);
    1.83 +
    1.84 +      // Append to target
    1.85 +      targetElem.appendChild(animElem);
    1.86 +    }
    1.87 +    // Insert target into DOM
    1.88 +    svg.appendChild(targetElem);
    1.89 +  }
    1.90 +
    1.91 +  // Take snapshot
    1.92 +  setTimeAndSnapshot(SNAPSHOT_TIME, true);
    1.93 +}
    1.94 +
    1.95 +function buildElement(tagName, attrHash) {
    1.96 +  var elem = document.createElementNS(SVGNS, tagName);
    1.97 +  for (var attrName in attrHash) {
    1.98 +    var attrValue = attrHash[attrName];
    1.99 +    elem.setAttribute(attrName, attrValue);
   1.100 +  }
   1.101 +  // If we're creating a text node, populate it with some text.
   1.102 +  if (tagName == "text") {
   1.103 +    elem.appendChild(document.createTextNode("abc"));
   1.104 +  }
   1.105 +  return elem;
   1.106 +}

mercurial