Search in sources :

Example 1 with GenericTypeMatcher

use of org.fest.swing.core.GenericTypeMatcher in project intellij-community by JetBrains.

the class ActionLinkFixture method findByActionId.

@NotNull
public static ActionLinkFixture findByActionId(@NotNull final String actionId, @NotNull final Robot robot, @NotNull final Container container) {
    final Ref<ActionLink> actionLinkRef = new Ref<ActionLink>();
    pause(new Condition("Find ActionLink with ID '" + actionId + "'") {

        @Override
        public boolean test() {
            Collection<ActionLink> found = robot.finder().findAll(container, new GenericTypeMatcher<ActionLink>(ActionLink.class) {

                @Override
                protected boolean isMatching(@NotNull ActionLink actionLink) {
                    if (actionLink.isVisible()) {
                        AnAction action = actionLink.getAction();
                        String id = ActionManager.getInstance().getId(action);
                        return actionId.equals(id);
                    }
                    return false;
                }
            });
            if (found.size() == 1) {
                actionLinkRef.set(getFirstItem(found));
                return true;
            }
            return false;
        }
    }, SHORT_TIMEOUT);
    ActionLink actionLink = actionLinkRef.get();
    if (actionLink == null) {
        throw new ComponentLookupException("Failed to find ActionLink with ID '" + actionId + "'");
    }
    return new ActionLinkFixture(robot, actionLink);
}
Also used : Condition(org.fest.swing.timing.Condition) Ref(com.intellij.openapi.util.Ref) Collection(java.util.Collection) ComponentLookupException(org.fest.swing.exception.ComponentLookupException) GenericTypeMatcher(org.fest.swing.core.GenericTypeMatcher) NotNull(org.jetbrains.annotations.NotNull) AnAction(com.intellij.openapi.actionSystem.AnAction) ActionLink(com.intellij.ui.components.labels.ActionLink) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GenericTypeMatcher

use of org.fest.swing.core.GenericTypeMatcher in project intellij-community by JetBrains.

the class ActionLinkFixture method findActionLinkByName.

@NotNull
public static ActionLinkFixture findActionLinkByName(@NotNull final String actionName, @NotNull final Robot robot, @NotNull final Container container, @NotNull final Timeout timeout) {
    final Ref<ActionLink> actionLinkRef = new Ref<ActionLink>();
    pause(new Condition("Find ActionLink with name '" + actionName + "'") {

        @Override
        public boolean test() {
            Collection<ActionLink> found = robot.finder().findAll(container, new GenericTypeMatcher<ActionLink>(ActionLink.class) {

                @Override
                protected boolean isMatching(@NotNull ActionLink actionLink) {
                    if (actionLink.isVisible()) {
                        return actionLink.getText().equals(actionName);
                    }
                    return false;
                }
            });
            if (found.size() == 1) {
                actionLinkRef.set(getFirstItem(found));
                return true;
            }
            return false;
        }
    }, timeout);
    ActionLink actionLink = actionLinkRef.get();
    if (actionLink == null) {
        throw new ComponentLookupException("Failed to find ActionLink with name '" + actionName + "'");
    }
    return new ActionLinkFixture(robot, actionLink);
}
Also used : Condition(org.fest.swing.timing.Condition) Ref(com.intellij.openapi.util.Ref) Collection(java.util.Collection) ComponentLookupException(org.fest.swing.exception.ComponentLookupException) GenericTypeMatcher(org.fest.swing.core.GenericTypeMatcher) NotNull(org.jetbrains.annotations.NotNull) ActionLink(com.intellij.ui.components.labels.ActionLink) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GenericTypeMatcher

use of org.fest.swing.core.GenericTypeMatcher in project intellij-community by JetBrains.

the class IdeFrameFixture method find.

@NotNull
public static IdeFrameFixture find(@NotNull final Robot robot, @Nullable final File projectPath, @Nullable final String projectName) {
    final GenericTypeMatcher<IdeFrameImpl> matcher = new GenericTypeMatcher<IdeFrameImpl>(IdeFrameImpl.class) {

        @Override
        protected boolean isMatching(@NotNull IdeFrameImpl frame) {
            Project project = frame.getProject();
            if (projectPath == null && project != null)
                return true;
            if (project != null && PathManager.getAbsolutePath(projectPath.getPath()).equals(PathManager.getAbsolutePath(project.getBasePath()))) {
                return projectName == null || projectName.equals(project.getName());
            }
            return false;
        }
    };
    Pause.pause(new Condition("IdeFrame " + (projectPath != null ? quote(projectPath.getPath()) : "") + " to show up") {

        @Override
        public boolean test() {
            Collection<IdeFrameImpl> frames = robot.finder().findAll(matcher);
            return !frames.isEmpty();
        }
    }, GuiTestUtil.LONG_TIMEOUT);
    IdeFrameImpl ideFrame = robot.finder().find(matcher);
    return new IdeFrameFixture(robot, ideFrame, new File(ideFrame.getProject().getBasePath()));
}
Also used : Condition(org.fest.swing.timing.Condition) IdeFrameImpl(com.intellij.openapi.wm.impl.IdeFrameImpl) Project(com.intellij.openapi.project.Project) GenericTypeMatcher(org.fest.swing.core.GenericTypeMatcher) Assert.assertNotNull(junit.framework.Assert.assertNotNull) NotNull(org.jetbrains.annotations.NotNull) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) Assert.assertNotNull(junit.framework.Assert.assertNotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with GenericTypeMatcher

use of org.fest.swing.core.GenericTypeMatcher in project ats-framework by Axway.

the class SwingElementLocator method buildLocator.

private static <T extends Component> GenericTypeMatcher<T> buildLocator(Class<T> componentClass, final UiElementProperties properties, boolean requireVisible, final String[] propertyNamesToUseForMatch) {
    // nested class not to be anonymous in stack traces
    class MyGenericTypeMatcher<Type1 extends Component> extends GenericTypeMatcher<Type1> {

        private int currentIndex = 0;

        public MyGenericTypeMatcher(Class<Type1> componentClass, boolean requireVisible) {
            super(componentClass, requireVisible);
        }

        /**
             * In addition to the type check in constructor adds check by other component properties
             * @param component
             * @return
             */
        @Override
        protected boolean isMatching(Component component) {
            // here we are sure that we do not search by label and
            // also property "visible" and class are tracked additionally by FEST
            // Having several properties means match ALL of them
            int propertiesMatching = 0;
            int currentPropIdxToProcess = 0;
            for (currentPropIdxToProcess = 0; currentPropIdxToProcess < propertyNamesToUseForMatch.length; currentPropIdxToProcess++) {
                String keyName = propertyNamesToUseForMatch[currentPropIdxToProcess];
                if ("visible".equals(keyName) || "class".equals(keyName)) {
                    // already considered as parameter in search
                    propertiesMatching++;
                    continue;
                }
                String propertyValue = properties.getProperty(keyName);
                if (propertyValue != null) {
                    if ("name".equals(keyName)) {
                        if (propertyValue.equals(component.getName())) {
                            propertiesMatching++;
                            log.debug("Found element with 'name' property: " + component);
                            continue;
                        } else {
                            return false;
                        }
                    } else if ("text".equals(keyName)) {
                        // Search by specific component properties
                        if (component instanceof JButton) {
                            JButton button = (JButton) component;
                            if (propertyValue.equals(button.getText())) {
                                propertiesMatching++;
                                log.debug("Found element by 'text' property: " + button);
                                continue;
                            } else {
                                return false;
                            }
                        } else if (component instanceof JMenuItem) {
                            JMenuItem menuItem = (JMenuItem) component;
                            if (propertyValue.equals(menuItem.getText())) {
                                log.debug("Found element by 'text' property: " + menuItem);
                                propertiesMatching++;
                                continue;
                            } else {
                                return false;
                            }
                        } else if (component instanceof JPopupMenu) {
                            JPopupMenu popupMenu = (JPopupMenu) component;
                            if (propertyValue.equals(popupMenu.getLabel())) {
                                log.debug("Found element by 'text' property: " + popupMenu);
                                propertiesMatching++;
                                continue;
                            } else {
                                return false;
                            }
                        } else if (component instanceof JToggleButton) {
                            JToggleButton toggleButton = (JToggleButton) component;
                            if (propertyValue.equals(toggleButton.getText())) {
                                log.debug("Found element by 'text' property: " + toggleButton);
                                propertiesMatching++;
                                continue;
                            } else {
                                return false;
                            }
                        } else if (component instanceof JTextComponent) {
                            JTextComponent textComponent = (JTextComponent) component;
                            if (propertyValue.equals(textComponent.getText())) {
                                log.debug("Found element by 'text' property: " + textComponent);
                                propertiesMatching++;
                                continue;
                            } else {
                                return false;
                            }
                        } else if (component instanceof JLabel) {
                            JLabel label = (JLabel) component;
                            if (propertyValue.equals(label.getText())) {
                                log.debug("Found element by 'text' property: " + label);
                                propertiesMatching++;
                                continue;
                            }
                        }
                        // Attempt to search for 'text' for unsupported component type
                        return false;
                    } else if ("tooltip".equals(keyName)) {
                        // Search by specific component properties
                        if (component instanceof JButton) {
                            JButton button = (JButton) component;
                            if (propertyValue.equals(button.getToolTipText())) {
                                propertiesMatching++;
                                log.debug("Found element by 'tooltip' property: " + button);
                                continue;
                            } else {
                                return false;
                            }
                        }
                    } else if ("index".equals(keyName)) {
                        if (Integer.parseInt(propertyValue) == currentIndex++) {
                            propertiesMatching++;
                            continue;
                        }
                    } else {
                        throw new IllegalStateException("Attempt to search for not supported property: " + keyName + ", component: " + component);
                    }
                }
            // if property != null
            }
            if (propertyNamesToUseForMatch.length == propertiesMatching) {
                return true;
            } else {
                if (propertiesMatching > 0 && log.isDebugEnabled()) {
                    log.debug("Not all properties matched. Only " + propertiesMatching + " instead of " + properties.getPropertiesSize());
                }
                return false;
            }
        }
    }
    return new MyGenericTypeMatcher<T>(componentClass, requireVisible);
}
Also used : JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) JTextComponent(javax.swing.text.JTextComponent) GenericTypeMatcher(org.fest.swing.core.GenericTypeMatcher) JPopupMenu(javax.swing.JPopupMenu) JToggleButton(javax.swing.JToggleButton) Component(java.awt.Component) JTextComponent(javax.swing.text.JTextComponent) JMenuItem(javax.swing.JMenuItem)

Example 5 with GenericTypeMatcher

use of org.fest.swing.core.GenericTypeMatcher in project ats-framework by Axway.

the class SwingElementLocator method useComponentInspector.

/**
     * Log the clicked element (its type, properties and index in the component hierarchy from the current container)
     *
     * @param driver {@link SwingDriverInternal} instance
     */
public static void useComponentInspector(final SwingDriverInternal driver) {
    // long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK;
    long eventMask = AWTEvent.MOUSE_EVENT_MASK;
    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {

        public void eventDispatched(AWTEvent e) {
            if (e.getID() == MouseEvent.MOUSE_PRESSED && e.getSource() instanceof Component) {
                Component component = (Component) e.getSource();
                Class<?> swingClass = findSwingClass(component.getClass());
                if (swingClass == null) {
                    swingClass = component.getClass();
                    log.warn("Can't find swing class of type \"" + swingClass.getName() + "\"");
                }
                String logEntry = "\t[INSPECTOR] " + getProperties(component).replaceFirst("\\[", " [") + " index=" + calculateIndex(component, swingClass);
                if (!component.getClass().getName().equals(swingClass.getName())) {
                    logEntry += "\t(extends " + swingClass.getName() + ")";
                }
                System.out.println(logEntry);
            }
        }

        private String getProperties(Component c) {
            String properties = Formatting.inEdtFormat(c);
            if (c instanceof JButton) {
                String tooltip = ((JButton) c).getToolTipText();
                if (!StringUtils.isNullOrEmpty(tooltip)) {
                    int lastBrIndex = properties.lastIndexOf(']');
                    if (lastBrIndex > 0) {
                        properties = properties.substring(0, lastBrIndex) + ", tooltip='" + tooltip + "'" + properties.substring(lastBrIndex);
                    } else {
                        return c.getClass().getName() + " [tooltip='" + tooltip + "']";
                    }
                }
            }
            return properties;
        }

        private Class<?> findSwingClass(Class<?> clazz) {
            if (clazz == null) {
                return null;
            }
            if (clazz.getName().startsWith("javax.swing")) {
                return clazz;
            }
            return findSwingClass(clazz.getSuperclass());
        }

        @SuppressWarnings("unchecked")
        private int calculateIndex(final Component component, Class<?> swingClass) {
            ContainerFixture<?> containerFixture = driver.getActiveContainerFixture();
            Robot robot = null;
            if (containerFixture != null) {
                // use the current robot instance
                robot = containerFixture.robot;
            } else {
                robot = BasicRobot.robotWithCurrentAwtHierarchy();
            }
            List<Component> found = SwingElementFinder.find(robot.hierarchy(), ((containerFixture != null) ? containerFixture.component() : null), new GenericTypeMatcher<Component>((Class<Component>) swingClass, true) {

                @Override
                protected boolean isMatching(Component c) {
                    return true;
                }
            });
            return found.indexOf(component);
        }
    }, eventMask);
}
Also used : AWTEventListener(java.awt.event.AWTEventListener) JButton(javax.swing.JButton) GenericTypeMatcher(org.fest.swing.core.GenericTypeMatcher) ContainerFixture(org.fest.swing.fixture.ContainerFixture) AWTEvent(java.awt.AWTEvent) List(java.util.List) JList(javax.swing.JList) Component(java.awt.Component) JTextComponent(javax.swing.text.JTextComponent) Robot(org.fest.swing.core.Robot) BasicRobot(org.fest.swing.core.BasicRobot)

Aggregations

GenericTypeMatcher (org.fest.swing.core.GenericTypeMatcher)9 NotNull (org.jetbrains.annotations.NotNull)6 Condition (org.fest.swing.timing.Condition)5 Collection (java.util.Collection)4 Ref (com.intellij.openapi.util.Ref)3 ComponentLookupException (org.fest.swing.exception.ComponentLookupException)3 AnAction (com.intellij.openapi.actionSystem.AnAction)2 ActionLink (com.intellij.ui.components.labels.ActionLink)2 Component (java.awt.Component)2 JButton (javax.swing.JButton)2 JTextComponent (javax.swing.text.JTextComponent)2 BasicRobot (org.fest.swing.core.BasicRobot)2 Robot (org.fest.swing.core.Robot)2 ResourceComponent (com.android.tools.idea.editors.theme.ui.ResourceComponent)1 ElementNotFoundException (com.axway.ats.uiengine.exceptions.ElementNotFoundException)1 ActionButton (com.intellij.openapi.actionSystem.impl.ActionButton)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 IdeFrameImpl (com.intellij.openapi.wm.impl.IdeFrameImpl)1 AWTEvent (java.awt.AWTEvent)1