Simplifying code.

This commit is contained in:
NissimHadar 2018-09-02 12:58:18 -07:00
parent 1ffd005b77
commit 0c8f80026a
3 changed files with 40 additions and 22 deletions

View file

@ -23,7 +23,10 @@ extern AutoTester* autoTester;
#include <math.h> #include <math.h>
Test::Test() { Test::Test(QProgressBar* progressBar, QCheckBox* checkBoxInteractiveMode) {
_progressBar = progressBar;
_checkBoxInteractiveMode = checkBoxInteractiveMode;
_mismatchWindow.setModal(true); _mismatchWindow.setModal(true);
if (autoTester) { if (autoTester) {
@ -58,11 +61,11 @@ void Test::zipAndDeleteTestResultsFolder() {
_index = 1; _index = 1;
} }
bool Test::compareImageLists(bool isInteractiveMode, QProgressBar* progressBar) { bool Test::compareImageLists() {
progressBar->setMinimum(0); _progressBar->setMinimum(0);
progressBar->setMaximum(_expectedImagesFullFilenames.length() - 1); _progressBar->setMaximum(_expectedImagesFullFilenames.length() - 1);
progressBar->setValue(0); _progressBar->setValue(0);
progressBar->setVisible(true); _progressBar->setVisible(true);
// Loop over both lists and compare each pair of images // Loop over both lists and compare each pair of images
// Quit loop if user has aborted due to a failed test. // Quit loop if user has aborted due to a failed test.
@ -74,6 +77,8 @@ bool Test::compareImageLists(bool isInteractiveMode, QProgressBar* progressBar)
QImage expectedImage(_expectedImagesFullFilenames[i]); QImage expectedImage(_expectedImagesFullFilenames[i]);
double similarityIndex; // in [-1.0 .. 1.0], where 1.0 means images are identical double similarityIndex; // in [-1.0 .. 1.0], where 1.0 means images are identical
bool isInteractiveMode = (!_isRunningFromCommandLine && _checkBoxInteractiveMode->isChecked());
// similarityIndex is set to -100.0 to indicate images are not the same size // similarityIndex is set to -100.0 to indicate images are not the same size
if (isInteractiveMode && (resultImage.width() != expectedImage.width() || resultImage.height() != expectedImage.height())) { if (isInteractiveMode && (resultImage.width() != expectedImage.width() || resultImage.height() != expectedImage.height())) {
@ -117,10 +122,10 @@ bool Test::compareImageLists(bool isInteractiveMode, QProgressBar* progressBar)
} }
} }
progressBar->setValue(i); _progressBar->setValue(i);
} }
progressBar->setVisible(false); _progressBar->setVisible(false);
return success; return success;
} }
@ -173,7 +178,13 @@ void Test::appendTestResultsToFile(const QString& _testResultsFolderPath, TestFa
comparisonImage.save(failureFolderPath + "/" + "Difference Image.png"); comparisonImage.save(failureFolderPath + "/" + "Difference Image.png");
} }
void Test::startTestsEvaluation(const QString& testFolder, const QString& branchFromCommandLine, const QString& userFromCommandLine) { void Test::startTestsEvaluation(const bool isRunningFromCommandLine,
const QString& testFolder,
const QString& branchFromCommandLine,
const QString& userFromCommandLine
) {
_isRunningFromCommandLine = isRunningFromCommandLine;
if (testFolder.isNull()) { if (testFolder.isNull()) {
// Get list of JPEG images in folder, sorted by name // Get list of JPEG images in folder, sorted by name
QString previousSelection = _snapshotDirectory; QString previousSelection = _snapshotDirectory;
@ -240,11 +251,10 @@ void Test::startTestsEvaluation(const QString& testFolder, const QString& branch
autoTester->downloadFiles(expectedImagesURLs, _snapshotDirectory, _expectedImagesFilenames); autoTester->downloadFiles(expectedImagesURLs, _snapshotDirectory, _expectedImagesFilenames);
} }
void Test::finishTestsEvaluation() {
void Test::finishTestsEvaluation(bool isRunningFromCommandline, bool interactiveMode, QProgressBar* progressBar) { bool success = compareImageLists();
bool success = compareImageLists((!isRunningFromCommandline && interactiveMode), progressBar);
if (!isRunningFromCommandline) { if (!_isRunningFromCommandLine) {
if (success) { if (success) {
QMessageBox::information(0, "Success", "All images are as expected"); QMessageBox::information(0, "Success", "All images are as expected");
} else { } else {

View file

@ -41,10 +41,14 @@ enum TestRailCreateMode {
class Test { class Test {
public: public:
Test(); Test(QProgressBar* progressBar, QCheckBox* checkBoxInteractiveMode);
void startTestsEvaluation(const QString& testFolder = QString(), const QString& branchFromCommandLine = QString(), const QString& userFromCommandLine = QString()); void startTestsEvaluation(const bool isRunningFromCommandLine,
void finishTestsEvaluation(bool isRunningFromCommandline, bool interactiveMode, QProgressBar* progressBar); const QString& testFolder = QString(),
const QString& branchFromCommandLine = QString(),
const QString& userFromCommandLine = QString());
void finishTestsEvaluation();
void createTests(); void createTests();
@ -70,7 +74,7 @@ public:
void createAllRecursiveScripts(); void createAllRecursiveScripts();
void createRecursiveScript(const QString& topLevelDirectory, bool interactiveMode); void createRecursiveScript(const QString& topLevelDirectory, bool interactiveMode);
bool compareImageLists(bool isInteractiveMode, QProgressBar* progressBar); bool compareImageLists();
QStringList createListOfAll_imagesInDirectory(const QString& imageFormat, const QString& pathToImageDirectory); QStringList createListOfAll_imagesInDirectory(const QString& imageFormat, const QString& pathToImageDirectory);
@ -93,6 +97,11 @@ public:
void setTestRailCreateMode(TestRailCreateMode testRailCreateMode); void setTestRailCreateMode(TestRailCreateMode testRailCreateMode);
private: private:
QProgressBar* _progressBar;
QCheckBox* _checkBoxInteractiveMode;
bool _isRunningFromCommandLine{ false };
const QString TEST_FILENAME { "test.js" }; const QString TEST_FILENAME { "test.js" };
const QString TEST_RESULTS_FOLDER { "TestResults" }; const QString TEST_RESULTS_FOLDER { "TestResults" };
const QString TEST_RESULTS_FILENAME { "TestResults.txt" }; const QString TEST_RESULTS_FILENAME { "TestResults.txt" };

View file

@ -36,12 +36,11 @@ AutoTester::AutoTester(QWidget* parent) : QMainWindow(parent) {
} }
void AutoTester::setup() { void AutoTester::setup() {
_test = new Test(); _test = new Test(_ui.progressBar, _ui.checkBoxInteractiveMode);
} }
void AutoTester::runFromCommandLine(const QString& testFolder, const QString& branch, const QString& user) { void AutoTester::runFromCommandLine(const QString& testFolder, const QString& branch, const QString& user) {
_isRunningFromCommandline = true; _test->startTestsEvaluation(true, testFolder, branch, user);
_test->startTestsEvaluation(testFolder, branch, user);
} }
void AutoTester::on_tabWidget_currentChanged(int index) { void AutoTester::on_tabWidget_currentChanged(int index) {
@ -55,7 +54,7 @@ void AutoTester::on_tabWidget_currentChanged(int index) {
} }
void AutoTester::on_evaluateTestsButton_clicked() { void AutoTester::on_evaluateTestsButton_clicked() {
_test->startTestsEvaluation(); _test->startTestsEvaluation(false);
} }
void AutoTester::on_createRecursiveScriptButton_clicked() { void AutoTester::on_createRecursiveScriptButton_clicked() {
@ -188,7 +187,7 @@ void AutoTester::saveFile(int index) {
if (_numberOfFilesDownloaded == _numberOfFilesToDownload) { if (_numberOfFilesDownloaded == _numberOfFilesToDownload) {
disconnect(_signalMapper, SIGNAL(mapped(int)), this, SLOT(saveFile(int))); disconnect(_signalMapper, SIGNAL(mapped(int)), this, SLOT(saveFile(int)));
_test->finishTestsEvaluation(_isRunningFromCommandline, _ui.checkBoxInteractiveMode->isChecked(), _ui.progressBar); _test->finishTestsEvaluation();
} else { } else {
_ui.progressBar->setValue(_numberOfFilesDownloaded); _ui.progressBar->setValue(_numberOfFilesDownloaded);
} }