OLD | NEW |
(Empty) | |
| 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 |
| 8 #include "gm_expectations.h" |
| 9 #include "SkBitmapHasher.h" |
| 10 #include "SkImageDecoder.h" |
| 11 |
| 12 #define DEBUGFAIL_SEE_STDERR SkDEBUGFAIL("see stderr for message") |
| 13 |
| 14 const static char kJsonKey_ActualResults[] = "actual-results"; |
| 15 const static char kJsonKey_ActualResults_Failed[] = "failed"; |
| 16 const static char kJsonKey_ActualResults_FailureIgnored[]= "failure-ignored"; |
| 17 const static char kJsonKey_ActualResults_NoComparison[] = "no-comparison"; |
| 18 const static char kJsonKey_ActualResults_Succeeded[] = "succeeded"; |
| 19 const static char kJsonKey_ActualResults_AnyStatus_BitmapCityhash[] = "bitmap-c
ityhash"; |
| 20 |
| 21 const static char kJsonKey_ExpectedResults[] = "expected-results"; |
| 22 const static char kJsonKey_ExpectedResults_AllowedBitmapCityhashes[] = "allowed-
bitmap-cityhashes"; |
| 23 const static char kJsonKey_ExpectedResults_IgnoreFailure[] = "ignore-f
ailure"; |
| 24 |
| 25 namespace skiagm { |
| 26 |
| 27 // TODO(epoger): This currently assumes that the result SkHashDigest was |
| 28 // generated as a CityHash of an SkBitmap. We'll need to allow for other |
| 29 // hash types to cover non-bitmaps, MD5 instead of CityHash, etc. |
| 30 Json::Value ActualResultAsJsonValue(const SkHashDigest& result) { |
| 31 Json::Value jsonValue; |
| 32 jsonValue[kJsonKey_ActualResults_AnyStatus_BitmapCityhash] = asJsonValue
(result); |
| 33 return jsonValue; |
| 34 } |
| 35 |
| 36 Json::Value CreateJsonTree(Json::Value expectedResults, |
| 37 Json::Value actualResultsFailed, |
| 38 Json::Value actualResultsFailureIgnored, |
| 39 Json::Value actualResultsNoComparison, |
| 40 Json::Value actualResultsSucceeded) { |
| 41 Json::Value actualResults; |
| 42 actualResults[kJsonKey_ActualResults_Failed] = actualResultsFailed; |
| 43 actualResults[kJsonKey_ActualResults_FailureIgnored] = actualResultsFail
ureIgnored; |
| 44 actualResults[kJsonKey_ActualResults_NoComparison] = actualResultsNoComp
arison; |
| 45 actualResults[kJsonKey_ActualResults_Succeeded] = actualResultsSucceeded
; |
| 46 Json::Value root; |
| 47 root[kJsonKey_ActualResults] = actualResults; |
| 48 root[kJsonKey_ExpectedResults] = expectedResults; |
| 49 return root; |
| 50 } |
| 51 |
| 52 |
| 53 // Expectations class... |
| 54 |
| 55 Expectations::Expectations(bool ignoreFailure) { |
| 56 fIgnoreFailure = ignoreFailure; |
| 57 } |
| 58 |
| 59 Expectations::Expectations(const SkBitmap& bitmap, bool ignoreFailure) { |
| 60 fBitmap = bitmap; |
| 61 fIgnoreFailure = ignoreFailure; |
| 62 SkHashDigest digest; |
| 63 // TODO(epoger): Better handling for error returned by ComputeDigest()? |
| 64 // For now, we just report a digest of 0 in error cases, like before. |
| 65 if (!SkBitmapHasher::ComputeDigest(bitmap, &digest)) { |
| 66 digest = 0; |
| 67 } |
| 68 fAllowedBitmapCityhashes.push_back() = digest; |
| 69 } |
| 70 |
| 71 Expectations::Expectations(Json::Value jsonElement) { |
| 72 if (jsonElement.empty()) { |
| 73 fIgnoreFailure = kDefaultIgnoreFailure; |
| 74 } else { |
| 75 Json::Value ignoreFailure = jsonElement[kJsonKey_ExpectedResults_Ign
oreFailure]; |
| 76 if (ignoreFailure.isNull()) { |
| 77 fIgnoreFailure = kDefaultIgnoreFailure; |
| 78 } else if (!ignoreFailure.isBool()) { |
| 79 gm_fprintf(stderr, "found non-boolean json value" |
| 80 " for key '%s' in element '%s'\n", |
| 81 kJsonKey_ExpectedResults_IgnoreFailure, |
| 82 jsonElement.toStyledString().c_str()); |
| 83 DEBUGFAIL_SEE_STDERR; |
| 84 fIgnoreFailure = kDefaultIgnoreFailure; |
| 85 } else { |
| 86 fIgnoreFailure = ignoreFailure.asBool(); |
| 87 } |
| 88 |
| 89 Json::Value allowedChecksums = |
| 90 jsonElement[kJsonKey_ExpectedResults_AllowedBitmapCityhashes]; |
| 91 if (allowedChecksums.isNull()) { |
| 92 // ok, we'll just assume there aren't any expected checksums to
compare against |
| 93 } else if (!allowedChecksums.isArray()) { |
| 94 gm_fprintf(stderr, "found non-array json value" |
| 95 " for key '%s' in element '%s'\n", |
| 96 kJsonKey_ExpectedResults_AllowedBitmapCityhashes, |
| 97 jsonElement.toStyledString().c_str()); |
| 98 DEBUGFAIL_SEE_STDERR; |
| 99 } else { |
| 100 for (Json::ArrayIndex i=0; i<allowedChecksums.size(); i++) { |
| 101 Json::Value checksumElement = allowedChecksums[i]; |
| 102 if (!checksumElement.isIntegral()) { |
| 103 gm_fprintf(stderr, "found non-integer checksum" |
| 104 " in json element '%s'\n", |
| 105 jsonElement.toStyledString().c_str()); |
| 106 DEBUGFAIL_SEE_STDERR; |
| 107 } else { |
| 108 fAllowedBitmapCityhashes.push_back() = asChecksum(checks
umElement); |
| 109 } |
| 110 } |
| 111 } |
| 112 } |
| 113 } |
| 114 |
| 115 bool Expectations::match(Checksum actualChecksum) const { |
| 116 for (int i=0; i < this->fAllowedBitmapCityhashes.count(); i++) { |
| 117 Checksum allowedChecksum = this->fAllowedBitmapCityhashes[i]; |
| 118 if (allowedChecksum == actualChecksum) { |
| 119 return true; |
| 120 } |
| 121 } |
| 122 return false; |
| 123 } |
| 124 |
| 125 Json::Value Expectations::asJsonValue() const { |
| 126 Json::Value allowedChecksumArray; |
| 127 if (!this->fAllowedBitmapCityhashes.empty()) { |
| 128 for (int i=0; i < this->fAllowedBitmapCityhashes.count(); i++) { |
| 129 Checksum allowedChecksum = this->fAllowedBitmapCityhashes[i]; |
| 130 allowedChecksumArray.append(Json::UInt64(allowedChecksum)); |
| 131 } |
| 132 } |
| 133 |
| 134 Json::Value jsonValue; |
| 135 jsonValue[kJsonKey_ExpectedResults_AllowedBitmapCityhashes] = allowedChe
cksumArray; |
| 136 jsonValue[kJsonKey_ExpectedResults_IgnoreFailure] = this->ignoreFailure(
); |
| 137 return jsonValue; |
| 138 } |
| 139 |
| 140 |
| 141 // IndividualImageExpectationsSource class... |
| 142 |
| 143 Expectations IndividualImageExpectationsSource::get(const char *testName) { |
| 144 SkString path = make_filename(fRootDir.c_str(), "", testName, |
| 145 "png"); |
| 146 SkBitmap referenceBitmap; |
| 147 bool decodedReferenceBitmap = |
| 148 SkImageDecoder::DecodeFile(path.c_str(), &referenceBitmap, |
| 149 SkBitmap::kARGB_8888_Config, |
| 150 SkImageDecoder::kDecodePixels_Mode, |
| 151 NULL); |
| 152 if (decodedReferenceBitmap) { |
| 153 return Expectations(referenceBitmap); |
| 154 } else { |
| 155 return Expectations(); |
| 156 } |
| 157 } |
| 158 |
| 159 |
| 160 // JsonExpectationsSource class... |
| 161 |
| 162 JsonExpectationsSource::JsonExpectationsSource(const char *jsonPath) { |
| 163 parse(jsonPath, &fJsonRoot); |
| 164 fJsonExpectedResults = fJsonRoot[kJsonKey_ExpectedResults]; |
| 165 } |
| 166 |
| 167 Expectations JsonExpectationsSource::get(const char *testName) { |
| 168 return Expectations(fJsonExpectedResults[testName]); |
| 169 } |
| 170 |
| 171 /*static*/ SkData* JsonExpectationsSource::readIntoSkData(SkStream &stream,
size_t maxBytes) { |
| 172 if (0 == maxBytes) { |
| 173 return SkData::NewEmpty(); |
| 174 } |
| 175 char* bufStart = reinterpret_cast<char *>(sk_malloc_throw(maxBytes)); |
| 176 char* bufPtr = bufStart; |
| 177 size_t bytesRemaining = maxBytes; |
| 178 while (bytesRemaining > 0) { |
| 179 size_t bytesReadThisTime = stream.read(bufPtr, bytesRemaining); |
| 180 if (0 == bytesReadThisTime) { |
| 181 break; |
| 182 } |
| 183 bytesRemaining -= bytesReadThisTime; |
| 184 bufPtr += bytesReadThisTime; |
| 185 } |
| 186 return SkData::NewFromMalloc(bufStart, maxBytes - bytesRemaining); |
| 187 } |
| 188 |
| 189 /*static*/ bool JsonExpectationsSource::parse(const char *jsonPath, Json::Va
lue *jsonRoot) { |
| 190 SkFILEStream inFile(jsonPath); |
| 191 if (!inFile.isValid()) { |
| 192 gm_fprintf(stderr, "unable to read JSON file %s\n", jsonPath); |
| 193 DEBUGFAIL_SEE_STDERR; |
| 194 return false; |
| 195 } |
| 196 |
| 197 SkAutoDataUnref dataRef(readFileIntoSkData(inFile)); |
| 198 if (NULL == dataRef.get()) { |
| 199 gm_fprintf(stderr, "error reading JSON file %s\n", jsonPath); |
| 200 DEBUGFAIL_SEE_STDERR; |
| 201 return false; |
| 202 } |
| 203 |
| 204 const char *bytes = reinterpret_cast<const char *>(dataRef.get()->data()
); |
| 205 size_t size = dataRef.get()->size(); |
| 206 Json::Reader reader; |
| 207 if (!reader.parse(bytes, bytes+size, *jsonRoot)) { |
| 208 gm_fprintf(stderr, "error parsing JSON file %s\n", jsonPath); |
| 209 DEBUGFAIL_SEE_STDERR; |
| 210 return false; |
| 211 } |
| 212 return true; |
| 213 } |
| 214 |
| 215 } |
OLD | NEW |