File: CheckboxCheckerScalingTest.java

package info (click to toggle)
openjdk-24 24.0.2%2B12-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 831,900 kB
  • sloc: java: 5,677,020; cpp: 1,323,154; xml: 1,320,524; ansic: 486,889; asm: 405,131; objc: 21,025; sh: 15,221; javascript: 11,049; python: 8,222; makefile: 2,504; perl: 357; awk: 351; sed: 172; pascal: 103; exp: 54; jsp: 24; csh: 3
file content (115 lines) | stat: -rw-r--r-- 4,217 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (c) 2024, 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.Checkbox;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/*
 * @test
 * @key headful
 * @bug 8233068
 * @summary Tests checkbox checker on scaling
 * @requires (os.family == "linux")
 * @run main CheckboxCheckerScalingTest
 */

public class CheckboxCheckerScalingTest {
    private static Frame frame;
    private static Checkbox checkbox;
    private static volatile Point point;
    private static boolean checkmarkFound = false;
    private static final int TOLERANCE = 5;
    private static final int COLOR_CHECK_THRESHOLD = 8;
    private static int colorCounter = 0;

    public static void main(String[] args) throws Exception {
        System.setProperty("sun.java2d.uiScale", "2");
        Robot robot = new Robot();

        try {
            EventQueue.invokeAndWait(() -> {
                frame = new Frame("CheckBox checker scaling test");
                checkbox = new Checkbox("one");
                checkbox.setState(true);
                frame.add(checkbox);
                frame.setLocationRelativeTo(null);
                frame.pack();
                frame.setVisible(true);
            });

            robot.waitForIdle();
            robot.delay(100);
            EventQueue.invokeAndWait(() -> point = checkbox.getLocationOnScreen());
            Rectangle rect = new Rectangle(point.x + 5, point.y + 7, 8, 8);
            BufferedImage imageAfterChecked = robot.createScreenCapture(rect);
            check:
            {
                for (int i = 0; i < imageAfterChecked.getHeight(); i++) {
                    for (int j = 0; j < imageAfterChecked.getWidth(); j++) {
                        Color pixelColor = new Color(imageAfterChecked.getRGB(i, j));
                        if (compareColor(pixelColor)) {
                            if (++colorCounter >= COLOR_CHECK_THRESHOLD) {
                                checkmarkFound = true;
                                break check;
                            }
                        }

                    }
                }
            }

            if (!checkmarkFound) {
                try {
                    ImageIO.write(imageAfterChecked, "png",
                            new File("imageAfterChecked.png"));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                throw new RuntimeException("Checkmark not scaled");
            }
            System.out.println("Test Passed");
        } finally {
            EventQueue.invokeAndWait(() -> {
                if (frame != null) {
                    frame.dispose();
                }
            });
        }
    }

    private static boolean compareColor(Color c) {
        return Math.abs(Color.black.getRed() - c.getRed()) < TOLERANCE &&
                Math.abs(Color.black.getGreen() - c.getGreen()) < TOLERANCE &&
                Math.abs(Color.black.getBlue() - c.getBlue()) < TOLERANCE;
    }
}