1 //=============================================================================
2 //  MuseScore
3 //  Music Composition & Notation
4 //
5 //  Copyright (C) 2012 Werner Schweer
6 //
7 //  This program is free software; you can redistribute it and/or modify
8 //  it under the terms of the GNU General Public License version 2
9 //  as published by the Free Software Foundation and appearing in
10 //  the file LICENCE.GPL
11 //=============================================================================
12 
13 #include <QtTest/QtTest>
14 #include "mtest/testutils.h"
15 #include "libmscore/excerpt.h"
16 #include "libmscore/score.h"
17 #include "libmscore/spanner.h"
18 
19 #define DIR QString("libmscore/remove/")
20 
21 using namespace Ms;
22 
23 //---------------------------------------------------------
24 //   TestRemove
25 //---------------------------------------------------------
26 
27 class TestRemove : public QObject, public MTest
28       {
29       Q_OBJECT
30 
31    private slots:
32       void initTestCase();
33       void removeStaff();
34       };
35 
36 //---------------------------------------------------------
37 //   initTestCase
38 //---------------------------------------------------------
39 
initTestCase()40 void TestRemove::initTestCase()
41       {
42       initMTest();
43       }
44 
45 //---------------------------------------------------------
46 //   StaffCheckData
47 //    For passing to the defined below inStaff() function.
48 //---------------------------------------------------------
49 
50 struct StaffCheckData {
51       int staffIdx;
52       bool staffHasElements;
53       };
54 
55 //---------------------------------------------------------
56 //   inStaff
57 //    for usage with Score::scanElements to check whether
58 //    the element belongs to a staff with a certain number.
59 //---------------------------------------------------------
60 
inStaff(void * staffCheckData,Element * e)61 static void inStaff(void* staffCheckData, Element* e)
62       {
63       StaffCheckData* checkData = static_cast<StaffCheckData*>(staffCheckData);
64       if (e->staffIdx() == checkData->staffIdx) {
65             qDebug() << e->name() << "is in staff" << checkData->staffIdx;
66             checkData->staffHasElements = true;
67             }
68       }
69 
70 //---------------------------------------------------------
71 //   staffHasElements
72 //---------------------------------------------------------
73 
staffHasElements(Score * score,int staffIdx)74 static bool staffHasElements(Score* score, int staffIdx)
75       {
76       for (auto i = score->spannerMap().cbegin(); i != score->spannerMap().cend(); ++i) {
77             Spanner* s = i->second;
78             if (s->staffIdx() == staffIdx) {
79                   qDebug() << s->name() << "is in staff" << staffIdx;
80                   return true;
81                   }
82             }
83       for (Spanner* s : score->unmanagedSpanners()) {
84             if (s->staffIdx() == staffIdx) {
85                   qDebug() << s->name() << "is in staff" << staffIdx;
86                   return true;
87                   }
88             }
89       StaffCheckData checkData { staffIdx, false };
90       score->scanElements(&checkData, inStaff, true);
91       return checkData.staffHasElements;
92       }
93 
94 //---------------------------------------------------------
95 //   removeStaff
96 //    Checks that after a staff removal all elements
97 //    belonging to it are removed in all parts.
98 //---------------------------------------------------------
99 
removeStaff()100 void TestRemove::removeStaff()
101       {
102       MasterScore* score = readScore(DIR + "remove_staff.mscx");
103 
104       // Remove the second staff and see what happens
105       score->startCmd();
106       score->cmdRemoveStaff(1);
107       score->endCmd();
108 
109       QVERIFY(!staffHasElements(score, 1));
110       for (Excerpt* ex : score->excerpts()) {
111             Score* s = ex->partScore();
112             QVERIFY(!staffHasElements(s, 1));
113             }
114 
115       delete score;
116       }
117 
118 QTEST_MAIN(TestRemove);
119 #include "tst_remove.moc"
120 
121