1 /*
   2  * Copyright (c) 2018, 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  * @key headful
  27  * @bug 8208743
  28  * @summary Non resizable focusable JDialog containing JTextArea is not
  29  *          focusable and not allowing text entry
  30  * @run main NonResizableFocusableDialogTest
  31  */
  32 import java.awt.AWTException;
  33 import java.awt.BorderLayout;
  34 import java.awt.Robot;
  35 import java.awt.event.KeyEvent;
  36 import java.lang.reflect.InvocationTargetException;
  37 import javax.swing.JDialog;
  38 import javax.swing.SwingUtilities;
  39 import javax.swing.JPanel;
  40 import javax.swing.JTextArea;
  41 import javax.swing.JScrollPane;
  42 import javax.swing.border.EmptyBorder;
  43 import javax.swing.JLabel;
  44 
  45 public class NonResizableFocusableDialogTest {
  46     private JTextArea textArea;
  47     private JDialog jDialog;
  48 
  49     private void createDialogWithTextArea() {
  50         jDialog = new JDialog();
  51         jDialog.setTitle("JDialog with a JTextArea");
  52         JPanel mainPanel = new JPanel(new BorderLayout());
  53         mainPanel.setOpaque(false);
  54 
  55         JPanel topPanel = new JPanel(new BorderLayout());
  56         topPanel.setBorder(new EmptyBorder(0, 5, 5, 0));
  57         JLabel topLabel = new JLabel("This is a automatic test");
  58         topLabel.setOpaque(false);
  59 
  60         topPanel.add(topLabel, BorderLayout.NORTH);
  61         mainPanel.add(topPanel, BorderLayout.NORTH);
  62 
  63         final TextPanel textPanel = new TextPanel(40);
  64         mainPanel.add(textPanel, BorderLayout.CENTER);
  65         mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  66 
  67         jDialog.add(mainPanel, BorderLayout.CENTER);
  68         jDialog.pack();
  69         jDialog.setResizable(false);
  70         jDialog.setVisible(true);
  71     }
  72 
  73     private void enterTextInTextArea() throws InvocationTargetException, InterruptedException {
  74         try {
  75             Robot robot = new Robot();
  76             robot.delay(3000);
  77             robot.keyPress(KeyEvent.VK_T);
  78             robot.keyRelease(KeyEvent.VK_T);
  79             robot.waitForIdle();
  80 
  81             //verify if text is entered in text area, if not throw exception
  82             if(!"t".equals(textArea.getText())) {
  83                 throw new RuntimeException("JTextArea is not focusable, text can't be" +
  84                         " entered in text area. TEST FAILED");
  85             }
  86         } catch (AWTException e) {
  87             throw new RuntimeException("Robot creation failed. TEST FAILED");
  88         } finally {
  89             SwingUtilities.invokeAndWait(jDialog::dispose);
  90         }
  91     }
  92 
  93     public static void main(String args[]) throws Exception {
  94         NonResizableFocusableDialogTest dialog = new NonResizableFocusableDialogTest();
  95         //create a JTextArea in JDialog.
  96         SwingUtilities.invokeAndWait(dialog::createDialogWithTextArea);
  97         //Try to enter text in JTextArea and check if text can be entered
  98         dialog.enterTextInTextArea();
  99     }
 100 
 101     private class TextPanel extends JPanel {
 102         TextPanel(int charactersPerLine) {
 103             textArea = new JTextArea(20, charactersPerLine);
 104             textArea.setEditable(true);
 105             setLayout(new BorderLayout());
 106             final JScrollPane textScroll = new JScrollPane(textArea,
 107                     JScrollPane.VERTICAL_SCROLLBAR_NEVER,
 108                     JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
 109             add(textScroll, BorderLayout.CENTER);
 110         }
 111     }
 112 }
 113