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