1 /*
   2  * Copyright (c) 2016, 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 
  25 /*
  26   @test
  27   @bug 8150176
  28   @author a.stepanov
  29   @summary Check if correct resolution variant is used
  30            for JOptionPane dialog icons.
  31   @library ../../../../lib/testlibrary/
  32   @build ExtendedRobot
  33   @run main/manual/othervm -Dsun.java2d.uiScale=2 MultiResolutionJOptionPaneIconTest
  34 */
  35 
  36 
  37 import java.awt.*;
  38 import java.awt.geom.AffineTransform;
  39 import java.awt.image.*;
  40 import javax.swing.*;
  41 
  42 public class MultiResolutionJOptionPaneIconTest extends JFrame {
  43 
  44     private static BufferedImage generateImage(int sz, Color c) {
  45 
  46         BufferedImage img = new BufferedImage(sz, sz, BufferedImage.TYPE_INT_RGB);
  47         Graphics g = img.getGraphics();
  48         g.setColor(c);
  49         g.fillRect(0, 0, sz, sz);
  50         g.setColor(Color.WHITE);
  51         g.fillRect(sz / 3, sz / 3, sz / 3, sz / 3);
  52         return img;
  53     }
  54 
  55     private final JDesktopPane parentPane;
  56 
  57     public MultiResolutionJOptionPaneIconTest() throws Exception {
  58         parentPane = new JDesktopPane();
  59         SwingUtilities.invokeAndWait(this::UI);
  60     }
  61 
  62     private void UI() {
  63 
  64         getContentPane().add(parentPane);
  65         setSize(400, 250);
  66         setLocation(50, 50);
  67         setUndecorated(true);
  68     }
  69 
  70     private void doTest() throws Exception {
  71 
  72         int sz = 16;
  73         BufferedImage img1x = generateImage(sz, Color.RED);
  74         BufferedImage img2x = generateImage(2 * sz, Color.GREEN);
  75         BaseMultiResolutionImage mri = new BaseMultiResolutionImage(
  76             new BufferedImage[]{img1x, img2x});
  77         Icon icon = new ImageIcon(mri);
  78 
  79         String msg = "...";
  80         int opt  = JOptionPane.DEFAULT_OPTION;
  81         int info = JOptionPane.INFORMATION_MESSAGE;
  82         Object v[] = {"one", "two"};
  83 
  84         String instructions[] = {"please check if all the icons",
  85             "for the further dialogs", "has green borders"};
  86 
  87         ExtendedRobot r = new ExtendedRobot();
  88         r.waitForIdle();
  89 
  90         // show instructions
  91         SwingUtilities.invokeAndWait(() -> {
  92             JOptionPane.showMessageDialog(null, instructions,
  93             "TEST INSTRUCTIONS", JOptionPane.WARNING_MESSAGE); });
  94         r.waitForIdle();
  95 
  96         SwingUtilities.invokeAndWait(() -> {
  97             JOptionPane.showConfirmDialog(null, msg, "confirm", opt, info, icon);
  98         });
  99         r.waitForIdle();
 100 
 101         SwingUtilities.invokeAndWait(() -> {
 102             JOptionPane.showInputDialog(null, msg, "input", info, icon, v, v[0]);
 103         });
 104         r.waitForIdle();
 105 
 106         SwingUtilities.invokeAndWait(() -> {
 107             JOptionPane.showMessageDialog(null, msg, "message", info, icon); });
 108         r.waitForIdle();
 109 
 110         SwingUtilities.invokeAndWait(() -> { JOptionPane.showOptionDialog(
 111             null, msg, "option", opt, info, icon, v, v[0]); });
 112         r.waitForIdle();
 113 
 114         SwingUtilities.invokeAndWait(() -> { setVisible(true); });
 115         r.waitForIdle();
 116 
 117         SwingUtilities.invokeAndWait(() -> {
 118             JOptionPane.showInternalConfirmDialog(
 119             parentPane, msg, "internalConfirm", opt, info, icon); });
 120         r.waitForIdle();
 121 
 122         SwingUtilities.invokeAndWait(() -> {
 123         JOptionPane.showInternalInputDialog(
 124             parentPane, msg, "internalInput", info, icon, v, v[0]); });
 125         r.waitForIdle();
 126 
 127         SwingUtilities.invokeAndWait(() -> {
 128         JOptionPane.showInternalMessageDialog(
 129             parentPane, msg, "internalMessage", info, icon); });
 130         r.waitForIdle();
 131 
 132         SwingUtilities.invokeAndWait(() -> {
 133         JOptionPane.showInternalOptionDialog(
 134             parentPane, msg, "internalOption", opt, info, icon, v, v[0]); });
 135         r.waitForIdle();
 136 
 137         SwingUtilities.invokeAndWait(() -> { setVisible(false); });
 138         r.waitForIdle();
 139 
 140         // pass / fail
 141         SwingUtilities.invokeAndWait(() -> {
 142             if (JOptionPane.showConfirmDialog(null,
 143                 "were all the icons green-and-white?",
 144                 "PASS / FAIL", JOptionPane.YES_NO_OPTION) !=
 145                 JOptionPane.YES_OPTION) {
 146                     throw new RuntimeException("test failed");
 147             }
 148         });
 149 
 150         dispose();
 151     }
 152 
 153 
 154     private static boolean is2x() {
 155 
 156         AffineTransform tr = GraphicsEnvironment.getLocalGraphicsEnvironment().
 157             getDefaultScreenDevice().getDefaultConfiguration().
 158             getDefaultTransform();
 159         return (Math.min(tr.getScaleX(), tr.getScaleY()) > 1.001);
 160     }
 161 
 162     public static void main(String[] args) throws Exception {
 163 
 164         // TODO: remove is2x() check after JDK-8150844 fix
 165         if (!is2x()) { return; }
 166 
 167         (new MultiResolutionJOptionPaneIconTest()).doTest();
 168     }
 169 }