blob: 1cd6a1f756233893c5c6b04c17641cb8653a2987 [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
epoger@google.com76c913d2013-04-26 15:06:44 +000025 Json::Value CreateJsonTree(Json::Value expectedResults,
26 Json::Value actualResultsFailed,
27 Json::Value actualResultsFailureIgnored,
28 Json::Value actualResultsNoComparison,
29 Json::Value actualResultsSucceeded);
epoger@google.com37269602013-01-19 04:21:27 +000030 /**
epoger@google.comd4993ff2013-05-24 14:33:28 +000031 * The digest of a GM test result.
32 *
33 * Currently, this is always a uint64_t hash digest of an SkBitmap...
34 * but we will add other flavors soon.
35 */
36 class GmResultDigest {
37 public:
38 /**
39 * Create a ResultDigest representing an actual image result.
40 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +000041 explicit GmResultDigest(const SkBitmap &bitmap);
epoger@google.comd4993ff2013-05-24 14:33:28 +000042
43 /**
44 * Create a ResultDigest representing an allowed result
45 * checksum within JSON expectations file, in the form
46 * ["bitmap-64bitMD5", 12345].
47 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +000048 explicit GmResultDigest(const Json::Value &jsonTypeValuePair);
epoger@google.comd4993ff2013-05-24 14:33:28 +000049
50 /**
51 * Returns true if this GmResultDigest was fully and successfully
52 * created.
53 */
54 bool isValid() const;
55
56 /**
57 * Returns true if this and other GmResultDigest could
58 * represent identical results.
59 */
60 bool equals(const GmResultDigest &other) const;
61
62 /**
63 * Returns a JSON type/value pair representing this result,
64 * such as ["bitmap-64bitMD5", 12345].
65 */
66 Json::Value asJsonTypeValuePair() const;
67
epoger@google.com6f7f14d2013-06-19 18:28:31 +000068 /**
69 * Returns the hashtype, such as "bitmap-64bitMD5", as an SkString.
70 */
71 SkString getHashType() const;
72
73 /**
74 * Returns the hash digest value, such as "12345", as an SkString.
75 */
76 SkString getDigestValue() const;
77
epoger@google.comd4993ff2013-05-24 14:33:28 +000078 private:
79 bool fIsValid; // always check this first--if it's false, other fields are meaningless
80 uint64_t fHashDigest;
81 };
82
83 /**
epoger@google.com6f7f14d2013-06-19 18:28:31 +000084 * Encapsulates an SkBitmap and its GmResultDigest, guaranteed to keep them in sync.
85 */
86 class BitmapAndDigest {
87 public:
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +000088 explicit BitmapAndDigest(const SkBitmap &bitmap) : fBitmap(bitmap), fDigest(bitmap) {}
epoger@google.com6f7f14d2013-06-19 18:28:31 +000089
90 const SkBitmap fBitmap;
91 const GmResultDigest fDigest;
92 };
93
94 /**
epoger@google.comd4993ff2013-05-24 14:33:28 +000095 * Test expectations (allowed image results, etc.)
epoger@google.com37269602013-01-19 04:21:27 +000096 */
97 class Expectations {
98 public:
99 /**
100 * No expectations at all.
epoger@google.com37269602013-01-19 04:21:27 +0000101 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000102 explicit Expectations(bool ignoreFailure=kDefaultIgnoreFailure);
epoger@google.com37269602013-01-19 04:21:27 +0000103
104 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000105 * Expect exactly one image (appropriate for the case when we
epoger@google.com37269602013-01-19 04:21:27 +0000106 * are comparing against a single PNG file).
epoger@google.com37269602013-01-19 04:21:27 +0000107 */
scroggo@google.com5187c432013-10-22 00:42:46 +0000108 explicit Expectations(const SkBitmap& bitmap, bool ignoreFailure=kDefaultIgnoreFailure);
109
110 /**
111 * Expect exactly one image, whose digest has already been computed.
112 */
113 explicit Expectations(const BitmapAndDigest& bitmapAndDigest);
epoger@google.com37269602013-01-19 04:21:27 +0000114
115 /**
epoger@google.comd271d242013-02-13 18:14:48 +0000116 * Create Expectations from a JSON element as found within the
117 * kJsonKey_ExpectedResults section.
118 *
119 * It's fine if the jsonElement is null or empty; in that case, we just
120 * don't have any expectations.
121 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000122 explicit Expectations(Json::Value jsonElement);
epoger@google.comd271d242013-02-13 18:14:48 +0000123
124 /**
epoger@google.com37269602013-01-19 04:21:27 +0000125 * Returns true iff we want to ignore failed expectations.
126 */
127 bool ignoreFailure() const { return this->fIgnoreFailure; }
128
129 /**
epoger@google.comdefc4872013-09-19 06:18:27 +0000130 * Override default setting of fIgnoreFailure.
131 */
132 void setIgnoreFailure(bool val) { this->fIgnoreFailure = val; }
133
134 /**
epoger@google.comd4993ff2013-05-24 14:33:28 +0000135 * Returns true iff there are no allowed results.
epoger@google.com37269602013-01-19 04:21:27 +0000136 */
epoger@google.comd4993ff2013-05-24 14:33:28 +0000137 bool empty() const { return this->fAllowedResultDigests.empty(); }
epoger@google.com37269602013-01-19 04:21:27 +0000138
139 /**
epoger@google.comd4993ff2013-05-24 14:33:28 +0000140 * Returns true iff resultDigest matches any allowed result,
epoger@google.com37269602013-01-19 04:21:27 +0000141 * regardless of fIgnoreFailure. (The caller can check
142 * that separately.)
143 */
epoger@google.comd4993ff2013-05-24 14:33:28 +0000144 bool match(GmResultDigest resultDigest) const;
epoger@google.com37269602013-01-19 04:21:27 +0000145
146 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000147 * If this Expectation is based on a single SkBitmap, return a
148 * pointer to that SkBitmap. Otherwise (if the Expectation is
149 * empty, or if it was based on a list of checksums rather
150 * than a single bitmap), returns NULL.
151 */
152 const SkBitmap *asBitmap() const {
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000153 return (kUnknown_SkColorType == fBitmap.colorType()) ? NULL : &fBitmap;
epoger@google.com84a18022013-02-01 20:39:15 +0000154 }
155
156 /**
epoger@google.com76c913d2013-04-26 15:06:44 +0000157 * Return a JSON representation of the expectations.
epoger@google.com37269602013-01-19 04:21:27 +0000158 */
epoger@google.com76c913d2013-04-26 15:06:44 +0000159 Json::Value asJsonValue() const;
epoger@google.com37269602013-01-19 04:21:27 +0000160
161 private:
epoger@google.comd271d242013-02-13 18:14:48 +0000162 const static bool kDefaultIgnoreFailure = false;
163
epoger@google.comd4993ff2013-05-24 14:33:28 +0000164 SkTArray<GmResultDigest> fAllowedResultDigests;
epoger@google.com37269602013-01-19 04:21:27 +0000165 bool fIgnoreFailure;
epoger@google.com84a18022013-02-01 20:39:15 +0000166 SkBitmap fBitmap;
epoger@google.com37269602013-01-19 04:21:27 +0000167 };
168
169 /**
170 * Abstract source of Expectations objects for individual tests.
171 */
172 class ExpectationsSource : public SkRefCnt {
173 public:
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000174 SK_DECLARE_INST_COUNT(ExpectationsSource)
175
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000176 virtual Expectations get(const char *testName) const = 0;
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000177
178 private:
179 typedef SkRefCnt INHERITED;
epoger@google.com37269602013-01-19 04:21:27 +0000180 };
181
182 /**
183 * Return Expectations based on individual image files on disk.
184 */
185 class IndividualImageExpectationsSource : public ExpectationsSource {
186 public:
187 /**
188 * Create an ExpectationsSource that will return Expectations based on
189 * image files found within rootDir.
190 *
191 * rootDir: directory under which to look for image files
192 * (this string will be copied to storage within this object)
epoger@google.com37269602013-01-19 04:21:27 +0000193 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000194 explicit IndividualImageExpectationsSource(const char *rootDir) : fRootDir(rootDir) {}
epoger@google.com37269602013-01-19 04:21:27 +0000195
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000196 Expectations get(const char *testName) const SK_OVERRIDE ;
epoger@google.com37269602013-01-19 04:21:27 +0000197
198 private:
199 const SkString fRootDir;
epoger@google.com37269602013-01-19 04:21:27 +0000200 };
201
epoger@google.comd271d242013-02-13 18:14:48 +0000202 /**
203 * Return Expectations based on JSON summary file.
204 */
205 class JsonExpectationsSource : public ExpectationsSource {
206 public:
207 /**
208 * Create an ExpectationsSource that will return Expectations based on
209 * a JSON file.
210 *
211 * jsonPath: path to JSON file to read
212 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000213 explicit JsonExpectationsSource(const char *jsonPath);
epoger@google.comd271d242013-02-13 18:14:48 +0000214
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000215 Expectations get(const char *testName) const SK_OVERRIDE;
epoger@google.comd271d242013-02-13 18:14:48 +0000216
217 private:
218
219 /**
epoger@google.comd271d242013-02-13 18:14:48 +0000220 * Read the file contents from jsonPath and parse them into jsonRoot.
221 *
222 * Returns true if successful.
223 */
scroggo@google.com6843bdb2013-05-08 19:14:23 +0000224 static bool Parse(const char *jsonPath, Json::Value *jsonRoot);
epoger@google.comd271d242013-02-13 18:14:48 +0000225
226 Json::Value fJsonRoot;
227 Json::Value fJsonExpectedResults;
228 };
229
epoger@google.com37269602013-01-19 04:21:27 +0000230}
231#endif