Clover Coverage Report - FEST Swing 1.2
Coverage timestamp: Tue Jun 1 2010 15:19:25 PDT
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
46   193   25   2.88
12   107   0.54   16
16     1.56  
1    
 
  ScreenshotTaker       Line # 44 46 0% 25 3 95.9% 0.9594595
 
No Tests
 
1    /*
2    * Created on May 6, 2007
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10    * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11    * or implied. See the License for the specific language governing permissions and limitations under
12    * the License.
13    *
14    * Copyright @2007-2010 the original author or authors.
15    */
16    package org.fest.swing.image;
17   
18    import static org.fest.swing.core.FocusOwnerFinder.focusOwner;
19    import static org.fest.swing.edt.GuiActionRunner.execute;
20    import static org.fest.swing.image.ImageFileExtensions.PNG;
21    import static org.fest.swing.query.ComponentLocationOnScreenQuery.locationOnScreen;
22    import static org.fest.swing.query.ComponentSizeQuery.sizeOf;
23    import static org.fest.util.Strings.*;
24   
25    import java.awt.*;
26    import java.awt.image.BufferedImage;
27    import java.util.Locale;
28   
29    import javax.swing.text.Caret;
30    import javax.swing.text.JTextComponent;
31   
32    import org.fest.swing.annotation.RunsInEDT;
33    import org.fest.swing.edt.GuiQuery;
34    import org.fest.swing.edt.GuiTask;
35    import org.fest.swing.util.RobotFactory;
36    import org.fest.util.VisibleForTesting;
37   
38    /**
39    * Understands taking screenshots of the desktop and GUI components.
40    *
41    * @author Alex Ruiz
42    * @author Yvonne Wang
43    */
 
44    public class ScreenshotTaker {
45   
46    /**
47    * Extension of the image files containing the screenshots taken by instances of this class (png).
48    * @deprecated use <code>{@link ImageFileExtensions#PNG}</code> instead.
49    */
50    @Deprecated public static final String PNG_EXTENSION = "png";
51   
52    private final Robot robot;
53    private final ImageFileWriter writer;
54   
55    /**
56    * Creates a new <code>{@link ScreenshotTaker}</code>.
57    * @throws ImageException if a AWT Robot (the responsible for taking screenshots) cannot be instantiated.
58    */
 
59  7 toggle public ScreenshotTaker() {
60  7 this(new ImageFileWriter(), new RobotFactory());
61    }
62   
 
63  9 toggle @VisibleForTesting
64    ScreenshotTaker(ImageFileWriter writer, RobotFactory robotFactory) {
65  9 this.writer = writer;
66  9 try {
67  9 robot = robotFactory.newRobotInPrimaryScreen();
68    } catch (AWTException e) {
69  1 throw new ImageException("Unable to create AWT Robot", e);
70    }
71    }
72   
73    /**
74    * Takes a screenshot of the desktop and saves it as a PNG file.
75    * @param imageFilePath the path of the file to save the screenshot to.
76    * @throws ImageException if the given file path is <code>null</code> or empty.
77    * @throws ImageException if the given file path does not end with ".png".
78    * @throws ImageException if the given file path belongs to a non-empty directory.
79    * @throws ImageException if an I/O error prevents the image from being saved as a file.
80    */
 
81  1 toggle public void saveDesktopAsPng(String imageFilePath) {
82  1 saveImage(takeDesktopScreenshot(), imageFilePath);
83    }
84   
85    /**
86    * Takes a screenshot of the desktop.
87    * @return the screenshot of the desktop.
88    * @throws SecurityException if <code>readDisplayPixels</code> permission is not granted.
89    */
 
90  1 toggle public BufferedImage takeDesktopScreenshot() {
91  1 Rectangle r = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
92  1 return takeScreenshot(r);
93    }
94   
95    /**
96    * Takes a screenshot of the given <code>{@link java.awt.Component}</code> and saves it as a PNG file.
97    * @param c the given component.
98    * @param imageFilePath the path of the file to save the screenshot to.
99    * @throws ImageException if the given file path is <code>null</code> or empty.
100    * @throws ImageException if the given file path does not end with ".png".
101    * @throws ImageException if the given file path belongs to a non-empty directory.
102    * @throws ImageException if an I/O error prevents the image from being saved as a file.
103    */
 
104  2 toggle public void saveComponentAsPng(Component c, String imageFilePath) {
105  2 saveImage(takeScreenshotOf(c), imageFilePath);
106    }
107   
108    /**
109    * Takes a screenshot of the given <code>{@link java.awt.Component}</code>.
110    * @param c the given component.
111    * @return a screenshot of the given component.
112    * @throws SecurityException if <code>readDisplayPixels</code> permission is not granted.
113    */
 
114  103 toggle public BufferedImage takeScreenshotOf(Component c) {
115  103 Point locationOnScreen = locationOnScreen(c);
116  103 Dimension size = sizeOf(c);
117  103 Rectangle r = new Rectangle(locationOnScreen.x, locationOnScreen.y, size.width, size.height);
118  103 return takeScreenshot(r);
119    }
120   
 
121  104 toggle private BufferedImage takeScreenshot(Rectangle r) {
122  104 JTextComponent textComponent = findFocusOwnerAndHideItsCaret();
123  104 robot.waitForIdle();
124  104 try {
125  104 return takeScreenshot(robot, r);
126    } finally {
127  104 showCaretIfPossible(textComponent);
128    }
129    }
130   
 
131  104 toggle @RunsInEDT
132    private static JTextComponent findFocusOwnerAndHideItsCaret() {
133  104 return execute(new GuiQuery<JTextComponent>() {
 
134  104 toggle protected JTextComponent executeInEDT() {
135  104 Component focusOwner = focusOwner();
136  1 if (!(focusOwner instanceof JTextComponent)) return null;
137  103 JTextComponent textComponent = (JTextComponent)focusOwner;
138  103 Caret caret = textComponent.getCaret();
139  103 if (caret == null || !caret.isVisible()) return null;
140  103 caret.setVisible(false);
141  103 return textComponent;
142    }
143    });
144    }
145   
 
146  104 toggle private static BufferedImage takeScreenshot(final Robot robot, final Rectangle r) {
147  104 return execute(new GuiQuery<BufferedImage>() {
 
148  104 toggle protected BufferedImage executeInEDT() {
149  104 return robot.createScreenCapture(r);
150    }
151    });
152    }
153   
 
154  104 toggle private void showCaretIfPossible(JTextComponent textComponent) {
155  1 if (textComponent == null) return;
156  103 showCaretOf(textComponent);
157  103 robot.waitForIdle();
158    }
159   
 
160  103 toggle @RunsInEDT
161    private static void showCaretOf(final JTextComponent textComponent) {
162  103 execute(new GuiTask() {
 
163  103 toggle protected void executeInEDT() {
164  103 Caret caret = textComponent.getCaret();
165  103 if (caret != null) caret.setVisible(true);
166    }
167    });
168    }
169   
170    /**
171    * Save the given image as a PNG file.
172    * @param image the image to save.
173    * @param filePath the path of the file to save the image to.
174    * @throws ImageException if the given file path is <code>null</code> or empty.
175    * @throws ImageException if the given file path does not end with ".png".
176    * @throws ImageException if the given file path belongs to a non-empty directory.
177    * @throws ImageException if an I/O error prevents the image from being saved as a file.
178    */
 
179  7 toggle public void saveImage(BufferedImage image, String filePath) {
180  7 validate(filePath);
181  4 try {
182  4 writer.writeAsPng(image, filePath);
183    } catch (Exception e) {
184  1 throw new ImageException(concat("Unable to save image as ", quote(filePath)), e);
185    }
186    }
187   
 
188  7 toggle private void validate(String imageFilePath) {
189  2 if (isEmpty(imageFilePath)) throw new ImageException("The image path cannot be empty");
190  5 if (!imageFilePath.endsWith(PNG))
191  1 throw new ImageException(concat("The image file should be a ", PNG.toUpperCase(Locale.getDefault())));
192    }
193    }