SwingOnScreenScrollingTest.java revision 430
0N/A/*
0N/A * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A/*
0N/A * @test
0N/A * @bug 6630702
0N/A * @summary Tests that scrolling after paint() is performed correctly.
0N/A * This is really only applicable to Vista
0N/A * @author Dmitri.Trembovetski@sun.com: area=Graphics
0N/A * @run main/othervm SwingOnScreenScrollingTest
0N/A * run main/othervm -Dsun.java2d.opengl=True SwingOnScreenScrollingTest
0N/A */
0N/A
0N/Aimport java.awt.AWTException;
0N/Aimport java.awt.Color;
0N/Aimport java.awt.Dimension;
0N/Aimport java.awt.EventQueue;
0N/Aimport java.awt.Graphics;
0N/Aimport java.awt.GraphicsEnvironment;
0N/Aimport java.awt.Point;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.awt.Robot;
0N/Aimport java.awt.image.BufferedImage;
0N/Aimport java.io.File;
0N/Aimport java.lang.reflect.InvocationTargetException;
0N/Aimport javax.imageio.ImageIO;
0N/Aimport javax.swing.JFrame;
0N/Aimport javax.swing.JPanel;
0N/Aimport javax.swing.JScrollPane;
0N/A
0N/Apublic class SwingOnScreenScrollingTest extends JPanel {
0N/A
0N/A static JScrollPane pane;
0N/A static SwingOnScreenScrollingTest test;
0N/A
0N/A public SwingOnScreenScrollingTest() {
0N/A }
0N/A
0N/A public static void main(String[] args) {
0N/A int size = GraphicsEnvironment.
0N/A getLocalGraphicsEnvironment().
0N/A getDefaultScreenDevice().
0N/A getDefaultConfiguration().getColorModel().getPixelSize();
0N/A if (size < 16) {
0N/A System.err.println("<16 bit display mode detected. Test PASSED");
0N/A return;
0N/A }
0N/A
0N/A final JFrame f = new JFrame("SwingOnScreenScrollingTest");
0N/A try {
0N/A EventQueue.invokeAndWait(new Runnable() {
0N/A public void run() {
0N/A test = new SwingOnScreenScrollingTest();
0N/A pane = new JScrollPane(test);
0N/A f.add(pane);
0N/A f.pack();
0N/A f.setSize(100, 200);
0N/A f.setVisible(true);
0N/A }
0N/A });
} catch (InvocationTargetException ex) {
ex.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
BufferedImage bi;
bi = new BufferedImage(100, 300,
BufferedImage.TYPE_INT_RGB);
Graphics gg = bi.getGraphics();
test.paint(gg);
for (int y = 80; y < 200; y +=10) {
test.scrollRectToVisible(new Rectangle(0, y, 100, 100));
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
Point p = pane.getViewport().getLocationOnScreen();
Robot r = null;
try {
r = new Robot();
} catch (AWTException ex) {
throw new RuntimeException(ex);
}
bi = r.createScreenCapture(new Rectangle(p.x+5, p.y+5, 30, 30));
for (int y = 0; y < bi.getHeight(); y++) {
for (int x = 0; x < bi.getHeight(); x++) {
int rgb = bi.getRGB(x, y);
if (bi.getRGB(x, y) != Color.red.getRGB()) {
System.err.printf("Test Failed at (%d,%d) c=0x%x\n",
x, y, rgb);
try {
String name =
"SwingOnScreenScrollingTest_out.png";
ImageIO.write(bi, "png", new File(name));
System.err.println("Wrote grabbed image to "+
name);
} catch (Throwable ex) {}
throw new RuntimeException("Test failed");
}
}
}
System.out.println("Test PASSED.");
f.dispose();
}
});
}
protected void paintComponent(Graphics g) {
g.setColor(Color.green);
g.fillRect(0, 0, getWidth(), 100);
g.setColor(Color.red);
g.fillRect(0, 100, getWidth(), getHeight()-100);
}
public Dimension getPreferredSize() {
return new Dimension(100, 300);
}
}