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() {
41 public void progress(Progress progress
, String name
) {
42 pg
= progress
.getProgress();
55 addSeries(new TestLauncher("Progress with children", args
) {
57 addTest(new TestCase("One child") {
59 public void test() throws Exception
{
60 Progress p
= new Progress();
61 Progress child
= new Progress();
63 p
.addProgress(child
, 100);
65 child
.setProgress(42);
66 assertEquals(42, p
.getProgress());
70 addTest(new TestCase("Multiple children") {
72 public void test() throws Exception
{
73 Progress p
= new Progress();
74 Progress child1
= new Progress();
75 Progress child2
= new Progress();
76 Progress child3
= new Progress();
78 p
.addProgress(child1
, 20);
79 p
.addProgress(child2
, 60);
80 p
.addProgress(child3
, 20);
82 child1
.setProgress(50);
83 assertEquals(10, p
.getProgress());
84 child2
.setProgress(100);
85 assertEquals(70, p
.getProgress());
86 child3
.setProgress(100);
87 assertEquals(90, p
.getProgress());
88 child1
.setProgress(100);
89 assertEquals(100, p
.getProgress());
93 addTest(new TestCase("Listeners with children") {
97 public void test() throws Exception
{
98 final Progress p
= new Progress();
99 Progress child1
= new Progress();
100 Progress child2
= new Progress();
101 p
.addProgress(child1
, 50);
102 p
.addProgress(child2
, 50);
104 p
.addProgressListener(new Progress
.ProgressListener() {
105 public void progress(Progress progress
, String name
) {
106 pg
= p
.getProgress();
110 child1
.setProgress(50);
111 assertEquals(25, pg
);
112 child2
.setProgress(100);
113 assertEquals(75, pg
);
114 child1
.setProgress(100);
115 assertEquals(100, pg
);
119 addTest(new TestCase("Listeners with children, not 1-100") {
123 public void test() throws Exception
{
124 final Progress p
= new Progress();
127 Progress child1
= new Progress();
130 Progress child2
= new Progress();
131 p
.addProgress(child1
, 500);
132 p
.addProgress(child2
, 500);
134 p
.addProgressListener(new Progress
.ProgressListener() {
135 public void progress(Progress progress
, String name
) {
136 pg
= p
.getProgress();
140 child1
.setProgress(1);
141 assertEquals(250, pg
);
142 child2
.setProgress(100);
143 assertEquals(750, pg
);
144 child1
.setProgress(2);
145 assertEquals(1000, pg
);
149 addTest(new TestCase(
150 "Listeners with children, not 1-100, local progress") {
154 public void test() throws Exception
{
155 final Progress p
= new Progress();
158 Progress child1
= new Progress();
161 Progress child2
= new Progress();
162 p
.addProgress(child1
, 400);
163 p
.addProgress(child2
, 400);
164 // 200 = local progress
166 p
.addProgressListener(new Progress
.ProgressListener() {
167 public void progress(Progress progress
, String name
) {
168 pg
= p
.getProgress();
172 child1
.setProgress(1);
173 assertEquals(200, pg
);
174 child2
.setProgress(100);
175 assertEquals(600, pg
);
177 assertEquals(700, pg
);
178 child1
.setProgress(2);
179 assertEquals(900, pg
);
181 assertEquals(1000, pg
);
185 addTest(new TestCase("Listeners with 5+ children, 4+ depth") {
189 public void test() throws Exception
{
190 final Progress p
= new Progress();
191 Progress child1
= new Progress();
192 Progress child2
= new Progress();
193 p
.addProgress(child1
, 50);
194 p
.addProgress(child2
, 50);
195 Progress child11
= new Progress();
196 child1
.addProgress(child11
, 100);
197 Progress child111
= new Progress();
198 child11
.addProgress(child111
, 100);
199 Progress child1111
= new Progress();
200 child111
.addProgress(child1111
, 20);
201 Progress child1112
= new Progress();
202 child111
.addProgress(child1112
, 20);
203 Progress child1113
= new Progress();
204 child111
.addProgress(child1113
, 20);
205 Progress child1114
= new Progress();
206 child111
.addProgress(child1114
, 20);
207 Progress child1115
= new Progress();
208 child111
.addProgress(child1115
, 20);
210 p
.addProgressListener(new Progress
.ProgressListener() {
211 public void progress(Progress progress
, String name
) {
212 pg
= p
.getProgress();
216 child1111
.setProgress(100);
217 child1112
.setProgress(50);
218 child1113
.setProgress(25);
219 child1114
.setProgress(25);
220 child1115
.setProgress(50);
221 assertEquals(25, pg
);
222 child2
.setProgress(100);
223 assertEquals(75, pg
);
224 child1111
.setProgress(100);
225 child1112
.setProgress(100);
226 child1113
.setProgress(100);
227 child1114
.setProgress(100);
228 child1115
.setProgress(100);
229 assertEquals(100, pg
);
233 addTest(new TestCase("Listeners with children, multi-thread") {
236 Object lock1
= new Object();
237 Object lock2
= new Object();
242 public void test() throws Exception
{
243 final Progress p
= new Progress(0, 200);
245 final Progress child1
= new Progress();
246 final Progress child2
= new Progress();
247 p
.addProgress(child1
, 100);
248 p
.addProgress(child2
, 100);
250 p
.addProgressListener(new Progress
.ProgressListener() {
251 public void progress(Progress progress
, String name
) {
252 int now
= p
.getProgress();
260 // Run 200 concurrent threads, 2 at a time allowed to
261 // make progress (each on a different child)
262 for (int i
= 0; i
<= 100; i
++) {
264 new Thread(new Runnable() {
266 synchronized (lock1
) {
267 if (step
> currentStep1
) {
269 child1
.setProgress(step
);
275 new Thread(new Runnable() {
277 synchronized (lock2
) {
278 if (step
> currentStep2
) {
280 child2
.setProgress(step
);
288 int timeout
= 20; // in 1/10th of seconds
289 for (i
= 0; i
< timeout
290 && (currentStep1
+ currentStep2
) < 200; i
++) {
294 assertEquals("The test froze at step " + currentStep1
295 + " + " + currentStep2
, true, i
< timeout
);
297 "There should not have any decresing steps",
299 assertEquals("The progress should have reached 200",
300 200, p
.getProgress());
302 "The progress should have reached completion",