blob: de8958975b0c87a2b9e0f4f3ee494b27a10546be [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
scroggo@google.com6843bdb2013-05-08 19:14:23 +000033 void gm_fprintf(FILE *stream, const char format[], ...);
epoger@google.com5efdd0c2013-03-13 14:18:40 +000034
epoger@google.com76c913d2013-04-26 15:06:44 +000035 Json::Value CreateJsonTree(Json::Value expectedResults,
36 Json::Value actualResultsFailed,
37 Json::Value actualResultsFailureIgnored,
38 Json::Value actualResultsNoComparison,
39 Json::Value actualResultsSucceeded);
40
epoger@google.com37269602013-01-19 04:21:27 +000041 /**
epoger@google.comd4993ff2013-05-24 14:33:28 +000042 * The digest of a GM test result.
43 *
44 * Currently, this is always a uint64_t hash digest of an SkBitmap...
45 * but we will add other flavors soon.
46 */
47 class GmResultDigest {
48 public:
49 /**
50 * Create a ResultDigest representing an actual image result.
51 */
52 GmResultDigest(const SkBitmap &bitmap);
53
54 /**
55 * Create a ResultDigest representing an allowed result
56 * checksum within JSON expectations file, in the form
57 * ["bitmap-64bitMD5", 12345].
58 */
59 GmResultDigest(const Json::Value &jsonTypeValuePair);
60
61 /**
62 * Returns true if this GmResultDigest was fully and successfully
63 * created.
64 */
65 bool isValid() const;
66
67 /**
68 * Returns true if this and other GmResultDigest could
69 * represent identical results.
70 */
71 bool equals(const GmResultDigest &other) const;
72
73 /**
74 * Returns a JSON type/value pair representing this result,
75 * such as ["bitmap-64bitMD5", 12345].
76 */
77 Json::Value asJsonTypeValuePair() const;
78
epoger@google.com6f7f14d2013-06-19 18:28:31 +000079 /**
80 * Returns the hashtype, such as "bitmap-64bitMD5", as an SkString.
81 */
82 SkString getHashType() const;
83
84 /**
85 * Returns the hash digest value, such as "12345", as an SkString.
86 */
87 SkString getDigestValue() const;
88
epoger@google.comd4993ff2013-05-24 14:33:28 +000089 private:
90 bool fIsValid; // always check this first--if it's false, other fields are meaningless
91 uint64_t fHashDigest;
92 };
93
94 /**
epoger@google.com6f7f14d2013-06-19 18:28:31 +000095 * Encapsulates an SkBitmap and its GmResultDigest, guaranteed to keep them in sync.
96 */
97 class BitmapAndDigest {
98 public:
99 BitmapAndDigest(const SkBitmap &bitmap) : fBitmap(bitmap), fDigest(bitmap) {}
100
101 const SkBitmap fBitmap;
102 const GmResultDigest fDigest;
103 };
104
105 /**
epoger@google.comd4993ff2013-05-24 14:33:28 +0000106 * Test expectations (allowed image results, etc.)
epoger@google.com37269602013-01-19 04:21:27 +0000107 */
108 class Expectations {
109 public:
110 /**
111 * No expectations at all.
epoger@google.com37269602013-01-19 04:21:27 +0000112 */
epoger@google.com76c913d2013-04-26 15:06:44 +0000113 Expectations(bool ignoreFailure=kDefaultIgnoreFailure);
epoger@google.com37269602013-01-19 04:21:27 +0000114
115 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000116 * Expect exactly one image (appropriate for the case when we
epoger@google.com37269602013-01-19 04:21:27 +0000117 * are comparing against a single PNG file).
epoger@google.com37269602013-01-19 04:21:27 +0000118 */
epoger@google.com76c913d2013-04-26 15:06:44 +0000119 Expectations(const SkBitmap& bitmap, bool ignoreFailure=kDefaultIgnoreFailure);
epoger@google.com37269602013-01-19 04:21:27 +0000120
121 /**
epoger@google.comd271d242013-02-13 18:14:48 +0000122 * Create Expectations from a JSON element as found within the
123 * kJsonKey_ExpectedResults section.
124 *
125 * It's fine if the jsonElement is null or empty; in that case, we just
126 * don't have any expectations.
127 */
epoger@google.com76c913d2013-04-26 15:06:44 +0000128 Expectations(Json::Value jsonElement);
epoger@google.comd271d242013-02-13 18:14:48 +0000129
130 /**
epoger@google.com37269602013-01-19 04:21:27 +0000131 * Returns true iff we want to ignore failed expectations.
132 */
133 bool ignoreFailure() const { return this->fIgnoreFailure; }
134
135 /**
epoger@google.comdefc4872013-09-19 06:18:27 +0000136 * Override default setting of fIgnoreFailure.
137 */
138 void setIgnoreFailure(bool val) { this->fIgnoreFailure = val; }
139
140 /**
epoger@google.comd4993ff2013-05-24 14:33:28 +0000141 * Returns true iff there are no allowed results.
epoger@google.com37269602013-01-19 04:21:27 +0000142 */
epoger@google.comd4993ff2013-05-24 14:33:28 +0000143 bool empty() const { return this->fAllowedResultDigests.empty(); }
epoger@google.com37269602013-01-19 04:21:27 +0000144
145 /**
epoger@google.comd4993ff2013-05-24 14:33:28 +0000146 * Returns true iff resultDigest matches any allowed result,
epoger@google.com37269602013-01-19 04:21:27 +0000147 * regardless of fIgnoreFailure. (The caller can check
148 * that separately.)
149 */
epoger@google.comd4993ff2013-05-24 14:33:28 +0000150 bool match(GmResultDigest resultDigest) const;
epoger@google.com37269602013-01-19 04:21:27 +0000151
152 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000153 * If this Expectation is based on a single SkBitmap, return a
154 * pointer to that SkBitmap. Otherwise (if the Expectation is
155 * empty, or if it was based on a list of checksums rather
156 * than a single bitmap), returns NULL.
157 */
158 const SkBitmap *asBitmap() const {
159 return (SkBitmap::kNo_Config == fBitmap.config()) ? NULL : &fBitmap;
160 }
161
162 /**
epoger@google.com76c913d2013-04-26 15:06:44 +0000163 * Return a JSON representation of the expectations.
epoger@google.com37269602013-01-19 04:21:27 +0000164 */
epoger@google.com76c913d2013-04-26 15:06:44 +0000165 Json::Value asJsonValue() const;
epoger@google.com37269602013-01-19 04:21:27 +0000166
167 private:
epoger@google.comd271d242013-02-13 18:14:48 +0000168 const static bool kDefaultIgnoreFailure = false;
169
epoger@google.comd4993ff2013-05-24 14:33:28 +0000170 SkTArray<GmResultDigest> fAllowedResultDigests;
epoger@google.com37269602013-01-19 04:21:27 +0000171 bool fIgnoreFailure;
epoger@google.com84a18022013-02-01 20:39:15 +0000172 SkBitmap fBitmap;
epoger@google.com37269602013-01-19 04:21:27 +0000173 };
174
175 /**
176 * Abstract source of Expectations objects for individual tests.
177 */
178 class ExpectationsSource : public SkRefCnt {
179 public:
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000180 SK_DECLARE_INST_COUNT(ExpectationsSource)
181
epoger@google.com37269602013-01-19 04:21:27 +0000182 virtual Expectations get(const char *testName) = 0;
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000183
184 private:
185 typedef SkRefCnt INHERITED;
epoger@google.com37269602013-01-19 04:21:27 +0000186 };
187
188 /**
189 * Return Expectations based on individual image files on disk.
190 */
191 class IndividualImageExpectationsSource : public ExpectationsSource {
192 public:
193 /**
194 * Create an ExpectationsSource that will return Expectations based on
195 * image files found within rootDir.
196 *
197 * rootDir: directory under which to look for image files
198 * (this string will be copied to storage within this object)
epoger@google.com37269602013-01-19 04:21:27 +0000199 */
epoger@google.comb0f8b432013-04-10 18:46:25 +0000200 IndividualImageExpectationsSource(const char *rootDir) : fRootDir(rootDir) {}
epoger@google.com37269602013-01-19 04:21:27 +0000201
epoger@google.com76c913d2013-04-26 15:06:44 +0000202 Expectations get(const char *testName) SK_OVERRIDE ;
epoger@google.com37269602013-01-19 04:21:27 +0000203
204 private:
205 const SkString fRootDir;
epoger@google.com37269602013-01-19 04:21:27 +0000206 };
207
epoger@google.comd271d242013-02-13 18:14:48 +0000208 /**
209 * Return Expectations based on JSON summary file.
210 */
211 class JsonExpectationsSource : public ExpectationsSource {
212 public:
213 /**
214 * Create an ExpectationsSource that will return Expectations based on
215 * a JSON file.
216 *
217 * jsonPath: path to JSON file to read
218 */
epoger@google.com76c913d2013-04-26 15:06:44 +0000219 JsonExpectationsSource(const char *jsonPath);
epoger@google.comd271d242013-02-13 18:14:48 +0000220
epoger@google.com76c913d2013-04-26 15:06:44 +0000221 Expectations get(const char *testName) SK_OVERRIDE;
epoger@google.comd271d242013-02-13 18:14:48 +0000222
223 private:
224
225 /**
226 * Read as many bytes as possible (up to maxBytes) from the stream into
227 * an SkData object.
228 *
229 * If the returned SkData contains fewer than maxBytes, then EOF has been
230 * reached and no more data would be available from subsequent calls.
231 * (If EOF has already been reached, then this call will return an empty
232 * SkData object immediately.)
233 *
234 * If there are fewer than maxBytes bytes available to read from the
235 * stream, but the stream has not been closed yet, this call will block
236 * until there are enough bytes to read or the stream has been closed.
237 *
238 * It is up to the caller to call unref() on the returned SkData object
239 * once the data is no longer needed, so that the underlying buffer will
240 * be freed. For example:
241 *
242 * {
243 * size_t maxBytes = 256;
244 * SkAutoDataUnref dataRef(readIntoSkData(stream, maxBytes));
245 * if (NULL != dataRef.get()) {
246 * size_t bytesActuallyRead = dataRef.get()->size();
247 * // use the data...
248 * }
249 * }
250 * // underlying buffer has been freed, thanks to auto unref
251 *
252 */
253 // TODO(epoger): Move this, into SkStream.[cpp|h] as attempted in
254 // https://codereview.appspot.com/7300071 ?
scroggo@google.com6843bdb2013-05-08 19:14:23 +0000255 // And maybe ReadFileIntoSkData() also?
256 static SkData* ReadIntoSkData(SkStream &stream, size_t maxBytes);
epoger@google.comd271d242013-02-13 18:14:48 +0000257
258 /**
scroggo@google.com6843bdb2013-05-08 19:14:23 +0000259 * Wrapper around ReadIntoSkData for files: reads the entire file into
epoger@google.comd271d242013-02-13 18:14:48 +0000260 * an SkData object.
261 */
scroggo@google.com6843bdb2013-05-08 19:14:23 +0000262 static SkData* ReadFileIntoSkData(SkFILEStream &stream) {
263 return ReadIntoSkData(stream, stream.getLength());
epoger@google.comd271d242013-02-13 18:14:48 +0000264 }
265
266 /**
267 * Read the file contents from jsonPath and parse them into jsonRoot.
268 *
269 * Returns true if successful.
270 */
scroggo@google.com6843bdb2013-05-08 19:14:23 +0000271 static bool Parse(const char *jsonPath, Json::Value *jsonRoot);
epoger@google.comd271d242013-02-13 18:14:48 +0000272
273 Json::Value fJsonRoot;
274 Json::Value fJsonExpectedResults;
275 };
276
epoger@google.com37269602013-01-19 04:21:27 +0000277}
278#endif