blob: 52e711fe7f3e051d233c23a1ddd8b71118869e63 [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.comd271d242013-02-13 18:14:48 +000014#include "SkData.h"
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000015#include "SkJSONCPP.h"
epoger@google.com37269602013-01-19 04:21:27 +000016#include "SkOSFile.h"
17#include "SkRefCnt.h"
epoger@google.comd271d242013-02-13 18:14:48 +000018#include "SkStream.h"
epoger@google.com37269602013-01-19 04:21:27 +000019#include "SkTArray.h"
20
epoger@google.com37269602013-01-19 04:21:27 +000021
22namespace skiagm {
23
epoger@google.com76c913d2013-04-26 15:06:44 +000024 Json::Value CreateJsonTree(Json::Value expectedResults,
25 Json::Value actualResultsFailed,
26 Json::Value actualResultsFailureIgnored,
27 Json::Value actualResultsNoComparison,
28 Json::Value actualResultsSucceeded);
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
halcanary96fcdcc2015-08-27 07:41:13 -0700149 * than a single bitmap), returns nullptr.
epoger@google.com84a18022013-02-01 20:39:15 +0000150 */
151 const SkBitmap *asBitmap() const {
halcanary96fcdcc2015-08-27 07:41:13 -0700152 return (kUnknown_SkColorType == fBitmap.colorType()) ? nullptr : &fBitmap;
epoger@google.com84a18022013-02-01 20:39:15 +0000153 }
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.org1d5bbb22013-10-14 14:15:28 +0000173 virtual Expectations get(const char *testName) const = 0;
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000174
175 private:
176 typedef SkRefCnt INHERITED;
epoger@google.com37269602013-01-19 04:21:27 +0000177 };
178
179 /**
180 * Return Expectations based on individual image files on disk.
181 */
182 class IndividualImageExpectationsSource : public ExpectationsSource {
183 public:
184 /**
185 * Create an ExpectationsSource that will return Expectations based on
186 * image files found within rootDir.
187 *
188 * rootDir: directory under which to look for image files
189 * (this string will be copied to storage within this object)
epoger@google.com37269602013-01-19 04:21:27 +0000190 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000191 explicit IndividualImageExpectationsSource(const char *rootDir) : fRootDir(rootDir) {}
epoger@google.com37269602013-01-19 04:21:27 +0000192
mtklein36352bf2015-03-25 18:17:31 -0700193 Expectations get(const char *testName) const override ;
epoger@google.com37269602013-01-19 04:21:27 +0000194
195 private:
196 const SkString fRootDir;
epoger@google.com37269602013-01-19 04:21:27 +0000197 };
198
epoger@google.comd271d242013-02-13 18:14:48 +0000199 /**
200 * Return Expectations based on JSON summary file.
201 */
202 class JsonExpectationsSource : public ExpectationsSource {
203 public:
204 /**
205 * Create an ExpectationsSource that will return Expectations based on
206 * a JSON file.
207 *
208 * jsonPath: path to JSON file to read
209 */
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +0000210 explicit JsonExpectationsSource(const char *jsonPath);
epoger@google.comd271d242013-02-13 18:14:48 +0000211
mtklein36352bf2015-03-25 18:17:31 -0700212 Expectations get(const char *testName) const override;
epoger@google.comd271d242013-02-13 18:14:48 +0000213
214 private:
215
216 /**
epoger@google.comd271d242013-02-13 18:14:48 +0000217 * Read the file contents from jsonPath and parse them into jsonRoot.
218 *
219 * Returns true if successful.
220 */
scroggo@google.com6843bdb2013-05-08 19:14:23 +0000221 static bool Parse(const char *jsonPath, Json::Value *jsonRoot);
epoger@google.comd271d242013-02-13 18:14:48 +0000222
223 Json::Value fJsonRoot;
224 Json::Value fJsonExpectedResults;
225 };
226
epoger@google.com37269602013-01-19 04:21:27 +0000227}
228#endif