blob: d454732dad3997b1be09faaa846857b18ba3e682 [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"
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000014#include "SkJSONCPP.h"
epoger@google.com37269602013-01-19 04:21:27 +000015#include "SkOSFile.h"
16#include "SkRefCnt.h"
epoger@google.comd271d242013-02-13 18:14:48 +000017#include "SkStream.h"
epoger@google.com37269602013-01-19 04:21:27 +000018#include "SkTArray.h"
19
epoger@google.com37269602013-01-19 04:21:27 +000020
21namespace skiagm {
22
epoger@google.com76c913d2013-04-26 15:06:44 +000023 Json::Value CreateJsonTree(Json::Value expectedResults,
24 Json::Value actualResultsFailed,
25 Json::Value actualResultsFailureIgnored,
26 Json::Value actualResultsNoComparison,
27 Json::Value actualResultsSucceeded);
28
epoger@google.com37269602013-01-19 04:21:27 +000029 /**
epoger@google.comd4993ff2013-05-24 14:33:28 +000030 * The digest of a GM test result.
31 *
32 * Currently, this is always a uint64_t hash digest of an SkBitmap...
33 * but we will add other flavors soon.
34 */
35 class GmResultDigest {
36 public:
37 /**
38 * Create a ResultDigest representing an actual image result.
39 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +000040 explicit GmResultDigest(const SkBitmap &bitmap);
epoger@google.comd4993ff2013-05-24 14:33:28 +000041
42 /**
43 * Create a ResultDigest representing an allowed result
44 * checksum within JSON expectations file, in the form
45 * ["bitmap-64bitMD5", 12345].
46 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +000047 explicit GmResultDigest(const Json::Value &jsonTypeValuePair);
epoger@google.comd4993ff2013-05-24 14:33:28 +000048
49 /**
50 * Returns true if this GmResultDigest was fully and successfully
51 * created.
52 */
53 bool isValid() const;
54
55 /**
56 * Returns true if this and other GmResultDigest could
57 * represent identical results.
58 */
59 bool equals(const GmResultDigest &other) const;
60
61 /**
62 * Returns a JSON type/value pair representing this result,
63 * such as ["bitmap-64bitMD5", 12345].
64 */
65 Json::Value asJsonTypeValuePair() const;
66
epoger@google.com6f7f14d2013-06-19 18:28:31 +000067 /**
68 * Returns the hashtype, such as "bitmap-64bitMD5", as an SkString.
69 */
70 SkString getHashType() const;
71
72 /**
73 * Returns the hash digest value, such as "12345", as an SkString.
74 */
75 SkString getDigestValue() const;
76
epoger@google.comd4993ff2013-05-24 14:33:28 +000077 private:
78 bool fIsValid; // always check this first--if it's false, other fields are meaningless
79 uint64_t fHashDigest;
80 };
81
82 /**
epoger@google.com6f7f14d2013-06-19 18:28:31 +000083 * Encapsulates an SkBitmap and its GmResultDigest, guaranteed to keep them in sync.
84 */
85 class BitmapAndDigest {
86 public:
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +000087 explicit BitmapAndDigest(const SkBitmap &bitmap) : fBitmap(bitmap), fDigest(bitmap) {}
epoger@google.com6f7f14d2013-06-19 18:28:31 +000088
89 const SkBitmap fBitmap;
90 const GmResultDigest fDigest;
91 };
92
93 /**
epoger@google.comd4993ff2013-05-24 14:33:28 +000094 * Test expectations (allowed image results, etc.)
epoger@google.com37269602013-01-19 04:21:27 +000095 */
96 class Expectations {
97 public:
98 /**
99 * No expectations at all.
epoger@google.com37269602013-01-19 04:21:27 +0000100 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000101 explicit Expectations(bool ignoreFailure=kDefaultIgnoreFailure);
epoger@google.com37269602013-01-19 04:21:27 +0000102
103 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000104 * Expect exactly one image (appropriate for the case when we
epoger@google.com37269602013-01-19 04:21:27 +0000105 * are comparing against a single PNG file).
epoger@google.com37269602013-01-19 04:21:27 +0000106 */
scroggo@google.com5187c432013-10-22 00:42:46 +0000107 explicit Expectations(const SkBitmap& bitmap, bool ignoreFailure=kDefaultIgnoreFailure);
108
109 /**
110 * Expect exactly one image, whose digest has already been computed.
111 */
112 explicit Expectations(const BitmapAndDigest& bitmapAndDigest);
epoger@google.com37269602013-01-19 04:21:27 +0000113
114 /**
epoger@google.comd271d242013-02-13 18:14:48 +0000115 * Create Expectations from a JSON element as found within the
116 * kJsonKey_ExpectedResults section.
117 *
118 * It's fine if the jsonElement is null or empty; in that case, we just
119 * don't have any expectations.
120 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000121 explicit Expectations(Json::Value jsonElement);
epoger@google.comd271d242013-02-13 18:14:48 +0000122
123 /**
epoger@google.com37269602013-01-19 04:21:27 +0000124 * Returns true iff we want to ignore failed expectations.
125 */
126 bool ignoreFailure() const { return this->fIgnoreFailure; }
127
128 /**
epoger@google.comdefc4872013-09-19 06:18:27 +0000129 * Override default setting of fIgnoreFailure.
130 */
131 void setIgnoreFailure(bool val) { this->fIgnoreFailure = val; }
132
133 /**
epoger@google.comd4993ff2013-05-24 14:33:28 +0000134 * Returns true iff there are no allowed results.
epoger@google.com37269602013-01-19 04:21:27 +0000135 */
epoger@google.comd4993ff2013-05-24 14:33:28 +0000136 bool empty() const { return this->fAllowedResultDigests.empty(); }
epoger@google.com37269602013-01-19 04:21:27 +0000137
138 /**
epoger@google.comd4993ff2013-05-24 14:33:28 +0000139 * Returns true iff resultDigest matches any allowed result,
epoger@google.com37269602013-01-19 04:21:27 +0000140 * regardless of fIgnoreFailure. (The caller can check
141 * that separately.)
142 */
epoger@google.comd4993ff2013-05-24 14:33:28 +0000143 bool match(GmResultDigest resultDigest) const;
epoger@google.com37269602013-01-19 04:21:27 +0000144
145 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000146 * If this Expectation is based on a single SkBitmap, return a
147 * pointer to that SkBitmap. Otherwise (if the Expectation is
148 * empty, or if it was based on a list of checksums rather
149 * than a single bitmap), returns NULL.
150 */
151 const SkBitmap *asBitmap() const {
152 return (SkBitmap::kNo_Config == fBitmap.config()) ? NULL : &fBitmap;
153 }
154
155 /**
epoger@google.com76c913d2013-04-26 15:06:44 +0000156 * Return a JSON representation of the expectations.
epoger@google.com37269602013-01-19 04:21:27 +0000157 */
epoger@google.com76c913d2013-04-26 15:06:44 +0000158 Json::Value asJsonValue() const;
epoger@google.com37269602013-01-19 04:21:27 +0000159
160 private:
epoger@google.comd271d242013-02-13 18:14:48 +0000161 const static bool kDefaultIgnoreFailure = false;
162
epoger@google.comd4993ff2013-05-24 14:33:28 +0000163 SkTArray<GmResultDigest> fAllowedResultDigests;
epoger@google.com37269602013-01-19 04:21:27 +0000164 bool fIgnoreFailure;
epoger@google.com84a18022013-02-01 20:39:15 +0000165 SkBitmap fBitmap;
epoger@google.com37269602013-01-19 04:21:27 +0000166 };
167
168 /**
169 * Abstract source of Expectations objects for individual tests.
170 */
171 class ExpectationsSource : public SkRefCnt {
172 public:
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000173 SK_DECLARE_INST_COUNT(ExpectationsSource)
174
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000175 virtual Expectations get(const char *testName) const = 0;
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000176
177 private:
178 typedef SkRefCnt INHERITED;
epoger@google.com37269602013-01-19 04:21:27 +0000179 };
180
181 /**
182 * Return Expectations based on individual image files on disk.
183 */
184 class IndividualImageExpectationsSource : public ExpectationsSource {
185 public:
186 /**
187 * Create an ExpectationsSource that will return Expectations based on
188 * image files found within rootDir.
189 *
190 * rootDir: directory under which to look for image files
191 * (this string will be copied to storage within this object)
epoger@google.com37269602013-01-19 04:21:27 +0000192 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000193 explicit IndividualImageExpectationsSource(const char *rootDir) : fRootDir(rootDir) {}
epoger@google.com37269602013-01-19 04:21:27 +0000194
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000195 Expectations get(const char *testName) const SK_OVERRIDE ;
epoger@google.com37269602013-01-19 04:21:27 +0000196
197 private:
198 const SkString fRootDir;
epoger@google.com37269602013-01-19 04:21:27 +0000199 };
200
epoger@google.comd271d242013-02-13 18:14:48 +0000201 /**
202 * Return Expectations based on JSON summary file.
203 */
204 class JsonExpectationsSource : public ExpectationsSource {
205 public:
206 /**
207 * Create an ExpectationsSource that will return Expectations based on
208 * a JSON file.
209 *
210 * jsonPath: path to JSON file to read
211 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000212 explicit JsonExpectationsSource(const char *jsonPath);
epoger@google.comd271d242013-02-13 18:14:48 +0000213
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000214 Expectations get(const char *testName) const SK_OVERRIDE;
epoger@google.comd271d242013-02-13 18:14:48 +0000215
216 private:
217
218 /**
219 * Read as many bytes as possible (up to maxBytes) from the stream into
220 * an SkData object.
221 *
222 * If the returned SkData contains fewer than maxBytes, then EOF has been
223 * reached and no more data would be available from subsequent calls.
224 * (If EOF has already been reached, then this call will return an empty
225 * SkData object immediately.)
226 *
227 * If there are fewer than maxBytes bytes available to read from the
228 * stream, but the stream has not been closed yet, this call will block
229 * until there are enough bytes to read or the stream has been closed.
230 *
231 * It is up to the caller to call unref() on the returned SkData object
232 * once the data is no longer needed, so that the underlying buffer will
233 * be freed. For example:
234 *
235 * {
236 * size_t maxBytes = 256;
237 * SkAutoDataUnref dataRef(readIntoSkData(stream, maxBytes));
238 * if (NULL != dataRef.get()) {
239 * size_t bytesActuallyRead = dataRef.get()->size();
240 * // use the data...
241 * }
242 * }
243 * // underlying buffer has been freed, thanks to auto unref
244 *
245 */
246 // TODO(epoger): Move this, into SkStream.[cpp|h] as attempted in
247 // https://codereview.appspot.com/7300071 ?
scroggo@google.com6843bdb2013-05-08 19:14:23 +0000248 // And maybe ReadFileIntoSkData() also?
249 static SkData* ReadIntoSkData(SkStream &stream, size_t maxBytes);
epoger@google.comd271d242013-02-13 18:14:48 +0000250
251 /**
scroggo@google.com6843bdb2013-05-08 19:14:23 +0000252 * Wrapper around ReadIntoSkData for files: reads the entire file into
epoger@google.comd271d242013-02-13 18:14:48 +0000253 * an SkData object.
254 */
scroggo@google.com6843bdb2013-05-08 19:14:23 +0000255 static SkData* ReadFileIntoSkData(SkFILEStream &stream) {
256 return ReadIntoSkData(stream, stream.getLength());
epoger@google.comd271d242013-02-13 18:14:48 +0000257 }
258
259 /**
260 * Read the file contents from jsonPath and parse them into jsonRoot.
261 *
262 * Returns true if successful.
263 */
scroggo@google.com6843bdb2013-05-08 19:14:23 +0000264 static bool Parse(const char *jsonPath, Json::Value *jsonRoot);
epoger@google.comd271d242013-02-13 18:14:48 +0000265
266 Json::Value fJsonRoot;
267 Json::Value fJsonExpectedResults;
268 };
269
epoger@google.com37269602013-01-19 04:21:27 +0000270}
271#endif