001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.forms.layout;
032:
033: import javax.swing.JLabel;
034:
035: import junit.framework.TestCase;
036:
037: import com.jgoodies.forms.factories.DefaultComponentFactory;
038:
039: /**
040: * A test case for class {@link DefaultComponentFactory}.
041: *
042: * @author Karsten Lentzsch
043: * @version $Revision: 1.3 $
044: */
045: public final class DefaultComponentFactoryTest extends TestCase {
046:
047: public void testLabelWithoutMnemonic() {
048: testMnemonic("", "", 0, -1);
049: testMnemonic(" ", " ", 0, -1);
050: testMnemonic(" ", " ", 0, -1);
051: testMnemonic(" ", " ", 0, -1);
052: testMnemonic("a", "a", 0, -1);
053: testMnemonic("a ", "a ", 0, -1);
054: testMnemonic(" a", " a", 0, -1);
055: testMnemonic(" a ", " a ", 0, -1);
056: testMnemonic("abcd", "abcd", 0, -1);
057: testMnemonic("abcd ", "abcd ", 0, -1);
058: }
059:
060: public void testLabelWithSingleMnemonicMarker() {
061: testMnemonic("&x", "x", 'X', 0);
062: testMnemonic("a&x", "ax", 'X', 1);
063: testMnemonic("ab&x", "abx", 'X', 2);
064: testMnemonic("abc&x", "abcx", 'X', 3);
065: testMnemonic("abc&x ", "abcx ", 'X', 3);
066: testMnemonic("x&x", "xx", 'X', 1);
067: testMnemonic("xa&x", "xax", 'X', 2);
068: testMnemonic("xab&x", "xabx", 'X', 3);
069: testMnemonic("xabc&x", "xabcx", 'X', 4);
070: testMnemonic("xabc&x ", "xabcx ", 'X', 4);
071: testMnemonic("X&x", "Xx", 'X', 1);
072: testMnemonic("Xa&x", "Xax", 'X', 2);
073: testMnemonic("Xab&x", "Xabx", 'X', 3);
074: testMnemonic("Xabc&x", "Xabcx", 'X', 4);
075: testMnemonic("Xabc&x ", "Xabcx ", 'X', 4);
076: }
077:
078: public void testLabelWithSingleQuotedMnemonicMarker() {
079: testMnemonic("&&", "&", 0, -1);
080: testMnemonic("a&&", "a&", 0, -1);
081: testMnemonic("ab&&", "ab&", 0, -1);
082: testMnemonic("abc&&", "abc&", 0, -1);
083: testMnemonic("abc&& ", "abc& ", 0, -1);
084: testMnemonic("abc&&a", "abc&a", 0, -1);
085: testMnemonic("abc&&ab", "abc&ab", 0, -1);
086: testMnemonic("abc&&ab ", "abc&ab ", 0, -1);
087: }
088:
089: public void testLabelWithMultipleMnemonicMarkers() {
090: testMnemonic("&x&x", "xx", 'X', 0);
091: testMnemonic("a&x&x", "axx", 'X', 1);
092: testMnemonic("ab&x&x", "abxx", 'X', 2);
093: testMnemonic("abc&x&x", "abcxx", 'X', 3);
094: testMnemonic("abc&x &x", "abcx x", 'X', 3);
095: testMnemonic("x&x&x", "xxx", 'X', 1);
096: testMnemonic("xa&x&x", "xaxx", 'X', 2);
097: testMnemonic("xab&x&x", "xabxx", 'X', 3);
098: testMnemonic("xabc&x&x", "xabcxx", 'X', 4);
099: testMnemonic("xabc&x &x", "xabcx x", 'X', 4);
100: testMnemonic("X&x&x", "Xxx", 'X', 1);
101: testMnemonic("Xa&x&x", "Xaxx", 'X', 2);
102: testMnemonic("Xab&x&x", "Xabxx", 'X', 3);
103: testMnemonic("Xabc&x&x", "Xabcxx", 'X', 4);
104: testMnemonic("Xabc&x &x", "Xabcx x", 'X', 4);
105: }
106:
107: public void testLabelWithQuotedMarkerBeforeMnemonic() {
108: testMnemonic("L&&F &choice", "L&F choice", 'C', 4);
109: testMnemonic("&&&x", "&x", 'X', 1);
110: testMnemonic("&&a&x", "&ax", 'X', 2);
111: testMnemonic("&&ab&x", "&abx", 'X', 3);
112: testMnemonic("&&abc&x", "&abcx", 'X', 4);
113: testMnemonic("&&abc&x ", "&abcx ", 'X', 4);
114: testMnemonic("&&x&x", "&xx", 'X', 2);
115: testMnemonic("&&xa&x", "&xax", 'X', 3);
116: testMnemonic("&&xab&x", "&xabx", 'X', 4);
117: testMnemonic("&&xabc&x", "&xabcx", 'X', 5);
118: testMnemonic("&&xabc&x ", "&xabcx ", 'X', 5);
119: testMnemonic("&&X&x", "&Xx", 'X', 2);
120: testMnemonic("X&&a&x", "X&ax", 'X', 3);
121: testMnemonic("X&&ab&x", "X&abx", 'X', 4);
122: testMnemonic("X&&abc&x", "X&abcx", 'X', 5);
123: testMnemonic("X&&abc&x ", "X&abcx ", 'X', 5);
124: }
125:
126: public void testLabelWithQuotedMarkerAfterMnemonic() {
127: testMnemonic("&Look&&Feel choice", "Look&Feel choice", 'L', 0);
128: testMnemonic("&x&&", "x&", 'X', 0);
129: testMnemonic("a&x&&", "ax&", 'X', 1);
130: testMnemonic("ab&x&&", "abx&", 'X', 2);
131: testMnemonic("abc&x&&", "abcx&", 'X', 3);
132: testMnemonic("abc&x &&", "abcx &", 'X', 3);
133: testMnemonic("a&x&&b", "ax&b", 'X', 1);
134: testMnemonic("a&xb&&c", "axb&c", 'X', 1);
135: testMnemonic("a&xbc&&", "axbc&", 'X', 1);
136: testMnemonic("a&xb&&c", "axb&c", 'X', 1);
137: }
138:
139: public void testLabelWithQuotedMarkersBeforeAndAfterMnemonic() {
140: testMnemonic("a&&b&&c&&d&x&&e&&f&&", "a&b&c&dx&e&f&", 'X', 7);
141: testMnemonic("a&&b&&c&&d&x&&e&&f&&g", "a&b&c&dx&e&f&g", 'X', 7);
142: }
143:
144: // Test Helper Code *******************************************************
145:
146: private void testMnemonic(String textWithMnemonic,
147: String expectedText, int expectedMnemonic,
148: int expectedMnemonicIndex) {
149: JLabel label = DefaultComponentFactory.getInstance()
150: .createLabel(textWithMnemonic);
151: assertEquals("Text", expectedText, label.getText());
152: assertEquals("Mnemonic", expectedMnemonic, label
153: .getDisplayedMnemonic());
154: assertEquals("Mnemonic index", expectedMnemonicIndex, label
155: .getDisplayedMnemonicIndex());
156: }
157:
158: }
|