1 /*
   2  * Copyright (c) 2013, 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 6384984 8004032
  27   @summary TrayIcon try to dispay a tooltip when is not visible
  28   @author Dmitry.Cherepanov@sun.com area=awt.tray
  29   @run applet/manual=yesno ShowAfterDisposeTest.html
  30 */
  31 
  32 import java.applet.*;
  33 
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 import java.awt.image.*;
  37 
  38 public class ShowAfterDisposeTest extends Applet
  39 {
  40     boolean traySupported;
  41 
  42     public void init()
  43     {
  44         this.setLayout (new BorderLayout ());
  45 
  46         String[] instructions;
  47         traySupported = SystemTray.isSupported();
  48         if (traySupported)
  49         {
  50             String[] s =
  51             {
  52                 "1) When the test starts an icon is added to the SystemTray area.",
  53                 "2a) If you use Apple OS X,",
  54                 "    right click on this icon (it's important to click before the tooltip is shown).",
  55                 "    The icon should disappear.",
  56                 "2b) If you use other os (Windows, Linux, Solaris),",
  57                 "    double click on this icon (it's important to click before the tooltip is shown).",
  58                 "    The icon should disappear.",
  59                 "3) If the bug is reproducible then the test will fail without assistance.",
  60                 "4) Just press the 'pass' button."
  61             };
  62             instructions = s;
  63         }
  64         else
  65         {
  66             String[] s =
  67             {
  68               "The test cannot be run because SystemTray is not supported.",
  69               "Simply press PASS button."
  70             };
  71             instructions = s;
  72         }
  73         Sysout.createDialogWithInstructions(instructions);
  74     }
  75 
  76     public void start ()
  77     {
  78         setSize (200,200);
  79         setVisible(true);
  80         validate();
  81 
  82         if (!traySupported)
  83         {
  84             return;
  85         }
  86 
  87         BufferedImage img = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
  88         Graphics g = img.createGraphics();
  89         g.setColor(Color.WHITE);
  90         g.fillRect(0, 0, 32, 32);
  91         g.setColor(Color.RED);
  92         g.fillRect(6, 6, 20, 20);
  93         g.dispose();
  94 
  95         final SystemTray tray = SystemTray.getSystemTray();
  96         final TrayIcon icon = new TrayIcon(img);
  97         icon.setImageAutoSize(true);
  98         icon.addActionListener(new ActionListener()
  99             {
 100                 public void actionPerformed(ActionEvent ev)
 101                 {
 102                     tray.remove(icon);
 103                 }
 104             }
 105         );
 106 
 107         try {
 108             tray.add(icon);
 109         } catch (AWTException e) {
 110             Sysout.println(e.toString());
 111             Sysout.println("!!! The test coudn't be performed !!!");
 112             return;
 113         }
 114         icon.setToolTip("tooltip");
 115     }
 116 }
 117 
 118 /****************************************************
 119  Standard Test Machinery
 120  DO NOT modify anything below -- it's a standard
 121   chunk of code whose purpose is to make user
 122   interaction uniform, and thereby make it simpler
 123   to read and understand someone else's test.
 124  ****************************************************/
 125 
 126 /**
 127  This is part of the standard test machinery.
 128  It creates a dialog (with the instructions), and is the interface
 129   for sending text messages to the user.
 130  To print the instructions, send an array of strings to Sysout.createDialog
 131   WithInstructions method.  Put one line of instructions per array entry.
 132  To display a message for the tester to see, simply call Sysout.println
 133   with the string to be displayed.
 134  This mimics System.out.println but works within the test harness as well
 135   as standalone.
 136  */
 137 
 138 class Sysout
 139 {
 140     private static TestDialog dialog;
 141 
 142     public static void createDialogWithInstructions( String[] instructions )
 143     {
 144         dialog = new TestDialog( new Frame(), "Instructions" );
 145         dialog.printInstructions( instructions );
 146         dialog.setVisible(true);
 147         println( "Any messages for the tester will display here." );
 148     }
 149 
 150     public static void createDialog( )
 151     {
 152         dialog = new TestDialog( new Frame(), "Instructions" );
 153         String[] defInstr = { "Instructions will appear here. ", "" } ;
 154         dialog.printInstructions( defInstr );
 155         dialog.setVisible(true);
 156         println( "Any messages for the tester will display here." );
 157     }
 158 
 159     public static void printInstructions( String[] instructions )
 160     {
 161         dialog.printInstructions( instructions );
 162     }
 163 
 164     public static void println( String messageIn )
 165     {
 166         dialog.displayMessage( messageIn );
 167     }
 168 }
 169 
 170 /**
 171   This is part of the standard test machinery.  It provides a place for the
 172    test instructions to be displayed, and a place for interactive messages
 173    to the user to be displayed.
 174   To have the test instructions displayed, see Sysout.
 175   To have a message to the user be displayed, see Sysout.
 176   Do not call anything in this dialog directly.
 177   */
 178 class TestDialog extends Dialog
 179 {
 180 
 181     TextArea instructionsText;
 182     TextArea messageText;
 183     int maxStringLength = 80;
 184 
 185     //DO NOT call this directly, go through Sysout
 186     public TestDialog( Frame frame, String name )
 187     {
 188         super( frame, name );
 189         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 190         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 191         add( "North", instructionsText );
 192 
 193         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 194         add("Center", messageText);
 195 
 196         pack();
 197 
 198         setVisible(true);
 199     }
 200 
 201     //DO NOT call this directly, go through Sysout
 202     public void printInstructions( String[] instructions )
 203     {
 204         //Clear out any current instructions
 205         instructionsText.setText( "" );
 206 
 207         //Go down array of instruction strings
 208 
 209         String printStr, remainingStr;
 210         for( int i=0; i < instructions.length; i++ )
 211         {
 212             //chop up each into pieces maxSringLength long
 213             remainingStr = instructions[ i ];
 214             while( remainingStr.length() > 0 )
 215             {
 216                 //if longer than max then chop off first max chars to print
 217                 if( remainingStr.length() >= maxStringLength )
 218                 {
 219                     //Try to chop on a word boundary
 220                     int posOfSpace = remainingStr.
 221                         lastIndexOf( ' ', maxStringLength - 1 );
 222 
 223                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 224 
 225                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 226                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 227                 }
 228                 //else just print
 229                 else
 230                 {
 231                     printStr = remainingStr;
 232                     remainingStr = "";
 233                 }
 234 
 235                 instructionsText.append( printStr + "\n" );
 236             }
 237         }
 238     }
 239 
 240     //DO NOT call this directly, go through Sysout
 241     public void displayMessage( String messageIn )
 242     {
 243         messageText.append( messageIn + "\n" );
 244         System.out.println(messageIn);
 245     }
 246 }