dimanche 26 avril 2015

Unit Testing a function that takes ActionEvent as a parameter

how can I test a function that takes "ActionEvent" type parameter in Java/Swing? The following is the function that I want to test:

public void actionPerformed(ActionEvent e)
    {
                            // Communicating with the Model class //

        /** if the "Add" button has been pressed **/
    if (e.getActionCommand() == "Add")
    {
        String cust_name = view.getAddCustNameField().getText();
        int ph_num = Integer.parseInt(view.getAddPhoneNumField().getText());
        model.addEntry(cust_name, ph_num, "controller");
    }

    /** if the "Delete" button has been pressed **/
    else if (e.getActionCommand() == "Delete")
    {
        String cust_name = view.getDelCustNameField().getText();
        model.deleteEntry(cust_name, "controller");
    }

    /** if the "Update" button has been pressed **/
    else if (e.getActionCommand() == "Update")
    {
        String cust_name = view.getUpdCustNameField().getText();
        int ph_num = Integer.parseInt(view.getUpdPhoneNumField().getText());
        model.updateEntry(cust_name, ph_num, "controller");
    }

                        // Communicating with the View class //     
    else 
    {

        /** if the "Change Family" button has been pressed **/
        Font fontRes = view.getResDisplayField().getFont();
        Font fontDir = view.getDirDisplayField().getFont();
        if (e.getActionCommand() == "Change Family")
        {
            String fontFamily = view.getFontFamilyField().getText();    
            if (fontFamily != "")
            {
                view.getResDisplayField().setFont(new Font(fontFamily, fontDir.getStyle(), fontRes.getSize()));
            }
        }

        /** if the "Change Size" button has been pressed **/
        else if (e.getActionCommand() == "Change Size")
        {
            int fontWeight = Integer.parseInt(view.getFontSizeField().getText());               
            if (fontWeight > 0)
            {
                view.getResDisplayField().setFont(new Font(fontRes.getFamily(), fontRes.getStyle(), fontWeight));
                view.getDirDisplayField().setFont(new Font(fontDir.getFamily(), fontDir.getStyle(), fontWeight));
            }
        }

        /** if the "Change Italics" button has been pressed **/
        else if (e.getActionCommand() == "Change Style")
        {
            String fontStyle = (String)(view.getFontStyleCombo().getSelectedItem());
            if (fontStyle == "Plain")
            {
                view.getResDisplayField().setFont(new Font(fontRes.getFamily(), Font.PLAIN, fontRes.getSize()));
                view.getDirDisplayField().setFont(new Font(fontDir.getFamily(), Font.PLAIN, fontDir.getSize()));
            }

            else if (fontStyle == "Italic")
            {
                view.getResDisplayField().setFont(new Font(fontRes.getFamily(), Font.ITALIC, fontRes.getSize()));
                view.getDirDisplayField().setFont(new Font(fontDir.getFamily(), Font.ITALIC, fontDir.getSize()));
            }

            else if (fontStyle == "Bold")
            {
                view.getResDisplayField().setFont(new Font(fontRes.getFamily(), Font.BOLD, fontRes.getSize()));
                view.getDirDisplayField().setFont(new Font(fontDir.getFamily(), Font.BOLD, fontDir.getSize()));
            }
        }

    }// 'else' (if the command is meant for the view class)

So, basically I want to emulate button pressing actions and depending on a particular button pressed one of the branches of the condition statement is executed. Corresponding to every button there is a set of 2 JTextFields whose value I have to somehow set in my unit test function. How can I do so?

Aucun commentaire:

Enregistrer un commentaire