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 Anton Avtamonov, Sergey Burlak
019: * @version $Revision$
020: */package javax.swing.plaf.basic;
021:
022: import java.io.File;
023: import javax.swing.Icon;
024: import javax.swing.ImageIcon;
025: import javax.swing.JFileChooser;
026: import javax.swing.JPanel;
027: import javax.swing.SwingTestCase;
028: import javax.swing.UIManager;
029:
030: public class BasicFileChooserUITest extends SwingTestCase {
031: private BasicFileChooserUI ui;
032:
033: private JFileChooser fc;
034:
035: @Override
036: protected void setUp() throws Exception {
037: fc = new JFileChooser();
038: ui = new BasicFileChooserUI(fc);
039: }
040:
041: @Override
042: protected void tearDown() throws Exception {
043: fc = null;
044: ui = null;
045: }
046:
047: public void testGetAcceptAllFileFilter() throws Exception {
048: ui.installUI(fc);
049: assertNotNull(ui.getAcceptAllFileFilter(fc));
050: assertEquals(UIManager
051: .getString("FileChooser.acceptAllFileFilterText"), ui
052: .getAcceptAllFileFilter(fc).getDescription());
053: assertTrue(ui.getAcceptAllFileFilter(fc).accept(new File("")));
054: }
055:
056: public void testGetApproveButtonText() throws Exception {
057: ui.installUI(fc);
058: assertEquals(UIManager.get("FileChooser.openDialogTitleText"),
059: ui.getDialogTitle(fc));
060: assertEquals(UIManager.get("FileChooser.openButtonText"), ui
061: .getApproveButtonText(fc));
062: assertEquals(
063: UIManager.get("FileChooser.openButtonToolTipText"), ui
064: .getApproveButtonToolTipText(fc));
065: assertEquals(
066: UIManager.getInt("FileChooser.openButtonMnemonic"), ui
067: .getApproveButtonMnemonic(fc));
068: fc.setDialogType(JFileChooser.SAVE_DIALOG);
069: assertEquals(UIManager.get("FileChooser.saveDialogTitleText"),
070: ui.getDialogTitle(fc));
071: assertEquals(UIManager.get("FileChooser.saveButtonText"), ui
072: .getApproveButtonText(fc));
073: assertEquals(
074: UIManager.get("FileChooser.saveButtonToolTipText"), ui
075: .getApproveButtonToolTipText(fc));
076: assertEquals(
077: UIManager.getInt("FileChooser.saveButtonMnemonic"), ui
078: .getApproveButtonMnemonic(fc));
079: fc.setDialogType(JFileChooser.CUSTOM_DIALOG);
080: assertNull(ui.getApproveButtonText(fc));
081: assertNull(ui.getApproveButtonToolTipText(fc));
082: assertNull(ui.getDialogTitle(fc));
083: assertEquals(0, ui.getApproveButtonMnemonic(fc));
084: }
085:
086: public void testGetApproveButtonToolTipText() throws Exception {
087: try {
088: javax.swing.plaf.basic.BasicFileChooserUI b = new javax.swing.plaf.basic.BasicFileChooserUI(
089: new JFileChooser(""));
090: b.getApproveButtonToolTipText(null);
091: fail("NPE should be thrown");
092: } catch (NullPointerException npe) {
093: // PASSED
094: }
095: }
096:
097: public void testGetModel() throws Exception {
098: ui.installUI(fc);
099: assertNotNull(ui.getModel());
100: }
101:
102: public void testBasicFileView() throws Exception {
103: BasicFileChooserUI.BasicFileView fileView = new BasicFileChooserUI(
104: fc).new BasicFileView();
105: assertNotNull(fileView);
106: assertEquals(0, fileView.iconCache.size());
107: File f = new File("f");
108: f.createNewFile();
109: Icon i = new ImageIcon();
110: fileView.cacheIcon(f, i);
111: assertEquals(1, fileView.iconCache.size());
112: assertTrue(i == fileView.getCachedIcon(f));
113: assertTrue(i == fileView.getIcon(f));
114: fileView.cacheIcon(null, null);
115: assertEquals(1, fileView.iconCache.size());
116: assertEquals(f.getName(), fileView.getDescription(f));
117: ui.installUI(fc);
118: fileView = (BasicFileChooserUI.BasicFileView) ui
119: .getFileView(fc);
120: fileView.clearIconCache();
121: assertEquals(0, fileView.iconCache.size());
122: f.delete();
123: }
124:
125: public void testCreatePropertyChangeListener() throws Exception {
126: assertNull(ui.createPropertyChangeListener(fc));
127: }
128:
129: public void testGetApproveButton() {
130: ui.installUI(fc);
131: assertNull(ui.getApproveButton(fc));
132: }
133:
134: public void testGetDialogTitle() {
135: assertNull(ui.getDialogTitle(fc));
136: ui.installUI(fc);
137: fc.setDialogTitle("my");
138: assertEquals("my", ui.getDialogTitle(fc));
139: }
140:
141: public void testGetFileView() {
142: ui.installUI(fc);
143: assertNotNull(ui.getFileView(fc));
144: assertTrue(ui.getFileView(fc) instanceof BasicFileChooserUI.BasicFileView);
145: }
146:
147: public void testGetSetDirectory() {
148: assertNull(ui.getDirectory());
149: ui.installUI(fc);
150: assertNull(ui.getDirectory());
151: final File f = new File("aa/aa/aa");
152: ui.setDirectory(f);
153: assertEquals(f, ui.getDirectory());
154: }
155:
156: public void testGetSetDirectoryName() {
157: assertNull(ui.getDirectoryName());
158: ui.setDirectoryName("a");
159: assertNull(ui.getDirectoryName());
160: }
161:
162: public void testGetSetDirectorySelected() {
163: assertFalse(ui.isDirectorySelected());
164: ui.setDirectorySelected(true);
165: assertTrue(ui.isDirectorySelected());
166: }
167:
168: public void testGetAccessoryPanel() {
169: assertNull(ui.getAccessoryPanel());
170: ui.installUI(fc);
171: assertNotNull(ui.getAccessoryPanel());
172: JPanel ap = new JPanel();
173: fc.setAccessory(ap);
174: assertNotSame(ap, ui.getAccessoryPanel());
175: }
176:
177: public void testGetApproveButtonAction() {
178: ui.installUI(fc);
179: assertNotNull(ui.getApproveSelectionAction());
180: assertEquals(ui.getApproveSelectionAction(), ui
181: .getApproveSelectionAction());
182: }
183:
184: public void testGetPreferredSize() {
185: assertNull(ui.getPreferredSize(fc));
186: ui.installUI(fc);
187: assertNull(ui.getPreferredSize(fc));
188: }
189:
190: public void testEnsureFileIsVisible() {
191: try {
192: BasicFileChooserUI fc = new BasicFileChooserUI(null);
193: fc.ensureFileIsVisible(new JFileChooser(), new File("a"));
194: // PASSED
195: } catch (NullPointerException npe) {
196: fail("NPE should not be thrown");
197: }
198: }
199:
200: public void testInstallDefaults() {
201: try {
202: new BasicFileChooserUI(null) {
203: public void installDefaults(JFileChooser fc) {
204: super .installDefaults(fc);
205: }
206: }.installDefaults(null);
207: fail("NPE should be thrown");
208: } catch (NullPointerException npe) {
209: // Passed
210: }
211: }
212: }
|