001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dennis Ushakov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.event.WindowEvent;
023:
024: public class ProgressMonitorTest extends BasicSwingTestCase {
025: private ProgressMonitor progressMonitor;
026:
027: private JFrame window;
028:
029: @Override
030: public void setUp() throws Exception {
031: super .setUp();
032: window = new JFrame();
033: }
034:
035: @Override
036: public void tearDown() throws Exception {
037: progressMonitor = null;
038: window = null;
039: super .tearDown();
040: }
041:
042: public void testProgressMonitor() {
043: progressMonitor = new ProgressMonitor(window, "message test",
044: "note test", 0, 100);
045: assertEquals(progressMonitor.getMillisToDecideToPopup(), 500);
046: assertEquals(progressMonitor.getMillisToPopup(), 2000);
047: }
048:
049: public void testSetProgress() throws Exception {
050: progressMonitor = new ProgressMonitor(window, "message test",
051: "note test", 0, 100);
052: assertEquals(0, window.getOwnedWindows().length);
053: progressMonitor.setProgress(1);
054: Thread.sleep(500 + 100);
055: progressMonitor.setProgress(2);
056: assertEquals(1, window.getOwnedWindows().length);
057: JProgressBar pb = getProgressBar();
058: assertEquals(2, pb.getValue());
059: progressMonitor.close();
060: progressMonitor = new ProgressMonitor(window, "message test",
061: "note test", -100, 100);
062: assertEquals(1, window.getOwnedWindows().length);
063: progressMonitor.setProgress(1);
064: Thread.sleep(500 + 100);
065: progressMonitor.setProgress(2);
066: assertEquals(1, window.getOwnedWindows().length);
067: progressMonitor = new ProgressMonitor(window, "message test",
068: "note test", 0, 300);
069: progressMonitor.setProgress(1);
070: Thread.sleep(500 + 100);
071: progressMonitor.setProgress(2);
072: assertEquals(1, window.getOwnedWindows().length);
073: }
074:
075: public void testGetSetMaximum() throws Exception {
076: progressMonitor = new ProgressMonitor(window, "message test",
077: "note test", 0, 300);
078: progressMonitor.setProgress(1);
079: Thread.sleep(500 + 100);
080: progressMonitor.setProgress(20);
081: assertEquals(300, getProgressBar().getMaximum());
082: progressMonitor.setMaximum(500);
083: if (isHarmony()) {
084: assertEquals(500, getProgressBar().getMaximum());
085: }
086: assertEquals(500, progressMonitor.getMaximum());
087: }
088:
089: public void testGetSetMinimum() throws Exception {
090: progressMonitor = new ProgressMonitor(window, "message test",
091: "note test", 0, 300);
092: progressMonitor.setProgress(1);
093: Thread.sleep(500 + 100);
094: progressMonitor.setProgress(20);
095: assertEquals(0, getProgressBar().getMinimum());
096: progressMonitor.setMinimum(-100);
097: if (isHarmony()) {
098: assertEquals(-100, getProgressBar().getMinimum());
099: }
100: assertEquals(-100, progressMonitor.getMinimum());
101: }
102:
103: public void testIsCancelled() throws Exception {
104: progressMonitor = new ProgressMonitor(window, "message test",
105: "note test", 0, 100);
106: progressMonitor.setProgress(1);
107: Thread.sleep(500 + 100);
108: progressMonitor.setProgress(20);
109: JDialog dialog = (JDialog) window.getOwnedWindows()[0];
110: JOptionPane comp = (JOptionPane) dialog.getContentPane()
111: .getComponent(0);
112: JPanel bottomPanel = (JPanel) comp.getComponent(1);
113: JButton cancelButton = (JButton) bottomPanel.getComponent(0);
114: cancelButton.doClick();
115: assertTrue(progressMonitor.isCanceled());
116: progressMonitor = new ProgressMonitor(window, "message test",
117: "note test", 0, 100);
118: progressMonitor.setProgress(1);
119: Thread.sleep(500 + 100);
120: progressMonitor.setProgress(20);
121: dialog = (JDialog) window.getOwnedWindows()[1];
122: dialog.dispatchEvent(new WindowEvent(window,
123: WindowEvent.WINDOW_CLOSING));
124: assertTrue(progressMonitor.isCanceled());
125: progressMonitor.setProgress(98);
126: assertEquals(2, window.getOwnedWindows().length);
127: }
128:
129: public void testClose() throws Exception {
130: progressMonitor = new ProgressMonitor(window, "message test",
131: "note test", 0, 100);
132: progressMonitor.setProgress(1);
133: Thread.sleep(500 + 100);
134: progressMonitor.setProgress(20);
135: progressMonitor.close();
136: assertEquals(1, window.getOwnedWindows().length);
137: assertFalse(window.getOwnedWindows()[0].isVisible());
138: assertFalse(window.getOwnedWindows()[0].isDisplayable());
139: progressMonitor.setProgress(25);
140: assertEquals(2, window.getOwnedWindows().length);
141: assertTrue(window.getOwnedWindows()[1].isVisible());
142: }
143:
144: private JProgressBar getProgressBar() {
145: JDialog dialog = (JDialog) window.getOwnedWindows()[0];
146: JOptionPane comp = (JOptionPane) dialog.getContentPane()
147: .getComponent(0);
148: JPanel topPanel = (JPanel) comp.getComponent(0);
149: JPanel panel = (JPanel) topPanel.getComponent(0);
150: JPanel panel2 = (JPanel) panel.getComponent(1);
151: JProgressBar pb = (JProgressBar) panel2.getComponent(2);
152: return pb;
153: }
154: }
|