1 /*
   2  * Copyright (c) 2017, 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 8058785
  26  * @summary Displaying border around the disabled component's tool tip text
  27  * @run main/manual TestDisabledToolTipBorder
  28  */
  29 import java.awt.GridBagConstraints;
  30 import java.awt.GridBagLayout;
  31 import java.awt.event.ActionEvent;
  32 import java.util.concurrent.CountDownLatch;
  33 import java.util.concurrent.TimeUnit;
  34 import javax.swing.BorderFactory;
  35 import javax.swing.JButton;
  36 import javax.swing.JFrame;
  37 import javax.swing.JPanel;
  38 import javax.swing.JTextArea;
  39 import javax.swing.SwingUtilities;
  40 import javax.swing.UIManager;
  41 import java.awt.Insets;
  42 
  43 public class TestDisabledToolTipBorder {
  44     private static TestUI test;
  45     public static void main(String args[]) throws Exception {
  46         final CountDownLatch latch = new CountDownLatch(1);
  47 
  48         test = new TestUI(latch);
  49         SwingUtilities.invokeAndWait(() -> {
  50             try {
  51                 test.createUI();
  52             } catch (Exception ex) {
  53                 throw new RuntimeException("Exception while creating UI");
  54             }
  55         });
  56 
  57         boolean status = latch.await(2, TimeUnit.MINUTES);
  58         if (!status) {
  59             System.out.println("Test timed out.");
  60         }
  61 
  62         if (test.testResult == false) {
  63             disposeUI();
  64             throw new RuntimeException("Test Failed.");
  65         }
  66     }
  67 
  68     public static void disposeUI() throws Exception {
  69         SwingUtilities.invokeAndWait(() -> {
  70             try {
  71                 if(test != null) {
  72                     test.disposeUI();
  73                 }
  74             } catch (Exception ex) {
  75                 throw new RuntimeException("Exception while disposing UI");
  76             }
  77         });
  78     }
  79 }
  80 
  81 class TestUI {
  82 
  83     private static JFrame mainFrame;
  84     private static JPanel mainControlPanel;
  85 
  86     private static JTextArea instructionTextArea;
  87 
  88     private static JPanel resultButtonPanel;
  89     private static JButton passButton;
  90     private static JButton failButton;
  91 
  92     private GridBagConstraints gbc;
  93     private static GridBagLayout layout;
  94     private final CountDownLatch latch;
  95     public boolean testResult = false;
  96 
  97     public final void initialize() throws Exception {
  98         // Apply nimbus look and feel
  99         UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
 100     }
 101 
 102     public TestUI(CountDownLatch latch) throws Exception {
 103         this.latch = latch;
 104 
 105         // initialize the UI
 106         initialize();
 107     }
 108 
 109     public final void createUI() throws Exception {
 110         mainFrame = new JFrame();
 111 
 112         layout = new GridBagLayout();
 113         mainControlPanel = new JPanel(layout);
 114         resultButtonPanel = new JPanel(layout);
 115 
 116         gbc = new GridBagConstraints();
 117         // Create Test instructions
 118         String instructions
 119                 = "Move cursor over disabled button.\n"
 120                 + "Check if there is a border around the tooltip text \n"
 121                 + "If yes, click on 'pass' else click on 'fail'\n";
 122 
 123         instructionTextArea = new JTextArea();
 124         instructionTextArea.setText(instructions);
 125         instructionTextArea.setEditable(false);
 126         instructionTextArea.setBorder(BorderFactory.
 127                 createTitledBorder("Test Instructions"));
 128 
 129         gbc.gridx = 0;
 130         gbc.gridy = 0;
 131         mainControlPanel.add(instructionTextArea, gbc);
 132 
 133         // Add customization to this test ui
 134         customize();
 135 
 136         // Create resultButtonPanel with Pass, Fail buttons
 137         passButton = new JButton("Pass");
 138         passButton.setActionCommand("Pass");
 139         passButton.addActionListener((ActionEvent e) -> {
 140             System.out.println("Pass Button pressed!");
 141             testResult = true;
 142             latch.countDown();
 143             disposeUI();
 144         });
 145 
 146         failButton = new JButton("Fail");
 147         failButton.setActionCommand("Fail");
 148         failButton.addActionListener((ActionEvent e) -> {
 149             System.out.println("Fail Button pressed!");
 150             testResult = false;
 151             latch.countDown();
 152             disposeUI();
 153         });
 154 
 155         gbc.gridx = 0;
 156         gbc.gridy = 0;
 157         resultButtonPanel.add(passButton, gbc);
 158 
 159         gbc.gridx = 1;
 160         gbc.gridy = 0;
 161         resultButtonPanel.add(failButton, gbc);
 162 
 163         gbc.gridx = 0;
 164         gbc.gridy = 2;
 165         mainControlPanel.add(resultButtonPanel, gbc);
 166 
 167         mainFrame.add(mainControlPanel);
 168         mainFrame.pack();
 169         mainFrame.setVisible(true);
 170     }
 171 
 172     public void disposeUI() {
 173         mainFrame.dispose();
 174     }
 175 
 176     private void customize() throws Exception {
 177         // Add custom panel for the main control panel
 178         JPanel customButtonPanel = new JPanel(layout);
 179 
 180         // Customize the test UI title
 181         mainFrame.setTitle("TestDisabledToolTipBorder");
 182 
 183         // Adding the disabled button along with tool tip text
 184         JButton disabledButton = new JButton("Disabled Button");
 185         disabledButton.setToolTipText("TooltipText");
 186         disabledButton.setMargin(new Insets(30, 30, 30, 30));
 187         disabledButton.setEnabled(false);
 188 
 189         gbc.gridx = 0;
 190         gbc.gridy = 0;
 191         customButtonPanel.add(disabledButton, gbc);
 192 
 193         gbc.gridx = 0;
 194         gbc.gridy = 1;
 195         mainControlPanel.add(customButtonPanel, gbc);
 196    }
 197 }