|
| 1 | +/* |
| 2 | + * Copyright (c) 2011, 2024, 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 | + * @bug 6494356 |
| 26 | + * @key headful |
| 27 | + * @summary Test that BoxView.layout() is not called with negative arguments |
| 28 | + * @run main bug6494356 |
| 29 | + */ |
| 30 | + |
| 31 | +import java.awt.Dimension; |
| 32 | +import java.awt.Toolkit; |
| 33 | +import java.beans.PropertyChangeEvent; |
| 34 | +import java.beans.PropertyChangeListener; |
| 35 | +import java.io.Writer; |
| 36 | +import java.nio.file.Files; |
| 37 | +import java.nio.file.Path; |
| 38 | +import java.util.concurrent.CountDownLatch; |
| 39 | +import javax.swing.JEditorPane; |
| 40 | +import javax.swing.JFrame; |
| 41 | +import javax.swing.SwingUtilities; |
| 42 | +import javax.swing.text.Element; |
| 43 | +import javax.swing.text.StyleConstants; |
| 44 | +import javax.swing.text.View; |
| 45 | +import javax.swing.text.ViewFactory; |
| 46 | +import javax.swing.text.html.HTML; |
| 47 | +import javax.swing.text.html.HTMLEditorKit; |
| 48 | +import javax.swing.text.html.ParagraphView; |
| 49 | + |
| 50 | +public class bug6494356 { |
| 51 | + static JEditorPane ep; |
| 52 | + private static final CountDownLatch latch = new CountDownLatch(1); |
| 53 | + |
| 54 | + public static void main(final String[] args) throws Exception { |
| 55 | + final Path file = Path.of("bug6494356.html"); |
| 56 | + try (Writer writer = Files.newBufferedWriter(file)) { |
| 57 | + writer.write("<p>Paragraph</p>"); |
| 58 | + } |
| 59 | + try { |
| 60 | + SwingUtilities.invokeLater(new Runnable() { |
| 61 | + public void run() { |
| 62 | + ep = new JEditorPane(); |
| 63 | + ep.setEditorKitForContentType("text/html", new MyEditorKit()); |
| 64 | + ep.addPropertyChangeListener("page", new PropertyChangeListener() { |
| 65 | + public void propertyChange(PropertyChangeEvent pce) { |
| 66 | + if (pce.getPropertyName().equals("page")) { |
| 67 | + latch.countDown(); |
| 68 | + } |
| 69 | + } |
| 70 | + }); |
| 71 | + JFrame f = new JFrame(); |
| 72 | + f.setTitle("6494356"); |
| 73 | + f.setSize(new Dimension( |
| 74 | + Toolkit.getDefaultToolkit().getScreenSize().width, 600)); |
| 75 | + f.setContentPane(ep); |
| 76 | + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 77 | + f.setVisible(true); |
| 78 | + try { |
| 79 | + ep.setPage("file:" + file); |
| 80 | + } catch (Exception ex) { |
| 81 | + testPassed = false; |
| 82 | + throw new RuntimeException(ex); |
| 83 | + } |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + latch.await(); |
| 88 | + if (!testPassed) { |
| 89 | + throw new RuntimeException("test failed."); |
| 90 | + } |
| 91 | + } finally { |
| 92 | + Files.delete(file); |
| 93 | + } |
| 94 | + System.out.println("6494356 OK"); |
| 95 | + } |
| 96 | + |
| 97 | + static volatile boolean testPassed = true; |
| 98 | + |
| 99 | + static class MyEditorKit extends HTMLEditorKit { |
| 100 | + static class MyViewFactory extends HTMLFactory { |
| 101 | + public View create(Element elem) { |
| 102 | + HTML.Tag tag = (HTML.Tag) elem.getAttributes().getAttribute( |
| 103 | + StyleConstants.NameAttribute); |
| 104 | + if ((tag != null) && (tag == HTML.Tag.P)) { |
| 105 | + return new MyParagraphView(elem); |
| 106 | + } else { |
| 107 | + return super.create(elem); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + static class MyParagraphView extends ParagraphView { |
| 112 | + MyParagraphView(Element elem) { |
| 113 | + super(elem); |
| 114 | + } |
| 115 | + |
| 116 | + protected void layout(int width, int height) { |
| 117 | + if ((width < 0) || (height < 0)) { |
| 118 | + testPassed = false; |
| 119 | + throw new RuntimeException("w=" + width + " h=" + height); |
| 120 | + } |
| 121 | + super.layout(width, height); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + final ViewFactory viewFactory = new MyViewFactory(); |
| 128 | + |
| 129 | + public ViewFactory getViewFactory() { |
| 130 | + return viewFactory; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | +} |
| 135 | + |
0 commit comments