1 16 17 package de.schlichtherle.key.passwd.swing; 18 19 import java.awt.*; 20 import java.util.*; 21 22 import javax.swing.*; 23 24 import junit.framework.*; 25 26 import org.netbeans.jemmy.*; 27 import org.netbeans.jemmy.operators.*; 28 import org.netbeans.jemmy.operators.JDialogOperator.JDialogFinder; 29 import org.netbeans.jemmy.util.*; 30 31 36 public class RemoteControl extends Assert implements Runnable { 37 private static int counter; 38 39 private final int index; 40 41 private static final Random rnd = new Random(); 42 43 44 public final String id; 45 46 50 public Throwable throwable; 51 52 public RemoteControl(final String id) { 53 this.index = counter++; 54 this.id = id; 55 } 56 57 public void run() { 58 try { 59 runIt(); 60 } catch (Throwable t) { 61 throwable = t; 62 } 63 } 64 65 private void runIt() throws AssertionError { 66 JDialogOperator dialog; 67 JPasswordFieldOperator passwd1, passwd2; 68 String passwd; 69 JCheckBoxOperator newPasswd; 70 71 dialog = waitCreateKeyDialog(); 72 73 assertNull(findErrorMessage(dialog)); 75 76 createResourceHook(dialog); 77 78 passwd1 = new JPasswordFieldOperator(dialog, 0); 80 passwd2 = new JPasswordFieldOperator(dialog, 1); 81 passwd1.typeText("foobar1"); 82 passwd2.clearText(); 83 passwd2.typeText("foobar2"); 84 85 pushDefaultButton(dialog); 86 87 dialog = waitCreateKeyDialog(); 88 89 assertNotNull(findErrorMessage(dialog)); 91 92 passwd = "secret " + index; 94 passwd1 = new JPasswordFieldOperator(dialog, 0); 95 passwd2 = new JPasswordFieldOperator(dialog, 1); 96 passwd1.typeText(passwd); 97 98 assertNull(findErrorMessage(dialog)); 100 101 passwd2.clearText(); 102 passwd2.typeText(passwd); 103 104 pushDefaultButton(dialog); 105 106 dialog = waitOpenKeyDialog(); 107 108 assertNull(findErrorMessage(dialog)); 110 111 newPasswd = new JCheckBoxOperator(dialog); 113 assertFalse(newPasswd.isSelected()); 114 115 newPasswd.push(); 117 118 passwd1 = new JPasswordFieldOperator(dialog, 0); 120 passwd2 = null; 121 passwd1.clearText(); 122 passwd1.typeText("wrong"); 123 124 pushDefaultButton(dialog); 125 126 dialog = waitOpenKeyDialog(); 127 128 assertNotNull(findErrorMessage(dialog)); 130 131 passwd1 = new JPasswordFieldOperator(dialog, 0); 133 passwd2 = null; 134 passwd1.clearText(); 135 passwd1.typeText(passwd); 136 137 assertNull(findErrorMessage(dialog)); 139 140 newPasswd = new JCheckBoxOperator(dialog); 142 assertTrue(newPasswd.isSelected()); 143 144 pushDefaultButton(dialog); 145 146 dialog = waitCreateKeyDialog(); 147 148 assertNull(findErrorMessage(dialog)); 150 151 overwriteResourceHook(dialog); 152 153 passwd1 = new JPasswordFieldOperator(dialog, 0); 155 passwd2 = new JPasswordFieldOperator(dialog, 1); 156 passwd1.typeText("short"); passwd2.clearText(); 158 passwd2.typeText("short"); 159 160 pushDefaultButton(dialog); 161 162 dialog = waitCreateKeyDialog(); 163 164 assertNotNull(findErrorMessage(dialog)); 166 167 if (0 == rnd.nextInt(2)) { 168 passwd = "top secret " + index; 170 passwd1 = new JPasswordFieldOperator(dialog, 0); 171 passwd2 = new JPasswordFieldOperator(dialog, 1); 172 passwd1.typeText(passwd); 173 174 assertNull(findErrorMessage(dialog)); 176 177 passwd2.clearText(); 178 passwd2.typeText(passwd); 179 180 pushDefaultButton(dialog); 181 } else { 182 dialog.close(); 184 } 185 } 186 187 protected void createResourceHook(JDialogOperator dialog) { 188 } 189 190 protected void overwriteResourceHook(JDialogOperator dialog) { 191 } 192 193 private JDialogOperator waitCreateKeyDialog() { 194 final JDialogOperator dialog = waitDialog(); 195 assertNotNull("Expected Create Key Dialog for \"" + id + "\"", 196 findPasswdField(dialog, 1)); 197 return dialog; 198 } 199 200 private JDialogOperator waitOpenKeyDialog() { 201 final JDialogOperator dialog = waitDialog(); 202 assertNull("Expected Open Key Dialog for \"" + id + "\"", 203 findPasswdField(dialog, 1)); 204 return dialog; 205 } 206 207 211 private JDialogOperator waitDialog() { 212 new QueueTool().waitEmpty(); JDialogOperator dialog = new JDialogOperator(new ByResourceIDFinder()); 216 new QueueTool().waitEmpty(); 221 222 assertNull(JDialogOperator.findJDialog( 225 new JDialogFinder(), 226 1)); 227 228 return dialog; 229 } 230 231 private static JPasswordFieldOperator findPasswdField(final JDialogOperator dialog, final int i) { 232 final JPasswordField field = JPasswordFieldOperator.findJPasswordField( 233 (Container) dialog.getSource(), 234 new JPasswordFieldOperator.JPasswordFieldFinder(), 235 i); 236 return field != null ? new JPasswordFieldOperator(field) : null; 237 } 238 239 private class ByResourceIDFinder implements ComponentChooser { 240 public boolean checkComponent(Component comp) { 241 return null != JTextComponentOperator.findJTextComponent( 242 (Container) comp, 243 new JTextComponentOperator.JTextComponentByTextFinder( 244 id, 245 new Operator.DefaultStringComparator(true, true))); 246 } 247 248 public String getDescription() { 249 return "Container with Component with exact text \"" + id + "\"."; 250 } 251 } 252 253 private static String findErrorMessage(final ContainerOperator dialog) { 254 final JLabelOperator errorLabel 255 = new JLabelOperator(dialog, new NameComponentChooser("error")); 256 if (errorLabel == null) 257 return null; 258 final String error = errorLabel.getText(); 259 if (error == null || error.trim().length() <= 0) 260 return null; 261 return error; 262 } 263 264 private void pushDefaultButton(JDialogOperator dialog) { 265 final JButtonOperator ok = new JButtonOperator( 266 dialog.getRootPane().getDefaultButton()); 267 while (!ok.isEnabled()) { 268 try { 269 Thread.sleep(50); 270 } catch (InterruptedException ignored) { 271 } 272 } 273 ok.push(); 274 } 275 } | Popular Tags |