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 Vadim L. Bogdanov
019: * @version $Revision$
020: */package javax.swing.plaf.metal;
021:
022: import java.awt.Color;
023: import java.awt.FontMetrics;
024: import java.awt.Rectangle;
025: import java.awt.image.BufferedImage;
026: import javax.swing.ImageIcon;
027: import javax.swing.JFrame;
028: import javax.swing.JLabel;
029: import javax.swing.JTabbedPane;
030: import javax.swing.SwingConstants;
031: import javax.swing.SwingTestCase;
032: import javax.swing.UIManager;
033: import javax.swing.plaf.ComponentUI;
034:
035: public class MetalTabbedPaneUITest extends SwingTestCase {
036: private class TestMetalTabbedPaneUI extends MetalTabbedPaneUI {
037: @Override
038: protected FontMetrics getFontMetrics() {
039: return super .getFontMetrics();
040: }
041:
042: @Override
043: protected int calculateTabWidth(final int tabPlacement,
044: final int tabIndex, final FontMetrics fm) {
045: return super .calculateTabWidth(tabPlacement, tabIndex, fm);
046: }
047:
048: @Override
049: protected int calculateTabHeight(final int tabPlacement,
050: final int tabIndex, final int fontHeight) {
051: return super .calculateTabHeight(tabPlacement, tabIndex,
052: fontHeight);
053: }
054:
055: @Override
056: protected int getRunForTab(final int tabCount,
057: final int tabIndex) {
058: return super .getRunForTab(tabCount, tabIndex);
059: }
060: }
061:
062: private JTabbedPane tabbed;
063:
064: private TestMetalTabbedPaneUI ui;
065:
066: private JFrame frame;
067:
068: @Override
069: protected void setUp() throws Exception {
070: super .setUp();
071: tabbed = new JTabbedPane();
072: ui = new TestMetalTabbedPaneUI();
073: tabbed.setUI(ui);
074: tabbed.addTab("tab1", new JLabel());
075: tabbed.setIconAt(0, new ImageIcon(new BufferedImage(10, 10,
076: BufferedImage.TYPE_INT_RGB)));
077: tabbed.setDisabledIconAt(0, new ImageIcon(new BufferedImage(10,
078: 10, BufferedImage.TYPE_INT_RGB)));
079: tabbed.addTab("tabtab2", new JLabel());
080: FontMetrics fm = ui.getFontMetrics();
081: tabbed.setSize(ui.calculateTabWidth(tabbed.getTabPlacement(),
082: 0, fm)
083: + ui.calculateTabWidth(tabbed.getTabPlacement(), 1, fm)
084: + 10, 100);
085: tabbed.doLayout();
086: }
087:
088: @Override
089: protected void tearDown() throws Exception {
090: super .tearDown();
091: if (frame != null) {
092: frame.dispose();
093: frame = null;
094: }
095: }
096:
097: public MetalTabbedPaneUITest(final String name) {
098: super (name);
099: }
100:
101: public void testUpdate() {
102: // Note: painting code, cannot test
103: }
104:
105: public void testPaint() {
106: // Note: painting code, cannot test
107: }
108:
109: public void testCreateUI() {
110: ComponentUI ui = MetalTabbedPaneUI.createUI(tabbed);
111: assertTrue(ui instanceof MetalTabbedPaneUI);
112: }
113:
114: public void testCalculateMaxTabHeight() {
115: int tabPlacement = tabbed.getTabPlacement();
116: int fontHeight = tabbed.getFontMetrics(tabbed.getFont())
117: .getHeight();
118: int height1 = ui
119: .calculateTabHeight(tabPlacement, 0, fontHeight);
120: int height2 = ui
121: .calculateTabHeight(tabPlacement, 1, fontHeight);
122: assertEquals(Math.max(height1, height2), ui
123: .calculateMaxTabHeight(tabPlacement));
124: }
125:
126: public void testCreateLayoutManager() {
127: assertTrue(ui.createLayoutManager() instanceof MetalTabbedPaneUI.TabbedPaneLayout);
128: }
129:
130: public void testGetTabLabelShiftX() {
131: assertEquals(0, ui.getTabLabelShiftX(SwingConstants.TOP, 0,
132: true));
133: assertEquals(0, ui.getTabLabelShiftX(SwingConstants.TOP, 1,
134: false));
135: }
136:
137: public void testGetTabLabelShiftY() {
138: assertEquals(0, ui.getTabLabelShiftY(SwingConstants.TOP, 0,
139: true));
140: assertEquals(0, ui.getTabLabelShiftY(SwingConstants.TOP, 1,
141: false));
142: }
143:
144: public void testGetTabRunOverlay() {
145: assertTrue(ui.getTabRunOverlay(SwingConstants.TOP) >= 0);
146: assertTrue(ui.getTabRunOverlay(SwingConstants.LEFT) >= 0);
147: assertTrue(ui.getTabRunOverlay(SwingConstants.RIGHT) >= 0);
148: assertTrue(ui.getTabRunOverlay(SwingConstants.BOTTOM) >= 0);
149: }
150:
151: public void testInstallDefaults() {
152: ui.minTabWidth = 1;
153: ui.selectColor = null;
154: ui.selectHighlight = null;
155: ui.tabAreaBackground = null;
156: ui.installDefaults();
157: assertEquals(1, ui.minTabWidth);
158: assertEquals(UIManager.get("TabbedPane.selected"),
159: ui.selectColor);
160: assertEquals(UIManager.get("TabbedPane.selectHighlight"),
161: ui.selectHighlight);
162: assertEquals(UIManager.get("TabbedPane.tabAreaBackground"),
163: ui.tabAreaBackground);
164: }
165:
166: public void testPaintContentBorderBottomEdge() {
167: // Note: painting code, cannot test
168: }
169:
170: public void testPaintContentBorderLeftEdge() {
171: // Note: painting code, cannot test
172: }
173:
174: public void testPaintContentBorderRightEdge() {
175: // Note: painting code, cannot test
176: }
177:
178: public void testPaintContentBorderTopEdge() {
179: // Note: painting code, cannot test
180: }
181:
182: public void testPaintFocusIndicator() {
183: // Note: painting code, cannot test
184: }
185:
186: public void testPaintTabBackground() {
187: // Note: painting code, cannot test
188: }
189:
190: public void testPaintTabBorder() {
191: // Note: painting code, cannot test
192: }
193:
194: public void testShouldPadTabRun() {
195: assertFalse(ui.shouldPadTabRun(SwingConstants.TOP, 0));
196: create3TabRuns();
197: assertFalse(ui.shouldPadTabRun(SwingConstants.TOP, 2));
198: assertTrue(ui.shouldPadTabRun(SwingConstants.TOP, 1));
199: assertTrue(ui.shouldPadTabRun(SwingConstants.TOP, 0));
200: }
201:
202: public void testMetalTabbedPaneUI() {
203: assertEquals(40, ui.minTabWidth);
204: }
205:
206: public void testGetColorForGap() {
207: assertEquals(tabbed.getBackground(), ui.getColorForGap(0, 0, 0));
208: create3TabRuns();
209: tabbed.setBackgroundAt(2, Color.green);
210: tabbed.setBackgroundAt(0, Color.red);
211: tabbed.setSelectedIndex(3);
212: int tabCount = tabbed.getTabCount();
213: Rectangle tabBounds = ui.getTabBounds(tabbed, 0);
214: assertEquals(tabbed.getBackgroundAt(2), ui.getColorForGap(ui
215: .getRunForTab(tabCount, 0), tabBounds.x, tabBounds.y));
216: tabbed.setSelectedIndex(2);
217: assertEquals(ui.selectColor, ui.getColorForGap(ui.getRunForTab(
218: tabCount, 0), tabBounds.x, tabBounds.y));
219: }
220:
221: public void testPaintHighlightBelowTab() {
222: // Note: painting code, cannot test
223: }
224:
225: public void testPaintBottomTabBorder() {
226: // Note: painting code, cannot test
227: }
228:
229: public void testPaintLeftTabBorder() {
230: // Note: painting code, cannot test
231: }
232:
233: public void testPaintRightTabBorder() {
234: // Note: painting code, cannot test
235: }
236:
237: public void testPaintTopTabBorder() {
238: // Note: painting code, cannot test
239: }
240:
241: public void testShouldFillGap() {
242: assertFalse(ui.shouldFillGap(0, 0, 0, 0));
243: create3TabRuns();
244: int tabCount = tabbed.getTabCount();
245: Rectangle tabBounds = ui.getTabBounds(tabbed, 0);
246: assertTrue(ui.shouldFillGap(ui.getRunForTab(tabCount, 0), 0,
247: tabBounds.x, tabBounds.y));
248: tabBounds = ui.getTabBounds(tabbed, tabCount - 1);
249: assertFalse(ui.shouldFillGap(ui.getRunForTab(tabCount,
250: tabCount - 1), tabCount - 1, tabBounds.x, tabBounds.y));
251: }
252:
253: public void testShouldRotateTabRuns() {
254: assertFalse(ui.shouldRotateTabRuns(SwingConstants.TOP, 0));
255: assertFalse(ui.shouldRotateTabRuns(SwingConstants.TOP, 1));
256: }
257:
258: private void create3TabRuns() {
259: tabbed.add("tab4", new JLabel());
260: tabbed.add("tabtabtabtab5", new JLabel());
261: tabbed.getLayout().layoutContainer(tabbed);
262: assertEquals("initialized incorrectly", 3, tabbed
263: .getTabRunCount());
264: }
265: }
|