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.plaf.basic;
021:
022: import java.awt.Dimension;
023: import java.util.Arrays;
024: import javax.swing.BasicSwingTestCase;
025: import javax.swing.JProgressBar;
026: import javax.swing.UIManager;
027:
028: public class BasicProgressBarUITest extends BasicSwingTestCase {
029: private BasicProgressBarUI ui;
030:
031: private JProgressBar progressBar;
032:
033: @Override
034: public void setUp() throws Exception {
035: super .setUp();
036: ui = new BasicProgressBarUI();
037: progressBar = new JProgressBar();
038: propertyChangeController = new PropertyChangeController();
039: progressBar.addPropertyChangeListener(propertyChangeController);
040: progressBar.getUI().uninstallUI(progressBar);
041: }
042:
043: @Override
044: public void tearDown() throws Exception {
045: progressBar = null;
046: ui = null;
047: propertyChangeController = null;
048: super .tearDown();
049: }
050:
051: public void testCreateUI() {
052: BasicProgressBarUI ui = (BasicProgressBarUI) BasicProgressBarUI
053: .createUI(progressBar);
054: assertNotNull(ui);
055: assertNull(ui.changeListener);
056: assertNull(ui.progressBar);
057: }
058:
059: public void testInstallUninstallUI() {
060: assertNull(progressBar.getBorder());
061: ui.installUI(progressBar);
062: assertSame(ui.progressBar, progressBar);
063: assertNotNull(ui.changeListener);
064: assertTrue(Arrays.asList(progressBar.getChangeListeners())
065: .contains(ui.changeListener));
066: assertTrue(propertyChangeController.isChanged("border"));
067: assertNotNull(progressBar.getBorder());
068: assertEquals(UIManager.getInt("ProgressBar.cellLength"), ui
069: .getCellLength());
070: assertEquals(UIManager.getInt("ProgressBar.cellSpacing"), ui
071: .getCellSpacing());
072: propertyChangeController.reset();
073: ui.uninstallUI(progressBar);
074: assertEquals(0, progressBar.getChangeListeners().length);
075: assertNull(ui.progressBar);
076: assertTrue(propertyChangeController.isChanged("border"));
077: assertNull(progressBar.getBorder());
078: }
079:
080: public void testInstallUninstallListeners() {
081: ui.progressBar = progressBar;
082: ui.installListeners();
083: assertTrue(Arrays.asList(progressBar.getChangeListeners())
084: .contains(ui.changeListener));
085: assertEquals(2, progressBar.getPropertyChangeListeners().length);
086: ui.uninstallListeners();
087: assertEquals(1, progressBar.getPropertyChangeListeners().length);
088: }
089:
090: public void testGetSelectionBackForeGround() {
091: ui.installUI(progressBar);
092: assertEquals(UIManager
093: .getColor("ProgressBar.selectionBackground"), ui
094: .getSelectionBackground());
095: assertEquals(UIManager
096: .getColor("ProgressBar.selectionForeground"), ui
097: .getSelectionForeground());
098: }
099:
100: public void testSetGetCellLengthSpacing() {
101: ui.installUI(progressBar);
102: assertEquals(UIManager.getInt("ProgressBar.cellLength"), ui
103: .getCellLength());
104: assertEquals(UIManager.getInt("ProgressBar.cellSpacing"), ui
105: .getCellSpacing());
106: ui.setCellLength(11);
107: assertEquals(11, ui.getCellLength());
108: ui.setCellLength(-1);
109: assertEquals(-1, ui.getCellLength());
110: progressBar.setStringPainted(true);
111: int length = ui.getCellLength();
112: if (isHarmony()) {
113: assertEquals(3, length);
114: }
115: ui.setCellLength(11);
116: assertEquals(length, ui.getCellLength());
117: ui.setCellSpacing(11111);
118: if (isHarmony()) {
119: assertEquals(2, ui.getCellSpacing());
120: }
121: progressBar.setStringPainted(false);
122: assertEquals(11111, ui.getCellSpacing());
123: }
124:
125: public void testGetPreferredSizes() {
126: ui.installUI(progressBar);
127: Dimension inner = ui.getPreferredInnerHorizontal();
128: assertSame(inner, ui.getPreferredInnerHorizontal());
129: assertEquals(ui.getPreferredSize(progressBar).width,
130: inner.width + 2);
131: assertEquals(ui.getPreferredSize(progressBar).height,
132: inner.height + 2);
133: UIManager.put("ProgressBar.verticalSize", new Dimension(
134: inner.width + 2, inner.height + 2));
135: assertEquals(ui.getPreferredSize(progressBar).width,
136: inner.width + 2);
137: assertEquals(ui.getPreferredSize(progressBar).height,
138: inner.height + 2);
139: assertEquals(ui.getPreferredInnerHorizontal().width,
140: inner.width);
141: assertEquals(ui.getPreferredInnerHorizontal().height,
142: inner.height);
143: }
144:
145: public void testAnimation() throws ArithmeticException {
146: ui.installUI(progressBar);
147: progressBar.setIndeterminate(true);
148: assertEquals(0, ui.getAnimationIndex());
149: ui.setAnimationIndex(5);
150: assertEquals(5, ui.getAnimationIndex());
151:
152: try { //Regression test for HARMONY-2699
153: new BasicProgressBarUI().setAnimationIndex(5);
154: fail("NullPointerException should have been thrown");
155: } catch (NullPointerException e) {
156: // Expected
157: }
158: }
159:
160: /**
161: * Regression test for HARMONY-2701
162: * */
163: public void testGetBoxLength() {
164: testBasicProgressBarUI pb = new testBasicProgressBarUI();
165: assertEquals(0, pb.getBoxLength(0, 1));
166: }
167:
168: /**
169: * Regression test for HARMONY-2701
170: * */
171: public void testGetBoxLength2() {
172: testBasicProgressBarUI pb = new testBasicProgressBarUI();
173: assertEquals(8, pb.getBoxLength(50, 1));
174: }
175:
176: class testBasicProgressBarUI extends BasicProgressBarUI {
177: public int getBoxLength(int a, int b) {
178: return super .getBoxLength(a, b);
179: }
180: }
181:
182: public void testStartStop() throws NullPointerException {
183: BasicProgressBarUIExt pb = new BasicProgressBarUIExt();
184: pb.startAnimationTimer();
185:
186: pb = new BasicProgressBarUIExt();
187: pb.stopAnimationTimer();
188: }
189:
190: class BasicProgressBarUIExt extends BasicProgressBarUI {
191: public void startAnimationTimer() {
192: super .startAnimationTimer();
193: }
194:
195: public void stopAnimationTimer() {
196: super .stopAnimationTimer();
197: }
198: }
199:
200: public void testHarmony2698Regression() {
201: class testBasicProgressBarUI extends BasicProgressBarUI {
202: public Dimension getPreferredInnerVertical() {
203: return super .getPreferredInnerVertical();
204: }
205:
206: public Dimension getPreferredInnerHorizontal() {
207: return super .getPreferredInnerHorizontal();
208: }
209: }
210:
211: try {
212: testBasicProgressBarUI pb = new testBasicProgressBarUI();
213: pb.getPreferredInnerHorizontal();
214: fail("NPE expected");
215: } catch (NullPointerException e) {
216: //expected
217: }
218:
219: try {
220: testBasicProgressBarUI pb = new testBasicProgressBarUI();
221: pb.getPreferredInnerVertical();
222: fail("NPE expected");
223: } catch (NullPointerException e) {
224: //expected
225: }
226: }
227: }
|