1 package be
.nikiroo
.utils
.test
;
3 import be
.nikiroo
.utils
.Progress
;
5 class ProgressTest
extends TestLauncher
{
6 public ProgressTest(String
[] args
) {
7 super("Progress reporting", args
);
9 addSeries(new TestLauncher("Simple progress", args
) {
11 addTest(new TestCase("Relative values and direct values") {
13 public void test() throws Exception
{
14 Progress p
= new Progress();
15 assertEquals(0, p
.getProgress());
16 assertEquals(0, p
.getRelativeProgress());
18 assertEquals(33, p
.getProgress());
19 assertEquals(0.33, p
.getRelativeProgress());
22 assertEquals(1, p
.getProgress());
24 generateAssertMessage("0.33..",
25 p
.getRelativeProgress()), true,
26 p
.getRelativeProgress() >= 0.332);
28 generateAssertMessage("0.33..",
29 p
.getRelativeProgress()), true,
30 p
.getRelativeProgress() <= 0.334);
34 addTest(new TestCase("Listeners at first level") {
38 public void test() throws Exception
{
39 Progress p
= new Progress();
40 p
.addProgressListener(new Progress
.ProgressListener() {
42 public void progress(Progress progress
, String name
) {
43 pg
= progress
.getProgress();
56 addSeries(new TestLauncher("Progress with children", args
) {
58 addTest(new TestCase("One child") {
60 public void test() throws Exception
{
61 Progress p
= new Progress();
62 Progress child
= new Progress();
64 p
.addProgress(child
, 100);
66 child
.setProgress(42);
67 assertEquals(42, p
.getProgress());
71 addTest(new TestCase("Multiple children") {
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();
79 p
.addProgress(child1
, 20);
80 p
.addProgress(child2
, 60);
81 p
.addProgress(child3
, 20);
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());
94 addTest(new TestCase("Listeners with children") {
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);
105 p
.addProgressListener(new Progress
.ProgressListener() {
107 public void progress(Progress progress
, String name
) {
108 pg
= p
.getProgress();
112 child1
.setProgress(50);
113 assertEquals(25, pg
);
114 child2
.setProgress(100);
115 assertEquals(75, pg
);
116 child1
.setProgress(100);
117 assertEquals(100, pg
);
121 addTest(new TestCase("Listeners with children, not 1-100") {
125 public void test() throws Exception
{
126 final Progress p
= new Progress();
129 Progress child1
= new Progress();
132 Progress child2
= new Progress();
133 p
.addProgress(child1
, 500);
134 p
.addProgress(child2
, 500);
136 p
.addProgressListener(new Progress
.ProgressListener() {
138 public void progress(Progress progress
, String name
) {
139 pg
= p
.getProgress();
143 child1
.setProgress(1);
144 assertEquals(250, pg
);
145 child2
.setProgress(100);
146 assertEquals(750, pg
);
147 child1
.setProgress(2);
148 assertEquals(1000, pg
);
152 addTest(new TestCase(
153 "Listeners with children, not 1-100, local progress") {
157 public void test() throws Exception
{
158 final Progress p
= new Progress();
161 Progress child1
= new Progress();
164 Progress child2
= new Progress();
165 p
.addProgress(child1
, 400);
166 p
.addProgress(child2
, 400);
167 // 200 = local progress
169 p
.addProgressListener(new Progress
.ProgressListener() {
171 public void progress(Progress progress
, String name
) {
172 pg
= p
.getProgress();
176 child1
.setProgress(1);
177 assertEquals(200, pg
);
178 child2
.setProgress(100);
179 assertEquals(600, pg
);
181 assertEquals(700, pg
);
182 child1
.setProgress(2);
183 assertEquals(900, pg
);
185 assertEquals(1000, pg
);
189 addTest(new TestCase("Listeners with 5+ children, 4+ depth") {
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);
214 p
.addProgressListener(new Progress
.ProgressListener() {
216 public void progress(Progress progress
, String name
) {
217 pg
= p
.getProgress();
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
);
238 addTest(new TestCase("Listeners with children, multi-thread") {
241 Object lock1
= new Object();
242 Object lock2
= new Object();
247 public void test() throws Exception
{
248 final Progress p
= new Progress(0, 200);
250 final Progress child1
= new Progress();
251 final Progress child2
= new Progress();
252 p
.addProgress(child1
, 100);
253 p
.addProgress(child2
, 100);
255 p
.addProgressListener(new Progress
.ProgressListener() {
257 public void progress(Progress progress
, String name
) {
258 int now
= p
.getProgress();
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
++) {
270 new Thread(new Runnable() {
273 synchronized (lock1
) {
274 if (step
> currentStep1
) {
276 child1
.setProgress(step
);
282 new Thread(new Runnable() {
285 synchronized (lock2
) {
286 if (step
> currentStep2
) {
288 child2
.setProgress(step
);
296 int timeout
= 20; // in 1/10th of seconds
297 for (i
= 0; i
< timeout
298 && (currentStep1
+ currentStep2
) < 200; i
++) {
302 assertEquals("The test froze at step " + currentStep1
303 + " + " + currentStep2
, true, i
< timeout
);
305 "There should not have any decresing steps",
307 assertEquals("The progress should have reached 200",
308 200, p
.getProgress());
310 "The progress should have reached completion",