Detecting Popup menus is a nasty task in AWT and swing. There is no direct handle to the popup window generally available in AWT or swing. As the popup window does not appear in the Frame list returned by Frame.getFrames() or the dialog list Dialog.getDialogs(). To complicate matters even further, the popup window which is generated by AWT is reused whenever possible, temporarily hosting a menu or combo box. This means that there is only one instance when the popup can be detected. On the AWT event queue when the first popup is opened.

How JFCUnit deals with Detection 

JFCUnit detects popup via the window creation events on the AWT Event queue. This method works in a non-intrusive method to the application being tested. However, this also has its drawbacks in that the WindowMonitor must be started prior to any GUI component starting. If a GUI component opens the WindowMonitor after a popup is created, none of the popups in the application will be searchable. This is due to the fact that the popup window is cached and re-used by the AWT.

When is the window monitor started

The window monitor is started when the first JFCTestCase is created. If a application is started prior to the JFCTestCase creation, then the user may need to initiate the WindowManager from the startup code for the application. The below shows how to start the window monitor.

junit.extensions.jfcunit.WindowMonitor.start();

Finding a menu item in a popup

When the window monitor is started before the GUI, finding a menu item within a popup window is easy. Simply use a find method on the TestHelper without the container specified.

JMenuItem item = (JMenuItem) TestHelper.findComponent(new JMenuItemFinder("ABC"), 0);