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