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 Denis M. Kishenko
019: * @version $Revision$
020: */package org.apache.harmony.awt.gl;
021:
022: import java.awt.Color;
023: import java.awt.Graphics;
024: import java.awt.Rectangle;
025: import java.awt.Tools;
026: import java.awt.geom.PathIteratorTestCase;
027: import java.awt.image.BufferedImage;
028: import java.net.URL;
029:
030: import org.apache.harmony.awt.gl.MultiRectArea;
031:
032: public class MultiRectAreaTestCase extends PathIteratorTestCase {
033:
034: static Color colorBack = Color.white;
035: static Color colorOverride = Color.black;
036:
037: String shapePath, outputPath;
038:
039: public MultiRectAreaTestCase(String name) {
040: super (name);
041:
042: String classPath = "../resources/shapes/"
043: + Tools.getClasstPath(this .getClass());
044: URL url = ClassLoader.getSystemClassLoader().getResource(
045: classPath);
046:
047: assertNotNull("Path not found " + classPath, url);
048: shapePath = url.getPath();
049: outputPath = shapePath + "output/";
050: }
051:
052: Color getRectColor(int index) {
053: return Tools.MultiRectArea.color[index
054: % Tools.MultiRectArea.color.length];
055: }
056:
057: private String concat(String msg1, String msg2) {
058: if (msg1 == null) {
059: return msg2;
060: }
061: return msg1 + " " + msg2;
062: }
063:
064: public void checkValidation(MultiRectArea area, String areaName) {
065: Rectangle[] rect = area.getRectangles();
066: if (rect.length == 0) {
067: return;
068: }
069:
070: // Check Negative values
071: for (int i = 0; i < rect.length; i++) {
072: assertTrue(
073: concat(areaName, "Negative or zero width #" + i),
074: rect[i].width > 0);
075: assertTrue(
076: concat(areaName, "Negative or zero height #" + i),
077: rect[i].height > 0);
078: }
079:
080: // Check order
081: for (int i = 1; i < rect.length; i++) {
082: if (rect[i - 1].y > rect[i].y) {
083: fail(concat(areaName, "Invalid Y order #" + i));
084: }
085: if (rect[i - 1].y == rect[i].y) {
086: if (rect[i - 1].x + rect[i - 1].width > rect[i].x) {
087: fail(concat(areaName, "Invalid X order #" + i));
088: }
089: }
090: }
091:
092: // Check override
093: Rectangle bounds = area.getBounds();
094: int width = bounds.x + bounds.width + 50;
095: int height = bounds.y + bounds.height + 50;
096: BufferedImage img = new BufferedImage(width, height,
097: BufferedImage.TYPE_INT_ARGB);
098: Graphics g = img.getGraphics();
099: g.setColor(colorBack);
100: g.fillRect(0, 0, width, height);
101:
102: boolean override = false;
103: for (int i = 0; i < rect.length; i++) {
104:
105: // Fill rect
106: for (int x = rect[i].x; x < rect[i].x + rect[i].width; x++) {
107: for (int y = rect[i].y; y < rect[i].y + rect[i].height; y++) {
108: if (img.getRGB(x, y) == colorBack.getRGB()) {
109: img.setRGB(x, y, getRectColor(i).getRGB());
110: } else {
111: override = true;
112: img.setRGB(x, y, colorOverride.getRGB());
113: }
114: }
115: }
116: }
117:
118: if (areaName != null) {
119: Tools.BufferedImage.saveIcon(img, outputPath + areaName
120: + ".ico");
121: }
122:
123: if (override) {
124: fail(concat(areaName, "Overrided rectangles"));
125: }
126: }
127:
128: public void checkArea(String areaName, MultiRectArea area, int[] buf) {
129: checkValidation(area, areaName);
130: Rectangle[] rect = new Rectangle[buf.length / 4];
131: int j = 0;
132: for (int i = 0; i < rect.length; i++) {
133: rect[i] = new Rectangle(buf[j], buf[j + 1], buf[j + 2]
134: - buf[j] + 1, buf[j + 3] - buf[j + 1] + 1);
135: j += 4;
136: }
137: assertEquals(areaName, rect, area.getRectangles());
138: }
139:
140: String getSubArray(int[] buf, int k) {
141: int i1 = Math.max(k - 2, 0);
142: int i2 = Math.min(k + 2, buf.length - 1);
143: String s = "";
144: for (int i = i1; i <= i2; i++) {
145: if (i == k) {
146: s += "[" + buf[i] + "]";
147: } else {
148: s += buf[i];
149: }
150: if (i < i2) {
151: s += ",";
152: }
153: }
154: if (i1 > 0) {
155: s = ".." + s;
156: }
157: if (i2 < buf.length - 1) {
158: s = s + "..";
159: }
160: return s;
161: }
162:
163: public void assertEquals(int[] a1, int a2[]) {
164: if (a1.length != a2.length) {
165: fail("Nonequal arrays length");
166: }
167: for (int i = 0; i < a1.length; i++) {
168: if (a1[i] != a2[i]) {
169: fail("Element #" + i + " expected <"
170: + getSubArray(a1, i) + "> but was <"
171: + getSubArray(a2, i) + ">");
172: }
173: }
174: }
175:
176: public void assertEquals(Rectangle[] a1, Rectangle[] a2) {
177: assertEquals(null, a1, a2);
178: }
179:
180: public void assertEquals(String areaName, Rectangle[] a1,
181: Rectangle[] a2) {
182: if (a1.length != a2.length) {
183: fail(concat(areaName, "Nonequal arrays length " + a1.length
184: + " and " + a2.length));
185: }
186: for (int i = 0; i < a1.length; i++) {
187: assertEquals(concat(areaName, "Element #" + i), a1[i],
188: a2[i]);
189: }
190: }
191:
192: }
|