001 package nl.cwi.sen1.visplugin.screenshotmaker; 002 003 import java.awt.Color; 004 import java.io.File; 005 006 import javax.swing.JLabel; 007 import javax.swing.JPanel; 008 009 010 import junit.framework.TestCase; 011 012 /** 013 * Test the screenshot function with different extensions. 014 * 015 * @author R. Bergen 016 * @author S. Tharmarajah 017 * @date 09-03-2007 018 */ 019 public class ScreenshotMakerTest extends TestCase{ 020 021 protected void setUp() throws Exception { 022 super.setUp(); 023 } 024 025 /** 026 * Test the screenshot function with different extensions. 027 * 028 * @author R. Bergen 029 * @author S. Tharmarajah 030 * @date 09-03-2007 031 */ 032 public void testExportScreenShot() { 033 //Create a test JPanel which can be saved 034 JPanel testPanel = new JPanel(); 035 testPanel.setSize(100,100); 036 testPanel.add(new JLabel("TEST")); 037 testPanel.setBackground(Color.CYAN); 038 039 //Testcases for supported file extensions 040 assertTrue(exportAs(testPanel, "testFile", "jpg")); 041 assertTrue(exportAs(testPanel, "testFile", "png")); 042 043 //Testcase for unsupported file extension 044 assertFalse(exportAs(testPanel, "testFile", "bogus")); 045 } 046 047 /** 048 * Test the screenshot function with different extensions. 049 * 050 * @param testPanel The Jpanel which should be saved 051 * @param exportDestination The destination of the exported image 052 * @param fileExtension The extension of the exported image 053 * 054 * @author R. Bergen 055 * @author S. Tharmarajah 056 * @date 09-03-2007 057 */ 058 private boolean exportAs(JPanel testPanel, String exportDestination, String fileExtension) { 059 ScreenshotMaker.saveScreenshot(testPanel, exportDestination, fileExtension); 060 ScreenshotMaker.saveScreenshot(testPanel, exportDestination, fileExtension); 061 /* 062 * Check if a screenshot is saved using the 063 * given extension when a bogus extension is given 064 */ 065 File file = new File(exportDestination + "." +fileExtension); 066 boolean exists = file.exists(); 067 068 if(exists) 069 file.delete(); 070 071 /* 072 * Check if a screenshot is saved using the 073 * default extension when a bogus extension is given 074 * 075 * The reason there is an extra test is that the screenshotmaker 076 * automatically gives a file with a bogus extension a default extension 077 */ 078 File fileWithDefaultExtension = new File(exportDestination + "." +ScreenshotMaker.DEFAULT_EXTENSION); 079 boolean fileWithDefaultExtensionExists = fileWithDefaultExtension.exists(); 080 081 if(fileWithDefaultExtensionExists) 082 fileWithDefaultExtension.delete(); 083 084 return exists; 085 } 086 }