tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

generate-object-fit-svg-tests.sh (4989B)


      1 #!/bin/bash
      2 #
      3 # Any copyright is dedicated to the Public Domain.
      4 # http://creativecommons.org/publicdomain/zero/1.0/
      5 #
      6 # This is a script that I used to generate a suite of tests for the CSS
      7 # properties "object-fit" and "object-position", using a template testcase
      8 # file and reference case file.
      9 #
     10 # The reference case uses the "background-size" & "background-position"
     11 # equivalent of the tested "object-fit" / "object-position" values.
     12 # (One exception: since there is no "background-size" equivalent of
     13 # "object-fit: scale-down", we add an extra CSS rule for the "scale-down"
     14 # reference cases -- see REPLACEME_SCALE_DOWN_EXTRA_RULE below.)
     15 
     16 FILE_PATH="./"
     17 REFTEST_LIST_FILE="$FILE_PATH/reftest.list"
     18 
     19 TEMPLATE_TESTCASE_FILENAME=$FILE_PATH/support/template-object-fit-test.html
     20 TEMPLATE_REFERENCE_FILENAME=$FILE_PATH/support/template-object-fit-ref.html
     21 
     22 imageFileFormat="svg"
     23 
     24 # Array of image files to use for testing:
     25 imageFileArr=("support/colors-16x8.svg"
     26              "support/colors-8x16.svg"
     27              "support/colors-16x8-noSize.svg"
     28              "support/colors-8x16-noSize.svg"
     29              "support/colors-16x8-parDefault.svg"
     30              "support/colors-8x16-parDefault.svg")
     31 numImageFiles=${#imageFileArr[@]}
     32 
     33 # Array of "object-fit" values, & of corresponding "background-size" values.
     34 # (Note: background-size has no equivalent for "object-fit: scale-down". We use
     35 # "auto auto" here, which is equivalent *some* of the time; and for the cases
     36 # where it's *not* equivalent, we use an extra CSS rule in the reference case.
     37 # See REPLACEME_SCALE_DOWN_EXTRA_RULE below.)
     38 objectFitArr=(           "fill"      "contain" "cover" "none"      "scale-down" )
     39 backgroundSizeEquivArr=( "100% 100%" "contain" "cover" "auto auto" "auto auto"  )
     40 numObjectFitVals=${#objectFitArr[@]}
     41 
     42 # Array of tag-names for elements that we'd like to test:
     43 # (Also: array of a single-letter abbreviation for each element, an array of
     44 # the close tag for each element -- if a close tag is needed -- and an array
     45 # indicating the attribute that each element uses to specify its image source.)
     46 tagNameArr=(       "embed" "img" "object"    "video" )
     47 tagLetterArr=(     "e"     "i"   "o"         "p" )
     48 tagCloseTokenArr=( ""      ""    "</object>" "</video>" )
     49 tagSrcAttrArr=(        "src"   "src" "data"      "poster" )
     50 numTags=${#tagNameArr[@]}
     51 
     52 # FIRST: Add blank line & descriptive comment to reftest manifest:
     53 echo "
     54 # Tests for 'object-fit' / 'object-position' with an SVG image" \
     55 >> $REFTEST_LIST_FILE
     56 
     57 for ((i = 0; i < $numObjectFitVals; i++)); do
     58  objectFit=${objectFitArr[$i]}
     59  backgroundSizeEquiv=${backgroundSizeEquivArr[$i]}
     60 
     61  # The reference case for "scale-down" needs an additional style rule, to
     62  # look like "object-fit: scale-down" on small objects. (This is necessary
     63  # because "background-size" doesn't have a value that exactly maps to
     64  # "object-fit: scale-down".)
     65  if [[ $objectFit == "scale-down" ]]; then
     66      scaledownRule=".objectOuter > .small { background-size: contain; }"
     67      scaledownSedCmd="s/REPLACEME_SCALE_DOWN_EXTRA_RULE/$scaledownRule/"
     68  else
     69      # (We're not testing "scale-down" in this generated file, so just delete
     70      # the template's placeholder line for a "scale-down"-specific CSS rule.)
     71      scaledownSedCmd="/REPLACEME_SCALE_DOWN_EXTRA_RULE/d"
     72  fi
     73 
     74  for ((j = 0; j < $numImageFiles; j++)); do
     75    imageFile=${imageFileArr[$j]}
     76    let testNum=$j+1
     77    testNum="00$testNum" # zero-pad to 3 digits, per w3c convention
     78 
     79    filenameStub="object-fit-$objectFit-$imageFileFormat-$testNum"
     80 
     81    # Generate a reference case:
     82    filenameRef="$filenameStub-ref.html"
     83    echo Generating ${filenameRef}.
     84    cat $TEMPLATE_REFERENCE_FILENAME \
     85      | sed "s,REPLACEME_IMAGE_FILENAME,$imageFile," \
     86      | sed "s/REPLACEME_BACKGROUND_SIZE_VAL/$backgroundSizeEquiv/" \
     87      | sed "$scaledownSedCmd" \
     88      | sed "/image-rendering:/d" \
     89      > $FILE_PATH/$filenameRef;
     90 
     91    # Generate a test for each of our tags:
     92    for ((k = 0; k < $numTags; k++)); do
     93      tagName=${tagNameArr[$k]}
     94      tagLetter=${tagLetterArr[$k]}
     95      tagCloseToken=${tagCloseTokenArr[$k]}
     96      tagSrcAttr=${tagSrcAttrArr[$k]}
     97 
     98      filenameTest="$filenameStub$tagLetter.html"
     99      testTitle="'object-fit: $objectFit' on $tagName element, with a SVG image and with various 'object-position' values"
    100      echo Generating ${filenameTest}.
    101      cat $TEMPLATE_TESTCASE_FILENAME \
    102        | sed "s,REPLACEME_IMAGE_FILENAME,$imageFile," \
    103        | sed "s/REPLACEME_TEST_TITLE/$testTitle/" \
    104        | sed "s,REPLACEME_REFERENCE_FILENAME,$filenameRef," \
    105        | sed "s/REPLACEME_CONTAINER_TAG/$tagName/" \
    106        | sed "s,REPLACEME_CONTAINER_CLOSETAG,$tagCloseToken,"  \
    107        | sed "s/REPLACEME_SRC_ATTR/$tagSrcAttr/" \
    108        | sed "s/REPLACEME_OBJECT_FIT_VAL/$objectFit/" \
    109        | sed "/image-rendering:/d" \
    110        > $FILE_PATH/$filenameTest
    111 
    112      echo "== $filenameTest $filenameRef" \
    113        >> $REFTEST_LIST_FILE
    114    done
    115  done
    116 done