0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * <p> This class is used to test a focus traversal policy implementation.
0N/A * <p> When using jtreg you should include this class via something like:
0N/A * <pre>
0N/A * @library ../../../regtesthelpers
0N/A * @build AbstractPolicyTest
0N/A * @run main YourTest
0N/A * </pre>
0N/A * <p> And put "import test.java.awt.regtesthelpers.AbstractPolicyTest;" into the test.
0N/A */
0N/A
0N/Apackage test.java.awt.regtesthelpers;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Map;
0N/A
0N/Apublic abstract class AbstractPolicyTest {
0N/A
0N/A /** Creates a new instance of AbstractPolicyTest */
0N/A protected AbstractPolicyTest() {
0N/A }
0N/A
0N/A Map<String, Component> registered_comps = new HashMap<String, Component>();
0N/A
0N/A protected abstract Frame createFrame();
0N/A protected abstract void customizeHierarchy();
0N/A
0N/A protected abstract Map<String, String> getForwardOrder();
0N/A protected abstract Map<String, String> getBackwardOrder();
0N/A
0N/A protected abstract String[] getContainersToTest();
0N/A protected abstract String getDefaultComp(String focusCycleRoot_id);
0N/A protected abstract String getFirstComp(String focusCycleRoot_id);
0N/A protected abstract String getLastComp(String focusCycleRoot_id);
0N/A
0N/A protected final Component registerComponent(final String id, final Component comp) {
0N/A if (registered_comps.containsKey(id)) {
0N/A throw new RuntimeException("The component with id (" + id + "), already registered.");
0N/A }
0N/A comp.setName(id);
0N/A registered_comps.put(id, comp);
0N/A return comp;
0N/A }
0N/A
0N/A public void testIt() {
0N/A Frame frame = createFrame();
0N/A customizeHierarchy();
0N/A try {
0N/A frame.pack();
0N/A frame.setVisible(true);
0N/A testPolicy(getForwardOrder(), getBackwardOrder());
0N/A } finally {
0N/A frame.dispose();
0N/A }
0N/A }
0N/A
0N/A void testPolicy(final Map<String, String> forward_order, final Map<String, String> backward_order)
0N/A {
0N/A if (getContainersToTest() != null)
0N/A for (String cont_id : getContainersToTest()) {
0N/A final Container cont = (Container) getComponent(cont_id);
0N/A FocusTraversalPolicy policy = cont.getFocusTraversalPolicy();
0N/A assertEquals(cont_id, "Test default component", getComponent(getDefaultComp(cont_id)), policy.getDefaultComponent(cont));
0N/A assertEquals(cont_id, "Test first component", getComponent(getFirstComp(cont_id)), policy.getFirstComponent(cont));
0N/A assertEquals(cont_id, "Test last component", getComponent(getLastComp(cont_id)), policy.getLastComponent(cont));
0N/A }
0N/A if (forward_order != null)
0N/A for (String key : forward_order.keySet()) {
0N/A final Component current = getComponent(key);
0N/A final Component next = getComponent(forward_order.get(key));
0N/A Container focusCycleRoot = current.getParent() == null ? (Container)current : current.getFocusCycleRootAncestor();
0N/A FocusTraversalPolicy policy = focusCycleRoot.getFocusTraversalPolicy();
0N/A assertEquals(null, "Test getComponentAfter() for " + key, next, policy.getComponentAfter(focusCycleRoot, current));
0N/A }
0N/A if (backward_order != null)
0N/A for (String key : backward_order.keySet()) {
0N/A final Component current = getComponent(key);
0N/A final Component previous = getComponent(backward_order.get(key));
0N/A Container focusCycleRoot = current.getParent() == null ? (Container)current : current.getFocusCycleRootAncestor();
0N/A FocusTraversalPolicy policy = focusCycleRoot.getFocusTraversalPolicy();
0N/A assertEquals(null, "Test getComponentBefore() for " + key, previous, policy.getComponentBefore(focusCycleRoot, current));
0N/A }
0N/A }
0N/A
0N/A protected final Component getComponent(final String id) {
0N/A if (!registered_comps.containsKey(id)) {
0N/A throw new RuntimeException("There is no registered component with given id(" + id +")");
0N/A }
0N/A return registered_comps.get(id);
0N/A }
0N/A
0N/A void assertEquals(final String message, final Object expected, final Object actual) {
0N/A assertEquals(null, message, expected, actual);
0N/A }
0N/A
0N/A void assertEquals(final String cont_id, final String message, final Object expected, final Object actual) {
0N/A if (actual == null && expected == null
0N/A || actual != null && actual.equals(expected))
0N/A {
0N/A // every thing ok.
0N/A return;
0N/A }
0N/A throw new RuntimeException((cont_id != null ? (cont_id + ": ") : "") + message +
0N/A "(actual = " + actual + ", expected = " + expected + ")");
0N/A }
0N/A}