#!/bin/bash
#
# Copyright (C) 2012-2020 SUSE Software Solutions Germany GmbH
#
# Author:
# Frank Sundermeyer <fsundermeyer at opensuse dot org>
#
# Testing DAPS: Images
#
# * Are the images correctly converted:
#   -DIA: -> PNG, SVG
#   -DITAA: -> PNG
#   -JPG: -> JPG (copy)
#   -ODG: -> PNG, SVG
#   -PNG: -> PNG (copy)
#   -SVG: -> PNG, SVG (differs)
# * Is an image not included in the XML ignored?
# * Is the correct number of images generated when a ROOTID is specified?

_IMGDIR=""
_IMGDIR_SRC=""
_IMGDIR_COLOR=""
_IMGDIR_GEN=""
_IMGDIR_GRAYSCALE=""

source lib/common_functions

header "Image Generation"

function oneTimeSetUp () {
    # Clean up the build directory
    clean_build_dir all
    _IMGDIR=$($_DAPSEXEC -d $_DCFILE showvariable VARIABLE=IMG_GENDIR 2>/dev/null)
    _IMGDIR_SRC=$($_DAPSEXEC -d $_DCFILE showvariable VARIABLE=IMG_SRC_DIR 2>/dev/null)

    if [ $? -ne 0 ]; then
        exit_on_error " The initial DAPS call to determine the path to the resulting image directory failed. Skipping tests"
    fi
    _IMGDIR_COLOR=${_IMGDIR}/color
    _IMGDIR_GEN=${_IMGDIR}/gen
    _IMGDIR_GRAYSCALE=${_IMGDIR}/grayscale
}

# Post
# this function is run _after_ the tests are executed
#
function oneTimeTearDown () {
    stats
    # Clean up the build directory
    clean_build_dir all
}

#---------------------------------------------------------------
# TESTS
#---------------------------------------------------------------

#--------------------------------
# * Is the image build run successful?
#
function test_images () {
    $_DAPSEXEC -v0 -d $_DCFILE images >/dev/null 2>&1
    assertTrue \
        ' └─ The \"images\" command itself failed' \
        "$?"
    assertTrue \
        " └─ The result directory $_IMGDIR_COLOR does not exist." \
        "[ -d $_IMGDIR_COLOR ]"
    assertTrue \
        " └─ The result directory $_IMGDIR_GEN does not exist." \
        "[ -d $_IMGDIR_GEN ]"
    assertTrue \
        " └─ The result directory $_IMGDIR_GRAYSCALE does not exist." \
        "[ -d $_IMGDIR_GRAYSCALE ]"
}

#--------------------------------
# * Are the DIA images correctly converted to PNG and SVG?
#
function test_imagesDIA () {
    local _DIA_IMG

    _DIA_IMG=$(basename -s .dia $(find ${_IMGDIR_SRC}/dia -type f | head -1))

    assertTrue \
        " └─ DIA to PNG generation failed" \
        "[ -s ${_IMGDIR_COLOR}/${_DIA_IMG}.png ]"
    assertTrue \
        " └─ DIA to SVG generation failed" \
        "[ -s ${_IMGDIR_GEN}/${_DIA_IMG}.svg ]"
    assertTrue \
        " └─ Color SVG link for $_DIA_IMG is missing" \
        "[ -L ${_IMGDIR_COLOR}/${_DIA_IMG}.svg ]"
    assertTrue \
        " └─ Grayscale PNG for $_DIA_IMG is missing" \
        "[ -s ${_IMGDIR_GRAYSCALE}/${_DIA_IMG}.png ]"
    assertTrue \
        " └─ Grayscale SVG for $_DIA_IMG is missing" \
        "[ -s ${_IMGDIR_GRAYSCALE}/${_DIA_IMG}.svg ]"

}

#--------------------------------
# * Are the JPG images correctly linked and converted?
#
function test_imagesJPG () {
    local _JPG_IMG

    _JPG_IMG=$(basename $(find ${_IMGDIR_SRC}/jpg -type f | head -1))

    assertTrue \
        " └─ Link for color $_JPG_IMG is missing" \
        "[ -L ${_IMGDIR_COLOR}/${_JPG_IMG} ]"
    assertTrue \
        " └─ Grayscale JPG for $_JPG_IMG is missing" \
        "[ -s ${_IMGDIR_GRAYSCALE}/${_JPG_IMG} ]"
}

#--------------------------------
# * Are the ODG images correctly converted to PNG and SVG?
#
function test_imagesODG () {
    local _ODG_IMG

    _ODG_IMG=$(basename -s .odg $(find ${_IMGDIR_SRC}/odg -type f | head -1))

    assertTrue \
        " └─ ODG to PNG generation failed" \
        "[ -s ${_IMGDIR_COLOR}/${_ODG_IMG}.png ]"
    assertTrue \
        " └─ ODG to SVG generation failed" \
        "[ -s ${_IMGDIR_GEN}/${_ODG_IMG}.svg ]"
    assertTrue \
        " └─ Color SVG for $_ODG_IMG is missing" \
        "[ -L ${_IMGDIR_COLOR}/${_ODG_IMG}.svg ]"
    assertTrue \
        " └─ Grayscale PNG for $_ODG_IMG is missing" \
        "[ -s ${_IMGDIR_GRAYSCALE}/${_ODG_IMG}.png ]"
    assertTrue \
        " └─ Grayscale SVG for $_ODG_IMG is missing" \
        "[ -s ${_IMGDIR_GRAYSCALE}/${_ODG_IMG}.svg ]"
}

#--------------------------------
# * Are the DITAA images correctly converted to PNG?
#
#function test_imagesDITAA () {
#    local _DITAA_IMG

#    _DITAA_IMG=$(basename -s .ditaa $(find ${_IMGDIR_SRC}/ditaa -type f | head -1))

#    assertTrue \
#        " └─ DITAA to PNG generation failed" \
#        "[ -s ${_IMGDIR_GEN}/png/${_DITAA_IMG}.png ]"
#    assertTrue \
#        " └─ Color PNG link for $_DITAA_IMG is missing" \
#        "[ -L ${_IMGDIR_COLOR}/${_DITAA_IMG}.png ]"
#    assertTrue \
#        " └─ Grayscale PNG for $_DITAA_IMG is missing" \
#        "[ -s ${_IMGDIR_GRAYSCALE}/${_DITAA_IMG}.png ]"
#}


#--------------------------------
# * Are the PNG images correctly linked and converted?
#
function test_imagesPNG () {
    local _PNG_IMG

    _PNG_IMG=$(basename $(find ${_IMGDIR_SRC}/png -type f | sort | head -1))

    assertTrue \
        " └─ Link for color $_PNG_IMG is missing" \
        "[ -L ${_IMGDIR_COLOR}/${_PNG_IMG} ]"
    assertTrue \
        " └─ Grayscale PNG for $_PNG_IMG is missing" \
        "[ -s ${_IMGDIR_GRAYSCALE}/${_PNG_IMG} ]"
}


#--------------------------------
# * Are the SVG images correctly converted to PNG and SVG?
#
function test_imagesSVG () {
    local _SVG_IMG

    _SVG_IMG=$(basename -s .svg $(find ${_IMGDIR_SRC}/svg -type f | head -1))

    assertTrue \
        " └─ SVG to PNG generation failed" \
        "[ -s ${_IMGDIR_COLOR}/${_SVG_IMG}.png ]"
    assertTrue \
        " └─ SVG to SVG generation failed" \
        "[ -s ${_IMGDIR_GEN}/${_SVG_IMG}.svg ]"
    assertTrue \
        " └─ Color SVG for $_SVG_IMG is missing" \
        "[ -L ${_IMGDIR_COLOR}/${_SVG_IMG}.svg ]"
    assertTrue \
        " └─ Grayscale PNG for $_SVG_IMG is missing" \
        "[ -s ${_IMGDIR_GRAYSCALE}/${_SVG_IMG}.png ]"
    assertTrue \
        " └─ Grayscale SVG for $_SVG_IMG is missing" \
        "[ -s ${_IMGDIR_GRAYSCALE}/${_SVG_IMG}.svg ]"

}

#--------------------------------
# * Is an image not included in the XML ignored?
#

function test_imagesIGNORE () {
    local _COUNT _IMAGE_IGNORE

    _IMAGE_IGNORE=z_not_included.png

    _COUNT=$(find $_IMGDIR_COLOR $_IMGDIR_GRAYSCALE -name "$_IMAGE_IGNORE" | wc -l)
    # expected: 0

    assertTrue \
        " └─ Image $_IMAGE_IGNORE was processed although it is not included in the XML" \
        "$_COUNT"
}

#--------------------------------
# * Is the correct number of images generated when a ROOTID is specified?
#

function test_imagesROOTID () {
    local _ROOTID _IMAGE_BUILD _IMAGE_EXPECTED

    # appendix has only one image (screenshot.png)
    #
    _ROOTID=appendix
    _IMAGE_EXPECTED=png_example2.png

    clean_build_dir images

    $_DAPSEXEC -v0 -d $_DCFILE images --rootid $_ROOTID >/dev/null 2>&1
    assertTrue \
        ' └─ The \"images\" command with --rootid failed' \
        "$?"

    _IMAGE_BUILD=$(basename $(find $_IMGDIR_COLOR -type l 2>/dev/null) 2>/dev/null)
    assertEquals \
        ' └─ color/ does not include the expected file:' \
        "$_IMAGE_EXPECTED" "$_IMAGE_BUILD"

    _IMAGE_BUILD=$(basename $(find $_IMGDIR_GRAYSCALE -type f 2>/dev/null) 2>/dev/null)
    assertEquals \
        ' └─ grayscale/ does not include the expected file:' \
        "$_IMAGE_EXPECTED" "$_IMAGE_BUILD"
}

# source shUnit2 test
source $_SHUNIT2SRC
