summaryrefslogtreecommitdiffstats
path: root/layout/reftests/w3c-css/submitted/images3/support/generate-object-fit-and-position-canvas-tests.sh
blob: b66ea4726951b70bfd9006ebef357efcd9d0f1aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
#!/bin/bash
#
# Any copyright is dedicated to the Public Domain.
# http://creativecommons.org/publicdomain/zero/1.0/
#
# Script to generate <canvas src> reftest files for "object-fit" and
# "object-position", from corresponding reftest files that use <object>.
#
# This script expects to be run from this working directory:
#  mozilla-central/layout/reftests/w3c-css/submitted/images3

# Array of image files that we'll use
imageFileArr=("support/colors-16x8.png" "support/colors-8x16.png")
canvasAttributeArr=('width="16" height="8"'   'width="8" height="16"')
numImageFiles=${#imageFileArr[@]}


for ((i = 0; i < $numImageFiles; i++)); do

  imageFile=${imageFileArr[$i]}
  canvasAttrs=${canvasAttributeArr[$i]}

  # Loop across <object> tests:
  # (We assume that tests that end with "001" use the first PNG image in
  # $imageFileArr, and tests that end with "002" use the second PNG image.)
  let testNum=$i+1
  for origTestName in object-*-png-*00${testNum}o.html; do
    # Find the corresponding reference case:
    origReferenceName=$(echo $origTestName |
                        sed "s/o.html/-ref.html/")

    # Replace "o" suffix in filename with "c" (for "canvas")
    canvasTestName=$(echo $origTestName |
                     sed "s/o.html/c.html/")

    # Generate testcase
    # (converting <object data="..."> to <canvas width="..." height="...">
    echo "Generating $canvasTestName from $origTestName."
    hg cp $origTestName $canvasTestName

    # Do string-replacements in testcase to convert it to test canvas:
    # Adjust html & body nodes:
    sed -i "s|<html>|<html class=\"reftest-wait\">|" $canvasTestName
    sed -i "s|<body>|<body onload=\"drawImageToCanvases('$imageFile')\">|" $canvasTestName
    # Adjust <title>:
    sed -i "s|object element|canvas element|g" $canvasTestName
    # Tweak the actual tags (open & close tags, and CSS rule):
    sed -i "s|object {|canvas {|" $canvasTestName
    sed -i "s|<object|<canvas|" $canvasTestName
    sed -i "s|</object>|</canvas>|" $canvasTestName
    # Drop "data" attr (pointing to image URI) and replace with
    # width/height attrs to establish the canvas's intrinsic size:
    sed -i "s|data=\"$imageFile\"|$canvasAttrs|" $canvasTestName

    # Add a <script> block to draw an image into each canvas:
    sed -i "/<\/style>/a \\
    <script>\n\
      function drawImageToCanvases(imageURI) {\n\
        var image = new Image();\n\
        image.onload = function() {\n\
          var canvasElems = document.getElementsByTagName(\"canvas\");\n\
          for (var i = 0; i < canvasElems.length; i++) {\n\
            var ctx = canvasElems[i].getContext(\"2d\");\n\
            ctx.drawImage(image, 0, 0);\n\
          }\n\
          document.documentElement.removeAttribute(\"class\");\n\
        }\n\
        image.src = imageURI;\n\
      }\n\
    <\/script>" $canvasTestName
  done
done