< prev index next >

java/awt/Component/GetScreenLocTest/GetScreenLocTest.java

Print this page


   1 /*
   2   test
   3   @bug 4356202
   4   @summary Tests that getLocationOnScreen returns valid value(WindowMaker only).
   5   @author dom@sparc.spb.su: area=
   6   @run applet GetScreenLocTest.html
   7 */
   8 
   9 // Note there is no @ in front of test above.  This is so that the
  10 //  harness will not mistake this file as a test file.  It should
  11 //  only see the html file as a test file. (the harness runs all
  12 //  valid test files, so it would run this test twice if this file
  13 //  were valid as well as the html file.)
  14 // Also, note the area= after Your Name in the author tag.  Here, you
  15 //  should put which functional area the test falls in.  See the
  16 //  AWT-core home page -> test areas and/or -> AWT team  for a list of
  17 //  areas.
  18 // Note also the 'GetScreenLocTest.html' in the run tag.  This should
  19 //  be changed to the name of the test.
  20 
  21 
  22 /**
  23  * GetScreenLocTest.java
  24  *
  25  * summary:


  26  */
  27 
  28 import java.applet.Applet;
  29 import java.awt.*;
  30 import java.awt.event.*;
  31 
  32 //Automated tests should run as applet tests if possible because they
  33 // get their environments cleaned up, including AWT threads, any
  34 // test created threads, and any system resources used by the test
  35 // such as file descriptors.  (This is normally not a problem as
  36 // main tests usually run in a separate VM, however on some platforms
  37 // such as the Mac, separate VMs are not possible and non-applet
  38 // tests will cause problems).  Also, you don't have to worry about
  39 // synchronisation stuff in Applet tests they way you do in main
  40 // tests...

  41 
  42 
  43 public class GetScreenLocTest extends Applet
  44 {





  45     //Declare things used in the test, like buttons and labels here
  46     Robot robot = null;
  47     private class MyCanvas extends Canvas {
  48         public Dimension getPreferredSize() {
  49             return new Dimension(100, 100);
  50         }
  51         public void paint(Graphics g) {
  52             super.paint(g);
  53             g.setColor(Color.blue);
  54             Rectangle r = getBounds();
  55             g.fillRect(0, 0, r.width, r.height);
  56         }
  57     }
  58     Canvas c = null;
  59     int state = 0; // there are three states - (-1,-1),(0,0),(1,1)
  60     Frame f = null;
  61     Frame bigOne = null;
  62     void bigPause() {
  63         Toolkit.getDefaultToolkit().sync();
  64         robot.waitForIdle();
  65         robot.delay(1000);
  66     }
  67     public void init()
  68     {
  69         //Create instructions for the user here, as well as set up
  70         // the environment -- set the layout manager, add buttons,
  71         // etc.
  72 
  73         this.setLayout (new BorderLayout ());




  74 
  75         try {
  76             robot = new Robot();
  77         } catch(AWTException e) {
  78             throw new RuntimeException(e.getMessage());
  79         }
  80         bigOne = new Frame();
  81         bigOne.setBounds(10, 10, 140, 170);
  82         bigOne.setVisible(true);
  83         f = new Frame();
  84         f.setLayout(new BorderLayout());
  85         f.setBounds(20, 20, 120, 150);
  86         f.add(c = new MyCanvas(), BorderLayout.CENTER);


  87         c.addMouseListener(new MouseAdapter() {
  88                 public void mousePressed(MouseEvent e) {
  89                     switch(state) {
  90                       case 0: // the first event should be (0,0)
  91                           if (e.getX() != 0 || e.getY() != 0) {
  92                               throw new RuntimeException("wrong location " + e);
  93                           }
  94                           state++;
  95                           break;
  96                       case 1: // the second event should be (1,1)
  97                           if (e.getX() != 1 || e.getY() != 1) {
  98                               throw new RuntimeException("wrong location " +e);
  99                           }
 100                           state++;
 101                           break;
 102                       case 2: // this should never happen
 103                           throw new RuntimeException("wrong location " + e);
 104                     }
 105                 }
 106             });
 107         f.pack();
 108         f.setVisible(true);
 109         bigPause();
 110 
 111     }//End  init()
 112     void doPress(Point p) {
 113         robot.mouseMove(p.x, p.y);
 114         robot.mousePress(InputEvent.BUTTON1_MASK);
 115         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 116     }
 117     public void start ()
 118     {
 119         //Get things going.  Request focus, set size, et cetera
 120         setSize (200,200);
 121         show();
 122         Point p = c.getLocationOnScreen();
 123         doPress(p);
 124         p.x += 1;
 125         p.y += 1;
 126         doPress(p);
 127         p.x -= 2;
 128         p.y -= 2;
 129         doPress(p);
 130 
 131         bigPause();




 132         // ...and at the end the state should be 2
 133         if (state != 2) {
 134             throw new RuntimeException("wrong state");
 135         }
 136     }// start()
 137 
 138 }// class GetScreenLocTest
   1 /*
   2  * Copyright (c) 2002, 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.AWTException;
  25 import java.awt.BorderLayout;
  26 import java.awt.Canvas;
  27 import java.awt.Color;
  28 import java.awt.Dimension;
  29 import java.awt.Frame;
  30 import java.awt.Graphics;
  31 import java.awt.Point;
  32 import java.awt.Rectangle;
  33 import java.awt.Robot;
  34 import java.awt.Toolkit;
  35 import java.awt.event.InputEvent;
  36 import java.awt.event.MouseAdapter;
  37 import java.awt.event.MouseEvent;
  38 
  39 /**
  40  * @test
  41  * @bug 4356202
  42  * @summary Tests that getLocationOnScreen returns valid value(WindowMaker
  43  *          only).
  44  * @author dom@sparc.spb.su: area=
  45  */
  46 public class GetScreenLocTest {
  47     //Declare things used in the test, like buttons and labels here
  48     static Robot robot = null;
  49     private static class MyCanvas extends Canvas {
  50         public Dimension getPreferredSize() {
  51             return new Dimension(100, 100);
  52         }
  53         public void paint(Graphics g) {
  54             super.paint(g);
  55             g.setColor(Color.blue);
  56             Rectangle r = getBounds();
  57             g.fillRect(0, 0, r.width, r.height);
  58         }
  59     }
  60     static int state = 0; // there are three states - (-1,-1),(0,0),(1,1)
  61 
  62     static void bigPause() {


  63         Toolkit.getDefaultToolkit().sync();
  64         robot.waitForIdle();
  65         robot.delay(1000);
  66     }





  67 
  68     static void doPress(Point p) {
  69         robot.mouseMove(p.x, p.y);
  70         robot.mousePress(InputEvent.BUTTON1_MASK);
  71         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  72     }
  73 
  74     public static void main(final String[] args) throws AWTException {
  75         robot = new Robot();
  76         Frame bigOne = new Frame();
  77         bigOne.setSize(200, 200);
  78         bigOne.setLocationRelativeTo(null);


  79         bigOne.setVisible(true);
  80         Frame f = new Frame();
  81         f.setLayout(new BorderLayout());
  82         f.setSize(120, 150);
  83         f.setLocationRelativeTo(null);
  84         Canvas c = new MyCanvas();
  85         f.add(c, BorderLayout.CENTER);
  86         c.addMouseListener(new MouseAdapter() {
  87             public void mousePressed(MouseEvent e) {
  88                 switch(state) {
  89                     case 0: // the first event should be (0,0)
  90                         if (e.getX() != 0 || e.getY() != 0) {
  91                             throw new RuntimeException("wrong location " + e);
  92                         }
  93                         state++;
  94                         break;
  95                     case 1: // the second event should be (1,1)
  96                         if (e.getX() != 1 || e.getY() != 1) {
  97                             throw new RuntimeException("wrong location " +e);
  98                         }
  99                         state++;
 100                         break;
 101                     case 2: // this should never happen
 102                         throw new RuntimeException("wrong location " + e);
 103                 }
 104             }
 105         });
 106         f.pack();
 107         f.setVisible(true);
 108         bigPause();
 109 











 110         Point p = c.getLocationOnScreen();
 111         doPress(p);
 112         p.x += 1;
 113         p.y += 1;
 114         doPress(p);
 115         p.x -= 2;
 116         p.y -= 2;
 117         doPress(p);

 118         bigPause();
 119 
 120         f.dispose();
 121         bigOne.dispose();
 122 
 123         // ...and at the end the state should be 2
 124         if (state != 2) {
 125             throw new RuntimeException("wrong state: " + state);
 126         }
 127     }
 128 }

< prev index next >