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(new GUIXEmbedListeners(), new ExtendedRobot()).runTask();
  40         System.exit(0);
  41     }
  42 
  43     public TaskXEmbedListeners(GUIXEmbedListeners gui, ExtendedRobot robot) {
  44          super(gui, 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         gui.textfield1.setText("Textfield");
  55         gui.frame.removeAll();
  56         gui.frame.add(gui.button1);
  57         gui.frame.setVisible(true);
  58         robot.waitForIdle(1000);
  59 
  60         robot.glide(new Point(100, 100), new Point(200, 105));
  61         robot.waitForIdle(1000);
  62         robot.click();
  63         Point button1Origin;
  64         Point button1Center;
  65         Point scrollBar1Origin;
  66         Point scrollBar1Center;
  67         Point textarea1Origin;
  68         Point textarea1Center;
  69         Point choicePos;
  70         Point choicePoint;
  71 
  72         button1Origin = gui.button1.getLocationOnScreen();
  73         button1Center = gui.button1.getLocationOnScreen();
  74         button1Center.translate(gui.button1.getWidth()/2, gui.button1.getHeight()/2);
  75 
  76         //ActionListener
  77         robot.glide(button1Origin, button1Center);
  78         robot.click();
  79         if (! gui.act_event)
  80             throw new RuntimeException("When Button is clicked, Action Event " +
  81                     "is not generated.\n");
  82 
  83         //AdjustmentListener
  84         gui.frame.remove(gui.button1);
  85         gui.frame.setLayout(null);
  86         gui.scrollBar.setSize(30, 150);
  87         gui.scrollBar.setLocation(40, 25);
  88         gui.frame.add(gui.scrollBar);
  89         robot.waitForIdle(1000);
  90 
  91         scrollBar1Origin = gui.scrollBar.getLocationOnScreen();
  92         scrollBar1Center = gui.scrollBar.getLocationOnScreen();
  93         scrollBar1Center.translate(gui.scrollBar.getWidth()/2, gui.scrollBar.getHeight()/2);
  94 
  95         robot.glide(scrollBar1Origin, scrollBar1Center);
  96         robot.click();
  97         if (! gui.adj_list)
  98             throw new RuntimeException("When Scroll bar is moved, Adjustment " +
  99                     "Event is not generated.\n");
 100 
 101         //ComponentListener
 102         gui.frame.remove(gui.scrollBar);
 103         gui.frame.repaint();
 104         gui.frame.add(gui.button1);
 105         gui.button1.setVisible(false);
 106         robot.waitForIdle(1000);
 107         if (! gui.comp_hidden)
 108             throw new RuntimeException("When Button is Hidden, Component " +
 109                     "hidden Event is not generated.\n");
 110 
 111         gui.frame.setLayout(null);
 112         gui.button1.setLocation(100, 50);
 113         gui.button1.setVisible(true);
 114         robot.waitForIdle(1000);
 115         if (! gui.comp_shown)
 116             throw new RuntimeException("When Button is Shown, Component " +
 117                     "shown Event is not generated.\n");
 118 
 119         gui.button1.setLocation(50, 100);
 120         robot.waitForIdle(1000);
 121         if (! gui.comp_moved) {
 122             throw new RuntimeException("When Button is Moved, Component " +
 123                     "Moved Event is not generated.\n");
 124         }
 125 
 126         gui.button1.setSize(100, 60);
 127         robot.waitForIdle(1000);
 128         if (! gui.comp_resized)
 129             throw new RuntimeException("When Button is Resized, Component " +
 130                     "Resized Event is not generated.\n");
 131 
 132         //ContainerListener
 133         gui.frame.remove(gui.button1);
 134         robot.waitForIdle(1000);
 135         if (! gui.comp_removed)
 136             throw new RuntimeException("When Button is removed, Component " +
 137                     "removed Event is not generated.\n");
 138 
 139         gui.frame.add(gui.button1);
 140         gui.frame.repaint();
 141         robot.waitForIdle(1000);
 142         if (! gui.comp_added)
 143             throw new RuntimeException("When Button is added, Component " +
 144                     "Added Event is not generated.\n");
 145 
 146         //FocusListener
 147         gui.button2.setSize(100, 60);
 148         gui.button2.setLocation(160, 100);
 149         gui.frame.add(gui.button2);
 150         gui.button1.requestFocus();
 151         robot.waitForIdle(1000);
 152         if (! gui.focus_gain)
 153             throw new RuntimeException("When Focus is requested, Focus " +
 154                     "gained Event is not generated.\n");
 155 
 156         gui.button1.transferFocus();
 157         robot.waitForIdle(1000);
 158         if (! gui.focus_lost)
 159             throw new RuntimeException("When Focus is lost, Focus lost Event " +
 160                     "is not generated.\n");
 161 
 162         //HierarchyBoundsListener
 163         gui.frame.remove(gui.button1);
 164         gui.frame.remove(gui.button2);
 165         gui.panel.setLocation(10, 10);
 166         gui.panel.add(gui.button1);
 167         gui.frame.add(gui.panel);
 168         gui.panel.setLocation(50, 50);
 169         gui.panel.setSize(200, 200);
 170         robot.waitForIdle(1000);
 171         if (! gui.ancestor_moved)
 172             throw new RuntimeException("When Ancestor is moved, Ancestor " +
 173                     "moved Event is not generated.\n");
 174 
 175         gui.panel.setSize(240, 200);
 176         robot.waitForIdle(1000);
 177         if (! gui.ancestor_resized)
 178             throw new RuntimeException("When Ancestor is resized, Ancestor " +
 179                     "resized Event is not generated.\n");
 180 
 181         //HierarchyListener
 182         gui.frame.remove(gui.panel);
 183         gui.panel1.setSize(200, 200);
 184         gui.panel1.add(gui.button1);
 185         gui.frame.add(gui.panel1);
 186         robot.waitForIdle(1000);
 187         if (! gui.hierarchy_changed)
 188             throw new RuntimeException("When Ancestor is Changed, hierarchy " +
 189                     "changed Event is not generated.\n");
 190 
 191         gui.frame.remove(gui.panel1);
 192         robot.waitForIdle(1000);
 193 
 194         //ItemListener
 195         gui.frame.setLayout(new FlowLayout());
 196         gui.frame.add(gui.colorchoice);
 197         gui.colorchoice.setLocation(50, 50);
 198         gui.frame.setVisible(true);
 199         robot.waitForIdle(1000);
 200 
 201         Point choiceOrigin = gui.colorchoice.getLocationOnScreen();
 202         choicePos = gui.colorchoice.getLocationOnScreen();
 203         choicePoint=new Point(choicePos.x+55, choicePos.y+8);
 204         robot.glide(choiceOrigin, choicePoint);
 205         robot.waitForIdle(1000);
 206         robot.click();
 207         robot.mouseMove(choicePoint);
 208         robot.waitForIdle(1000);
 209         choicePoint = new Point(choicePos.x+50,choicePos.y+80);
 210         robot.mouseMove(choicePoint);
 211         robot.waitForIdle(1000);
 212         robot.click();
 213 
 214         if (gui.item_changed == false)
 215             throw new RuntimeException("When Item is changed, Item Method " +
 216                     "Event is not generated.\n");
 217 
 218         gui.frame.remove(gui.colorchoice);
 219 
 220         //KeyListener
 221         gui.frame.add(gui.textfield1);
 222         robot.waitForIdle(1000);
 223 
 224         textarea1Origin = gui.textfield1.getLocationOnScreen();
 225         textarea1Center = gui.textfield1.getLocationOnScreen();
 226         textarea1Center.translate(gui.textfield1.getWidth()/2, gui.textfield1.getHeight()/2);
 227         robot.glide(textarea1Origin, textarea1Center);
 228         robot.waitForIdle(1000);
 229         robot.click();
 230         robot.type('N');
 231         if (gui.key_pressed == false)
 232             throw new RuntimeException("When Key is pressed, Key Pressed " +
 233                     "Event is not generated.\n");
 234 
 235         if (gui.key_released == false)
 236             throw new RuntimeException("When Key is released, Key Released " +
 237                     "Event is not generated.\n");
 238 
 239         if (gui.key_typed == false)
 240             throw new RuntimeException("When Key is typed, Key Typed Event " +
 241                     "is not generated.\n");
 242 
 243         //MouseListener
 244         gui.frame.remove(gui.textfield1);
 245         gui.frame.add(gui.button1);
 246         gui.button1.setLocation(100, 50);
 247         robot.waitForIdle(1000);
 248 
 249         button1Origin = gui.button1.getLocationOnScreen();
 250         button1Center = gui.button1.getLocationOnScreen();
 251         button1Center.translate(gui.button1.getWidth()/2, gui.button1.getHeight()/2);
 252         robot.glide(button1Origin, button1Center);
 253         robot.waitForIdle(1000);
 254         if (gui.mouse_entered == false)
 255             throw new RuntimeException("When Mouse is entered, mouse entered " +
 256                     "Event is not generated.\n");
 257 
 258         robot.click();
 259         if (gui.mouse_pressed == false)
 260             throw new RuntimeException("When Mouse is pressed, mouse pressed " +
 261                     "Event is not generated.\n");
 262 
 263         if (gui.mouse_released == false)
 264             throw new RuntimeException("When Mouse is released, mouse released " +
 265                     "Event is not generated.\n");
 266 
 267         if (gui.mouse_clicked == false)
 268             throw new RuntimeException("When Mouse is clicked, mouse clicked " +
 269                     "Event is not generated.\n");
 270 
 271         robot.mouseMove(150, 150);
 272         robot.waitForIdle(1000);
 273         if (gui.mouse_exited == false)
 274             throw new RuntimeException("When Mouse is exited, mouse exited " +
 275                     "Event is not generated.\n");
 276 
 277         gui.frame.remove(gui.button1);
 278 
 279         //MouseMotionListener
 280         gui.frame.add(gui.button1);
 281         robot.waitForIdle(1000);
 282 
 283         button1Origin = gui.button1.getLocationOnScreen();
 284         button1Center = gui.button1.getLocationOnScreen();
 285         button1Center.translate(gui.button1.getWidth()/2, gui.button1.getHeight()/2);
 286         robot.glide(button1Origin, button1Center);
 287         robot.waitForIdle(1000);
 288         robot.dragAndDrop(button1Center, button1Origin);
 289         if (gui.mouse_dragged == false)
 290             throw new RuntimeException("When Mouse is dragged, mouse dragged " +
 291                     "Event is not generated.\n");
 292 
 293         robot.mouseMove(button1Origin);
 294         robot.waitForIdle(1000);
 295         if (gui.mouse_moved == false)
 296             throw new RuntimeException("When Mouse is moved, mouse moved " +
 297                     "Event is not generated.\n");
 298 
 299         gui.frame.remove(gui.button1);
 300 
 301         //TextListener
 302         gui.frame.add(gui.textfield1);
 303         robot.waitForIdle(1000);
 304 
 305         robot.mouseMove(gui.textfield1.getLocationOnScreen());
 306         robot.waitForIdle(1000);
 307         robot.click();
 308         robot.type('A');
 309         if (gui.textvalue_changed == false)
 310             throw new RuntimeException("When Text value is changed, TextEvent " +
 311                     "is not generated.\n");
 312 
 313         gui.frame.remove(gui.textfield1);
 314         robot.waitForIdle(1000);
 315 
 316         gui.destroyProcess();
 317     }
 318 }