| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package docrobot; |
| 20 |
|
| 21 |
import java.awt.*; |
| 22 |
import java.awt.image.BufferedImage; |
| 23 |
import java.io.File; |
| 24 |
import java.io.IOException; |
| 25 |
import java.util.HashMap; |
| 26 |
import java.util.Map; |
| 27 |
|
| 28 |
import javax.imageio.ImageIO; |
| 29 |
import javax.swing.JButton; |
| 30 |
import javax.swing.JDialog; |
| 31 |
|
| 32 |
import org.fest.swing.core.Robot; |
| 33 |
import org.fest.swing.edt.*; |
| 34 |
import org.fest.swing.timing.Condition; |
| 35 |
import org.fest.swing.timing.Pause; |
| 36 |
import org.jdesktop.swingx.JXLoginPane; |
| 37 |
import org.jdesktop.swingx.JXLoginPane.JXLoginDialog; |
| 38 |
import org.pushingpixels.substance.api.SubstanceLookAndFeel; |
| 39 |
import org.pushingpixels.substance.api.SubstanceSkin; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class LoginDialogRobot { |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
protected SubstanceSkin skin; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
protected String screenshotFilename; |
| 56 |
|
| 57 |
protected JXLoginDialog dialog; |
| 58 |
|
| 59 |
private Robot robot; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public LoginDialogRobot(SubstanceSkin skin, String screenshotFilename, |
| 70 |
Robot robot) { |
| 71 |
this.skin = skin; |
| 72 |
this.screenshotFilename = screenshotFilename; |
| 73 |
this.robot = robot; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public void run() { |
| 80 |
long start = System.currentTimeMillis(); |
| 81 |
|
| 82 |
|
| 83 |
GuiActionRunner.execute(new GuiTask() { |
| 84 |
@Override |
| 85 |
protected void executeInEDT() throws Throwable { |
| 86 |
SubstanceLookAndFeel.setSkin(skin); |
| 87 |
JDialog.setDefaultLookAndFeelDecorated(true); |
| 88 |
} |
| 89 |
}); |
| 90 |
robot.waitForIdle(); |
| 91 |
|
| 92 |
|
| 93 |
GuiActionRunner.execute(new GuiTask() { |
| 94 |
@Override |
| 95 |
protected void executeInEDT() throws Throwable { |
| 96 |
JXLoginPane loginPane = new JXLoginPane(); |
| 97 |
loginPane.setMessage("Sample login message"); |
| 98 |
loginPane.setBannerText("Sample banner"); |
| 99 |
|
| 100 |
dialog = new JXLoginDialog((Frame) null, loginPane); |
| 101 |
dialog.setVisible(true); |
| 102 |
} |
| 103 |
}); |
| 104 |
robot.waitForIdle(); |
| 105 |
|
| 106 |
|
| 107 |
Pause.pause(1000); |
| 108 |
|
| 109 |
|
| 110 |
final JButton loginButton = GuiActionRunner |
| 111 |
.execute(new GuiQuery<JButton>() { |
| 112 |
@Override |
| 113 |
protected JButton executeInEDT() throws Throwable { |
| 114 |
return getLoginButton(dialog.getContentPane()); |
| 115 |
} |
| 116 |
|
| 117 |
private JButton getLoginButton(Component comp) { |
| 118 |
if (comp instanceof JButton) { |
| 119 |
JButton button = (JButton) comp; |
| 120 |
if ("Login".equals(button.getText())) { |
| 121 |
return button; |
| 122 |
} |
| 123 |
} |
| 124 |
if (comp instanceof Container) { |
| 125 |
Container cont = (Container) comp; |
| 126 |
for (int i = 0; i < cont.getComponentCount(); i++) { |
| 127 |
JButton result = getLoginButton(cont |
| 128 |
.getComponent(i)); |
| 129 |
if (result != null) |
| 130 |
return result; |
| 131 |
} |
| 132 |
} |
| 133 |
return null; |
| 134 |
} |
| 135 |
}); |
| 136 |
|
| 137 |
|
| 138 |
if (loginButton != null) { |
| 139 |
robot.moveMouse(loginButton); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
GuiActionRunner.execute(new GuiTask() { |
| 144 |
@Override |
| 145 |
protected void executeInEDT() throws Throwable { |
| 146 |
makeScreenshot(); |
| 147 |
} |
| 148 |
}); |
| 149 |
|
| 150 |
|
| 151 |
if (loginButton != null) { |
| 152 |
GuiActionRunner.execute(new GuiTask() { |
| 153 |
@Override |
| 154 |
protected void executeInEDT() throws Throwable { |
| 155 |
loginButton.doClick(); |
| 156 |
} |
| 157 |
}); |
| 158 |
} |
| 159 |
|
| 160 |
Pause.pause(new Condition("Wait for the login button click") { |
| 161 |
@Override |
| 162 |
public boolean test() { |
| 163 |
return !dialog.isDisplayable(); |
| 164 |
} |
| 165 |
}); |
| 166 |
|
| 167 |
long end = System.currentTimeMillis(); |
| 168 |
System.out.println(this.getClass().getSimpleName() + " [" |
| 169 |
+ this.skin.getDisplayName() + "] : " + (end - start) + "ms"); |
| 170 |
} |
| 171 |
|
| 172 |
private boolean dismiss(Component comp) { |
| 173 |
if (comp instanceof JButton) { |
| 174 |
JButton button = (JButton) comp; |
| 175 |
if ("Login".equals(button.getText())) { |
| 176 |
button.doClick(); |
| 177 |
return true; |
| 178 |
} |
| 179 |
} |
| 180 |
if (comp instanceof Container) { |
| 181 |
Container cont = (Container) comp; |
| 182 |
for (int i = 0; i < cont.getComponentCount(); i++) { |
| 183 |
if (dismiss(cont.getComponent(i))) |
| 184 |
return true; |
| 185 |
} |
| 186 |
} |
| 187 |
return false; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public void makeScreenshot() { |
| 194 |
BufferedImage bi = new BufferedImage(dialog.getWidth(), dialog |
| 195 |
.getHeight(), BufferedImage.TYPE_INT_ARGB); |
| 196 |
Map<Component, Boolean> map = new HashMap<Component, Boolean>(); |
| 197 |
RobotUtilities.makePreviewable(dialog.getRootPane(), map); |
| 198 |
Graphics g = bi.getGraphics(); |
| 199 |
dialog.paint(g); |
| 200 |
try { |
| 201 |
ImageIO |
| 202 |
.write(bi, "png", |
| 203 |
new File(this.screenshotFilename + ".png")); |
| 204 |
} catch (IOException ioe) { |
| 205 |
ioe.printStackTrace(); |
| 206 |
} |
| 207 |
} |
| 208 |
} |