001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package javax.microedition.lcdui;
028:
029: import com.sun.midp.i3test.*;
030:
031: public class TestGraphicsClipping extends TestCase {
032:
033: static final int BACKGROUND_COLOR = 0xffffffff;
034: int width = 10;
035: int height = 10;
036: int backgroundColor = 0;
037:
038: void draw(Graphics g) {
039: g.setColor(0);
040: g.fillRect(0, 0, width, height);
041: }
042:
043: void testOne() {
044: int[] unclippedData = new int[width * height];
045: int[] clippedData = new int[width * height];
046:
047: Image image = Image.createImage(width, height);
048: Graphics g = image.getGraphics();
049:
050: backgroundColor = g.getDisplayColor(BACKGROUND_COLOR);
051:
052: for (int cx = 1; cx < width; cx++) {
053: for (int cy = 1; cy < height; cy++) {
054: for (int cw = 1; cw < width - cx; cw++) {
055: for (int ch = 1; ch < height - cy; ch++) {
056: g.setClip(0, 0, width, height);
057: g.setColor(backgroundColor);
058: g.fillRect(0, 0, width, height);
059: draw(g);
060: image.getRGB(unclippedData, 0, width, 0, 0,
061: width, height);
062:
063: g.setColor(backgroundColor);
064: g.fillRect(0, 0, width, height);
065: g.setClip(cx, cy, cw, ch);
066: draw(g);
067: image.getRGB(clippedData, 0, width, 0, 0,
068: width, height);
069:
070: boolean result = verifyClip(unclippedData,
071: clippedData, cx, cy, cw, ch);
072:
073: assertTrue(result);
074: }
075: }
076: }
077: }
078: }
079:
080: boolean verifyClip(int[] unclippedData, int[] clippedData, int cx,
081: int cy, int cw, int ch) {
082: for (int y = 0; y < height; y++) {
083: for (int x = 0; x < width; x++) {
084: int index = y * width + x;
085: if (y >= cy && y < cy + ch && x >= cx && x < cx + cw) {
086: /* should match unclipped version when alpha is ignored */
087: if ((clippedData[index]) != (unclippedData[index])) {
088: System.out
089: .println("FAILURE: inside clip ("
090: + x
091: + ","
092: + y
093: + ")"
094: + " clipped="
095: + Integer
096: .toHexString(clippedData[index])
097: + " unclipped="
098: + Integer
099: .toHexString(unclippedData[index]));
100: return false;
101: }
102: } else {
103: /* should be background color when alpha is ignored */
104: if ((clippedData[index] & 0x00ffffff) != (backgroundColor & 0xffffff)) {
105: System.out
106: .println("FAILURE: outside clip ("
107: + x
108: + ","
109: + y
110: + ")"
111: + " clipped="
112: + Integer
113: .toHexString(clippedData[index] & 0xffffff)
114: + " backgroundColor="
115: + Integer
116: .toHexString(backgroundColor & 0xffffff));
117: return false;
118: }
119: }
120: }
121: }
122:
123: return true;
124: }
125:
126: public void runTests() {
127: declare("testOne");
128: testOne();
129: }
130: }
|