1/*
2 * Copyright (c) 2014, 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
24import java.awt.Component;
25import java.awt.Dimension;
26import java.awt.Rectangle;
27import java.awt.Toolkit;
28import javax.swing.JComponent;
29import javax.swing.JFrame;
30import javax.swing.JPanel;
31import javax.swing.JTabbedPane;
32import javax.swing.JViewport;
33import javax.swing.SwingUtilities;
34import javax.swing.UIManager;
35import javax.swing.plaf.metal.MetalLookAndFeel;
36
37
38/**
39 * @test
40 * @key headful
41 * @bug 7170310
42 * @author Alexey Ivanov
43 * @summary Selected tab should be scrolled into view.
44 * @library ../../../../lib/testlibrary/
45 * @build ExtendedRobot
46 * @run main bug7170310
47 */
48
49public class bug7170310 {
50    private static final int TABS_NUMBER = 3;
51
52    private static volatile JTabbedPane tabbedPane;
53    private static volatile int count = 1;
54
55    private static volatile JFrame frame;
56
57    private static volatile Exception exception = null;
58
59    public static void main(String[] args) throws Exception {
60        try {
61            UIManager.setLookAndFeel(new MetalLookAndFeel());
62            SwingUtilities.invokeAndWait(bug7170310::createAndShowUI);
63
64            sync();
65
66            for (int i = 0; i < TABS_NUMBER; i++) {
67                SwingUtilities.invokeAndWait(bug7170310::addTab);
68                sync();
69            }
70
71            SwingUtilities.invokeAndWait(bug7170310::check);
72
73            if (exception != null) {
74                System.out.println("Test failed: " + exception.getMessage());
75                throw exception;
76            } else {
77                System.out.printf("Test passed");
78            }
79        } finally {
80            if (frame != null) { frame.dispose(); }
81        }
82    }
83
84    private static void createAndShowUI() {
85        frame = new JFrame("bug7170310");
86        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
87        frame.setSize(200, 100);
88
89        tabbedPane = new JTabbedPane();
90        tabbedPane.addTab("Main Tab", new JPanel());
91
92        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
93
94        frame.getContentPane().add(tabbedPane);
95        frame.setVisible(true);
96    }
97
98    private static void addTab() {
99        tabbedPane.addTab("Added Tab " + count++, new JPanel());
100        tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1);
101    }
102
103    private static void check() {
104        try {
105            JViewport vp = null;
106            for (Component c : tabbedPane.getComponents()) {
107                if (c instanceof JViewport) {
108                    vp = (JViewport) c;
109                    break;
110                }
111            }
112
113            JComponent v = (JComponent) vp.getView();
114            Rectangle vr = vp.getViewRect();
115            Dimension vs = v.getSize();
116
117            // The tab view must be scrolled to the end so that the last tab is visible
118            if (vs.width != (vr.x + vr.width)) {
119                throw new RuntimeException("tabScroller.tabPanel view is positioned incorrectly: "
120                        + vs.width + " vs " + (vr.x + vr.width));
121            }
122        } catch (Exception e) {
123            exception = e;
124        }
125    }
126    private static void sync() {
127        try {
128             ExtendedRobot robot = new ExtendedRobot();
129             robot.waitForIdle(300);
130         }catch(Exception ex) {
131             ex.printStackTrace();
132             throw new Error("Unexpected Failure");
133         }
134    }
135}
136