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 | 7 |
8 #include "gm_expectations.h" | 8 #include "gm_expectations.h" |
9 #include "SkBitmapHasher.h" | 9 #include "SkBitmapHasher.h" |
10 #include "SkImageDecoder.h" | 10 #include "SkImageDecoder.h" |
11 | 11 |
12 #define DEBUGFAIL_SEE_STDERR SkDEBUGFAIL("see stderr for message") | 12 #define DEBUGFAIL_SEE_STDERR SkDEBUGFAIL("see stderr for message") |
13 | 13 |
14 const static char kJsonKey_ActualResults[] = "actual-results"; | 14 const static char kJsonKey_ActualResults[] = "actual-results"; |
15 const static char kJsonKey_ActualResults_Failed[] = "failed"; | 15 const static char kJsonKey_ActualResults_Failed[] = "failed"; |
16 const static char kJsonKey_ActualResults_FailureIgnored[]= "failure-ignored"; | 16 const static char kJsonKey_ActualResults_FailureIgnored[]= "failure-ignored"; |
17 const static char kJsonKey_ActualResults_NoComparison[] = "no-comparison"; | 17 const static char kJsonKey_ActualResults_NoComparison[] = "no-comparison"; |
18 const static char kJsonKey_ActualResults_Succeeded[] = "succeeded"; | 18 const static char kJsonKey_ActualResults_Succeeded[] = "succeeded"; |
19 const static char kJsonKey_ActualResults_AnyStatus_BitmapHash[] = "bitmap-64bit
MD5"; | 19 const static char kJsonKey_ActualResults_AnyStatus_BitmapHash[] = "bitmap-64bit
MD5"; |
20 | 20 |
21 const static char kJsonKey_ExpectedResults[] = "expected-results"; | 21 const static char kJsonKey_ExpectedResults[] = "expected-results"; |
22 const static char kJsonKey_ExpectedResults_AllowedBitmapHashes[] = "allowed-bitm
ap-64bitMD5s"; | 22 const static char kJsonKey_ExpectedResults_AllowedBitmapHashes[] = "allowed-bitm
ap-64bitMD5s"; |
23 const static char kJsonKey_ExpectedResults_IgnoreFailure[] = "ignore-f
ailure"; | 23 const static char kJsonKey_ExpectedResults_IgnoreFailure[] = "ignore-f
ailure"; |
24 | 24 |
25 namespace skiagm { | 25 namespace skiagm { |
26 | 26 |
| 27 void gm_fprintf(FILE *stream, const char format[], ...) { |
| 28 va_list args; |
| 29 va_start(args, format); |
| 30 fprintf(stream, "GM: "); |
| 31 vfprintf(stream, format, args); |
| 32 va_end(args); |
| 33 } |
| 34 |
| 35 SkString make_filename(const char path[], |
| 36 const char renderModeDescriptor[], |
| 37 const char *name, |
| 38 const char suffix[]) { |
| 39 SkString filename(path); |
| 40 if (filename.endsWith(SkPATH_SEPARATOR)) { |
| 41 filename.remove(filename.size() - 1, 1); |
| 42 } |
| 43 filename.appendf("%c%s%s.%s", SkPATH_SEPARATOR, |
| 44 name, renderModeDescriptor, suffix); |
| 45 return filename; |
| 46 } |
| 47 |
27 // TODO(epoger): This currently assumes that the result SkHashDigest was | 48 // TODO(epoger): This currently assumes that the result SkHashDigest was |
28 // generated as an SkHashDigest of an SkBitmap. We'll need to allow for oth
er | 49 // generated as an SkHashDigest of an SkBitmap. We'll need to allow for oth
er |
29 // hash types to cover non-bitmaps. | 50 // hash types to cover non-bitmaps. |
30 Json::Value ActualResultAsJsonValue(const SkHashDigest& result) { | 51 Json::Value ActualResultAsJsonValue(const SkHashDigest& result) { |
31 Json::Value jsonValue; | 52 Json::Value jsonValue; |
32 jsonValue[kJsonKey_ActualResults_AnyStatus_BitmapHash] = asJsonValue(res
ult); | 53 jsonValue[kJsonKey_ActualResults_AnyStatus_BitmapHash] = asJsonValue(res
ult); |
33 return jsonValue; | 54 return jsonValue; |
34 } | 55 } |
35 | 56 |
36 Json::Value CreateJsonTree(Json::Value expectedResults, | 57 Json::Value CreateJsonTree(Json::Value expectedResults, |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 return Expectations(referenceBitmap); | 174 return Expectations(referenceBitmap); |
154 } else { | 175 } else { |
155 return Expectations(); | 176 return Expectations(); |
156 } | 177 } |
157 } | 178 } |
158 | 179 |
159 | 180 |
160 // JsonExpectationsSource class... | 181 // JsonExpectationsSource class... |
161 | 182 |
162 JsonExpectationsSource::JsonExpectationsSource(const char *jsonPath) { | 183 JsonExpectationsSource::JsonExpectationsSource(const char *jsonPath) { |
163 parse(jsonPath, &fJsonRoot); | 184 Parse(jsonPath, &fJsonRoot); |
164 fJsonExpectedResults = fJsonRoot[kJsonKey_ExpectedResults]; | 185 fJsonExpectedResults = fJsonRoot[kJsonKey_ExpectedResults]; |
165 } | 186 } |
166 | 187 |
167 Expectations JsonExpectationsSource::get(const char *testName) { | 188 Expectations JsonExpectationsSource::get(const char *testName) { |
168 return Expectations(fJsonExpectedResults[testName]); | 189 return Expectations(fJsonExpectedResults[testName]); |
169 } | 190 } |
170 | 191 |
171 /*static*/ SkData* JsonExpectationsSource::readIntoSkData(SkStream &stream,
size_t maxBytes) { | 192 /*static*/ SkData* JsonExpectationsSource::ReadIntoSkData(SkStream &stream,
size_t maxBytes) { |
172 if (0 == maxBytes) { | 193 if (0 == maxBytes) { |
173 return SkData::NewEmpty(); | 194 return SkData::NewEmpty(); |
174 } | 195 } |
175 char* bufStart = reinterpret_cast<char *>(sk_malloc_throw(maxBytes)); | 196 char* bufStart = reinterpret_cast<char *>(sk_malloc_throw(maxBytes)); |
176 char* bufPtr = bufStart; | 197 char* bufPtr = bufStart; |
177 size_t bytesRemaining = maxBytes; | 198 size_t bytesRemaining = maxBytes; |
178 while (bytesRemaining > 0) { | 199 while (bytesRemaining > 0) { |
179 size_t bytesReadThisTime = stream.read(bufPtr, bytesRemaining); | 200 size_t bytesReadThisTime = stream.read(bufPtr, bytesRemaining); |
180 if (0 == bytesReadThisTime) { | 201 if (0 == bytesReadThisTime) { |
181 break; | 202 break; |
182 } | 203 } |
183 bytesRemaining -= bytesReadThisTime; | 204 bytesRemaining -= bytesReadThisTime; |
184 bufPtr += bytesReadThisTime; | 205 bufPtr += bytesReadThisTime; |
185 } | 206 } |
186 return SkData::NewFromMalloc(bufStart, maxBytes - bytesRemaining); | 207 return SkData::NewFromMalloc(bufStart, maxBytes - bytesRemaining); |
187 } | 208 } |
188 | 209 |
189 /*static*/ bool JsonExpectationsSource::parse(const char *jsonPath, Json::Va
lue *jsonRoot) { | 210 /*static*/ bool JsonExpectationsSource::Parse(const char *jsonPath, Json::Va
lue *jsonRoot) { |
190 SkFILEStream inFile(jsonPath); | 211 SkFILEStream inFile(jsonPath); |
191 if (!inFile.isValid()) { | 212 if (!inFile.isValid()) { |
192 gm_fprintf(stderr, "unable to read JSON file %s\n", jsonPath); | 213 gm_fprintf(stderr, "unable to read JSON file %s\n", jsonPath); |
193 DEBUGFAIL_SEE_STDERR; | 214 DEBUGFAIL_SEE_STDERR; |
194 return false; | 215 return false; |
195 } | 216 } |
196 | 217 |
197 SkAutoDataUnref dataRef(readFileIntoSkData(inFile)); | 218 SkAutoDataUnref dataRef(ReadFileIntoSkData(inFile)); |
198 if (NULL == dataRef.get()) { | 219 if (NULL == dataRef.get()) { |
199 gm_fprintf(stderr, "error reading JSON file %s\n", jsonPath); | 220 gm_fprintf(stderr, "error reading JSON file %s\n", jsonPath); |
200 DEBUGFAIL_SEE_STDERR; | 221 DEBUGFAIL_SEE_STDERR; |
201 return false; | 222 return false; |
202 } | 223 } |
203 | 224 |
204 const char *bytes = reinterpret_cast<const char *>(dataRef.get()->data()
); | 225 const char *bytes = reinterpret_cast<const char *>(dataRef.get()->data()
); |
205 size_t size = dataRef.get()->size(); | 226 size_t size = dataRef.get()->size(); |
206 Json::Reader reader; | 227 Json::Reader reader; |
207 if (!reader.parse(bytes, bytes+size, *jsonRoot)) { | 228 if (!reader.parse(bytes, bytes+size, *jsonRoot)) { |
208 gm_fprintf(stderr, "error parsing JSON file %s\n", jsonPath); | 229 gm_fprintf(stderr, "error parsing JSON file %s\n", jsonPath); |
209 DEBUGFAIL_SEE_STDERR; | 230 DEBUGFAIL_SEE_STDERR; |
210 return false; | 231 return false; |
211 } | 232 } |
212 return true; | 233 return true; |
213 } | 234 } |
214 | 235 |
215 } | 236 } |
OLD | NEW |