File: test_notationview_selection.cpp

package info (click to toggle)
rosegarden 1%3A25.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,888 kB
  • sloc: cpp: 209,189; xml: 6,580; sh: 1,608; perl: 755; python: 416; ansic: 324; lisp: 139; ruby: 33; makefile: 20
file content (268 lines) | stat: -rw-r--r-- 9,398 bytes parent folder | download
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
#include "base/NotationTypes.h"
#include "base/Pitch.h"
#include "base/Segment.h"
#include "base/Selection.h"
#include <QTest>
#include "gui/seqmanager/SequenceManager.h"
#include "document/RosegardenDocument.h"
#include "gui/editors/notation/NotationView.h"

#if 0
int init()
{
    // ??? This causes the tests to fail completely.  I suspect there
    //     is a new warning being issued by Qt.
    qputenv("QT_FATAL_WARNINGS", "1");
    return 0;
}
Q_CONSTRUCTOR_FUNCTION(init)
#endif

using namespace Rosegarden;

// This test opens data/examples/test_selection.rg and simulates using the keyboard to navigate and select in the notation view
class TestNotationViewSelection : public QObject
{
    Q_OBJECT

public:
    TestNotationViewSelection() :
        m_doc(nullptr,  // parent
              {},  // audioPluginManager
              true,  // skipAutoload
              true,  // clearCommandHistory
              false)  // enableSound
    {
    }

private Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();
    void init();
    void testNavigate();
    void testKeyboardSelection_data();
    void testKeyboardSelection();
    void testSelectForwardAndBackward();

private:
    QString selectedNotes() const;

    RosegardenDocument m_doc;
    Segment *m_segment{nullptr};
    NotationView *m_view{nullptr};
    SequenceManager m_seqManager;
};

void TestNotationViewSelection::initTestCase()
{
    // Make sure settings end up in the right place.
    QCoreApplication::setOrganizationName("rosegardenmusic");

    // Make sure RD knows about itself.
    RosegardenDocument::currentDocument = &m_doc;

    // Loading from a file
    const QString input = QFINDTESTDATA("../data/examples/test_selection.rg");
    QVERIFY(!input.isEmpty()); // file not found
    m_doc.openDocument(input, false /*not permanent, i.e. don't create midi devices*/, true /*no progress dlg*/);

    const SegmentMultiSet segments = m_doc.getComposition().getSegments();
    QVERIFY(!segments.empty());
    m_segment = *segments.begin();
    std::vector<Segment *> segmentsVec;
    segmentsVec.push_back(m_segment);

    m_view = new NotationView(segmentsVec);

    QVERIFY(!m_view->getSelection());
    QCOMPARE(m_view->getCurrentSegment(), m_segment);
    QCOMPARE(m_segment->getStartTime(), timeT(0));

    m_seqManager.setDocument(&m_doc);

    // The mainwindow connects fast-forward and rewind (to intercept them when recording), so we need to do it ourselves here.
    connect(m_view, &NotationView::fastForwardPlayback,
            &m_seqManager, &SequenceManager::fastforward);
    connect(m_view, &NotationView::rewindPlayback,
            &m_seqManager, &SequenceManager::rewind);
    connect(m_view, &NotationView::fastForwardPlaybackToEnd,
            &m_seqManager, &SequenceManager::fastForwardToEnd);
    connect(m_view, &NotationView::rewindPlaybackToBeginning,
            &m_seqManager, &SequenceManager::rewindToBeginning);

}

void TestNotationViewSelection::cleanupTestCase()
{
    delete m_view;
}

// Returns the notes in the selection, as a string of note names. Ex: "ABBDG".
QString TestNotationViewSelection::selectedNotes() const
{
    EventSelection *selection = m_view->getSelection();
    if (!selection) {
        return QString();
    }
    const EventContainer &eventContainer = selection->getSegmentEvents();
    QString ret;
    Key defaultKey;
    for (EventContainer::const_iterator it = eventContainer.begin(); it != eventContainer.end(); ++it) {
        Event *ev = *it;
        if (ev->isa(Note::EventType)) {
            ret += Pitch(*ev).getNoteName(defaultKey);
        } else if (ev->isa(Note::EventRestType)) {
            ret += 'R';
        }
    }
    return ret;
}

void TestNotationViewSelection::init()
{
    // Before each test, unselect all and go back to position 0
    m_view->setSelection(nullptr, false);
    m_doc.slotSetPointerPosition(0);
}

void TestNotationViewSelection::testNavigate()
{
    // Go right one note
    m_view->slotStepForward();
    QCOMPARE(m_doc.getComposition().getPosition(), timeT(960)); // one quarter

    // Go to next bar
    m_seqManager.fastforward();
    QCOMPARE(m_doc.getComposition().getPosition(), timeT(3840)); // one quarter

    QVector<timeT> expectedPositions;
    expectedPositions << 960       // one quarter
                      << 960 * 2   // one rest
                      << 960 * 2.5 // one eighth
                      << 960 * 3   // another
                      << 960 * 3.5 // another
                      << 3840      // second bar
                      << 3840 + 480
                      << 3840 + 960
                      << 3840 + 960 * 2
                      << 7680
                      << 7680 + 3840
                      << 15360
                      << 15360 + 480
                      << 15360 + 480 * 2
                      << 15360 + 480 * 3
                      << 15360 + 480 * 4
                         ;
    for (int i = 0 ; i < expectedPositions.size(); ++i) {
        m_view->slotStepForward();
        QCOMPARE(m_doc.getComposition().getPosition(), timeT(3840 + expectedPositions.at(i)));
    }
}

void TestNotationViewSelection::testKeyboardSelection_data()
{
    QTest::addColumn<QString>("keysPressed");
    QTest::addColumn<QStringList>("expectedSelections");

    // Syntax for keyPressed:
    // l = left, r = right, L = shift-left, R = shift right
    // n = next bar (ctrl+right), N = select until next bar (ctrl+shift+right)
    // p = prev bar (ctrl+left), P = select until previous bar (ctrl+shift+left)

    // To understand expectedSelections, note that the beginning of the file says: ABCDG[rest]...
    QTest::newRow("1-3-5") << "RrRrR" << (QStringList() << "A" << "A" << "AC" << "AC" << "ACG");
    QTest::newRow("shift_change_direction_1") << "RRLR" << (QStringList() << "A" << "AB" << "A" << "AB");
    QTest::newRow("shift_change_direction_2") << "nLLRL" << (QStringList() << "" << "D" << "CD" << "D" << "CD");
    QTest::newRow("bug_1519_testcase_2") << "RrL" << (QStringList() << "A" << "A" << "AB");
    QTest::newRow("shift_right_again_same_note") << "RRlR" << (QStringList() << "A" << "AB" << "AB" << "A");
    QTest::newRow("shift_left_again_same_note") << "nLLrL" << (QStringList() << "" << "D" << "CD" << "CD" << "D");
    QTest::newRow("select_unselect_bar") << "NprNP" << (QStringList() << "ABCD" << "ABCD" << "ABCD" << "A" << "ABCD");
}

void TestNotationViewSelection::testKeyboardSelection()
{
    QFETCH(QString, keysPressed);
    QFETCH(QStringList, expectedSelections);
    for (int i = 0 ; i < keysPressed.size(); ++i) {
        const QChar key = keysPressed.at(i);
        switch (key.toLatin1()) {
        case 'l':
            m_view->slotStepBackward();
            break;
        case 'r':
            m_view->slotStepForward();
            break;
        case 'L':
            m_view->slotExtendSelectionBackward();
            break;
        case 'R':
            m_view->slotExtendSelectionForward();
            break;
        case 'n':
            m_seqManager.fastforward();
            break;
        case 'N':
            m_view->slotExtendSelectionForwardBar();
            break;
        case 'p':
            m_seqManager.rewind();
            break;
        case 'P':
            m_view->slotExtendSelectionBackwardBar();
            break;
        }
        const QString prefix = QString("step %1, key %2: ").arg(i).arg(key); // more info in case of failure
        QCOMPARE(prefix + selectedNotes(), prefix + expectedSelections.at(i));
    }
}

void TestNotationViewSelection::testSelectForwardAndBackward()
{
    m_seqManager.fastforward();

    QStringList expectedSelections;
    expectedSelections << "G"
                       << "G" // the rest doesn't get selected, currently
                       << "GB"
                       << "GBCC" // tied notes get selected together
                       << "GBCCBB"
                       << "GBCCBBGGG"
                       << "GBCCBBGGGCC"
                       << "GBCCBBGGGCCG"
                       << "GBCCBBGGGCCGBDBD"
                       << "GBCCBBGGGCCGBDBDG"
                       << "GBCCBBGGGCCGBDBDGC"
                       ;

    // select forward
    for (int i = 0 ; i < expectedSelections.size(); ++i) {
        m_view->slotExtendSelectionForward();
        QCOMPARE(selectedNotes(), expectedSelections.at(i));
    }

    const int pos = m_doc.getComposition().getPosition();

    // unselect backward
    QStringList expectedSelectionsBack = expectedSelections;
    std::reverse(expectedSelectionsBack.begin(), expectedSelectionsBack.end());
    expectedSelectionsBack.append(QString());

    for (int i = 0 ; i < expectedSelectionsBack.size(); ++i) {
        if (i > 0)
            m_view->slotExtendSelectionBackward();
        QCOMPARE(selectedNotes(), expectedSelectionsBack.at(i));
    }
    QCOMPARE(selectedNotes(), QString());

    // select everything backward, check at end
    m_doc.slotSetPointerPosition(pos);
    for (int i = 0 ; i < expectedSelections.size(); ++i) {
        m_view->slotExtendSelectionBackward();
    }
    QCOMPARE(m_doc.getComposition().getPosition(), timeT(3840)); // one quarter
    QCOMPARE(selectedNotes(), QString("GBCCBBGGGCCGDBDBGC")); // order of notes in the chords is reversed, doesn't matter
}

QTEST_MAIN(TestNotationViewSelection)

#include "test_notationview_selection.moc"