1 package be
.nikiroo
.utils
.test_code
;
3 import be
.nikiroo
.utils
.Progress
;
4 import be
.nikiroo
.utils
.test
.TestCase
;
5 import be
.nikiroo
.utils
.test
.TestLauncher
;
7 class ProgressTest
extends TestLauncher
{
8 public ProgressTest(String
[] args
) {
9 super("Progress reporting", args
);
11 addSeries(new TestLauncher("Simple progress", args
) {
13 addTest(new TestCase("Relative values and direct values") {
15 public void test() throws Exception
{
16 Progress p
= new Progress();
17 assertEquals(0, p
.getProgress());
18 assertEquals(0, p
.getRelativeProgress());
20 assertEquals(33, p
.getProgress());
21 assertEquals(0.33, p
.getRelativeProgress());
24 assertEquals(1, p
.getProgress());
26 generateAssertMessage("0.33..",
27 p
.getRelativeProgress()), true,
28 p
.getRelativeProgress() >= 0.332);
30 generateAssertMessage("0.33..",
31 p
.getRelativeProgress()), true,
32 p
.getRelativeProgress() <= 0.334);
36 addTest(new TestCase("Listeners at first level") {
40 public void test() throws Exception
{
41 Progress p
= new Progress();
42 p
.addProgressListener(new Progress
.ProgressListener() {
44 public void progress(Progress progress
, String name
) {
45 pg
= progress
.getProgress();
58 addSeries(new TestLauncher("Progress with children", args
) {
60 addTest(new TestCase("One child") {
62 public void test() throws Exception
{
63 Progress p
= new Progress();
64 Progress child
= new Progress();
66 p
.addProgress(child
, 100);
68 child
.setProgress(42);
69 assertEquals(42, p
.getProgress());
73 addTest(new TestCase("Multiple children") {
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();
81 p
.addProgress(child1
, 20);
82 p
.addProgress(child2
, 60);
83 p
.addProgress(child3
, 20);
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());
96 addTest(new TestCase("Listeners with children") {
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);
107 p
.addProgressListener(new Progress
.ProgressListener() {
109 public void progress(Progress progress
, String name
) {
110 pg
= p
.getProgress();
114 child1
.setProgress(50);
115 assertEquals(25, pg
);
116 child2
.setProgress(100);
117 assertEquals(75, pg
);
118 child1
.setProgress(100);
119 assertEquals(100, pg
);
123 addTest(new TestCase("Listeners with children, not 1-100") {
127 public void test() throws Exception
{
128 final Progress p
= new Progress();
131 Progress child1
= new Progress();
134 Progress child2
= new Progress();
135 p
.addProgress(child1
, 500);
136 p
.addProgress(child2
, 500);
138 p
.addProgressListener(new Progress
.ProgressListener() {
140 public void progress(Progress progress
, String name
) {
141 pg
= p
.getProgress();
145 child1
.setProgress(1);
146 assertEquals(250, pg
);
147 child2
.setProgress(100);
148 assertEquals(750, pg
);
149 child1
.setProgress(2);
150 assertEquals(1000, pg
);
154 addTest(new TestCase(
155 "Listeners with children, not 1-100, local progress") {
159 public void test() throws Exception
{
160 final Progress p
= new Progress();
163 Progress child1
= new Progress();
166 Progress child2
= new Progress();
167 p
.addProgress(child1
, 400);
168 p
.addProgress(child2
, 400);
169 // 200 = local progress
171 p
.addProgressListener(new Progress
.ProgressListener() {
173 public void progress(Progress progress
, String name
) {
174 pg
= p
.getProgress();
178 child1
.setProgress(1);
179 assertEquals(200, pg
);
180 child2
.setProgress(100);
181 assertEquals(600, pg
);
183 assertEquals(700, pg
);
184 child1
.setProgress(2);
185 assertEquals(900, pg
);
187 assertEquals(1000, pg
);
191 addTest(new TestCase("Listeners with 5+ children, 4+ depth") {
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);
216 p
.addProgressListener(new Progress
.ProgressListener() {
218 public void progress(Progress progress
, String name
) {
219 pg
= p
.getProgress();
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
);
240 addTest(new TestCase("Listeners with children, multi-thread") {
243 Object lock1
= new Object();
244 Object lock2
= new Object();
249 public void test() throws Exception
{
250 final Progress p
= new Progress(0, 200);
252 final Progress child1
= new Progress();
253 final Progress child2
= new Progress();
254 p
.addProgress(child1
, 100);
255 p
.addProgress(child2
, 100);
257 p
.addProgressListener(new Progress
.ProgressListener() {
259 public void progress(Progress progress
, String name
) {
260 int now
= p
.getProgress();
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
++) {
272 new Thread(new Runnable() {
275 synchronized (lock1
) {
276 if (step
> currentStep1
) {
278 child1
.setProgress(step
);
284 new Thread(new Runnable() {
287 synchronized (lock2
) {
288 if (step
> currentStep2
) {
290 child2
.setProgress(step
);
298 int timeout
= 20; // in 1/10th of seconds
299 for (i
= 0; i
< timeout
300 && (currentStep1
+ currentStep2
) < 200; i
++) {
304 assertEquals("The test froze at step " + currentStep1
305 + " + " + currentStep2
, true, i
< timeout
);
307 "There should not have any decresing steps",
309 assertEquals("The progress should have reached 200",
310 200, p
.getProgress());
312 "The progress should have reached completion",