#!/bin/bash

. "$XDG_TEST_DIR/include/testassertions.sh"
. "$XDG_TEST_DIR/include/testcontrol.sh"

test_file_icon() {

test_start "$FUNCNAME: ensure that associations with filetypes is correct."

ICON_SIZES="16 22 24 32 48 64 128"

test_init

# get icon files
for i in $ICON_SIZES; do
	use_file "$XDG_TEST_DIR/icons/red-$i.png" TMP
done

# get & edit mime file
use_file "$XDG_TEST_DIR/xdg-utils-usecases/data/icon_file.xml" MIME_FILE
edit_file "$MIME_FILE" 'icontest' MIME_MINOR "x-$XDG_TEST_SHORTID"
edit_file "$MIME_FILE" 'xdgtestext' EXT "f$XDG_TEST_SHORTID"

MIME_TYPE="xdgtestdata/$MIME_MINOR"
TEST_FILE="testfile.$EXT"
TEST_DIR="$XDG_TEST_TMPDIR/show"
assert_exit 0 mkdir -p "$TEST_DIR"

test_procedure

# install icons of all sizes
for i in $ICON_SIZES; do
	assert_exit 0 xdg-icon-resource install --context mimetypes --size "$i" "xdgtestdata-$XDG_TEST_ID-red-$i.png" "xdgtestdata-$MIME_MINOR"
	assert_nostdout
	assert_nostderr
done

# associate icons with mime-type
assert_exit 0 xdg-mime install "$MIME_FILE"
assert_nostdout
assert_nostderr

cp "$XDG_TEST_DIR/xdg-utils-usecases/data/testfile" "$TEST_DIR/$TEST_FILE"

# Wait a little for caches to update
sleep 1

# open directory with test file
if [ `whoami` != 'root' ] ; then
	assert_exit 0 xdg-open "$TEST_DIR"
	assert_nostdout
	assert_nostderr
	assert_interactive "Did the file browser open the '$TEST_DIR' directory?" y
else
	assert_interactive "Please open '$TEST_DIR' in the file browser."
fi

# ask user to cycle through sizes & ensure correct icons are displayed.
assert_interactive "Is a file named '$TEST_FILE' present in the directory?" y
assert_interactive "Does the icon consist of a red number in the list {$ICON_SIZES}?\n\t(You may need to select something like 'View->As Icons')" y
# assert_interactive "Does the number change appropriately when you increase or decrease the view size?" y

## cleanup
if [ `whoami` != 'root' ] ; then
	assert_interactive "Please close any windows opened by this test." C
else
	assert_interactive "You may close the file browser window." C
fi

# remove icons
for i in $ICON_SIZES; do
	assert_exit 0 xdg-icon-resource uninstall --context mimetypes --size "$i" "xdgtestdata-$MIME_MINOR"
	assert_nostdout
	assert_nostderr
done

# remove mime file
assert_exit 0 xdg-mime uninstall "$MIME_FILE"
assert_nostdout
assert_nostderr

test_result
}

run_test test_file_icon
