MuseScore/mtest/testutils.cpp

341 lines
11 KiB
C++
Raw Normal View History

2012-05-26 14:49:10 +02:00
//=============================================================================
// MuseScore
// Music Composition & Notation
//
2013-04-25 21:37:19 +02:00
// Copyright (C) 2012-2013 Werner Schweer
2012-05-26 14:49:10 +02:00
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2
// as published by the Free Software Foundation and appearing in
// the file LICENCE.GPL
//=============================================================================
#include <QtTest/QtTest>
2015-04-20 14:38:13 +02:00
#include <QTextStream>
2012-06-04 10:05:18 +02:00
#include "config.h"
2012-05-26 14:49:10 +02:00
#include "libmscore/score.h"
#include "libmscore/note.h"
#include "libmscore/chord.h"
#include "libmscore/instrtemplate.h"
#include "omr/omr.h"
#include "testutils.h"
#include "mscore/preferences.h"
#include "libmscore/page.h"
2013-04-03 12:49:55 +02:00
#include "synthesizer/msynthesizer.h"
2015-02-03 14:18:29 +01:00
#include "libmscore/musescoreCore.h"
2012-12-10 09:15:50 +01:00
#include "mscore/shortcut.h"
#include "mscore/importmidi/importmidi_operations.h"
2014-04-09 16:09:21 +02:00
#include "libmscore/xml.h"
2014-05-08 17:59:24 +02:00
#include "libmscore/excerpt.h"
2012-05-26 14:49:10 +02:00
inline void initMyResources() {
Q_INIT_RESOURCE(mtest);
Q_INIT_RESOURCE(musescorefonts_MScore);
Q_INIT_RESOURCE(musescorefonts_Gootville);
Q_INIT_RESOURCE(musescorefonts_Bravura);
Q_INIT_RESOURCE(musescorefonts_MuseJazz);
2014-10-27 12:12:31 +01:00
Q_INIT_RESOURCE(musescorefonts_FreeSerif);
Q_INIT_RESOURCE(musescorefonts_Free);
}
2016-03-11 12:18:46 +01:00
extern Ms::Score::FileError importOve(Ms::MasterScore*, const QString& name);
Q_LOGGING_CATEGORY(undoRedo, "undoRedo", QtCriticalMsg)
// Q_LOGGING_CATEGORY(undoRedo, "undoRedo", QtDebugMsg)
2013-05-13 20:24:03 +02:00
namespace Ms {
2012-06-04 10:05:18 +02:00
#ifdef OMR
extern Score::FileError importPdf(MasterScore*, const QString&);
2012-06-04 10:05:18 +02:00
#endif
2016-03-11 12:18:46 +01:00
extern Score::FileError importBB(MasterScore*, const QString&);
extern Score::FileError importCapella(MasterScore*, const QString&);
extern Score::FileError importCapXml(MasterScore*, const QString&);
extern Score::FileError importCompressedMusicXml(MasterScore*, const QString&);
extern Score::FileError importMusicXml(MasterScore*, const QString&);
extern Score::FileError importGTP(MasterScore*, const QString&);
2012-09-27 21:34:36 +02:00
extern bool saveXml(Score*, const QString&);
2012-05-26 14:49:10 +02:00
bool debugMode = false;
QString revision;
2013-02-06 17:43:43 +01:00
bool enableTestMode;
2012-05-26 14:49:10 +02:00
2016-03-10 10:41:31 +01:00
MasterScore* score;
MasterSynthesizer* synti;
2012-12-10 09:15:50 +01:00
QString dataPath;
QIcon* icons[0];
QString mscoreGlobalShare;
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
// writeReadElement
// writes and element and reads it back
//---------------------------------------------------------
Element* MTest::writeReadElement(Element* element)
{
//
// write element
//
QBuffer buffer;
buffer.open(QIODevice::WriteOnly);
2016-11-19 11:51:21 +01:00
XmlWriter xml(element->score(), &buffer);
2012-05-26 14:49:10 +02:00
xml.header();
element->write(xml);
buffer.close();
//
// read element
//
XmlReader e(buffer.buffer());
2013-01-17 12:56:14 +01:00
e.readNextStartElement();
QString tag(e.name().toString());
2013-01-18 14:46:49 +01:00
element = Element::name2Element(e.name(), score);
2012-05-26 14:49:10 +02:00
element->read(e);
return element;
}
//---------------------------------------------------------
// MTest
//---------------------------------------------------------
MTest::MTest()
2017-06-19 12:59:53 +02:00
: ed(0)
2012-05-26 14:49:10 +02:00
{
MScore::testMode = true;
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// readScore
//---------------------------------------------------------
2016-03-10 10:41:31 +01:00
MasterScore* MTest::readScore(const QString& name)
2012-05-26 14:49:10 +02:00
{
QString path = root + "/" + name;
return readCreatedScore(path);
}
//---------------------------------------------------------
// readCreatedScore
//---------------------------------------------------------
2016-03-10 10:41:31 +01:00
MasterScore* MTest::readCreatedScore(const QString& name)
2012-05-26 14:49:10 +02:00
{
2016-03-10 10:41:31 +01:00
MasterScore* score = new MasterScore(mscore->baseStyle());
2014-10-28 17:19:06 +01:00
QFileInfo fi(name);
score->setName(fi.completeBaseName());
QString csl = fi.suffix().toLower();
2012-05-26 14:49:10 +02:00
ScoreLoad sl;
2012-10-01 10:31:55 +02:00
Score::FileError rv;
2016-09-22 12:02:27 +02:00
if (csl == "cap") {
2013-04-25 21:37:19 +02:00
rv = importCapella(score, name);
2016-09-22 12:02:27 +02:00
score->setMetaTag("originalFormat", csl);
}
else if (csl == "capx") {
2013-04-25 21:37:19 +02:00
rv = importCapXml(score, name);
2016-09-22 12:02:27 +02:00
score->setMetaTag("originalFormat", csl);
}
else if (csl == "ove")
rv = importOve(score, name);
2013-05-07 13:36:21 +02:00
else if (csl == "sgu")
rv = importBB(score, name);
2013-04-25 21:37:19 +02:00
else if (csl == "mscz" || csl == "mscx")
2012-10-01 10:31:55 +02:00
rv = score->loadMsc(name, false);
2012-10-13 13:47:57 +02:00
else if (csl == "mxl")
rv = importCompressedMusicXml(score, name);
2012-06-04 10:05:18 +02:00
#ifdef OMR
2012-05-26 14:49:10 +02:00
else if (csl == "pdf")
rv = importPdf(score, name);
2012-06-04 10:05:18 +02:00
#endif
else if (csl == "xml" || csl == "musicxml")
2012-09-27 21:34:36 +02:00
rv = importMusicXml(score, name);
else if (csl == "gp3" || csl == "gp4" || csl == "gp5" || csl == "gpx")
2013-08-01 08:17:21 +02:00
rv = importGTP(score, name);
2012-10-01 10:31:55 +02:00
else
rv = Score::FileError::FILE_UNKNOWN_TYPE;
2012-10-01 10:31:55 +02:00
if (rv != Score::FileError::FILE_NO_ERROR) {
2012-05-26 14:49:10 +02:00
QWARN(qPrintable(QString("readScore: cannot load <%1> type <%2>\n").arg(name).arg(csl)));
delete score;
2018-06-27 09:04:11 +02:00
score = 0;
2012-05-26 14:49:10 +02:00
}
2018-06-27 09:04:11 +02:00
else {
for (Score* s : score->scoreList())
s->doLayout();
}
2012-05-26 14:49:10 +02:00
return score;
}
//---------------------------------------------------------
// saveScore
//---------------------------------------------------------
2016-04-13 13:07:32 +02:00
bool MTest::saveScore(Score* score, const QString& name) const
2012-05-26 14:49:10 +02:00
{
QFileInfo fi(name);
// MScore::testMode = true;
2016-03-10 10:41:31 +01:00
return score->Score::saveFile(fi);
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
2012-09-27 21:34:36 +02:00
// compareFiles
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
bool MTest::compareFiles(const QString& saveName, const QString& compareWith) const
2012-05-26 14:49:10 +02:00
{
QString cmd = "diff";
QStringList args;
args.append("-u");
2016-10-10 14:16:13 +02:00
args.append("--strip-trailing-cr");
2012-05-26 14:49:10 +02:00
args.append(root + "/" + compareWith);
args.append(saveName);
2013-05-16 12:48:23 +02:00
QProcess p;
qDebug() << "Running " << cmd << " with arg1: " << compareWith << " and arg2: " << saveName;
2013-05-16 12:48:23 +02:00
p.start(cmd, args);
2015-04-13 17:12:21 +02:00
if (!p.waitForFinished() || p.exitCode()) {
2013-05-16 12:48:23 +02:00
QByteArray ba = p.readAll();
2015-04-20 14:38:13 +02:00
//qDebug("%s", qPrintable(ba));
//qDebug(" <diff -u %s %s failed", qPrintable(compareWith),
// qPrintable(QString(root + "/" + saveName)));
2015-04-20 14:38:13 +02:00
QTextStream outputText(stdout);
outputText << QString(ba);
outputText << QString(" <diff -u %1 %2 failed").arg(QString(compareWith)).arg(QString(root + "/" + saveName));
2012-05-26 14:49:10 +02:00
return false;
}
return true;
}
2012-09-27 21:34:36 +02:00
//---------------------------------------------------------
// saveCompareScore
//---------------------------------------------------------
2016-04-13 13:07:32 +02:00
// bool MTest::saveCompareScore(MasterScore* score, const QString& saveName, const QString& compareWith) const
bool MTest::saveCompareScore(Score* score, const QString& saveName, const QString& compareWith) const
2012-09-27 21:34:36 +02:00
{
2015-04-13 17:12:21 +02:00
if (!saveScore(score, saveName))
return false;
2012-09-27 21:34:36 +02:00
return compareFiles(saveName, compareWith);
}
//---------------------------------------------------------
// saveCompareMusicXMLScore
//---------------------------------------------------------
2016-03-10 10:41:31 +01:00
bool MTest::saveCompareMusicXmlScore(MasterScore* score, const QString& saveName, const QString& compareWith)
2012-09-27 21:34:36 +02:00
{
saveMusicXml(score, saveName);
return compareFiles(saveName, compareWith);
}
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
// savePdf
//---------------------------------------------------------
2016-03-10 10:41:31 +01:00
bool MTest::savePdf(MasterScore* cs, const QString& saveName)
2012-05-26 14:49:10 +02:00
{
QPrinter printerDev(QPrinter::HighResolution);
2018-03-27 15:36:00 +02:00
double w = cs->styleD(Sid::pageWidth);
double h = cs->styleD(Sid::pageHeight);
printerDev.setPaperSize(QSizeF(w,h), QPrinter::Inch);
2012-05-26 14:49:10 +02:00
printerDev.setCreator("MuseScore Version: " VERSION);
printerDev.setFullPage(true);
printerDev.setColorMode(QPrinter::Color);
2017-02-17 15:48:17 +01:00
// printerDev.setDocName(cs->name());
2012-05-26 14:49:10 +02:00
printerDev.setOutputFormat(QPrinter::PdfFormat);
printerDev.setOutputFileName(saveName);
QPainter p(&printerDev);
p.setRenderHint(QPainter::Antialiasing, true);
p.setRenderHint(QPainter::TextAntialiasing, true);
2015-11-16 14:24:47 +01:00
double mag = printerDev.logicalDpiX() / DPI;
2012-05-26 14:49:10 +02:00
p.scale(mag, mag);
const QList<Page*> pl = cs->pages();
int pages = pl.size();
int offset = cs->pageNumberOffset();
int fromPage = printerDev.fromPage() - 1 - offset;
int toPage = printerDev.toPage() - 1 - offset;
if (fromPage < 0)
fromPage = 0;
if ((toPage < 0) || (toPage >= pages))
toPage = pages - 1;
for (int copy = 0; copy < printerDev.numCopies(); ++copy) {
bool firstPage = true;
for (int n = fromPage; n <= toPage; ++n) {
if (!firstPage)
printerDev.newPage();
firstPage = false;
cs->print(&p, n);
if ((copy + 1) < printerDev.numCopies())
printerDev.newPage();
}
}
p.end();
return true;
}
2012-09-27 21:34:36 +02:00
//---------------------------------------------------------
// saveMusicXml
//---------------------------------------------------------
2016-03-10 10:41:31 +01:00
bool MTest::saveMusicXml(MasterScore* score, const QString& saveName)
2012-09-27 21:34:36 +02:00
{
return saveXml(score, saveName);
}
//---------------------------------------------------------
// saveMimeData
//---------------------------------------------------------
bool MTest::saveMimeData(QByteArray mimeData, const QString& saveName)
{
QFile f(saveName);
if (!f.open(QIODevice::WriteOnly))
return false;
f.write(mimeData);
return f.error() == QFile::NoError;
}
//---------------------------------------------------------
// saveCompareMimeData
//---------------------------------------------------------
bool MTest::saveCompareMimeData(QByteArray mimeData, const QString& saveName, const QString& compareWith)
{
saveMimeData(mimeData, saveName);
return compareFiles(saveName, compareWith);
}
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
// initMTest
//---------------------------------------------------------
void MTest::initMTest()
{
2018-10-24 12:29:44 +02:00
qputenv("QML_DISABLE_DISK_CACHE", "true");
qSetMessagePattern("%{function}: %{message}");
initMyResources();
2015-11-16 14:24:47 +01:00
// DPI = 120;
// PDPI = 120;
2014-02-28 11:14:43 +01:00
MScore::noGui = true;
2012-05-26 14:49:10 +02:00
synti = new MasterSynthesizer();
2012-05-26 14:49:10 +02:00
mscore = new MScore;
2015-02-03 14:18:29 +01:00
new MuseScoreCore;
2012-05-26 14:49:10 +02:00
mscore->init();
ed.init();
preferences.init(true);
2012-05-26 14:49:10 +02:00
root = TESTROOT "/mtest";
loadInstrumentTemplates(":/instruments.xml");
2016-04-20 09:30:26 +02:00
score = readScore("test.mscx");
2012-05-26 14:49:10 +02:00
}
2013-05-13 20:24:03 +02:00
}
2012-05-26 14:49:10 +02:00