jdk/test/java/awt/regtesthelpers/AbstractPolicyTest.java
author amurillo
Wed, 15 Aug 2012 16:49:38 -0700
changeset 13465 d3fc5d192448
parent 5506 202f599c92aa
permissions -rw-r--r--
7191765: make jdk8 the default jprt release for hs24 Reviewed-by: jcoomes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * <p> This class is used to test a focus traversal policy implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * <p> When using jtreg you should include this class via something like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @library ../../../regtesthelpers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @build AbstractPolicyTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run main YourTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * <p> And put "import test.java.awt.regtesthelpers.AbstractPolicyTest;" into the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
package test.java.awt.regtesthelpers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public abstract class AbstractPolicyTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /** Creates a new instance of AbstractPolicyTest */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    protected AbstractPolicyTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    Map<String, Component> registered_comps = new HashMap<String, Component>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    protected abstract Frame createFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    protected abstract void customizeHierarchy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    protected abstract Map<String, String> getForwardOrder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    protected abstract Map<String, String> getBackwardOrder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    protected abstract String[] getContainersToTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    protected abstract String getDefaultComp(String focusCycleRoot_id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected abstract String getFirstComp(String focusCycleRoot_id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected abstract String getLastComp(String focusCycleRoot_id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected final Component registerComponent(final String id, final Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (registered_comps.containsKey(id)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            throw new RuntimeException("The component with id (" + id + "), already registered.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        comp.setName(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        registered_comps.put(id, comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public void testIt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        Frame frame = createFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        customizeHierarchy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            frame.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            frame.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            testPolicy(getForwardOrder(), getBackwardOrder());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            frame.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    void testPolicy(final Map<String, String> forward_order, final Map<String, String> backward_order)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (getContainersToTest() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            for (String cont_id : getContainersToTest()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                final Container cont = (Container) getComponent(cont_id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                FocusTraversalPolicy policy = cont.getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                assertEquals(cont_id, "Test default component", getComponent(getDefaultComp(cont_id)), policy.getDefaultComponent(cont));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                assertEquals(cont_id, "Test first component", getComponent(getFirstComp(cont_id)), policy.getFirstComponent(cont));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                assertEquals(cont_id, "Test last component", getComponent(getLastComp(cont_id)), policy.getLastComponent(cont));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (forward_order != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            for (String key : forward_order.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                final Component current = getComponent(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                final Component next = getComponent(forward_order.get(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                Container focusCycleRoot = current.getParent() == null ? (Container)current : current.getFocusCycleRootAncestor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                FocusTraversalPolicy policy = focusCycleRoot.getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                assertEquals(null, "Test getComponentAfter() for " + key, next, policy.getComponentAfter(focusCycleRoot, current));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (backward_order != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            for (String key : backward_order.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                final Component current = getComponent(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                final Component previous = getComponent(backward_order.get(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                Container focusCycleRoot = current.getParent() == null ? (Container)current : current.getFocusCycleRootAncestor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                FocusTraversalPolicy policy = focusCycleRoot.getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                assertEquals(null, "Test getComponentBefore() for " + key, previous, policy.getComponentBefore(focusCycleRoot, current));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected final Component getComponent(final String id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (!registered_comps.containsKey(id)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            throw new RuntimeException("There is no registered component with given id(" + id +")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return registered_comps.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    void assertEquals(final String message, final Object expected, final Object actual) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        assertEquals(null, message, expected, actual);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    void assertEquals(final String cont_id, final String message, final Object expected, final Object actual) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (actual == null && expected == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            || actual != null && actual.equals(expected))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            // every thing ok.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        throw new RuntimeException((cont_id != null ? (cont_id + ": ") : "") + message +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                   "(actual = " + actual + ", expected = " + expected + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
}