1 /*
   2  * Copyright (c) 2007, 2016, 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  * @test
  25  * @key headful
  26  * @bug 6630702
  27  * @summary Tests that scrolling after paint() is performed correctly.
  28  *          This is really only applicable to Vista
  29  * @author Dmitri.Trembovetski@sun.com: area=Graphics
  30  * @run main/othervm SwingOnScreenScrollingTest
  31  * run main/othervm -Dsun.java2d.opengl=True SwingOnScreenScrollingTest
  32  */
  33 
  34 import java.awt.AWTException;
  35 import java.awt.Color;
  36 import java.awt.Dimension;
  37 import java.awt.EventQueue;
  38 import java.awt.Graphics;
  39 import java.awt.GraphicsEnvironment;
  40 import java.awt.Point;
  41 import java.awt.Rectangle;
  42 import java.awt.Robot;
  43 import java.awt.image.BufferedImage;
  44 import java.io.File;
  45 import java.lang.reflect.InvocationTargetException;
  46 import javax.imageio.ImageIO;
  47 import javax.swing.JFrame;
  48 import javax.swing.JPanel;
  49 import javax.swing.JScrollPane;
  50 
  51 public class SwingOnScreenScrollingTest extends JPanel {
  52 
  53     static JScrollPane pane;
  54     static SwingOnScreenScrollingTest test;
  55 
  56     public SwingOnScreenScrollingTest() {
  57     }
  58 
  59     public static void main(String[] args) {
  60         int size = GraphicsEnvironment.
  61             getLocalGraphicsEnvironment().
  62                 getDefaultScreenDevice().
  63                     getDefaultConfiguration().getColorModel().getPixelSize();
  64         if (size < 16) {
  65             System.err.println("<16 bit display mode detected. Test PASSED");
  66             return;
  67         }
  68 
  69         final JFrame f = new JFrame("SwingOnScreenScrollingTest");
  70         try {
  71             EventQueue.invokeAndWait(new Runnable() {
  72                 public void run() {
  73                     test = new SwingOnScreenScrollingTest();
  74                     pane = new JScrollPane(test);
  75                     f.add(pane);
  76                     f.pack();
  77                     f.setSize(100, 200);
  78                     f.setVisible(true);
  79                 }
  80             });
  81         } catch (InvocationTargetException ex) {
  82             ex.printStackTrace();
  83         } catch (InterruptedException ex) {
  84             ex.printStackTrace();
  85         }
  86         try {
  87             Thread.sleep(500);
  88         } catch (InterruptedException ex) {
  89             ex.printStackTrace();
  90         }
  91         EventQueue.invokeLater(new Runnable() {
  92             public void run() {
  93                 BufferedImage bi;
  94                 bi = new BufferedImage(100, 300,
  95                                        BufferedImage.TYPE_INT_RGB);
  96                 Graphics gg = bi.getGraphics();
  97                 test.paint(gg);
  98                 for (int y = 80; y < 200; y +=10) {
  99                     test.scrollRectToVisible(new Rectangle(0, y, 100, 100));
 100                     try {
 101                         Thread.sleep(200);
 102                     } catch (InterruptedException ex) {
 103                         ex.printStackTrace();
 104                     }
 105                 }
 106                 Point p = pane.getViewport().getLocationOnScreen();
 107                 Robot r = null;
 108                 try {
 109                     r = new Robot();
 110                 } catch (AWTException ex) {
 111                     throw new RuntimeException(ex);
 112                 }
 113                 bi = r.createScreenCapture(new Rectangle(p.x+5, p.y+5, 30, 30));
 114                 for (int y = 0; y < bi.getHeight(); y++) {
 115                     for (int x = 0; x < bi.getHeight(); x++) {
 116                         int rgb = bi.getRGB(x, y);
 117                         if (bi.getRGB(x, y) != Color.red.getRGB()) {
 118                             System.err.printf("Test Failed at (%d,%d) c=0x%x\n",
 119                                               x, y, rgb);
 120                             try {
 121                                 String name =
 122                                     "SwingOnScreenScrollingTest_out.png";
 123                                 ImageIO.write(bi, "png", new File(name));
 124                                 System.err.println("Wrote grabbed image to "+
 125                                                    name);
 126                             } catch (Throwable ex) {}
 127                             throw new RuntimeException("Test failed");
 128                         }
 129                     }
 130                 }
 131                 System.out.println("Test PASSED.");
 132                 f.dispose();
 133             }
 134         });
 135     }
 136 
 137     protected void paintComponent(Graphics g) {
 138         g.setColor(Color.green);
 139         g.fillRect(0, 0, getWidth(), 100);
 140         g.setColor(Color.red);
 141         g.fillRect(0, 100, getWidth(), getHeight()-100);
 142     }
 143 
 144     public Dimension getPreferredSize() {
 145         return new Dimension(100, 300);
 146     }
 147 }