File: AutoTestOnTop.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 (797 lines) | stat: -rw-r--r-- 29,872 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
/*
 * Copyright (c) 2003, 2015, 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.
 */

/*
  @test
  @key headful
  @bug 4632143
  @summary Unit test for the RFE window/frame/dialog always on top
  @author dom@sparc.spb.su: area=awt.toplevel
  @modules java.desktop/sun.awt
  @run main AutoTestOnTop
*/

import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.*;
import javax.swing.*;
import java.util.Vector;

/**
 * @author tav@sparc.spb.su
 * @author dom@sparc.spb.su
 * Tests that always-on-top windows combine correctly with different kinds of window in different styles and conditions.
 *
 * !!! WARNING !!!
 * The test fails sometimes because the toFront() method doesn't guarantee
 * that after its invocation the frame will be placed above all other windows.
 */
public class AutoTestOnTop {
    static Window topw;
    static Frame  parentw = new Frame();
    static Window f;
    static Frame  parentf = new Frame();

    static Object  uncheckedSrc = new Object(); // used when no need to check event source
    static Object  eventSrc = uncheckedSrc;
    static boolean dispatchedCond;

    static Semaphore STATE_SEMA = new Semaphore();
    static Semaphore VIS_SEMA = new Semaphore();
    static Vector errors = new Vector();

    static boolean isUnix = false;

    static StringBuffer msgError = new StringBuffer();
    static StringBuffer msgCase = new StringBuffer();
    static StringBuffer msgAction = new StringBuffer();
    static StringBuffer msgFunc = new StringBuffer();
    static StringBuffer msgVisibility = new StringBuffer();

    static volatile int stageNum;
    static volatile int actNum;
    static volatile int testResult = 0;

    static volatile boolean doCheckEvents;
    static volatile boolean eventsCheckPassed;
    static boolean[] eventsCheckInitVals = new boolean[] { // Whether events are checked for abcence or precence
        true, true, true, true, true, false, false, false, false
    };
    static String[] msgEventsChecks = new String[] {
        null, null, null, null, null,
        "expected WindowEvent.WINDOW_STATE_CHANGED hasn't been generated",
        "expected WindowEvent.WINDOW_STATE_CHANGED hasn't been generated",
        "expected WindowEvent.WINDOW_STATE_CHANGED hasn't been generated",
        "expected WindowEvent.WINDOW_STATE_CHANGED hasn't been generated",
    };

    static final int stagesCount = 7;
    static final int actionsCount = 9;

    static Method[] preActions = new Method[actionsCount];
    static Method[] postActions = new Method[actionsCount];
    static Method[] isActionsAllowed = new Method[actionsCount];
    static Method[] checksActionEvents = new Method[actionsCount];

    static Robot robot;

    static boolean doStartTest;
    static String osName = System.getProperty("os.name");


    public static void main(String[] args) {
        checkTesting();

    }

    public static void performTesting() {
        isUnix = osName.equals("Linux") || osName.equals("SunOS");

        Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
                public void eventDispatched(AWTEvent e) {
                    if (e.getID() == MouseEvent.MOUSE_CLICKED) {
                        if (eventSrc != null & eventSrc != uncheckedSrc && e.getSource() != eventSrc) {
                            error("Test failed: stage #" + stageNum + ", action # " + actNum + ": " + msgCase + ": " + msgAction + ": " + msgError);
                            testResult = -1;
                        }
                        synchronized (eventSrc) {
                            dispatchedCond = true;
                            eventSrc.notify();
                        }
                    }

                    if (doCheckEvents && (e.getSource() == topw || e.getSource() == f)) {

                        //System.err.println("AWTEventListener: catched the event " + e);

                        try {
                            checksActionEvents[actNum].invoke(null, new Object[] {e});
                        } catch (InvocationTargetException ite) {
                            ite.printStackTrace();
                        } catch (IllegalAccessException iae) {
                            iae.printStackTrace();
                        }
                        return;
                    }
                }
            }, 0xffffffffffffffffL);

        Method[] allMethods;

        try {
            allMethods = AutoTestOnTop.class.getDeclaredMethods();
        } catch (SecurityException se) {
            throw new RuntimeException(se);
        }

        for (int i = 0; i < allMethods.length; i++) {
            String name = allMethods[i].getName();
            if (name.startsWith("preAction")) {
                preActions[name.charAt(name.length() - 1) - '0'] = allMethods[i];
            } else if (name.startsWith("postAction")) {
                postActions[name.charAt(name.length() - 1) - '0'] = allMethods[i];
            } else if (name.startsWith("isActionAllowed")) {
                isActionsAllowed[name.charAt(name.length() - 1) - '0'] = allMethods[i];
            } else if (name.startsWith("checkActionEvents")) {
                checksActionEvents[name.charAt(name.length() - 1) - '0'] = allMethods[i];
            }
        }

        f = new Frame("Auxiliary Frame");
        f.setBounds(50, 0, 400, 50);
        f.setVisible(true);
        waitTillShown(f);

        try {
            robot = new Robot();
        } catch (AWTException e) {
            throw new RuntimeException("Error: unable to create robot", e);
        }

        mainTest();

        if (testResult != 0) {
            System.err.println("The following errors were encountered: ");
            for (int i = 0; i < errors.size(); i++) {
                System.err.println(errors.get(i).toString());
            }
            throw new RuntimeException("Test failed.");
        } else {
            System.err.println("Test PASSED.");
        }
    }

    public static void mainTest() {
//         stageNum = 0;
//         for (int i = 0; i < 5; i++) {
//             actNum = 2;
//             System.err.println("************************* A C T I O N " + actNum + " *************************");
//             doStage(stageNum, actNum);
// //             pause(500);
//             actNum = 3;
//             System.err.println("************************* A C T I O N " + actNum + " *************************");
//             doStage(stageNum, actNum);
// //             pause(500);
//         }
        for (stageNum = 0; stageNum < stagesCount; stageNum++) {
            System.err.println("************************* S T A G E " + stageNum + " *************************");
            for (actNum = 0; actNum < actionsCount; actNum++) {
                System.err.println("************************* A C T I O N " + actNum + " *************************");
                doStage(stageNum, actNum);
            } // for thru actNum
        } // fow thru stageNum

        eventSrc = null;
    }

    private static void doStage(int stageNum, int actNum) {
        try {

            if (!((Boolean)isActionsAllowed[actNum].invoke(null, new Object[0])).booleanValue()) {
                System.err.println("Action skipped due to a platform limitations");
                return;
            }

            STATE_SEMA.reset();
            createWindow(stageNum);

            //*************************
            // Set window always-on-top
            //*************************

            preActions[actNum].invoke(null, new Object[0]);
            setAlwaysOnTop(topw, true);
            waitForIdle(true);

            if (!topw.isAlwaysOnTopSupported()) return;

            postActions[actNum].invoke(null, new Object[0]);
            waitForIdle(false);

            STATE_SEMA.reset();

            testForAlwaysOnTop();

            //*****************************
            // Set window not always-on-top
            //*****************************

            preActions[actNum].invoke(null, new Object[0]);
            setAlwaysOnTop(topw, false);
            waitForIdle(true);
            postActions[actNum].invoke(null, new Object[0]);
            waitForIdle(false);
            STATE_SEMA.reset();

            testForNotAlwaysOnTop();

        } catch (InvocationTargetException ite) {
            ite.printStackTrace();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    private static void checkTesting() {
        if (Toolkit.getDefaultToolkit().isAlwaysOnTopSupported()) {
            performTesting();
        }
    }

    public static void testForAlwaysOnTop() {
        System.err.println("Checking for always-on-top " + topw);

        ensureInitialWinPosition(topw);

        // Check that always-on-top window is topmost.
        // - Click on always-on-top window on the windows cross area.
        clickOn(topw, f, 10, 30, "setting " + msgVisibility +
                " window (1) always-on-top didn't make it topmost");

        // Check that we can't change z-order of always-on-top window.
        // - a) Try to put the other window on the top.
        f.toFront();
        clickOn(uncheckedSrc, f, 190, 30, ""); // coz toFront() works not always
        pause(300);

        // - b) Click on always-on-top window on the windows cross area.
        clickOn(topw, f, 10, 30, "setting " + msgVisibility +
                " window (1) always-on-top didn't make it such");

        // Ask for always-on-top property
        if (isAlwaysOnTop(topw) != true)
                error("Test failed: stage #" + stageNum + ", action #" + actNum + ": " + msgCase + ": " + msgAction +
                                   ": isAlwaysOnTop() returned 'false' for window (1) set always-on-top at state "
                                   + msgVisibility);
    }

    public static void testForNotAlwaysOnTop() {
        System.err.println("Checking for non always-on-top of " + topw);
        ensureInitialWinPosition(topw);

        if (msgVisibility.equals("visible") && actNum != 2) {
            // Check that the window remains topmost.
            // - click on the window on the windows cross area.
            clickOn(topw, f, 10, 30, "setting " + msgVisibility +
                    " window (1) not always-on-top didn't keep it topmost");
        }

        // Check that we can change z-order of not always-on-top window.
        // - a) try to put the other window on the top.
        f.toFront();
        clickOn(uncheckedSrc, f, 190, 30, ""); // coz toFront() works not always
        pause(300);

        // - b) click on not always-on-top window on the windows cross area.
        clickOn(f, f, 10, 30, "setting " + msgVisibility +
                " window (1) not always-on-top didn't make it such");

        // Ask for always-on-top property
        if (isAlwaysOnTop(topw) != false)
            error("Test failed: stage #" + stageNum + ", action #" + actNum + ": " + msgCase + ": " + msgAction +
                               ": isAlwaysOnTop() returned 'true' for window (1) set not always-on-top at state "
                               + msgVisibility);
    }


    private static void createWindow(int stageNum) {
        // Free native resourses
        if (topw != null && topw.isVisible()) {
            topw.dispose();
        }

        switch (stageNum) {
        case 0:
            topw = new Frame("Top Frame");
            msgCase.replace(0, msgCase.length(), "Frame (1) over Frame (2)");
            break;
        case 1:
            topw = new JFrame("Top JFrame");
            msgCase.replace(0, msgCase.length(), "JFrame (1) over Frame (2)");
            break;
        case 2:
            topw = new Dialog(parentw, "Top Dialog");
            msgCase.replace(0, msgCase.length(), "Dialog (1) over Frame (2)");
            break;
        case 3:
            topw = new JDialog(parentw, "Top JDialog");
            msgCase.replace(0, msgCase.length(), "JDialog (1) over Frame (2)");
            break;
        case 4:
            topw = new Frame("Top Frame");
            f.dispose();
            f = new Dialog(parentf, "Auxiliary Dialog");
            f.setBounds(50, 0, 250, 50);
            f.setVisible(true);
            waitTillShown(f);
            msgCase.replace(0, msgCase.length(), "Frame (1) over Dialog (2)");
            break;
        case 5:
            topw = new Window(parentw);
            msgCase.replace(0, msgCase.length(), "Window (1) over Frame (2)");
            break;
        case 6:
            topw = new JWindow(parentw);
            msgCase.replace(0, msgCase.length(), "JWindow (1) over Frame (2)");
            break;
        }
        topw.addWindowStateListener(new WindowAdapter() {
                public void windowStateChanged(WindowEvent e) {
                    System.err.println("* " + e);
                    STATE_SEMA.raise();
                }
            });
        topw.setSize(200, 50);
    }

    /**
     * 0: setting always-on-top to invisible window
     * 1: setting always-on-top to visible window
     * 2: always-on-top on visible non-focusable window
     * 3: always-on-top on visible, dragging topw after that
     * 4: always-on-top on visible, dragging f after that
     * 5: always-on-top on (visible, maximized), make normal after that
     * 6: always-on-top on (visible, iconified), make normal after that
     * 7: always-on-top on visible, iconify/deiconify after that
     * 8: always-on-top on visible, maximize/restore after that
     */
    public static void preAction_0() {
        topw.setVisible(false);
    }
    public static void postAction_0() {
        if (topw.isShowing()) {
            error("Test failed: stage #" + stageNum + ", action #" + actNum + ": " + msgCase +
                               ": no actions with windows: changing always-on-top property at window (1) state 'invisible' makes window (1) visible");
        }
        setWindowVisible("no actions with windows", "invisible");
    }
    public static boolean isActionAllowed_0() {
        // Window on Linux is always always-on-top!
        return !((stageNum == 5 || stageNum == 6) && isUnix) && (stageNum < stagesCount);
    }
    public static void checkActionEvents_0(AWTEvent e) {
        System.err.println(e.toString());
   }

    public static void preAction_1() {
        setWindowVisible("no actions with windows", "visible");
    }
    public static void postAction_1() {}
    public static boolean isActionAllowed_1() {
        return !((stageNum == 5 || stageNum == 6) && isUnix) && (stageNum < stagesCount );
    }
    public static void checkActionEvents_1(AWTEvent e) {
        System.err.println(e.toString());
        if (e instanceof PaintEvent) {
            return;
        }
        eventsCheckPassed = false;
        error("Test failed: stage #" + stageNum + ", action # " + actNum + ": " + msgCase + ": " + msgAction + ": after call " + msgFunc +
                           ":  unexpected event " + e + " was generated");
    }

    public static void preAction_2() {
        setWindowVisible("when window (1) set not focusable", "visible");
        topw.setFocusableWindowState(false);
        f.toFront();
        pause(300);
    }
    public static void postAction_2() {}
    public static boolean isActionAllowed_2() {
        return !((stageNum == 5 || stageNum == 6) && isUnix) && (stageNum < stagesCount);
    }
    public static void checkActionEvents_2(AWTEvent e) {
        System.err.println(e.toString());
        if ( (e.getID() >= FocusEvent.FOCUS_FIRST && e.getID() <= FocusEvent.FOCUS_LAST) ||
             (e.getID() == WindowEvent.WINDOW_LOST_FOCUS && e.getID() == WindowEvent.WINDOW_GAINED_FOCUS)) {
            eventsCheckPassed = false;
            error("Test failed: stage #" + stageNum + ", action # " + actNum + ": " + msgCase + ": " +
                               msgAction + ": after call " + msgFunc +
                               ": unexpected event " + e + " was generated");
        }
    }

    public static void preAction_3() {
        setWindowVisible("after dragging",  "visible");
    }
    public static void postAction_3() {
        Point p = topw.getLocationOnScreen();
        int x = p.x + 40, y = p.y + 5;

        try {                      // Take a pause to avoid double click
            Thread.sleep(500);     // when called one after another.
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        } catch (IllegalComponentStateException e) {
            e.printStackTrace();
        }

        // Drag the window.
        robot.mouseMove(x, y);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseMove(200, 50);
        robot.mouseMove(x, y);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
    }
    public static boolean isActionAllowed_3() {
        return (stageNum < 5);
    }
    public static void checkActionEvents_3(AWTEvent e) {
        System.err.println(e.toString());
    }

    public static void preAction_4() {
        setWindowVisible("after dragging window (2)",  "visible");
    }
    public static void postAction_4() {
        Point p = f.getLocationOnScreen();
        int x = p.x + 150, y = p.y + 5;

        try {                      // Take a pause to avoid double click
            Thread.sleep(500);     // when called one after another.
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        } catch (IllegalComponentStateException e) {
            e.printStackTrace();
        }

        // Drag the window.
        robot.mouseMove(x, y);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseMove(200, 50);
        robot.mouseMove(x, y);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);

        ensureInitialWinPosition(f);
    }
    public static boolean isActionAllowed_4() {
        return !((stageNum == 5 || stageNum == 6) && isUnix);
    }
    public static void checkActionEvents_4(AWTEvent e) {
        System.err.println(e.toString());
    }

    // Metacity has a bug not allowing to set a window to NORMAL state!!!

    public static void preAction_5() {
        setWindowVisible("at state 'maximized'",  "visible");
        ((Frame)topw).setExtendedState(Frame.MAXIMIZED_BOTH);
        waitForStateChange();
    }
    public static void postAction_5() {
        ((Frame)topw).setExtendedState(Frame.NORMAL);
        waitForStateChange();
    }
    public static boolean isActionAllowed_5() {
        return (stageNum < 2);
    }
    public static void checkActionEvents_5(AWTEvent e) {
        System.err.println("=" + e.toString());
        if (e.getID() == WindowEvent.WINDOW_STATE_CHANGED) {
            eventsCheckPassed = true;
        }
    }

    public static void preAction_6() {
        setWindowVisible("at state 'iconified'",  "visible");
        System.err.println("Iconifying " + topw);
        ((Frame)topw).setExtendedState(Frame.ICONIFIED);
        if (!waitForStateChange()) {
            error("Test failed: stage #" + stageNum + ", action # " + actNum + ": " + msgCase + ": " + msgAction + ": after call " + msgFunc +
                               ":  state change to ICONIFIED hasn't been generated");
        }
    }
    public static void postAction_6() {
        System.err.println("Restoring " + topw);
        ((Frame)topw).setExtendedState(Frame.NORMAL);
        if (!waitForStateChange()) {
            error("Test failed: stage #" + stageNum + ", action # " + actNum + ": " + msgCase + ": " + msgAction + ": after call " + msgFunc +
                               ":  state change to NORMAL hasn't been generated");
        }
    }
    public static boolean isActionAllowed_6() {
        return (stageNum < 2 );
    }
    public static void checkActionEvents_6(AWTEvent e) {
        System.err.println("+" + e.toString());
        if (e.getID() == WindowEvent.WINDOW_STATE_CHANGED) {
            eventsCheckPassed = true;
        }
    }

    public static void preAction_7() {
        setWindowVisible("before state 'iconified'",  "visible");
    }
    public static void postAction_7() {
        System.err.println("Setting iconified");
        ((Frame)topw).setExtendedState(Frame.ICONIFIED);
        if (!waitForStateChange()) {
            error("Test failed: stage #" + stageNum + ", action # " + actNum + ": " + msgCase + ": " + msgAction + ": after call " + msgFunc +
                               ":  state change to ICONIFIED hasn't been generated");
        }
        System.err.println("Setting normal");
        ((Frame)topw).setExtendedState(Frame.NORMAL);
        if (!waitForStateChange()) {
            error("Test failed: stage #" + stageNum + ", action # " + actNum + ": " + msgCase + ": " + msgAction + ": after call " + msgFunc +
                               ":  state change to NORMAL hasn't been generated");
        }
    }
    public static boolean isActionAllowed_7() {
        return (stageNum < 2);
    }
    public static void checkActionEvents_7(AWTEvent e) {
        System.err.println(e.toString());
        if (e.getID() == WindowEvent.WINDOW_STATE_CHANGED) {
            eventsCheckPassed = true;
        }
    }

    public static void preAction_8() {
        setWindowVisible("before state 'maximized'",  "visible");
    }
    public static void postAction_8() {
        ((Frame)topw).setExtendedState(Frame.MAXIMIZED_BOTH);
        if (!waitForStateChange()) {
            error("Test failed: stage #" + stageNum + ", action # " + actNum + ": " + msgCase + ": " + msgAction + ": after call " + msgFunc +
                               ":  state change to MAXIMIZED hasn't been generated");
        }
        ((Frame)topw).setExtendedState(Frame.NORMAL);
        if (!waitForStateChange()) {
            error("Test failed: stage #" + stageNum + ", action # " + actNum + ": " + msgCase + ": " + msgAction + ": after call " + msgFunc +
                               ":  state change to NORMAL hasn't been generated");
        }
    }
    public static boolean isActionAllowed_8() {
        return (stageNum < 2);
    }
    public static void checkActionEvents_8(AWTEvent e) {
        System.err.println(e.toString());
        if (e.getID() == WindowEvent.WINDOW_STATE_CHANGED) {
           eventsCheckPassed = true;
        }
    }

    //***************************************************************************

    private static void setWindowVisible(String mAction, String mVisibility) {
        msgAction.replace(0, msgAction.length(), mAction);
        msgVisibility.replace(0, msgVisibility.length(), mVisibility);

        topw.setVisible(true);
        pause(100); // Needs for Sawfish
        topw.setLocation(0, 0);
        waitTillShown(topw);
        f.toFront();
        pause(300);
    }

    private static void clickOn(Object src, Window relwin, int x, int y, String errorStr) {
        Point p = relwin.getLocationOnScreen();
        int counter = 10;
        while (--counter > 0) {
            eventSrc = src;
            msgError.replace(0, msgError.length(), errorStr);

            robot.mouseMove(p.x + x, p.y + y);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);

            synchronized (eventSrc) {
                if (!dispatchedCond) {
                    try {
                        eventSrc.wait(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if (!dispatchedCond) {
                    //System.err.println("clickOn: MOUSE_CLICKED event losed, trying to generate it again...");
                    continue;
                }
                dispatchedCond = false;
            }
            break;
        } // end while
        if (counter <= 0) {
            eventSrc = uncheckedSrc;
            error("Test: internal error: could't catch MOUSE_CLICKED event. Skip testing this stage");
        }
    }

    private static void setAlwaysOnTop(Window w, boolean value) {
        System.err.println("Setting always on top on " + w + " to " + value);
        robot.mouseMove(0, 100); // Move out of the window
        msgFunc.replace(0, msgCase.length(), "setAlwaysOnTop()");
        try {
            w.setAlwaysOnTop(value);
        } catch (Exception e) {
            error("Test failed: stage#" + stageNum + "action #" + actNum + ": " + msgCase + ": " + msgAction +
                               ": setAlwaysOnTop(" + value + ") called at state " + msgVisibility +
                               " threw exeption " + e);
        }
    }

    private static boolean isAlwaysOnTop(Window w) {
        robot.mouseMove(0, 100); // Move out of the window
        msgFunc.replace(0, msgCase.length(), "isAlwaysOnTop()");
        boolean result = false;
        try {
            result = w.isAlwaysOnTop();
        } catch (Exception e) {
            error("Test failed: stage #" + stageNum + ", action # " + actNum + ": " + msgCase + ": " + msgAction +
                               ": isAlwaysOnTop() called at state " + msgVisibility +
                               " threw exeption " + e);
        }
        return result;
    }

    private static void waitTillShown(Component c) {
        while (true) {
            try {
                Thread.sleep(100);
                c.getLocationOnScreen();
                break;
            } catch (InterruptedException e) {
                e.printStackTrace();
                break;
            }
        }
    }

    private static void waitForIdle(boolean doCheck) {
        try {
            robot.waitForIdle();
            EventQueue.invokeAndWait( new Runnable() {
                    public void run() {} // Dummy implementation
                } );
        } catch(InterruptedException ite) {
            System.err.println("waitForIdle, non-fatal exception caught:");
            ite.printStackTrace();
        } catch(InvocationTargetException ine) {
            System.err.println("waitForIdle, non-fatal exception caught:");
            ine.printStackTrace();
        }
        doCheckEvents = doCheck;

        if (doCheck) {
            eventsCheckPassed = eventsCheckInitVals[actNum]; // Initialize
        } else if (!eventsCheckPassed &&
                 msgEventsChecks[actNum] != null) {


            // Some expected event hasn't been catched,
            // so give it one more chance...
            doCheckEvents = true;
            pause(500);
            doCheckEvents = false;

            if (!eventsCheckPassed) {
                testResult = -1;
                error("Test failed: stage #" + stageNum + ", action # " + actNum + ": " + msgCase + ": " + msgAction + ": after call "
                                   + msgFunc + ": " + msgEventsChecks[actNum]);
            }
        }
    }

    private static boolean waitForStateChange() {
        System.err.println("------- Waiting for state change");
        try {
            STATE_SEMA.doWait(3000);
        } catch (InterruptedException ie) {
            System.err.println("Wait interrupted: " + ie);
        }
        boolean state = STATE_SEMA.getState();
        STATE_SEMA.reset();
        return state;
    }

    private static void ensureInitialWinPosition(Window w) {
        int counter = 30;
        while (w.getLocationOnScreen().y != 0 && --counter > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
                break;
            }
        }
        if (counter <= 0) {
            w.setLocation(0, 0);
            pause(100);
            System.err.println("Test: window set to initial position forcedly");
        }
    }

    private static void pause(int mls) {
        try {
            Thread.sleep(mls);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void error(String msg) {
        errors.add(msg);
        System.err.println(msg);
    }
}

class Semaphore {
    boolean state = false;
    int waiting = 0;
    public Semaphore() {
    }
    public synchronized void doWait() throws InterruptedException {
        if (state) {
            return;
        }
        waiting++;
        wait();
        waiting--;
    }
    public synchronized void doWait(int timeout) throws InterruptedException {
        if (state) {
            return;
        }
        waiting++;
        wait(timeout);
        waiting--;
    }
    public synchronized void raise() {
        state = true;
        if (waiting > 0) {
            notifyAll();
        }
    }

    public synchronized void doNotify() {
        notifyAll();
    }
    public synchronized boolean getState() {
        return state;
    }

    public synchronized void reset() {
        state = false;
    }
}