| 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> |
| 11 #include "gm.h" | 11 #include "gm.h" |
| 12 #include "SkBitmap.h" | 12 #include "SkBitmap.h" |
| 13 #include "SkBitmapHasher.h" | 13 #include "SkBitmapHasher.h" |
| 14 #include "SkData.h" | 14 #include "SkData.h" |
| 15 #include "SkHashDigest.h" |
| 15 #include "SkImageDecoder.h" | 16 #include "SkImageDecoder.h" |
| 16 #include "SkOSFile.h" | 17 #include "SkOSFile.h" |
| 17 #include "SkRefCnt.h" | 18 #include "SkRefCnt.h" |
| 18 #include "SkStream.h" | 19 #include "SkStream.h" |
| 19 #include "SkTArray.h" | 20 #include "SkTArray.h" |
| 20 | 21 |
| 21 #ifdef SK_BUILD_FOR_WIN | 22 #ifdef SK_BUILD_FOR_WIN |
| 22 // json includes xlocale which generates warning 4530 because we're compilin
g without | 23 // json includes xlocale which generates warning 4530 because we're compilin
g without |
| 23 // exceptions; see https://code.google.com/p/skia/issues/detail?id=1067 | 24 // exceptions; see https://code.google.com/p/skia/issues/detail?id=1067 |
| 24 #pragma warning(push) | 25 #pragma warning(push) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 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_Checksum[] = "checksum"; |
| 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_Checksums[] = "checksums"; |
| 44 const static char kJsonKey_ExpectedResults_IgnoreFailure[] = "ignore-failure"; | 45 const static char kJsonKey_ExpectedResults_IgnoreFailure[] = "ignore-failure"; |
| 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. |
| 50 #ifdef BITMAP_HASH_TYPE_SkHashDigest |
| 51 typedef SkHashDigest Checksum; |
| 52 static inline Json::Value asJsonValue(Checksum checksum) { |
| 53 // TODO(epoger): This creates a new copy of the string representation |
| 54 // of the checksum, by calling Json::duplicateStringValue(). |
| 55 // We may want to investigate calling Json::Value(const StaticString &va
lue) |
| 56 // instead, which eliminates the copy but requires that StaticString |
| 57 // to "stick around". |
| 58 return Json::Value(checksum.toHexString().c_str()); |
| 59 } |
| 60 static inline Checksum asChecksum(Json::Value jsonValue) { |
| 61 SkHashDigest digest; |
| 62 // TODO(epoger): handle error in copyFromHexString()? |
| 63 digest.copyFromHexString(jsonValue.asCString()); |
| 64 return digest; |
| 65 } |
| 66 #else |
| 49 typedef Json::UInt64 Checksum; | 67 typedef Json::UInt64 Checksum; |
| 50 static inline Json::Value asJsonValue(Checksum checksum) { | 68 static inline Json::Value asJsonValue(Checksum checksum) { |
| 51 return checksum; | 69 return checksum; |
| 52 } | 70 } |
| 53 static inline Checksum asChecksum(Json::Value jsonValue) { | 71 static inline Checksum asChecksum(Json::Value jsonValue) { |
| 54 return jsonValue.asUInt64(); | 72 return jsonValue.asUInt64(); |
| 55 } | 73 } |
| 74 #endif |
| 56 | 75 |
| 57 static void gm_fprintf(FILE *stream, const char format[], ...) { | 76 static void gm_fprintf(FILE *stream, const char format[], ...) { |
| 58 va_list args; | 77 va_list args; |
| 59 va_start(args, format); | 78 va_start(args, format); |
| 60 fprintf(stream, "GM: "); | 79 fprintf(stream, "GM: "); |
| 61 vfprintf(stream, format, args); | 80 vfprintf(stream, format, args); |
| 62 va_end(args); | 81 va_end(args); |
| 63 } | 82 } |
| 64 | 83 |
| 65 static SkString make_filename(const char path[], | 84 static SkString make_filename(const char path[], |
| (...skipping 21 matching lines...) Expand all Loading... |
| 87 fIgnoreFailure = ignoreFailure; | 106 fIgnoreFailure = ignoreFailure; |
| 88 } | 107 } |
| 89 | 108 |
| 90 /** | 109 /** |
| 91 * Expect exactly one image (appropriate for the case when we | 110 * Expect exactly one image (appropriate for the case when we |
| 92 * are comparing against a single PNG file). | 111 * are comparing against a single PNG file). |
| 93 */ | 112 */ |
| 94 Expectations(const SkBitmap& bitmap, bool ignoreFailure=kDefaultIgnoreFa
ilure) { | 113 Expectations(const SkBitmap& bitmap, bool ignoreFailure=kDefaultIgnoreFa
ilure) { |
| 95 fBitmap = bitmap; | 114 fBitmap = bitmap; |
| 96 fIgnoreFailure = ignoreFailure; | 115 fIgnoreFailure = ignoreFailure; |
| 97 SkHashDigest digest; | 116 BITMAP_HASH_TYPE digest; |
| 98 // 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 if (!SkBitmapHasher::ComputeDigest(bitmap, &digest)) { | 117 if (!SkBitmapHasher::ComputeDigest(bitmap, &digest)) { |
| 101 digest = 0; | 118 // TODO(epoger): Better handling for error returned by ComputeDi
gest()? |
| 119 // For now, we just leave digest empty... |
| 102 } | 120 } |
| 103 fAllowedChecksums.push_back() = digest; | 121 fAllowedChecksums.push_back() = digest; |
| 104 } | 122 } |
| 105 | 123 |
| 106 /** | 124 /** |
| 107 * Create Expectations from a JSON element as found within the | 125 * Create Expectations from a JSON element as found within the |
| 108 * kJsonKey_ExpectedResults section. | 126 * kJsonKey_ExpectedResults section. |
| 109 * | 127 * |
| 110 * It's fine if the jsonElement is null or empty; in that case, we just | 128 * It's fine if the jsonElement is null or empty; in that case, we just |
| 111 * don't have any expectations. | 129 * don't have any expectations. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 133 // ok, we'll just assume there aren't any expected checksums
to compare against | 151 // ok, we'll just assume there aren't any expected checksums
to compare against |
| 134 } else if (!allowedChecksums.isArray()) { | 152 } else if (!allowedChecksums.isArray()) { |
| 135 gm_fprintf(stderr, "found non-array json value" | 153 gm_fprintf(stderr, "found non-array json value" |
| 136 " for key '%s' in element '%s'\n", | 154 " for key '%s' in element '%s'\n", |
| 137 kJsonKey_ExpectedResults_Checksums, | 155 kJsonKey_ExpectedResults_Checksums, |
| 138 jsonElement.toStyledString().c_str()); | 156 jsonElement.toStyledString().c_str()); |
| 139 DEBUGFAIL_SEE_STDERR; | 157 DEBUGFAIL_SEE_STDERR; |
| 140 } else { | 158 } else { |
| 141 for (Json::ArrayIndex i=0; i<allowedChecksums.size(); i++) { | 159 for (Json::ArrayIndex i=0; i<allowedChecksums.size(); i++) { |
| 142 Json::Value checksumElement = allowedChecksums[i]; | 160 Json::Value checksumElement = allowedChecksums[i]; |
| 161 #ifdef BITMAP_HASH_TYPE_SkHashDigest |
| 162 if (!checksumElement.isString()) { |
| 163 gm_fprintf(stderr, "found non-string checksum" |
| 164 " in json element '%s'\n", |
| 165 jsonElement.toStyledString().c_str()); |
| 166 DEBUGFAIL_SEE_STDERR; |
| 167 } else { |
| 168 fAllowedChecksums.push_back() = asChecksum(checksumE
lement); |
| 169 } |
| 170 #else |
| 143 if (!checksumElement.isIntegral()) { | 171 if (!checksumElement.isIntegral()) { |
| 144 gm_fprintf(stderr, "found non-integer checksum" | 172 gm_fprintf(stderr, "found non-integer checksum" |
| 145 " in json element '%s'\n", | 173 " in json element '%s'\n", |
| 146 jsonElement.toStyledString().c_str()); | 174 jsonElement.toStyledString().c_str()); |
| 147 DEBUGFAIL_SEE_STDERR; | 175 DEBUGFAIL_SEE_STDERR; |
| 148 } else { | 176 } else { |
| 149 fAllowedChecksums.push_back() = asChecksum(checksumE
lement); | 177 fAllowedChecksums.push_back() = asChecksum(checksumE
lement); |
| 150 } | 178 } |
| 179 #endif |
| 151 } | 180 } |
| 152 } | 181 } |
| 153 } | 182 } |
| 154 } | 183 } |
| 155 | 184 |
| 156 /** | 185 /** |
| 157 * Returns true iff we want to ignore failed expectations. | 186 * Returns true iff we want to ignore failed expectations. |
| 158 */ | 187 */ |
| 159 bool ignoreFailure() const { return this->fIgnoreFailure; } | 188 bool ignoreFailure() const { return this->fIgnoreFailure; } |
| 160 | 189 |
| 161 /** | 190 /** |
| 162 * Returns true iff there are no allowed checksums. | 191 * Returns true iff there are no allowed checksums. |
| 163 */ | 192 */ |
| 164 bool empty() const { return this->fAllowedChecksums.empty(); } | 193 bool empty() const { return this->fAllowedChecksums.empty(); } |
| 165 | 194 |
| 166 /** | 195 /** |
| 167 * Returns true iff actualChecksum matches any allowedChecksum, | 196 * Returns true iff actualChecksum matches any allowedChecksum, |
| 168 * regardless of fIgnoreFailure. (The caller can check | 197 * regardless of fIgnoreFailure. (The caller can check |
| 169 * that separately.) | 198 * that separately.) |
| 170 */ | 199 */ |
| 171 bool match(Checksum actualChecksum) const { | 200 bool match(Checksum actualChecksum) const { |
| 172 for (int i=0; i < this->fAllowedChecksums.count(); i++) { | 201 for (int i=0; i < this->fAllowedChecksums.count(); i++) { |
| 173 Checksum allowedChecksum = this->fAllowedChecksums[i]; | 202 BITMAP_HASH_TYPE allowedChecksum = this->fAllowedChecksums[i]; |
| 174 if (allowedChecksum == actualChecksum) { | 203 if (allowedChecksum == actualChecksum) { |
| 175 return true; | 204 return true; |
| 176 } | 205 } |
| 177 } | 206 } |
| 178 return false; | 207 return false; |
| 179 } | 208 } |
| 180 | 209 |
| 181 /** | 210 /** |
| 182 * If this Expectation is based on a single SkBitmap, return a | 211 * If this Expectation is based on a single SkBitmap, return a |
| 183 * pointer to that SkBitmap. Otherwise (if the Expectation is | 212 * pointer to that SkBitmap. Otherwise (if the Expectation is |
| 184 * empty, or if it was based on a list of checksums rather | 213 * empty, or if it was based on a list of checksums rather |
| 185 * than a single bitmap), returns NULL. | 214 * than a single bitmap), returns NULL. |
| 186 */ | 215 */ |
| 187 const SkBitmap *asBitmap() const { | 216 const SkBitmap *asBitmap() const { |
| 188 return (SkBitmap::kNo_Config == fBitmap.config()) ? NULL : &fBitmap; | 217 return (SkBitmap::kNo_Config == fBitmap.config()) ? NULL : &fBitmap; |
| 189 } | 218 } |
| 190 | 219 |
| 191 /** | 220 /** |
| 192 * Return a JSON representation of the allowed checksums. | 221 * Return a JSON representation of the allowed checksums. |
| 193 * This does NOT include any information about whether to | 222 * This does NOT include any information about whether to |
| 194 * ignore failures. | 223 * ignore failures. |
| 195 */ | 224 */ |
| 196 Json::Value allowedChecksumsAsJson() const { | 225 Json::Value allowedChecksumsAsJson() const { |
| 197 Json::Value allowedChecksumArray; | 226 Json::Value allowedChecksumArray; |
| 198 if (!this->fAllowedChecksums.empty()) { | 227 if (!this->fAllowedChecksums.empty()) { |
| 199 for (int i=0; i < this->fAllowedChecksums.count(); i++) { | 228 for (int i=0; i < this->fAllowedChecksums.count(); i++) { |
| 200 Checksum allowedChecksum = this->fAllowedChecksums[i]; | 229 BITMAP_HASH_TYPE allowedChecksum = this->fAllowedChecksums[i
]; |
| 201 allowedChecksumArray.append(asJsonValue(allowedChecksum)); | 230 allowedChecksumArray.append(asJsonValue(allowedChecksum)); |
| 202 } | 231 } |
| 203 } | 232 } |
| 204 return allowedChecksumArray; | 233 return allowedChecksumArray; |
| 205 } | 234 } |
| 206 | 235 |
| 207 private: | 236 private: |
| 208 const static bool kDefaultIgnoreFailure = false; | 237 const static bool kDefaultIgnoreFailure = false; |
| 209 | 238 |
| 210 SkTArray<Checksum> fAllowedChecksums; | 239 SkTArray<BITMAP_HASH_TYPE> fAllowedChecksums; |
| 211 bool fIgnoreFailure; | 240 bool fIgnoreFailure; |
| 212 SkBitmap fBitmap; | 241 SkBitmap fBitmap; |
| 213 }; | 242 }; |
| 214 | 243 |
| 215 /** | 244 /** |
| 216 * Abstract source of Expectations objects for individual tests. | 245 * Abstract source of Expectations objects for individual tests. |
| 217 */ | 246 */ |
| 218 class ExpectationsSource : public SkRefCnt { | 247 class ExpectationsSource : public SkRefCnt { |
| 219 public: | 248 public: |
| 220 virtual Expectations get(const char *testName) = 0; | 249 virtual Expectations get(const char *testName) = 0; |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 } | 392 } |
| 364 return true; | 393 return true; |
| 365 } | 394 } |
| 366 | 395 |
| 367 Json::Value fJsonRoot; | 396 Json::Value fJsonRoot; |
| 368 Json::Value fJsonExpectedResults; | 397 Json::Value fJsonExpectedResults; |
| 369 }; | 398 }; |
| 370 | 399 |
| 371 } | 400 } |
| 372 #endif | 401 #endif |
| OLD | NEW |