blob: adc0d27ba913217f08f7dd14e2f694e08d19ed84 [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#
epoger@google.com0b62b3d2013-03-20 17:59:28 +00008# To generate new baselines when gm behavior changes, run gm/tests/rebaseline.sh
9#
commit-bot@chromium.orgc61c3c32013-03-01 15:32:34 +000010# TODO: because this is written as a shell script (instead of, say, Python)
11# it only runs on Linux and Mac.
epoger@google.com3aa33582013-01-02 15:53:25 +000012# See https://code.google.com/p/skia/issues/detail?id=677
13# ('make tools/tests/run.sh work cross-platform')
epoger@google.com454008a2012-11-13 20:46:50 +000014# Ideally, these tests should pass on all development platforms...
15# otherwise, how can developers be expected to test them before committing a
16# change?
epoger@google.coma413a532012-11-12 18:04:51 +000017
18# cd into .../trunk so all the paths will work
19cd $(dirname $0)/../..
20
21# TODO(epoger): make it look in Release and/or Debug
22GM_BINARY=out/Debug/gm
23
epoger@google.com407f8da2013-01-18 19:19:47 +000024OUTPUT_ACTUAL_SUBDIR=output-actual
25OUTPUT_EXPECTED_SUBDIR=output-expected
scroggo@google.com09fd4d22013-03-20 14:20:18 +000026CONFIGS="--config 8888 565"
epoger@google.com407f8da2013-01-18 19:19:47 +000027
epoger@google.com0cc99cf2013-04-26 17:45:06 +000028ENCOUNTERED_ANY_ERRORS=0
29
epoger@google.coma413a532012-11-12 18:04:51 +000030# Compare contents of all files within directories $1 and $2,
31# EXCEPT for any dotfiles.
32# If there are any differences, a description is written to stdout and
33# we exit with a nonzero return value.
34# Otherwise, we write nothing to stdout and return.
35function compare_directories {
36 if [ $# != 2 ]; then
37 echo "compare_directories requires exactly 2 parameters, got $#"
38 exit 1
39 fi
40 diff -r --exclude=.* $1 $2
41 if [ $? != 0 ]; then
42 echo "failed in: compare_directories $1 $2"
epoger@google.com0cc99cf2013-04-26 17:45:06 +000043 ENCOUNTERED_ANY_ERRORS=1
epoger@google.coma413a532012-11-12 18:04:51 +000044 fi
45}
46
epoger@google.com26515ba2013-05-14 18:58:12 +000047# Run a command, and validate that it succeeds (returns 0).
48function assert_passes {
49 COMMAND="$1"
50 OUTPUT=$($COMMAND 2>&1)
51 if [ $? != 0 ]; then
52 echo "This command was supposed to pass, but failed: [$COMMAND]"
53 echo $OUTPUT
54 ENCOUNTERED_ANY_ERRORS=1
55 fi
56}
57
58# Run a command, and validate that it fails (returns nonzero).
59function assert_fails {
60 COMMAND="$1"
61 OUTPUT=$($COMMAND 2>&1)
62 if [ $? == 0 ]; then
63 echo "This command was supposed to fail, but passed: [$COMMAND]"
64 echo $OUTPUT
65 ENCOUNTERED_ANY_ERRORS=1
66 fi
67}
68
epoger@google.coma413a532012-11-12 18:04:51 +000069# Run gm...
70# - with the arguments in $1
epoger@google.com407f8da2013-01-18 19:19:47 +000071# - writing stdout into $2/$OUTPUT_ACTUAL_SUBDIR/stdout
72# - writing json summary into $2/$OUTPUT_ACTUAL_SUBDIR/json-summary.txt
73# - writing return value into $2/$OUTPUT_ACTUAL_SUBDIR/return_value
74# Then compare all of those against $2/$OUTPUT_EXPECTED_SUBDIR .
epoger@google.coma413a532012-11-12 18:04:51 +000075function gm_test {
76 if [ $# != 2 ]; then
77 echo "gm_test requires exactly 2 parameters, got $#"
78 exit 1
79 fi
80 GM_ARGS="$1"
epoger@google.com407f8da2013-01-18 19:19:47 +000081 ACTUAL_OUTPUT_DIR="$2/$OUTPUT_ACTUAL_SUBDIR"
82 EXPECTED_OUTPUT_DIR="$2/$OUTPUT_EXPECTED_SUBDIR"
83 JSON_SUMMARY_FILE="$ACTUAL_OUTPUT_DIR/json-summary.txt"
epoger@google.coma413a532012-11-12 18:04:51 +000084
85 rm -rf $ACTUAL_OUTPUT_DIR
86 mkdir -p $ACTUAL_OUTPUT_DIR
epoger@google.comfed9db62013-05-08 14:10:28 +000087
epoger@google.com5f995452013-06-21 18:16:47 +000088 COMMAND="$GM_BINARY $GM_ARGS --writeJsonSummaryPath $JSON_SUMMARY_FILE --writePath $ACTUAL_OUTPUT_DIR/writePath --mismatchPath $ACTUAL_OUTPUT_DIR/mismatchPath --missingExpectationsPath $ACTUAL_OUTPUT_DIR/missingExpectationsPath"
epoger@google.comfed9db62013-05-08 14:10:28 +000089
epoger@google.coma413a532012-11-12 18:04:51 +000090 echo "$COMMAND" >$ACTUAL_OUTPUT_DIR/command_line
epoger@google.com5efdd0c2013-03-13 14:18:40 +000091 $COMMAND >$ACTUAL_OUTPUT_DIR/stdout 2>$ACTUAL_OUTPUT_DIR/stderr
epoger@google.coma413a532012-11-12 18:04:51 +000092 echo $? >$ACTUAL_OUTPUT_DIR/return_value
93
epoger@google.com80d44782013-01-18 20:03:58 +000094 # Only compare selected lines in the stdout, to ignore any spurious lines
epoger@google.com407f8da2013-01-18 19:19:47 +000095 # as noted in http://code.google.com/p/skia/issues/detail?id=1068 .
96 #
97 # TODO(epoger): This is still hacky... we need to rewrite this script in
98 # Python soon, and make stuff like this more maintainable.
epoger@google.com5efdd0c2013-03-13 14:18:40 +000099 grep ^GM: $ACTUAL_OUTPUT_DIR/stdout >$ACTUAL_OUTPUT_DIR/stdout-tmp
epoger@google.com98204f92013-01-16 04:19:01 +0000100 mv $ACTUAL_OUTPUT_DIR/stdout-tmp $ACTUAL_OUTPUT_DIR/stdout
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000101 grep ^GM: $ACTUAL_OUTPUT_DIR/stderr >$ACTUAL_OUTPUT_DIR/stderr-tmp
102 mv $ACTUAL_OUTPUT_DIR/stderr-tmp $ACTUAL_OUTPUT_DIR/stderr
epoger@google.com98204f92013-01-16 04:19:01 +0000103
epoger@google.comee2c3b92013-05-09 18:09:06 +0000104 # Replace image file contents with just the filename, for two reasons:
105 # 1. Image file encoding may vary by platform
106 # 2. https://code.google.com/p/chromium/issues/detail?id=169600
107 # ('gcl/upload.py fail to upload binary files to rietveld')
epoger@google.com37d747d2013-05-21 15:41:35 +0000108 for IMAGEFILE in $(find $ACTUAL_OUTPUT_DIR -name *.png); do
epoger@google.comee2c3b92013-05-09 18:09:06 +0000109 echo "[contents of $IMAGEFILE]" >$IMAGEFILE
110 done
epoger@google.come33e1372013-07-08 19:13:33 +0000111 for IMAGEFILE in $(find $ACTUAL_OUTPUT_DIR -name *.pdf); do
112 echo "[contents of $IMAGEFILE]" >$IMAGEFILE
113 done
epoger@google.com5f995452013-06-21 18:16:47 +0000114
115 # Add a file to any empty subdirectories.
116 for DIR in $(find $ACTUAL_OUTPUT_DIR -mindepth 1 -type d); do
117 echo "Created additional file to make sure directory isn't empty, because self-test cannot handle empty directories." >$DIR/bogusfile
118 done
epoger@google.comfed9db62013-05-08 14:10:28 +0000119
epoger@google.coma413a532012-11-12 18:04:51 +0000120 compare_directories $EXPECTED_OUTPUT_DIR $ACTUAL_OUTPUT_DIR
121}
122
epoger@google.come460a472013-02-06 18:41:04 +0000123# Create input dir (at path $1) with expectations (both image and json)
124# that gm will match or mismatch as appropriate.
epoger@google.com407f8da2013-01-18 19:19:47 +0000125#
epoger@google.com37d747d2013-05-21 15:41:35 +0000126# We used to check these files into SVN, but then we needed to rebaseline them
epoger@google.com407f8da2013-01-18 19:19:47 +0000127# when our drawing changed at all... so, as proposed in
128# http://code.google.com/p/skia/issues/detail?id=1068 , we generate them
129# new each time.
130function create_inputs_dir {
131 if [ $# != 1 ]; then
132 echo "create_inputs_dir requires exactly 1 parameter, got $#"
133 exit 1
134 fi
135 INPUTS_DIR="$1"
epoger@google.come460a472013-02-06 18:41:04 +0000136 IMAGES_DIR=$INPUTS_DIR/images
137 JSON_DIR=$INPUTS_DIR/json
138 mkdir -p $IMAGES_DIR $JSON_DIR
epoger@google.com407f8da2013-01-18 19:19:47 +0000139
epoger@google.com37d747d2013-05-21 15:41:35 +0000140 THIS_IMAGE_DIR=$IMAGES_DIR/identical-bytes
141 mkdir -p $THIS_IMAGE_DIR
epoger@google.come460a472013-02-06 18:41:04 +0000142 # Run GM to write out the images actually generated.
epoger@google.com37d747d2013-05-21 15:41:35 +0000143 $GM_BINARY --hierarchy --match selftest1 $CONFIGS -w $THIS_IMAGE_DIR
epoger@google.come460a472013-02-06 18:41:04 +0000144 # Run GM again to read in those images and write them out as a JSON summary.
epoger@google.com37d747d2013-05-21 15:41:35 +0000145 $GM_BINARY --hierarchy --match selftest1 $CONFIGS -r $THIS_IMAGE_DIR \
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000146 --writeJsonSummaryPath $JSON_DIR/identical-bytes.json
epoger@google.com407f8da2013-01-18 19:19:47 +0000147
epoger@google.com37d747d2013-05-21 15:41:35 +0000148 THIS_IMAGE_DIR=$IMAGES_DIR/identical-pixels
149 mkdir -p $THIS_IMAGE_DIR
150 $GM_BINARY --hierarchy --match selftest1 $CONFIGS -w $THIS_IMAGE_DIR
epoger@google.come460a472013-02-06 18:41:04 +0000151 echo "more bytes that do not change the image pixels" \
epoger@google.com37d747d2013-05-21 15:41:35 +0000152 >> $THIS_IMAGE_DIR/8888/selftest1.png
epoger@google.come460a472013-02-06 18:41:04 +0000153 echo "more bytes that do not change the image pixels" \
epoger@google.com37d747d2013-05-21 15:41:35 +0000154 >> $THIS_IMAGE_DIR/565/selftest1.png
155 $GM_BINARY --hierarchy --match selftest1 $CONFIGS -r $THIS_IMAGE_DIR \
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000156 --writeJsonSummaryPath $JSON_DIR/identical-pixels.json
epoger@google.come460a472013-02-06 18:41:04 +0000157
epoger@google.com37d747d2013-05-21 15:41:35 +0000158 THIS_IMAGE_DIR=$IMAGES_DIR/different-pixels
159 mkdir -p $THIS_IMAGE_DIR
160 $GM_BINARY --hierarchy --match selftest2 $CONFIGS -w $THIS_IMAGE_DIR
161 mv $THIS_IMAGE_DIR/8888/selftest2.png $THIS_IMAGE_DIR/8888/selftest1.png
162 mv $THIS_IMAGE_DIR/565/selftest2.png $THIS_IMAGE_DIR/565/selftest1.png
163 $GM_BINARY --hierarchy --match selftest1 $CONFIGS -r $THIS_IMAGE_DIR \
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000164 --writeJsonSummaryPath $JSON_DIR/different-pixels.json
epoger@google.com407f8da2013-01-18 19:19:47 +0000165
epoger@google.com5852c8b2013-08-30 20:19:25 +0000166 # Create another JSON expectations file which is identical to
167 # different-pixels.json, but in which the *first* ignore-failure is changed
168 # from false to true.
169 OLD='"ignore-failure" : false'
170 NEW='"ignore-failure" : true'
171 sed -e "0,/$OLD/{s/$OLD/$NEW/}" $JSON_DIR/different-pixels.json \
172 >$JSON_DIR/different-pixels-ignore-some-failures.json
173
epoger@google.com37d747d2013-05-21 15:41:35 +0000174 THIS_IMAGE_DIR=$IMAGES_DIR/different-pixels-no-hierarchy
175 mkdir -p $THIS_IMAGE_DIR
176 $GM_BINARY --match selftest2 $CONFIGS -w $THIS_IMAGE_DIR
177 mv $THIS_IMAGE_DIR/selftest2_8888.png $THIS_IMAGE_DIR/selftest1_8888.png
178 mv $THIS_IMAGE_DIR/selftest2_565.png $THIS_IMAGE_DIR/selftest1_565.png
179 $GM_BINARY --match selftest1 $CONFIGS -r $THIS_IMAGE_DIR \
180 --writeJsonSummaryPath $JSON_DIR/different-pixels-no-hierarchy.json
181
epoger@google.come460a472013-02-06 18:41:04 +0000182 mkdir -p $IMAGES_DIR/empty-dir
epoger@google.com407f8da2013-01-18 19:19:47 +0000183}
184
epoger@google.coma413a532012-11-12 18:04:51 +0000185GM_TESTDIR=gm/tests
186GM_INPUTS=$GM_TESTDIR/inputs
187GM_OUTPUTS=$GM_TESTDIR/outputs
epoger@google.com37269602013-01-19 04:21:27 +0000188GM_TEMPFILES=$GM_TESTDIR/tempfiles
epoger@google.coma413a532012-11-12 18:04:51 +0000189
epoger@google.com407f8da2013-01-18 19:19:47 +0000190create_inputs_dir $GM_INPUTS
191
epoger@google.com570aafe2012-11-28 20:08:32 +0000192# Compare generated image against an input image file with identical bytes.
epoger@google.com51dbabe2013-04-10 15:24:53 +0000193gm_test "--verbose --hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/images/identical-bytes" "$GM_OUTPUTS/compared-against-identical-bytes-images"
194gm_test "--verbose --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 +0000195
epoger@google.com570aafe2012-11-28 20:08:32 +0000196# Compare generated image against an input image file with identical pixels but different PNG encoding.
epoger@google.com51dbabe2013-04-10 15:24:53 +0000197gm_test "--verbose --hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/images/identical-pixels" "$GM_OUTPUTS/compared-against-identical-pixels-images"
198gm_test "--verbose --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 +0000199
200# Compare generated image against an input image file with different pixels.
epoger@google.com51dbabe2013-04-10 15:24:53 +0000201gm_test "--verbose --hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/images/different-pixels" "$GM_OUTPUTS/compared-against-different-pixels-images"
202gm_test "--verbose --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 +0000203
epoger@google.com5852c8b2013-08-30 20:19:25 +0000204# Compare different pixels, but with a SUBSET of the expectations marked as
205# ignore-failure.
206gm_test "--verbose --hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/json/different-pixels-ignore-some-failures.json" "$GM_OUTPUTS/ignoring-some-failures"
207
epoger@google.comee8a8e32012-12-18 19:13:49 +0000208# Compare generated image against an empty "expected image" dir.
epoger@google.com51dbabe2013-04-10 15:24:53 +0000209gm_test "--verbose --hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/images/empty-dir" "$GM_OUTPUTS/compared-against-empty-dir"
210
epoger@google.com318a0592013-04-12 19:05:57 +0000211# Compare generated image against a nonexistent "expected image" dir.
212gm_test "--verbose --hierarchy --match selftest1 $CONFIGS -r ../path/to/nowhere" "$GM_OUTPUTS/compared-against-nonexistent-dir"
213
epoger@google.com51dbabe2013-04-10 15:24:53 +0000214# Compare generated image against an empty "expected image" dir, but NOT in verbose mode.
215gm_test "--hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/images/empty-dir" "$GM_OUTPUTS/nonverbose"
epoger@google.comee8a8e32012-12-18 19:13:49 +0000216
epoger@google.come33e1372013-07-08 19:13:33 +0000217# Add pdf to the list of configs.
218gm_test "--verbose --hierarchy --match selftest1 $CONFIGS pdf -r $GM_INPUTS/json/identical-bytes.json" "$GM_OUTPUTS/add-config-pdf"
219
epoger@google.com5852c8b2013-08-30 20:19:25 +0000220# Test what happens if run without -r (no expected-results.json to compare
221# against).
epoger@google.com51dbabe2013-04-10 15:24:53 +0000222gm_test "--verbose --hierarchy --match selftest1 $CONFIGS" "$GM_OUTPUTS/no-readpath"
epoger@google.com9c56a8d2012-12-20 18:34:29 +0000223
epoger@google.comcaac3db2013-04-04 19:23:11 +0000224# Test what happens if a subset of the renderModes fail (e.g. pipe)
epoger@google.com51dbabe2013-04-10 15:24:53 +0000225gm_test "--simulatePipePlaybackFailure --verbose --hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/json/identical-pixels.json" "$GM_OUTPUTS/pipe-playback-failure"
epoger@google.comcaac3db2013-04-04 19:23:11 +0000226
epoger@google.comc8263e72013-04-10 12:17:34 +0000227# Confirm that IntentionallySkipped tests are recorded as such.
epoger@google.com51dbabe2013-04-10 15:24:53 +0000228gm_test "--verbose --hierarchy --match selftest1 selftest2 $CONFIGS" "$GM_OUTPUTS/intentionally-skipped-tests"
epoger@google.comc8263e72013-04-10 12:17:34 +0000229
epoger@google.com5079d2c2013-04-12 14:11:21 +0000230# Ignore some error types (including ExpectationsMismatch)
231gm_test "--ignoreErrorTypes ExpectationsMismatch NoGpuContext --verbose --hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/json/different-pixels.json" "$GM_OUTPUTS/ignore-expectations-mismatch"
232
epoger@google.com37d747d2013-05-21 15:41:35 +0000233# Test non-hierarchical mode.
234gm_test "--verbose --match selftest1 $CONFIGS -r $GM_INPUTS/json/different-pixels-no-hierarchy.json" "$GM_OUTPUTS/no-hierarchy"
235
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000236# Try writing out actual images using checksum-based filenames, like we do when
237# uploading to Google Storage.
238gm_test "--verbose --writeChecksumBasedFilenames --match selftest1 $CONFIGS -r $GM_INPUTS/json/different-pixels-no-hierarchy.json" "$GM_OUTPUTS/checksum-based-filenames"
239
epoger@google.coma55e48d2013-05-21 16:06:40 +0000240# Exercise display_json_results.py
epoger@google.com26515ba2013-05-14 18:58:12 +0000241PASSING_CASES="compared-against-identical-bytes-json compared-against-identical-pixels-json"
242FAILING_CASES="compared-against-different-pixels-json"
243for CASE in $PASSING_CASES; do
epoger@google.coma55e48d2013-05-21 16:06:40 +0000244 assert_passes "python gm/display_json_results.py $GM_OUTPUTS/$CASE/$OUTPUT_EXPECTED_SUBDIR/json-summary.txt"
epoger@google.com26515ba2013-05-14 18:58:12 +0000245done
246for CASE in $FAILING_CASES; do
epoger@google.coma55e48d2013-05-21 16:06:40 +0000247 assert_fails "python gm/display_json_results.py $GM_OUTPUTS/$CASE/$OUTPUT_EXPECTED_SUBDIR/json-summary.txt"
epoger@google.com26515ba2013-05-14 18:58:12 +0000248done
249
epoger@google.com0cc99cf2013-04-26 17:45:06 +0000250if [ $ENCOUNTERED_ANY_ERRORS == 0 ]; then
251 echo "All tests passed."
252 exit 0
253else
254 exit 1
255fi