File: FocusTraversalPolicyProviderTest.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 (509 lines) | stat: -rw-r--r-- 19,523 bytes parent folder | download | duplicates (13)
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
/*
 * Copyright (c) 2001, 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.
 */

/*
  @test
  @summary unit test for ability of FocusTraversalPolicyProvider
  @key headful
*/

import java.awt.Button;
import java.awt.Component;
import java.awt.Container;
import java.awt.ContainerOrderFocusTraversalPolicy;
import java.awt.DefaultFocusTraversalPolicy;
import java.awt.EventQueue;
import java.awt.FocusTraversalPolicy;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.Robot;
import java.awt.Window;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.LayoutFocusTraversalPolicy;

public class FocusTraversalPolicyProviderTest {
    final String errorOrderMessage = "Test Failed. Traversal Order not correct.";
    final String successStage = "Test stage completed.Passed.";

    final int n_buttons = 4;
    final int jumps = 3 * n_buttons;
    Container[] cycle_roots = new Container[3];
    Panel[] a_conts = new Panel[cycle_roots.length];
    Panel[] b_conts = new Panel[cycle_roots.length];
    Component[][] a_buttons = new Component[cycle_roots.length][n_buttons];
    Component[][] b_buttons = new Component[cycle_roots.length][n_buttons];

    static volatile Frame mainFrame = null;
    static volatile Frame frame = null;
    static volatile JFrame jframe = null;

    static Robot robot;

    public static void main(String[] args) throws Exception {
        FocusTraversalPolicyProviderTest test
                = new FocusTraversalPolicyProviderTest();
        try {
            robot = new Robot();
            EventQueue.invokeAndWait(test::init);
            robot.delay(1000);
            EventQueue.invokeAndWait(test::testStages);

            EventQueue.invokeAndWait(test::initSwingContInFrame);
            robot.delay(1000);
            EventQueue.invokeAndWait(test::testSwingContInFrame);
            // test for Swing container in java.awt.Frame
            System.out.println("Test passed.");
        } finally {
            EventQueue.invokeAndWait(() -> {
                if (mainFrame != null) mainFrame.dispose();
                if (frame != null) frame.dispose();
                if (jframe != null) jframe.dispose();
            });
        }
    }

    public void init() {
        mainFrame = new Frame("FocusTraversalPolicyProviderTest - main");
        mainFrame.setSize(400, 400);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);

        for (int i = 0; i < cycle_roots.length; i++) {
            cycle_roots[i] = new Panel();
            cycle_roots[i].setFocusable(false);
            cycle_roots[i].setName("root" + i);
            cycle_roots[i].setFocusCycleRoot(true);
            cycle_roots[i].setLayout (new GridLayout(1, 2));
            mainFrame.add(cycle_roots[i]);

            a_conts[i] = new Panel();
            a_conts[i].setName("ac" + i);
            a_conts[i].setFocusable(false);
            cycle_roots[i].add(a_conts[i]);

            b_conts[i] = new Panel();
            b_conts[i].setName("bc" + i);
            b_conts[i].setFocusable(false);
            cycle_roots[i].add(b_conts[i]);

            for (int j = 0; j < n_buttons; j++){
                String name = "a" + i + "x" + j;
                a_buttons[i][j] = new Button(name);
                a_buttons[i][j].setName(name);
                a_conts[i].add(a_buttons[i][j]);
            }

            for (int j = 0; j < n_buttons; j++){
                String name = "b" + i + "x" + j;
                b_buttons[i][j] = new Button(name);
                b_buttons[i][j].setName(name);
                b_conts[i].add(b_buttons[i][j]);
            }
        }

        cycle_roots[0].setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
        cycle_roots[1].setFocusTraversalPolicy(new ContainerOrderFocusTraversalPolicy());
        cycle_roots[2].setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
    }

    public void testStages() {
        for (int i = 0; i < cycle_roots.length; i++) {
            testStage(cycle_roots[i], a_conts[i], b_conts[i],
                    a_buttons[i], b_buttons[i]);
        }
    }

    void testStage(Container aFCR, Container aCont, Container bCont,
                   Component[] a_comps, Component[] b_comps) {
            System.out.println("focus cycle root = " + aFCR.getName());
            System.out.println("policy = " + aFCR.getFocusTraversalPolicy());
            System.out.println("aContainer = " + aCont.getName());
            System.out.println("bContainer = " + bCont.getName());
            System.out.println("Both containers are not Providers.");

            Component[] a_comps_backward = revertArray(a_comps);
            Component[] b_comps_backward = revertArray(b_comps);

            testForwardStage(aFCR, aCont, bCont,
                    false, a_comps, false, b_comps);
            testBackwardStage(aFCR, aCont, bCont,
                    false, a_comps_backward,
                    false, b_comps_backward);

            System.out.println("Both containers are Providers.");
            testForwardStage(aFCR, aCont, bCont,
                    true, a_comps, true, b_comps);
            testForwardStage(aFCR, aCont, bCont,
                             true, shakeArray(a_comps),
                             true, shakeArray(b_comps));

            testBackwardStage(aFCR, aCont, bCont,
                              true, a_comps_backward,
                    true, b_comps_backward);
            testBackwardStage(aFCR, aCont, bCont,
                              true, shakeArray(a_comps_backward),
                              true, shakeArray(b_comps_backward));

            System.out.println("aContainer.isProvider = true. "
                    + "bContainer.isProvider = false.");
            testForwardStage(aFCR, aCont, bCont,
                             true, a_comps, false, b_comps);
            testForwardStage(aFCR, aCont, bCont,
                             true, shakeArray(a_comps),
                    false, b_comps);
            testBackwardStage(aFCR, aCont, bCont,
                              true, a_comps_backward,
                    false, b_comps_backward);
            testBackwardStage(aFCR, aCont, bCont,
                              true, shakeArray(a_comps_backward),
                              false, b_comps_backward);

            System.out.println("aContainer.isProvider = false. "
                    + "bContainer.isProvider = true.");
            testForwardStage(aFCR, aCont, bCont,
                             false, a_comps,
                    true, b_comps);
            testForwardStage(aFCR, aCont, bCont,
                             false, a_comps,
                    true, shakeArray(b_comps));
            testBackwardStage(aFCR, aCont, bCont,
                              false, a_comps_backward,
                    true, b_comps_backward);
            testBackwardStage(aFCR, aCont, bCont,
                              false, a_comps_backward,
                              true, shakeArray(b_comps_backward));

            System.out.println("Stage completed.");
    }

    public void printGoldOrder(Component[] comps) {
        String goldOrderStr = "";
        for (int i =0;i < jumps; i++){
            goldOrderStr += " " + comps[i].getName();
        }
        System.out.println("GoldOrder: " + goldOrderStr);
    }

    public void testForwardStage(Container focusCycleRoot,
                                 Container aContainer,
                                 Container bContainer,
                                 boolean aProvider, Component[] aComps,
                                 boolean bProvider, Component[] bComps) {
        System.out.println("test forward traversal");
        System.out.println("\taProvider = " + aProvider);
        System.out.println("\tbProvider = " + bProvider);
        Component[] goldOrder = new Component[2*aComps.length + bComps.length];
        System.arraycopy(aComps, 0, goldOrder, 0, aComps.length);
        System.arraycopy(bComps, 0, goldOrder,
                aComps.length, bComps.length);
        System.arraycopy(aComps, 0, goldOrder,
                aComps.length + bComps.length,
                         aComps.length);
        printGoldOrder(goldOrder);

        String jumpStr = "";
        aContainer.setFocusTraversalPolicyProvider(aProvider);
        aContainer.setFocusTraversalPolicy(
            new ArrayOrderFocusTraversalPolicy(aContainer, aComps));
        bContainer.setFocusTraversalPolicyProvider(bProvider);
        bContainer.setFocusTraversalPolicy(
            new ArrayOrderFocusTraversalPolicy(bContainer, bComps));
        FocusTraversalPolicy policy = focusCycleRoot.getFocusTraversalPolicy();
        System.out.println("policy=" + policy);
        Component current = policy.getFirstComponent(focusCycleRoot);

        for (int i = 0;i<jumps;i++){
            jumpStr += " " + current.getName();
            if (current != goldOrder[i]) {
                System.out.println("i=" + i + " label = "+ current.getName()
                               + " i%8= " + i%goldOrder.length );
                throw new RuntimeException(errorOrderMessage);
            }
            System.out.println("getComponentAfter() on " + focusCycleRoot + ", " + current);
            current = policy.getComponentAfter(focusCycleRoot, current);
            System.out.println("RealOrder :" + jumpStr);
        }
        System.out.println(successStage);
    }

    public void testBackwardStage(Container focusCycleRoot,
                                  Container aContainer,
                                  Container bContainer,
                                  boolean aProvider, Component[] aComps,
                                  boolean bProvider, Component[] bComps)
    {
        System.out.println("test backward traversal");
        System.out.println("\taProvider = " + aProvider);
        System.out.println("\tbProvider = " + bProvider);
        Component[] goldOrder = new Component[2*bComps.length + bComps.length];
        System.arraycopy(bComps, 0, goldOrder, 0, bComps.length);
        System.arraycopy(aComps, 0, goldOrder, bComps.length, aComps.length);
        System.arraycopy(bComps, 0, goldOrder,
                aComps.length + bComps.length, bComps.length);
        printGoldOrder(goldOrder);

        String jumpStr = "";
        aContainer.setFocusTraversalPolicyProvider(aProvider);
        aContainer.setFocusTraversalPolicy(
            new ArrayOrderFocusTraversalPolicy(aContainer, revertArray(aComps)));
        bContainer.setFocusTraversalPolicyProvider(bProvider);
        bContainer.setFocusTraversalPolicy(
            new ArrayOrderFocusTraversalPolicy(bContainer, revertArray(bComps)));
        FocusTraversalPolicy policy = focusCycleRoot.getFocusTraversalPolicy();
        System.out.println("policy=" + policy);
        Component current = policy.getLastComponent(focusCycleRoot);

        for (int i = 0;i<jumps;i++){
            jumpStr += " " + current.getName();
            if (current != goldOrder[i]) {
                System.out.println("i=" + i + " label = "+ current.getName());
                throw new RuntimeException(errorOrderMessage);
            }
            System.out.println("getComponentBefore() on "
                    + focusCycleRoot.getName() + ", " + current.getName());
            current = policy.getComponentBefore(focusCycleRoot, current);
            System.out.println("RealOrder :" + jumpStr);
        }
        System.out.println(successStage);
    }

    Component[] shakeArray(Component[] comps) {
        Component[] new_comps = new Component[comps.length];
        System.arraycopy(comps, 0, new_comps, 0, comps.length);
        new_comps[0] = comps[1];
        new_comps[1] = comps[0];
        return new_comps;
    }

    Component[] revertArray(Component[] comps) {
        Component[] new_comps = new Component[comps.length];
        for (int i=0; i < comps.length; i++) {
            new_comps[i] = comps[comps.length - 1 - i];
        }

        return new_comps;
    }

    public void initSwingContInFrame()  {
        System.out.println("test Swing policy provider in AWT Frame.");

        jframe = new JFrame("FocusTraversalPolicyProviderTest - JFrame");
        jframe.setName("JFrame");
        JPanel panel1 = createPanel();
        jframe.getContentPane().add(panel1);

        frame = new Frame("FocusTraversalPolicyProviderTest - Frame");
        frame.setName("Frame");
        JPanel panel2 = createPanel();
        panel2.setFocusTraversalPolicyProvider(true);
        panel2.setFocusTraversalPolicy(jframe.getFocusTraversalPolicy());
        frame.add(panel2);

        jframe.pack();
        jframe.setVisible(true);
        frame.pack();
        frame.setVisible(true);
    }

    public void testSwingContInFrame() {
        FocusTraversalPolicy policy = frame.getFocusTraversalPolicy();
        FocusTraversalPolicy jpolicy = jframe.getFocusTraversalPolicy();

        System.out.println("policy = " + policy);
        System.out.println("jpolicy = " + jpolicy);

        assertEquals("Different default components.",
                jpolicy.getDefaultComponent(jframe),
                policy.getDefaultComponent(frame));

        assertEquals("Different first components.",
                jpolicy.getFirstComponent(jframe),
                policy.getFirstComponent(frame));

        assertEquals("Different last components.",
                jpolicy.getLastComponent(jframe),
                policy.getLastComponent(frame));

        System.out.println("test forward traversal order.");

        Component jcur = jpolicy.getFirstComponent(jframe);
        Component cur = jpolicy.getFirstComponent(frame);

        for (int i = 0; i < 2 * n_buttons; i++) {
            assertEquals("Wrong sequence (step=" + i + ")",
                    jcur, cur);
            jcur = jpolicy.getComponentAfter(jframe, jcur);
            cur = policy.getComponentAfter(frame, cur);
        }

        System.out.println("test backward traversal order.");

        jcur = jpolicy.getLastComponent(jframe);
        cur = jpolicy.getLastComponent(frame);

        for (int i = 0; i < 2 * n_buttons; i++) {
            assertEquals("Wrong sequence (step=" + i + ")",
                    jcur, cur);
            jcur = jpolicy.getComponentBefore(jframe, jcur);
            cur = policy.getComponentBefore(frame, cur);
        }
    }

    public void assertEquals(String msg, Component expected, Component actual) {
        if (expected == null && actual != null
            || actual == null && expected != null)
        {
            throw new RuntimeException(msg + "(expected=" + expected
                                       + ", actual=" + actual + ")");
        }

        String expected_name = expected.getName();
        String actual_name = actual.getName();

        if ((expected_name != null && !expected_name.equals(actual_name))
            || (actual_name != null && !actual_name.equals(expected_name)))
        {
            throw new RuntimeException(msg + "(expected_name=" + expected_name
                                       + ", actual_name=" + actual_name + ")");
        }
    }

    public JPanel createPanel() {
        JPanel pane = new JPanel();
        pane.setName("jpanel");
        for (int i = 0; i < n_buttons; i++) {
            JButton btn = new JButton("jbtn" + i);
            btn.setName("jbtn" + i);
            pane.add(btn);
        }
        return pane;
    }
}

class ArrayOrderFocusTraversalPolicy extends FocusTraversalPolicy {

    final Component[] comps;
    final Container cont;

    public ArrayOrderFocusTraversalPolicy(Container aCont, Component[] aComps) {
        if (aCont == null) {
            throw new NullPointerException("aCont is null.");
        }
        cont = aCont;
        comps = new Component[aComps.length];
        for (int i = 0; i < comps.length; i++) {
            comps[i] = aComps[i];
        }
    }

    private void checkContainer(Container aCont) {
        if (aCont != cont) {
            System.err.println("aCont = " + aCont);
            System.err.println("cont = " + cont);
            throw new IllegalArgumentException(
                    "Policy is not registered for this container.");
        }
    }

    private int findIndex(Component aComp) {
        for (int i = 0; i < comps.length; i++) {
            if (aComp == comps[i]) {
                return i;
            }
        }

        return -1;
    }

    public Component getComponentAfter(Container focusCycleRoot,
                                       Component aComponent) {
        checkContainer(focusCycleRoot);

        int current_index = findIndex(aComponent);
        if (current_index < 0) {
            return null;
        }

        current_index++;

        if (current_index < comps.length) {
            return comps[current_index];
        }

        if (focusCycleRoot.isFocusCycleRoot()) {
            return getFirstComponent(focusCycleRoot);
        } else {
            return null;
        }
    }

    public Component getComponentBefore(Container focusCycleRoot,
                                        Component aComponent) {
        checkContainer(focusCycleRoot);

        int current_index = findIndex(aComponent);
        if (current_index < 0) {
            return null;
        }

        current_index--;

        if (current_index >= 0) {
            return comps[current_index];
        }

        if (focusCycleRoot.isFocusCycleRoot()) {
            return getLastComponent(focusCycleRoot);
        } else {
            return null;
        }
    }

    public Component getFirstComponent(Container focusCycleRoot) {
        checkContainer(focusCycleRoot);
        return comps[0];
    }

    public Component getLastComponent(Container focusCycleRoot) {
        checkContainer(focusCycleRoot);
        return comps[comps.length - 1];
    }

    public Component getDefaultComponent(Container focusCycleRoot) {
        return getFirstComponent(focusCycleRoot);
    }

    public Component getInitialComponent(Window window) {
        throw new UnsupportedOperationException("getInitialComponent() is not supported.");
    }

    public Component[] getCycle(Container focusCycleRoot) {
        checkContainer(focusCycleRoot);
        Component[] temp = new Component[comps.length];
        System.arraycopy(comps, 0, temp, 0, comps.length);
        return temp;
    }
}