Chromium Code Reviews| 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" | |
|
epoger
2013/04/18 16:39:34
Patchset 3 is just a checkpoint... it's not time f
| |
| 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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 */ | 181 */ |
| 164 bool empty() const { return this->fAllowedChecksums.empty(); } | 182 bool empty() const { return this->fAllowedChecksums.empty(); } |
| 165 | 183 |
| 166 /** | 184 /** |
| 167 * Returns true iff actualChecksum matches any allowedChecksum, | 185 * Returns true iff actualChecksum matches any allowedChecksum, |
| 168 * regardless of fIgnoreFailure. (The caller can check | 186 * regardless of fIgnoreFailure. (The caller can check |
| 169 * that separately.) | 187 * that separately.) |
| 170 */ | 188 */ |
| 171 bool match(Checksum actualChecksum) const { | 189 bool match(Checksum actualChecksum) const { |
| 172 for (int i=0; i < this->fAllowedChecksums.count(); i++) { | 190 for (int i=0; i < this->fAllowedChecksums.count(); i++) { |
| 173 Checksum allowedChecksum = this->fAllowedChecksums[i]; | 191 BITMAP_HASH_TYPE allowedChecksum = this->fAllowedChecksums[i]; |
| 174 if (allowedChecksum == actualChecksum) { | 192 if (allowedChecksum == actualChecksum) { |
| 175 return true; | 193 return true; |
| 176 } | 194 } |
| 177 } | 195 } |
| 178 return false; | 196 return false; |
| 179 } | 197 } |
| 180 | 198 |
| 181 /** | 199 /** |
| 182 * If this Expectation is based on a single SkBitmap, return a | 200 * If this Expectation is based on a single SkBitmap, return a |
| 183 * pointer to that SkBitmap. Otherwise (if the Expectation is | 201 * pointer to that SkBitmap. Otherwise (if the Expectation is |
| 184 * empty, or if it was based on a list of checksums rather | 202 * empty, or if it was based on a list of checksums rather |
| 185 * than a single bitmap), returns NULL. | 203 * than a single bitmap), returns NULL. |
| 186 */ | 204 */ |
| 187 const SkBitmap *asBitmap() const { | 205 const SkBitmap *asBitmap() const { |
| 188 return (SkBitmap::kNo_Config == fBitmap.config()) ? NULL : &fBitmap; | 206 return (SkBitmap::kNo_Config == fBitmap.config()) ? NULL : &fBitmap; |
| 189 } | 207 } |
| 190 | 208 |
| 191 /** | 209 /** |
| 192 * Return a JSON representation of the allowed checksums. | 210 * Return a JSON representation of the allowed checksums. |
| 193 * This does NOT include any information about whether to | 211 * This does NOT include any information about whether to |
| 194 * ignore failures. | 212 * ignore failures. |
| 195 */ | 213 */ |
| 196 Json::Value allowedChecksumsAsJson() const { | 214 Json::Value allowedChecksumsAsJson() const { |
| 197 Json::Value allowedChecksumArray; | 215 Json::Value allowedChecksumArray; |
| 198 if (!this->fAllowedChecksums.empty()) { | 216 if (!this->fAllowedChecksums.empty()) { |
| 199 for (int i=0; i < this->fAllowedChecksums.count(); i++) { | 217 for (int i=0; i < this->fAllowedChecksums.count(); i++) { |
| 200 Checksum allowedChecksum = this->fAllowedChecksums[i]; | 218 BITMAP_HASH_TYPE allowedChecksum = this->fAllowedChecksums[i ]; |
| 201 allowedChecksumArray.append(asJsonValue(allowedChecksum)); | 219 allowedChecksumArray.append(asJsonValue(allowedChecksum)); |
| 202 } | 220 } |
| 203 } | 221 } |
| 204 return allowedChecksumArray; | 222 return allowedChecksumArray; |
| 205 } | 223 } |
| 206 | 224 |
| 207 private: | 225 private: |
| 208 const static bool kDefaultIgnoreFailure = false; | 226 const static bool kDefaultIgnoreFailure = false; |
| 209 | 227 |
| 210 SkTArray<Checksum> fAllowedChecksums; | 228 SkTArray<BITMAP_HASH_TYPE> fAllowedChecksums; |
| 211 bool fIgnoreFailure; | 229 bool fIgnoreFailure; |
| 212 SkBitmap fBitmap; | 230 SkBitmap fBitmap; |
| 213 }; | 231 }; |
| 214 | 232 |
| 215 /** | 233 /** |
| 216 * Abstract source of Expectations objects for individual tests. | 234 * Abstract source of Expectations objects for individual tests. |
| 217 */ | 235 */ |
| 218 class ExpectationsSource : public SkRefCnt { | 236 class ExpectationsSource : public SkRefCnt { |
| 219 public: | 237 public: |
| 220 virtual Expectations get(const char *testName) = 0; | 238 virtual Expectations get(const char *testName) = 0; |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 363 } | 381 } |
| 364 return true; | 382 return true; |
| 365 } | 383 } |
| 366 | 384 |
| 367 Json::Value fJsonRoot; | 385 Json::Value fJsonRoot; |
| 368 Json::Value fJsonExpectedResults; | 386 Json::Value fJsonExpectedResults; |
| 369 }; | 387 }; |
| 370 | 388 |
| 371 } | 389 } |
| 372 #endif | 390 #endif |
| OLD | NEW |