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 org.netbeans.modules.web.core.syntax.*;
import org.netbeans.editor.ext.*;
import org.netbeans.editor.ext.java.*;

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

import javax.swing.AbstractButton;
import javax.swing.JList;
import javax.swing.ListModel;
import javax.swing.JScrollPane;
import javax.swing.JComponent;
import javax.swing.JViewport;

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

import org.netbeans.jemmy.operators.JScrollPaneOperator;
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.JamList;
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.jam.JamComboBox;

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

public class TestEditorKeyBinding extends NbTestCase {

    private static String workDir = null;
    private static Explorer explorer = null;
    private static Editor editor = null;
    private static int line = 1;


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

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

    public static Test suite() {
        TestSuite suite = new NbTestSuite(TestEditorKeyBinding.class); 
        workDir = System.getProperty("scratch.dir").replace('/',File.separatorChar); 
        JamController.setFast(false);
        explorer = Explorer.find();
        if(explorer == null) {
            fail("Explorer was not found");
        }
        explorer.switchToFilesystemsTab();
        (new Thread () {
            public void run() {
                try {
                JamDialog nbDialog = new JamDialog("Mount Web Module");
                new JamButton(nbDialog,"Ok").doClick();
                } catch (Exception e) {
                }
            }
        }).run();
        System.out.println("Test started");
            /* try {
                org.netbeans.test.oo.gui.jello.JelloOKOnlyDialog ok = new org.netbeans.test.oo.gui.jello.JelloOKOnlyDialog("Warning");
                ok.ok();
            } catch (Exception timeExc) {
                //This warning doesn't matter. We just need to skip it.
            } */
        return suite;
    }

    // Tests for Copy

    //TEST 1 - 2
    public void testCopyKey1 () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("Test");
        editor.select("Test",0);
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_C);
        editor.deleteRow(1);
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_V);
        if(!checkResult("Test")) {
            failure("Ctrl-C or Ctrl-V is not working");
        }
        closeEditor();
    }

    //TEST 3 - 4
    public void testCopyKey2 () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("Test");
        editor.select("Test",0);
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_INSERT);
        editor.deleteRow(1);
        pressKey(KeyEvent.SHIFT_MASK,KeyEvent.VK_INSERT);
        if(!checkResult("Test")) {
            failure("Ctrl-Insert or Shift-Insert is not working");
        }
        closeEditor();
    }

    //TEST 5
    public void testCutKey1 () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("Test Cut");
        editor.select("Test",0);
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_X);
        if(!checkResult(" Cut")) {
            failure("Ctrl-X is not working");
        }
        editor.goToLine(1);
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_V);
        if(!checkResult("Test Cut")) {
            failure("Ctrl-X doesn't copy to clipboard");
        }
        closeEditor();
    }

    //TEST 6
    public void testCutKey2 () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("Test Cut");
        editor.select("Test",0);
        pressKey(KeyEvent.SHIFT_MASK,KeyEvent.VK_DELETE);
        if(!checkResult(" Cut")) {
            failure("Shift-Delete is not working");
        }
        editor.goToLine(1);
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_V);
        if(!checkResult("Test Cut")) {
            failure("Shift-Delete doesn't copy to clipboard");
        }
        closeEditor();
    }

    //TEST 7-8

    public void testUndoRedoKey () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("Test UndoRedo");
        editor.select("Test",0);
        pressKey(0,KeyEvent.VK_DELETE);
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_Z);
        if(!checkResult("Test UndoRedo")) {
            failure("Ctrl-Z doesn't work");
        }
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_Y);
        if(!checkResult(" UndoRedo")) {
            failure("Ctrl-Y doesn't work");
        }
        closeEditor();
    }

    //TEST 9

    public void testDeleteLineKey () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insertRow("Line1",1);
        editor.insert("Line2",2,1);
        editor.goToLine(1);
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_E);
        if(!checkResult("Line2")) {
            failure("Ctrl-E doesn't work");
        }
        closeEditor();
    }
    // TEST 10

    public void testDeleteKey () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("DeleteKey",1,1);
        editor.goToLine(1);
        pressKey(0,KeyEvent.VK_DELETE);
        if(!checkResult("eleteKey")) {
            failure("Del key doesn't work");
        }
        closeEditor();
    }

    //TEST 11

    public void testDeletePreceedingKey () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("DeletePreceedingKey",1,1);
        editor.changeCaretPosition(1,7);
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_U);
        if(!checkResult("PreceedingKey")) {
            failure("Ctrl-U key doesn't work");
        }
        closeEditor();
    }

    //TEST 12

    public void testDeletePrevKey1 () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("DeletePrevKey",1,1);
        editor.changeCaretPosition(1,7);
        pressKey(0,KeyEvent.VK_BACK_SPACE);
        if(!checkResult("DeletPrevKey")) {
            failure("BackSpace key doesn't work");
        }
        closeEditor();
    }

    //TEST 13

    public void testDeletePrevKey2 () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("DeletePrevKey",1,1);
        editor.changeCaretPosition(1,7);
        pressKey(KeyEvent.SHIFT_MASK,KeyEvent.VK_BACK_SPACE);
        if(!checkResult("DeletPrevKey")) {
            failure("Shift+BackSpace key doesn't work");
        }
        closeEditor();
    }

    //TEST 14

    public void testDeleteSelectionKey1 () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("DeleteSelectionKey",1,1);
        editor.select("Delete");
        pressKey(0,KeyEvent.VK_BACK_SPACE);
        if(!checkResult("SelectionKey")) {
            failure("BackSpace key doesn't work for selection");
        }
        closeEditor();
    }

    //TEST 15

    public void testDeleteSelectionKey2 () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("DeleteSelectionKey",1,1);
        editor.select("Delete");
        pressKey(0,KeyEvent.VK_DELETE);
        if(!checkResult("SelectionKey")) {
            failure("Delete key doesn't work for selection");
        }
        closeEditor();
    }

    // TEST 16

    public void testDeleteWordKey () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("Delete Word",1,1);
        editor.goToLine(1);
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_W);
        if(!checkResult(" Word")) {
            failure("Ctrl-W key doesn't work");
        }
        closeEditor();
    }

    // TEST 17

    public void testEnterKey () {
        line = 1;
        prepareFile("KeyBinding");
        editor.goToLine(1);
        pressKey(0,KeyEvent.VK_ENTER);
        if(editor.getLineNumber() != 2) {
            failure("Enter key doesn't work");
        }
        closeEditor();
    }
    
    
    //TEST 18

    public void testExpandAbbrKey () {
        line = 1;
        prepareFile("KeyBinding");
        editor.changeCaretPosition(1,1);
        editor.getJEditorPaneOperator().typeKey('a');
        editor.getJEditorPaneOperator().typeKey('g');
        editor.getJEditorPaneOperator().typeKey(' ');
        if(!checkResult("application.getValue(\"")) {
            failure("Space key doesn't work for abbreviation");
        }
        closeEditor();
    }

    //TEST 19

    public void testTabKey () {
        line = 1;
        prepareFile("KeyBinding");
        editor.changeCaretPosition(1,1);
        pressKey(0,KeyEvent.VK_TAB);
        if(!checkResult("    ")) {
            failure("Tab key doesn't work");
        }
        closeEditor();
    }

    //TEST 20

    public void testGoToKey () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("\n\n\n\n\n\n\n\n\n\n");
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_G);
        try {
            JamDialog dialog = new JamDialog("Go to Line");
            JamComboBox box = new JamComboBox(dialog);
            box.setEditableText("3");
            new JamButton(dialog,"Goto").doClick();
        } catch (Exception e) {
            failure("Ctrl-G doesn't work");
        }
        if(editor.getLineNumber() != 3) {
            failure("Ctrl-G doesn't work");
        }
        closeEditor();
    }

    //TEST 21

    public void testFindKey () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("\n\nTest\n");
        editor.goToLine(1);
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_F);
        try {
            JamDialog dialog = new JamDialog("Find");
            JamComboBox box = new JamComboBox(dialog);
            box.setEditableText("Test");
            new JamButton(dialog,"Find").doClick();
        } catch (Exception e) {
            failure("Ctrl-F doesn't work");
        }
        if(editor.getLineNumber() != 3) {
            failure("Ctrl-F doesn't work");
        }
        closeEditor();
    }

    //TEST 22

    public void testFindNextKey () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("\n\nTest\n\nTest");
        editor.goToLine(1);
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_F);
        try {
            JamDialog dialog = new JamDialog("Find");
            JamComboBox box = new JamComboBox(dialog);
            box.setEditableText("Test");
            new JamButton(dialog,"Find").doClick();
        } catch (Exception e) {
            failure("Ctrl-F doesn't work");
        }
        if(editor.getLineNumber() != 3) {
            failure("Ctrl-F doesn't work");
        }
        pressKey(0,KeyEvent.VK_F3);
        if(editor.getLineNumber() != 5) {
            failure("F3 doesn't work");
        }
        closeEditor();
    }

    //TEST 23

    public void testFindPrevKey () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("\n\nTest\n\nTest");
        editor.goToLine(4);
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_F);
        try {
            JamDialog dialog = new JamDialog("Find");
            JamComboBox box = new JamComboBox(dialog);
            box.setEditableText("Test");
            new JamButton(dialog,"Find").doClick();
        } catch (Exception e) {
            failure("Ctrl-F doesn't work");
        }
        if(editor.getLineNumber() != 5) {
            failure("Ctrl-F doesn't work");
        }
        pressKey(KeyEvent.SHIFT_MASK,KeyEvent.VK_F3);
        pressKey(KeyEvent.SHIFT_MASK,KeyEvent.VK_F3);
        if(editor.getLineNumber() != 3) {
            failure("Shift-F3 doesn't work");
        }
        closeEditor();
    }

    //TEST 24

    public void testFindSelectionKey () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("\n\nTest\n\nTest");
        editor.goToLine(1);
        editor.select("Test");
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_F3);
        if(editor.getLineNumber() != 5) {
            failure("Ctrl-F3 doesn't work");
        }
        closeEditor();
    }

    //TEST 25

    public void testCtrlHomeKey () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("\n\nTest\n\nTest");
        editor.goToLine(5);
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_HOME);
        if(editor.getLineNumber() != 1) {
            failure("Ctrl-HOME doesn't work");
        }
        closeEditor();
    }

    //TEST 26

    public void testCtrlEndKey () {
        line = 1;
        prepareFile("KeyBinding");
        editor.insert("\n\nTest\n\nTest");
        editor.goToLine(1);
        pressKey(KeyEvent.CTRL_MASK,KeyEvent.VK_END);
        if(editor.getLineNumber() != 5) {
            failure("Ctrl-End doesn't work");
        }
        closeEditor();
    }
    // Private functions

    private boolean checkResult(String find) {
        if(editor.getText(line).regionMatches(0,find,0,find.length())) 
            return true;
        else
            return false;
    }
    private void failure(String reason) {
        closeEditor();
        fail(reason);
    }
    private void closeEditor() {
        (new Thread() {
            public void run() {
                JamDialog dialog = new JamDialog("Question");
                new JamButton(dialog,"Discard").doClick();
            }
        }).start();
        editor.close();  
    }
        

    private void delay() {
        try {
            Thread.sleep(2000);
        } catch (Exception e) {}
    }
    private void pressKey(int mask, int key) {
        
        new EventDispatcher(editor.getContainer()).dispatchKeyEvent(KeyEvent.KEY_PRESSED, mask, key);
	    new EventDispatcher(editor.getContainer()).dispatchKeyEvent(KeyEvent.KEY_RELEASED, mask, key); 
        delay(); 
        // editor.pushKey(key,mask);
    }
    private void prepareFile(String file) {
        if(file != null) 
            explorer.pushPopupMenu("Open",workDir + Explorer.delim + file + Explorer.delim + "JSP");
        else
            explorer.pushPopupMenu("Open",workDir + Explorer.delim + "JSP");

         editor = Editor.find("JSP");
         if(editor == null) {
             fail("Was unable to open an editor");
         }
    }

}
... 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.