alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */


/*
 * TestEditorWizard.java
 * NetBeans JUnit based test
 *
 * Created on April 18, 2002, 4:11 PM
 */
package org.netbeans.test.gui.editor;

import junit.framework.*;
import org.netbeans.junit.*;

import java.io.File;

import java.awt.event.KeyEvent;
import java.awt.Container;
import java.awt.Component;

import javax.swing.AbstractButton;

import org.netbeans.test.oo.gui.jelly.Explorer;

import org.netbeans.jemmy.operators.JTreeOperator;
import org.netbeans.jemmy.operators.JTextFieldOperator;
import org.netbeans.jemmy.EventDispatcher;
import org.netbeans.jemmy.ComponentSearcher;
import org.netbeans.jemmy.operators.JButtonOperator;
import org.netbeans.jemmy.TimeoutExpiredException;

import org.netbeans.test.oo.gui.jam.JamController;
import org.netbeans.test.oo.gui.jam.JamContainer;
import org.netbeans.test.oo.gui.jam.JamButton;
import org.netbeans.test.oo.gui.jam.JamTextComponent;
import org.netbeans.test.oo.gui.jam.JamUtilities;
import org.netbeans.test.oo.gui.jam.JamMenuBar;
import org.netbeans.test.oo.gui.jam.JamFrame;
import org.netbeans.test.oo.gui.jam.JamDialog;
import org.netbeans.test.oo.gui.jam.JamTree;
import org.netbeans.test.oo.gui.jam.JamTextField;

import org.netbeans.test.oo.gui.jello.JelloUtilities;

public class TestEditorWizard extends NbTestCase {

    private static String workDir = null;
    private static Explorer explorer = null;
    private static JTreeOperator treeOper = null;

    public TestEditorWizard(String testName) {
        super(testName);
    }

    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }

    public static Test suite() {
        TestSuite suite = new NbTestSuite(TestEditorWizard.class);
        /* TestSuite suite = new NbTestSuite();
        suite.addTest(new TestEditorWizard("testHelpButtonKey"));
        */
        System.out.println("Test started");
        workDir = System.getProperty("scratch.dir").replace('/',File.separatorChar); 
        JamController.setFast(false);
        explorer = Explorer.find();
        if(explorer == null) {
            fail("Explorer was not found");
        }
        explorer.switchToFilesystemsTab();
        treeOper = explorer.getJTreeOperator();
        (new Thread () {
            public void run() {
                try {
                JamDialog nbDialog = new JamDialog("Mount Web Module");
                new JamButton(nbDialog,"Ok").doClick();
                } catch (Exception e) {
                }
            }
        }).run();
        return suite;
    }
    private void delay() {
        try {
            JamUtilities.waitEventQueueEmpty();
        } catch (Exception e) {
        }
    }
    private void pressKey(int mask, int key) {
        new EventDispatcher(explorer.getContainer()).dispatchKeyEvent(KeyEvent.KEY_PRESSED, mask, key);
        delay();
	    new EventDispatcher(explorer.getContainer()).dispatchKeyEvent(KeyEvent.KEY_RELEASED, mask, key); 
        delay();
    }

    /* Tests for focus/enable properties */

    // TEST 1-2

    public void testWizardNameFocus() {
        JTreeOperator jTree = null; //Filesystem tree
        JamTextField jName = null; //JSP Name
        JamTextField jPackage = null; //Package path
        JamDialog wizard = getNewWizard();
        if(wizard == null)  {
            fail("New wizard dialog initialization failed");
            return;
        }
        jTree = new JTreeOperator(JTreeOperator.findJTree(wizard.getContainer(), ComponentSearcher.getTrueChooser("JamTree")));
        jName = new JamTextField(wizard,0); 
        jPackage = new JamTextField(wizard,1);
        if(!jName.hasFocus()) {
            new JamButton(wizard, "Cancel").doClick();
            fail("text field Name should be focused");
        }
        pressKey(KeyEvent.ALT_MASK,KeyEvent.VK_N);

        if(!jName.hasFocus()) {
            new JamButton(wizard, "Cancel").doClick();
            fail("text field Name didn't recieve a focus after ALT-N");
        }

        System.out.println("jName: " + jName.hasFocus());
        new JamButton(wizard, "Cancel").doClick();
    }

    // TEST 3-4

    public void testWizardPackageFocus() {
        JamTextField jPackage = null; //Package path
        JamDialog wizard = getNewWizard();
        if(wizard == null)  {
            fail("New wizard dialog initialization failed");
            return;
        }
        jPackage = new JamTextField(wizard,1);
        if(jPackage.hasFocus()) {
            new JamButton(wizard, "Cancel").doClick();
            fail("text field Package should not be focused");
        }
        pressKey(KeyEvent.ALT_MASK,KeyEvent.VK_P);

        if(!jPackage.hasFocus()) {
            new JamButton(wizard, "Cancel").doClick();
            fail("text field Package didn't recieve a focus after ALT-P");
        }
        new JamButton(wizard, "Cancel").doClick();
    }

    // TEST 5-6

    public void testWizardTreeFocus() {
        JTreeOperator jTree = null; //Filesystem tree
        JamDialog wizard = getNewWizard();
        if(wizard == null)  {
            fail("New wizard dialog initialization failed");
            return;
        }
        jTree = new JTreeOperator(JTreeOperator.findJTree(wizard.getContainer(), ComponentSearcher.getTrueChooser("JamTree")));
        if(jTree.hasFocus()) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Tree should not be focused");
        }
        pressKey(KeyEvent.ALT_MASK,KeyEvent.VK_S);

        if(!jTree.hasFocus()) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Tree didn't recieve a focus after ALT-S");
        }
        new JamButton(wizard, "Cancel").doClick();
    }

    // TEST 7-8

    public void testFinishButtonCheck() {
        AbstractButton finish = null;
        JamDialog wizard = getNewWizard();
        if(wizard == null)  {
            fail("New wizard dialog initialization failed");
            return;
        }
        finish = getButton(wizard, "Finish");
        if (finish == null) {
                new JamButton(wizard, "Cancel").doClick();
                fail("Can't find Finish button");
        }
        if(!finish.isEnabled()) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Finish button is not enabled");
        } else 
        if(finish.hasFocus()) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Finish button should  not be focused");
        }
        new JamButton(wizard, "Cancel").doClick();
    }

    // TEST 9-10

    public void testBackButtonCheck() {
        AbstractButton back = null;
        JamDialog wizard = getNewWizard();
        if(wizard == null)  {
            fail("New wizard dialog initialization failed");
            return;
        }
        back = getButton(wizard, "Back");
        if(back == null) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Can't find Back button");
        }
        if(!back.isEnabled()) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Back button is not enabled");
        } else 
        if(back.hasFocus()) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Back button should not be focused");
        }
        new JamButton(wizard, "Cancel").doClick();
    }

    // TEST 11-12

    public void testNextButtonCheck() {
        AbstractButton next = null;
        JamDialog wizard = getNewWizard();
        if(wizard == null)  {
            fail("New wizard dialog initialization failed");
            return;
        }
        next = getButton(wizard, "Next");
        if(next == null) {
            fail("Can't find Next button");
        }
        if(next.isEnabled()) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Next button should not be enabled");
        } else 
        if(next.hasFocus()) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Next button should not be focused");
        }
        new JamButton(wizard, "Cancel").doClick();
    }

    //TEST 13-14
    public void testCancelButtonCheck() {
        AbstractButton cancel = null;
        JamDialog wizard = getNewWizard();
        if(wizard == null)  {
            fail("New wizard dialog initialization failed");
            return;
        }
        cancel = getButton(wizard, "Cancel");
        if(cancel == null) {
            fail("Can't find Cancel button");
        }
        if(!cancel.isEnabled()) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Cancel button should be enabled");
        } else 
        if(cancel.hasFocus()) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Cancel button should not be focused");
        }
        new JamButton(wizard, "Cancel").doClick();
    }

    //TEST 15-16
    public void testHelpButtonCheck() {
        AbstractButton help = null;
        JamDialog wizard = getNewWizard();
        if(wizard == null)  {
            fail("New wizard dialog initialization failed");
            return;
        }
        help = getButton(wizard, "Help");
        if(help == null) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Can't find Help button");
        }
        if(!help.isEnabled()) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Help button should be enabled");
        } else 
        if(help.hasFocus()) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Help button should not be focused");
        }
        new JamButton(wizard, "Cancel").doClick();
    }

    /* Tests for editable fields. Check that we can change all necessary fields */

    // TEST 17-18  Name field Editable, FileSystem tree selectable

    public void testNameFieldEditable() {
        JamTextField jName = null; //JSP Name
        JTreeOperator jTree = null; //Filesystem tree
        JamDialog wizard = getNewWizard();
        if(wizard == null)  {
            fail("New wizard dialog initialization failed");
            return;
        }
        jName = new JamTextField(wizard,0); 
        String newName = "JSP_1";
        jName.setText(newName);
        jTree = new JTreeOperator(JTreeOperator.findJTree(wizard.getContainer(), ComponentSearcher.getTrueChooser("JamTree")));
        jTree.selectPath(jTree.findPath(workDir,Explorer.delim));
        new JamButton(wizard, "Finish").doClick();
        try {
            treeOper.findPath(workDir + Explorer.delim + newName, Explorer.delim);
        } catch (org.netbeans.jemmy.TimeoutExpiredException timeExc) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Node with new JSP name was not created");
        }
    }

    // TEST 19  Package field Editable

    public void testPackageFieldEditable() {
        JamTextField jPackage = null; //Package path
        JTreeOperator jTree = null; //Filesystem tree
        JamDialog wizard = getNewWizard();
        if(wizard == null)  {
            fail("New wizard dialog initialization failed");
            return;
        }
        jTree = new JTreeOperator(JTreeOperator.findJTree(wizard.getContainer(), ComponentSearcher.getTrueChooser("JamTree")));
        jTree.selectPath(jTree.findPath(workDir,Explorer.delim));
        jPackage = new JamTextField(wizard,1);
        String newPackageName = "NewPackage";
        jPackage.setText(newPackageName);
        new JamButton(wizard, "Finish").doClick();
        try {
            treeOper.findPath(workDir + Explorer.delim + newPackageName + Explorer.delim + "JSP", Explorer.delim);
        } catch (org.netbeans.jemmy.TimeoutExpiredException timeExc) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Node with new package name was not created");
        }
    }

    /* Button key bindings  tests */

    // TEST 20

    public void testFinishButtonKey() {
        JamTextField jName = null; //JSP Name
        JTreeOperator jTree = null; //Filesystem tree
        AbstractButton finish = null;
        JamDialog wizard = getNewWizard();
        if(wizard == null)  {
            fail("New wizard dialog initialization failed");
            return;
        }
        jName = new JamTextField(wizard,0); 
        jTree = new JTreeOperator(JTreeOperator.findJTree(wizard.getContainer(), ComponentSearcher.getTrueChooser("JamTree")));
        jTree.selectPath(jTree.findPath(workDir,Explorer.delim));
        String newName = "JSP_FINISH";
        jName.setText(newName);
        pressKey(0,KeyEvent.VK_ENTER);
        try {
            treeOper.findPath(workDir + Explorer.delim + newName, Explorer.delim);
        } catch (org.netbeans.jemmy.TimeoutExpiredException timeExc) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Enter key didn't work");
        }
    }

    // TEST 21

    public void testCancelButtonKey() {
        JamTextField jName = null; //JSP Name
        JTreeOperator jTree = null; //Filesystem tree
        AbstractButton finish = null;
        JamDialog wizard = getNewWizard();
        if(wizard == null)  {
            fail("New wizard dialog initialization failed");
            return;
        }
        jName = new JamTextField(wizard,0); 
        jTree = new JTreeOperator(JTreeOperator.findJTree(wizard.getContainer(), ComponentSearcher.getTrueChooser("JamTree")));
        jTree.selectPath(jTree.findPath(workDir,Explorer.delim));
        String newName = "JSP_CANCEL";
        jName.setText(newName);
        pressKey(0,KeyEvent.VK_ESCAPE);
        try {
            treeOper.findPath(workDir + Explorer.delim + newName, Explorer.delim);
            new JamButton(wizard, "Cancel").doClick();
            fail("Cancel key didn't work");
        } catch (org.netbeans.jemmy.TimeoutExpiredException timeExc) {
            //It's okay that we haven't found a new JSP
        }
    }

    // TEST 22

    public void testBackButtonKey() {
        JamDialog wizard = getNewWizard();
        JamDialog backWizard = null;
        if(wizard == null)  {
            fail("New wizard dialog initialization failed");
            return;
        }
        pressKey(KeyEvent.ALT_MASK,KeyEvent.VK_B);
        backWizard = new JamDialog("New Wizard");
        if (backWizard == null) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Alt-B for Back button didn't work");
        }
        new JamButton(backWizard, "Cancel").doClick();
    }

    // TEST 23

    public void testHelpButtonKey() {
        JamDialog wizard = getNewWizard();
        JamFrame helpFrame = null;
        JamDialog backWizard = null;
        if(wizard == null)  {
            fail("New wizard dialog initialization failed");
            return;
        }
        pressKey(0,KeyEvent.VK_F1);
        helpFrame = new JamFrame ("Help - All");
        if(helpFrame == null) {
            new JamButton(wizard, "Cancel").doClick();
            fail("Help was not found after F1 key");
        }
        new JamButton(wizard, "Cancel").doClick();
    }


    /* Private methods  */

    private AbstractButton getButton(JamContainer container, String label) {
        AbstractButton button = JButtonOperator.findJButton(container.getContainer(), label, false, false, 0); // null if not found
        if (button == null) {
            try {
                button = JButtonOperator.waitJButton(container.getContainer(), label, false, false, 0); // null if not found
            } catch (TimeoutExpiredException e) {    
                return null;
            }
        }
        return button;
    }

    private JamDialog getNewWizard() {
        JamDialog newWizard = null;
        explorer.selectNode(workDir);
        JamMenuBar mainMenu = null;
        try {
            mainMenu = new JamMenuBar(new JamFrame(JelloUtilities.getMainFrame()));
            mainMenu.push("File|New...");
            newWizard = new JamDialog("New Wizard");
            JTreeOperator jTree = new JTreeOperator(JTreeOperator.findJTree(newWizard.getContainer(), ComponentSearcher.getTrueChooser("JamTree")));
            jTree.selectPath(jTree.findPath("JSPs & Servlets" + Explorer.delim + "JSP", Explorer.delim, false, false));
            new JamButton(newWizard, "Next").doClick();
        } catch(Exception e) {
            return null;
        }
        return newWizard;
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.