blob: 5293ed29d3cea69711cac3b6b1ab1fae59fbd013 [file] [log] [blame]
epoger@google.coma413a532012-11-12 18:04:51 +00001#!/bin/bash
2
3# Self-tests for gm, based on tools/tests/run.sh
epoger@google.com454008a2012-11-13 20:46:50 +00004#
5# These tests are run by the Skia_PerCommit_House_Keeping bot at every commit,
6# so make sure that they still pass when you make changes to gm!
7#
commit-bot@chromium.orgc61c3c32013-03-01 15:32:34 +00008# TODO: because this is written as a shell script (instead of, say, Python)
9# it only runs on Linux and Mac.
epoger@google.com3aa33582013-01-02 15:53:25 +000010# See https://code.google.com/p/skia/issues/detail?id=677
11# ('make tools/tests/run.sh work cross-platform')
epoger@google.com454008a2012-11-13 20:46:50 +000012# Ideally, these tests should pass on all development platforms...
13# otherwise, how can developers be expected to test them before committing a
14# change?
epoger@google.coma413a532012-11-12 18:04:51 +000015
16# cd into .../trunk so all the paths will work
17cd $(dirname $0)/../..
18
19# TODO(epoger): make it look in Release and/or Debug
20GM_BINARY=out/Debug/gm
21
epoger@google.com407f8da2013-01-18 19:19:47 +000022OUTPUT_ACTUAL_SUBDIR=output-actual
23OUTPUT_EXPECTED_SUBDIR=output-expected
epoger@google.com4688de12013-01-31 15:06:36 +000024CONFIGS="--config 8888 --config 565"
epoger@google.com407f8da2013-01-18 19:19:47 +000025
epoger@google.coma413a532012-11-12 18:04:51 +000026# Compare contents of all files within directories $1 and $2,
27# EXCEPT for any dotfiles.
28# If there are any differences, a description is written to stdout and
29# we exit with a nonzero return value.
30# Otherwise, we write nothing to stdout and return.
31function compare_directories {
32 if [ $# != 2 ]; then
33 echo "compare_directories requires exactly 2 parameters, got $#"
34 exit 1
35 fi
36 diff -r --exclude=.* $1 $2
37 if [ $? != 0 ]; then
38 echo "failed in: compare_directories $1 $2"
39 exit 1
40 fi
41}
42
43# Run gm...
44# - with the arguments in $1
epoger@google.com407f8da2013-01-18 19:19:47 +000045# - writing stdout into $2/$OUTPUT_ACTUAL_SUBDIR/stdout
46# - writing json summary into $2/$OUTPUT_ACTUAL_SUBDIR/json-summary.txt
47# - writing return value into $2/$OUTPUT_ACTUAL_SUBDIR/return_value
48# Then compare all of those against $2/$OUTPUT_EXPECTED_SUBDIR .
epoger@google.coma413a532012-11-12 18:04:51 +000049function gm_test {
50 if [ $# != 2 ]; then
51 echo "gm_test requires exactly 2 parameters, got $#"
52 exit 1
53 fi
54 GM_ARGS="$1"
epoger@google.com407f8da2013-01-18 19:19:47 +000055 ACTUAL_OUTPUT_DIR="$2/$OUTPUT_ACTUAL_SUBDIR"
56 EXPECTED_OUTPUT_DIR="$2/$OUTPUT_EXPECTED_SUBDIR"
57 JSON_SUMMARY_FILE="$ACTUAL_OUTPUT_DIR/json-summary.txt"
epoger@google.coma413a532012-11-12 18:04:51 +000058
59 rm -rf $ACTUAL_OUTPUT_DIR
60 mkdir -p $ACTUAL_OUTPUT_DIR
epoger@google.com80d44782013-01-18 20:03:58 +000061 COMMAND="$GM_BINARY $GM_ARGS --writeJsonSummary $JSON_SUMMARY_FILE"
epoger@google.coma413a532012-11-12 18:04:51 +000062 echo "$COMMAND" >$ACTUAL_OUTPUT_DIR/command_line
63 $COMMAND &>$ACTUAL_OUTPUT_DIR/stdout
64 echo $? >$ACTUAL_OUTPUT_DIR/return_value
65
epoger@google.com80d44782013-01-18 20:03:58 +000066 # Only compare selected lines in the stdout, to ignore any spurious lines
epoger@google.com407f8da2013-01-18 19:19:47 +000067 # as noted in http://code.google.com/p/skia/issues/detail?id=1068 .
68 #
69 # TODO(epoger): This is still hacky... we need to rewrite this script in
70 # Python soon, and make stuff like this more maintainable.
71 grep --regexp=^reading --regexp=^writing --regexp=^drawing \
72 --regexp=^FAILED --regexp=^Ran $ACTUAL_OUTPUT_DIR/stdout \
73 >$ACTUAL_OUTPUT_DIR/stdout-tmp
epoger@google.com98204f92013-01-16 04:19:01 +000074 mv $ACTUAL_OUTPUT_DIR/stdout-tmp $ACTUAL_OUTPUT_DIR/stdout
75
epoger@google.coma413a532012-11-12 18:04:51 +000076 compare_directories $EXPECTED_OUTPUT_DIR $ACTUAL_OUTPUT_DIR
77}
78
epoger@google.come460a472013-02-06 18:41:04 +000079# Create input dir (at path $1) with expectations (both image and json)
80# that gm will match or mismatch as appropriate.
epoger@google.com407f8da2013-01-18 19:19:47 +000081#
82# We used to check these files into SVN, but then we needed to rebasline them
83# when our drawing changed at all... so, as proposed in
84# http://code.google.com/p/skia/issues/detail?id=1068 , we generate them
85# new each time.
86function create_inputs_dir {
87 if [ $# != 1 ]; then
88 echo "create_inputs_dir requires exactly 1 parameter, got $#"
89 exit 1
90 fi
91 INPUTS_DIR="$1"
epoger@google.come460a472013-02-06 18:41:04 +000092 IMAGES_DIR=$INPUTS_DIR/images
93 JSON_DIR=$INPUTS_DIR/json
94 mkdir -p $IMAGES_DIR $JSON_DIR
epoger@google.com407f8da2013-01-18 19:19:47 +000095
epoger@google.come460a472013-02-06 18:41:04 +000096 mkdir -p $IMAGES_DIR/identical-bytes
97 # Run GM to write out the images actually generated.
commit-bot@chromium.orgc61c3c32013-03-01 15:32:34 +000098 $GM_BINARY --hierarchy --match selftest1 $CONFIGS \
epoger@google.come460a472013-02-06 18:41:04 +000099 -w $IMAGES_DIR/identical-bytes
100 # Run GM again to read in those images and write them out as a JSON summary.
commit-bot@chromium.orgc61c3c32013-03-01 15:32:34 +0000101 $GM_BINARY --hierarchy --match selftest1 $CONFIGS \
epoger@google.come460a472013-02-06 18:41:04 +0000102 -r $IMAGES_DIR/identical-bytes \
103 --writeJsonSummary $JSON_DIR/identical-bytes.json
epoger@google.com407f8da2013-01-18 19:19:47 +0000104
epoger@google.come460a472013-02-06 18:41:04 +0000105 mkdir -p $IMAGES_DIR/identical-pixels
commit-bot@chromium.orgc61c3c32013-03-01 15:32:34 +0000106 $GM_BINARY --hierarchy --match selftest1 $CONFIGS \
epoger@google.come460a472013-02-06 18:41:04 +0000107 -w $IMAGES_DIR/identical-pixels
108 echo "more bytes that do not change the image pixels" \
commit-bot@chromium.orgc61c3c32013-03-01 15:32:34 +0000109 >> $IMAGES_DIR/identical-pixels/8888/selftest1.png
epoger@google.come460a472013-02-06 18:41:04 +0000110 echo "more bytes that do not change the image pixels" \
commit-bot@chromium.orgc61c3c32013-03-01 15:32:34 +0000111 >> $IMAGES_DIR/identical-pixels/565/selftest1.png
112 $GM_BINARY --hierarchy --match selftest1 $CONFIGS \
epoger@google.come460a472013-02-06 18:41:04 +0000113 -r $IMAGES_DIR/identical-pixels \
114 --writeJsonSummary $JSON_DIR/identical-pixels.json
115
116 mkdir -p $IMAGES_DIR/different-pixels
commit-bot@chromium.orgc61c3c32013-03-01 15:32:34 +0000117 $GM_BINARY --hierarchy --match selftest2 $CONFIGS \
epoger@google.come460a472013-02-06 18:41:04 +0000118 -w $IMAGES_DIR/different-pixels
commit-bot@chromium.orgc61c3c32013-03-01 15:32:34 +0000119 mv $IMAGES_DIR/different-pixels/8888/selftest2.png \
120 $IMAGES_DIR/different-pixels/8888/selftest1.png
121 mv $IMAGES_DIR/different-pixels/565/selftest2.png \
122 $IMAGES_DIR/different-pixels/565/selftest1.png
123 $GM_BINARY --hierarchy --match selftest1 $CONFIGS \
epoger@google.come460a472013-02-06 18:41:04 +0000124 -r $IMAGES_DIR/different-pixels \
125 --writeJsonSummary $JSON_DIR/different-pixels.json
epoger@google.com407f8da2013-01-18 19:19:47 +0000126
epoger@google.come460a472013-02-06 18:41:04 +0000127 mkdir -p $IMAGES_DIR/empty-dir
epoger@google.com407f8da2013-01-18 19:19:47 +0000128}
129
epoger@google.coma413a532012-11-12 18:04:51 +0000130GM_TESTDIR=gm/tests
131GM_INPUTS=$GM_TESTDIR/inputs
132GM_OUTPUTS=$GM_TESTDIR/outputs
epoger@google.com37269602013-01-19 04:21:27 +0000133GM_TEMPFILES=$GM_TESTDIR/tempfiles
epoger@google.coma413a532012-11-12 18:04:51 +0000134
epoger@google.com407f8da2013-01-18 19:19:47 +0000135create_inputs_dir $GM_INPUTS
136
epoger@google.com570aafe2012-11-28 20:08:32 +0000137# Compare generated image against an input image file with identical bytes.
commit-bot@chromium.orgc61c3c32013-03-01 15:32:34 +0000138gm_test "--hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/images/identical-bytes" "$GM_OUTPUTS/compared-against-identical-bytes-images"
139gm_test "--hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/json/identical-bytes.json" "$GM_OUTPUTS/compared-against-identical-bytes-json"
epoger@google.coma413a532012-11-12 18:04:51 +0000140
epoger@google.com570aafe2012-11-28 20:08:32 +0000141# Compare generated image against an input image file with identical pixels but different PNG encoding.
commit-bot@chromium.orgc61c3c32013-03-01 15:32:34 +0000142gm_test "--hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/images/identical-pixels" "$GM_OUTPUTS/compared-against-identical-pixels-images"
143gm_test "--hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/json/identical-pixels.json" "$GM_OUTPUTS/compared-against-identical-pixels-json"
epoger@google.com570aafe2012-11-28 20:08:32 +0000144
145# Compare generated image against an input image file with different pixels.
commit-bot@chromium.orgc61c3c32013-03-01 15:32:34 +0000146gm_test "--hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/images/different-pixels" "$GM_OUTPUTS/compared-against-different-pixels-images"
147gm_test "--hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/json/different-pixels.json" "$GM_OUTPUTS/compared-against-different-pixels-json"
epoger@google.coma413a532012-11-12 18:04:51 +0000148
epoger@google.comee8a8e32012-12-18 19:13:49 +0000149# Compare generated image against an empty "expected image" dir.
commit-bot@chromium.orgc61c3c32013-03-01 15:32:34 +0000150gm_test "--hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/images/empty-dir" "$GM_OUTPUTS/compared-against-empty-dir"
epoger@google.comee8a8e32012-12-18 19:13:49 +0000151
epoger@google.com9c56a8d2012-12-20 18:34:29 +0000152# If run without "-r", the JSON's "actual-results" section should contain
153# actual checksums marked as "failure-ignored", but the "expected-results"
154# section should be empty.
commit-bot@chromium.orgc61c3c32013-03-01 15:32:34 +0000155gm_test "--hierarchy --match selftest1 $CONFIGS" "$GM_OUTPUTS/no-readpath"
epoger@google.com9c56a8d2012-12-20 18:34:29 +0000156
epoger@google.coma413a532012-11-12 18:04:51 +0000157echo "All tests passed."