blob: c69fafc13034de29a5dd602ea9f60f65404da053 [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.
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +00006 *
7 * TODO(epoger): Combine this with tools/image_expectations.h, or eliminate one of the two.
epoger@google.com37269602013-01-19 04:21:27 +00008 */
9#ifndef gm_expectations_DEFINED
10#define gm_expectations_DEFINED
11
12#include "gm.h"
epoger@google.com84a18022013-02-01 20:39:15 +000013#include "SkBitmap.h"
epoger@google.com908f5832013-04-12 02:23:55 +000014#include "SkBitmapHasher.h"
epoger@google.comd271d242013-02-13 18:14:48 +000015#include "SkData.h"
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000016#include "SkJSONCPP.h"
epoger@google.com37269602013-01-19 04:21:27 +000017#include "SkOSFile.h"
18#include "SkRefCnt.h"
epoger@google.comd271d242013-02-13 18:14:48 +000019#include "SkStream.h"
epoger@google.com37269602013-01-19 04:21:27 +000020#include "SkTArray.h"
21
epoger@google.com37269602013-01-19 04:21:27 +000022
23namespace skiagm {
24
commit-bot@chromium.org25c10662014-05-16 17:56:43 +000025#ifdef SK_BUILD_JSON_WRITER
epoger@google.com76c913d2013-04-26 15:06:44 +000026 Json::Value CreateJsonTree(Json::Value expectedResults,
27 Json::Value actualResultsFailed,
28 Json::Value actualResultsFailureIgnored,
29 Json::Value actualResultsNoComparison,
30 Json::Value actualResultsSucceeded);
commit-bot@chromium.org25c10662014-05-16 17:56:43 +000031#endif
epoger@google.com37269602013-01-19 04:21:27 +000032 /**
epoger@google.comd4993ff2013-05-24 14:33:28 +000033 * The digest of a GM test result.
34 *
35 * Currently, this is always a uint64_t hash digest of an SkBitmap...
36 * but we will add other flavors soon.
37 */
38 class GmResultDigest {
39 public:
40 /**
41 * Create a ResultDigest representing an actual image result.
42 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +000043 explicit GmResultDigest(const SkBitmap &bitmap);
epoger@google.comd4993ff2013-05-24 14:33:28 +000044
commit-bot@chromium.org25c10662014-05-16 17:56:43 +000045#ifdef SK_BUILD_JSON_WRITER
epoger@google.comd4993ff2013-05-24 14:33:28 +000046 /**
47 * Create a ResultDigest representing an allowed result
48 * checksum within JSON expectations file, in the form
49 * ["bitmap-64bitMD5", 12345].
50 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +000051 explicit GmResultDigest(const Json::Value &jsonTypeValuePair);
commit-bot@chromium.org25c10662014-05-16 17:56:43 +000052#endif
epoger@google.comd4993ff2013-05-24 14:33:28 +000053
54 /**
55 * Returns true if this GmResultDigest was fully and successfully
56 * created.
57 */
58 bool isValid() const;
59
60 /**
61 * Returns true if this and other GmResultDigest could
62 * represent identical results.
63 */
64 bool equals(const GmResultDigest &other) const;
65
commit-bot@chromium.org25c10662014-05-16 17:56:43 +000066#ifdef SK_BUILD_JSON_WRITER
epoger@google.comd4993ff2013-05-24 14:33:28 +000067 /**
68 * Returns a JSON type/value pair representing this result,
69 * such as ["bitmap-64bitMD5", 12345].
70 */
71 Json::Value asJsonTypeValuePair() const;
commit-bot@chromium.org25c10662014-05-16 17:56:43 +000072#endif
epoger@google.comd4993ff2013-05-24 14:33:28 +000073
epoger@google.com6f7f14d2013-06-19 18:28:31 +000074 /**
75 * Returns the hashtype, such as "bitmap-64bitMD5", as an SkString.
76 */
77 SkString getHashType() const;
78
79 /**
80 * Returns the hash digest value, such as "12345", as an SkString.
81 */
82 SkString getDigestValue() const;
83
epoger@google.comd4993ff2013-05-24 14:33:28 +000084 private:
85 bool fIsValid; // always check this first--if it's false, other fields are meaningless
86 uint64_t fHashDigest;
87 };
88
89 /**
epoger@google.com6f7f14d2013-06-19 18:28:31 +000090 * Encapsulates an SkBitmap and its GmResultDigest, guaranteed to keep them in sync.
91 */
92 class BitmapAndDigest {
93 public:
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +000094 explicit BitmapAndDigest(const SkBitmap &bitmap) : fBitmap(bitmap), fDigest(bitmap) {}
epoger@google.com6f7f14d2013-06-19 18:28:31 +000095
96 const SkBitmap fBitmap;
97 const GmResultDigest fDigest;
98 };
99
100 /**
epoger@google.comd4993ff2013-05-24 14:33:28 +0000101 * Test expectations (allowed image results, etc.)
epoger@google.com37269602013-01-19 04:21:27 +0000102 */
103 class Expectations {
104 public:
105 /**
106 * No expectations at all.
epoger@google.com37269602013-01-19 04:21:27 +0000107 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000108 explicit Expectations(bool ignoreFailure=kDefaultIgnoreFailure);
epoger@google.com37269602013-01-19 04:21:27 +0000109
110 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000111 * Expect exactly one image (appropriate for the case when we
epoger@google.com37269602013-01-19 04:21:27 +0000112 * are comparing against a single PNG file).
epoger@google.com37269602013-01-19 04:21:27 +0000113 */
scroggo@google.com5187c432013-10-22 00:42:46 +0000114 explicit Expectations(const SkBitmap& bitmap, bool ignoreFailure=kDefaultIgnoreFailure);
115
116 /**
117 * Expect exactly one image, whose digest has already been computed.
118 */
119 explicit Expectations(const BitmapAndDigest& bitmapAndDigest);
epoger@google.com37269602013-01-19 04:21:27 +0000120
commit-bot@chromium.org25c10662014-05-16 17:56:43 +0000121#ifdef SK_BUILD_JSON_WRITER
epoger@google.com37269602013-01-19 04:21:27 +0000122 /**
epoger@google.comd271d242013-02-13 18:14:48 +0000123 * Create Expectations from a JSON element as found within the
124 * kJsonKey_ExpectedResults section.
125 *
126 * It's fine if the jsonElement is null or empty; in that case, we just
127 * don't have any expectations.
128 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000129 explicit Expectations(Json::Value jsonElement);
commit-bot@chromium.org25c10662014-05-16 17:56:43 +0000130#endif
epoger@google.comd271d242013-02-13 18:14:48 +0000131
132 /**
epoger@google.com37269602013-01-19 04:21:27 +0000133 * Returns true iff we want to ignore failed expectations.
134 */
135 bool ignoreFailure() const { return this->fIgnoreFailure; }
136
137 /**
epoger@google.comdefc4872013-09-19 06:18:27 +0000138 * Override default setting of fIgnoreFailure.
139 */
140 void setIgnoreFailure(bool val) { this->fIgnoreFailure = val; }
141
142 /**
epoger@google.comd4993ff2013-05-24 14:33:28 +0000143 * Returns true iff there are no allowed results.
epoger@google.com37269602013-01-19 04:21:27 +0000144 */
epoger@google.comd4993ff2013-05-24 14:33:28 +0000145 bool empty() const { return this->fAllowedResultDigests.empty(); }
epoger@google.com37269602013-01-19 04:21:27 +0000146
147 /**
epoger@google.comd4993ff2013-05-24 14:33:28 +0000148 * Returns true iff resultDigest matches any allowed result,
epoger@google.com37269602013-01-19 04:21:27 +0000149 * regardless of fIgnoreFailure. (The caller can check
150 * that separately.)
151 */
epoger@google.comd4993ff2013-05-24 14:33:28 +0000152 bool match(GmResultDigest resultDigest) const;
epoger@google.com37269602013-01-19 04:21:27 +0000153
154 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000155 * If this Expectation is based on a single SkBitmap, return a
156 * pointer to that SkBitmap. Otherwise (if the Expectation is
157 * empty, or if it was based on a list of checksums rather
158 * than a single bitmap), returns NULL.
159 */
160 const SkBitmap *asBitmap() const {
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000161 return (kUnknown_SkColorType == fBitmap.colorType()) ? NULL : &fBitmap;
epoger@google.com84a18022013-02-01 20:39:15 +0000162 }
163
commit-bot@chromium.org25c10662014-05-16 17:56:43 +0000164#ifdef SK_BUILD_JSON_WRITER
epoger@google.com84a18022013-02-01 20:39:15 +0000165 /**
epoger@google.com76c913d2013-04-26 15:06:44 +0000166 * Return a JSON representation of the expectations.
epoger@google.com37269602013-01-19 04:21:27 +0000167 */
epoger@google.com76c913d2013-04-26 15:06:44 +0000168 Json::Value asJsonValue() const;
commit-bot@chromium.org25c10662014-05-16 17:56:43 +0000169#endif
epoger@google.com37269602013-01-19 04:21:27 +0000170
171 private:
epoger@google.comd271d242013-02-13 18:14:48 +0000172 const static bool kDefaultIgnoreFailure = false;
173
epoger@google.comd4993ff2013-05-24 14:33:28 +0000174 SkTArray<GmResultDigest> fAllowedResultDigests;
epoger@google.com37269602013-01-19 04:21:27 +0000175 bool fIgnoreFailure;
epoger@google.com84a18022013-02-01 20:39:15 +0000176 SkBitmap fBitmap;
epoger@google.com37269602013-01-19 04:21:27 +0000177 };
178
179 /**
180 * Abstract source of Expectations objects for individual tests.
181 */
182 class ExpectationsSource : public SkRefCnt {
183 public:
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000184 SK_DECLARE_INST_COUNT(ExpectationsSource)
185
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000186 virtual Expectations get(const char *testName) const = 0;
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000187
188 private:
189 typedef SkRefCnt INHERITED;
epoger@google.com37269602013-01-19 04:21:27 +0000190 };
191
192 /**
193 * Return Expectations based on individual image files on disk.
194 */
195 class IndividualImageExpectationsSource : public ExpectationsSource {
196 public:
197 /**
198 * Create an ExpectationsSource that will return Expectations based on
199 * image files found within rootDir.
200 *
201 * rootDir: directory under which to look for image files
202 * (this string will be copied to storage within this object)
epoger@google.com37269602013-01-19 04:21:27 +0000203 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000204 explicit IndividualImageExpectationsSource(const char *rootDir) : fRootDir(rootDir) {}
epoger@google.com37269602013-01-19 04:21:27 +0000205
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000206 Expectations get(const char *testName) const SK_OVERRIDE ;
epoger@google.com37269602013-01-19 04:21:27 +0000207
208 private:
209 const SkString fRootDir;
epoger@google.com37269602013-01-19 04:21:27 +0000210 };
211
commit-bot@chromium.org25c10662014-05-16 17:56:43 +0000212#ifdef SK_BUILD_JSON_WRITER
epoger@google.comd271d242013-02-13 18:14:48 +0000213 /**
214 * Return Expectations based on JSON summary file.
215 */
216 class JsonExpectationsSource : public ExpectationsSource {
217 public:
218 /**
219 * Create an ExpectationsSource that will return Expectations based on
220 * a JSON file.
221 *
222 * jsonPath: path to JSON file to read
223 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000224 explicit JsonExpectationsSource(const char *jsonPath);
epoger@google.comd271d242013-02-13 18:14:48 +0000225
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000226 Expectations get(const char *testName) const SK_OVERRIDE;
epoger@google.comd271d242013-02-13 18:14:48 +0000227
228 private:
229
230 /**
epoger@google.comd271d242013-02-13 18:14:48 +0000231 * Read the file contents from jsonPath and parse them into jsonRoot.
232 *
233 * Returns true if successful.
234 */
scroggo@google.com6843bdb2013-05-08 19:14:23 +0000235 static bool Parse(const char *jsonPath, Json::Value *jsonRoot);
epoger@google.comd271d242013-02-13 18:14:48 +0000236
237 Json::Value fJsonRoot;
238 Json::Value fJsonExpectedResults;
239 };
commit-bot@chromium.org25c10662014-05-16 17:56:43 +0000240#endif // SK_BUILD_JSON_WRITER
epoger@google.comd271d242013-02-13 18:14:48 +0000241
epoger@google.com37269602013-01-19 04:21:27 +0000242}
243#endif