1 /*
   2  * Copyright (c) 2002, 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   test 1.3 02/06/25
  25   @bug 4902933
  26   @summary Test that selecting the current item sends an ItemEvent
  27   @author bchristi : area= Choice
  28   @run applet SelectCurrentItemTest.html
  29 */
  30 
  31 // Note there is no @ in front of test above.  This is so that the
  32 //  harness will not mistake this file as a test file.  It should
  33 //  only see the html file as a test file. (the harness runs all
  34 //  valid test files, so it would run this test twice if this file
  35 //  were valid as well as the html file.)
  36 // Also, note the area= after Your Name in the author tag.  Here, you
  37 //  should put which functional area the test falls in.  See the
  38 //  AWT-core home page -> test areas and/or -> AWT team  for a list of
  39 //  areas.
  40 // Note also the 'SelectCurrentItemTest.html' in the run tag.  This should
  41 //  be changed to the name of the test.
  42 
  43 
  44 /**
  45  * SelectCurrentItemTest.java
  46  *
  47  * summary:
  48  */
  49 
  50 import java.applet.Applet;
  51 import java.awt.*;
  52 import java.awt.event.*;
  53 
  54 //Automated tests should run as applet tests if possible because they
  55 // get their environments cleaned up, including AWT threads, any
  56 // test created threads, and any system resources used by the test
  57 // such as file descriptors.  (This is normally not a problem as
  58 // main tests usually run in a separate VM, however on some platforms
  59 // such as the Mac, separate VMs are not possible and non-applet
  60 // tests will cause problems).  Also, you don't have to worry about
  61 // synchronisation stuff in Applet tests they way you do in main
  62 // tests...
  63 
  64 
  65 public class SelectCurrentItemTest extends Applet implements ItemListener,
  66  WindowListener, Runnable
  67 {
  68     //Declare things used in the test, like buttons and labels here
  69     Frame frame;
  70     Choice theChoice;
  71     Robot robot;
  72 
  73     Object lock = new Object();
  74     boolean passed = false;
  75 
  76     public void init()
  77     {
  78         //Create instructions for the user here, as well as set up
  79         // the environment -- set the layout manager, add buttons,
  80         // etc.
  81 
  82         this.setLayout (new BorderLayout ());
  83 
  84         frame = new Frame("SelectCurrentItemTest");
  85         theChoice = new Choice();
  86         for (int i = 0; i < 10; i++) {
  87             theChoice.add(new String("Choice Item " + i));
  88         }
  89         theChoice.addItemListener(this);
  90         frame.add(theChoice);
  91         frame.addWindowListener(this);
  92 
  93         try {
  94             robot = new Robot();
  95             robot.setAutoDelay(500);
  96         }
  97         catch (AWTException e) {
  98             throw new RuntimeException("Unable to create Robot.  Test fails.");
  99         }
 100 
 101     }//End  init()
 102 
 103     public void start ()
 104     {
 105         //Get things going.  Request focus, set size, et cetera
 106         setSize (200,200);
 107         setVisible(true);
 108         validate();
 109 
 110         //What would normally go into main() will probably go here.
 111         //Use System.out.println for diagnostic messages that you want
 112         //to read after the test is done.
 113         //Use System.out.println for messages you want the tester to read.
 114 
 115         frame.setLocation(1,20);
 116         robot.mouseMove(10, 30);
 117         frame.pack();
 118         frame.setVisible(true);
 119         synchronized(lock) {
 120         try {
 121         lock.wait(120000);
 122         }
 123         catch(InterruptedException e) {}
 124         }
 125         robot.waitForIdle();
 126         if (!passed) {
 127             throw new RuntimeException("TEST FAILED!");
 128         }
 129 
 130         // wait to make sure ItemEvent has been processed
 131 
 132 //        try {Thread.sleep(10000);} catch (InterruptedException e){}
 133     }// start()
 134 
 135     public void run() {
 136         try {Thread.sleep(1000);} catch (InterruptedException e){}
 137         // get loc of Choice on screen
 138         Point loc = theChoice.getLocationOnScreen();
 139         // get bounds of Choice
 140         Dimension size = theChoice.getSize();
 141         robot.mouseMove(loc.x + size.width - 10, loc.y + size.height / 2);
 142 
 143         robot.setAutoDelay(250);
 144         robot.mousePress(InputEvent.BUTTON1_MASK);
 145         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 146 
 147         robot.setAutoDelay(1000);
 148         robot.mouseMove(loc.x + size.width / 2, loc.y + size.height + size.height / 2);
 149         robot.setAutoDelay(250);
 150         robot.mousePress(InputEvent.BUTTON1_MASK);
 151         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 152         robot.waitForIdle();
 153         synchronized(lock) {
 154             lock.notify();
 155         }
 156     }
 157 
 158     public void itemStateChanged(ItemEvent e) {
 159         System.out.println("ItemEvent received.  Test passes");
 160         passed = true;
 161     }
 162 
 163     public void windowOpened(WindowEvent e) {
 164         System.out.println("windowActivated()");
 165         Thread testThread = new Thread(this);
 166         testThread.start();
 167     }
 168     public void windowActivated(WindowEvent e) {
 169     }
 170     public void windowDeactivated(WindowEvent e) {}
 171     public void windowClosed(WindowEvent e) {}
 172     public void windowClosing(WindowEvent e) {}
 173     public void windowIconified(WindowEvent e) {}
 174     public void windowDeiconified(WindowEvent e) {}
 175 
 176 }// class SelectCurrentItemTest