forked from musescore/MuseScore
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtestutils.cpp
353 lines (302 loc) · 11 KB
/
testutils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2012-2013 Werner Schweer
//
// 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>
#include <QTextStream>
#include "config.h"
#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"
#include "mscore/preferences.h"
#include "libmscore/page.h"
#include "audio/midi/msynthesizer.h"
#include "libmscore/musescoreCore.h"
#include "mscore/shortcut.h"
#include "libmscore/xml.h"
#include "libmscore/excerpt.h"
#include "thirdparty/qzip/qzipreader_p.h"
static 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_Leland);
Q_INIT_RESOURCE(musescorefonts_MuseJazz);
Q_INIT_RESOURCE(musescorefonts_Edwin);
Q_INIT_RESOURCE(musescorefonts_FreeSerif);
Q_INIT_RESOURCE(musescorefonts_Free);
Q_INIT_RESOURCE(musescorefonts_Petaluma);
}
namespace Ms {
//---------------------------------------------------------
// writeReadElement
// writes and element and reads it back
//---------------------------------------------------------
Element* MTest::writeReadElement(Element* element)
{
//
// write element
//
QBuffer buffer;
buffer.open(QIODevice::WriteOnly);
XmlWriter xml(element->score(), &buffer);
xml.header();
element->write(xml);
buffer.close();
//
// read element
//
XmlReader e(buffer.buffer());
e.readNextStartElement();
QString tag(e.name().toString());
element = Element::name2Element(e.name(), score);
element->read(e);
return element;
}
//---------------------------------------------------------
// MTest
//---------------------------------------------------------
MTest::MTest()
: ed(0)
{
MScore::testMode = true;
}
//---------------------------------------------------------
// readScore
//---------------------------------------------------------
MasterScore* MTest::readScore(const QString& name)
{
QString path = root + "/" + name;
return readCreatedScore(path);
}
//---------------------------------------------------------
// readCreatedScore
//---------------------------------------------------------
MasterScore* MTest::readCreatedScore(const QString& name)
{
MasterScore* score = new MasterScore(mscore->baseStyle());
QFileInfo fi(name);
score->setName(fi.completeBaseName());
QString csl = fi.suffix().toLower();
ScoreLoad sl;
Score::FileError rv;
if (csl == "cap") {
rv = importCapella(score, name);
score->setMetaTag("originalFormat", csl);
}
else if (csl == "capx") {
rv = importCapXml(score, name);
score->setMetaTag("originalFormat", csl);
}
else if (csl == "ove")
rv = importOve(score, name);
else if (csl == "sgu")
rv = importBB(score, name);
else if (csl == "mscz" || csl == "mscx")
rv = score->loadMsc(name, false);
else if (csl == "mxl")
rv = importCompressedMusicXml(score, name);
#ifdef OMR
else if (csl == "pdf")
rv = importPdf(score, name);
#endif
else if (csl == "xml" || csl == "musicxml")
rv = importMusicXml(score, name);
else if (csl == "gp3" || csl == "gp4" || csl == "gp5" || csl == "gpx" || csl == "gp" || csl == "ptb")
rv = importGTP(score, name);
else
rv = Score::FileError::FILE_UNKNOWN_TYPE;
if (rv != Score::FileError::FILE_NO_ERROR) {
QWARN(qPrintable(QString("readScore: cannot load <%1> type <%2>\n").arg(name).arg(csl)));
delete score;
score = 0;
}
else {
for (Score* s : score->scoreList())
s->doLayout();
}
return score;
}
//---------------------------------------------------------
// saveScore
//---------------------------------------------------------
bool MTest::saveScore(Score* score, const QString& name) const
{
QFileInfo fi(name);
// MScore::testMode = true;
return score->Score::saveFile(fi);
}
//---------------------------------------------------------
// compareFiles
//---------------------------------------------------------
bool MTest::compareFilesFromPaths(const QString& f1, const QString& f2)
{
QString cmd = "diff";
QStringList args;
args.append("-u");
args.append("--strip-trailing-cr");
args.append(f2);
args.append(f1);
QProcess p;
qDebug() << "Running " << cmd << " with arg1: " << QFileInfo(f2).fileName() << " and arg2: " << QFileInfo(f1).fileName();
p.start(cmd, args);
if (!p.waitForFinished() || p.exitCode()) {
QByteArray ba = p.readAll();
//qDebug("%s", qPrintable(ba));
//qDebug(" <diff -u %s %s failed", qPrintable(compareWith),
// qPrintable(QString(root + "/" + saveName)));
QTextStream outputText(stdout);
outputText << QString(ba);
outputText << QString(" <diff -u %1 %2 failed").arg(f2).arg(f1);
return false;
}
return true;
}
bool MTest::compareFiles(const QString& saveName, const QString& compareWith) const
{
return compareFilesFromPaths(saveName, root + "/" + compareWith);
}
//---------------------------------------------------------
// saveCompareScore
//---------------------------------------------------------
// bool MTest::saveCompareScore(MasterScore* score, const QString& saveName, const QString& compareWith) const
bool MTest::saveCompareScore(Score* score, const QString& saveName, const QString& compareWith) const
{
if (!saveScore(score, saveName))
return false;
return compareFiles(saveName, compareWith);
}
//---------------------------------------------------------
// saveCompareMusicXMLScore
//---------------------------------------------------------
bool MTest::saveCompareMusicXmlScore(MasterScore* score, const QString& saveName, const QString& compareWith)
{
saveMusicXml(score, saveName);
return compareFiles(saveName, compareWith);
}
//---------------------------------------------------------
// savePdf
//---------------------------------------------------------
bool MTest::savePdf(MasterScore* cs, const QString& saveName)
{
QPrinter printerDev(QPrinter::HighResolution);
double w = cs->styleD(Sid::pageWidth);
double h = cs->styleD(Sid::pageHeight);
printerDev.setPaperSize(QSizeF(w,h), QPrinter::Inch);
printerDev.setCreator("MuseScore Version: " VERSION);
printerDev.setFullPage(true);
printerDev.setColorMode(QPrinter::Color);
// printerDev.setDocName(cs->name());
printerDev.setOutputFormat(QPrinter::PdfFormat);
printerDev.setOutputFileName(saveName);
QPainter p(&printerDev);
p.setRenderHint(QPainter::Antialiasing, true);
p.setRenderHint(QPainter::TextAntialiasing, true);
double mag = printerDev.logicalDpiX() / DPI;
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;
}
//---------------------------------------------------------
// saveMusicXml
//---------------------------------------------------------
bool MTest::saveMusicXml(MasterScore* score, const QString& saveName)
{
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);
}
//---------------------------------------------------------
// 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);
if (rootfile.isEmpty()) {
qDebug("can't find rootfile in: %s", qPrintable(zipFile));
return;
}
const QByteArray ba = f.fileData(rootfile);
QFile out(destination);
if (!out.open(QIODevice::WriteOnly))
return;
out.write(ba);
out.close();
}
QString MTest::rootPath()
{
return TESTROOT "/mtest/";
}
//---------------------------------------------------------
// initMTest
//---------------------------------------------------------
void MTest::initMTest()
{
qputenv("QML_DISABLE_DISK_CACHE", "true");
qSetMessagePattern("%{function}: %{message}");
initMyResources();
// DPI = 120;
// PDPI = 120;
MScore::noGui = true;
synti = new MasterSynthesizer();
mscore = new MScore;
new MuseScoreCore;
mscore->init();
preferences.init(true);
root = TESTROOT "/mtest";
loadInstrumentTemplates(":/instruments.xml");
score = readScore("test.mscx");
}
}