< prev index next >

test/java/awt/Robot/HiDPIMouseClick/HiDPIRobotMouseClick.java

Print this page


   1 /*
   2  * Copyright (c) 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.Frame;
  25 import java.awt.Rectangle;
  26 import java.awt.Robot;
  27 import java.awt.event.InputEvent;
  28 import java.awt.event.MouseAdapter;
  29 import java.awt.event.MouseEvent;
  30 import javax.swing.UIManager;



  31 
  32 /* @test
  33  * @bug 8073320
  34  * @summary  Windows HiDPI support
  35  * @author Alexander Scherbatiy
  36  * @requires (os.family == "windows")
  37  * @run main/othervm -Dsun.java2d.win.uiScale=2 HiDPIRobotMouseClick






  38  */
  39 public class HiDPIRobotMouseClick {
  40 
  41     private static volatile int mouseX;
  42     private static volatile int mouseY;


  43 
  44     public static void main(String[] args) throws Exception {
  45 
  46         try {
  47             UIManager.setLookAndFeel(
  48                     "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  49         } catch (Exception e) {
  50             return;
  51         }












  52 
  53         Frame frame = new Frame();
  54         frame.setBounds(30, 20, 400, 300);

  55         frame.setUndecorated(true);
  56 
  57         frame.addMouseListener(new MouseAdapter() {
  58 
  59             @Override
  60             public void mouseClicked(MouseEvent e) {
  61                 mouseX = e.getXOnScreen();
  62                 mouseY = e.getYOnScreen();

  63             }
  64         });
  65 
  66         frame.setVisible(true);
  67 
  68         Robot robot = new Robot();
  69         robot.waitForIdle();
  70         Thread.sleep(200);
  71 
  72         Rectangle rect = frame.getBounds();
  73         rect.setLocation(frame.getLocationOnScreen());
  74 
  75         int x = (int) rect.getCenterX();
  76         int y = (int) rect.getCenterY();
  77 
  78         robot.mouseMove(x, y);
  79         robot.mousePress(InputEvent.BUTTON1_MASK);
  80         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  81         robot.waitForIdle();
  82 
  83         if (x != mouseX || y != mouseY) {


  84             throw new RuntimeException("Wrong mouse click point!");
  85         }
  86     }
  87 }
   1 /*
   2  * Copyright (c) 2015, 2017, 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.Frame;
  25 import java.awt.Rectangle;
  26 import java.awt.Robot;
  27 import java.awt.event.InputEvent;
  28 import java.awt.event.MouseAdapter;
  29 import java.awt.event.MouseEvent;
  30 import java.util.List;
  31 import java.util.concurrent.CountDownLatch;
  32 
  33 import jdk.testlibrary.OSInfo;
  34 
  35 /* @test
  36  * @bug 8073320
  37  * @summary  Windows HiDPI support
  38  * @library ../../../../lib/testlibrary
  39  * @build jdk.testlibrary.OSInfo
  40  * @run main/othervm HiDPIRobotMouseClick
  41  * @run main/othervm -Dsun.java2d.uiScale=1 HiDPIRobotMouseClick
  42  * @run main/othervm -Dsun.java2d.uiScale=2 HiDPIRobotMouseClick
  43  * @run main/othervm -Dsun.java2d.win.uiScaleX=3 -Dsun.java2d.win.uiScaleY=2
  44  *                    HiDPIRobotMouseClick
  45  * @run main/othervm -Dsun.java2d.win.uiScaleX=2 -Dsun.java2d.win.uiScaleY=3
  46  *                    HiDPIRobotMouseClick
  47  */
  48 public class HiDPIRobotMouseClick {
  49 
  50     private static volatile int mouseX;
  51     private static volatile int mouseY;
  52     private static CountDownLatch go;
  53     private static Robot robot;
  54 
  55     public static void main(String[] args) throws Exception {
  56         if (!OSInfo.getOSType().equals(OSInfo.OSType.WINDOWS)
  57                 && System.getProperty("sun.java2d.win.uiScaleX") != null) {



  58             return;
  59         }
  60         robot = new Robot();
  61         robot.setAutoDelay(50);
  62         for (int size : List.of(50, 87, 100, 123, 137, 150)) {
  63             test(size);
  64         }
  65     }
  66 
  67     private static void test(int size) throws Exception {
  68         go = new CountDownLatch(1);
  69         mouseX = -1;
  70         mouseY = -1;
  71         robot.mouseMove(100, 100);
  72 
  73         Frame frame = new Frame();
  74         frame.setSize(size, size + 1);
  75         frame.setLocationRelativeTo(null);
  76         frame.setUndecorated(true);
  77 
  78         frame.addMouseListener(new MouseAdapter() {
  79 
  80             @Override
  81             public void mouseClicked(MouseEvent e) {
  82                 mouseX = e.getXOnScreen();
  83                 mouseY = e.getYOnScreen();
  84                 go.countDown();
  85             }
  86         });
  87 
  88         frame.setVisible(true);
  89 

  90         robot.waitForIdle();
  91         Thread.sleep(200);
  92 
  93         Rectangle rect = frame.getBounds();
  94         rect.setLocation(frame.getLocationOnScreen());
  95 
  96         int x = (int) rect.getCenterX();
  97         int y = (int) rect.getCenterY();
  98 
  99         robot.mouseMove(x, y);
 100         robot.mousePress(InputEvent.BUTTON1_MASK);
 101         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 102         go.await();
 103         frame.dispose();
 104         if (x != mouseX || y != mouseY) {
 105             System.err.println("Expected: x = " + x + ", y = " + y);
 106             System.err.println("Actual: x = " + mouseX + ", y = " + mouseY);
 107             throw new RuntimeException("Wrong mouse click point!");
 108         }
 109     }
 110 }
< prev index next >