2013-01-19 04:21:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
#ifndef gm_expectations_DEFINED
|
|
|
|
#define gm_expectations_DEFINED
|
|
|
|
|
|
|
|
#include "gm.h"
|
2013-02-01 20:39:15 +00:00
|
|
|
#include "SkBitmap.h"
|
2013-04-12 02:23:55 +00:00
|
|
|
#include "SkBitmapHasher.h"
|
2013-02-13 18:14:48 +00:00
|
|
|
#include "SkData.h"
|
2013-01-19 04:21:27 +00:00
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkRefCnt.h"
|
2013-02-13 18:14:48 +00:00
|
|
|
#include "SkStream.h"
|
2013-01-19 04:21:27 +00:00
|
|
|
#include "SkTArray.h"
|
|
|
|
|
|
|
|
#ifdef SK_BUILD_FOR_WIN
|
|
|
|
// json includes xlocale which generates warning 4530 because we're compiling without
|
|
|
|
// exceptions; see https://code.google.com/p/skia/issues/detail?id=1067
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4530)
|
|
|
|
#endif
|
2013-02-13 18:14:48 +00:00
|
|
|
#include "json/reader.h"
|
2013-01-19 04:21:27 +00:00
|
|
|
#include "json/value.h"
|
|
|
|
#ifdef SK_BUILD_FOR_WIN
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
2013-05-08 19:14:23 +00:00
|
|
|
void gm_fprintf(FILE *stream, const char format[], ...);
|
2013-03-13 14:18:40 +00:00
|
|
|
|
2013-05-24 18:28:57 +00:00
|
|
|
/**
|
|
|
|
* Assembles rootPath and relativePath into a single path, like this:
|
|
|
|
* rootPath/relativePath
|
|
|
|
*
|
|
|
|
* Uses SkPATH_SEPARATOR, to work on all platforms.
|
|
|
|
*
|
|
|
|
* TODO(epoger): This should probably move into SkOSFile.h
|
|
|
|
*/
|
|
|
|
SkString SkPathJoin(const char *rootPath, const char *relativePath);
|
|
|
|
|
2013-04-26 15:06:44 +00:00
|
|
|
Json::Value CreateJsonTree(Json::Value expectedResults,
|
|
|
|
Json::Value actualResultsFailed,
|
|
|
|
Json::Value actualResultsFailureIgnored,
|
|
|
|
Json::Value actualResultsNoComparison,
|
|
|
|
Json::Value actualResultsSucceeded);
|
|
|
|
|
2013-01-19 04:21:27 +00:00
|
|
|
/**
|
2013-05-24 14:33:28 +00:00
|
|
|
* The digest of a GM test result.
|
|
|
|
*
|
|
|
|
* Currently, this is always a uint64_t hash digest of an SkBitmap...
|
|
|
|
* but we will add other flavors soon.
|
|
|
|
*/
|
|
|
|
class GmResultDigest {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Create a ResultDigest representing an actual image result.
|
|
|
|
*/
|
|
|
|
GmResultDigest(const SkBitmap &bitmap);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a ResultDigest representing an allowed result
|
|
|
|
* checksum within JSON expectations file, in the form
|
|
|
|
* ["bitmap-64bitMD5", 12345].
|
|
|
|
*/
|
|
|
|
GmResultDigest(const Json::Value &jsonTypeValuePair);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this GmResultDigest was fully and successfully
|
|
|
|
* created.
|
|
|
|
*/
|
|
|
|
bool isValid() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this and other GmResultDigest could
|
|
|
|
* represent identical results.
|
|
|
|
*/
|
|
|
|
bool equals(const GmResultDigest &other) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a JSON type/value pair representing this result,
|
|
|
|
* such as ["bitmap-64bitMD5", 12345].
|
|
|
|
*/
|
|
|
|
Json::Value asJsonTypeValuePair() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool fIsValid; // always check this first--if it's false, other fields are meaningless
|
|
|
|
uint64_t fHashDigest;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test expectations (allowed image results, etc.)
|
2013-01-19 04:21:27 +00:00
|
|
|
*/
|
|
|
|
class Expectations {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* No expectations at all.
|
|
|
|
*/
|
2013-04-26 15:06:44 +00:00
|
|
|
Expectations(bool ignoreFailure=kDefaultIgnoreFailure);
|
2013-01-19 04:21:27 +00:00
|
|
|
|
|
|
|
/**
|
2013-02-01 20:39:15 +00:00
|
|
|
* Expect exactly one image (appropriate for the case when we
|
2013-01-19 04:21:27 +00:00
|
|
|
* are comparing against a single PNG file).
|
|
|
|
*/
|
2013-04-26 15:06:44 +00:00
|
|
|
Expectations(const SkBitmap& bitmap, bool ignoreFailure=kDefaultIgnoreFailure);
|
2013-01-19 04:21:27 +00:00
|
|
|
|
2013-02-13 18:14:48 +00:00
|
|
|
/**
|
|
|
|
* Create Expectations from a JSON element as found within the
|
|
|
|
* kJsonKey_ExpectedResults section.
|
|
|
|
*
|
|
|
|
* It's fine if the jsonElement is null or empty; in that case, we just
|
|
|
|
* don't have any expectations.
|
|
|
|
*/
|
2013-04-26 15:06:44 +00:00
|
|
|
Expectations(Json::Value jsonElement);
|
2013-02-13 18:14:48 +00:00
|
|
|
|
2013-01-19 04:21:27 +00:00
|
|
|
/**
|
|
|
|
* Returns true iff we want to ignore failed expectations.
|
|
|
|
*/
|
|
|
|
bool ignoreFailure() const { return this->fIgnoreFailure; }
|
|
|
|
|
|
|
|
/**
|
2013-05-24 14:33:28 +00:00
|
|
|
* Returns true iff there are no allowed results.
|
2013-01-19 04:21:27 +00:00
|
|
|
*/
|
2013-05-24 14:33:28 +00:00
|
|
|
bool empty() const { return this->fAllowedResultDigests.empty(); }
|
2013-01-19 04:21:27 +00:00
|
|
|
|
|
|
|
/**
|
2013-05-24 14:33:28 +00:00
|
|
|
* Returns true iff resultDigest matches any allowed result,
|
2013-01-19 04:21:27 +00:00
|
|
|
* regardless of fIgnoreFailure. (The caller can check
|
|
|
|
* that separately.)
|
|
|
|
*/
|
2013-05-24 14:33:28 +00:00
|
|
|
bool match(GmResultDigest resultDigest) const;
|
2013-01-19 04:21:27 +00:00
|
|
|
|
2013-02-01 20:39:15 +00:00
|
|
|
/**
|
|
|
|
* If this Expectation is based on a single SkBitmap, return a
|
|
|
|
* pointer to that SkBitmap. Otherwise (if the Expectation is
|
|
|
|
* empty, or if it was based on a list of checksums rather
|
|
|
|
* than a single bitmap), returns NULL.
|
|
|
|
*/
|
|
|
|
const SkBitmap *asBitmap() const {
|
|
|
|
return (SkBitmap::kNo_Config == fBitmap.config()) ? NULL : &fBitmap;
|
|
|
|
}
|
|
|
|
|
2013-01-19 04:21:27 +00:00
|
|
|
/**
|
2013-04-26 15:06:44 +00:00
|
|
|
* Return a JSON representation of the expectations.
|
2013-01-19 04:21:27 +00:00
|
|
|
*/
|
2013-04-26 15:06:44 +00:00
|
|
|
Json::Value asJsonValue() const;
|
2013-01-19 04:21:27 +00:00
|
|
|
|
|
|
|
private:
|
2013-02-13 18:14:48 +00:00
|
|
|
const static bool kDefaultIgnoreFailure = false;
|
|
|
|
|
2013-05-24 14:33:28 +00:00
|
|
|
SkTArray<GmResultDigest> fAllowedResultDigests;
|
2013-01-19 04:21:27 +00:00
|
|
|
bool fIgnoreFailure;
|
2013-02-01 20:39:15 +00:00
|
|
|
SkBitmap fBitmap;
|
2013-01-19 04:21:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract source of Expectations objects for individual tests.
|
|
|
|
*/
|
|
|
|
class ExpectationsSource : public SkRefCnt {
|
|
|
|
public:
|
|
|
|
virtual Expectations get(const char *testName) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return Expectations based on individual image files on disk.
|
|
|
|
*/
|
|
|
|
class IndividualImageExpectationsSource : public ExpectationsSource {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Create an ExpectationsSource that will return Expectations based on
|
|
|
|
* image files found within rootDir.
|
|
|
|
*
|
|
|
|
* rootDir: directory under which to look for image files
|
|
|
|
* (this string will be copied to storage within this object)
|
|
|
|
*/
|
2013-04-10 18:46:25 +00:00
|
|
|
IndividualImageExpectationsSource(const char *rootDir) : fRootDir(rootDir) {}
|
2013-01-19 04:21:27 +00:00
|
|
|
|
2013-04-26 15:06:44 +00:00
|
|
|
Expectations get(const char *testName) SK_OVERRIDE ;
|
2013-01-19 04:21:27 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const SkString fRootDir;
|
|
|
|
};
|
|
|
|
|
2013-02-13 18:14:48 +00:00
|
|
|
/**
|
|
|
|
* Return Expectations based on JSON summary file.
|
|
|
|
*/
|
|
|
|
class JsonExpectationsSource : public ExpectationsSource {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Create an ExpectationsSource that will return Expectations based on
|
|
|
|
* a JSON file.
|
|
|
|
*
|
|
|
|
* jsonPath: path to JSON file to read
|
|
|
|
*/
|
2013-04-26 15:06:44 +00:00
|
|
|
JsonExpectationsSource(const char *jsonPath);
|
2013-02-13 18:14:48 +00:00
|
|
|
|
2013-04-26 15:06:44 +00:00
|
|
|
Expectations get(const char *testName) SK_OVERRIDE;
|
2013-02-13 18:14:48 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read as many bytes as possible (up to maxBytes) from the stream into
|
|
|
|
* an SkData object.
|
|
|
|
*
|
|
|
|
* If the returned SkData contains fewer than maxBytes, then EOF has been
|
|
|
|
* reached and no more data would be available from subsequent calls.
|
|
|
|
* (If EOF has already been reached, then this call will return an empty
|
|
|
|
* SkData object immediately.)
|
|
|
|
*
|
|
|
|
* If there are fewer than maxBytes bytes available to read from the
|
|
|
|
* stream, but the stream has not been closed yet, this call will block
|
|
|
|
* until there are enough bytes to read or the stream has been closed.
|
|
|
|
*
|
|
|
|
* It is up to the caller to call unref() on the returned SkData object
|
|
|
|
* once the data is no longer needed, so that the underlying buffer will
|
|
|
|
* be freed. For example:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* size_t maxBytes = 256;
|
|
|
|
* SkAutoDataUnref dataRef(readIntoSkData(stream, maxBytes));
|
|
|
|
* if (NULL != dataRef.get()) {
|
|
|
|
* size_t bytesActuallyRead = dataRef.get()->size();
|
|
|
|
* // use the data...
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* // underlying buffer has been freed, thanks to auto unref
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
// TODO(epoger): Move this, into SkStream.[cpp|h] as attempted in
|
|
|
|
// https://codereview.appspot.com/7300071 ?
|
2013-05-08 19:14:23 +00:00
|
|
|
// And maybe ReadFileIntoSkData() also?
|
|
|
|
static SkData* ReadIntoSkData(SkStream &stream, size_t maxBytes);
|
2013-02-13 18:14:48 +00:00
|
|
|
|
|
|
|
/**
|
2013-05-08 19:14:23 +00:00
|
|
|
* Wrapper around ReadIntoSkData for files: reads the entire file into
|
2013-02-13 18:14:48 +00:00
|
|
|
* an SkData object.
|
|
|
|
*/
|
2013-05-08 19:14:23 +00:00
|
|
|
static SkData* ReadFileIntoSkData(SkFILEStream &stream) {
|
|
|
|
return ReadIntoSkData(stream, stream.getLength());
|
2013-02-13 18:14:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the file contents from jsonPath and parse them into jsonRoot.
|
|
|
|
*
|
|
|
|
* Returns true if successful.
|
|
|
|
*/
|
2013-05-08 19:14:23 +00:00
|
|
|
static bool Parse(const char *jsonPath, Json::Value *jsonRoot);
|
2013-02-13 18:14:48 +00:00
|
|
|
|
|
|
|
Json::Value fJsonRoot;
|
|
|
|
Json::Value fJsonExpectedResults;
|
|
|
|
};
|
|
|
|
|
2013-01-19 04:21:27 +00:00
|
|
|
}
|
|
|
|
#endif
|