MuseScore/mtest/testutils.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

363 lines
10 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/musescore.h"
2012-05-26 14:49:10 +02:00
#include "mscore/preferences.h"
#include "libmscore/page.h"
#include "audio/midi/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"
2014-04-09 16:09:21 +02:00
#include "libmscore/xml.h"
2014-05-08 17:59:24 +02:00
#include "libmscore/excerpt.h"
#include "thirdparty/qzip/qzipreader_p.h"
2012-05-26 14:49:10 +02:00
static void initMyResources()
{
Q_INIT_RESOURCE(mtest);
2020-09-29 10:15:51 +02:00
Q_INIT_RESOURCE(fonts_Leland);
2020-09-01 13:06:23 +02:00
Q_INIT_RESOURCE(fonts_Bravura);
Q_INIT_RESOURCE(fonts_Campania);
Q_INIT_RESOURCE(fonts_Free);
Q_INIT_RESOURCE(fonts_FreeSerif);
Q_INIT_RESOURCE(fonts_Gootville);
Q_INIT_RESOURCE(fonts_MScore);
Q_INIT_RESOURCE(fonts_MuseJazz);
Q_INIT_RESOURCE(fonts_Smufl);
Q_INIT_RESOURCE(fonts_Tabulature);
}
2013-05-13 20:24:03 +02:00
namespace Ms {
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();
2020-05-26 15:54:26 +02:00
2012-05-26 14:49:10 +02:00
//
// read element
//
2020-05-26 15:54:26 +02:00
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
//---------------------------------------------------------
2020-05-29 18:47:27 +02:00
MTest::MTest()
: 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();
2020-05-26 15:54:26 +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);
2020-05-26 15:54:26 +02:00
}
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);
2020-05-26 15:54:26 +02:00
}
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);
2020-03-27 13:41:24 +01:00
} else if (csl == "gp3" || csl == "gp4" || csl == "gp5" || csl == "gpx" || csl == "gp" || csl == "ptb") {
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;
2020-05-26 15:54:26 +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;
} else {
for (Score* s : score->scoreList()) {
s->doLayout();
2020-05-26 15:54:26 +02:00
}
2018-06-27 09:04:11 +02:00
}
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::compareFilesFromPaths(const QString& f1, const QString& f2)
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");
args.append(f2);
args.append(f1);
2013-05-16 12:48:23 +02:00
QProcess p;
qDebug() << "Running " << cmd << " with arg1: " << QFileInfo(f2).fileName() << " and arg2: "
<< QFileInfo(f1).fileName();
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(f2).arg(f1);
2012-05-26 14:49:10 +02:00
return false;
}
return true;
}
bool MTest::compareFiles(const QString& saveName, const QString& compareWith) const
{
return compareFilesFromPaths(saveName, root + "/" + compareWith);
}
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;
2020-05-26 15:54:26 +02:00
}
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);
2020-05-26 15:54:26 +02:00
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);
2020-05-26 15:54:26 +02:00
2012-05-26 14:49:10 +02:00
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);
2020-05-26 15:54:26 +02:00
2012-05-26 14:49:10 +02:00
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;
2020-05-26 15:54:26 +02:00
}
2012-05-26 14:49:10 +02:00
if ((toPage < 0) || (toPage >= pages)) {
toPage = pages - 1;
2020-05-26 15:54:26 +02:00
}
2012-05-26 14:49:10 +02:00
for (int copy = 0; copy < printerDev.numCopies(); ++copy) {
bool firstPage = true;
for (int n = fromPage; n <= toPage; ++n) {
if (!firstPage) {
printerDev.newPage();
2020-05-26 15:54:26 +02:00
}
2012-05-26 14:49:10 +02:00
firstPage = false;
2020-05-26 15:54:26 +02:00
2012-05-26 14:49:10 +02:00
cs->print(&p, n);
if ((copy + 1) < printerDev.numCopies()) {
printerDev.newPage();
}
2020-05-26 15:54:26 +02:00
}
2012-05-26 14:49:10 +02:00
}
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;
2020-05-26 15:54:26 +02:00
}
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);
}
//---------------------------------------------------------
// extractRootFile
//---------------------------------------------------------
extern QString readRootFile(MQZipReader*, QList<QString>&);
void MTest::extractRootFile(const QString& zipFile, const QString& destination)
{
MQZipReader f(zipFile);
QList<QString> images;
const QString rootfile = readRootFile(&f, images);
2020-05-26 15:54:26 +02:00
if (rootfile.isEmpty()) {
qDebug("can't find rootfile in: %s", qPrintable(zipFile));
return;
}
2020-05-26 15:54:26 +02:00
const QByteArray ba = f.fileData(rootfile);
2020-05-26 15:54:26 +02:00
QFile out(destination);
if (!out.open(QIODevice::WriteOnly)) {
return;
2020-05-26 15:54:26 +02:00
}
out.write(ba);
out.close();
}
QString MTest::rootPath()
{
return TESTROOT "/mtest/";
}
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();
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
}