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.BorderLayout;
023: import java.awt.Insets;
024: import java.awt.Point;
025: import java.awt.event.ContainerListener;
026: import java.awt.event.MouseEvent;
027: import java.beans.PropertyChangeListener;
028: import java.util.Arrays;
029: import javax.swing.JButton;
030: import javax.swing.JFrame;
031: import javax.swing.JLabel;
032: import javax.swing.JToolBar;
033: import javax.swing.SwingTestCase;
034: import javax.swing.border.Border;
035: import javax.swing.border.EmptyBorder;
036: import javax.swing.plaf.ComponentUI;
037: import javax.swing.plaf.UIResource;
038:
039: public class MetalToolBarUITest extends SwingTestCase {
040: private class TestMetalToolBarUI extends MetalToolBarUI {
041: public class TestMetalDockingListener extends
042: MetalDockingListener {
043: TestMetalDockingListener(final JToolBar toolBar) {
044: super (toolBar);
045: }
046:
047: boolean isDragging() {
048: return isDragging;
049: }
050:
051: Point getOrigin() {
052: return origin;
053: }
054: }
055:
056: public Border nonRolloverBorder;
057:
058: private boolean dragToCalled;
059:
060: @Override
061: protected PropertyChangeListener createRolloverListener() {
062: return new MetalRolloverListener();
063: }
064:
065: @Override
066: protected ContainerListener createContainerListener() {
067: return new MetalContainerListener();
068: }
069:
070: public Point getDragWindowOffset() {
071: return dragWindow.getOffset();
072: }
073:
074: @Override
075: protected Border createNonRolloverBorder() {
076: nonRolloverBorder = super .createNonRolloverBorder();
077: return nonRolloverBorder;
078: }
079:
080: @Override
081: protected void dragTo(final Point position, final Point origin) {
082: dragToCalled = true;
083: super .dragTo(position, origin);
084: }
085: }
086:
087: private JFrame frame;
088:
089: private JToolBar toolBar;
090:
091: private MetalToolBarUI ui;
092:
093: private JButton b;
094:
095: private JLabel label;
096:
097: @Override
098: protected void setUp() throws Exception {
099: super .setUp();
100: b = new JButton();
101: label = new JLabel();
102: toolBar = new JToolBar();
103: ui = new MetalToolBarUI();
104: toolBar.setUI(ui);
105: }
106:
107: @Override
108: protected void tearDown() throws Exception {
109: super .tearDown();
110: if (frame != null) {
111: frame.dispose();
112: frame = null;
113: }
114: }
115:
116: public MetalToolBarUITest(final String name) {
117: super (name);
118: }
119:
120: public void testUpdate() {
121: // painting code, cannot test
122: }
123:
124: public void testUninstallUI() {
125: // nothing to test
126: }
127:
128: public void testInstallUI() {
129: // nothing to test
130: }
131:
132: public void testCreateUI() {
133: ComponentUI ui1 = MetalToolBarUI.createUI(toolBar);
134: assertTrue(ui1 instanceof MetalToolBarUI);
135: ComponentUI ui2 = MetalToolBarUI.createUI(toolBar);
136: assertNotSame(ui1, ui2);
137: }
138:
139: public void testCreateDockingListener() {
140: assertTrue(ui.createDockingListener() instanceof MetalToolBarUI.MetalDockingListener);
141: }
142:
143: public void testCreateRolloverBorder() {
144: assertNotNull(ui.createRolloverBorder());
145: if (isHarmony()) {
146: assertTrue(ui.createRolloverBorder() instanceof UIResource);
147: }
148: }
149:
150: public void testCreateNonRolloverBorder() {
151: assertNotNull(ui.createNonRolloverBorder());
152: if (isHarmony()) {
153: assertTrue(ui.createNonRolloverBorder() instanceof UIResource);
154: }
155: }
156:
157: public void testInstallListeners() {
158: TestMetalToolBarUI ui = new TestMetalToolBarUI();
159: toolBar.setUI(ui);
160: assertTrue(Arrays.asList(toolBar.getContainerListeners())
161: .contains(ui.contListener));
162: assertTrue(Arrays.asList(toolBar.getPropertyChangeListeners())
163: .contains(ui.rolloverListener));
164: assertFalse(Arrays
165: .asList(
166: toolBar
167: .getPropertyChangeListeners("JToolBar.isRollover"))
168: .contains(ui.rolloverListener));
169: }
170:
171: public void testUninstallListeners() {
172: TestMetalToolBarUI ui = new TestMetalToolBarUI();
173: toolBar.setUI(ui);
174: ui.uninstallListeners();
175: assertFalse(Arrays.asList(toolBar.getContainerListeners())
176: .contains(ui.contListener));
177: assertFalse(Arrays.asList(toolBar.getPropertyChangeListeners())
178: .contains(ui.rolloverListener));
179: }
180:
181: public void testSetBorderToNonRollover() {
182: TestMetalToolBarUI ui = new TestMetalToolBarUI();
183: toolBar.setUI(ui);
184: ui.setBorderToNonRollover(b);
185: assertSame(ui.nonRolloverBorder, b.getBorder());
186: assertFalse(b.isRolloverEnabled());
187: Border border = new EmptyBorder(new Insets(0, 0, 0, 0));
188: b.setBorder(border);
189: ui.setBorderToNonRollover(b);
190: assertSame(border, b.getBorder());
191: ui.setBorderToNonRollover(label);
192: assertNull(label.getBorder());
193: ui.setBorderToNonRollover(null);
194: }
195:
196: public void testMetalToolBarUI() {
197: // nothing to test
198: }
199:
200: public void testCreateContainerListener() {
201: assertNull(ui.createContainerListener());
202: }
203:
204: public void testCreateRolloverListener() {
205: assertNull(ui.createRolloverListener());
206: }
207:
208: public void testSetDragOffset() {
209: TestMetalToolBarUI ui = new TestMetalToolBarUI();
210: toolBar.setUI(ui);
211: Point offset = new Point(1, 2);
212: ui.setDragOffset(offset);
213: assertEquals(offset, ui.getDragWindowOffset());
214: }
215:
216: public void testMetalDockingListener() {
217: TestMetalToolBarUI ui = new TestMetalToolBarUI();
218: toolBar.setUI(ui);
219: toolBar.add(b);
220: createAndShowFrame();
221: TestMetalToolBarUI.TestMetalDockingListener l = ui.new TestMetalDockingListener(
222: toolBar);
223: MouseEvent e = new MouseEvent(toolBar,
224: MouseEvent.MOUSE_PRESSED, 0, 0, toolBar.getWidth(),
225: toolBar.getHeight(), 0, false);
226: l.mousePressed(e);
227: e = new MouseEvent(toolBar, MouseEvent.MOUSE_DRAGGED, 0, 0,
228: toolBar.getWidth(), toolBar.getHeight(), 0, false);
229: l.mouseDragged(e);
230: // false because drag should occur only on bumps
231: assertFalse(l.isDragging());
232: assertFalse(ui.dragToCalled);
233: Point p = new Point(4, 8);
234: e = new MouseEvent(toolBar, MouseEvent.MOUSE_PRESSED, 0, 0,
235: p.x, p.y, 0, false);
236: l.mousePressed(e);
237: e = new MouseEvent(toolBar, MouseEvent.MOUSE_DRAGGED, 0, 0,
238: p.x, p.y, 0, false);
239: l.mouseDragged(e);
240: assertTrue(l.isDragging());
241: assertTrue(ui.dragToCalled);
242: }
243:
244: private void createAndShowFrame() {
245: frame = new JFrame();
246: frame.setSize(90, 90);
247: frame.getContentPane().add(toolBar, BorderLayout.NORTH);
248: frame.setVisible(true);
249: }
250: }
|