Menu

How do you control right click popup menus

2002-01-31
2003-01-29
  • Hugh Rodgers

    Hugh Rodgers - 2002-01-31

    Hello -

    What method(s) need to be called in JFCTestHelper to be able to display the right click menu for a component and to then select an item from that right click menu? How do you handle sub-menus within the right click menu?

    Thanks!

     
    • Vijay Aravamudhan

      To do this, you first need to call one of the overloaded constructors for MouseEventData which accepts a boolean for 'isPopupTrigger' and pass in the appropriate mask (for the right-mouse-click). After firing this event, you will get a popup menu.
      Using one of the finder methods, you can then "find" the top level menu(s) and then traverse subsequent hierarchy levels using the appropriate parent.
      Hope that helps.

       
    • Rakesh Madhwani

      Rakesh Madhwani - 2003-01-20

      Hi Vijay,
         After using ,

      helper.enterClickAndLeave( new MouseEventData(this, jcomp,1,true));

      I did get the PopupMenu, but I am unable to find the top level menu/menuitem using any of the finder methods.

      Can you please provide some more help on finding popup menus, which appears by Right clicking on component.

      Thanks in Advance.
      -Rakesh.

       
      • Kevin L Wilson

        Kevin L Wilson - 2003-01-21

        Rakesh,

        The problem you are trying to solve does not have a solution if you are using Java 1.2. Java 1.2 does not expose enough API's to access the popup menus and find components in there windows. Mainly because the popup window open in java 1.2 does not emit events. (If you are using this version then your application will need to expose the popupmenu to the test program.

        However, in Java 1.3 and Higher the JPopupMenu does emit window open events which are captured by the WindowMonitor of JFCUnit. If you are not finding your menu with the find methods, then be sure that the window monitor is tarted prior to the first popup instanciation. (Popup windows are cached and if missed, you will not be able to search the popups.)

        <code>WindowMonitor.start()</code>

        There are now expamples and explanation on how to capture the popup window in the latest documentation.

        Kevin

         
        • Rakesh Madhwani

          Rakesh Madhwani - 2003-01-21

          Hi Kevin,
              I am using Java 1.4 version for Compilation and Execution of my Test Application.
          I have used

          WindowMonitor.start()

          in the main() method, like this

          public static void main(String args[])
              {
                  WindowMonitor.start();
                  TestRunner.run(MyTest.class);
              }

          but still no luck of getting JPopupMenu.

          Also, can you please tell from where do I get latest documentation for JFCUnit. I have jfcunit_1.03 with me.

          With Regards,
          -Rakesh.

           
    • Rakesh Madhwani

      Rakesh Madhwani - 2003-01-27

      Hello Kevin,
          I went through the links provided by you, but the source code which you have provided, talks about MenuBar-Menus and not the menues which appears on Mouse Right Click (JPopupMenu)

      Is there any proposal in JFCUnit 1.04 for accesing the Popup menu which appears with Mouse right click ? And when 1.04 release will be out for use ?
      As of now, I am not able to Detect the Right Mouse click popups, even by using WindowMonitor.start().

      With Regards,
      -Rakesh.

      Java Version: 1.4
      JFCUnit Version: 1.03

       
    • Kevin L Wilson

      Kevin L Wilson - 2003-01-29

      Works for me.

      package junit.extensions.jfcunit;

      import junit.extensions.jfcunit.finder.JMenuItemFinder;

      import java.awt.Window;
      import java.awt.event.ActionEvent;
      import java.util.Arrays;
      import java.util.Vector;

      import javax.swing.JButton;
      import javax.swing.AbstractAction;
      import javax.swing.JFrame;
      import javax.swing.JMenuItem;
      import javax.swing.JPopupMenu;

      /**
      * Test class to exercise the WindowMonitor class.
      *
      * @author Kevin Wilson
      * @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
      */
      public class WindowMonitorT extends AbstractTestCase {
          /**
           * <code>Window</code> which is being tested.
           */
          private Window window = null;

          /**
           * Constructor
           *
           * @param name Test case name.
           */
          public WindowMonitorT(String name) {
              super(name);
          }

          /**
           * Setup the test case.
           *
           * @exception  Exception   An instance of java.lang.Exception can be thrown
           */
          protected void setUp() throws Exception {
              super.setUp();
              if (helper == null) {
                  helper = new JFCTestHelper();
              }
          }

          /**
           * Overridden method that is used to remove the test fixtures.
           *
           * @exception  Exception   An instance of java.lang.Exception can be thrown
           */
          protected void tearDown() throws Exception {
              TestHelper.disposeWindow(window, this);
              super.tearDown();
          }

          private boolean abcPressed = false;
          private boolean defPressed = false;

          /**
           * Validate that we can capture the Heavy Weight
           * popup window.
           *
           * @exception  Exception   An instance of java.lang.Exception can be thrown
           */
          public void testGetPopupMenu() throws Exception {
              JButton button = new JButton("TEST");
              JPopupMenu menu = new JPopupMenu("TEST Menu");
              menu.setLightWeightPopupEnabled(false);
              menu.add(new AbstractAction("ABC") {
                public void actionPerformed(ActionEvent ae) {
                 abcPressed = true;
                }
              });;
              menu.add(new AbstractAction("DEF") {
                public void actionPerformed(ActionEvent ae) {
                  defPressed = true;
                }
              });

              JFrame frame = createJFrame(null);
              window = frame; // register this to be destroyed at the end
              frame.getContentPane().add(button);
              frame.pack();
              frame.setTitle("testPopup");
              frame.setVisible(true);
              menu.show(button, 30, 30);

              flushAWT();

              helper = new JFCTestHelper();
              JMenuItem item = (JMenuItem) TestHelper.findComponent(new JMenuItemFinder("ABC"), 0);
              assertNotNull("Could not find the first item in menu:", item);
              helper.enterClickAndLeave(new junit.extensions.jfcunit.eventdata.MouseEventData(this,item));
              assertTrue("ABC not pressed", abcPressed);

              menu.show(button, 30, 30);
              item = (JMenuItem) TestHelper.findComponent(new JMenuItemFinder("DEF"), 0);
              assertNotNull("Could not find the second item in menu:", item);
              helper.enterClickAndLeave(new junit.extensions.jfcunit.eventdata.MouseEventData(this,item));
              assertTrue("DEF not pressed", defPressed);
          }

          /**
           * Validate that we can capture the JWindow.
           *
           * @exception  Exception   An instance of java.lang.Exception can be thrown
           */
          public void testGetWindows_JWindow() throws Exception {
              SplashScreen splashScreen = new SplashScreen();

              Window[] windows = WindowMonitor.getWindows();
              Vector results = new Vector(Arrays.asList(windows));
              assertTrue("Windows are not the same", results.contains(splashScreen));
              window = splashScreen; // register this to be destroyed at the end
          }
      }

       

Log in to post a comment.