Permalink
Browse files

Add rendering unit tests configuration

  • Loading branch information...
feragon committed Jul 31, 2017
1 parent 0ec5de8 commit 0d2a4a4c4de09bda3ddb6c86fa71715440ed172b
View
@@ -15,4 +15,5 @@ xcode
debug
doc
lckernel/version.cpp
cmake-build-debug
cmake-build-debug
unittest/rendering/res/*.out
@@ -246,8 +246,17 @@ void DocumentCanvas::autoScale() {
auto extends = _entityContainer.boundingBox();
extends = extends.increaseBy(std::min(extends.width(), extends.height()) * 0.1);
double zoom = std::min(_deviceWidth / extends.width(), _deviceHeight / extends.height());
this->zoom(zoom, false, extends.width() / 2 + extends.minP().x(), extends.height() / 2. + extends.minP().y(), _deviceWidth / 2., _deviceHeight / 2.);
setDisplayArea(extends);
}
void DocumentCanvas::setDisplayArea(const lc::geo::Area& area) {
double zoom = std::min(_deviceWidth / area.width(), _deviceHeight / area.height());
this->zoom(zoom, false,
area.width() / 2 + area.minP().x(),
area.height() / 2. + area.minP().y(),
_deviceWidth / 2.,
_deviceHeight / 2.
);
}
void DocumentCanvas::render(std::function<void(LcPainter&)> before, std::function<void(LcPainter&)> after) {
@@ -55,6 +55,12 @@ class DocumentCanvas : public std::enable_shared_from_this<DocumentCanvas> {
*/
void autoScale();
/**
* @brief Set display area
* @param area Area to display
*/
void setDisplayArea(const lc::geo::Area& area);
void pan(double move_x, double move_y);
void transX(int x);
View
@@ -122,6 +122,11 @@ if(WITH_RENDERING_UNITTESTS)
include_directories(${PANGO_INCLUDE_DIRS})
link_directories(${PANGO_LIBRARIES})
# Boost
set(Boost_USE_MULTITHREADED ON)
find_package(Boost COMPONENTS program_options filesystem system REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
set(EXTRA_LIBS
${EXTRA_LIBS}
${CAIRO_LIBRARIES}
@@ -130,6 +135,7 @@ if(WITH_RENDERING_UNITTESTS)
${GDK_LIBRARIES}
${GLib_LIBRARY}
${GOBJECT_LIBRARIES}
${Boost_LIBRARIES}
)
set(src
@@ -12,8 +12,33 @@
#include <painters/lccairopainter.tcc>
#include <file.h>
#include <drawables/gradientbackground.h>
#include <boost/program_options/variables_map.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options.hpp>
#include <fstream>
#define DEFAULT_IMAGE_WIDTH 100
#define DEFAULT_IMAGE_HEIGHT 100
#define DEFAULT_X 0
#define DEFAULT_Y 0
#define DEFAULT_W 100
#define DEFAULT_H 100
#define DEFAULT_TOLERANCE 0
namespace po = boost::program_options;
void resetConfig(int* imageWidth, int* imageHeight, int* x, int* y, int* w, int* h, int* tolerance) {
*imageWidth = DEFAULT_IMAGE_WIDTH;
*imageHeight = DEFAULT_IMAGE_HEIGHT;
*x = DEFAULT_X;
*y = DEFAULT_Y;
*w = DEFAULT_W;
*h = DEFAULT_H;
*tolerance = DEFAULT_TOLERANCE;
}
void render(const std::string& dxf, const std::string& output, unsigned int width, unsigned int height) {
void render(const std::string& dxf, const std::string& output, unsigned int imageWidth, unsigned int imageHeight,
int x, int y, int w, int h) {
auto _storageManager = std::make_shared<lc::StorageManagerImpl>();
auto _document = std::make_shared<lc::DocumentImpl>(_storageManager);
auto _canvas = std::make_shared<LCViewer::DocumentCanvas>(_document);
@@ -29,7 +54,7 @@ void render(const std::string& dxf, const std::string& output, unsigned int widt
_canvas->createPainterFunctor(
[&](const unsigned int width, const unsigned int height) {
if (lcPainter == nullptr) {
lcPainter = new LcCairoPainter<CairoPainter::backend::SVG>(width, height, nullptr);
lcPainter = new LcCairoPainter<CairoPainter::backend::SVG>(imageWidth, imageHeight, nullptr);
}
return lcPainter;
@@ -43,7 +68,7 @@ void render(const std::string& dxf, const std::string& output, unsigned int widt
}
});
_canvas->newDeviceSize(width, height);
_canvas->newDeviceSize(imageWidth, imageHeight);
_canvas->render(
[&](LcPainter&) {},
@@ -52,7 +77,7 @@ void render(const std::string& dxf, const std::string& output, unsigned int widt
lc::File::open(_document, dxf, lc::File::LIBDXFRW);
_canvas->autoScale();
_canvas->setDisplayArea(lc::geo::Area(lc::geo::Coordinate(x, y), w, h));
_canvas->render(
[&](LcPainter&) {},
[&](LcPainter&) {}
@@ -61,18 +86,20 @@ void render(const std::string& dxf, const std::string& output, unsigned int widt
static_cast<LcCairoPainter<CairoPainter::backend::Image>*>(lcPainter)->writePNG(output);
}
bool checkRender(const std::string& image1, const std::string& image2) {
bool checkRender(const std::string& image1, const std::string& image2, int tolerance) {
GError* error1 = NULL;
GError* error2 = NULL;
auto pixbuf1 = gdk_pixbuf_new_from_file(image1.c_str(), &error1);
auto pixbuf2 = gdk_pixbuf_new_from_file(image2.c_str(), &error2);
auto pixbuf2 = gdk_pixbuf_new_from_file(image1.c_str(), &error2);
if(error1) {
std::cout << error1->message << std::endl;
g_error_free(error1);
return false;
}
if(error2) {
std::cout << error2->message << std::endl;
g_error_free(error2);
return false;
}
@@ -83,10 +110,45 @@ bool checkRender(const std::string& image1, const std::string& image2) {
return false;
}
return (bool) g_bytes_equal(gdk_pixbuf_read_pixel_bytes(pixbuf1), gdk_pixbuf_read_pixel_bytes(pixbuf2));
auto channelTolerance = 256 * (tolerance / 100);
auto nbPixels = gdk_pixbuf_get_height(pixbuf1) * gdk_pixbuf_get_rowstride(pixbuf1);
auto pixels1 = gdk_pixbuf_read_pixels(pixbuf1);
auto pixels2 = gdk_pixbuf_read_pixels(pixbuf2);
for(auto i = 0; i < nbPixels; i++) {
auto pixel1 = pixels1[i];
auto pixel2 = pixels2[i];
if(pixel1 < pixel2 - channelTolerance || pixel1 > pixel2 + channelTolerance) {
return false;
}
}
return true;
}
TEST(RenderingTest, Test) {
int imageW;
int imageH;
int x;
int y;
int w;
int h;
int tolerance;
resetConfig(&imageW, &imageH, &x, &y, &w, &h, &tolerance);
po::variables_map vm;
po::options_description desc("Allowed options");
desc.add_options()
("imageW", po::value<int>(&imageW), "Image width (in pixels)")
("imageH", po::value<int>(&imageH), "Image height (in pixels)")
("x", po::value<int>(&x), "Canvas base")
("y", po::value<int>(&y), "Canvas base")
("w", po::value<int>(&w), "Canvas width")
("h", po::value<int>(&h), "Canvas height")
("tolerance", po::value<int>(&tolerance), "Tolerance (between 0 and 100)");
DIR* dir = opendir("../unittest/rendering/res"); //TODO: get correct folder
if(!dir) {
perror("Error");
@@ -97,6 +159,7 @@ TEST(RenderingTest, Test) {
unsigned int testNumber = 0;
bool dxfFound = false;
bool pngFound = false;
bool configFound = false;
while ((file = readdir(dir)) != NULL) {
unsigned int newNumber;
@@ -110,6 +173,7 @@ TEST(RenderingTest, Test) {
testNumber = newNumber;
dxfFound = false;
pngFound = false;
configFound = false;
}
if(strcmp(extension, "dxf") == 0) {
@@ -118,18 +182,30 @@ TEST(RenderingTest, Test) {
else if(strcmp(extension, "png") == 0) {
pngFound = true;
}
else if(strcmp(extension, "cfg") == 0) {
configFound = true;
}
if(dxfFound && pngFound) {
if(dxfFound && pngFound && configFound) {
auto base = std::string("../unittest/rendering/res/") + std::to_string(newNumber);
auto expectedFile = base + ".png";
auto dxfFile = base + ".dxf";
auto resultFile = base + ".out";
auto configFile = base + ".cfg";
resetConfig(&imageW, &imageH, &x, &y, &w, &h, &tolerance);
vm = po::variables_map();
std::ifstream configStream(configFile.c_str());
po::store(po::parse_config_file(configStream, desc), vm);
po::notify(vm);
render(dxfFile, resultFile, 100, 100); //TODO: get real size
ASSERT_TRUE(checkRender(expectedFile, resultFile)) << "Failed with " << expectedFile;
render(dxfFile, resultFile, imageW, imageH, x, y, w, h);
ASSERT_TRUE(checkRender(expectedFile, resultFile, tolerance)) << "Failed with " << expectedFile;
dxfFound = false; //Prevent running the test more than once
pngFound = false;
configFound = false;
}
}
@@ -0,0 +1,7 @@
imageW = 100
imageH = 100
x = 0
y = 0
w = 100
h = 100
tolerance = 10
Oops, something went wrong.

0 comments on commit 0d2a4a4

Please sign in to comment.