1 /* 2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @summary This test draws lines and probes points just next to the line to see if it has been aliased correctly. 27 * @summary com.apple.junit.java.graphics.primitives 28 * @library ../../regtesthelpers 29 * @build Waypoint 30 * @run main LineAntialiasing01 31 */ 32 33 import test.java.awt.regtesthelpers.Waypoint; 34 import junit.framework.*; 35 36 import java.awt.*; 37 38 39 public class LineAntialiasing01 extends TestCase { 40 41 static final int kPaintTimeOut = 500; 42 43 public static Test suite() { 44 return new TestSuite(LineAntialiasing01.class); 45 } 46 47 protected void setUp() throws AWTException { 48 // set up the kRobot for grabbing colors 49 if (kRobot == null) { 50 kRobot = new Robot(); 51 } 52 53 // Set up probe points just off the test lines 54 if (kProbePoints == null) { 55 kProbePoints = new Point[ kRects.length * 2]; 56 57 for (int i = 0; i < kRects.length; i++ ) { 58 int midW = (int) Math.rint( kRects[i].width/2.0); 59 int midH = (int) Math.rint( kRects[i].height/2.0); 60 61 if (kRects[i].width > kRects[i].height) { 62 kProbePoints[2*i] = new Point( kRects[i].x+midW+1, kRects[i].y+midH ); 63 kProbePoints[2*i+1] = new Point( kRects[i].x+midW, kRects[i].y+midH+1 ); 64 } 65 else { 66 kProbePoints[2*i] = new Point( kRects[i].x+midW-1, kRects[i].y+midH ); 67 kProbePoints[2*i+1] = new Point( kRects[i].x+midW, kRects[i].y+midH-1 ); 68 } 69 } 70 } 71 } 72 73 class TestWindow extends Window { 74 Object antialiasing; 75 Waypoint painted; 76 77 public TestWindow( Frame f, Waypoint painted, Object antialiasing) { 78 super(f); 79 this.antialiasing = antialiasing; 80 this.painted = painted; 81 } 82 83 // Rectangle r = new Rectangle(5,5,256,64); 84 Rectangle r = new Rectangle(2,2,30,40); 85 86 public void paint(Graphics g) { 87 g.setColor(Color.white); 88 g.fillRect( 0, 0, kWidth, kWidth ); 89 90 g.setColor(Color.black); 91 ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasing); 92 93 // Draw the test lines 94 int x0,x1,y0,y1; 95 for (int i = 0; i < kRects.length; i++ ) { 96 x0 = kRects[i].x; 97 x1 = kRects[i].x+kRects[i].width; 98 y0 = kRects[i].y; 99 y1 = kRects[i].y+ kRects[i].height; 100 g.drawLine( x0, y0, x1, y1 ); 101 } 102 103 104 // For debugging -- this turns the probe points blue 105 /* 106 for (int i = 0; i < kProbePoints.length; i++ ) { 107 g.setColor( Color.blue); 108 g.fillRect( kProbePoints[i].x, kProbePoints[i].y, 1, 1 ); 109 } 110 */ 111 112 painted.clear();// paint has been called at least once, now 113 114 } 115 } 116 117 public void testDrawLinesAWT() throws Exception { 118 119 Frame dummy = new Frame(); 120 Window[] windows = new Window[3]; 121 Waypoint[] painted = new Waypoint[windows.length]; 122 123 for (int i = 0 ; i< 3; i++) { 124 125 painted[i] = new Waypoint();// this gets cleared in the paint method of the TestWindow, so we know that at least on paint() call has been made 126 127 // create the test windows 128 switch( i) { 129 case 0: windows[i] = new TestWindow(dummy, painted[i], RenderingHints.VALUE_ANTIALIAS_ON ); break; 130 case 1: windows[i] = new TestWindow(dummy, painted[i], RenderingHints.VALUE_ANTIALIAS_OFF ); break; 131 case 2: windows[i] = new TestWindow(dummy, painted[i], RenderingHints.VALUE_ANTIALIAS_DEFAULT ); break; 132 default: 133 throw new Exception("Should not get here!"); 134 135 } 136 137 windows[i].setBounds(50 + (i * (kWidth + 5)), 50, kWidth,KHeight); 138 // VisibilityValidator checkpoint = new VisibilityValidator(windows[i]); // Re-add this when VisiblityValidator handes windows (currently only handles frames) 139 windows[i].setVisible(true); 140 141 // checkpoint.requireVisible(); 142 // assertTrue( "Could not confirm test window was visible", checkpoint.isValid() ); 143 144 Thread.sleep(kPaintTimeOut); 145 146 painted[i].requireClear(); 147 assertTrue( "paint() not called on test window after " + kPaintTimeOut + "ms" , painted[i].isClear() ); 148 149 // dumpRedsFromImage(w);// <-- turn this on for debugging info 150 151 for (int ii = 0; ii<kProbePoints.length; ii++) { 152 153 // Collect the colors of points just off the test line 154 Point screen_offset = windows[i].getLocationOnScreen(); 155 int x = screen_offset.x + kProbePoints[ii].x; 156 int y = screen_offset.y + kProbePoints[ii].y; 157 Color fProbeColor= kRobot.getPixelColor(x, y); 158 159 assertNotNull( fProbeColor ); 160 switch( i) { 161 // RenderingHints.VALUE_ANTIALIAS_ON 162 case 0: 163 assertTrue("Point just off line with ANTIALIAS_ON should not be white", Color.white.equals(fProbeColor) == false); 164 break; 165 166 // RenderingHints.VALUE_ANTIALIAS_OFF 167 case 1: 168 assertEquals("Point just off line with ANTIALIAS_OFF should be white", Color.white, fProbeColor); 169 break; 170 171 // RenderingHints.VALUE_ANTIALIAS_DEFAULT 172 case 2: 173 assertEquals("For non-Aqua, point just off line with VALUE_ANTIALIAS_DEFAULT should be white", Color.white, fProbeColor); 174 break; 175 default: 176 throw new Exception("Should not get here!"); 177 178 } 179 } 180 181 } 182 183 // make sure the window is up and drawn once 184 Thread.sleep(100); // <-- do asserts here 185 186 for (int i = 0 ; i< 3; i++) { 187 windows[i].dispose(); 188 } 189 190 dummy.dispose(); 191 192 } 193 194 195 // ------------- DEBUGGING UTILITIES ---------------- 196 197 198 public static void dumpRedsFromImage( Window w) 199 { 200 Point offset = w.getLocationOnScreen(); 201 202 System.err.println(""); 203 System.err.println(""); 204 System.err.println(""); 205 206 for (int y = 0; y < w.getHeight(); y++) 207 { 208 System.err.print(" "); 209 for (int x = 0; x < w.getWidth(); x++) 210 { 211 Color rgb = kRobot.getPixelColor(offset.x+x, offset.y+ y); 212 int r = rgb.getRed(); 213 String c = (r == 0xff ? "X" : (r == 0 ? "." : "?")); 214 System.err.print(c + " "); 215 // System.err.print( r + " "); 216 } 217 System.err.println(""); 218 } 219 } 220 221 // ------------- JUnit Boilerplate ---------------- 222 223 public static void main (String[] args) throws RuntimeException { 224 TestResult tr = junit.textui.TestRunner.run(suite()); 225 if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) { 226 throw new RuntimeException("### FAILED: unexpected JUnit errors or failures."); 227 } 228 } 229 230 231 // ------------- ---------------- 232 233 234 static final int kWidth = 30; 235 static final int KHeight = 30; 236 static final RectangleKRect = new Rectangle(0,0,kWidth,KHeight); 237 238 // Some relatively mundane rectangles for use drawing test lines 239 static final Rectangle[] kRects = { 240 new Rectangle(2,2,26,26), 241 }; 242 243 protected Point[] kProbePoints = { 244 new Point(14,15), 245 new Point(15,14) 246 }; 247 248 // Some collector objects for picking colors off the screen 249 protected static Robot kRobot = null; 250 251 } 252