blob: 2506a98127a8057aa62e515766eca29885a4e476 [file] [log] [blame]
epoger@google.com37269602013-01-19 04:21:27 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#ifndef gm_expectations_DEFINED
8#define gm_expectations_DEFINED
9
10#include "gm.h"
epoger@google.com84a18022013-02-01 20:39:15 +000011#include "SkBitmap.h"
epoger@google.com908f5832013-04-12 02:23:55 +000012#include "SkBitmapHasher.h"
epoger@google.comd271d242013-02-13 18:14:48 +000013#include "SkData.h"
epoger@google.com37269602013-01-19 04:21:27 +000014#include "SkOSFile.h"
15#include "SkRefCnt.h"
epoger@google.comd271d242013-02-13 18:14:48 +000016#include "SkStream.h"
epoger@google.com37269602013-01-19 04:21:27 +000017#include "SkTArray.h"
18
19#ifdef SK_BUILD_FOR_WIN
20 // json includes xlocale which generates warning 4530 because we're compiling without
21 // exceptions; see https://code.google.com/p/skia/issues/detail?id=1067
22 #pragma warning(push)
23 #pragma warning(disable : 4530)
24#endif
epoger@google.comd271d242013-02-13 18:14:48 +000025#include "json/reader.h"
epoger@google.com37269602013-01-19 04:21:27 +000026#include "json/value.h"
27#ifdef SK_BUILD_FOR_WIN
28 #pragma warning(pop)
29#endif
30
31namespace skiagm {
32
33 // The actual type we use to represent a checksum is hidden in here.
34 typedef Json::UInt64 Checksum;
35 static inline Json::Value asJsonValue(Checksum checksum) {
36 return checksum;
37 }
epoger@google.comd271d242013-02-13 18:14:48 +000038 static inline Checksum asChecksum(Json::Value jsonValue) {
39 return jsonValue.asUInt64();
40 }
epoger@google.com37269602013-01-19 04:21:27 +000041
epoger@google.com5efdd0c2013-03-13 14:18:40 +000042 static void gm_fprintf(FILE *stream, const char format[], ...) {
43 va_list args;
44 va_start(args, format);
45 fprintf(stream, "GM: ");
46 vfprintf(stream, format, args);
47 va_end(args);
48 }
49
epoger@google.com37269602013-01-19 04:21:27 +000050 static SkString make_filename(const char path[],
51 const char renderModeDescriptor[],
52 const char *name,
53 const char suffix[]) {
54 SkString filename(path);
55 if (filename.endsWith(SkPATH_SEPARATOR)) {
56 filename.remove(filename.size() - 1, 1);
57 }
58 filename.appendf("%c%s%s.%s", SkPATH_SEPARATOR,
59 name, renderModeDescriptor, suffix);
60 return filename;
61 }
62
epoger@google.com76c913d2013-04-26 15:06:44 +000063 Json::Value ActualResultAsJsonValue(const SkHashDigest& result);
64
65 Json::Value CreateJsonTree(Json::Value expectedResults,
66 Json::Value actualResultsFailed,
67 Json::Value actualResultsFailureIgnored,
68 Json::Value actualResultsNoComparison,
69 Json::Value actualResultsSucceeded);
70
epoger@google.com37269602013-01-19 04:21:27 +000071 /**
72 * Test expectations (allowed image checksums, etc.)
73 */
74 class Expectations {
75 public:
76 /**
77 * No expectations at all.
epoger@google.com37269602013-01-19 04:21:27 +000078 */
epoger@google.com76c913d2013-04-26 15:06:44 +000079 Expectations(bool ignoreFailure=kDefaultIgnoreFailure);
epoger@google.com37269602013-01-19 04:21:27 +000080
81 /**
epoger@google.com84a18022013-02-01 20:39:15 +000082 * Expect exactly one image (appropriate for the case when we
epoger@google.com37269602013-01-19 04:21:27 +000083 * are comparing against a single PNG file).
epoger@google.com37269602013-01-19 04:21:27 +000084 */
epoger@google.com76c913d2013-04-26 15:06:44 +000085 Expectations(const SkBitmap& bitmap, bool ignoreFailure=kDefaultIgnoreFailure);
epoger@google.com37269602013-01-19 04:21:27 +000086
87 /**
epoger@google.comd271d242013-02-13 18:14:48 +000088 * Create Expectations from a JSON element as found within the
89 * kJsonKey_ExpectedResults section.
90 *
91 * It's fine if the jsonElement is null or empty; in that case, we just
92 * don't have any expectations.
93 */
epoger@google.com76c913d2013-04-26 15:06:44 +000094 Expectations(Json::Value jsonElement);
epoger@google.comd271d242013-02-13 18:14:48 +000095
96 /**
epoger@google.com37269602013-01-19 04:21:27 +000097 * Returns true iff we want to ignore failed expectations.
98 */
99 bool ignoreFailure() const { return this->fIgnoreFailure; }
100
101 /**
102 * Returns true iff there are no allowed checksums.
103 */
epoger@google.com366a7702013-05-07 15:51:54 +0000104 bool empty() const { return this->fAllowedBitmapChecksums.empty(); }
epoger@google.com37269602013-01-19 04:21:27 +0000105
106 /**
107 * Returns true iff actualChecksum matches any allowedChecksum,
108 * regardless of fIgnoreFailure. (The caller can check
109 * that separately.)
110 */
epoger@google.com76c913d2013-04-26 15:06:44 +0000111 bool match(Checksum actualChecksum) const;
epoger@google.com37269602013-01-19 04:21:27 +0000112
113 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000114 * If this Expectation is based on a single SkBitmap, return a
115 * pointer to that SkBitmap. Otherwise (if the Expectation is
116 * empty, or if it was based on a list of checksums rather
117 * than a single bitmap), returns NULL.
118 */
119 const SkBitmap *asBitmap() const {
120 return (SkBitmap::kNo_Config == fBitmap.config()) ? NULL : &fBitmap;
121 }
122
123 /**
epoger@google.com76c913d2013-04-26 15:06:44 +0000124 * Return a JSON representation of the expectations.
epoger@google.com37269602013-01-19 04:21:27 +0000125 */
epoger@google.com76c913d2013-04-26 15:06:44 +0000126 Json::Value asJsonValue() const;
epoger@google.com37269602013-01-19 04:21:27 +0000127
128 private:
epoger@google.comd271d242013-02-13 18:14:48 +0000129 const static bool kDefaultIgnoreFailure = false;
130
epoger@google.com366a7702013-05-07 15:51:54 +0000131 SkTArray<Checksum> fAllowedBitmapChecksums;
epoger@google.com37269602013-01-19 04:21:27 +0000132 bool fIgnoreFailure;
epoger@google.com84a18022013-02-01 20:39:15 +0000133 SkBitmap fBitmap;
epoger@google.com37269602013-01-19 04:21:27 +0000134 };
135
136 /**
137 * Abstract source of Expectations objects for individual tests.
138 */
139 class ExpectationsSource : public SkRefCnt {
140 public:
141 virtual Expectations get(const char *testName) = 0;
142 };
143
144 /**
145 * Return Expectations based on individual image files on disk.
146 */
147 class IndividualImageExpectationsSource : public ExpectationsSource {
148 public:
149 /**
150 * Create an ExpectationsSource that will return Expectations based on
151 * image files found within rootDir.
152 *
153 * rootDir: directory under which to look for image files
154 * (this string will be copied to storage within this object)
epoger@google.com37269602013-01-19 04:21:27 +0000155 */
epoger@google.comb0f8b432013-04-10 18:46:25 +0000156 IndividualImageExpectationsSource(const char *rootDir) : fRootDir(rootDir) {}
epoger@google.com37269602013-01-19 04:21:27 +0000157
epoger@google.com76c913d2013-04-26 15:06:44 +0000158 Expectations get(const char *testName) SK_OVERRIDE ;
epoger@google.com37269602013-01-19 04:21:27 +0000159
160 private:
161 const SkString fRootDir;
epoger@google.com37269602013-01-19 04:21:27 +0000162 };
163
epoger@google.comd271d242013-02-13 18:14:48 +0000164 /**
165 * Return Expectations based on JSON summary file.
166 */
167 class JsonExpectationsSource : public ExpectationsSource {
168 public:
169 /**
170 * Create an ExpectationsSource that will return Expectations based on
171 * a JSON file.
172 *
173 * jsonPath: path to JSON file to read
174 */
epoger@google.com76c913d2013-04-26 15:06:44 +0000175 JsonExpectationsSource(const char *jsonPath);
epoger@google.comd271d242013-02-13 18:14:48 +0000176
epoger@google.com76c913d2013-04-26 15:06:44 +0000177 Expectations get(const char *testName) SK_OVERRIDE;
epoger@google.comd271d242013-02-13 18:14:48 +0000178
179 private:
180
181 /**
182 * Read as many bytes as possible (up to maxBytes) from the stream into
183 * an SkData object.
184 *
185 * If the returned SkData contains fewer than maxBytes, then EOF has been
186 * reached and no more data would be available from subsequent calls.
187 * (If EOF has already been reached, then this call will return an empty
188 * SkData object immediately.)
189 *
190 * If there are fewer than maxBytes bytes available to read from the
191 * stream, but the stream has not been closed yet, this call will block
192 * until there are enough bytes to read or the stream has been closed.
193 *
194 * It is up to the caller to call unref() on the returned SkData object
195 * once the data is no longer needed, so that the underlying buffer will
196 * be freed. For example:
197 *
198 * {
199 * size_t maxBytes = 256;
200 * SkAutoDataUnref dataRef(readIntoSkData(stream, maxBytes));
201 * if (NULL != dataRef.get()) {
202 * size_t bytesActuallyRead = dataRef.get()->size();
203 * // use the data...
204 * }
205 * }
206 * // underlying buffer has been freed, thanks to auto unref
207 *
208 */
209 // TODO(epoger): Move this, into SkStream.[cpp|h] as attempted in
210 // https://codereview.appspot.com/7300071 ?
211 // And maybe readFileIntoSkData() also?
epoger@google.com76c913d2013-04-26 15:06:44 +0000212 static SkData* readIntoSkData(SkStream &stream, size_t maxBytes);
epoger@google.comd271d242013-02-13 18:14:48 +0000213
214 /**
215 * Wrapper around readIntoSkData for files: reads the entire file into
216 * an SkData object.
217 */
218 static SkData* readFileIntoSkData(SkFILEStream &stream) {
219 return readIntoSkData(stream, stream.getLength());
220 }
221
222 /**
223 * Read the file contents from jsonPath and parse them into jsonRoot.
224 *
225 * Returns true if successful.
226 */
epoger@google.com76c913d2013-04-26 15:06:44 +0000227 static bool parse(const char *jsonPath, Json::Value *jsonRoot);
epoger@google.comd271d242013-02-13 18:14:48 +0000228
229 Json::Value fJsonRoot;
230 Json::Value fJsonExpectedResults;
231 };
232
epoger@google.com37269602013-01-19 04:21:27 +0000233}
234#endif