File: ActualFocusedWindowRetaining.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 (212 lines) | stat: -rw-r--r-- 7,429 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
/*
 * Copyright (c) 2008, 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      4823903
  @summary  Tests actual focused window retaining.
  @author   Anton.Tarasov: area=awt.focus
  @library  ../../regtesthelpers
  @build    Util
  @run      main ActualFocusedWindowRetaining
*/

import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.*;
import java.applet.*;
import test.java.awt.regtesthelpers.Util;

public class ActualFocusedWindowRetaining extends Applet {
    public static Frame frame = new Frame("Other Frame");
    public static Frame owner = new Frame("Test Frame");
    public static Button otherButton1 = new Button("Other Button 1");
    public static Button otherButton2 = new Button("Other Button 2");
    public static Button otherButton3 = new Button("Other Button 3");
    public static Button testButton1 = new Button("Test Button 1");
    public static Button testButton2 = new Button("Test Button 2");
    public static Button testButton3 = new Button("Test Button 3");
    public static Window window1 = new TestWindow(owner, otherButton2, testButton2, 800, 200);
    public static Window window2 = new TestWindow(owner, otherButton3, testButton3, 800, 300);
    public static int step;
    public static Robot robot = Util.createRobot();

    public static void main(String[] args) {
        ActualFocusedWindowRetaining a = new ActualFocusedWindowRetaining();
        a.init();
        a.start();
    }

    public void start () {
        Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
                public void eventDispatched(AWTEvent e) {
                    Object src = e.getSource();
                    Class cls = src.getClass();

                    if (cls == TestWindow.class) {
                        System.out.println(e.paramString() + " on <" + (src == window1 ? "Window 1" : "Window 2") + ">");
                    } else if (cls == Frame.class) {
                        System.out.println(e.paramString() + " on <" + ((Frame)src).getTitle() + ">");
                    } else if (cls == Button.class) {
                        System.out.println(e.paramString() + " on <" + ((Button)src).getLabel() + ">");
                    } else {
                        System.out.println(e.paramString() + " on <Non-testing component>");
                    }
                }
            }, AWTEvent.WINDOW_EVENT_MASK | AWTEvent.WINDOW_FOCUS_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);

        setSize (500, 200);
        setVisible(true);
        validate();

        frame.setSize(new Dimension(400, 100));
        frame.setLocation(800, 400);
        frame.setVisible(true);
        frame.toFront();

        owner.setLayout(new FlowLayout());
        owner.add(testButton1);
        owner.add(otherButton1);
        owner.pack();
        owner.setLocation(800, 100);
        owner.setSize(new Dimension(400, 100));
        owner.setVisible(true);
        owner.toFront();
        Util.waitTillShown(owner);

        window1.setVisible(true);
        window2.setVisible(true);
        window1.toFront();
        window2.toFront();
        // Wait longer...
        Util.waitTillShown(window1);
        Util.waitTillShown(window2);

        test();

        frame.dispose();
        owner.dispose();
    }

    public void test() {
        Button[] butArr = new Button[] {testButton3, testButton2, testButton1};
        Window[] winArr = new Window[] {window2, window1, owner};

        step = 1;
        for (int i = 0; i < 3; i++) {
            clickInSeriesCheckFocus(null, butArr[i], frame);
            clickOwnerCheckFocus(winArr[i], butArr[i]);
            step++;
        }

        step = 4;
        clickInSeriesCheckFocus(testButton3, testButton1, frame);
        clickOwnerCheckFocus(owner, testButton1);

        step = 5;
        clickInSeriesCheckFocus(testButton3, testButton2, frame);
        clickOwnerCheckFocus(window1, testButton2);

        step = 6;
        clickInSeriesCheckFocus(testButton1, testButton2, frame);
        clickOwnerCheckFocus(window1, testButton2);

        step = 7;
        clickInSeriesCheckFocus(testButton1, testButton2, frame);
        window1.setVisible(false);
        Util.waitForIdle(robot);
        clickOwnerCheckFocus(owner, testButton1);

        step = 8;
        window1.setVisible(true);
        Util.waitTillShown(window1);
        clickInSeriesCheckFocus(null, testButton2, frame);
        clickOwnerCheckFocus(window1, testButton2);
    }

    boolean checkFocusOwner(Component comp) {
        return (comp == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
    }

    boolean checkFocusedWindow(Window win) {
        return (win == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow());
    }

    void clickOwnerCheckFocus(Window focusedWindow, Component focusedComp) {
        Util.clickOnTitle(owner, robot);
        robot.delay(500);

        if (!checkFocusedWindow(focusedWindow)) {
            stopTest("Test failed: actual focused window didn't get a focus");
        }
        if (!checkFocusOwner(focusedComp)) {
            stopTest("Test failed: actual focus owner didn't get a focus");
        }
    }

    void clickInSeriesCheckFocus(Component comp1, Component comp2, Frame frame) {
        if (comp1 != null) {
            clickOnCheckFocusOwner(comp1);
        }
        if (comp2 != null) {
            clickOnCheckFocusOwner(comp2);
        }
        clickOnCheckFocusedWindow(frame);
    }

    void clickOnCheckFocusOwner(Component c) {
        Util.clickOnComp(c, robot);
        robot.delay(500);

        if (!checkFocusOwner(c)) {
            stopTest("Error: can't bring a focus on Component by clicking on it");
        }
    }

    void clickOnCheckFocusedWindow(Frame f) {
        Util.clickOnTitle(f, robot);
        robot.delay(500);

        if (!checkFocusedWindow(f)) {
            stopTest("Error: can't bring a focus on Frame by clicking on it");
        }
    }

    void stopTest(String msg) {
        throw new RuntimeException(new String("Step " + step + ": " + msg));
    }
}

class TestWindow extends Window {
    TestWindow(Frame owner, Button otherButton, Button testButton, int x, int y) {
        super(owner);

        setLayout(new FlowLayout());
        setLocation(x, y);
        add(testButton);
        add(otherButton);
        pack();
        setBackground(Color.green);
    }
}