File: IconShowingTest.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (280 lines) | stat: -rw-r--r-- 11,224 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Window;
import java.awt.image.BaseMultiResolutionImage;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
import static jdk.test.lib.Platform.isWindows;

/*
 * @test
 * @bug 6233560 6280303 6292933
 * @summary Tests if toplevel's icons are shown correctly
 * @key headful
 * @requires (os.family == "windows")
 * @library /java/awt/regtesthelpers /test/lib
 * @build PassFailJFrame jdk.test.lib.Platform
 * @run main/manual IconShowingTest
 */

public class IconShowingTest {
    private static final int EXTRA_OFFSET = 50;

    private static final String INSTRUCTIONS =
            "Look at the icons shown on frames and dialogs, icons of minimized frames\n"
            + (isWindows() ? "are displayed in ALT+TAB window\n" : "") + "\n"+
            """
            Alpha-channel (transparency) should be supported
            by Windows and may not be supported by other platforms.

            Notes:
              * Icons might appear in grayscale.
              * Default icon might be either Duke or Java Cup.

            Press PASS if the icons match label description in windows
            and are shown correctly, FAIL otherwise.
            """;

    public static void main(String[] args) throws Exception {
        PassFailJFrame passFailJFrame = new PassFailJFrame("Icon Showing " +
                "Test Instructions", INSTRUCTIONS, 5, 18, 48);
        SwingUtilities.invokeAndWait(() -> {
            try {
                createAndShowGUI();
            } catch (Exception e) {
               throw new RuntimeException("Error while running the test", e);
            }
        });
        passFailJFrame.awaitAndCheck();
    }

    public static void createAndShowGUI()
            throws InterruptedException, InvocationTargetException {
        Image i_16 = createIcon(16, 8, "16");
        Image i_32 = createIcon(32, 14, "32");
        Image i_48 = createIcon(48, 24, "48");
        Image i_64 = createIcon(64, 30, "64");

        ImageIcon ji_16 = new ImageIcon(IconShowingTest.class.getResource(
                "java-icon16.png"));

        Image[] images = new Image[] {i_16, i_32, i_48, i_64};
        List<Image> imageList = Arrays.asList(images);
        ImageIcon icon = new ImageIcon(new MRImage(images));

        Frame f1 = new Frame("Frame 1");
        f1.setIconImages(imageList);
        f1.setLayout(new GridLayout(0, 1));

        f1.add(new JLabel("Icon 16x16", new ImageIcon(i_16), JLabel.CENTER));
        f1.add(new JLabel("Icon 32x32", new ImageIcon(i_32), JLabel.CENTER));
        f1.add(new JLabel("Icon 48x48", new ImageIcon(i_48), JLabel.CENTER));
        f1.add(new JLabel("Icon 64x64", new ImageIcon(i_64), JLabel.CENTER));

        PassFailJFrame.positionTestWindow(null,
                PassFailJFrame.Position.TOP_LEFT_CORNER);
        Rectangle bounds = PassFailJFrame.getInstructionFrameBounds();

        int windowPosX = bounds.x + bounds.width + 5;
        f1.setBounds(windowPosX, EXTRA_OFFSET, 200, 300);
        f1.setVisible(true);
        f1.toFront();
        PassFailJFrame.addTestWindow(f1);
        int windowPosY = f1.getY() + f1.getHeight();


        Dialog d11 = new Dialog(f1, "Dialog 1.1");
        d11.setResizable(false);
        addIconAndLabelToWindow(d11, windowPosX, windowPosY - EXTRA_OFFSET,
                (isWindows() ? "No icon, non-resizable dialog"
                             : "Inherited icon, non-resizable dialog"),
                (isWindows() ? null : icon));

        Dialog d12 = new Dialog(d11, "Dialog 1.2");
        addIconAndLabelToWindow(d12, windowPosX, windowPosY + EXTRA_OFFSET,
                "Inherited icon, resizable dialog", icon);

        Frame f2 = new Frame("Frame 2");
        addIconAndLabelToWindow(f2, windowPosX + 200, 0,
                "Default Icon", ji_16);

        Dialog d21 = new Dialog(f2, "Dialog 2.1");
        d21.setResizable(false);
        addIconAndLabelToWindow(d21, windowPosX + 200, 100,
                (isWindows() ? "No icon, non-resizable dialog"
                             : "Inherited default Icon, non-resizable dialog"),
                (isWindows() ? null : ji_16));

        Dialog d22 = new Dialog(f2, "Dialog 2.2");
        addIconAndLabelToWindow(d22, windowPosX + 200, 200,
                "Inherited default Icon, resizable dialog", ji_16);

        Dialog d23 = new Dialog(f2, "Dialog 2.3");
        d23.setIconImages(imageList);
        d23.setResizable(false);
        addIconAndLabelToWindow(d23, windowPosX + 200, 300,
                "Modified Icon, non-resizable dialog", icon);

        Dialog d24 = new Dialog(f2, "Dialog 2.4");
        d24.setIconImages(imageList);
        addIconAndLabelToWindow(d24, windowPosX + 200, 400,
                "Modified Icon, resizable dialog", icon);

        Dialog d31 = new Dialog((Frame)null, "Dialog 3.1");
        addIconAndLabelToWindow(d31, windowPosX + 400, 100,
                "Default icon, resizable dialog", ji_16);

        Dialog d32 = new Dialog(d31, "Dialog 3.2");
        d32.setResizable(false);
        addIconAndLabelToWindow(d32, windowPosX + 400, 200,
                (isWindows() ? "No icon, non-resizable dialog"
                             : "Default Icon, non-resizable dialog"),
                (isWindows() ? null : ji_16));

        Dialog d33 = new Dialog(d31, "Dialog 3.3");
        d33.setIconImages(imageList);
        d33.setResizable(false);
        addIconAndLabelToWindow(d33, windowPosX + 400, 300,
                "Modified icon, non-resizable dialog", icon);


        Dialog d34 = new Dialog(d33, "Dialog 3.4");
        d34.setResizable(false);
        addIconAndLabelToWindow(d34, windowPosX + 400, 400,
                (isWindows() ? "No icon, non-resizable dialog"
                             : "Inherited modified icon, non-resizable dialog"),
                (isWindows() ? null : icon));


        Dialog d41 = new Dialog((Frame) null, "Dialog 4.1");
        d41.setResizable(false);
        addIconAndLabelToWindow(d41, windowPosX + 600, 100,
                "Default icon, non-resizable dialog", ji_16);


        Dialog d42 = new Dialog(d41, "Dialog 4.2");
        addIconAndLabelToWindow(d42, windowPosX + 600, 200,
                "Inherited default icon, resizable dialog", ji_16);

        Dialog d43 = new Dialog(d41, "Dialog 4.3");
        d43.setIconImages(imageList);
        addIconAndLabelToWindow(d43, windowPosX + 600, 300,
                "Modified icon, resizable dialog", icon);

        Dialog d44 = new Dialog(d43, "Dialog 4.4");
        addIconAndLabelToWindow(d44, windowPosX + 600, 400,
                "Inherited modified icon, resizable dialog", icon);
    }

    private static void addIconAndLabelToWindow(Window win, int x, int y,
                                                String title, ImageIcon icon) {
        win.setBounds(x, (y + EXTRA_OFFSET), 200, 100);
        win.add(new JLabel(title, icon, JLabel.CENTER));
        win.setVisible(true);
        win.toFront();
        PassFailJFrame.addTestWindow(win);
    }

    public static Image createIcon(int size, int fontSize, String value) {
        BufferedImage bImg = new BufferedImage(size, size, TYPE_INT_ARGB);
        Graphics2D g2d = bImg.createGraphics();

        int half = size / 2;
        for (int i = 0; i < half - 1; i += 2) {
            g2d.setComposite(AlphaComposite.Src);
            g2d.setColor(Color.RED);
            g2d.fillRect(0, i, half, 1);
            g2d.setComposite(AlphaComposite.Clear);
            g2d.fillRect(0, i + 1, half, 1);
        }
        g2d.setComposite(AlphaComposite.Clear);
        g2d.fillRect(half, 0, half, half);
        g2d.setComposite(AlphaComposite.Src);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setFont(new Font("Dialog", Font.PLAIN, fontSize));
        g2d.setColor(Color.BLUE);
        g2d.drawString(value, half - 1, half - 2);

        int height = (half + 1) / 3;
        // Green
        GradientPaint greenGradient = new GradientPaint(0, half - 1, Color.GREEN,
                size, half - 1, new Color(0, 255, 0, 0));
        g2d.setPaint(greenGradient);
        g2d.fillRect(0, half - 1, size, height);

        // Blue
        GradientPaint blueGradient = new GradientPaint(0, (half - 1) + height, Color.BLUE,
                size, (half - 1) + height, new Color(0, 0, 255, 0));
        g2d.setPaint(blueGradient);
        g2d.fillRect(0, (half - 1) + height, size, height);

        // Red
        GradientPaint redGradient = new GradientPaint(0, (half - 1) + height * 2, Color.RED,
                size, (half - 1) + height * 2, new Color(255, 0, 0, 0));
        g2d.setPaint(redGradient);
        g2d.fillRect(0, (half - 1) + height * 2, size, height);
        g2d.dispose();

        return bImg;
    }

    private static class MRImage extends BaseMultiResolutionImage {
        public MRImage(Image... resolutionVariants) {
            super(resolutionVariants);
        }

        @Override
        public Image getResolutionVariant(double expectedSize, double unused) {
            final int size = (int) Math.round(expectedSize / 16.0) * 16;
            List<Image> imageList = getResolutionVariants();
            for (int i = 0; i < imageList.size(); i++) {
                if (size == imageList.get(i).getWidth(null)) {
                    return imageList.get(i);
                } else if (imageList.get(i).getWidth(null) > size) {
                    return imageList.get(i > 0 ? i - 1 : i);
                }
            }
            return imageList.get(0); //default/base image
        }
    }
}