001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.openide.util;
042:
043: import java.awt.Color;
044: import java.awt.Image;
045: import java.awt.Transparency;
046: import java.awt.image.BufferedImage;
047: import junit.framework.*;
048: import java.lang.ref.*;
049: import java.util.*;
050:
051: /**
052: *
053: * @author Radim Kubacki
054: */
055: public class IconManagerTest extends TestCase {
056:
057: public IconManagerTest(String testName) {
058: super (testName);
059: }
060:
061: public void testMergeImages() throws Exception {
062: // test if merged image preserves alpha (#90862)
063: BufferedImage img1 = new BufferedImage(16, 16,
064: BufferedImage.TYPE_INT_ARGB);
065: // System.out.println("img1 transparency "+img1.getTransparency());
066: java.awt.Graphics2D g = img1.createGraphics();
067: Color c = new Color(255, 255, 255, 128);
068: g.setColor(c);
069: g.fillRect(0, 0, 16, 16);
070: g.dispose();
071:
072: BufferedImage img2 = new BufferedImage(1, 1,
073: BufferedImage.TYPE_INT_ARGB);
074: // System.out.println("img2 transparency "+img2.getTransparency());
075: g = img2.createGraphics();
076: c = new Color(255, 255, 255);
077: g.setColor(c);
078: g.fillRect(0, 0, 2, 2);
079: g.dispose();
080:
081: Image mergedImg = IconManager.mergeImages(img1, img2, 0, 0);
082: if (!(mergedImg instanceof BufferedImage)) {
083: fail("It is assumed that mergeImages returns BufferedImage. Need to update test");
084: }
085:
086: BufferedImage merged = (BufferedImage) mergedImg;
087: // System.out.println("pixels " + Integer.toHexString(merged.getRGB(10, 10)) +", "+ Integer.toHexString(merged.getRGB(0, 0)));
088: assertNotSame("transparency has to be kept for pixel <1,1>",
089: merged.getRGB(10, 10), merged.getRGB(0, 0));
090: }
091:
092: public void testMergeBitmaskImages() throws Exception {
093: // test if two bitmask images are merged to bitmask again to avoid use of alpha channel
094: BufferedImage img1 = new BufferedImage(16, 16,
095: BufferedImage.TYPE_INT_RGB);
096: // System.out.println("img1 transparency "+img1.getTransparency());
097: java.awt.Graphics2D g = img1.createGraphics();
098: g.setColor(Color.BLUE);
099: g.fillRect(0, 0, 16, 16);
100: g.dispose();
101:
102: BufferedImage img2 = new BufferedImage(2, 2,
103: BufferedImage.TYPE_INT_RGB);
104: // System.out.println("img2 transparency "+img2.getTransparency());
105: g = img2.createGraphics();
106: g.setColor(Color.RED);
107: g.fillRect(0, 0, 2, 2);
108: g.dispose();
109:
110: Image mergedImg = IconManager.mergeImages(img1, img2, 0, 0);
111: if (!(mergedImg instanceof BufferedImage)) {
112: fail("It is assumed that mergeImages returns BufferedImage. Need to update test");
113: }
114:
115: BufferedImage merged = (BufferedImage) mergedImg;
116: assertEquals("Should create bitmask image",
117: Transparency.BITMASK, merged.getTransparency());
118: assertEquals(Color.RED, new Color(merged.getRGB(1, 1)));
119: assertEquals(Color.BLUE, new Color(merged.getRGB(10, 10)));
120: }
121:
122: }
|