Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / utils / test_code / ProgressTest.java
... / ...
CommitLineData
1package be.nikiroo.utils.test_code;
2
3import be.nikiroo.utils.Progress;
4import be.nikiroo.utils.test.TestCase;
5import be.nikiroo.utils.test.TestLauncher;
6
7class ProgressTest extends TestLauncher {
8 public ProgressTest(String[] args) {
9 super("Progress reporting", args);
10
11 addSeries(new TestLauncher("Simple progress", args) {
12 {
13 addTest(new TestCase("Relative values and direct values") {
14 @Override
15 public void test() throws Exception {
16 Progress p = new Progress();
17 assertEquals(0, p.getProgress());
18 assertEquals(0, p.getRelativeProgress());
19 p.setProgress(33);
20 assertEquals(33, p.getProgress());
21 assertEquals(0.33, p.getRelativeProgress());
22 p.setMax(3);
23 p.setProgress(1);
24 assertEquals(1, p.getProgress());
25 assertEquals(
26 generateAssertMessage("0.33..",
27 p.getRelativeProgress()), true,
28 p.getRelativeProgress() >= 0.332);
29 assertEquals(
30 generateAssertMessage("0.33..",
31 p.getRelativeProgress()), true,
32 p.getRelativeProgress() <= 0.334);
33 }
34 });
35
36 addTest(new TestCase("Listeners at first level") {
37 int pg;
38
39 @Override
40 public void test() throws Exception {
41 Progress p = new Progress();
42 p.addProgressListener(new Progress.ProgressListener() {
43 @Override
44 public void progress(Progress progress, String name) {
45 pg = progress.getProgress();
46 }
47 });
48
49 p.setProgress(42);
50 assertEquals(42, pg);
51 p.setProgress(0);
52 assertEquals(0, pg);
53 }
54 });
55 }
56 });
57
58 addSeries(new TestLauncher("Progress with children", args) {
59 {
60 addTest(new TestCase("One child") {
61 @Override
62 public void test() throws Exception {
63 Progress p = new Progress();
64 Progress child = new Progress();
65
66 p.addProgress(child, 100);
67
68 child.setProgress(42);
69 assertEquals(42, p.getProgress());
70 }
71 });
72
73 addTest(new TestCase("Multiple children") {
74 @Override
75 public void test() throws Exception {
76 Progress p = new Progress();
77 Progress child1 = new Progress();
78 Progress child2 = new Progress();
79 Progress child3 = new Progress();
80
81 p.addProgress(child1, 20);
82 p.addProgress(child2, 60);
83 p.addProgress(child3, 20);
84
85 child1.setProgress(50);
86 assertEquals(10, p.getProgress());
87 child2.setProgress(100);
88 assertEquals(70, p.getProgress());
89 child3.setProgress(100);
90 assertEquals(90, p.getProgress());
91 child1.setProgress(100);
92 assertEquals(100, p.getProgress());
93 }
94 });
95
96 addTest(new TestCase("Listeners with children") {
97 int pg;
98
99 @Override
100 public void test() throws Exception {
101 final Progress p = new Progress();
102 Progress child1 = new Progress();
103 Progress child2 = new Progress();
104 p.addProgress(child1, 50);
105 p.addProgress(child2, 50);
106
107 p.addProgressListener(new Progress.ProgressListener() {
108 @Override
109 public void progress(Progress progress, String name) {
110 pg = p.getProgress();
111 }
112 });
113
114 child1.setProgress(50);
115 assertEquals(25, pg);
116 child2.setProgress(100);
117 assertEquals(75, pg);
118 child1.setProgress(100);
119 assertEquals(100, pg);
120 }
121 });
122
123 addTest(new TestCase("Listeners with children, not 1-100") {
124 int pg;
125
126 @Override
127 public void test() throws Exception {
128 final Progress p = new Progress();
129 p.setMax(1000);
130
131 Progress child1 = new Progress();
132 child1.setMax(2);
133
134 Progress child2 = new Progress();
135 p.addProgress(child1, 500);
136 p.addProgress(child2, 500);
137
138 p.addProgressListener(new Progress.ProgressListener() {
139 @Override
140 public void progress(Progress progress, String name) {
141 pg = p.getProgress();
142 }
143 });
144
145 child1.setProgress(1);
146 assertEquals(250, pg);
147 child2.setProgress(100);
148 assertEquals(750, pg);
149 child1.setProgress(2);
150 assertEquals(1000, pg);
151 }
152 });
153
154 addTest(new TestCase(
155 "Listeners with children, not 1-100, local progress") {
156 int pg;
157
158 @Override
159 public void test() throws Exception {
160 final Progress p = new Progress();
161 p.setMax(1000);
162
163 Progress child1 = new Progress();
164 child1.setMax(2);
165
166 Progress child2 = new Progress();
167 p.addProgress(child1, 400);
168 p.addProgress(child2, 400);
169 // 200 = local progress
170
171 p.addProgressListener(new Progress.ProgressListener() {
172 @Override
173 public void progress(Progress progress, String name) {
174 pg = p.getProgress();
175 }
176 });
177
178 child1.setProgress(1);
179 assertEquals(200, pg);
180 child2.setProgress(100);
181 assertEquals(600, pg);
182 p.setProgress(100);
183 assertEquals(700, pg);
184 child1.setProgress(2);
185 assertEquals(900, pg);
186 p.setProgress(200);
187 assertEquals(1000, pg);
188 }
189 });
190
191 addTest(new TestCase("Listeners with 5+ children, 4+ depth") {
192 int pg;
193
194 @Override
195 public void test() throws Exception {
196 final Progress p = new Progress();
197 Progress child1 = new Progress();
198 Progress child2 = new Progress();
199 p.addProgress(child1, 50);
200 p.addProgress(child2, 50);
201 Progress child11 = new Progress();
202 child1.addProgress(child11, 100);
203 Progress child111 = new Progress();
204 child11.addProgress(child111, 100);
205 Progress child1111 = new Progress();
206 child111.addProgress(child1111, 20);
207 Progress child1112 = new Progress();
208 child111.addProgress(child1112, 20);
209 Progress child1113 = new Progress();
210 child111.addProgress(child1113, 20);
211 Progress child1114 = new Progress();
212 child111.addProgress(child1114, 20);
213 Progress child1115 = new Progress();
214 child111.addProgress(child1115, 20);
215
216 p.addProgressListener(new Progress.ProgressListener() {
217 @Override
218 public void progress(Progress progress, String name) {
219 pg = p.getProgress();
220 }
221 });
222
223 child1111.setProgress(100);
224 child1112.setProgress(50);
225 child1113.setProgress(25);
226 child1114.setProgress(25);
227 child1115.setProgress(50);
228 assertEquals(25, pg);
229 child2.setProgress(100);
230 assertEquals(75, pg);
231 child1111.setProgress(100);
232 child1112.setProgress(100);
233 child1113.setProgress(100);
234 child1114.setProgress(100);
235 child1115.setProgress(100);
236 assertEquals(100, pg);
237 }
238 });
239
240 addTest(new TestCase("Listeners with children, multi-thread") {
241 int pg;
242 boolean decrease;
243 Object lock1 = new Object();
244 Object lock2 = new Object();
245 int currentStep1;
246 int currentStep2;
247
248 @Override
249 public void test() throws Exception {
250 final Progress p = new Progress(0, 200);
251
252 final Progress child1 = new Progress();
253 final Progress child2 = new Progress();
254 p.addProgress(child1, 100);
255 p.addProgress(child2, 100);
256
257 p.addProgressListener(new Progress.ProgressListener() {
258 @Override
259 public void progress(Progress progress, String name) {
260 int now = p.getProgress();
261 if (now < pg) {
262 decrease = true;
263 }
264 pg = now;
265 }
266 });
267
268 // Run 200 concurrent threads, 2 at a time allowed to
269 // make progress (each on a different child)
270 for (int i = 0; i <= 100; i++) {
271 final int step = i;
272 new Thread(new Runnable() {
273 @Override
274 public void run() {
275 synchronized (lock1) {
276 if (step > currentStep1) {
277 currentStep1 = step;
278 child1.setProgress(step);
279 }
280 }
281 }
282 }).start();
283
284 new Thread(new Runnable() {
285 @Override
286 public void run() {
287 synchronized (lock2) {
288 if (step > currentStep2) {
289 currentStep2 = step;
290 child2.setProgress(step);
291 }
292 }
293 }
294 }).start();
295 }
296
297 int i;
298 int timeout = 20; // in 1/10th of seconds
299 for (i = 0; i < timeout
300 && (currentStep1 + currentStep2) < 200; i++) {
301 Thread.sleep(100);
302 }
303
304 assertEquals("The test froze at step " + currentStep1
305 + " + " + currentStep2, true, i < timeout);
306 assertEquals(
307 "There should not have any decresing steps",
308 decrease, false);
309 assertEquals("The progress should have reached 200",
310 200, p.getProgress());
311 assertEquals(
312 "The progress should have reached completion",
313 true, p.isDone());
314 }
315 });
316 }
317 });
318 }
319}