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