All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.objogate.wl.swing.driver.JFrameDriver Maven / Gradle / Ivy

The newest version!
package com.objogate.wl.swing.driver;

import javax.swing.JFrame;
import java.awt.Component;
import static org.hamcrest.CoreMatchers.allOf;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import static org.hamcrest.Matchers.equalTo;
import com.objogate.wl.Prober;
import com.objogate.wl.Query;
import com.objogate.wl.swing.ComponentManipulation;
import com.objogate.wl.swing.ComponentSelector;
import com.objogate.wl.swing.gesture.GesturePerformer;
import com.objogate.wl.swing.probe.RecursiveComponentFinder;
import com.objogate.wl.swing.probe.SingleComponentFinder;
import com.objogate.wl.swing.probe.TopLevelWindowFinder;

public class JFrameDriver extends ComponentDriver {
    public JFrameDriver(GesturePerformer gesturePerformer, ComponentSelector componentSelector, Prober prober) {
        super(gesturePerformer, componentSelector, prober);
    }

    public JFrameDriver(ComponentDriver parentOrOwner, ComponentSelector componentSelector) {
        super(parentOrOwner, componentSelector);
    }

    public JFrameDriver(ComponentDriver parentOrOwner, Class componentType, Matcher... matchers) {
        super(parentOrOwner, componentType, matchers);
    }

    public JFrameDriver(GesturePerformer gesturePerformer, Prober prober, Matcher... withMatchers) {
        this(gesturePerformer, topLevelFrame(withMatchers), prober);
    }

    public JFrameDriver(ComponentDriver ownerDriver, Matcher... matchers) {
        super(ownerDriver,
                new SingleComponentFinder(
                        new RecursiveComponentFinder(JFrame.class, allOf(matchers),
                                ownerDriver.component())));
    }

    /**
     * Creates a ComponentSelector that will find a top-level JFrame that conforms
     * to the given matchers
     *
     * @param matchers The matchers to conform to
     * @return A ComponentSelector
     */
    public static ComponentSelector topLevelFrame(Matcher... matchers) {
        return new SingleComponentFinder(
                new RecursiveComponentFinder(JFrame.class, allOf(matchers),
                        new TopLevelWindowFinder()));
    }

    /**
     * Disposes of the frame and also sets the frame's name to null so that it cannot be found
     * by the {@link com.objogate.wl.swing.driver.ComponentDriver#named(String)} named} matcher in a subsequent
     * test while it is being garbage collected.
     */
    public void dispose() {
        perform("disposing", new ComponentManipulation() {
            public void manipulate(JFrame component) {
                component.dispose();
                component.setName(null);
            }
        });
    }

    public void hasTitle(String title) {
        hasTitle(equalTo(title));
    }

    public void hasTitle(Matcher matcher) {
        has(title(), matcher);
    }

    private static Query title() {
        return new Query() {
            public String query(JFrame label) {
                return label.getTitle();
            }

            public void describeTo(Description description) {
                description.appendText("frame title");
            }
        };
    }

    public JMenuBarDriver menuBar() {
        return new JMenuBarDriver(this);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy