1 /*
   2  * Copyright (c) 2004, 2014, 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.*;
  25 
  26 /*
  27  * @test
  28  * @summary Testcase to check if the listeners are notified when events occur
  29  *          in the components added on an embedded AWT Frame in an SWT shell.
  30  * @author Girish R (girish.ramachandran@sun.com)
  31  * @library ../../../lib/testlibrary
  32  * @build ExtendedRobot
  33  * @run shell TaskXEmbedListeners.sh
  34  */
  35 
  36 public class TaskXEmbedListeners extends Task<GUIXEmbedListeners> {
  37 
  38     public static void main (String[] args) throws Exception {
  39         new TaskXEmbedListeners(GUIXEmbedListeners.class, new ExtendedRobot()).runTask();
  40         System.exit(0);
  41     }
  42 
  43     public TaskXEmbedListeners(Class guiClass, ExtendedRobot robot) throws Exception {
  44          super(guiClass, robot);
  45     }
  46 
  47     /**
  48      * Do the test. There's a problem in closing the SWT shell. Its not getting
  49      * closed when the close method is called. Currently using setVisible
  50      * (false). Not sure if it'll be gc'd.
  51      * Took the test from Frame test.
  52      */
  53     public void task() throws Exception {
  54         EventQueue.invokeAndWait(() -> {
  55             gui.textfield1.setText("Textfield");
  56             gui.frame.removeAll();
  57             gui.frame.add(gui.button1);
  58             gui.frame.setVisible(true);
  59         });
  60         robot.waitForIdle(1000);
  61 
  62         robot.glide(new Point(100, 100), new Point(200, 105));
  63         robot.waitForIdle(1000);
  64         robot.click();
  65         Point button1Origin;
  66         Point button1Center;
  67         Point scrollBar1Origin;
  68         Point scrollBar1Center;
  69         Point textarea1Origin;
  70         Point textarea1Center;
  71         Point choicePos;
  72         Point choicePoint;
  73 
  74         button1Origin = gui.button1.getLocationOnScreen();
  75         button1Center = gui.button1.getLocationOnScreen();
  76         button1Center.translate(gui.button1.getWidth()/2, gui.button1.getHeight()/2);
  77 
  78         //ActionListener
  79         robot.glide(button1Origin, button1Center);
  80         robot.click();
  81         if (! gui.act_event)
  82             throw new RuntimeException("When Button is clicked, Action Event " +
  83                     "is not generated.\n");
  84 
  85         //AdjustmentListener
  86         EventQueue.invokeAndWait(() -> {
  87             gui.frame.remove(gui.button1);
  88             gui.frame.setLayout(null);
  89             gui.scrollBar.setSize(30, 150);
  90             gui.scrollBar.setLocation(40, 25);
  91             gui.frame.add(gui.scrollBar);
  92         });
  93         robot.waitForIdle(1000);
  94 
  95         scrollBar1Origin = gui.scrollBar.getLocationOnScreen();
  96         scrollBar1Center = gui.scrollBar.getLocationOnScreen();
  97         scrollBar1Center.translate(gui.scrollBar.getWidth()/2, gui.scrollBar.getHeight()/2);
  98 
  99         robot.glide(scrollBar1Origin, scrollBar1Center);
 100         robot.click();
 101         if (! gui.adj_list)
 102             throw new RuntimeException("When Scroll bar is moved, Adjustment " +
 103                     "Event is not generated.\n");
 104 
 105         //ComponentListener
 106         EventQueue.invokeAndWait(() -> {
 107             gui.frame.remove(gui.scrollBar);
 108             gui.frame.repaint();
 109             gui.frame.add(gui.button1);
 110             gui.button1.setVisible(false);
 111         });
 112         robot.waitForIdle(1000);
 113         if (! gui.comp_hidden)
 114             throw new RuntimeException("When Button is Hidden, Component " +
 115                     "hidden Event is not generated.\n");
 116 
 117         EventQueue.invokeAndWait(() -> {
 118             gui.frame.setLayout(null);
 119             gui.button1.setLocation(100, 50);
 120             gui.button1.setVisible(true);
 121         });
 122         robot.waitForIdle(1000);
 123         if (! gui.comp_shown)
 124             throw new RuntimeException("When Button is Shown, Component " +
 125                     "shown Event is not generated.\n");
 126 
 127         EventQueue.invokeAndWait(() -> gui.button1.setLocation(50, 100));
 128         robot.waitForIdle(1000);
 129         if (! gui.comp_moved) {
 130             throw new RuntimeException("When Button is Moved, Component " +
 131                     "Moved Event is not generated.\n");
 132         }
 133 
 134         EventQueue.invokeAndWait(() -> gui.button1.setSize(100, 60));
 135         robot.waitForIdle(1000);
 136         if (! gui.comp_resized)
 137             throw new RuntimeException("When Button is Resized, Component " +
 138                     "Resized Event is not generated.\n");
 139 
 140         //ContainerListener
 141         EventQueue.invokeAndWait(() -> gui.frame.remove(gui.button1));
 142         robot.waitForIdle(1000);
 143         if (! gui.comp_removed)
 144             throw new RuntimeException("When Button is removed, Component " +
 145                     "removed Event is not generated.\n");
 146 
 147         EventQueue.invokeAndWait(() -> {
 148             gui.frame.add(gui.button1);
 149             gui.frame.repaint();
 150         });
 151         robot.waitForIdle(1000);
 152         if (! gui.comp_added)
 153             throw new RuntimeException("When Button is added, Component " +
 154                     "Added Event is not generated.\n");
 155 
 156         //FocusListener
 157         EventQueue.invokeAndWait(() -> {
 158             gui.button2.setSize(100, 60);
 159             gui.button2.setLocation(160, 100);
 160             gui.frame.add(gui.button2);
 161             gui.button1.requestFocus();
 162         });
 163         robot.waitForIdle(1000);
 164         if (! gui.focus_gain)
 165             throw new RuntimeException("When Focus is requested, Focus " +
 166                     "gained Event is not generated.\n");
 167 
 168         EventQueue.invokeAndWait(gui.button1::transferFocus);
 169         robot.waitForIdle(1000);
 170         if (! gui.focus_lost)
 171             throw new RuntimeException("When Focus is lost, Focus lost Event " +
 172                     "is not generated.\n");
 173 
 174         //HierarchyBoundsListener
 175         EventQueue.invokeAndWait(() -> {
 176             gui.frame.remove(gui.button1);
 177             gui.frame.remove(gui.button2);
 178             gui.panel.setLocation(10, 10);
 179             gui.panel.add(gui.button1);
 180             gui.frame.add(gui.panel);
 181             gui.panel.setLocation(50, 50);
 182             gui.panel.setSize(200, 200);
 183         });
 184         robot.waitForIdle(1000);
 185         if (! gui.ancestor_moved)
 186             throw new RuntimeException("When Ancestor is moved, Ancestor " +
 187                     "moved Event is not generated.\n");
 188 
 189         EventQueue.invokeAndWait(() -> gui.panel.setSize(240, 200));
 190         robot.waitForIdle(1000);
 191         if (! gui.ancestor_resized)
 192             throw new RuntimeException("When Ancestor is resized, Ancestor " +
 193                     "resized Event is not generated.\n");
 194 
 195         //HierarchyListener
 196         EventQueue.invokeAndWait(() -> {
 197             gui.frame.remove(gui.panel);
 198             gui.panel1.setSize(200, 200);
 199             gui.panel1.add(gui.button1);
 200             gui.frame.add(gui.panel1);
 201         });
 202         robot.waitForIdle(1000);
 203         if (! gui.hierarchy_changed)
 204             throw new RuntimeException("When Ancestor is Changed, hierarchy " +
 205                     "changed Event is not generated.\n");
 206 
 207         EventQueue.invokeAndWait(() -> gui.frame.remove(gui.panel1));
 208         robot.waitForIdle(1000);
 209 
 210         //ItemListener
 211         EventQueue.invokeAndWait(() -> {
 212             gui.frame.setLayout(new FlowLayout());
 213             gui.frame.add(gui.colorchoice);
 214             gui.colorchoice.setLocation(50, 50);
 215             gui.frame.setVisible(true);
 216         });
 217         robot.waitForIdle(1000);
 218 
 219         Point choiceOrigin = gui.colorchoice.getLocationOnScreen();
 220         choicePos = gui.colorchoice.getLocationOnScreen();
 221         choicePoint=new Point(choicePos.x+55, choicePos.y+8);
 222         robot.glide(choiceOrigin, choicePoint);
 223         robot.waitForIdle(1000);
 224         robot.click();
 225         robot.mouseMove(choicePoint);
 226         robot.waitForIdle(1000);
 227         choicePoint = new Point(choicePos.x+50,choicePos.y+80);
 228         robot.mouseMove(choicePoint);
 229         robot.waitForIdle(1000);
 230         robot.click();
 231 
 232         if (gui.item_changed == false)
 233             throw new RuntimeException("When Item is changed, Item Method " +
 234                     "Event is not generated.\n");
 235 
 236         EventQueue.invokeAndWait(() -> {
 237             gui.frame.remove(gui.colorchoice);
 238 
 239             //KeyListener
 240             gui.frame.add(gui.textfield1);
 241         });
 242         robot.waitForIdle(1000);
 243 
 244         textarea1Origin = gui.textfield1.getLocationOnScreen();
 245         textarea1Center = gui.textfield1.getLocationOnScreen();
 246         textarea1Center.translate(gui.textfield1.getWidth()/2, gui.textfield1.getHeight()/2);
 247         robot.glide(textarea1Origin, textarea1Center);
 248         robot.waitForIdle(1000);
 249         robot.click();
 250         robot.type('N');
 251         if (gui.key_pressed == false)
 252             throw new RuntimeException("When Key is pressed, Key Pressed " +
 253                     "Event is not generated.\n");
 254 
 255         if (gui.key_released == false)
 256             throw new RuntimeException("When Key is released, Key Released " +
 257                     "Event is not generated.\n");
 258 
 259         if (gui.key_typed == false)
 260             throw new RuntimeException("When Key is typed, Key Typed Event " +
 261                     "is not generated.\n");
 262 
 263         //MouseListener
 264         EventQueue.invokeAndWait(() -> {
 265             gui.frame.remove(gui.textfield1);
 266             gui.frame.add(gui.button1);
 267             gui.button1.setLocation(100, 50);
 268         });
 269         robot.waitForIdle(1000);
 270 
 271         button1Origin = gui.button1.getLocationOnScreen();
 272         button1Center = gui.button1.getLocationOnScreen();
 273         button1Center.translate(gui.button1.getWidth()/2, gui.button1.getHeight()/2);
 274         robot.glide(button1Origin, button1Center);
 275         robot.waitForIdle(1000);
 276         if (gui.mouse_entered == false)
 277             throw new RuntimeException("When Mouse is entered, mouse entered " +
 278                     "Event is not generated.\n");
 279 
 280         robot.click();
 281         if (gui.mouse_pressed == false)
 282             throw new RuntimeException("When Mouse is pressed, mouse pressed " +
 283                     "Event is not generated.\n");
 284 
 285         if (gui.mouse_released == false)
 286             throw new RuntimeException("When Mouse is released, mouse released " +
 287                     "Event is not generated.\n");
 288 
 289         if (gui.mouse_clicked == false)
 290             throw new RuntimeException("When Mouse is clicked, mouse clicked " +
 291                     "Event is not generated.\n");
 292 
 293         robot.mouseMove(150, 150);
 294         robot.waitForIdle(1000);
 295         if (gui.mouse_exited == false)
 296             throw new RuntimeException("When Mouse is exited, mouse exited " +
 297                     "Event is not generated.\n");
 298 
 299         EventQueue.invokeAndWait(() -> {
 300             gui.frame.remove(gui.button1);
 301 
 302             //MouseMotionListener
 303             gui.frame.add(gui.button1);
 304         });
 305         robot.waitForIdle(1000);
 306 
 307         button1Origin = gui.button1.getLocationOnScreen();
 308         button1Center = gui.button1.getLocationOnScreen();
 309         button1Center.translate(gui.button1.getWidth()/2, gui.button1.getHeight()/2);
 310         robot.glide(button1Origin, button1Center);
 311         robot.waitForIdle(1000);
 312         robot.dragAndDrop(button1Center, button1Origin);
 313         if (gui.mouse_dragged == false)
 314             throw new RuntimeException("When Mouse is dragged, mouse dragged " +
 315                     "Event is not generated.\n");
 316 
 317         robot.mouseMove(button1Origin);
 318         robot.waitForIdle(1000);
 319         if (gui.mouse_moved == false)
 320             throw new RuntimeException("When Mouse is moved, mouse moved " +
 321                     "Event is not generated.\n");
 322 
 323         EventQueue.invokeAndWait(() -> {
 324             gui.frame.remove(gui.button1);
 325 
 326             //TextListener
 327             gui.frame.add(gui.textfield1);
 328         });
 329         robot.waitForIdle(1000);
 330 
 331         robot.mouseMove(gui.textfield1.getLocationOnScreen());
 332         robot.waitForIdle(1000);
 333         robot.click();
 334         robot.type('A');
 335         if (gui.textvalue_changed == false)
 336             throw new RuntimeException("When Text value is changed, TextEvent " +
 337                     "is not generated.\n");
 338 
 339         EventQueue.invokeAndWait(() -> {
 340             gui.frame.remove(gui.textfield1);
 341         });
 342         robot.waitForIdle(1000);
 343 
 344         gui.destroyProcess();
 345     }
 346 }