| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2008 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 net.sf.wraplog.Logger; |
| 20 | import net.sf.jomic.tools.ProgressFrame; |
| 21 | import net.sf.jomic.tools.TestTools; |
| 22 | |
| 23 | /** |
| 24 | * TestCase for ProgressFrame. |
| 25 | * |
| 26 | * @author Thomas Aglassinger |
| 27 | */ |
| 28 | public class ProgressFrameTest extends TestCase |
| 29 | { |
| 30 | private static final int MAXIMUM_DELAY = 5; |
| 31 | private static final long MAXIMUM_PROGRESS = Long.MAX_VALUE / 4; |
| 32 | |
| 33 | private Logger logger; |
| 34 | private ProgressFrame progressFrame; |
| 35 | private TestTools testTools; |
| 36 | |
| 37 | protected void setUp() |
| 38 | throws Exception { |
| 39 | super.setUp(); |
| 40 | testTools = TestTools.instance(); |
| 41 | logger = Logger.getLogger(ProgressFrameTest.class); |
| 42 | } |
| 43 | |
| 44 | public void testProgress() { |
| 45 | long increment = Math.max(MAXIMUM_PROGRESS / 32, 1); |
| 46 | |
| 47 | progressFrame = new ProgressFrame(); |
| 48 | progressFrame.setTitle(ProgressFrameTest.class.getName()); |
| 49 | progressFrame.setMaximum(MAXIMUM_PROGRESS); |
| 50 | for (int run = 0; run < 2; run += 1) { |
| 51 | progressFrame.reset(); |
| 52 | progressFrame.setNote("Testing..."); |
| 53 | progressFrame.setVisible(true); |
| 54 | delay(true); |
| 55 | for (long progress = 0; (!progressFrame.isCanceled()) |
| 56 | && (progress <= MAXIMUM_PROGRESS); progress += increment) { |
| 57 | progressFrame.setProgress(progress); |
| 58 | if (logger.isInfoEnabled()) { |
| 59 | |
| 60 | logger.info("progress " + progress + "/" |
| 61 | + MAXIMUM_PROGRESS); |
| 62 | } |
| 63 | computeComplicatedStuff(); |
| 64 | } |
| 65 | if (progressFrame.isCanceled()) { |
| 66 | progressFrame.setNote("Canceling..."); |
| 67 | delay(false); |
| 68 | } |
| 69 | progressFrame.dispose(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private void computeComplicatedStuff() { |
| 74 | testTools.waitSomeTime(); |
| 75 | } |
| 76 | |
| 77 | private void delay(boolean cancelable) { |
| 78 | for (int delay = 0; (!(cancelable && progressFrame.isCanceled()) && (delay <= MAXIMUM_DELAY)); delay += 1) { |
| 79 | if (logger.isInfoEnabled()) { |
| 80 | logger.info("delay " + delay + "/" + MAXIMUM_DELAY); |
| 81 | } |
| 82 | computeComplicatedStuff(); |
| 83 | } |
| 84 | } |
| 85 | } |