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 // EPOGER: misc cleanup (including 100 char wraps) | |
15 namespace skiagm { | |
16 | |
17 Expectations::Expectations(bool ignoreFailure) { | |
18 fIgnoreFailure = ignoreFailure; | |
19 } | |
20 | |
21 Expectations::Expectations(const SkBitmap& bitmap, bool ignoreFailure) { | |
epoger
2013/04/25 17:23:20
patchset 2: moved most implementation details out
| |
22 fBitmap = bitmap; | |
23 fIgnoreFailure = ignoreFailure; | |
24 SkHashDigest digest; | |
25 // TODO(epoger): Better handling for error returned by ComputeDigest()? | |
26 // For now, we just report a digest of 0 in error cases, like before. | |
27 if (!SkBitmapHasher::ComputeDigest(bitmap, &digest)) { | |
28 digest = 0; | |
29 } | |
30 fAllowedBitmapCityhashes.push_back() = digest; | |
31 } | |
32 | |
33 Expectations::Expectations(Json::Value jsonElement) { | |
34 if (jsonElement.empty()) { | |
35 fIgnoreFailure = kDefaultIgnoreFailure; | |
36 } else { | |
37 Json::Value ignoreFailure = jsonElement[kJsonKey_ExpectedResults_Ign oreFailure]; | |
38 if (ignoreFailure.isNull()) { | |
39 fIgnoreFailure = kDefaultIgnoreFailure; | |
40 } else if (!ignoreFailure.isBool()) { | |
41 gm_fprintf(stderr, "found non-boolean json value" | |
42 " for key '%s' in element '%s'\n", | |
43 kJsonKey_ExpectedResults_IgnoreFailure, | |
44 jsonElement.toStyledString().c_str()); | |
45 DEBUGFAIL_SEE_STDERR; | |
46 fIgnoreFailure = kDefaultIgnoreFailure; | |
47 } else { | |
48 fIgnoreFailure = ignoreFailure.asBool(); | |
49 } | |
50 | |
51 Json::Value allowedChecksums = jsonElement[kJsonKey_ExpectedResults_ AllowedBitmapCityhashes]; | |
52 if (allowedChecksums.isNull()) { | |
53 // ok, we'll just assume there aren't any expected checksums to compare against | |
54 } else if (!allowedChecksums.isArray()) { | |
55 gm_fprintf(stderr, "found non-array json value" | |
56 " for key '%s' in element '%s'\n", | |
57 kJsonKey_ExpectedResults_AllowedBitmapCityhashes, | |
58 jsonElement.toStyledString().c_str()); | |
59 DEBUGFAIL_SEE_STDERR; | |
60 } else { | |
61 for (Json::ArrayIndex i=0; i<allowedChecksums.size(); i++) { | |
62 Json::Value checksumElement = allowedChecksums[i]; | |
63 if (!checksumElement.isIntegral()) { | |
64 gm_fprintf(stderr, "found non-integer checksum" | |
65 " in json element '%s'\n", | |
66 jsonElement.toStyledString().c_str()); | |
67 DEBUGFAIL_SEE_STDERR; | |
68 } else { | |
69 fAllowedBitmapCityhashes.push_back() = asChecksum(checks umElement); | |
70 } | |
71 } | |
72 } | |
73 } | |
74 } | |
75 | |
76 bool Expectations::match(Checksum actualChecksum) const { | |
77 for (int i=0; i < this->fAllowedBitmapCityhashes.count(); i++) { | |
78 Checksum allowedChecksum = this->fAllowedBitmapCityhashes[i]; | |
79 if (allowedChecksum == actualChecksum) { | |
80 return true; | |
81 } | |
82 } | |
83 return false; | |
84 } | |
85 | |
86 Json::Value Expectations::allowedChecksumsAsJson() const { | |
87 Json::Value allowedChecksumArray; | |
88 if (!this->fAllowedBitmapCityhashes.empty()) { | |
89 for (int i=0; i < this->fAllowedBitmapCityhashes.count(); i++) { | |
90 Checksum allowedChecksum = this->fAllowedBitmapCityhashes[i]; | |
91 allowedChecksumArray.append(asJsonValue(allowedChecksum)); | |
92 } | |
93 } | |
94 return allowedChecksumArray; | |
95 } | |
96 | |
97 Expectations IndividualImageExpectationsSource::get(const char *testName) SK _OVERRIDE { | |
98 SkString path = make_filename(fRootDir.c_str(), "", testName, | |
99 "png"); | |
100 SkBitmap referenceBitmap; | |
101 bool decodedReferenceBitmap = | |
102 SkImageDecoder::DecodeFile(path.c_str(), &referenceBitmap, | |
103 SkBitmap::kARGB_8888_Config, | |
104 SkImageDecoder::kDecodePixels_Mode, | |
105 NULL); | |
106 if (decodedReferenceBitmap) { | |
107 return Expectations(referenceBitmap); | |
108 } else { | |
109 return Expectations(); | |
110 } | |
111 } | |
112 | |
113 JsonExpectationsSource::JsonExpectationsSource(const char *jsonPath) { | |
114 parse(jsonPath, &fJsonRoot); | |
115 fJsonExpectedResults = fJsonRoot[kJsonKey_ExpectedResults]; | |
116 } | |
117 | |
118 Expectations JsonExpectationsSource::get(const char *testName) SK_OVERRIDE { | |
119 return Expectations(fJsonExpectedResults[testName]); | |
120 } | |
121 | |
122 /*static*/ SkData* JsonExpectationsSource::readIntoSkData(SkStream &stream, size_t maxBytes) { | |
123 if (0 == maxBytes) { | |
124 return SkData::NewEmpty(); | |
125 } | |
126 char* bufStart = reinterpret_cast<char *>(sk_malloc_throw(maxBytes)); | |
127 char* bufPtr = bufStart; | |
128 size_t bytesRemaining = maxBytes; | |
129 while (bytesRemaining > 0) { | |
130 size_t bytesReadThisTime = stream.read(bufPtr, bytesRemaining); | |
131 if (0 == bytesReadThisTime) { | |
132 break; | |
133 } | |
134 bytesRemaining -= bytesReadThisTime; | |
135 bufPtr += bytesReadThisTime; | |
136 } | |
137 return SkData::NewFromMalloc(bufStart, maxBytes - bytesRemaining); | |
138 } | |
139 | |
140 /*static*/ bool JsonExpectationsSource::parse(const char *jsonPath, Json::Va lue *jsonRoot) { | |
141 SkFILEStream inFile(jsonPath); | |
142 if (!inFile.isValid()) { | |
143 gm_fprintf(stderr, "unable to read JSON file %s\n", jsonPath); | |
144 DEBUGFAIL_SEE_STDERR; | |
145 return false; | |
146 } | |
147 | |
148 SkAutoDataUnref dataRef(readFileIntoSkData(inFile)); | |
149 if (NULL == dataRef.get()) { | |
150 gm_fprintf(stderr, "error reading JSON file %s\n", jsonPath); | |
151 DEBUGFAIL_SEE_STDERR; | |
152 return false; | |
153 } | |
154 | |
155 const char *bytes = reinterpret_cast<const char *>(dataRef.get()->data() ); | |
156 size_t size = dataRef.get()->size(); | |
157 Json::Reader reader; | |
158 if (!reader.parse(bytes, bytes+size, *jsonRoot)) { | |
159 gm_fprintf(stderr, "error parsing JSON file %s\n", jsonPath); | |
160 DEBUGFAIL_SEE_STDERR; | |
161 return false; | |
162 } | |
163 return true; | |
164 } | |
165 | |
166 } | |
OLD | NEW |