//: swing/TestSwingApplication.java package swing; import static org.junit.Assert.*; import org.junit.Before; import org.junit.After; import org.junit.Test; import org.junit.BeforeClass; //Set wait's import org.netbeans.jemmy.QueueTool; //JFrame JButton ... import org.netbeans.jemmy.operators.*; //Set Some timeouts import org.netbeans.jemmy.JemmyProperties; //Key ENTER into TextField import java.awt.event.KeyEvent; /**Testing class by using Junit framework and * Jemmy 2 toolkit * * @author Shpatserman Maria */ public class TestSwingApplication { //JFrame of the Dialog JFrameOperator mainFrame; QueueTool mainQueue; @BeforeClass public static void setTimeouts(){ //input text timeout JemmyProperties.setCurrentTimeout("JTextComponentOperator.PushKeyTimeout", 50); //waiting Frame JemmyProperties.setCurrentTimeout("FrameWaiter.WaitFrameTimeout", 2000); //wait all components JemmyProperties.setCurrentTimeout("ComponentOperator.WaitComponentTimeout", 1000); } @Before public void getFrame(){ //Timeouts.setDefault("FrameWaiter.WaitFrameTimeout", 1500); SwingApplication.startApp(); mainFrame=new JFrameOperator(); mainQueue = new QueueTool(); mainQueue.waitEmpty(200); //Setting Timeouts } @After public void setDown(){ mainFrame.dispose(); } //TestingName of JFrame @Test public void testTitleFrame(){ String titleFrame = mainFrame.getTitle(); assertEquals("SwingApplication",titleFrame); mainQueue.waitEmpty(100); } //Testing First and Second Buttons @Test public void testFindComponents(){ JButtonOperator butn1 = new JButtonOperator(mainFrame,0); assertTrue(butn1.getText().equals("First")); mainQueue.waitEmpty(100); JButtonOperator butn2 = new JButtonOperator(mainFrame,1); assertTrue(butn2.getText().equals("Second")); mainQueue.waitEmpty(100); JLabelOperator LeftLabel = new JLabelOperator(mainFrame,0); assertTrue(LeftLabel.getName().equals("LeftLabel")); mainQueue.waitEmpty(100); JLabelOperator RightLabel = new JLabelOperator(mainFrame,1); assertTrue(RightLabel.getName().equals("RightLabel")); mainQueue.waitEmpty(100); JTextFieldOperator LeftField = new JTextFieldOperator(mainFrame,0); assertEquals("LeftField",LeftField.getName()); mainQueue.waitEmpty(100); JTextFieldOperator RightField = new JTextFieldOperator(mainFrame,1); assertEquals("RightField",RightField.getName()); } //Testing Action of theButtons //push buttons and Read TextFields @Test public void testActionButtons(){ JButtonOperator butn1 = new JButtonOperator(mainFrame,"First"); butn1.push(); JTextFieldOperator LeftField = new JTextFieldOperator(mainFrame,0); assertTrue("After First Button push LeftField should " + "contained \"First pressed\" string",LeftField.getText().equals("First pressed")); mainQueue.waitEmpty(500); JButtonOperator butn2 = new JButtonOperator(mainFrame,"Second"); butn2.push(); JTextFieldOperator RightField = new JTextFieldOperator(mainFrame,1); assertFalse("After Second Button push RightField should " + "not contain \"AAA\" string ",RightField.getText().equals("AAA")); mainQueue.waitEmpty(500); } @Test public void testActionFields(){ //typing text JTextFieldOperator LeftField = new JTextFieldOperator(mainFrame,0); LeftField.enterText("Typing Left Field"); mainQueue.waitEmpty(200); JLabelOperator LeftLabel = new JLabelOperator(mainFrame,"Typing Left Field"); assertNotNull("Find Left Label", LeftLabel); //clear text LeftField.clearText(); LeftField.pushKey(KeyEvent.VK_ENTER); assertTrue("Left label should be empty",LeftLabel.getText().equals("")); mainQueue.waitEmpty(200); //typing text JTextFieldOperator RightField = new JTextFieldOperator(mainFrame,1); RightField.enterText("Typing Right Field"); mainQueue.waitEmpty(200); assertNotNull("Find Right Label", new JLabelOperator(mainFrame,"Typing Right Field")); //clear text RightField.clearText(); RightField.pushKey(KeyEvent.VK_ENTER); assertTrue("Right label should be empty",RightField.getText().equals("")); mainQueue.waitEmpty(200); } }