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