#!/bin/bash
# Automated testing of RTL layouts with reference to corresponding LTR layouts
# 
# mitcho <mitcho@mitcho.com> 2013
# dedicated to the public domain
#
# Dependencies:
# - PhantomJS (http://phantomjs.org/)
# - ImageMagick (http://imagemagick.org)
#
# WordPress plugins required for these specific tests:
# - Yoav's RTL tester: https://wordpress.org/extend/plugins/rtl-tester/
# - mitcho's test tools: https://gist.github.com/mitcho/5483140
#
# Usage:
# - make this script executable: chmod +x ltreqrtl.sh
# - set WPADMIN to be the path to your test wp-admin in your shell and run the tests:
#     WPADMIN="http://trunk.local/wp-admin" ./ltreqrtl.sh

if [ -z "$WPADMIN" ]
then
	WPADMIN="http://trunk.local/wp-admin"
fi
TMPDIR="tmp"

# ltreqrtl test_name url mouseover_query
ltreqrtl () {
	
	# lenient mode:
	threshold=1800
	fuzz=30
	
	echo -e "\033[1mTEST:\033[0m $1 ($2)"
	
 	phantomjs rasterize.js "$2\&invisibletext=1\&genericimages=1\&d=ltr" "$TMPDIR/$1-ltr.png"
 	phantomjs rasterize.js "$2\&invisibletext=1\&genericimages=1\&d=rtl" "$TMPDIR/$1-rtl.png"
	# flip the resulting rtl image to be "faux-ltr"
	convert "$TMPDIR/$1-rtl.png" -flop -extent 800x800 "$TMPDIR/$1-fauxltr.png"
	convert "$TMPDIR/$1-ltr.png" -extent 800x800 "$TMPDIR/$1-ltr.png"

	# info on compare:
	# http://www.imagemagick.org/script/compare.php
	# http://www.imagemagick.org/Usage/compare/

	# mask:
	status=$(compare -fuzz "$fuzz%" -metric AE "$TMPDIR/$1-ltr.png" "$TMPDIR/$1-fauxltr.png" "$TMPDIR/$1-diff.png" 2>&1)
	echo "  diff saved to $TMPDIR/$1-diff.png"

	if [ "$status" -le "$threshold" ]
	then
		echo -e "  \033[1;32mPASS:\033[0m $status / 640000"
	else
		echo -e "  \033[1;31mFAIL:\033[0m $status / 640000"
	fi
	
	# open $TMPDIR/admin-diff.png 

	# diff:
	# compare -fuzz 10% -metric AE -compose difference $TMPDIR/admin-ltr2.png $TMPDIR/admin-fauxltr2.png $TMPDIR/admin-diff.png
}

ltreqrtl admin "$WPADMIN/?"
ltreqrtl posts "$WPADMIN/edit.php?"
ltreqrtl new "$WPADMIN/post-new.php?"
ltreqrtl comments "$WPADMIN/edit-comments.php?"
ltreqrtl themes "$WPADMIN/themes.php?"
ltreqrtl customize "$WPADMIN/customize.php?"
ltreqrtl plugins "$WPADMIN/plugins.php?"
ltreqrtl plugin-editor "$WPADMIN/plugin-editor.php?"
ltreqrtl profile "$WPADMIN/profile.php?"
ltreqrtl about "$WPADMIN/about.php?"
