tor-browser

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

generate-object-fit-png-tests.sh (4691B)


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