summaryrefslogtreecommitdiff
path: root/tests/auto/timeline/timelinenotesmodel/tst_timelinenotesmodel.cpp
blob: 27cf3c1ddcd907a004c6af8474645fa7413d1a94 (plain)
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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include <QtTest>
#include <QColor>
#include <timeline/timelinenotesmodel.h>

class tst_TimelineNotesModel : public QObject
{
    Q_OBJECT

private slots:
    void timelineModel();
    void addRemove();
    void properties();
    void selection();
    void modify();
};

class TestModel : public Timeline::TimelineModel {
public:
    TestModel(int modelId = 10) : TimelineModel(modelId, QString())
    {
        insert(0, 10, 10);
    }

    int typeId(int) const
    {
        return 7;
    }
};

class TestNotesModel : public Timeline::TimelineNotesModel {
    friend class tst_TimelineNotesModel;
};

void tst_TimelineNotesModel::timelineModel()
{
    TestNotesModel notes;
    TestModel *model = new TestModel;
    TestModel *model2 = new TestModel(2);
    notes.addTimelineModel(model);
    notes.addTimelineModel(model2);
    QCOMPARE(notes.timelineModelByModelId(10), model);
    QCOMPARE(notes.timelineModels().count(), 2);
    QVERIFY(notes.timelineModels().contains(model));
    QVERIFY(notes.timelineModels().contains(model2));
    QVERIFY(!notes.isModified());
    notes.clear(); // clear() only clears the data, not the models
    QCOMPARE(notes.timelineModels().count(), 2);
    delete model; // notes model does monitor the destroyed() signal
    QCOMPARE(notes.timelineModels().count(), 1);
    delete model2;
}

void tst_TimelineNotesModel::addRemove()
{
    TestNotesModel notes;
    TestModel model;
    notes.addTimelineModel(&model);

    QSignalSpy spy(&notes, SIGNAL(changed(int,int,int)));
    int id = notes.add(10, 0, QLatin1String("xyz"));
    QCOMPARE(spy.count(), 1);
    QCOMPARE(notes.isModified(), true);
    QCOMPARE(notes.count(), 1);
    notes.resetModified();
    QCOMPARE(notes.isModified(), false);
    notes.remove(id);
    QCOMPARE(spy.count(), 2);
    QCOMPARE(notes.isModified(), true);
    QCOMPARE(notes.count(), 0);
}

void tst_TimelineNotesModel::properties()
{

    TestNotesModel notes;
    int id = -1;
    {
        TestModel model;
        notes.addTimelineModel(&model);

        id = notes.add(10, 0, QLatin1String("xyz"));
        QVERIFY(id >= 0);
        QCOMPARE(notes.typeId(id), 7);
        QCOMPARE(notes.timelineIndex(id), 0);
        QCOMPARE(notes.timelineModel(id), 10);
        QCOMPARE(notes.text(id), QLatin1String("xyz"));
    }

    QCOMPARE(notes.typeId(id), -1); // cannot ask the model anymore
    QCOMPARE(notes.timelineIndex(id), 0);
    QCOMPARE(notes.timelineModel(id), 10);
    QCOMPARE(notes.text(id), QLatin1String("xyz"));
}

void tst_TimelineNotesModel::selection()
{
    TestNotesModel notes;
    TestModel model;
    notes.addTimelineModel(&model);
    int id1 = notes.add(10, 0, QLatin1String("blablub"));
    int id2 = notes.add(10, 0, QLatin1String("xyz"));
    QVariantList ids = notes.byTimelineModel(10);
    QCOMPARE(ids.length(), 2);
    QVERIFY(ids.contains(id1));
    QVERIFY(ids.contains(id2));

    ids = notes.byTypeId(7);
    QCOMPARE(ids.length(), 2);
    QVERIFY(ids.contains(id1));
    QVERIFY(ids.contains(id2));

    int got = notes.get(10, 0);
    QVERIFY(got == id1 || got == id2);
    QCOMPARE(notes.get(10, 20), -1);
    QCOMPARE(notes.get(20, 10), -1);
}

void tst_TimelineNotesModel::modify()
{
    TestNotesModel notes;
    TestModel model;
    notes.addTimelineModel(&model);
    QSignalSpy spy(&notes, SIGNAL(changed(int,int,int)));
    int id = notes.add(10, 0, QLatin1String("a"));
    QCOMPARE(spy.count(), 1);
    notes.resetModified();
    notes.update(id, QLatin1String("b"));
    QVERIFY(notes.isModified());
    QCOMPARE(spy.count(), 2);
    QCOMPARE(notes.text(id), QLatin1String("b"));
    notes.resetModified();
    notes.update(id, QLatin1String("b"));
    QVERIFY(!notes.isModified());
    QCOMPARE(spy.count(), 2);
    QCOMPARE(notes.text(id), QLatin1String("b"));

    notes.setText(id, QLatin1String("a"));
    QVERIFY(notes.isModified());
    QCOMPARE(spy.count(), 3);
    QCOMPARE(notes.text(id), QLatin1String("a"));
    notes.resetModified();

    notes.setText(10, 0, QLatin1String("x"));
    QVERIFY(notes.isModified());
    QCOMPARE(spy.count(), 4);
    QCOMPARE(notes.text(id), QLatin1String("x"));
    notes.resetModified();

    TestModel model2(9);
    notes.addTimelineModel(&model2);
    notes.setText(9, 0, QLatin1String("hh"));
    QVERIFY(notes.isModified());
    QCOMPARE(spy.count(), 5);
    QCOMPARE(notes.count(), 2);
    notes.resetModified();

    notes.setText(id, QString());
    QVERIFY(notes.isModified());
    QCOMPARE(spy.count(), 6);
    QCOMPARE(notes.count(), 1);
    notes.resetModified();

}

QTEST_MAIN(tst_TimelineNotesModel)

#include "tst_timelinenotesmodel.moc"