1 /*
   2  * Copyright (c) 2014, 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 import java.awt.BorderLayout;
  24 import java.awt.Toolkit;
  25 import java.awt.Robot;
  26 import javax.swing.JFrame;
  27 import javax.swing.JLabel;
  28 import javax.swing.JTabbedPane;
  29 import javax.swing.SwingUtilities;
  30 import javax.swing.plaf.metal.MetalLookAndFeel;
  31 
  32 /**
  33  * @test
  34  * @key headful
  35  * @bug 8017284
  36  * @author Alexander Scherbatiy
  37  * @summary  Aqua LaF: memory leak when HTML is used for JTabbedPane tab titles
  38  * @run main/othervm/timeout=60 -Xmx128m bug8017284
  39  */
  40 public class bug8017284 {
  41 
  42     private static final int TAB_COUNT = 100;
  43     private static final int ITERATIONS = 100;
  44     private static JFrame frame;
  45     private static JTabbedPane tabbedPane;
  46 
  47     public static void main(String[] args) throws Exception {
  48 
  49         Robot robot = new Robot();
  50         SwingUtilities.invokeAndWait(() -> {
  51             frame = new JFrame();
  52             frame.setSize(500, 500);
  53             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54 
  55             tabbedPane = new JTabbedPane();
  56 
  57             for (int i = 0; i < TAB_COUNT; i++) {
  58                 tabbedPane.add("Header " + i, new JLabel("Content: " + i));
  59             }
  60 
  61             frame.getContentPane().setLayout(new BorderLayout());
  62             frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
  63             frame.setVisible(true);
  64         });
  65 
  66         robot.waitForIdle();
  67 
  68         SwingUtilities.invokeAndWait(() -> {
  69             for (int j = 0; j < ITERATIONS; j++) {
  70                 for (int i = 0; i < TAB_COUNT; i++) {
  71                     tabbedPane.setTitleAt(i, getHtmlText(j * TAB_COUNT + i));
  72                 }
  73             }
  74         });
  75         robot.waitForIdle();
  76 
  77         SwingUtilities.invokeAndWait(() -> frame.dispose());
  78     }
  79 
  80     private static String getHtmlText(int i) {
  81         return "<html><b><i>" + i + "</b></i>";
  82     }
  83 }