1 /*
   2  * Copyright (c) 2015, 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 import java.awt.Color;
  25 import java.awt.GridBagConstraints;
  26 import java.awt.GridBagLayout;
  27 import java.awt.event.ActionEvent;
  28 import java.awt.event.ActionListener;
  29 import javax.swing.BorderFactory;
  30 import javax.swing.JButton;
  31 import javax.swing.JFrame;
  32 import javax.swing.JPanel;
  33 import javax.swing.JTextArea;
  34 import javax.swing.SwingUtilities;
  35 import static javax.swing.WindowConstants.EXIT_ON_CLOSE;
  36 
  37 /* @test
  38  * @bug 8148555
  39  * @summary verifies JTextArea emoji enter exception. Emoji is not supported.
  40  * @requires (os.family=="mac")
  41  * @run main JTextAreaEmojiTest
  42  */
  43 public class JTextAreaEmojiTest implements
  44         ActionListener {
  45 
  46     private static GridBagLayout layout;
  47     private static JPanel textAreaPanel;
  48     private static JPanel mainControlPanel;
  49     private static JPanel instructionPanel;
  50     private static JPanel resultButtonPanel;
  51     private static JPanel controlPanel;
  52     private static JTextArea instructionTextArea;
  53     private static JTextArea emojiTextArea;
  54     private static JButton passButton;
  55     private static JButton failButton;
  56 
  57     private static JFrame mainFrame;
  58 
  59     public static void main(String[] args) throws Exception {
  60 
  61         JTextAreaEmojiTest test = new JTextAreaEmojiTest();
  62     }
  63 
  64     public JTextAreaEmojiTest() throws Exception {
  65         createControlPanelUI();
  66     }
  67 
  68     public final void createControlPanelUI() throws Exception {
  69         SwingUtilities.invokeAndWait(new Runnable() {
  70             @Override
  71             public void run() {
  72                 layout = new GridBagLayout();
  73                 mainControlPanel = new JPanel(layout);
  74                 instructionPanel = new JPanel(layout);
  75                 resultButtonPanel = new JPanel(layout);
  76                 textAreaPanel = new JPanel(layout);
  77                 controlPanel = new JPanel(layout);
  78 
  79                 GridBagConstraints gbc = new GridBagConstraints();
  80                 String instructions
  81                         = "1) Text Area size should be zero"
  82                         + "\n2) Select one emoji from Character Viewer"
  83                         + "\n3) If Text Area size increases displaying"
  84                         + "Blank or supported emoji for default font, click pass"
  85                         + "\n4) Else press fail";
  86                 instructionTextArea = new JTextArea();
  87                 instructionTextArea.setText(instructions);
  88                 instructionTextArea.setEnabled(false);
  89                 instructionTextArea.setDisabledTextColor(Color.black);
  90                 instructionTextArea.setBackground(Color.white);
  91                 instructionTextArea.setBorder(
  92                         BorderFactory.createLineBorder(Color.black));
  93                 gbc.gridx = 0;
  94                 gbc.gridy = 0;
  95                 gbc.fill = GridBagConstraints.HORIZONTAL;
  96                 instructionPanel.add(instructionTextArea, gbc);
  97 
  98                 emojiTextArea = new JTextArea();
  99                 emojiTextArea.setEnabled(true);
 100                 emojiTextArea.setDisabledTextColor(Color.black);
 101                 emojiTextArea.setBackground(Color.white);
 102                 emojiTextArea.setBorder(
 103                         BorderFactory.createLineBorder(Color.black));
 104                 gbc.gridx = 0;
 105                 gbc.gridy = 1;
 106                 gbc.fill = GridBagConstraints.HORIZONTAL;
 107                 textAreaPanel.add(emojiTextArea, gbc);
 108 
 109                 passButton = new JButton("Pass");
 110                 passButton.setActionCommand("Pass");
 111                 passButton.addActionListener(JTextAreaEmojiTest.this);
 112                 failButton = new JButton("Fail");
 113                 failButton.setActionCommand("Fail");
 114                 failButton.addActionListener(JTextAreaEmojiTest.this);
 115                 gbc.gridx = 0;
 116                 gbc.gridy = 0;
 117                 resultButtonPanel.add(passButton, gbc);
 118                 gbc.gridx = 1;
 119                 gbc.gridy = 0;
 120                 resultButtonPanel.add(failButton, gbc);
 121 
 122                 gbc.gridx = 0;
 123                 gbc.gridy = 0;
 124                 mainControlPanel.add(instructionPanel, gbc);
 125                 gbc.gridx = 0;
 126                 gbc.gridy = 1;
 127                 mainControlPanel.add(textAreaPanel, gbc);
 128                 gbc.gridx = 0;
 129                 gbc.gridy = 2;
 130                 mainControlPanel.add(resultButtonPanel, gbc);
 131 
 132                 mainControlPanel.add(controlPanel, gbc);
 133                 mainFrame = new JFrame("Control Panel");
 134                 mainFrame.add(mainControlPanel);
 135                 mainFrame.pack();
 136                 mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
 137                 mainFrame.setVisible(true);
 138             }
 139         });
 140     }
 141 
 142     @Override
 143     public void actionPerformed(ActionEvent evt) {
 144         if (evt.getSource() instanceof JButton) {
 145             JButton btn = (JButton) evt.getSource();
 146             cleanUp();
 147 
 148             switch (btn.getActionCommand()) {
 149                 case "Pass":
 150                     break;
 151                 case "Fail":
 152                     throw new AssertionError("Test case has failed!");
 153             }
 154         }
 155     }
 156 
 157     private static void cleanUp() {
 158         mainFrame.dispose();
 159     }
 160 }