Permalink
Browse files

LinePatternSelect unit test and missing files from other unit tests

  • Loading branch information...
feragon committed Aug 3, 2016
1 parent b5c9abb commit 8e5edd8e1c94aadd3a4dc264927b304ca45e5bc5
@@ -0,0 +1,105 @@
#pragma once
#include <QTest>
#include <QThread>
#include <dialogs/addlinepatterndialog.cpp>
class AddLinePatternDialogTest : AddLinePatternDialog {
public:
AddLinePatternDialogTest(lc::Document_SPtr document, QWidget* parent = 0) :
AddLinePatternDialog(document, parent) {
}
AddLinePatternDialogTest(lc::Document_SPtr document, lc::DxfLinePattern_CSPtr linePattern, QWidget* parent = 0) :
AddLinePatternDialog(document, linePattern, parent) {
}
std::string name() {
return ui->name->text().toStdString();
}
std::string description() {
return ui->description->text().toStdString();
}
std::vector<double> path() {
std::vector<double> path;
int nbValues = _layout->count() - 1;
for(int i = 0; i < nbValues; i++) {
auto pathPart = static_cast<LinePatternPathPart *>(_layout->itemAt(i)->widget());
if (!pathPart) {
continue;
}
switch (pathPart->type()) {
case LinePatternPathPart::PATH_SPACE:
path.push_back(-pathPart->value());
break;
case LinePatternPathPart::PATH_PLAIN:
path.push_back(pathPart->value());
break;
case LinePatternPathPart::PATH_DOT:
path.push_back(0);
break;
}
}
return path;
}
void setName(std::string name) {
ui->name->setText(name.c_str());
}
void setDescription(std::string description) {
ui->description->setText(description.c_str());
}
void setPath(std::vector<double> path) {
int nbValues = _layout->count() - 1;
for(int i = 0; i < nbValues; i++) {
auto item = _layout->takeAt(0);
delete item->widget();
delete item;
}
for(auto value : path) {
QTest::mouseClick(ui->newValueButton, Qt::LeftButton, Qt::NoModifier, QPoint(5, 5), 100);
auto pathPart = _layout->itemAt(_layout->count() - 2)->widget();
if(!pathPart) {
continue;
}
auto typeInput = pathPart->findChildren<QComboBox*>()[0];
auto valueInput = pathPart->findChildren<QDoubleSpinBox*>()[0];
if(value < 0) {
typeInput->setCurrentText(PATH_SPACE_NAME);
valueInput->setValue(-value);
}
else if(value == 0) {
typeInput->setCurrentText(PATH_DOT_NAME);
valueInput->setValue(0);
}
else {
typeInput->setCurrentText(PATH_PLAIN_NAME);
valueInput->setValue(value);
}
}
}
void create() {
QTest::mouseClick(ui->saveButton, Qt::LeftButton, Qt::NoModifier, QPoint(5, 5), 100);
}
void edit() {
QTest::mouseClick(editButton, Qt::LeftButton, Qt::NoModifier, QPoint(5, 5), 100);
}
};
View
@@ -0,0 +1,110 @@
#include <gtest/gtest.h>
#include "uitests.h"
#include "widgets/layers.h"
TEST(LayersTest, Creation) {
QApplication app(argc, argv);
auto document = createDocument();
auto layers = new LayersTest(document);
auto layer = std::make_shared<const lc::Layer>("Name", lc::MetaLineWidthByValue(1), lc::Color(255,255,255,255));
/*
* Creation when document exists
*/
ASSERT_EQ(0, layers->nbLayers()) << "Document contains layers after creation";
layers->addLayer(layer);
EXPECT_EQ(1, layers->nbLayers()) << "Layer was not appended to document";
/*
* Creation when document doesn't exist
*/
layers->setDocument(nullptr);
ASSERT_EQ(0, layers->nbLayers()) << "Unexisting document contains layers";
layers->addLayer(layer);
EXPECT_EQ(0, layers->nbLayers()) << "Layer was added to unexisting document";
delete layers;
}
TEST(LayersTest, Selection) {
QApplication app(argc, argv);
auto document = createDocument();
auto layers = new LayersTest(document);
auto layer = std::make_shared<const lc::Layer>("Name", lc::MetaLineWidthByValue(1), lc::Color(255,255,255,255));
/*
* Selection when no layers are present in the document
*/
ASSERT_EQ(0, layers->nbLayers()) << "Document contains layers after creation";
EXPECT_NE(nullptr, layers->activeLayer()) << "Empty layer should be created";
EXPECT_EQ(1, layers->nbLayers()) << "Empty layer should be added to document";
/*
* Select layer
*/
layers->addLayer(layer);
ASSERT_EQ(2, layers->nbLayers()) << "Layer was not added to document";
layers->selectLayer(0);
auto selectedLayer1 = layers->activeLayer();
layers->selectLayer(1);
auto selectedLayer2 = layers->activeLayer();
EXPECT_TRUE((selectedLayer1 == layer) ^ (selectedLayer2 == layer)) << "Wrong layer selected";
delete layers;
}
TEST(LayersTest, Edition) {
QApplication app(argc, argv);
auto document = createDocument();
auto layers = new LayersTest(document);
auto layer = std::make_shared<const lc::Layer>("Name", lc::MetaLineWidthByValue(1), lc::Color(255,255,255,255));
layers->addLayer(layer);
ASSERT_EQ(1, layers->nbLayers()) << "Layer was not added to document";
/*
* Trigger locked
*/
bool locked = layers->activeLayer()->isFrozen();
layers->cellClick(LayerModel::LOCKED, 0);
locked = !locked;
ASSERT_EQ(locked, layers->activeLayer()->isFrozen()) << "Locked value didn't change";
layers->cellClick(LayerModel::LOCKED, 0);
locked = !locked;
EXPECT_EQ(locked, layers->activeLayer()->isFrozen()) << "Locked value didn't change";
delete layers;
}
TEST(LayersTest, Remove) {
QApplication app(argc, argv);
auto document = createDocument();
auto layers = new LayersTest(document);
auto layer = std::make_shared<const lc::Layer>("Name", lc::MetaLineWidthByValue(1), lc::Color(255,255,255,255));
layers->addLayer(layer);
ASSERT_EQ(1, layers->nbLayers()) << "Layer was not added to document";
layers->selectLayer(0);
ASSERT_EQ(layer, layers->activeLayer());
layers->remove();
EXPECT_EQ(0, layers->nbLayers()) << "Layer was not removed";
delete layers;
}
@@ -0,0 +1,94 @@
#include <gtest/gtest.h>
#include <cad/meta/dxflinepattern.h>
#include <widgets/linepatternselect.h>
#include "uitests.h"
#include "dialogs/addlinepatterndialog.h"
TEST(LinePatternTest, Creation) {
QApplication app(argc, argv);
auto document = createDocument();
ASSERT_EQ(0, document->linePatterns().size()) << "Document already contains line patterns";
auto linePattern = std::make_shared<const lc::DxfLinePattern>("LP", "Line Pattern", std::vector<double>{1, -1, 0, -1}, 3);
AddLinePatternDialogTest linePatternDialog(document);
linePatternDialog.setName(linePattern->name());
linePatternDialog.setDescription(linePattern->description());
linePatternDialog.setPath(linePattern->path());
linePatternDialog.create();
ASSERT_EQ(1, document->linePatterns().size()) << "Line pattern was not added to document";
auto addedLinePattern = document->linePatterns()[0];
EXPECT_EQ(linePattern->name(), addedLinePattern->name());
EXPECT_EQ(linePattern->description(), addedLinePattern->description());
EXPECT_EQ(linePattern->lcPattern(), addedLinePattern->lcPattern());
}
TEST(LinePatternTest, Edition) {
QApplication app(argc, argv);
auto document = createDocument();
auto linePattern = std::make_shared<const lc::DxfLinePattern>("LP", "Line Pattern", std::vector<double>{1, -1, 0, -1}, 3);
auto newLinePattern = std::make_shared<const lc::DxfLinePattern>("NLP", "New Line Pattern", std::vector<double>{0, -1, 1, -1}, 3);
auto operation = std::make_shared<lc::operation::AddLinePattern>(document, linePattern);
operation->execute();
ASSERT_EQ(1, document->linePatterns().size()) << "Line pattern was not added to the document";
AddLinePatternDialogTest dialog(document, linePattern);
EXPECT_EQ(linePattern->name(), dialog.name());
EXPECT_EQ(linePattern->description(), dialog.description());
EXPECT_EQ(linePattern->path(), dialog.path());
dialog.setName(newLinePattern->name());
dialog.setDescription(newLinePattern->description());
dialog.setPath(newLinePattern->path());
dialog.edit();
ASSERT_EQ(1, document->linePatterns().size()) << "Line pattern was not edited";
auto documentLinePattern = document->linePatterns()[0];
EXPECT_EQ(newLinePattern->name(), documentLinePattern->name());
EXPECT_EQ(newLinePattern->description(), documentLinePattern->description());
EXPECT_EQ(newLinePattern->path(), documentLinePattern->path());
}
TEST(LinePatternTest, Selection) {
QApplication app(argc, argv);
auto document = createDocument();
auto linePatternSelect = new LinePatternSelect(document);
EXPECT_EQ(1, linePatternSelect->count()) << "Select should contains New";
delete linePatternSelect;
linePatternSelect = new LinePatternSelect(document, 0, true, true);
EXPECT_EQ(3, linePatternSelect->count()) << "Select should contains New/ByBlock/ByLayer";
auto linePattern1 = std::make_shared<const lc::DxfLinePattern>("LP", "Line Pattern", std::vector<double>{1, -1, 0, -1}, 3);
auto linePattern2 = std::make_shared<const lc::DxfLinePattern>("NLP", "New Line Pattern", std::vector<double>{0, -1, 1, -1}, 3);
auto operation = std::make_shared<lc::operation::AddLinePattern>(document, linePattern1);
operation->execute();
operation = std::make_shared<lc::operation::AddLinePattern>(document, linePattern2);
operation->execute();
EXPECT_EQ(5, linePatternSelect->count()) << "Layers were not added to line pattern select";
linePatternSelect->setCurrentText(linePattern1->name().c_str());
EXPECT_EQ(linePattern1, linePatternSelect->linePattern()) << "Selected line pattern wasn't returned";
linePatternSelect->setCurrentText(linePattern2->name().c_str());
EXPECT_EQ(linePattern2, linePatternSelect->linePattern()) << "Selected line pattern wasn't returned";
delete linePatternSelect;
}
View
@@ -0,0 +1,15 @@
#include "uitests.h"
LuaIntf::LuaState startLC() {
LuaInterface luaInterface;
luaInterface.hideUI(true);
luaInterface.initLua();
return luaInterface.luaState();
}
lc::Document_SPtr createDocument() {
auto storageManager = std::make_shared<lc::StorageManagerImpl>();
return std::make_shared<lc::DocumentImpl>(storageManager);
};
View
@@ -0,0 +1,11 @@
#pragma once
#include <QApplication>
#include <luainterface.h>
#include <cad/dochelpers/documentimpl.h>
static int argc = 0;
static char** argv = NULL;
LuaIntf::LuaState startLC();
lc::Document_SPtr createDocument();
@@ -0,0 +1,37 @@
#pragma once
#include <QTest>
#include <widgets/layers.cpp>
class LayersTest : public Layers {
public:
LayersTest(lc::Document_SPtr document = nullptr, QWidget* parent = 0) :
Layers(document, parent)
{}
unsigned int nbLayers() {
QAbstractItemModel* model = ui->layerList->model();
return model->rowCount();
}
void selectLayer(unsigned int row) {
ui->layerList->selectRow(row);
}
void cellClick(unsigned int col, unsigned int row) {
int xPos = ui->layerList->columnViewportPosition(col) + 2;
int yPos = ui->layerList->rowViewportPosition(row) + 2;
QWidget* viewport = ui->layerList->viewport();
QTest::mouseClick(viewport, Qt::LeftButton, NULL, QPoint(xPos, yPos));
}
void addLayer(lc::Layer_CSPtr layer) {
createLayer(layer);
}
void remove() {
QTest::mouseClick(ui->deleteButton, Qt::LeftButton, Qt::NoModifier, QPoint(5, 5), 100);
}
};

0 comments on commit 8e5edd8

Please sign in to comment.