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