1 /*
2  *  Copyright (c) 2011 Dmitry Kazakov <dimula73@gmail.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "kis_stroke_test.h"
20 #include <QTest>
21 
22 #include "kis_stroke.h"
23 #include "scheduler_utils.h"
24 
testRegularStroke()25 void KisStrokeTest::testRegularStroke()
26 {
27     KisStroke stroke(new KisTestingStrokeStrategy());
28     QQueue<KisStrokeJob*> &queue = stroke.testingGetQueue();
29 
30     QCOMPARE(queue.size(), 1);
31     SCOMPARE(getJobName(queue[0]), "init");
32     QCOMPARE(stroke.isEnded(), false);
33 
34     stroke.addJob(0);
35 
36     QCOMPARE(queue.size(), 2);
37     SCOMPARE(getJobName(queue[0]), "init");
38     SCOMPARE(getJobName(queue[1]), "dab");
39     QCOMPARE(stroke.isEnded(), false);
40 
41     stroke.endStroke();
42 
43     QCOMPARE(queue.size(), 3);
44     SCOMPARE(getJobName(queue[0]), "init");
45     SCOMPARE(getJobName(queue[1]), "dab");
46     SCOMPARE(getJobName(queue[2]), "finish");
47     QCOMPARE(stroke.isEnded(), true);
48 
49     // uncomment this line to catch an assert:
50     // stroke.addJob(0);
51 
52     KisStrokeJob* job;
53 
54     job = stroke.popOneJob();
55     delete job;
56     QCOMPARE(queue.size(), 2);
57     SCOMPARE(getJobName(queue[0]), "dab");
58     SCOMPARE(getJobName(queue[1]), "finish");
59 
60     job = stroke.popOneJob();
61     delete job;
62     QCOMPARE(queue.size(), 1);
63     SCOMPARE(getJobName(queue[0]), "finish");
64 
65     job = stroke.popOneJob();
66     delete job;
67     QCOMPARE(queue.size(), 0);
68 
69     job = stroke.popOneJob();
70     QCOMPARE(job, (KisStrokeJob*)0);
71 }
72 
testCancelStrokeCase1()73 void KisStrokeTest::testCancelStrokeCase1()
74 {
75     KisStroke stroke(new KisTestingStrokeStrategy());
76     QQueue<KisStrokeJob*> &queue = stroke.testingGetQueue();
77 
78     stroke.addJob(0);
79 
80     // "not initialized, has jobs"
81 
82     QCOMPARE(queue.size(), 2);
83     SCOMPARE(getJobName(queue[0]), "init");
84     SCOMPARE(getJobName(queue[1]), "dab");
85     QCOMPARE(stroke.isEnded(), false);
86 
87     stroke.cancelStroke();
88 
89     QCOMPARE(queue.size(), 0);
90     QCOMPARE(stroke.isEnded(), true);
91 
92     stroke.clearQueueOnCancel();
93 }
94 
testCancelStrokeCase2and3()95 void KisStrokeTest::testCancelStrokeCase2and3()
96 {
97     KisStroke stroke(new KisTestingStrokeStrategy());
98     QQueue<KisStrokeJob*> &queue = stroke.testingGetQueue();
99 
100     stroke.addJob(0);
101     delete stroke.popOneJob();
102 
103     // "initialized, has jobs"
104 
105     QCOMPARE(queue.size(), 1);
106     SCOMPARE(getJobName(queue[0]), "dab");
107     QCOMPARE(stroke.isEnded(), false);
108 
109     stroke.cancelStroke();
110 
111     QCOMPARE(queue.size(), 1);
112     SCOMPARE(getJobName(queue[0]), "cancel");
113     QCOMPARE(stroke.isEnded(), true);
114 
115     stroke.clearQueueOnCancel();
116 }
117 
testCancelStrokeCase5()118 void KisStrokeTest::testCancelStrokeCase5()
119 {
120     KisStroke stroke(new KisTestingStrokeStrategy());
121     QQueue<KisStrokeJob*> &queue = stroke.testingGetQueue();
122 
123     // initialized, no jobs, not finished
124 
125     stroke.addJob(0);
126     delete stroke.popOneJob(); // init
127     delete stroke.popOneJob(); // dab
128 
129     QCOMPARE(stroke.isEnded(), false);
130 
131     stroke.cancelStroke();
132     QCOMPARE(queue.size(), 1);
133     SCOMPARE(getJobName(queue[0]), "cancel");
134     QCOMPARE(stroke.isEnded(), true);
135 
136     delete stroke.popOneJob(); // cancel
137 }
138 
testCancelStrokeCase4()139 void KisStrokeTest::testCancelStrokeCase4()
140 {
141     KisStroke stroke(new KisTestingStrokeStrategy());
142     QQueue<KisStrokeJob*> &queue = stroke.testingGetQueue();
143 
144     stroke.addJob(0);
145     stroke.endStroke();
146     delete stroke.popOneJob(); // init
147     delete stroke.popOneJob(); // dab
148     delete stroke.popOneJob(); // finish
149 
150     QCOMPARE(stroke.isEnded(), true);
151 
152     stroke.cancelStroke();
153     QCOMPARE(queue.size(), 0);
154     QCOMPARE(stroke.isEnded(), true);
155 }
156 
testCancelStrokeCase6()157 void KisStrokeTest::testCancelStrokeCase6()
158 {
159     KisStroke stroke(new KisTestingStrokeStrategy());
160     QQueue<KisStrokeJob*> &queue = stroke.testingGetQueue();
161 
162     stroke.addJob(0);
163     delete stroke.popOneJob();
164 
165     // "initialized, has jobs"
166 
167     QCOMPARE(queue.size(), 1);
168     SCOMPARE(getJobName(queue[0]), "dab");
169     QCOMPARE(stroke.isEnded(), false);
170 
171     // "cancelled"
172 
173     stroke.cancelStroke();
174 
175     QCOMPARE(queue.size(), 1);
176     SCOMPARE(getJobName(queue[0]), "cancel");
177     QCOMPARE(stroke.isEnded(), true);
178 
179     int seqNo = cancelSeqNo(queue.head());
180 
181     // try cancel once more...
182 
183     stroke.cancelStroke();
184 
185     QCOMPARE(queue.size(), 1);
186     SCOMPARE(getJobName(queue[0]), "cancel");
187     QCOMPARE(stroke.isEnded(), true);
188     QCOMPARE(cancelSeqNo(queue.head()), seqNo);
189 
190     stroke.clearQueueOnCancel();
191 }
192 
193 QTEST_MAIN(KisStrokeTest)
194