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