epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 90c0fbd | 2014-05-09 03:18:41 +0000 | [diff] [blame] | 6 | * |
| 7 | * TODO(epoger): Combine this with tools/image_expectations.h, or eliminate one of the two. |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 8 | */ |
| 9 | #ifndef gm_expectations_DEFINED |
| 10 | #define gm_expectations_DEFINED |
| 11 | |
| 12 | #include "gm.h" |
epoger@google.com | 84a1802 | 2013-02-01 20:39:15 +0000 | [diff] [blame] | 13 | #include "SkBitmap.h" |
epoger@google.com | 908f583 | 2013-04-12 02:23:55 +0000 | [diff] [blame] | 14 | #include "SkBitmapHasher.h" |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 15 | #include "SkData.h" |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 16 | #include "SkJSONCPP.h" |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 17 | #include "SkOSFile.h" |
| 18 | #include "SkRefCnt.h" |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 19 | #include "SkStream.h" |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 20 | #include "SkTArray.h" |
| 21 | |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 22 | |
| 23 | namespace skiagm { |
| 24 | |
commit-bot@chromium.org | 25c1066 | 2014-05-16 17:56:43 +0000 | [diff] [blame] | 25 | #ifdef SK_BUILD_JSON_WRITER |
epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 26 | 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.org | 25c1066 | 2014-05-16 17:56:43 +0000 | [diff] [blame] | 31 | #endif |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 32 | /** |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 33 | * 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.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 43 | explicit GmResultDigest(const SkBitmap &bitmap); |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 44 | |
commit-bot@chromium.org | 25c1066 | 2014-05-16 17:56:43 +0000 | [diff] [blame] | 45 | #ifdef SK_BUILD_JSON_WRITER |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 46 | /** |
| 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.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 51 | explicit GmResultDigest(const Json::Value &jsonTypeValuePair); |
commit-bot@chromium.org | 25c1066 | 2014-05-16 17:56:43 +0000 | [diff] [blame] | 52 | #endif |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 53 | |
| 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.org | 25c1066 | 2014-05-16 17:56:43 +0000 | [diff] [blame] | 66 | #ifdef SK_BUILD_JSON_WRITER |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 67 | /** |
| 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.org | 25c1066 | 2014-05-16 17:56:43 +0000 | [diff] [blame] | 72 | #endif |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 73 | |
epoger@google.com | 6f7f14d | 2013-06-19 18:28:31 +0000 | [diff] [blame] | 74 | /** |
| 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.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 84 | 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.com | 6f7f14d | 2013-06-19 18:28:31 +0000 | [diff] [blame] | 90 | * Encapsulates an SkBitmap and its GmResultDigest, guaranteed to keep them in sync. |
| 91 | */ |
| 92 | class BitmapAndDigest { |
| 93 | public: |
commit-bot@chromium.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 94 | explicit BitmapAndDigest(const SkBitmap &bitmap) : fBitmap(bitmap), fDigest(bitmap) {} |
epoger@google.com | 6f7f14d | 2013-06-19 18:28:31 +0000 | [diff] [blame] | 95 | |
| 96 | const SkBitmap fBitmap; |
| 97 | const GmResultDigest fDigest; |
| 98 | }; |
| 99 | |
| 100 | /** |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 101 | * Test expectations (allowed image results, etc.) |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 102 | */ |
| 103 | class Expectations { |
| 104 | public: |
| 105 | /** |
| 106 | * No expectations at all. |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 107 | */ |
commit-bot@chromium.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 108 | explicit Expectations(bool ignoreFailure=kDefaultIgnoreFailure); |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 109 | |
| 110 | /** |
epoger@google.com | 84a1802 | 2013-02-01 20:39:15 +0000 | [diff] [blame] | 111 | * Expect exactly one image (appropriate for the case when we |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 112 | * are comparing against a single PNG file). |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 113 | */ |
scroggo@google.com | 5187c43 | 2013-10-22 00:42:46 +0000 | [diff] [blame] | 114 | 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.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 120 | |
commit-bot@chromium.org | 25c1066 | 2014-05-16 17:56:43 +0000 | [diff] [blame] | 121 | #ifdef SK_BUILD_JSON_WRITER |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 122 | /** |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 123 | * 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.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 129 | explicit Expectations(Json::Value jsonElement); |
commit-bot@chromium.org | 25c1066 | 2014-05-16 17:56:43 +0000 | [diff] [blame] | 130 | #endif |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 131 | |
| 132 | /** |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 133 | * Returns true iff we want to ignore failed expectations. |
| 134 | */ |
| 135 | bool ignoreFailure() const { return this->fIgnoreFailure; } |
| 136 | |
| 137 | /** |
epoger@google.com | defc487 | 2013-09-19 06:18:27 +0000 | [diff] [blame] | 138 | * Override default setting of fIgnoreFailure. |
| 139 | */ |
| 140 | void setIgnoreFailure(bool val) { this->fIgnoreFailure = val; } |
| 141 | |
| 142 | /** |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 143 | * Returns true iff there are no allowed results. |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 144 | */ |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 145 | bool empty() const { return this->fAllowedResultDigests.empty(); } |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 146 | |
| 147 | /** |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 148 | * Returns true iff resultDigest matches any allowed result, |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 149 | * regardless of fIgnoreFailure. (The caller can check |
| 150 | * that separately.) |
| 151 | */ |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 152 | bool match(GmResultDigest resultDigest) const; |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 153 | |
| 154 | /** |
epoger@google.com | 84a1802 | 2013-02-01 20:39:15 +0000 | [diff] [blame] | 155 | * 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.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 161 | return (kUnknown_SkColorType == fBitmap.colorType()) ? NULL : &fBitmap; |
epoger@google.com | 84a1802 | 2013-02-01 20:39:15 +0000 | [diff] [blame] | 162 | } |
| 163 | |
commit-bot@chromium.org | 25c1066 | 2014-05-16 17:56:43 +0000 | [diff] [blame] | 164 | #ifdef SK_BUILD_JSON_WRITER |
epoger@google.com | 84a1802 | 2013-02-01 20:39:15 +0000 | [diff] [blame] | 165 | /** |
epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 166 | * Return a JSON representation of the expectations. |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 167 | */ |
epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 168 | Json::Value asJsonValue() const; |
commit-bot@chromium.org | 25c1066 | 2014-05-16 17:56:43 +0000 | [diff] [blame] | 169 | #endif |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 170 | |
| 171 | private: |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 172 | const static bool kDefaultIgnoreFailure = false; |
| 173 | |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 174 | SkTArray<GmResultDigest> fAllowedResultDigests; |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 175 | bool fIgnoreFailure; |
epoger@google.com | 84a1802 | 2013-02-01 20:39:15 +0000 | [diff] [blame] | 176 | SkBitmap fBitmap; |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | /** |
| 180 | * Abstract source of Expectations objects for individual tests. |
| 181 | */ |
| 182 | class ExpectationsSource : public SkRefCnt { |
| 183 | public: |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 184 | SK_DECLARE_INST_COUNT(ExpectationsSource) |
| 185 | |
commit-bot@chromium.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 186 | virtual Expectations get(const char *testName) const = 0; |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 187 | |
| 188 | private: |
| 189 | typedef SkRefCnt INHERITED; |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 190 | }; |
| 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.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 203 | */ |
commit-bot@chromium.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 204 | explicit IndividualImageExpectationsSource(const char *rootDir) : fRootDir(rootDir) {} |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 205 | |
commit-bot@chromium.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 206 | Expectations get(const char *testName) const SK_OVERRIDE ; |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 207 | |
| 208 | private: |
| 209 | const SkString fRootDir; |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 210 | }; |
| 211 | |
commit-bot@chromium.org | 25c1066 | 2014-05-16 17:56:43 +0000 | [diff] [blame] | 212 | #ifdef SK_BUILD_JSON_WRITER |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 213 | /** |
| 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.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 224 | explicit JsonExpectationsSource(const char *jsonPath); |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 225 | |
commit-bot@chromium.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 226 | Expectations get(const char *testName) const SK_OVERRIDE; |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 227 | |
| 228 | private: |
| 229 | |
| 230 | /** |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 231 | * Read the file contents from jsonPath and parse them into jsonRoot. |
| 232 | * |
| 233 | * Returns true if successful. |
| 234 | */ |
scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 235 | static bool Parse(const char *jsonPath, Json::Value *jsonRoot); |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 236 | |
| 237 | Json::Value fJsonRoot; |
| 238 | Json::Value fJsonExpectedResults; |
| 239 | }; |
commit-bot@chromium.org | 25c1066 | 2014-05-16 17:56:43 +0000 | [diff] [blame] | 240 | #endif // SK_BUILD_JSON_WRITER |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 241 | |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 242 | } |
| 243 | #endif |