#include <vector>
#include "base/command_line.h"
#include "skia/ext/switches.h"
#include "third_party/fuzztest/src/fuzztest/fuzztest.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/gfx/vector_icon_utils.h"
namespace {
using fuzztest::Arbitrary;
using fuzztest::Domain;
using fuzztest::ElementOf;
using fuzztest::Finite;
using fuzztest::FlatMap;
using fuzztest::InRange;
using fuzztest::Just;
using fuzztest::Map;
using fuzztest::NonEmpty;
using fuzztest::StructOf;
using fuzztest::VariantOf;
using fuzztest::VectorOf;
#define DECLARE_VECTOR_COMMAND(x) gfx::CommandType::x,
auto AnyCommandType() {
return ElementOf({DECLARE_VECTOR_COMMANDS});
}
struct Command {
gfx::CommandType type;
std::vector<SkScalar> args;
};
Domain<std::vector<SkScalar>> AnyArgsForCommandType(gfx::CommandType type) {
int args_count = gfx::GetCommandArgumentCount(type);
switch (type) {
case gfx::PATH_COLOR_ARGB:
return VectorOf(InRange(SkScalar(0.0), SkScalar(255.0)))
.WithSize(args_count);
case gfx::CANVAS_DIMENSIONS:
return VectorOf(InRange(SkScalar(1.0), SkScalar(1024.0)))
.WithSize(args_count);
default:
return VectorOf(Finite<SkScalar>()).WithSize(args_count);
}
}
Domain<Command> AnyCommandWithType(gfx::CommandType type) {
return StructOf<Command>(Just(type), AnyArgsForCommandType(type));
}
auto AnyCommand() {
return FlatMap(AnyCommandWithType, AnyCommandType());
}
std::vector<gfx::PathElement> ConvertCommands(
const std::vector<Command>& commands) {
std::vector<gfx::PathElement> path;
for (const auto& command : commands) {
path.emplace_back(command.type);
for (SkScalar arg : command.args) {
path.emplace_back(arg);
}
}
return path;
}
class PaintVectorIconFuzzTest {
public:
PaintVectorIconFuzzTest() {
CHECK(base::CommandLine::Init(0, nullptr));
base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
command_line.AppendSwitchASCII(switches::kTextContrast, "1.0");
command_line.AppendSwitchASCII(switches::kTextGamma, "1.0");
}
void PaintVectorIcon(std::vector<Command> commands) {
std::vector<gfx::PathElement> path = ConvertCommands(commands);
gfx::VectorIconRep rep{path};
gfx::VectorIcon icon(&rep, 1u, "icon");
constexpr float kImageScale = 1.f;
constexpr bool kIsOpaque = true;
gfx::Canvas canvas(gfx::Size(1024, 1024), kImageScale, kIsOpaque);
constexpr int kDipSize = 1024;
constexpr SkColor kBlack = SkColorSetARGB(255, 0, 0, 0);
gfx::PaintVectorIcon(&canvas, icon, kDipSize, kBlack);
}
};
FUZZ_TEST_F(PaintVectorIconFuzzTest, PaintVectorIcon)
.WithDomains(NonEmpty(VectorOf(AnyCommand())));
}