1 /*
   2  * Copyright (c) 2016, 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 import java.awt.Font;
  24 import java.awt.Graphics;
  25 import java.util.concurrent.CountDownLatch;
  26 import java.util.concurrent.TimeUnit;
  27 import javax.swing.JFrame;
  28 import javax.swing.JLabel;
  29 import javax.swing.SwingUtilities;
  30 
  31 /**
  32  * @test
  33  * @key headful
  34  * @bug 8142966
  35  * @summary Wrong cursor position in text components on HiDPI display
  36  * @run main/othervm -Dsun.java2d.uiScale=2 SwingFontMetricsTest
  37  */
  38 public class SwingFontMetricsTest {
  39 
  40     private static final String LOWER_CASE_TEXT = "the quick brown fox jumps over the lazy dog";
  41     private static final String UPPER_CASE_TEXT = LOWER_CASE_TEXT.toUpperCase();
  42     private static final String TEXT = LOWER_CASE_TEXT + UPPER_CASE_TEXT;
  43     private static boolean passed = false;
  44     private static CountDownLatch latch = new CountDownLatch(1);
  45 
  46     public static void main(String[] args) throws Exception {
  47         SwingUtilities.invokeAndWait(SwingFontMetricsTest::createAndShowGUI);
  48         latch.await(5, TimeUnit.SECONDS);
  49 
  50         if (!passed) {
  51             throw new RuntimeException("Test Failed!");
  52         }
  53     }
  54 
  55     private static void createAndShowGUI() {
  56         final JFrame frame = new JFrame();
  57         frame.setSize(300, 300);
  58         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  59         JLabel label = new JLabel(TEXT) {
  60             @Override
  61             public void paint(Graphics g) {
  62                 super.paint(g);
  63                 Font font = getFont();
  64                 int width1 = getFontMetrics(font).stringWidth(TEXT);
  65                 int width2 = g.getFontMetrics(font).stringWidth(TEXT);
  66                 passed = (width1 == width2);
  67                 latch.countDown();
  68                 frame.dispose();
  69             }
  70         };
  71         frame.add(label);
  72         frame.setVisible(true);
  73     }
  74 }