3 # Self-tests for gm, based on tools/tests/run.sh
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!
8 # To generate new baselines when gm behavior changes, run gm/tests/rebaseline.sh
10 # TODO: because this is written as a shell script (instead of, say, Python)
11 # it only runs on Linux and Mac.
12 # See https://code.google.com/p/skia/issues/detail?id=677
13 # ('make tools/tests/run.sh work cross-platform')
14 # Ideally, these tests should pass on all development platforms...
15 # otherwise, how can developers be expected to test them before committing a
18 # cd into .../trunk so all the paths will work
19 cd $(dirname $0)/../..
21 # TODO(epoger): make it look in Release and/or Debug
22 GM_BINARY=out/Debug/gm
24 OUTPUT_ACTUAL_SUBDIR=output-actual
25 OUTPUT_EXPECTED_SUBDIR=output-expected
26 CONFIGS="--config 8888 565"
28 ENCOUNTERED_ANY_ERRORS=0
30 # 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.
35 function compare_directories {
37 echo "compare_directories requires exactly 2 parameters, got $#"
40 diff -r --exclude=.* $1 $2
42 echo "failed in: compare_directories $1 $2"
43 ENCOUNTERED_ANY_ERRORS=1
47 # Run a command, and validate that it succeeds (returns 0).
48 function assert_passes {
51 echo "assert_passes $COMMAND ..."
54 echo "This command was supposed to pass, but failed: [$COMMAND]"
55 ENCOUNTERED_ANY_ERRORS=1
59 # Run a command, and validate that it fails (returns nonzero).
60 function assert_fails {
63 echo "assert_fails $COMMAND ..."
66 echo "This command was supposed to fail, but passed: [$COMMAND]"
67 ENCOUNTERED_ANY_ERRORS=1
72 # - with the arguments in $1
73 # - writing json summary into $2/$OUTPUT_ACTUAL_SUBDIR/json-summary.txt
74 # - writing return value into $2/$OUTPUT_ACTUAL_SUBDIR/return_value
75 # Then compare all of those against $2/$OUTPUT_EXPECTED_SUBDIR .
78 echo "gm_test requires exactly 2 parameters, got $#"
82 ACTUAL_OUTPUT_DIR="$2/$OUTPUT_ACTUAL_SUBDIR"
83 EXPECTED_OUTPUT_DIR="$2/$OUTPUT_EXPECTED_SUBDIR"
84 JSON_SUMMARY_FILE="$ACTUAL_OUTPUT_DIR/json-summary.txt"
86 rm -rf $ACTUAL_OUTPUT_DIR
87 mkdir -p $ACTUAL_OUTPUT_DIR
89 COMMAND="$GM_BINARY $GM_ARGS --writeJsonSummaryPath $JSON_SUMMARY_FILE --writePath $ACTUAL_OUTPUT_DIR/writePath --mismatchPath $ACTUAL_OUTPUT_DIR/mismatchPath --missingExpectationsPath $ACTUAL_OUTPUT_DIR/missingExpectationsPath"
92 echo $? >$ACTUAL_OUTPUT_DIR/return_value
94 # Replace image file contents with just the filename, for two reasons:
95 # 1. Image file encoding may vary by platform
96 # 2. https://code.google.com/p/chromium/issues/detail?id=169600
97 # ('gcl/upload.py fail to upload binary files to rietveld')
98 for IMAGEFILE in $(find $ACTUAL_OUTPUT_DIR -name \*.png); do
99 echo "[contents of $IMAGEFILE]" >$IMAGEFILE
101 for IMAGEFILE in $(find $ACTUAL_OUTPUT_DIR -name \*.pdf); do
102 echo "[contents of $IMAGEFILE]" >$IMAGEFILE
105 # Add a file to any empty subdirectories.
106 for DIR in $(find $ACTUAL_OUTPUT_DIR -mindepth 1 -type d); do
107 echo "Created additional file to make sure directory isn't empty, because self-test cannot handle empty directories." >$DIR/bogusfile
110 compare_directories $EXPECTED_OUTPUT_DIR $ACTUAL_OUTPUT_DIR
113 # Swap contents of two files at paths $1 and $2.
114 function swap_files {
116 echo "swap_files requires exactly 2 parameters, got $#"
124 # Create input dir (at path $1) with expectations (both image and json)
125 # that gm will match or mismatch as appropriate.
127 # We used to check these files into SVN, but then we needed to rebaseline them
128 # when our drawing changed at all... so, as proposed in
129 # http://code.google.com/p/skia/issues/detail?id=1068 , we generate them
131 function create_inputs_dir {
133 echo "create_inputs_dir requires exactly 1 parameter, got $#"
137 IMAGES_DIR=$INPUTS_DIR/images
138 JSON_DIR=$INPUTS_DIR/json
139 mkdir -p $IMAGES_DIR $JSON_DIR
141 THIS_IMAGE_DIR=$IMAGES_DIR/identical-bytes
142 mkdir -p $THIS_IMAGE_DIR
143 # Run GM to write out the images actually generated.
144 $GM_BINARY --hierarchy --match selftest1 $CONFIGS -w $THIS_IMAGE_DIR
145 # Run GM again to read in those images and write them out as a JSON summary.
146 $GM_BINARY --hierarchy --match selftest1 $CONFIGS -r $THIS_IMAGE_DIR \
147 --writeJsonSummaryPath $JSON_DIR/identical-bytes.json
149 THIS_IMAGE_DIR=$IMAGES_DIR/identical-pixels
150 mkdir -p $THIS_IMAGE_DIR
151 $GM_BINARY --hierarchy --match selftest1 $CONFIGS -w $THIS_IMAGE_DIR
152 echo "more bytes that do not change the image pixels" \
153 >> $THIS_IMAGE_DIR/8888/selftest1.png
154 echo "more bytes that do not change the image pixels" \
155 >> $THIS_IMAGE_DIR/565/selftest1.png
156 $GM_BINARY --hierarchy --match selftest1 $CONFIGS -r $THIS_IMAGE_DIR \
157 --writeJsonSummaryPath $JSON_DIR/identical-pixels.json
159 THIS_IMAGE_DIR=$IMAGES_DIR/different-pixels
160 mkdir -p $THIS_IMAGE_DIR
161 $GM_BINARY --hierarchy --match selftest $CONFIGS -w $THIS_IMAGE_DIR
162 swap_files $THIS_IMAGE_DIR/8888/selftest2.png $THIS_IMAGE_DIR/8888/selftest1.png
163 swap_files $THIS_IMAGE_DIR/565/selftest2.png $THIS_IMAGE_DIR/565/selftest1.png
164 $GM_BINARY --hierarchy --match selftest $CONFIGS -r $THIS_IMAGE_DIR \
165 --writeJsonSummaryPath $JSON_DIR/different-pixels.json
167 # Create another JSON expectations file which is identical to
168 # different-pixels.json, but in which the *first* ignore-failure is changed
169 # from false to true.
170 OLD='"ignore-failure" : false'
171 NEW='"ignore-failure" : true'
172 sed -e "0,/$OLD/{s/$OLD/$NEW/}" $JSON_DIR/different-pixels.json \
173 >$JSON_DIR/different-pixels-ignore-some-failures.json
175 THIS_IMAGE_DIR=$IMAGES_DIR/different-pixels-no-hierarchy
176 mkdir -p $THIS_IMAGE_DIR
177 $GM_BINARY --match selftest2 $CONFIGS -w $THIS_IMAGE_DIR
178 mv $THIS_IMAGE_DIR/selftest2_8888.png $THIS_IMAGE_DIR/selftest1_8888.png
179 mv $THIS_IMAGE_DIR/selftest2_565.png $THIS_IMAGE_DIR/selftest1_565.png
180 $GM_BINARY --match selftest1 $CONFIGS -r $THIS_IMAGE_DIR \
181 --writeJsonSummaryPath $JSON_DIR/different-pixels-no-hierarchy.json
183 mkdir -p $IMAGES_DIR/empty-dir
185 echo "# Comment line" >$GM_IGNORE_FAILURES_FILE
186 echo "" >>$GM_IGNORE_FAILURES_FILE
187 echo "# ignore any runs of the 'selftest1' test" >>$GM_IGNORE_FAILURES_FILE
188 echo "selftest1" >>$GM_IGNORE_FAILURES_FILE
189 echo "" >>$GM_IGNORE_FAILURES_FILE
190 echo "# make sure we don't do partial matches (should NOT ignore 'selftest2' runs)" >>$GM_IGNORE_FAILURES_FILE
191 echo "selftest" >>$GM_IGNORE_FAILURES_FILE
195 GM_INPUTS=$GM_TESTDIR/inputs
196 GM_OUTPUTS=$GM_TESTDIR/outputs
197 GM_TEMPFILES=$GM_TESTDIR/tempfiles
198 GM_IGNORE_FAILURES_FILE=$GM_INPUTS/ignored-tests.txt
200 create_inputs_dir $GM_INPUTS
202 # Compare generated image against an input image file with identical bytes.
203 gm_test "--verbose --hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/images/identical-bytes" "$GM_OUTPUTS/compared-against-identical-bytes-images"
204 gm_test "--verbose --hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/json/identical-bytes.json" "$GM_OUTPUTS/compared-against-identical-bytes-json"
206 # Compare generated image against an input image file with identical pixels but different PNG encoding.
207 gm_test "--verbose --hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/images/identical-pixels" "$GM_OUTPUTS/compared-against-identical-pixels-images"
208 gm_test "--verbose --hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/json/identical-pixels.json" "$GM_OUTPUTS/compared-against-identical-pixels-json"
210 # Compare generated image against an input image file with different pixels.
211 gm_test "--verbose --hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/images/different-pixels" "$GM_OUTPUTS/compared-against-different-pixels-images"
212 gm_test "--verbose --hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/json/different-pixels.json" "$GM_OUTPUTS/compared-against-different-pixels-json"
214 # Exercise --ignoreFailuresFile flag.
215 # This should run two GM tests: selftest1 and selftest2.
216 # Failures in selftest1 should be ignored, but failures in selftest2 should not.
217 gm_test "--verbose --hierarchy --match selftest --ignoreFailuresFile $GM_IGNORE_FAILURES_FILE $CONFIGS -r $GM_INPUTS/json/different-pixels.json" "$GM_OUTPUTS/ignoring-one-test"
219 # Compare different pixels, but with a SUBSET of the expectations marked as
221 gm_test "--verbose --hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/json/different-pixels-ignore-some-failures.json" "$GM_OUTPUTS/ignoring-some-failures"
223 # Compare generated image against an empty "expected image" dir.
224 # Even the tests that have been marked as ignore-failure (selftest1) should
225 # show up as no-comparison.
226 gm_test "--verbose --hierarchy --match selftest --ignoreFailuresFile $GM_IGNORE_FAILURES_FILE $CONFIGS -r $GM_INPUTS/images/empty-dir" "$GM_OUTPUTS/compared-against-empty-dir"
228 # Compare generated image against a nonexistent "expected image" dir.
229 gm_test "--verbose --hierarchy --match selftest1 $CONFIGS -r ../path/to/nowhere" "$GM_OUTPUTS/compared-against-nonexistent-dir"
231 # Compare generated image against an empty "expected image" dir, but NOT in verbose mode.
232 gm_test "--hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/images/empty-dir" "$GM_OUTPUTS/nonverbose"
234 # Add pdf to the list of configs.
235 gm_test "--verbose --hierarchy --match selftest1 $CONFIGS pdf -r $GM_INPUTS/json/identical-bytes.json" "$GM_OUTPUTS/add-config-pdf"
237 # Test what happens if run without -r (no expected-results.json to compare
239 gm_test "--verbose --hierarchy --match selftest1 $CONFIGS" "$GM_OUTPUTS/no-readpath"
241 # Test what happens if a subset of the renderModes fail (e.g. pipe)
242 gm_test "--pipe --simulatePipePlaybackFailure --verbose --hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/json/identical-pixels.json" "$GM_OUTPUTS/pipe-playback-failure"
244 # Confirm that IntentionallySkipped tests are recorded as such.
245 gm_test "--verbose --hierarchy --match selftest1 selftest2 $CONFIGS" "$GM_OUTPUTS/intentionally-skipped-tests"
247 # Ignore some error types (including ExpectationsMismatch)
248 gm_test "--ignoreErrorTypes ExpectationsMismatch NoGpuContext --verbose --hierarchy --match selftest1 $CONFIGS -r $GM_INPUTS/json/different-pixels.json" "$GM_OUTPUTS/ignore-expectations-mismatch"
250 # Test non-hierarchical mode.
251 gm_test "--verbose --match selftest1 $CONFIGS -r $GM_INPUTS/json/different-pixels-no-hierarchy.json" "$GM_OUTPUTS/no-hierarchy"
253 # Try writing out actual images using checksum-based filenames, like we do when
254 # uploading to Google Storage.
255 gm_test "--verbose --writeChecksumBasedFilenames --match selftest1 $CONFIGS -r $GM_INPUTS/json/different-pixels-no-hierarchy.json" "$GM_OUTPUTS/checksum-based-filenames"
257 # Exercise display_json_results.py
258 PASSING_CASES="compared-against-identical-bytes-json compared-against-identical-pixels-json"
259 FAILING_CASES="compared-against-different-pixels-json"
260 for CASE in $PASSING_CASES; do
261 assert_passes "python gm/display_json_results.py $GM_OUTPUTS/$CASE/$OUTPUT_EXPECTED_SUBDIR/json-summary.txt"
263 for CASE in $FAILING_CASES; do
264 assert_fails "python gm/display_json_results.py $GM_OUTPUTS/$CASE/$OUTPUT_EXPECTED_SUBDIR/json-summary.txt"
267 # Exercise all Python unittests.
268 assert_passes "python gm/test_all.py"
271 if [ $ENCOUNTERED_ANY_ERRORS == 0 ]; then
272 echo "All tests passed."
275 echo "Some tests failed."