|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, 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 | + * @key headful |
| 27 | + * @bug 8213781 |
| 28 | + * @summary Verify webpage background color renders correctly in JEditorPane |
| 29 | + */ |
| 30 | + |
| 31 | +import java.awt.Toolkit; |
| 32 | +import java.awt.event.WindowAdapter; |
| 33 | +import java.awt.event.WindowEvent; |
| 34 | +import java.io.IOException; |
| 35 | +import java.net.MalformedURLException; |
| 36 | +import javax.swing.JDialog; |
| 37 | +import javax.swing.JEditorPane; |
| 38 | +import javax.swing.JFrame; |
| 39 | +import javax.swing.JScrollPane; |
| 40 | +import javax.swing.SwingUtilities; |
| 41 | +import javax.swing.event.HyperlinkEvent; |
| 42 | +import javax.swing.event.HyperlinkListener; |
| 43 | +import javax.swing.text.html.HTMLFrameHyperlinkEvent; |
| 44 | +import javax.swing.text.html.HTMLDocument; |
| 45 | +import java.awt.Color; |
| 46 | +import java.awt.Insets; |
| 47 | +import java.awt.Point; |
| 48 | +import java.awt.Robot; |
| 49 | + |
| 50 | +public class TestBrowserBGColor extends JFrame implements HyperlinkListener { |
| 51 | + |
| 52 | + private static TestBrowserBGColor b; |
| 53 | + private static JEditorPane browser; |
| 54 | + |
| 55 | + public static void main(final String[] args) throws Exception { |
| 56 | + Robot r = new Robot(); |
| 57 | + SwingUtilities.invokeAndWait(() -> { |
| 58 | + try { |
| 59 | + b = new TestBrowserBGColor(); |
| 60 | + } catch (Exception e) { |
| 61 | + throw new RuntimeException(e); |
| 62 | + } |
| 63 | + b.setSize(Toolkit.getDefaultToolkit().getScreenSize()); |
| 64 | + b.setVisible(true); |
| 65 | + b.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); |
| 66 | + b.addWindowListener(new WindowAdapter() { |
| 67 | + public void windowClosing(WindowEvent e) { |
| 68 | + b.dispose(); |
| 69 | + b = null; |
| 70 | + } |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + r.waitForIdle(); |
| 75 | + r.delay(500); |
| 76 | + |
| 77 | + SwingUtilities.invokeAndWait(() -> { |
| 78 | + Insets insets = browser.getInsets(); |
| 79 | + Point loc = browser.getLocationOnScreen(); |
| 80 | + Color c = r.getPixelColor( loc.x + insets.left+100, |
| 81 | + loc.y + insets.top + 100); |
| 82 | + b.dispose(); |
| 83 | + if (!c.equals(Color.WHITE)) { |
| 84 | + throw new RuntimeException("webpage background color wrong"); |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + String htmlDoc = " <!DOCTYPE html> <html><style> body { background: #FFF; } </style> <head> <title>Title</title> </head> <body> </body> </html>"; |
| 91 | + |
| 92 | + public TestBrowserBGColor() throws IOException, MalformedURLException { |
| 93 | + browser = new JEditorPane("text/html", htmlDoc); |
| 94 | + browser.setEditable(false); |
| 95 | + browser.addHyperlinkListener(this); |
| 96 | + JScrollPane scroll = new JScrollPane(browser); |
| 97 | + getContentPane().add(scroll); |
| 98 | + } |
| 99 | + |
| 100 | + public void hyperlinkUpdate(final HyperlinkEvent e) { |
| 101 | + if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { |
| 102 | + JEditorPane pane = (JEditorPane) e.getSource(); |
| 103 | + if (e instanceof HTMLFrameHyperlinkEvent) { |
| 104 | + HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e; |
| 105 | + HTMLDocument doc = (HTMLDocument) pane.getDocument(); |
| 106 | + doc.processHTMLFrameHyperlinkEvent(evt); |
| 107 | + } else { |
| 108 | + try { |
| 109 | + pane.setPage(e.getURL()); |
| 110 | + } catch (Throwable t) { |
| 111 | + t.printStackTrace(); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments