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  * @test
  26  * @key headful
  27  * @bug 8149371
  28  * @requires (os.family == "linux" | os.family == "mac")
  29  * @summary multi-res. image: -Dsun.java2d.uiScale does not work for Window
  30  * icons (some ambiguity for Window.setIconImages()?)
  31  * @run main/othervm/manual -Dsun.java2d.uiScale=2 MultiResIconTest
  32  */
  33 import java.awt.Color;
  34 import java.awt.EventQueue;
  35 import java.awt.Graphics;
  36 import java.awt.GridBagConstraints;
  37 import java.awt.GridBagLayout;
  38 import java.awt.event.ActionEvent;
  39 import java.awt.event.ActionListener;
  40 import java.awt.event.WindowAdapter;
  41 import java.awt.event.WindowEvent;
  42 import java.awt.image.BaseMultiResolutionImage;
  43 import java.awt.image.BufferedImage;
  44 import java.util.concurrent.CountDownLatch;
  45 import javax.swing.JButton;
  46 import javax.swing.JDialog;
  47 import javax.swing.JFrame;
  48 import javax.swing.JLabel;
  49 import javax.swing.JPanel;
  50 import javax.swing.SwingUtilities;
  51 
  52 public class MultiResIconTest {
  53 
  54     private static GridBagLayout layout;
  55     private static JPanel mainControlPanel;
  56     private static JPanel resultButtonPanel;
  57     private static JLabel instructionText;
  58     private static JButton passButton;
  59     private static JButton failButton;
  60     private static JDialog f;
  61     private static CountDownLatch latch;
  62     private static TestFrame frame;
  63     private static boolean testPassed = true;
  64     
  65     private static BufferedImage generateImage(int x, Color c) {
  66 
  67         BufferedImage img = new BufferedImage(x, x, BufferedImage.TYPE_INT_RGB);
  68         Graphics g = img.getGraphics();
  69         g.setColor(c);
  70         g.fillRect(0, 0, x, x);
  71         g.setColor(Color.WHITE);
  72         g.fillRect(x / 3, x / 3, x / 3, x / 3);
  73         return img;
  74     }
  75 
  76     private static void createUI() throws Exception {
  77         SwingUtilities.invokeAndWait(() -> {
  78             frame = new TestFrame();
  79             f = new JDialog(frame);
  80             f.setTitle("Instruction Dialog");
  81             layout = new GridBagLayout();
  82             mainControlPanel = new JPanel(layout);
  83             resultButtonPanel = new JPanel(layout);
  84             GridBagConstraints gbc = new GridBagConstraints();
  85             String instructions
  86                     = "<html>    INSTRUCTIONS:<br><br>"
  87                     + "1) Test frame title icon and frame color should be green."
  88                     + "<br>"
  89                     + "2) Test frame task bar icon should be blue<br>"
  90                     + "3) If color are same as mentioned in 1 and 2 press pass<br>"
  91                     + "   else press fail.<br><br></html>";
  92 
  93             instructionText = new JLabel();
  94             instructionText.setText(instructions);
  95 
  96             gbc.gridx = 0;
  97             gbc.gridy = 0;
  98             gbc.fill = GridBagConstraints.HORIZONTAL;
  99             mainControlPanel.add(instructionText, gbc);
 100             passButton = new JButton("Pass");
 101             passButton.setActionCommand("Pass");
 102             passButton.addActionListener((ActionEvent e) -> {
 103                 latch.countDown();
 104                 f.dispose();
 105                 frame.dispose();
 106             });
 107             failButton = new JButton("Fail");
 108             failButton.setActionCommand("Fail");
 109             failButton.addActionListener(new ActionListener() {
 110                 @Override
 111                 public void actionPerformed(ActionEvent e) {
 112                     testPassed = false;
 113                     latch.countDown();
 114                     f.dispose();
 115                     frame.dispose();
 116                 }
 117             });
 118             gbc.gridx = 1;
 119             gbc.gridy = 0;
 120             resultButtonPanel.add(passButton, gbc);
 121             gbc.gridx = 2;
 122             gbc.gridy = 0;
 123             resultButtonPanel.add(failButton, gbc);
 124 
 125             gbc.gridx = 0;
 126             gbc.gridy = 1;
 127             mainControlPanel.add(resultButtonPanel, gbc);
 128 
 129             f.add(mainControlPanel);
 130             f.setSize(400, 200);
 131             f.setLocationRelativeTo(null);
 132             f.setVisible(true);
 133 
 134             f.addWindowListener(new WindowAdapter() {
 135                 @Override
 136                 public void windowClosing(WindowEvent e) {
 137                     latch.countDown();
 138                     f.dispose();
 139                     frame.dispose();
 140                 }
 141             });
 142         });
 143     }
 144 
 145     private static class TestFrame extends JFrame {
 146 
 147         private static final int W = 200;
 148 
 149         private static final BaseMultiResolutionImage IMG
 150                 = new BaseMultiResolutionImage(
 151                         new BufferedImage[]{generateImage(W, Color.RED),
 152                             generateImage(2 * W, Color.GREEN),
 153                             generateImage(4 * W, Color.BLUE)});
 154 
 155         private static final BaseMultiResolutionImage ICON
 156                 = new BaseMultiResolutionImage(
 157                         new BufferedImage[]{generateImage(16, Color.RED),
 158                             generateImage(32, Color.GREEN),
 159                             generateImage(64, Color.BLUE),
 160                             generateImage(128, Color.BLACK),
 161                             generateImage(256, Color.GRAY)});
 162 
 163         public TestFrame() {
 164             createUI();
 165         }
 166 
 167         private void createUI() {
 168             setTitle("Test Frame");
 169             setIconImage(ICON);
 170             addWindowListener(new WindowAdapter() {
 171                 @Override
 172                 public void windowClosing(WindowEvent e) {
 173                     dispose();
 174                 }
 175             });
 176             setSize(W, W);
 177             setLocation(50, 50);
 178             setResizable(false);
 179             setVisible(true);
 180         }
 181 
 182         @Override
 183         public void paint(Graphics gr) {
 184             gr.drawImage(IMG, 0, 0, this);
 185         }
 186     }
 187 
 188     public static void main(String[] args) throws Exception {
 189         latch = new CountDownLatch(1);
 190         new MultiResIconTest().createUI();
 191         latch.await();
 192         if (!testPassed) {
 193             throw new RuntimeException("Test Failed");
 194         }
 195     }
 196 }