1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
3 | // |
4 | // This program is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | package net.sf.jomic.ui; |
17 | |
18 | import junit.framework.TestCase; |
19 | import org.apache.commons.logging.Log; |
20 | import org.apache.commons.logging.LogFactory; |
21 | |
22 | import net.sf.jomic.tools.ProgressFrame; |
23 | import net.sf.jomic.tools.TestTools; |
24 | |
25 | /** |
26 | * TestCase for ProgressFrame. |
27 | * |
28 | * @author Thomas Aglassinger |
29 | */ |
30 | public class ProgressFrameTest extends TestCase |
31 | { |
32 | private static final int MAXIMUM_DELAY = 5; |
33 | private static final long MAXIMUM_PROGRESS = Long.MAX_VALUE / 4; |
34 | |
35 | private Log logger; |
36 | private ProgressFrame progressFrame; |
37 | private TestTools testTools; |
38 | |
39 | protected void setUp() |
40 | throws Exception { |
41 | super.setUp(); |
42 | testTools = TestTools.instance(); |
43 | logger = LogFactory.getLog(ProgressFrameTest.class); |
44 | } |
45 | |
46 | public void testProgress() { |
47 | long increment = Math.max(MAXIMUM_PROGRESS / 32, 1); |
48 | |
49 | progressFrame = new ProgressFrame(); |
50 | progressFrame.setTitle(ProgressFrameTest.class.getName()); |
51 | progressFrame.setMaximum(MAXIMUM_PROGRESS); |
52 | for (int run = 0; run < 2; run += 1) { |
53 | progressFrame.reset(); |
54 | progressFrame.setNote("Testing..."); |
55 | progressFrame.setVisible(true); |
56 | delay(true); |
57 | for (long progress = 0; (!progressFrame.isCanceled()) |
58 | && (progress <= MAXIMUM_PROGRESS); progress += increment) { |
59 | progressFrame.setProgress(progress); |
60 | if (logger.isInfoEnabled()) { |
61 | |
62 | logger.info("progress " + progress + "/" |
63 | + MAXIMUM_PROGRESS); |
64 | } |
65 | computeComplicatedStuff(); |
66 | } |
67 | if (progressFrame.isCanceled()) { |
68 | progressFrame.setNote("Canceling..."); |
69 | delay(false); |
70 | } |
71 | progressFrame.dispose(); |
72 | } |
73 | } |
74 | |
75 | private void computeComplicatedStuff() { |
76 | testTools.waitSomeTime(); |
77 | } |
78 | |
79 | private void delay(boolean cancelable) { |
80 | for (int delay = 0; (!(cancelable && progressFrame.isCanceled()) && (delay <= MAXIMUM_DELAY)); delay += 1) { |
81 | if (logger.isInfoEnabled()) { |
82 | logger.info("delay " + delay + "/" + MAXIMUM_DELAY); |
83 | } |
84 | computeComplicatedStuff(); |
85 | } |
86 | } |
87 | } |