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. |
| 6 | */ |
| 7 | #ifndef gm_expectations_DEFINED |
| 8 | #define gm_expectations_DEFINED |
| 9 | |
| 10 | #include "gm.h" |
epoger@google.com | 84a1802 | 2013-02-01 20:39:15 +0000 | [diff] [blame] | 11 | #include "SkBitmap.h" |
epoger@google.com | 908f583 | 2013-04-12 02:23:55 +0000 | [diff] [blame] | 12 | #include "SkBitmapHasher.h" |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 13 | #include "SkData.h" |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 14 | #include "SkJSONCPP.h" |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 15 | #include "SkOSFile.h" |
| 16 | #include "SkRefCnt.h" |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 17 | #include "SkStream.h" |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 18 | #include "SkTArray.h" |
| 19 | |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 20 | |
| 21 | namespace skiagm { |
| 22 | |
epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 23 | Json::Value CreateJsonTree(Json::Value expectedResults, |
| 24 | Json::Value actualResultsFailed, |
| 25 | Json::Value actualResultsFailureIgnored, |
| 26 | Json::Value actualResultsNoComparison, |
| 27 | Json::Value actualResultsSucceeded); |
| 28 | |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 29 | /** |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 30 | * 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.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 40 | explicit GmResultDigest(const SkBitmap &bitmap); |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 41 | |
| 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.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 47 | explicit GmResultDigest(const Json::Value &jsonTypeValuePair); |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 48 | |
| 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.com | 6f7f14d | 2013-06-19 18:28:31 +0000 | [diff] [blame] | 67 | /** |
| 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.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 77 | 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.com | 6f7f14d | 2013-06-19 18:28:31 +0000 | [diff] [blame] | 83 | * Encapsulates an SkBitmap and its GmResultDigest, guaranteed to keep them in sync. |
| 84 | */ |
| 85 | class BitmapAndDigest { |
| 86 | public: |
commit-bot@chromium.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 87 | explicit BitmapAndDigest(const SkBitmap &bitmap) : fBitmap(bitmap), fDigest(bitmap) {} |
epoger@google.com | 6f7f14d | 2013-06-19 18:28:31 +0000 | [diff] [blame] | 88 | |
| 89 | const SkBitmap fBitmap; |
| 90 | const GmResultDigest fDigest; |
| 91 | }; |
| 92 | |
| 93 | /** |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 94 | * Test expectations (allowed image results, etc.) |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 95 | */ |
| 96 | class Expectations { |
| 97 | public: |
| 98 | /** |
| 99 | * No expectations at all. |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 100 | */ |
commit-bot@chromium.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 101 | explicit Expectations(bool ignoreFailure=kDefaultIgnoreFailure); |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 102 | |
| 103 | /** |
epoger@google.com | 84a1802 | 2013-02-01 20:39:15 +0000 | [diff] [blame] | 104 | * Expect exactly one image (appropriate for the case when we |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 105 | * are comparing against a single PNG file). |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 106 | */ |
scroggo@google.com | 5187c43 | 2013-10-22 00:42:46 +0000 | [diff] [blame] | 107 | 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.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 113 | |
| 114 | /** |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 115 | * 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.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 121 | explicit Expectations(Json::Value jsonElement); |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 122 | |
| 123 | /** |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 124 | * Returns true iff we want to ignore failed expectations. |
| 125 | */ |
| 126 | bool ignoreFailure() const { return this->fIgnoreFailure; } |
| 127 | |
| 128 | /** |
epoger@google.com | defc487 | 2013-09-19 06:18:27 +0000 | [diff] [blame] | 129 | * Override default setting of fIgnoreFailure. |
| 130 | */ |
| 131 | void setIgnoreFailure(bool val) { this->fIgnoreFailure = val; } |
| 132 | |
| 133 | /** |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 134 | * Returns true iff there are no allowed results. |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 135 | */ |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 136 | bool empty() const { return this->fAllowedResultDigests.empty(); } |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 137 | |
| 138 | /** |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 139 | * Returns true iff resultDigest matches any allowed result, |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 140 | * regardless of fIgnoreFailure. (The caller can check |
| 141 | * that separately.) |
| 142 | */ |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 143 | bool match(GmResultDigest resultDigest) const; |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 144 | |
| 145 | /** |
epoger@google.com | 84a1802 | 2013-02-01 20:39:15 +0000 | [diff] [blame] | 146 | * 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 |
| 149 | * than a single bitmap), returns NULL. |
| 150 | */ |
| 151 | const SkBitmap *asBitmap() const { |
| 152 | return (SkBitmap::kNo_Config == fBitmap.config()) ? NULL : &fBitmap; |
| 153 | } |
| 154 | |
| 155 | /** |
epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 156 | * Return a JSON representation of the expectations. |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 157 | */ |
epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 158 | Json::Value asJsonValue() const; |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 159 | |
| 160 | private: |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 161 | const static bool kDefaultIgnoreFailure = false; |
| 162 | |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 163 | SkTArray<GmResultDigest> fAllowedResultDigests; |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 164 | bool fIgnoreFailure; |
epoger@google.com | 84a1802 | 2013-02-01 20:39:15 +0000 | [diff] [blame] | 165 | SkBitmap fBitmap; |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | /** |
| 169 | * Abstract source of Expectations objects for individual tests. |
| 170 | */ |
| 171 | class ExpectationsSource : public SkRefCnt { |
| 172 | public: |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 173 | SK_DECLARE_INST_COUNT(ExpectationsSource) |
| 174 | |
commit-bot@chromium.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 175 | virtual Expectations get(const char *testName) const = 0; |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 176 | |
| 177 | private: |
| 178 | typedef SkRefCnt INHERITED; |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | /** |
| 182 | * Return Expectations based on individual image files on disk. |
| 183 | */ |
| 184 | class IndividualImageExpectationsSource : public ExpectationsSource { |
| 185 | public: |
| 186 | /** |
| 187 | * Create an ExpectationsSource that will return Expectations based on |
| 188 | * image files found within rootDir. |
| 189 | * |
| 190 | * rootDir: directory under which to look for image files |
| 191 | * (this string will be copied to storage within this object) |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 192 | */ |
commit-bot@chromium.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 193 | explicit IndividualImageExpectationsSource(const char *rootDir) : fRootDir(rootDir) {} |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 194 | |
commit-bot@chromium.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 195 | Expectations get(const char *testName) const SK_OVERRIDE ; |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 196 | |
| 197 | private: |
| 198 | const SkString fRootDir; |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 199 | }; |
| 200 | |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 201 | /** |
| 202 | * Return Expectations based on JSON summary file. |
| 203 | */ |
| 204 | class JsonExpectationsSource : public ExpectationsSource { |
| 205 | public: |
| 206 | /** |
| 207 | * Create an ExpectationsSource that will return Expectations based on |
| 208 | * a JSON file. |
| 209 | * |
| 210 | * jsonPath: path to JSON file to read |
| 211 | */ |
commit-bot@chromium.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 212 | explicit JsonExpectationsSource(const char *jsonPath); |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 213 | |
commit-bot@chromium.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 214 | Expectations get(const char *testName) const SK_OVERRIDE; |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 215 | |
| 216 | private: |
| 217 | |
| 218 | /** |
| 219 | * Read as many bytes as possible (up to maxBytes) from the stream into |
| 220 | * an SkData object. |
| 221 | * |
| 222 | * If the returned SkData contains fewer than maxBytes, then EOF has been |
| 223 | * reached and no more data would be available from subsequent calls. |
| 224 | * (If EOF has already been reached, then this call will return an empty |
| 225 | * SkData object immediately.) |
| 226 | * |
| 227 | * If there are fewer than maxBytes bytes available to read from the |
| 228 | * stream, but the stream has not been closed yet, this call will block |
| 229 | * until there are enough bytes to read or the stream has been closed. |
| 230 | * |
| 231 | * It is up to the caller to call unref() on the returned SkData object |
| 232 | * once the data is no longer needed, so that the underlying buffer will |
| 233 | * be freed. For example: |
| 234 | * |
| 235 | * { |
| 236 | * size_t maxBytes = 256; |
| 237 | * SkAutoDataUnref dataRef(readIntoSkData(stream, maxBytes)); |
| 238 | * if (NULL != dataRef.get()) { |
| 239 | * size_t bytesActuallyRead = dataRef.get()->size(); |
| 240 | * // use the data... |
| 241 | * } |
| 242 | * } |
| 243 | * // underlying buffer has been freed, thanks to auto unref |
| 244 | * |
| 245 | */ |
| 246 | // TODO(epoger): Move this, into SkStream.[cpp|h] as attempted in |
| 247 | // https://codereview.appspot.com/7300071 ? |
scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 248 | // And maybe ReadFileIntoSkData() also? |
| 249 | static SkData* ReadIntoSkData(SkStream &stream, size_t maxBytes); |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 250 | |
| 251 | /** |
scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 252 | * Wrapper around ReadIntoSkData for files: reads the entire file into |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 253 | * an SkData object. |
| 254 | */ |
scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 255 | static SkData* ReadFileIntoSkData(SkFILEStream &stream) { |
| 256 | return ReadIntoSkData(stream, stream.getLength()); |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Read the file contents from jsonPath and parse them into jsonRoot. |
| 261 | * |
| 262 | * Returns true if successful. |
| 263 | */ |
scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 264 | static bool Parse(const char *jsonPath, Json::Value *jsonRoot); |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 265 | |
| 266 | Json::Value fJsonRoot; |
| 267 | Json::Value fJsonExpectedResults; |
| 268 | }; |
| 269 | |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 270 | } |
| 271 | #endif |