| 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 | public void progress(Progress progress, String name) { |
| 42 | pg = progress.getProgress(); |
| 43 | } |
| 44 | }); |
| 45 | |
| 46 | p.setProgress(42); |
| 47 | assertEquals(42, pg); |
| 48 | p.setProgress(0); |
| 49 | assertEquals(0, pg); |
| 50 | } |
| 51 | }); |
| 52 | } |
| 53 | }); |
| 54 | |
| 55 | addSeries(new TestLauncher("Progress with children", args) { |
| 56 | { |
| 57 | addTest(new TestCase("One child") { |
| 58 | @Override |
| 59 | public void test() throws Exception { |
| 60 | Progress p = new Progress(); |
| 61 | Progress child = new Progress(); |
| 62 | |
| 63 | p.addProgress(child, 100); |
| 64 | |
| 65 | child.setProgress(42); |
| 66 | assertEquals(42, p.getProgress()); |
| 67 | } |
| 68 | }); |
| 69 | |
| 70 | addTest(new TestCase("Multiple children") { |
| 71 | @Override |
| 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(); |
| 77 | |
| 78 | p.addProgress(child1, 20); |
| 79 | p.addProgress(child2, 60); |
| 80 | p.addProgress(child3, 20); |
| 81 | |
| 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()); |
| 90 | } |
| 91 | }); |
| 92 | |
| 93 | addTest(new TestCase("Listeners with children") { |
| 94 | int pg; |
| 95 | |
| 96 | @Override |
| 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); |
| 103 | |
| 104 | p.addProgressListener(new Progress.ProgressListener() { |
| 105 | public void progress(Progress progress, String name) { |
| 106 | pg = p.getProgress(); |
| 107 | } |
| 108 | }); |
| 109 | |
| 110 | child1.setProgress(50); |
| 111 | assertEquals(25, pg); |
| 112 | child2.setProgress(100); |
| 113 | assertEquals(75, pg); |
| 114 | child1.setProgress(100); |
| 115 | assertEquals(100, pg); |
| 116 | } |
| 117 | }); |
| 118 | |
| 119 | addTest(new TestCase("Listeners with children, not 1-100") { |
| 120 | int pg; |
| 121 | |
| 122 | @Override |
| 123 | public void test() throws Exception { |
| 124 | final Progress p = new Progress(); |
| 125 | p.setMax(1000); |
| 126 | |
| 127 | Progress child1 = new Progress(); |
| 128 | child1.setMax(2); |
| 129 | |
| 130 | Progress child2 = new Progress(); |
| 131 | p.addProgress(child1, 500); |
| 132 | p.addProgress(child2, 500); |
| 133 | |
| 134 | p.addProgressListener(new Progress.ProgressListener() { |
| 135 | public void progress(Progress progress, String name) { |
| 136 | pg = p.getProgress(); |
| 137 | } |
| 138 | }); |
| 139 | |
| 140 | child1.setProgress(1); |
| 141 | assertEquals(250, pg); |
| 142 | child2.setProgress(100); |
| 143 | assertEquals(750, pg); |
| 144 | child1.setProgress(2); |
| 145 | assertEquals(1000, pg); |
| 146 | } |
| 147 | }); |
| 148 | |
| 149 | addTest(new TestCase( |
| 150 | "Listeners with children, not 1-100, local progress") { |
| 151 | int pg; |
| 152 | |
| 153 | @Override |
| 154 | public void test() throws Exception { |
| 155 | final Progress p = new Progress(); |
| 156 | p.setMax(1000); |
| 157 | |
| 158 | Progress child1 = new Progress(); |
| 159 | child1.setMax(2); |
| 160 | |
| 161 | Progress child2 = new Progress(); |
| 162 | p.addProgress(child1, 400); |
| 163 | p.addProgress(child2, 400); |
| 164 | // 200 = local progress |
| 165 | |
| 166 | p.addProgressListener(new Progress.ProgressListener() { |
| 167 | public void progress(Progress progress, String name) { |
| 168 | pg = p.getProgress(); |
| 169 | } |
| 170 | }); |
| 171 | |
| 172 | child1.setProgress(1); |
| 173 | assertEquals(200, pg); |
| 174 | child2.setProgress(100); |
| 175 | assertEquals(600, pg); |
| 176 | p.setProgress(100); |
| 177 | assertEquals(700, pg); |
| 178 | child1.setProgress(2); |
| 179 | assertEquals(900, pg); |
| 180 | p.setProgress(200); |
| 181 | assertEquals(1000, pg); |
| 182 | } |
| 183 | }); |
| 184 | |
| 185 | addTest(new TestCase("Listeners with 5+ children, 4+ depth") { |
| 186 | int pg; |
| 187 | |
| 188 | @Override |
| 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); |
| 209 | |
| 210 | p.addProgressListener(new Progress.ProgressListener() { |
| 211 | public void progress(Progress progress, String name) { |
| 212 | pg = p.getProgress(); |
| 213 | } |
| 214 | }); |
| 215 | |
| 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); |
| 230 | } |
| 231 | }); |
| 232 | |
| 233 | addTest(new TestCase("Listeners with children, multi-thread") { |
| 234 | int pg; |
| 235 | boolean decrease; |
| 236 | Object lock1 = new Object(); |
| 237 | Object lock2 = new Object(); |
| 238 | int currentStep1; |
| 239 | int currentStep2; |
| 240 | |
| 241 | @Override |
| 242 | public void test() throws Exception { |
| 243 | final Progress p = new Progress(0, 200); |
| 244 | |
| 245 | final Progress child1 = new Progress(); |
| 246 | final Progress child2 = new Progress(); |
| 247 | p.addProgress(child1, 100); |
| 248 | p.addProgress(child2, 100); |
| 249 | |
| 250 | p.addProgressListener(new Progress.ProgressListener() { |
| 251 | public void progress(Progress progress, String name) { |
| 252 | int now = p.getProgress(); |
| 253 | if (now < pg) { |
| 254 | decrease = true; |
| 255 | } |
| 256 | pg = now; |
| 257 | } |
| 258 | }); |
| 259 | |
| 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++) { |
| 263 | final int step = i; |
| 264 | new Thread(new Runnable() { |
| 265 | public void run() { |
| 266 | synchronized (lock1) { |
| 267 | if (step > currentStep1) { |
| 268 | currentStep1 = step; |
| 269 | child1.setProgress(step); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | }).start(); |
| 274 | |
| 275 | new Thread(new Runnable() { |
| 276 | public void run() { |
| 277 | synchronized (lock2) { |
| 278 | if (step > currentStep2) { |
| 279 | currentStep2 = step; |
| 280 | child2.setProgress(step); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | }).start(); |
| 285 | } |
| 286 | |
| 287 | int i; |
| 288 | int timeout = 20; // in 1/10th of seconds |
| 289 | for (i = 0; i < timeout |
| 290 | && (currentStep1 + currentStep2) < 200; i++) { |
| 291 | Thread.sleep(100); |
| 292 | } |
| 293 | |
| 294 | assertEquals("The test froze at step " + currentStep1 |
| 295 | + " + " + currentStep2, true, i < timeout); |
| 296 | assertEquals( |
| 297 | "There should not have any decresing steps", |
| 298 | decrease, false); |
| 299 | assertEquals("The progress should have reached 200", |
| 300 | 200, p.getProgress()); |
| 301 | assertEquals( |
| 302 | "The progress should have reached completion", |
| 303 | true, p.isDone()); |
| 304 | } |
| 305 | }); |
| 306 | } |
| 307 | }); |
| 308 | } |
| 309 | } |