1 /*
   2  * Copyright (c) 2011, 2012, 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  * @bug 6796710 7124242
  27  * @summary Html content in JEditorPane is overlapping on swing components while resizing the application.
  28  * @library ../../../regtesthelpers
  29  * @build Util
  30  * @author Pavel Porvatov
  31    @run main bug6796710
  32  */
  33 
  34 import java.awt.*;
  35 import java.awt.image.BufferedImage;
  36 import javax.swing.*;
  37 import sun.awt.SunToolkit;
  38 
  39 public class bug6796710 {
  40     // The page is inlined because we want to be sure that the JEditorPane filled synchronously
  41     public static final String TEXT = "<html>" +
  42             "<body>" +
  43             "<table cellpadding=\"0\" cellspacing=\"0\" border=\"1\">" +
  44             "    <tbody>" +
  45             "        <tr>" +
  46             "            <td>Col1</td>" +
  47             "            <td>Col2</td>" +
  48             "            <td>Col3</td>" +
  49             "        </tr>" +
  50             "        <tr>" +
  51             "            <td>1. It's a regression from CR 4419748. The problem is in the CSSBorder#paintBorder, which ignores clip area while painting.</td>" +
  52             "            <td>2. It's a regression from CR 4419748. The problem is in the CSSBorder#paintBorder, which ignores clip area while painting.</td>" +
  53             "            <td>3. It's a regression from CR 4419748. The problem is in the CSSBorder#paintBorder, which ignores clip area while painting.</td>" +
  54             "        </tr>" +
  55             "    </tbody>" +
  56             "</table>" +
  57             "</body>" +
  58             "</html>";
  59 
  60     private static Robot robot;
  61 
  62     private static JFrame frame;
  63 
  64     private static JPanel pnBottom;
  65 
  66     public static void main(String[] args) throws Exception {
  67         robot = new Robot();
  68 
  69         SwingUtilities.invokeAndWait(new Runnable() {
  70             @Override
  71             public void run() {
  72                 frame = new JFrame();
  73 
  74                 frame.setUndecorated(true);
  75 
  76                 pnBottom = new JPanel();
  77                 pnBottom.add(new JLabel("Some label"));
  78                 pnBottom.add(new JButton("A button"));
  79 
  80                 JEditorPane editorPane = new JEditorPane();
  81 
  82                 editorPane.setContentType("text/html");
  83                 editorPane.setText(TEXT);
  84                 editorPane.setEditable(false);
  85 
  86                 JPanel pnContent = new JPanel(new BorderLayout());
  87 
  88                 pnContent.add(new JScrollPane(editorPane), BorderLayout.CENTER);
  89                 pnContent.add(pnBottom, BorderLayout.SOUTH);
  90 
  91                 frame.setContentPane(pnContent);
  92                 frame.setSize(400, 600);
  93                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  94                 frame.setVisible(true);
  95             }
  96         });
  97 
  98         ((SunToolkit) SunToolkit.getDefaultToolkit()).realSync();
  99 
 100         // This delay should be added for MacOSX, realSync is not enough
 101         Thread.sleep(1000);
 102 
 103         BufferedImage bufferedImage = getPnBottomImage();
 104 
 105         SwingUtilities.invokeAndWait(new Runnable() {
 106             @Override
 107             public void run() {
 108                 frame.setSize(400, 150);
 109             }
 110         });
 111 
 112         ((SunToolkit) SunToolkit.getDefaultToolkit()).realSync();
 113 
 114         // On Linux platforms realSync doesn't guaranties setSize completion
 115         Thread.sleep(1000);
 116 
 117         if (!Util.compareBufferedImages(bufferedImage, getPnBottomImage())) {
 118             throw new RuntimeException("The test failed");
 119         }
 120 
 121         System.out.println("The test bug6796710 passed.");
 122     }
 123 
 124     private static BufferedImage getPnBottomImage() {
 125         Rectangle rect = pnBottom.getBounds();
 126 
 127         Util.convertRectToScreen(rect, pnBottom.getParent());
 128 
 129         return robot.createScreenCapture(rect);
 130     }
 131 }