001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text.html;
021:
022: import java.lang.reflect.Field;
023:
024: import javax.swing.BasicSwingTestCase;
025: import javax.swing.text.MutableAttributeSet;
026: import javax.swing.text.SimpleAttributeSet;
027: import javax.swing.text.html.CSS.Attribute;
028:
029: public class StyleSheet_ConvertAttr_BackgroundPositionAdvancedTest
030: extends BasicSwingTestCase {
031:
032: /**
033: * Expected values for validValues.
034: */
035: private static final String[] expectedVV = new String[] { "0% 0%",
036: "0% 0%", "50% 0%", "50% 0%", "50% 0%", "100% 0%",
037: "100% 0%", "0% 50%", "0% 50%", "0% 50%", "50% 50%",
038: "50% 50%", "100% 50%", "100% 50%", "100% 50%", "0% 100%",
039: "0% 100%", "50% 100%", "50% 100%", "50% 100%", "100% 100%",
040: "100% 100%" };
041:
042: private static final String[] validValues = StyleSheet_ConvertAttr_BackgroundPositionTest.validValues;
043: private static final String[] percentValues = StyleSheet_ConvertAttr_BackgroundPositionTest.percentValues;
044: private static final String[] otherValues = StyleSheet_ConvertAttr_BackgroundPositionTest.otherValues;
045:
046: private StyleSheet ss;
047: private MutableAttributeSet simple;
048: private Attribute cssKey;
049: private Object cssValue;
050:
051: protected void setUp() throws Exception {
052: super .setUp();
053: cssKey = Attribute.BACKGROUND_POSITION;
054: ss = new StyleSheet();
055: simple = new SimpleAttributeSet();
056: }
057:
058: public void testBackgroundPositionValidKeywords() throws Exception {
059: if (!isHarmony()) {
060: return;
061: }
062:
063: assertEquals(validValues.length, expectedVV.length);
064: for (int i = 0; i < validValues.length; i++) {
065: simple.removeAttribute(cssKey);
066: ss.addCSSAttribute(simple, cssKey, validValues[i]);
067: cssValue = simple.getAttribute(cssKey);
068: assertEquals("@ " + i, expectedVV[i], getString());
069: }
070: }
071:
072: public void testBackgroundPositionPercentage() throws Exception {
073: if (!isHarmony()) {
074: return;
075: }
076:
077: for (int i = 0; i < percentValues.length; i++) {
078: simple.removeAttribute(cssKey);
079: ss.addCSSAttribute(simple, cssKey, percentValues[i]);
080: cssValue = simple.getAttribute(cssKey);
081: assertEquals("@ " + i, getExtendedValue(percentValues[i]),
082: getString());
083: }
084: }
085:
086: public void testBackgroundPositionOther() throws Exception {
087: if (!isHarmony()) {
088: return;
089: }
090:
091: for (int i = 0; i < otherValues.length; i++) {
092: simple.removeAttribute(cssKey);
093: ss.addCSSAttribute(simple, cssKey, otherValues[i]);
094: cssValue = simple.getAttribute(cssKey);
095: assertEquals("@ " + i, getExtendedValue(otherValues[i]),
096: getString());
097: }
098: }
099:
100: private static String getExtendedValue(String value) {
101: return value.indexOf(' ') != -1 ? value : value + " 50%";
102: }
103:
104: private Object getHorz() {
105: try {
106: Field f = cssValue.getClass().getDeclaredField("horz");
107: f.setAccessible(true);
108: return f.get(cssValue);
109: } catch (IllegalAccessException e) {
110: fail(e.getMessage());
111: } catch (NoSuchFieldException e) {
112: fail(e.getMessage());
113: }
114: return null;
115: }
116:
117: private Object getVert() {
118: try {
119: Field f = cssValue.getClass().getDeclaredField("vert");
120: f.setAccessible(true);
121: return f.get(cssValue);
122: } catch (IllegalAccessException e) {
123: fail(e.getMessage());
124: } catch (NoSuchFieldException e) {
125: fail(e.getMessage());
126: }
127: return null;
128: }
129:
130: private String getString() {
131: return getHorz() + " " + getVert();
132: }
133: }
|