bug7189299.java revision 11018:66682f651425
1/*
2 * Copyright (c) 2013, 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 * Portions Copyright (c) 2013 IBM Corporation
26 */
27import java.awt.BorderLayout;
28import java.awt.Robot;
29
30import java.awt.event.ActionListener;
31import javax.swing.DefaultButtonModel;
32import javax.swing.JEditorPane;
33import javax.swing.JFrame;
34import javax.swing.SwingUtilities;
35import javax.swing.text.StyleConstants;
36import javax.swing.text.StyleContext;
37import javax.swing.text.html.HTMLEditorKit;
38
39
40/*
41 * @test
42 * @bug 8008289
43 * @summary Shared ButtonModel instance should deregister previous listeners.
44 * @author Frank Ding
45 */
46public class bug7189299 {
47
48    private static JEditorPane html;
49    private static JFrame frame;
50
51    private static void setup() {
52        /**
53         * Note the input type is not restricted to "submit". Types "image",
54         * "checkbox", "radio" have the same problem.
55         */
56        html = new JEditorPane("text/html",
57                "<html><body><form action=\"http://localhost.cgi\">"
58                        + "<input type=submit name=submit value=\"submit\"/>"
59                        + "</form></body></html>");
60        frame = new JFrame();
61        frame.setLayout(new BorderLayout());
62        frame.add(html, BorderLayout.CENTER);
63        frame.setSize(200, 100);
64        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
65        frame.setVisible(true);
66    }
67
68    private static void doTest() {
69        /*
70         * Calling updateComponentTreeUI creates a new FormView instance with
71         * its own associated JButton instance. The same DefaultButtonModel
72         * instance is used for both FormView's.
73         *
74         * The action listeners associated with (the JButton for) the first
75         * FormView should be unregistered from this common DefaultButtonModel,
76         * such that only those for the new FormView remain.
77         */
78        SwingUtilities.updateComponentTreeUI(html);
79    }
80
81    private static void verifySingleDefaultButtonModelListener() {
82        HTMLEditorKit htmlEditorKit = (HTMLEditorKit) html.getEditorKit();
83        StyleContext.NamedStyle style = ((StyleContext.NamedStyle) htmlEditorKit
84                .getInputAttributes());
85        DefaultButtonModel model = ((DefaultButtonModel) style
86                .getAttribute(StyleConstants.ModelAttribute));
87        ActionListener[] listeners = model.getActionListeners();
88        int actionListenerNum = listeners.length;
89        if (actionListenerNum != 1) {
90            throw new RuntimeException(
91                    "Expected single ActionListener object registered with "
92                    + "DefaultButtonModel; found " + actionListenerNum
93                    + " listeners registered.");
94        }
95
96        int changeListenerNum = model.getChangeListeners().length;
97        if (changeListenerNum != 1) {
98            throw new RuntimeException(
99                    "Expected at most one ChangeListener object registered "
100                    + "with DefaultButtonModel; found " + changeListenerNum
101                    + " listeners registered.");
102        }
103        int itemListenerNum = model.getItemListeners().length;
104        if (itemListenerNum != 1) {
105            throw new RuntimeException(
106                    "Expected at most one ItemListener object registered "
107                    + "with DefaultButtonModel; found " + itemListenerNum
108                    + " listeners registered.");
109        }
110    }
111
112    public static void main(String[] args) throws Exception {
113        final Robot robot = new Robot();
114
115        SwingUtilities.invokeAndWait(new Runnable() {
116
117            @Override
118            public void run() {
119                setup();
120            }
121        });
122        robot.waitForIdle();
123        SwingUtilities.invokeAndWait(new Runnable() {
124
125            @Override
126            public void run() {
127                try {
128                    verifySingleDefaultButtonModelListener();
129                    doTest();
130                    verifySingleDefaultButtonModelListener();
131                } finally {
132                    frame.dispose();
133                }
134            }
135        });
136    }
137}
138