OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkBitmapHasher.h" | 9 #include "SkBitmapHasher.h" |
10 #include "SkData.h" | 10 #include "SkData.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 return fImageDigestRef.get(); | 88 return fImageDigestRef.get(); |
89 } | 89 } |
90 | 90 |
91 const SkBitmap *BitmapAndDigest::getBitmapPtr() const { | 91 const SkBitmap *BitmapAndDigest::getBitmapPtr() const { |
92 return &fBitmap; | 92 return &fBitmap; |
93 } | 93 } |
94 | 94 |
95 // ImageResultsAndExpectations class... | 95 // ImageResultsAndExpectations class... |
96 | 96 |
97 bool ImageResultsAndExpectations::readExpectationsFile(const char *jsonPath) { | 97 bool ImageResultsAndExpectations::readExpectationsFile(const char *jsonPath) { |
98 if (Parse(jsonPath, &fExpectedJsonRoot)) { | 98 if (NULL == jsonPath) { |
99 fExpectedResults = fExpectedJsonRoot[kJsonKey_ExpectedResults]; | 99 SkDebugf("JSON expectations filename not specified\n"); |
100 return true; | |
101 } else { | |
102 return false; | 100 return false; |
103 } | 101 } |
102 SkFILE* filePtr = sk_fopen(jsonPath, kRead_SkFILE_Flag); | |
103 if (NULL == filePtr) { | |
104 SkDebugf("JSON expectations file '%s' does not exist\n", jsonPath); | |
105 return false; | |
106 } | |
107 size_t size = sk_fgetsize(filePtr); | |
108 if (0 == size) { | |
109 SkDebugf("JSON expectations file '%s' is empty, so no expectations\n ", jsonPath); | |
110 sk_fclose(filePtr); | |
111 fExpectedResults.clear(); | |
112 return true; | |
113 } | |
114 bool parsedJson = Parse(filePtr, &fExpectedJsonRoot); | |
115 sk_fclose(filePtr); | |
116 if (!parsedJson) { | |
117 SkDebugf("Failed to parse JSON expectations file '%s'\n", jsonPath); | |
118 return false; | |
119 } | |
120 Json::Value header = fExpectedJsonRoot[kJsonKey_Header]; | |
121 Json::Value headerType = header[kJsonKey_Header_Type]; | |
122 Json::Value headerRevision = header[kJsonKey_Header_Revision]; | |
123 if (strcmp(headerType.asCString(), kJsonValue_Header_Type)) { | |
epoger
2014/05/16 21:02:32
While I was in here, added validation of headerTyp
scroggo
2014/05/19 13:28:32
What would cause one of these to fail?
epoger
2014/05/19 14:52:00
If you mean "what would cause one of these if-stat
| |
124 SkDebugf("JSON expectations file '%s': expected headerType '%s', fou nd '%s'\n", | |
125 jsonPath, kJsonValue_Header_Type, headerType.asCString()); | |
126 return false; | |
127 } | |
128 if (headerRevision.asInt() != kJsonValue_Header_Revision) { | |
129 SkDebugf("JSON expectations file '%s': expected headerRevision %d, f ound %d\n", | |
130 jsonPath, kJsonValue_Header_Revision, headerRevision.asInt( )); | |
131 return false; | |
132 } | |
133 fExpectedResults = fExpectedJsonRoot[kJsonKey_ExpectedResults]; | |
134 return true; | |
104 } | 135 } |
105 | 136 |
106 void ImageResultsAndExpectations::add(const char *sourceName, const char *fi leName, | 137 void ImageResultsAndExpectations::add(const char *sourceName, const char *fi leName, |
107 const ImageDigest &digest, const int * tileNumber) { | 138 const ImageDigest &digest, const int * tileNumber) { |
108 // Get expectation, if any. | 139 // Get expectation, if any. |
109 Json::Value expectedImage; | 140 Json::Value expectedImage; |
110 if (!fExpectedResults.isNull()) { | 141 if (!fExpectedResults.isNull()) { |
111 if (NULL == tileNumber) { | 142 if (NULL == tileNumber) { |
112 expectedImage = fExpectedResults[sourceName][kJsonKey_Source_Who leImage]; | 143 expectedImage = fExpectedResults[sourceName][kJsonKey_Source_Who leImage]; |
113 } else { | 144 } else { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 header[kJsonKey_Header_Type] = kJsonValue_Header_Type; | 205 header[kJsonKey_Header_Type] = kJsonValue_Header_Type; |
175 header[kJsonKey_Header_Revision] = kJsonValue_Header_Revision; | 206 header[kJsonKey_Header_Revision] = kJsonValue_Header_Revision; |
176 Json::Value root; | 207 Json::Value root; |
177 root[kJsonKey_Header] = header; | 208 root[kJsonKey_Header] = header; |
178 root[kJsonKey_ActualResults] = fActualResults; | 209 root[kJsonKey_ActualResults] = fActualResults; |
179 std::string jsonStdString = root.toStyledString(); | 210 std::string jsonStdString = root.toStyledString(); |
180 SkFILEWStream stream(filename); | 211 SkFILEWStream stream(filename); |
181 stream.write(jsonStdString.c_str(), jsonStdString.length()); | 212 stream.write(jsonStdString.c_str(), jsonStdString.length()); |
182 } | 213 } |
183 | 214 |
184 /*static*/ bool ImageResultsAndExpectations::Parse(const char *jsonPath, | 215 /*static*/ bool ImageResultsAndExpectations::Parse(SkFILE *filePtr, |
185 Json::Value *jsonRoot) { | 216 Json::Value *jsonRoot) { |
186 SkAutoDataUnref dataRef(SkData::NewFromFileName(jsonPath)); | 217 SkAutoDataUnref dataRef(SkData::NewFromFILE(filePtr)); |
scroggo
2014/05/19 13:28:32
What's the motivation for the change to make the c
epoger
2014/05/19 14:52:00
I made this change so that the empty file case cou
scroggo
2014/05/19 15:07:38
The new way looks good to me.
| |
187 if (NULL == dataRef.get()) { | 218 if (NULL == dataRef.get()) { |
188 SkDebugf("error reading JSON file %s\n", jsonPath); | |
189 return false; | 219 return false; |
190 } | 220 } |
191 | 221 |
192 const char *bytes = reinterpret_cast<const char *>(dataRef.get()->data() ); | 222 const char *bytes = reinterpret_cast<const char *>(dataRef.get()->data() ); |
193 size_t size = dataRef.get()->size(); | 223 size_t size = dataRef.get()->size(); |
194 Json::Reader reader; | 224 Json::Reader reader; |
195 if (!reader.parse(bytes, bytes+size, *jsonRoot)) { | 225 if (!reader.parse(bytes, bytes+size, *jsonRoot)) { |
196 SkDebugf("error parsing JSON file %s\n", jsonPath); | |
197 return false; | 226 return false; |
198 } | 227 } |
199 | 228 |
200 return true; | 229 return true; |
201 } | 230 } |
202 | 231 |
203 } // namespace sk_tools | 232 } // namespace sk_tools |
OLD | NEW |