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 */
23import java.awt.*;
24import javax.swing.*;
25import java.awt.event.*;
26
27/**
28 * @test
29 * @key headful
30 * @bug 7161568
31 * @author Alexander Scherbatiy
32 * @summary Tests that navigating tabs in the JTAbbedPane does not throw NPE
33 * @run main bug7161568
34 */
35public class bug7161568 {
36
37    private static final int N = 50;
38    private static JTabbedPane tabbedPane;
39
40    public static void main(String[] args) throws Exception {
41        UIManager.put("TabbedPane.selectionFollowsFocus", Boolean.FALSE);
42
43        Robot robot = new Robot();
44        robot.setAutoDelay(50);
45
46        SwingUtilities.invokeAndWait(new Runnable() {
47
48            @Override
49            public void run() {
50                createAndShowUI();
51            }
52        });
53
54        robot.waitForIdle();
55
56        SwingUtilities.invokeAndWait(new Runnable() {
57
58            @Override
59            public void run() {
60                tabbedPane.requestFocus();
61            }
62        });
63
64        robot.waitForIdle();
65
66        for (int i = 0; i < N; i++) {
67            robot.keyPress(KeyEvent.VK_LEFT);
68            robot.keyRelease(KeyEvent.VK_LEFT);
69            robot.waitForIdle();
70        }
71    }
72
73    static void createAndShowUI() {
74        JFrame frame = new JFrame("Test");
75        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
76        frame.setSize(100, 100);
77
78        tabbedPane = new JTabbedPane();
79
80        for (int i = 0; i < N; i++) {
81            tabbedPane.addTab("Tab: " + i, new JLabel("Test"));
82        }
83
84        tabbedPane.setSelectedIndex(0);
85
86        frame.getContentPane().add(tabbedPane);
87        frame.setVisible(true);
88    }
89}
90