1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.awt.Color ; 23 import javax.xml.parsers.DocumentBuilderFactory ; 24 import junit.framework.TestCase; 25 import org.openide.explorer.propertysheet.editors.XMLPropertyEditor; 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.NamedNodeMap ; 28 import org.w3c.dom.Node ; 29 30 34 public class ColorEditorTest extends TestCase { 35 36 public ColorEditorTest (String testName) { 37 super (testName); 38 } 39 40 public void testStoreToXml() throws Exception { 41 ColorEditor.SuperColor sc = new ColorEditor.SuperColor( 42 "TextField.inactiveBackground", 43 ColorEditor.SWING_PALETTE, 44 Color.BLUE); 45 System.out.println("original "+sc); 46 XMLPropertyEditor propEd = new ColorEditor(); 47 propEd.setValue(sc); 48 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 49 Node element = propEd.storeToXML(doc); 50 51 NamedNodeMap nodeMap = element.getAttributes(); 52 for (int i = 0; i < nodeMap.getLength(); i++) { 53 System.out.println("attr "+i+", "+nodeMap.item(i)); 54 } 55 56 propEd.readFromXML(element); 57 Color restoredColor = (Color )propEd.getValue(); 58 System.out.println("restoredColor "+restoredColor); 59 assertEquals("Restored value has to be the same", sc, restoredColor); 60 assertTrue("It is SuperColor", restoredColor instanceof ColorEditor.SuperColor); 61 assertEquals ("Generate Java source with UI color.", "javax.swing.UIManager.getDefaults().getColor(\"TextField.inactiveBackground\")", propEd.getJavaInitializationString ()); 62 } 63 64 public void testStoreToXmlWhenUIResourceIsMissing() throws Exception { 65 ColorEditor.SuperColor sc = new ColorEditor.SuperColor( 66 "Fake.inactiveBackground", 67 ColorEditor.SWING_PALETTE, 68 Color.BLUE); 69 System.out.println("original "+sc); 70 XMLPropertyEditor propEd = new ColorEditor(); 71 propEd.setValue(sc); 72 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 73 Node element = propEd.storeToXML(doc); 74 75 NamedNodeMap nodeMap = element.getAttributes(); 76 for (int i = 0; i < nodeMap.getLength(); i++) { 77 System.out.println("attr "+i+", "+nodeMap.item(i)); 78 } 79 80 propEd.readFromXML(element); 81 Color restoredColor = (Color )propEd.getValue(); 82 System.out.println("restoredColor "+restoredColor); 83 assertEquals("Restored value has to be the same", sc, restoredColor); 84 assertTrue("It is SuperColor", restoredColor instanceof ColorEditor.SuperColor); 85 assertEquals ("Generate Java source with UI color.", "javax.swing.UIManager.getDefaults().getColor(\"Fake.inactiveBackground\")", propEd.getJavaInitializationString ()); 86 } 87 88 public void testGetValue () throws Exception { 89 Color c = new Color (16, 16, 16); 90 System.out.println("original "+c); 91 XMLPropertyEditor propEd = new ColorEditor(); 92 propEd.setValue(c); 93 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 94 Node element = propEd.storeToXML(doc); 95 96 NamedNodeMap nodeMap = element.getAttributes(); 97 for (int i = 0; i < nodeMap.getLength(); i++) { 98 System.out.println("attr "+i+", "+nodeMap.item(i)); 99 } 100 101 propEd.readFromXML(element); 102 Color restoredColor = (Color )propEd.getValue(); 103 System.out.println("restoredColor "+restoredColor); 104 assertEquals("Restored value has to be the same", c, restoredColor); 105 assertTrue("It is Color", restoredColor instanceof Color ); 106 assertFalse("It is not SuperColor", restoredColor instanceof ColorEditor.SuperColor); 107 System.out.println("GENERATE: " + propEd.getJavaInitializationString ()); 108 assertEquals ("Generate Java source with UI color.", "new java.awt.Color(16, 16, 16)", propEd.getJavaInitializationString ()); 109 } 110 } 111 | Popular Tags |