2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
7 * TODO(epoger): Combine this with tools/image_expectations.h, or eliminate one of the two.
9 #ifndef gm_expectations_DEFINED
10 #define gm_expectations_DEFINED
14 #include "SkBitmapHasher.h"
16 #include "SkJSONCPP.h"
25 Json::Value CreateJsonTree(Json::Value expectedResults,
26 Json::Value actualResultsFailed,
27 Json::Value actualResultsFailureIgnored,
28 Json::Value actualResultsNoComparison,
29 Json::Value actualResultsSucceeded);
31 * The digest of a GM test result.
33 * Currently, this is always a uint64_t hash digest of an SkBitmap...
34 * but we will add other flavors soon.
36 class GmResultDigest {
39 * Create a ResultDigest representing an actual image result.
41 explicit GmResultDigest(const SkBitmap &bitmap);
44 * Create a ResultDigest representing an allowed result
45 * checksum within JSON expectations file, in the form
46 * ["bitmap-64bitMD5", 12345].
48 explicit GmResultDigest(const Json::Value &jsonTypeValuePair);
51 * Returns true if this GmResultDigest was fully and successfully
57 * Returns true if this and other GmResultDigest could
58 * represent identical results.
60 bool equals(const GmResultDigest &other) const;
63 * Returns a JSON type/value pair representing this result,
64 * such as ["bitmap-64bitMD5", 12345].
66 Json::Value asJsonTypeValuePair() const;
69 * Returns the hashtype, such as "bitmap-64bitMD5", as an SkString.
71 SkString getHashType() const;
74 * Returns the hash digest value, such as "12345", as an SkString.
76 SkString getDigestValue() const;
79 bool fIsValid; // always check this first--if it's false, other fields are meaningless
84 * Encapsulates an SkBitmap and its GmResultDigest, guaranteed to keep them in sync.
86 class BitmapAndDigest {
88 explicit BitmapAndDigest(const SkBitmap &bitmap) : fBitmap(bitmap), fDigest(bitmap) {}
90 const SkBitmap fBitmap;
91 const GmResultDigest fDigest;
95 * Test expectations (allowed image results, etc.)
100 * No expectations at all.
102 explicit Expectations(bool ignoreFailure=kDefaultIgnoreFailure);
105 * Expect exactly one image (appropriate for the case when we
106 * are comparing against a single PNG file).
108 explicit Expectations(const SkBitmap& bitmap, bool ignoreFailure=kDefaultIgnoreFailure);
111 * Expect exactly one image, whose digest has already been computed.
113 explicit Expectations(const BitmapAndDigest& bitmapAndDigest);
116 * Create Expectations from a JSON element as found within the
117 * kJsonKey_ExpectedResults section.
119 * It's fine if the jsonElement is null or empty; in that case, we just
120 * don't have any expectations.
122 explicit Expectations(Json::Value jsonElement);
125 * Returns true iff we want to ignore failed expectations.
127 bool ignoreFailure() const { return this->fIgnoreFailure; }
130 * Override default setting of fIgnoreFailure.
132 void setIgnoreFailure(bool val) { this->fIgnoreFailure = val; }
135 * Returns true iff there are no allowed results.
137 bool empty() const { return this->fAllowedResultDigests.empty(); }
140 * Returns true iff resultDigest matches any allowed result,
141 * regardless of fIgnoreFailure. (The caller can check
144 bool match(GmResultDigest resultDigest) const;
147 * 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.
152 const SkBitmap *asBitmap() const {
153 return (kUnknown_SkColorType == fBitmap.colorType()) ? NULL : &fBitmap;
157 * Return a JSON representation of the expectations.
159 Json::Value asJsonValue() const;
162 const static bool kDefaultIgnoreFailure = false;
164 SkTArray<GmResultDigest> fAllowedResultDigests;
170 * Abstract source of Expectations objects for individual tests.
172 class ExpectationsSource : public SkRefCnt {
174 SK_DECLARE_INST_COUNT(ExpectationsSource)
176 virtual Expectations get(const char *testName) const = 0;
179 typedef SkRefCnt INHERITED;
183 * Return Expectations based on individual image files on disk.
185 class IndividualImageExpectationsSource : public ExpectationsSource {
188 * Create an ExpectationsSource that will return Expectations based on
189 * image files found within rootDir.
191 * rootDir: directory under which to look for image files
192 * (this string will be copied to storage within this object)
194 explicit IndividualImageExpectationsSource(const char *rootDir) : fRootDir(rootDir) {}
196 Expectations get(const char *testName) const override ;
199 const SkString fRootDir;
203 * Return Expectations based on JSON summary file.
205 class JsonExpectationsSource : public ExpectationsSource {
208 * Create an ExpectationsSource that will return Expectations based on
211 * jsonPath: path to JSON file to read
213 explicit JsonExpectationsSource(const char *jsonPath);
215 Expectations get(const char *testName) const override;
220 * Read the file contents from jsonPath and parse them into jsonRoot.
222 * Returns true if successful.
224 static bool Parse(const char *jsonPath, Json::Value *jsonRoot);
226 Json::Value fJsonRoot;
227 Json::Value fJsonExpectedResults;