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 Anton Avtamonov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Color;
023: import java.awt.Image;
024: import java.awt.image.BufferedImage;
025:
026: public class GrayFilterTest extends SwingTestCase {
027: private GrayFilter filter;
028:
029: public GrayFilterTest(final String name) {
030: super (name);
031: }
032:
033: @Override
034: protected void tearDown() throws Exception {
035: filter = null;
036: }
037:
038: public void testGrayFilter() throws Exception {
039: new GrayFilter(true, 0);
040: new GrayFilter(false, 100);
041: checkIncorrectGrayPercentage(-1);
042: checkIncorrectGrayPercentage(101);
043: }
044:
045: public void testFilterRGB() throws Exception {
046: checkRange(0, 85);
047: checkRange(10, 76);
048: checkRange(20, 68);
049: checkRange(30, 59);
050: checkRange(40, 51);
051: checkRange(50, 42);
052: checkRange(60, 34);
053: checkRange(70, 25);
054: checkRange(80, 17);
055: checkRange(90, 8);
056: checkRange(100, 0);
057: }
058:
059: public void testCreateDisabledImage() throws Exception {
060: Image original = new BufferedImage(20, 20,
061: BufferedImage.TYPE_INT_RGB);
062: Image grayed = GrayFilter.createDisabledImage(original);
063: assertNotNull(grayed);
064: assertEquals(original.getHeight(null), grayed.getHeight(null));
065: assertEquals(original.getWidth(null), grayed.getWidth(null));
066: assertEquals(20, grayed.getWidth(null));
067: assertEquals(20, grayed.getHeight(null));
068: assertNotSame(original, grayed);
069: }
070:
071: private void checkIncorrectGrayPercentage(final int percentage) {
072: try {
073: new GrayFilter(false, percentage);
074: if (isHarmony()) {
075: fail("Incorrect gray percentage is not detected");
076: }
077: } catch (final IllegalArgumentException iae) {
078: }
079: }
080:
081: private void checkRange(final int percentage,
082: final int expectedInterval) {
083: filter = new GrayFilter(false, percentage);
084: checkIsClosedTo(0, filter.filterRGB(0, 0, new Color(0, 0, 0)
085: .getRGB()));
086: checkIsClosedTo(expectedInterval, filter.filterRGB(0, 0,
087: new Color(255, 255, 255).getRGB()));
088: int lowBound = 255 * percentage / 100;
089: filter = new GrayFilter(true, percentage);
090: checkIsClosedTo(lowBound, filter.filterRGB(0, 0, new Color(0,
091: 0, 0).getRGB()));
092: checkIsClosedTo(lowBound + expectedInterval, filter.filterRGB(
093: 0, 0, new Color(255, 255, 255).getRGB()));
094: }
095:
096: private void checkIsClosedTo(final int expected, final int actualRGB) {
097: Color c = new Color(actualRGB);
098: assertEquals(c.getRed(), c.getGreen());
099: assertEquals(c.getRed(), c.getBlue());
100: assertTrue(Math.abs(c.getRed() - expected) <= 1);
101: }
102: }
|