1 /*
   2  * Copyright (c) 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 /* @test
  24    @bug 4936917 7190578 8174717
  25    @summary  Tests if background is correctly painted when <BODY> has css margins
  26    @author Denis Sharypov
  27    @library ../../../regtesthelpers
  28    @run main bug4936917
  29 */
  30 
  31 
  32 
  33 import java.awt.Color;
  34 import java.awt.Point;
  35 import java.awt.Robot;
  36 import java.util.Timer;
  37 import javax.swing.JComponent;
  38 import javax.swing.JEditorPane;
  39 import javax.swing.JFrame;
  40 import javax.swing.SwingUtilities;
  41 
  42 
  43 public class bug4936917 {
  44 
  45     private boolean passed = false;
  46     private Timer timer;
  47     private JEditorPane editorPane;
  48     private static JFrame f;
  49     private volatile Point p = null;
  50 
  51     private String text =
  52                 "<html><head><style>" +
  53                 "body {background-color: #cccccc; margin-top: 36.000000pt;}" +
  54                 "</style></head>" +
  55                 "<body> some text </body></html>";
  56 
  57     public void init() throws Exception {
  58         SwingUtilities.invokeAndWait(new Runnable() {
  59             @Override
  60             public void run() {
  61                 editorPane = new JEditorPane("text/html", "");
  62                 editorPane.setEditable(false);
  63                 editorPane.setMargin(new java.awt.Insets(0, 0, 0, 0));
  64                 editorPane.setText(text);
  65 
  66                 f = new JFrame();
  67                 f.getContentPane().add(editorPane);
  68                 f.setSize(600, 400);
  69                 f.setVisible(true);
  70             }
  71         });
  72         blockTillDisplayed(editorPane);
  73         Robot robot  = new Robot();
  74         robot.waitForIdle();
  75         robot.delay(300);
  76 
  77         int x0 = p.x + 15 ;
  78         int y = p.y + 15;
  79         int match = 0;
  80         int nonmatch = 0;
  81 
  82         passed = true;
  83         for (int x = x0; x < x0 + 10; x++) {
  84             System.out.println("color ("+x+"," + y +")=" + robot.getPixelColor(x,y));
  85             if (!robot.getPixelColor(x, y).equals(new Color(0xcc, 0xcc, 0xcc))) {
  86                 nonmatch++;
  87             } else match++;
  88         }
  89         if (nonmatch > match) {
  90             passed = false;
  91         }
  92     }
  93 
  94     void blockTillDisplayed(JComponent comp) throws Exception {
  95         while (p == null) {
  96             try {
  97                 SwingUtilities.invokeAndWait(() -> {
  98                     p = comp.getLocationOnScreen();
  99                 });
 100             } catch (IllegalStateException e) {
 101                 try {
 102                     Thread.sleep(1000);
 103                 } catch (InterruptedException ie) {
 104                 }
 105             }
 106         }
 107     }
 108 
 109     public void destroy() throws Exception {
 110         SwingUtilities.invokeAndWait(()->f.dispose());
 111         if(!passed) {
 112             throw new RuntimeException("Test failed.");
 113         }
 114     }
 115 
 116 
 117     public static void main(String args[]) throws Exception {
 118             bug4936917 test = new bug4936917();
 119             test.init();
 120             test.destroy();
 121     }
 122 }