Skip to content

Commit

Permalink
#15 Let robot find popup even if it contains inner popup menus.
Browse files Browse the repository at this point in the history
Backport from Master to Branch 2.2.x (java 1.8 to 1.7)
(cherry picked from commit 9d5ff3b)
  • Loading branch information
croesch committed Nov 15, 2017
1 parent e90b7ab commit 180ae05
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static javax.swing.SwingUtilities.isEventDispatchThread;
import static org.assertj.core.util.Lists.newArrayList;
import static org.assertj.core.util.Preconditions.checkNotNull;
import static org.assertj.core.util.Sets.newHashSet;
import static org.assertj.core.util.Strings.concat;
import static org.assertj.swing.awt.AWT.centerOf;
import static org.assertj.swing.awt.AWT.visibleCenterOf;
Expand Down Expand Up @@ -62,6 +63,7 @@
import java.awt.event.WindowEvent;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -896,10 +898,34 @@ public boolean isReadyForInput(@Nonnull Component c) {
@RunsInEDT
private @Nullable JPopupMenu activePopupMenu() {
List<Component> found = newArrayList(finder().findAll(POPUP_MATCHER));
if (found.size() >= 1) {
return findOuterPopupMenu(found);
}
return null;
}

@RunsInEDT
private JPopupMenu findOuterPopupMenu(List<Component> found) {
if (found.size() == 1) {
return (JPopupMenu) found.get(0);
}
return null;
List<JPopupMenu> innerMenus = newArrayList();
for (Component component : found) {
innerMenus.addAll(popupMenus(((JPopupMenu) component).getComponents()));
}
found.removeAll(innerMenus);
return findOuterPopupMenu(found);
}

@RunsInEDT
private Collection<? extends JPopupMenu> popupMenus(Component[] components) {
Set<JPopupMenu> menus = newHashSet();
for (Component component : components) {
if (component instanceof JPopupMenu) {
menus.add((JPopupMenu) component);
}
}
return menus;
}

@RunsInEDT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,58 @@
package org.assertj.swing.core;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.swing.edt.GuiActionRunner.execute;

import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import org.assertj.swing.annotation.RunsInEDT;
import org.assertj.swing.edt.GuiQuery;
import org.junit.Test;

/**
* Tests for {@link BasicRobot#findActivePopupMenu()}.
*
*
* @author Yvonne Wang
* @author Alex Ruiz
*/
public class BasicRobot_findActivePopupMenu_Test extends BasicRobot_TestCase {
@RunsInEDT
final JPopupMenu addPopupMenuToTextFieldWithCascade() {
return createAndSetPopupMenuWithCascade(window().textField(), "Luke", "Leia");
}

@RunsInEDT
public static JPopupMenu createAndSetPopupMenuWithCascade(final JComponent c, final String itemOne,
final String itemTwo) {
return execute(new GuiQuery<JPopupMenu>() {
@Override
protected JPopupMenu executeInEDT() {
JPopupMenu popupMenu = new JPopupMenu();

JPopupMenu innerMenu = new JPopupMenu("cascade test");
innerMenu.add(new JMenuItem("cascade line 1"));
innerMenu.add(new JMenuItem("cascade line 2"));

popupMenu.add(new JMenuItem(itemOne));
popupMenu.add(innerMenu);
popupMenu.add(new JMenuItem(itemTwo));

c.setComponentPopupMenu(popupMenu);
return popupMenu;
}
});
}

@Test
public void should_Return_Outer_PopupMenu_When_Having_A_CascadingPopup() {
JPopupMenu popupMenu = addPopupMenuToTextFieldWithCascade();
robot().showPopupMenu(window().textField());
JPopupMenu found = robot().findActivePopupMenu();
assertThat(found).isSameAs(popupMenu);
}

@Test
public void should_Return_Active_PopupMenu() {
JPopupMenu popupMenu = addPopupMenuToTextField();
Expand Down

0 comments on commit 180ae05

Please sign in to comment.