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 * | |
7 * TODO(epoger): Combine this with tools/image_expectations.cpp, or eliminate on e of the two. | |
6 */ | 8 */ |
7 | 9 |
8 #include "gm_expectations.h" | 10 #include "gm_expectations.h" |
9 #include "SkBitmapHasher.h" | 11 #include "SkBitmapHasher.h" |
12 #include "SkData.h" | |
10 #include "SkImageDecoder.h" | 13 #include "SkImageDecoder.h" |
11 | 14 |
12 #define DEBUGFAIL_SEE_STDERR SkDEBUGFAIL("see stderr for message") | 15 #define DEBUGFAIL_SEE_STDERR SkDEBUGFAIL("see stderr for message") |
13 | 16 |
14 // See gm_json.py for descriptions of each of these JSON keys. | 17 // See gm_json.py for descriptions of each of these JSON keys. |
15 // These constants must be kept in sync with the ones in that Python file! | 18 // These constants must be kept in sync with the ones in that Python file! |
16 const static char kJsonKey_ActualResults[] = "actual-results"; | 19 const static char kJsonKey_ActualResults[] = "actual-results"; |
17 const static char kJsonKey_ActualResults_Failed[] = "failed"; | 20 const static char kJsonKey_ActualResults_Failed[] = "failed"; |
18 const static char kJsonKey_ActualResults_FailureIgnored[]= "failure-ignored"; | 21 const static char kJsonKey_ActualResults_FailureIgnored[]= "failure-ignored"; |
19 const static char kJsonKey_ActualResults_NoComparison[] = "no-comparison"; | 22 const static char kJsonKey_ActualResults_NoComparison[] = "no-comparison"; |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 | 215 |
213 JsonExpectationsSource::JsonExpectationsSource(const char *jsonPath) { | 216 JsonExpectationsSource::JsonExpectationsSource(const char *jsonPath) { |
214 Parse(jsonPath, &fJsonRoot); | 217 Parse(jsonPath, &fJsonRoot); |
215 fJsonExpectedResults = fJsonRoot[kJsonKey_ExpectedResults]; | 218 fJsonExpectedResults = fJsonRoot[kJsonKey_ExpectedResults]; |
216 } | 219 } |
217 | 220 |
218 Expectations JsonExpectationsSource::get(const char *testName) const { | 221 Expectations JsonExpectationsSource::get(const char *testName) const { |
219 return Expectations(fJsonExpectedResults[testName]); | 222 return Expectations(fJsonExpectedResults[testName]); |
220 } | 223 } |
221 | 224 |
222 /*static*/ SkData* JsonExpectationsSource::ReadIntoSkData(SkStream &stream, size_t maxBytes) { | |
223 if (0 == maxBytes) { | |
224 return SkData::NewEmpty(); | |
225 } | |
226 char* bufStart = reinterpret_cast<char *>(sk_malloc_throw(maxBytes)); | |
227 char* bufPtr = bufStart; | |
228 size_t bytesRemaining = maxBytes; | |
229 while (bytesRemaining > 0) { | |
230 size_t bytesReadThisTime = stream.read(bufPtr, bytesRemaining); | |
231 if (0 == bytesReadThisTime) { | |
232 break; | |
233 } | |
234 bytesRemaining -= bytesReadThisTime; | |
235 bufPtr += bytesReadThisTime; | |
236 } | |
237 return SkData::NewFromMalloc(bufStart, maxBytes - bytesRemaining); | |
238 } | |
239 | |
240 /*static*/ bool JsonExpectationsSource::Parse(const char *jsonPath, Json::Va lue *jsonRoot) { | 225 /*static*/ bool JsonExpectationsSource::Parse(const char *jsonPath, Json::Va lue *jsonRoot) { |
241 SkFILEStream inFile(jsonPath); | 226 SkAutoDataUnref dataRef(SkData::NewFromFileName(jsonPath)); |
epoger
2014/05/08 20:17:14
Thanks for the tip! Last time I touched this stuf
| |
242 if (!inFile.isValid()) { | |
243 SkDebugf("unable to read JSON file %s\n", jsonPath); | |
244 DEBUGFAIL_SEE_STDERR; | |
245 return false; | |
246 } | |
247 | |
248 SkAutoDataUnref dataRef(ReadFileIntoSkData(inFile)); | |
249 if (NULL == dataRef.get()) { | 227 if (NULL == dataRef.get()) { |
250 SkDebugf("error reading JSON file %s\n", jsonPath); | 228 SkDebugf("error reading JSON file %s\n", jsonPath); |
251 DEBUGFAIL_SEE_STDERR; | 229 DEBUGFAIL_SEE_STDERR; |
252 return false; | 230 return false; |
253 } | 231 } |
254 | 232 |
255 const char *bytes = reinterpret_cast<const char *>(dataRef.get()->data() ); | 233 const char *bytes = reinterpret_cast<const char *>(dataRef.get()->data() ); |
256 size_t size = dataRef.get()->size(); | 234 size_t size = dataRef.get()->size(); |
257 Json::Reader reader; | 235 Json::Reader reader; |
258 if (!reader.parse(bytes, bytes+size, *jsonRoot)) { | 236 if (!reader.parse(bytes, bytes+size, *jsonRoot)) { |
259 SkDebugf("error parsing JSON file %s\n", jsonPath); | 237 SkDebugf("error parsing JSON file %s\n", jsonPath); |
260 DEBUGFAIL_SEE_STDERR; | 238 DEBUGFAIL_SEE_STDERR; |
261 return false; | 239 return false; |
262 } | 240 } |
263 return true; | 241 return true; |
264 } | 242 } |
265 | 243 |
266 } | 244 } |
OLD | NEW |