File: VisibilityValidator.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (307 lines) | stat: -rw-r--r-- 10,164 bytes parent folder | download | duplicates (2)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * Copyright (c) 2011, 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.
 */

 /*
 * @summary Utility routines that wait for a window to be displayed or for
colors to be visible
 * @summary com.apple.junit.utils
 */
package test.java.awt.regtesthelpers;

import java.awt.*;
import java.awt.event.*;

//import junit.framework.Assert;
public class VisibilityValidator {

    // Wait up to five seconds for our window events
    static final int SETUP_PERIOD = 5000;
    static final boolean DEBUG = false;

    volatile Window win = null;
    boolean activated = false;
    boolean opened = false;
    boolean focused = false;
    volatile boolean valid = false;

    //
    // Utility functions that encapsulates normal usage patterns
    //
    public static void setVisibleAndConfirm(Frame testframe) throws Exception {
        setVisibleAndConfirm(testframe, "Could not confirm test frame was "
                + "visible");
    }

    public static void setVisibleAndConfirm(Frame testframe, String msg)
            throws Exception {
        if (testframe.isVisible()) {
            throw new RuntimeException("Frame is already visible");
        }

        VisibilityValidator checkpoint = new VisibilityValidator(testframe);
        testframe.setVisible(true);
        checkpoint.requireVisible();
        if (!checkpoint.isValid()) {
            //System.err.println(msg);
            throw new Exception("Frame not visible after " + SETUP_PERIOD
                    + " milliseconds");
        }
    }

    //
    // Add listeners to the window
    //
    public VisibilityValidator(Window win) {
        this.win = win;
        WindowAdapter watcher = new WindowAdapter() {
            public void windowOpened(WindowEvent e) {
                doOpen();
            }

            public void windowActivated(WindowEvent e) {
                doActivate();
            }

            public void windowGainedFocus(WindowEvent e) {
                doGainedFocus();
            }
        };

        win.addWindowListener(watcher);
        win.addWindowFocusListener(watcher);
    }

    // Make the window visible
    //
    // The only way to make it through this routine is for the window to
    // generate BOTH a windowOpened, a windowActivated event and a
    // windowGainedFocus, or to timeout.
    //
    synchronized public void requireVisible() {
        int tries = 0;

        // wait for windowOpened and windowActivated events
        try {
            while ((opened == false)
                    || (activated == false)
                    || (focused == false)) {
                if (tries < 4) {
                    tries += 1;
                    wait(SETUP_PERIOD);
                } else {
                    break;
                }
            }

            if (opened && activated) {
                valid = true;
            } else {
                valid = false;
            }
        } catch (InterruptedException ix) {
            valid = false;
        }

        // Extra-super paranoid checks
        if (win.isVisible() == false) {
            valid = false;
        }

        if (win.isShowing() == false) {
            valid = false;
        }

        if (win.isFocused() == false) {
            valid = false;
        }

        if (DEBUG) {
            if (!isValid()) {
                System.out.println("\tactivated:" + new Boolean(activated));
                System.out.println("\topened:" + new Boolean(opened));
                System.out.println("\tfocused:" + new Boolean(focused));
                System.out.println("\tvalid:" + new Boolean(valid));
                System.out.println("\tisVisible():"
                        + new Boolean(win.isVisible()));
                System.out.println("\tisShowing():"
                        + new Boolean(win.isShowing()));
                System.out.println("\tisFocused():"
                        + new Boolean(win.isFocused()));
            }
        }

    }

    synchronized void doOpen() {
        opened = true;
        notify();
    }

    synchronized void doActivate() {
        activated = true;
        notify();
    }

    synchronized void doGainedFocus() {
        focused = true;
        notify();
    }

    public boolean isValid() {
        return valid;
    }

    public boolean isClear() {
        return valid;
    }

    volatile static Robot robot = null;

    // utility function that waits until a Component is shown with the
    // appropriate color
    public static boolean waitForColor(Component c,
            Color expected) throws AWTException,
            InterruptedException {
        Dimension dim = c.getSize();
        int xOff = dim.width / 2;
        int yOff = dim.height / 2;
        return waitForColor(c, xOff, yOff, expected);
    }

    // utility function that waits for 5 seconds for Component to be shown with
    // the appropriate color
    public static boolean waitForColor(Component c,
            int xOff,
            int yOff,
            Color expected) throws AWTException, InterruptedException {
        return waitForColor(c, xOff, yOff, expected, 5000L);
    }

    // utility function that waits until a Component is up with the appropriate
    // color
    public static boolean waitForColor(Component c,
            int xOff,
            int yOff,
            Color expected,
            long timeout) throws AWTException, InterruptedException {
        Point p = c.getLocationOnScreen();
        int x = (int) p.getX() + xOff;
        int y = (int) p.getY() + yOff;
        return waitForColor(x, y, expected, timeout);
    }

    // utility function that waits until specific screen coords have the
    // appropriate color
    public static boolean waitForColor(int locX,
            int locY,
            Color expected,
            long timeout) throws AWTException, InterruptedException {
        if (robot == null) {
            robot = new Robot();
        }

        long endtime = System.currentTimeMillis() + timeout;
        while (endtime > System.currentTimeMillis()) {
            if (colorMatch(robot.getPixelColor(locX, locY), expected)) {
                return true;
            }
            Thread.sleep(50);
        }

        return false;
    }

    // utility function that asserts that two colors are similar to each other
    public static void assertColorEquals(final String message,
            final Color actual,
            final Color expected) {
        System.out.println("actual color: " + actual);
        System.out.println("expect color: " + expected);
        //Assert.assertTrue(message, colorMatch(actual, expected));
    }

    // determines if two colors are close in hue and brightness
    public static boolean colorMatch(final Color actual, final Color expected) {
        final float[] actualHSB = getHSB(actual);
        final float[] expectedHSB = getHSB(expected);

        final float actualHue = actualHSB[0];
        final float expectedHue = expectedHSB[0];
        final boolean hueMatched = closeMatchHue(actualHue, expectedHue, 0.17f);
        //System.out.println("hueMatched? " + hueMatched);
        final float actualBrightness = actualHSB[2];
        final float expectedBrightness = expectedHSB[2];
        final boolean brightnessMatched = closeMatch(actualBrightness,
                expectedBrightness, 0.15f);
        //System.out.println("brightnessMatched? " + brightnessMatched);

        // check to see if the brightness was so low or so high that the hue
        // got clamped to red
        if (brightnessMatched && !hueMatched) {
            return (expectedBrightness < 0.15f);
        }

        return brightnessMatched && hueMatched;
    }

    static float[] getHSB(final Color color) {
        final float[] hsb = new float[3];
        Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb);
        return hsb;
    }

    // matches hues from 0.0 to 1.0, accounting for wrap-around at the 1.0/0.0
    // boundry
    static boolean closeMatchHue(final float actual,
            final float expected,
            final float tolerance) {
        if (closeMatch(actual, expected, tolerance)) {
            return true;
        }

        // all that remains is the overflow and underflow cases
        final float expectedHigh = expected + tolerance;
        final float expectedLow = expected - tolerance;

        if (expectedHigh > 1.0f) {
            // expected is too high, and actual was too low
            //System.out.println("\thue expected too high, actual too low");
            return closeMatch(actual + 0.5f, expected - 0.5f, tolerance);
        }

        if (expectedLow < 0.0f) {
            // expected is too low, and actual was too high
            //System.out.println("\thue expected too low, actual too high");
            return closeMatch(actual - 0.5f, expected + 0.5f, tolerance);
        }

        //System.out.println("\tcloseMatchHue? " + false);
        return false;
    }

    static boolean closeMatch(final float actual,
            final float expected,
            final float tolerance) {
        return (expected + tolerance) > actual && (expected - tolerance) < actual;
    }
}