1/*
2 * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25  test
26  @bug       6380743 8158380
27  @summary   Submenu should be shown by mnemonic key press.
28  @author    anton.tarasov@...: area=awt.focus
29  @run       applet SubMenuShowTest.html
30*/
31
32import java.awt.*;
33import java.awt.event.*;
34import javax.swing.*;
35import java.applet.Applet;
36import java.util.concurrent.atomic.AtomicBoolean;
37import java.lang.reflect.InvocationTargetException;
38import test.java.awt.regtesthelpers.Util;
39import jdk.testlibrary.OSInfo;
40
41public class SubMenuShowTest extends Applet {
42    Robot robot;
43    JFrame frame = new JFrame("Test Frame");
44    JMenuBar bar = new JMenuBar();
45    JMenu menu = new JMenu("Menu");
46    JMenu submenu = new JMenu("More");
47    JMenuItem item = new JMenuItem("item");
48    AtomicBoolean activated = new AtomicBoolean(false);
49
50    public static void main(String[] args) {
51        SubMenuShowTest app = new SubMenuShowTest();
52        app.init();
53        app.start();
54    }
55
56    public void init() {
57        robot = Util.createRobot();
58        robot.setAutoDelay(200);
59        robot.setAutoWaitForIdle(true);
60
61        // Create instructions for the user here, as well as set up
62        // the environment -- set the layout manager, add buttons,
63        // etc.
64        this.setLayout (new BorderLayout ());
65        Sysout.createDialogWithInstructions(new String[]
66            {"This is a manual test. Simple wait until it is done."
67            });
68    }
69
70    public void start() {
71        menu.setMnemonic('f');
72        submenu.setMnemonic('m');
73        menu.add(submenu);
74        submenu.add(item);
75        bar.add(menu);
76        frame.setJMenuBar(bar);
77        frame.pack();
78
79        item.addActionListener(new ActionListener() {
80                public void actionPerformed(ActionEvent e) {
81                    Sysout.println(e.toString());
82                    synchronized (activated) {
83                        activated.set(true);
84                        activated.notifyAll();
85                    }
86                }
87            });
88
89        frame.setVisible(true);
90
91        boolean isMacOSX = (OSInfo.getOSType() == OSInfo.OSType.MACOSX);
92        if (isMacOSX) {
93            robot.keyPress(KeyEvent.VK_CONTROL);
94        }
95        robot.keyPress(KeyEvent.VK_ALT);
96        robot.keyPress(KeyEvent.VK_F);
97        robot.keyRelease(KeyEvent.VK_F);
98        robot.keyRelease(KeyEvent.VK_ALT);
99
100        if (isMacOSX) {
101            robot.keyRelease(KeyEvent.VK_CONTROL);
102        }
103
104        robot.keyPress(KeyEvent.VK_M);
105        robot.keyRelease(KeyEvent.VK_M);
106        robot.keyPress(KeyEvent.VK_SPACE);
107        robot.keyRelease(KeyEvent.VK_SPACE);
108
109        if (!Util.waitForCondition(activated, 2000)) {
110            throw new TestFailedException("a submenu wasn't activated by mnemonic key press");
111        }
112
113        Sysout.println("Test passed.");
114    }
115}
116
117class TestFailedException extends RuntimeException {
118    TestFailedException(String msg) {
119        super("Test failed: " + msg);
120    }
121}
122
123/****************************************************
124 Standard Test Machinery
125 DO NOT modify anything below -- it's a standard
126  chunk of code whose purpose is to make user
127  interaction uniform, and thereby make it simpler
128  to read and understand someone else's test.
129 ****************************************************/
130
131/**
132 This is part of the standard test machinery.
133 It creates a dialog (with the instructions), and is the interface
134  for sending text messages to the user.
135 To print the instructions, send an array of strings to Sysout.createDialog
136  WithInstructions method.  Put one line of instructions per array entry.
137 To display a message for the tester to see, simply call Sysout.println
138  with the string to be displayed.
139 This mimics System.out.println but works within the test harness as well
140  as standalone.
141 */
142
143class Sysout
144{
145    static TestDialog dialog;
146
147    public static void createDialogWithInstructions( String[] instructions )
148    {
149        dialog = new TestDialog( new Frame(), "Instructions" );
150        dialog.printInstructions( instructions );
151//        dialog.setVisible(true);
152        println( "Any messages for the tester will display here." );
153    }
154
155    public static void createDialog( )
156    {
157        dialog = new TestDialog( new Frame(), "Instructions" );
158        String[] defInstr = { "Instructions will appear here. ", "" } ;
159        dialog.printInstructions( defInstr );
160//        dialog.setVisible(true);
161        println( "Any messages for the tester will display here." );
162    }
163
164
165    public static void printInstructions( String[] instructions )
166    {
167        dialog.printInstructions( instructions );
168    }
169
170
171    public static void println( String messageIn )
172    {
173        dialog.displayMessage( messageIn );
174    }
175
176}// Sysout  class
177
178/**
179  This is part of the standard test machinery.  It provides a place for the
180   test instructions to be displayed, and a place for interactive messages
181   to the user to be displayed.
182  To have the test instructions displayed, see Sysout.
183  To have a message to the user be displayed, see Sysout.
184  Do not call anything in this dialog directly.
185  */
186class TestDialog extends Dialog
187{
188
189    TextArea instructionsText;
190    TextArea messageText;
191    int maxStringLength = 80;
192
193    //DO NOT call this directly, go through Sysout
194    public TestDialog( Frame frame, String name )
195    {
196        super( frame, name );
197        int scrollBoth = TextArea.SCROLLBARS_BOTH;
198        instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
199        add( "North", instructionsText );
200
201        messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
202        add("Center", messageText);
203
204        pack();
205
206//        setVisible(true);
207    }// TestDialog()
208
209    //DO NOT call this directly, go through Sysout
210    public void printInstructions( String[] instructions )
211    {
212        //Clear out any current instructions
213        instructionsText.setText( "" );
214
215        //Go down array of instruction strings
216
217        String printStr, remainingStr;
218        for( int i=0; i < instructions.length; i++ )
219        {
220            //chop up each into pieces maxSringLength long
221            remainingStr = instructions[ i ];
222            while( remainingStr.length() > 0 )
223            {
224                //if longer than max then chop off first max chars to print
225                if( remainingStr.length() >= maxStringLength )
226                {
227                    //Try to chop on a word boundary
228                    int posOfSpace = remainingStr.
229                        lastIndexOf( ' ', maxStringLength - 1 );
230
231                    if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
232
233                    printStr = remainingStr.substring( 0, posOfSpace + 1 );
234                    remainingStr = remainingStr.substring( posOfSpace + 1 );
235                }
236                //else just print
237                else
238                {
239                    printStr = remainingStr;
240                    remainingStr = "";
241                }
242
243                instructionsText.append( printStr + "\n" );
244
245            }// while
246
247        }// for
248
249    }//printInstructions()
250
251    //DO NOT call this directly, go through Sysout
252    public void displayMessage( String messageIn )
253    {
254        messageText.append( messageIn + "\n" );
255        System.out.println(messageIn);
256    }
257
258}// TestDialog  class
259