| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 #ifndef gm_expectations_DEFINED | 7 #ifndef gm_expectations_DEFINED |
| 8 #define gm_expectations_DEFINED | 8 #define gm_expectations_DEFINED |
| 9 | 9 |
| 10 #include <stdarg.h> | 10 #include <stdarg.h> |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #pragma warning(disable : 4530) | 25 #pragma warning(disable : 4530) |
| 26 #endif | 26 #endif |
| 27 #include "json/reader.h" | 27 #include "json/reader.h" |
| 28 #include "json/value.h" | 28 #include "json/value.h" |
| 29 #ifdef SK_BUILD_FOR_WIN | 29 #ifdef SK_BUILD_FOR_WIN |
| 30 #pragma warning(pop) | 30 #pragma warning(pop) |
| 31 #endif | 31 #endif |
| 32 | 32 |
| 33 #define DEBUGFAIL_SEE_STDERR SkDEBUGFAIL("see stderr for message") | 33 #define DEBUGFAIL_SEE_STDERR SkDEBUGFAIL("see stderr for message") |
| 34 | 34 |
| 35 // EPOGER: move these into a .cpp file so we know nobody else uses them? |
| 35 const static char kJsonKey_ActualResults[] = "actual-results"; | 36 const static char kJsonKey_ActualResults[] = "actual-results"; |
| 36 const static char kJsonKey_ActualResults_Failed[] = "failed"; | 37 const static char kJsonKey_ActualResults_Failed[] = "failed"; |
| 37 const static char kJsonKey_ActualResults_FailureIgnored[]= "failure-ignored"; | 38 const static char kJsonKey_ActualResults_FailureIgnored[]= "failure-ignored"; |
| 38 const static char kJsonKey_ActualResults_NoComparison[] = "no-comparison"; | 39 const static char kJsonKey_ActualResults_NoComparison[] = "no-comparison"; |
| 39 const static char kJsonKey_ActualResults_Succeeded[] = "succeeded"; | 40 const static char kJsonKey_ActualResults_Succeeded[] = "succeeded"; |
| 40 const static char kJsonKey_ActualResults_AnyStatus_Checksum[] = "checksum"; | 41 const static char kJsonKey_ActualResults_AnyStatus_BitmapCityhash[] = "bitmap-c
ityhash"; |
| 41 | 42 |
| 42 const static char kJsonKey_ExpectedResults[] = "expected-results"; | 43 const static char kJsonKey_ExpectedResults[] = "expected-results"; |
| 43 const static char kJsonKey_ExpectedResults_Checksums[] = "checksums"; | 44 const static char kJsonKey_ExpectedResults_AllowedBitmapCityhashes[] = "allowed-
bitmap-cityhashes"; |
| 44 const static char kJsonKey_ExpectedResults_IgnoreFailure[] = "ignore-failure"; | 45 const static char kJsonKey_ExpectedResults_IgnoreFailure[] = "ignore-f
ailure"; |
| 45 | 46 |
| 46 namespace skiagm { | 47 namespace skiagm { |
| 47 | 48 |
| 48 // The actual type we use to represent a checksum is hidden in here. | 49 // The actual type we use to represent a checksum is hidden in here. |
| 49 typedef Json::UInt64 Checksum; | 50 typedef Json::UInt64 Checksum; |
| 50 static inline Json::Value asJsonValue(Checksum checksum) { | 51 static inline Json::Value asJsonValue(Checksum checksum) { |
| 51 return checksum; | 52 return checksum; |
| 52 } | 53 } |
| 53 static inline Checksum asChecksum(Json::Value jsonValue) { | 54 static inline Checksum asChecksum(Json::Value jsonValue) { |
| 54 return jsonValue.asUInt64(); | 55 return jsonValue.asUInt64(); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 */ | 94 */ |
| 94 Expectations(const SkBitmap& bitmap, bool ignoreFailure=kDefaultIgnoreFa
ilure) { | 95 Expectations(const SkBitmap& bitmap, bool ignoreFailure=kDefaultIgnoreFa
ilure) { |
| 95 fBitmap = bitmap; | 96 fBitmap = bitmap; |
| 96 fIgnoreFailure = ignoreFailure; | 97 fIgnoreFailure = ignoreFailure; |
| 97 SkHashDigest digest; | 98 SkHashDigest digest; |
| 98 // TODO(epoger): Better handling for error returned by ComputeDigest
()? | 99 // TODO(epoger): Better handling for error returned by ComputeDigest
()? |
| 99 // For now, we just report a digest of 0 in error cases, like before
. | 100 // For now, we just report a digest of 0 in error cases, like before
. |
| 100 if (!SkBitmapHasher::ComputeDigest(bitmap, &digest)) { | 101 if (!SkBitmapHasher::ComputeDigest(bitmap, &digest)) { |
| 101 digest = 0; | 102 digest = 0; |
| 102 } | 103 } |
| 103 fAllowedChecksums.push_back() = digest; | 104 fAllowedBitmapCityhashes.push_back() = digest; |
| 104 } | 105 } |
| 105 | 106 |
| 106 /** | 107 /** |
| 107 * Create Expectations from a JSON element as found within the | 108 * Create Expectations from a JSON element as found within the |
| 108 * kJsonKey_ExpectedResults section. | 109 * kJsonKey_ExpectedResults section. |
| 109 * | 110 * |
| 110 * It's fine if the jsonElement is null or empty; in that case, we just | 111 * It's fine if the jsonElement is null or empty; in that case, we just |
| 111 * don't have any expectations. | 112 * don't have any expectations. |
| 112 */ | 113 */ |
| 113 Expectations(Json::Value jsonElement) { | 114 Expectations(Json::Value jsonElement) { |
| 114 if (jsonElement.empty()) { | 115 if (jsonElement.empty()) { |
| 115 fIgnoreFailure = kDefaultIgnoreFailure; | 116 fIgnoreFailure = kDefaultIgnoreFailure; |
| 116 } else { | 117 } else { |
| 117 Json::Value ignoreFailure = jsonElement[kJsonKey_ExpectedResults
_IgnoreFailure]; | 118 Json::Value ignoreFailure = jsonElement[kJsonKey_ExpectedResults
_IgnoreFailure]; |
| 118 if (ignoreFailure.isNull()) { | 119 if (ignoreFailure.isNull()) { |
| 119 fIgnoreFailure = kDefaultIgnoreFailure; | 120 fIgnoreFailure = kDefaultIgnoreFailure; |
| 120 } else if (!ignoreFailure.isBool()) { | 121 } else if (!ignoreFailure.isBool()) { |
| 121 gm_fprintf(stderr, "found non-boolean json value" | 122 gm_fprintf(stderr, "found non-boolean json value" |
| 122 " for key '%s' in element '%s'\n", | 123 " for key '%s' in element '%s'\n", |
| 123 kJsonKey_ExpectedResults_IgnoreFailure, | 124 kJsonKey_ExpectedResults_IgnoreFailure, |
| 124 jsonElement.toStyledString().c_str()); | 125 jsonElement.toStyledString().c_str()); |
| 125 DEBUGFAIL_SEE_STDERR; | 126 DEBUGFAIL_SEE_STDERR; |
| 126 fIgnoreFailure = kDefaultIgnoreFailure; | 127 fIgnoreFailure = kDefaultIgnoreFailure; |
| 127 } else { | 128 } else { |
| 128 fIgnoreFailure = ignoreFailure.asBool(); | 129 fIgnoreFailure = ignoreFailure.asBool(); |
| 129 } | 130 } |
| 130 | 131 |
| 131 Json::Value allowedChecksums = jsonElement[kJsonKey_ExpectedResu
lts_Checksums]; | 132 Json::Value allowedChecksums = jsonElement[kJsonKey_ExpectedResu
lts_AllowedBitmapCityhashes]; |
| 132 if (allowedChecksums.isNull()) { | 133 if (allowedChecksums.isNull()) { |
| 133 // ok, we'll just assume there aren't any expected checksums
to compare against | 134 // ok, we'll just assume there aren't any expected checksums
to compare against |
| 134 } else if (!allowedChecksums.isArray()) { | 135 } else if (!allowedChecksums.isArray()) { |
| 135 gm_fprintf(stderr, "found non-array json value" | 136 gm_fprintf(stderr, "found non-array json value" |
| 136 " for key '%s' in element '%s'\n", | 137 " for key '%s' in element '%s'\n", |
| 137 kJsonKey_ExpectedResults_Checksums, | 138 kJsonKey_ExpectedResults_AllowedBitmapCityhashes, |
| 138 jsonElement.toStyledString().c_str()); | 139 jsonElement.toStyledString().c_str()); |
| 139 DEBUGFAIL_SEE_STDERR; | 140 DEBUGFAIL_SEE_STDERR; |
| 140 } else { | 141 } else { |
| 141 for (Json::ArrayIndex i=0; i<allowedChecksums.size(); i++) { | 142 for (Json::ArrayIndex i=0; i<allowedChecksums.size(); i++) { |
| 142 Json::Value checksumElement = allowedChecksums[i]; | 143 Json::Value checksumElement = allowedChecksums[i]; |
| 143 if (!checksumElement.isIntegral()) { | 144 if (!checksumElement.isIntegral()) { |
| 144 gm_fprintf(stderr, "found non-integer checksum" | 145 gm_fprintf(stderr, "found non-integer checksum" |
| 145 " in json element '%s'\n", | 146 " in json element '%s'\n", |
| 146 jsonElement.toStyledString().c_str()); | 147 jsonElement.toStyledString().c_str()); |
| 147 DEBUGFAIL_SEE_STDERR; | 148 DEBUGFAIL_SEE_STDERR; |
| 148 } else { | 149 } else { |
| 149 fAllowedChecksums.push_back() = asChecksum(checksumE
lement); | 150 fAllowedBitmapCityhashes.push_back() = asChecksum(ch
ecksumElement); |
| 150 } | 151 } |
| 151 } | 152 } |
| 152 } | 153 } |
| 153 } | 154 } |
| 154 } | 155 } |
| 155 | 156 |
| 156 /** | 157 /** |
| 157 * Returns true iff we want to ignore failed expectations. | 158 * Returns true iff we want to ignore failed expectations. |
| 158 */ | 159 */ |
| 159 bool ignoreFailure() const { return this->fIgnoreFailure; } | 160 bool ignoreFailure() const { return this->fIgnoreFailure; } |
| 160 | 161 |
| 161 /** | 162 /** |
| 162 * Returns true iff there are no allowed checksums. | 163 * Returns true iff there are no allowed checksums. |
| 163 */ | 164 */ |
| 164 bool empty() const { return this->fAllowedChecksums.empty(); } | 165 bool empty() const { return this->fAllowedBitmapCityhashes.empty(); } |
| 165 | 166 |
| 166 /** | 167 /** |
| 167 * Returns true iff actualChecksum matches any allowedChecksum, | 168 * Returns true iff actualChecksum matches any allowedChecksum, |
| 168 * regardless of fIgnoreFailure. (The caller can check | 169 * regardless of fIgnoreFailure. (The caller can check |
| 169 * that separately.) | 170 * that separately.) |
| 170 */ | 171 */ |
| 171 bool match(Checksum actualChecksum) const { | 172 bool match(Checksum actualChecksum) const { |
| 172 for (int i=0; i < this->fAllowedChecksums.count(); i++) { | 173 for (int i=0; i < this->fAllowedBitmapCityhashes.count(); i++) { |
| 173 Checksum allowedChecksum = this->fAllowedChecksums[i]; | 174 Checksum allowedChecksum = this->fAllowedBitmapCityhashes[i]; |
| 174 if (allowedChecksum == actualChecksum) { | 175 if (allowedChecksum == actualChecksum) { |
| 175 return true; | 176 return true; |
| 176 } | 177 } |
| 177 } | 178 } |
| 178 return false; | 179 return false; |
| 179 } | 180 } |
| 180 | 181 |
| 181 /** | 182 /** |
| 182 * If this Expectation is based on a single SkBitmap, return a | 183 * If this Expectation is based on a single SkBitmap, return a |
| 183 * pointer to that SkBitmap. Otherwise (if the Expectation is | 184 * pointer to that SkBitmap. Otherwise (if the Expectation is |
| 184 * empty, or if it was based on a list of checksums rather | 185 * empty, or if it was based on a list of checksums rather |
| 185 * than a single bitmap), returns NULL. | 186 * than a single bitmap), returns NULL. |
| 186 */ | 187 */ |
| 187 const SkBitmap *asBitmap() const { | 188 const SkBitmap *asBitmap() const { |
| 188 return (SkBitmap::kNo_Config == fBitmap.config()) ? NULL : &fBitmap; | 189 return (SkBitmap::kNo_Config == fBitmap.config()) ? NULL : &fBitmap; |
| 189 } | 190 } |
| 190 | 191 |
| 191 /** | 192 /** |
| 192 * Return a JSON representation of the allowed checksums. | 193 * Return a JSON representation of the allowed checksums. |
| 193 * This does NOT include any information about whether to | 194 * This does NOT include any information about whether to |
| 194 * ignore failures. | 195 * ignore failures. |
| 195 */ | 196 */ |
| 196 Json::Value allowedChecksumsAsJson() const { | 197 Json::Value allowedChecksumsAsJson() const { |
| 197 Json::Value allowedChecksumArray; | 198 Json::Value allowedChecksumArray; |
| 198 if (!this->fAllowedChecksums.empty()) { | 199 if (!this->fAllowedBitmapCityhashes.empty()) { |
| 199 for (int i=0; i < this->fAllowedChecksums.count(); i++) { | 200 for (int i=0; i < this->fAllowedBitmapCityhashes.count(); i++) { |
| 200 Checksum allowedChecksum = this->fAllowedChecksums[i]; | 201 Checksum allowedChecksum = this->fAllowedBitmapCityhashes[i]
; |
| 201 allowedChecksumArray.append(asJsonValue(allowedChecksum)); | 202 allowedChecksumArray.append(asJsonValue(allowedChecksum)); |
| 202 } | 203 } |
| 203 } | 204 } |
| 204 return allowedChecksumArray; | 205 return allowedChecksumArray; |
| 205 } | 206 } |
| 206 | 207 |
| 207 private: | 208 private: |
| 208 const static bool kDefaultIgnoreFailure = false; | 209 const static bool kDefaultIgnoreFailure = false; |
| 209 | 210 |
| 210 SkTArray<Checksum> fAllowedChecksums; | 211 SkTArray<Checksum> fAllowedBitmapCityhashes; |
| 211 bool fIgnoreFailure; | 212 bool fIgnoreFailure; |
| 212 SkBitmap fBitmap; | 213 SkBitmap fBitmap; |
| 213 }; | 214 }; |
| 214 | 215 |
| 215 /** | 216 /** |
| 216 * Abstract source of Expectations objects for individual tests. | 217 * Abstract source of Expectations objects for individual tests. |
| 217 */ | 218 */ |
| 218 class ExpectationsSource : public SkRefCnt { | 219 class ExpectationsSource : public SkRefCnt { |
| 219 public: | 220 public: |
| 220 virtual Expectations get(const char *testName) = 0; | 221 virtual Expectations get(const char *testName) = 0; |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 } | 364 } |
| 364 return true; | 365 return true; |
| 365 } | 366 } |
| 366 | 367 |
| 367 Json::Value fJsonRoot; | 368 Json::Value fJsonRoot; |
| 368 Json::Value fJsonExpectedResults; | 369 Json::Value fJsonExpectedResults; |
| 369 }; | 370 }; |
| 370 | 371 |
| 371 } | 372 } |
| 372 #endif | 373 #endif |
| OLD | NEW |