|
1 | 1 | /* |
2 | | - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
21 | 21 | * questions. |
22 | 22 | */ |
23 | 23 | import java.awt.Dialog; |
| 24 | +import java.awt.EventQueue; |
24 | 25 | import java.awt.Frame; |
25 | 26 | import java.awt.Graphics; |
26 | 27 | import java.awt.Graphics2D; |
|
29 | 30 | import java.awt.GraphicsEnvironment; |
30 | 31 | import java.awt.Panel; |
31 | 32 | import java.awt.geom.AffineTransform; |
| 33 | +import java.util.concurrent.CountDownLatch; |
| 34 | +import java.util.concurrent.TimeUnit; |
32 | 35 |
|
33 | 36 | /* |
34 | 37 | * @test |
35 | 38 | * @bug 8069361 |
36 | 39 | * @key headful |
37 | 40 | * @summary SunGraphics2D.getDefaultTransform() does not include scale factor |
38 | | - * @author Alexander Scherbatiy |
39 | | - * @run main ScaledTransform |
| 41 | + * @run main/timeout=300 ScaledTransform |
40 | 42 | */ |
41 | 43 | public class ScaledTransform { |
42 | 44 |
|
43 | | - private static volatile boolean passed = false; |
| 45 | + private static volatile CountDownLatch painted; |
| 46 | + private static volatile boolean passed; |
| 47 | + private static volatile Dialog dialog; |
| 48 | + private static volatile long startTime; |
| 49 | + private static volatile long endTime; |
44 | 50 |
|
45 | | - public static void main(String[] args) { |
| 51 | + public static void main(String[] args) throws Exception { |
46 | 52 | GraphicsEnvironment ge = GraphicsEnvironment. |
47 | 53 | getLocalGraphicsEnvironment(); |
48 | 54 |
|
49 | | - if (ge.isHeadlessInstance()) { |
50 | | - return; |
51 | | - } |
52 | | - |
53 | 55 | for (GraphicsDevice gd : ge.getScreenDevices()) { |
54 | | - for (GraphicsConfiguration gc : gd.getConfigurations()) { |
55 | | - testScaleFactor(gc); |
| 56 | + System.out.println("Screen = " + gd); |
| 57 | + test(gd.getDefaultConfiguration()); |
| 58 | + /* Don't want to run too long. Test the default and up to 10 more */ |
| 59 | + GraphicsConfiguration[] configs = gd.getConfigurations(); |
| 60 | + for (int c = 0; c < configs.length && c < 10; c++) { |
| 61 | + test(configs[c]); |
56 | 62 | } |
57 | 63 | } |
58 | 64 | } |
59 | 65 |
|
60 | | - private static void testScaleFactor(final GraphicsConfiguration gc) { |
61 | | - final Dialog dialog = new Dialog((Frame) null, "Test", true, gc); |
62 | | - |
| 66 | + static void test(GraphicsConfiguration gc) throws Exception { |
63 | 67 | try { |
64 | | - dialog.setSize(100, 100); |
65 | | - Panel panel = new Panel() { |
66 | | - |
67 | | - @Override |
68 | | - public void paint(Graphics g) { |
69 | | - if (g instanceof Graphics2D) { |
70 | | - AffineTransform gcTx = gc.getDefaultTransform(); |
71 | | - AffineTransform gTx |
72 | | - = ((Graphics2D) g).getTransform(); |
73 | | - passed = gcTx.getScaleX() == gTx.getScaleX() |
74 | | - && gcTx.getScaleY() == gTx.getScaleY(); |
75 | | - } else { |
76 | | - passed = true; |
77 | | - } |
78 | | - dialog.setVisible(false); |
79 | | - } |
80 | | - }; |
81 | | - dialog.add(panel); |
82 | | - dialog.setVisible(true); |
83 | | - |
| 68 | + /* reset vars for each run */ |
| 69 | + passed = false; |
| 70 | + dialog = null; |
| 71 | + painted = new CountDownLatch(1); |
| 72 | + EventQueue.invokeLater(() -> showDialog(gc)); |
| 73 | + startTime = System.currentTimeMillis(); |
| 74 | + endTime = startTime; |
| 75 | + if (!painted.await(5, TimeUnit.SECONDS)) { |
| 76 | + throw new RuntimeException("Panel is not painted!"); |
| 77 | + } |
| 78 | + System.out.println("Time to paint = " + (endTime - startTime) + "ms."); |
84 | 79 | if (!passed) { |
85 | 80 | throw new RuntimeException("Transform is not scaled!"); |
86 | 81 | } |
87 | 82 | } finally { |
88 | | - dialog.dispose(); |
| 83 | + EventQueue.invokeAndWait(() -> disposeDialog()); |
89 | 84 | } |
90 | 85 | } |
| 86 | + |
| 87 | + private static void showDialog(final GraphicsConfiguration gc) { |
| 88 | + System.out.println("Creating dialog for gc=" + gc + " with tx=" + gc.getDefaultTransform()); |
| 89 | + dialog = new Dialog((Frame) null, "ScaledTransform", true, gc); |
| 90 | + dialog.setSize(300, 100); |
| 91 | + |
| 92 | + Panel panel = new Panel() { |
| 93 | + |
| 94 | + @Override |
| 95 | + public void paint(Graphics g) { |
| 96 | + System.out.println("Painting panel"); |
| 97 | + if (g instanceof Graphics2D g2d) { |
| 98 | + AffineTransform gcTx = gc.getDefaultTransform(); |
| 99 | + AffineTransform gTx = g2d.getTransform(); |
| 100 | + System.out.println("GTX = " + gTx); |
| 101 | + passed = (gcTx.getScaleX() == gTx.getScaleX()) && |
| 102 | + (gcTx.getScaleY() == gTx.getScaleY()); |
| 103 | + } else { |
| 104 | + passed = true; |
| 105 | + } |
| 106 | + endTime = System.currentTimeMillis(); |
| 107 | + painted.countDown(); |
| 108 | + System.out.println("Painted panel"); |
| 109 | + } |
| 110 | + }; |
| 111 | + dialog.add(panel); |
| 112 | + dialog.setVisible(true); |
| 113 | + } |
| 114 | + |
| 115 | + private static void disposeDialog() { |
| 116 | + if (dialog != null) { |
| 117 | + System.out.println("Disposing dialog"); |
| 118 | + dialog.setVisible(false); |
| 119 | + dialog.dispose(); |
| 120 | + } |
| 121 | + } |
91 | 122 | } |
0 commit comments