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 | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 14 | #include "SkData.h" |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 15 | #include "SkJSONCPP.h" |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 16 | #include "SkOSFile.h" |
| 17 | #include "SkRefCnt.h" |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 18 | #include "SkStream.h" |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 19 | #include "SkTArray.h" |
| 20 | |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 21 | |
| 22 | namespace skiagm { |
| 23 | |
epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 24 | Json::Value CreateJsonTree(Json::Value expectedResults, |
| 25 | Json::Value actualResultsFailed, |
| 26 | Json::Value actualResultsFailureIgnored, |
| 27 | Json::Value actualResultsNoComparison, |
| 28 | Json::Value actualResultsSucceeded); |
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 |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 149 | * than a single bitmap), returns nullptr. |
epoger@google.com | 84a1802 | 2013-02-01 20:39:15 +0000 | [diff] [blame] | 150 | */ |
| 151 | const SkBitmap *asBitmap() const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 152 | return (kUnknown_SkColorType == fBitmap.colorType()) ? nullptr : &fBitmap; |
epoger@google.com | 84a1802 | 2013-02-01 20:39:15 +0000 | [diff] [blame] | 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 | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 173 | virtual Expectations get(const char *testName) const = 0; |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 174 | |
| 175 | private: |
| 176 | typedef SkRefCnt INHERITED; |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 177 | }; |
| 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.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 190 | */ |
commit-bot@chromium.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 191 | explicit IndividualImageExpectationsSource(const char *rootDir) : fRootDir(rootDir) {} |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 192 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 193 | Expectations get(const char *testName) const override ; |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 194 | |
| 195 | private: |
| 196 | const SkString fRootDir; |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 199 | /** |
| 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.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 210 | explicit JsonExpectationsSource(const char *jsonPath); |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 211 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 212 | Expectations get(const char *testName) const override; |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 213 | |
| 214 | private: |
| 215 | |
| 216 | /** |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 217 | * Read the file contents from jsonPath and parse them into jsonRoot. |
| 218 | * |
| 219 | * Returns true if successful. |
| 220 | */ |
scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 221 | static bool Parse(const char *jsonPath, Json::Value *jsonRoot); |
epoger@google.com | d271d24 | 2013-02-13 18:14:48 +0000 | [diff] [blame] | 222 | |
| 223 | Json::Value fJsonRoot; |
| 224 | Json::Value fJsonExpectedResults; |
| 225 | }; |
| 226 | |
epoger@google.com | 3726960 | 2013-01-19 04:21:27 +0000 | [diff] [blame] | 227 | } |
| 228 | #endif |