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