| 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.Graphics; |
| 22 |
import java.awt.KeyboardFocusManager; |
| 23 |
import java.awt.image.BufferedImage; |
| 24 |
import java.io.File; |
| 25 |
import java.io.IOException; |
| 26 |
|
| 27 |
import javax.imageio.ImageIO; |
| 28 |
import javax.swing.ImageIcon; |
| 29 |
import javax.swing.JFrame; |
| 30 |
|
| 31 |
import org.fest.swing.core.Robot; |
| 32 |
import org.fest.swing.edt.GuiActionRunner; |
| 33 |
import org.fest.swing.edt.GuiTask; |
| 34 |
import org.fest.swing.timing.Condition; |
| 35 |
import org.fest.swing.timing.Pause; |
| 36 |
import org.pushingpixels.substance.api.*; |
| 37 |
import org.pushingpixels.substance.internal.utils.SubstanceImageCreator; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class FrameRobot { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
protected SubstanceSkin skin; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
protected String screenshotFilename; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
protected String frameClass; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
protected boolean done = false; |
| 64 |
|
| 65 |
protected JFrame frame; |
| 66 |
|
| 67 |
private Robot robot; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public FrameRobot(String frameClass, SubstanceSkin skin, |
| 78 |
String screenshotFilename, Robot robot) { |
| 79 |
this.frameClass = frameClass; |
| 80 |
this.skin = skin; |
| 81 |
this.screenshotFilename = screenshotFilename; |
| 82 |
this.robot = robot; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public void run() { |
| 89 |
long start = System.currentTimeMillis(); |
| 90 |
|
| 91 |
|
| 92 |
GuiActionRunner.execute(new GuiTask() { |
| 93 |
@Override |
| 94 |
protected void executeInEDT() throws Throwable { |
| 95 |
SubstanceLookAndFeel.setSkin(skin); |
| 96 |
JFrame.setDefaultLookAndFeelDecorated(true); |
| 97 |
} |
| 98 |
}); |
| 99 |
robot.waitForIdle(); |
| 100 |
|
| 101 |
|
| 102 |
GuiActionRunner.execute(new GuiTask() { |
| 103 |
@Override |
| 104 |
protected void executeInEDT() throws Throwable { |
| 105 |
try { |
| 106 |
frame = (JFrame) Class.forName(frameClass).newInstance(); |
| 107 |
} catch (Exception exc) { |
| 108 |
exc.printStackTrace(); |
| 109 |
} |
| 110 |
frame.setIconImage(SubstanceImageCreator.getColorSchemeImage( |
| 111 |
null, new ImageIcon(FrameRobot.class.getClassLoader() |
| 112 |
.getResource( |
| 113 |
"test/resource/image-x-generic.png")), |
| 114 |
SubstanceLookAndFeel |
| 115 |
.getCurrentSkin(frame.getRootPane()) |
| 116 |
.getActiveColorScheme( |
| 117 |
DecorationAreaType.NONE), 0.0f)); |
| 118 |
|
| 119 |
frame.setVisible(true); |
| 120 |
} |
| 121 |
}); |
| 122 |
robot.waitForIdle(); |
| 123 |
|
| 124 |
|
| 125 |
GuiActionRunner.execute(new GuiTask() { |
| 126 |
@Override |
| 127 |
protected void executeInEDT() throws Throwable { |
| 128 |
KeyboardFocusManager.getCurrentKeyboardFocusManager() |
| 129 |
.clearGlobalFocusOwner(); |
| 130 |
} |
| 131 |
}); |
| 132 |
|
| 133 |
|
| 134 |
GuiActionRunner.execute(new GuiTask() { |
| 135 |
@Override |
| 136 |
protected void executeInEDT() throws Throwable { |
| 137 |
makeScreenshot(); |
| 138 |
} |
| 139 |
}); |
| 140 |
robot.waitForIdle(); |
| 141 |
|
| 142 |
|
| 143 |
GuiActionRunner.execute(new GuiTask() { |
| 144 |
@Override |
| 145 |
protected void executeInEDT() throws Throwable { |
| 146 |
frame.dispose(); |
| 147 |
} |
| 148 |
}); |
| 149 |
robot.waitForIdle(); |
| 150 |
|
| 151 |
|
| 152 |
Pause.pause(new Condition("Wait for the frame to become disposed") { |
| 153 |
@Override |
| 154 |
public boolean test() { |
| 155 |
return !frame.isDisplayable(); |
| 156 |
} |
| 157 |
}); |
| 158 |
|
| 159 |
long end = System.currentTimeMillis(); |
| 160 |
System.out.println(this.getClass().getSimpleName() + " [" |
| 161 |
+ skin.getDisplayName() + "] : " + (end - start) + "ms"); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public void makeScreenshot() { |
| 168 |
BufferedImage bi = new BufferedImage(frame.getWidth(), frame |
| 169 |
.getHeight(), BufferedImage.TYPE_INT_ARGB); |
| 170 |
Graphics g = bi.getGraphics(); |
| 171 |
frame.paint(g); |
| 172 |
try { |
| 173 |
ImageIO.write(bi, "png", new File(screenshotFilename + ".png")); |
| 174 |
} catch (IOException ioe) { |
| 175 |
ioe.printStackTrace(); |
| 176 |
} |
| 177 |
} |
| 178 |
} |