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