1 7 22 23 package de.schlichtherle.io.swing; 24 25 import de.schlichtherle.io.File; 26 import java.awt.event.KeyEvent ; 27 import java.io.FilenameFilter ; 28 import java.io.IOException ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.text.Collator ; 31 import java.util.Arrays ; 32 import java.util.Iterator ; 33 import java.util.LinkedHashSet ; 34 import java.util.LinkedList ; 35 import java.util.List ; 36 import java.util.Set ; 37 import java.util.SortedSet ; 38 import java.util.TreeSet ; 39 import java.util.logging.Logger ; 40 41 import javax.swing.ComboBoxModel ; 42 import javax.swing.JComboBox ; 43 44 import junit.framework.*; 45 46 import org.netbeans.jemmy.JemmyProperties; 47 import org.netbeans.jemmy.TestOut; 48 import org.netbeans.jemmy.operators.JFrameOperator; 49 import org.netbeans.jemmy.operators.JTextComponentOperator; 50 51 56 public class FileComboBoxBrowserTest extends TestCase { 57 static { 58 JemmyProperties.setCurrentOutput(TestOut.getNullOutput()); } 60 61 private static final Logger logger 62 = Logger.getLogger(FileComboBoxBrowserTest.class.getName()); 63 64 public FileComboBoxBrowserTest(String testName) { 65 super(testName); 66 } 67 68 protected void setUp() throws Exception { 69 70 } 71 72 protected void tearDown() throws Exception { 73 } 74 75 78 public void testDirectory() { 79 final FileComboBoxBrowser browser = new FileComboBoxBrowser(); 80 final java.io.File cur = new File("."); 81 java.io.File dir; 82 83 dir = browser.getDirectory(); 84 assertEquals(cur, dir); 85 86 browser.setDirectory(cur); 87 dir = browser.getDirectory(); 88 assertSame(cur, dir); 89 90 browser.setDirectory(null); 91 dir = browser.getDirectory(); 92 assertEquals(cur, dir); 93 } 94 95 98 public void testState4Update() { 99 final FileComboBoxBrowser browser = new FileComboBoxBrowser(); 100 try { 101 browser.update(""); 102 fail("Calling the previous method should throw a NullPointerException!"); 103 } catch (NullPointerException expected) { 104 } 105 106 final JComboBox combo = new JComboBox (); 107 browser.setComboBox(combo); 108 browser.update(""); 109 } 110 111 public void testAutoCompletion() throws IOException { 112 java.io.File dir = new java.io.File ("."); 113 testAutoCompletion(dir); 114 testAutoCompletion(dir.getCanonicalFile()); 115 116 dir = new File("."); 117 testAutoCompletion(dir); 118 testAutoCompletion(dir.getCanonicalFile()); 119 } 120 121 public void testAutoCompletion(final java.io.File dir) { 122 final String [] entries = dir.list(); 123 if (entries == null || entries.length == 0) { 124 logger.warning("Current directory does not contain any files - skipping test!"); 125 return; 126 } 127 128 final JComboBox combo = new JComboBox (); 129 final FileComboBoxBrowser browser = new FileComboBoxBrowser(); 130 browser.setDirectory(dir); 131 132 for (int i = 0; i < entries.length; i++) { 133 final String entry = entries[i]; 134 for (int j = 0; j < entry.length(); j++) 135 testAutoCompletion(browser, combo, entry.substring(0, j)); 136 } 137 } 138 139 private static void testAutoCompletion( 140 final FileComboBoxBrowser browser, 141 final JComboBox combo, 142 final String initials) { 143 browser.setComboBox(null); combo.removeAllItems(); combo.setEditable(true); 146 combo.setSelectedItem(initials); 147 browser.setComboBox(combo); assertEquals(filter(initials), asList(combo)); 149 } 150 151 private static List filter(final String initials) { 152 final String [] entries = new File(".").list(new FilenameFilter () { 153 final int l = initials.length(); 154 155 public boolean accept(java.io.File dir, String name) { 156 if (name.length() >= l) 157 return initials.equalsIgnoreCase(name.substring(0, l)); 158 else 159 return false; 160 } 161 }); 162 Arrays.sort(entries, Collator.getInstance()); 163 return Arrays.asList(entries); 164 } 165 166 private static List asList(final JComboBox combo) { 167 final List list = new LinkedList (); 168 final ComboBoxModel model = combo.getModel(); 169 for (int i = 0, l = model.getSize(); i < l; i++) 170 list.add(model.getElementAt(i)); 171 return list; 172 } 173 174 public void testGUI() throws InterruptedException , InvocationTargetException { 175 FileComboBoxPanel.main(new String [] { "." }); 176 177 179 final JFrameOperator frame = new JFrameOperator(); 180 final JTextComponentOperator tc0 = new JTextComponentOperator(frame, 0); 181 final JTextComponentOperator tc1 = new JTextComponentOperator(frame, 1); 182 183 tc0.typeText("?"); 185 assertEquals("?", tc1.getText()); 186 187 tc1.clearText(); 189 assertEquals("", tc0.getText()); 190 191 tc0.pressKey(KeyEvent.VK_DOWN); 194 final String child = tc0.getText(); 195 assertEquals(child, tc1.getText()); 196 } 197 } 198 | Popular Tags |