Path: blob/master/test/jdk/javax/swing/JTabbedPane/7170310/bug7170310.java
41153 views
/*1* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Component;24import java.awt.Dimension;25import java.awt.Rectangle;26import java.awt.Toolkit;27import javax.swing.JComponent;28import javax.swing.JFrame;29import javax.swing.JPanel;30import javax.swing.JTabbedPane;31import javax.swing.JViewport;32import javax.swing.SwingUtilities;33import javax.swing.UIManager;34import javax.swing.plaf.metal.MetalLookAndFeel;353637/**38* @test39* @key headful40* @bug 717031041* @author Alexey Ivanov42* @summary Selected tab should be scrolled into view.43* @library /lib/client/44* @build ExtendedRobot45* @run main bug717031046*/4748public class bug7170310 {49private static final int TABS_NUMBER = 3;5051private static volatile JTabbedPane tabbedPane;52private static volatile int count = 1;5354private static volatile JFrame frame;5556private static volatile Exception exception = null;5758public static void main(String[] args) throws Exception {59try {60UIManager.setLookAndFeel(new MetalLookAndFeel());61SwingUtilities.invokeAndWait(bug7170310::createAndShowUI);6263sync();6465for (int i = 0; i < TABS_NUMBER; i++) {66SwingUtilities.invokeAndWait(bug7170310::addTab);67sync();68}6970SwingUtilities.invokeAndWait(bug7170310::check);7172if (exception != null) {73System.out.println("Test failed: " + exception.getMessage());74throw exception;75} else {76System.out.printf("Test passed");77}78} finally {79if (frame != null) { frame.dispose(); }80}81}8283private static void createAndShowUI() {84frame = new JFrame("bug7170310");85frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);86frame.setSize(200, 100);8788tabbedPane = new JTabbedPane();89tabbedPane.addTab("Main Tab", new JPanel());9091tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);9293frame.getContentPane().add(tabbedPane);94frame.setVisible(true);95}9697private static void addTab() {98tabbedPane.addTab("Added Tab " + count++, new JPanel());99tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1);100}101102private static void check() {103try {104JViewport vp = null;105for (Component c : tabbedPane.getComponents()) {106if (c instanceof JViewport) {107vp = (JViewport) c;108break;109}110}111112JComponent v = (JComponent) vp.getView();113Rectangle vr = vp.getViewRect();114Dimension vs = v.getSize();115116// The tab view must be scrolled to the end so that the last tab is visible117if (vs.width != (vr.x + vr.width)) {118throw new RuntimeException("tabScroller.tabPanel view is positioned incorrectly: "119+ vs.width + " vs " + (vr.x + vr.width));120}121} catch (Exception e) {122exception = e;123}124}125private static void sync() {126try {127ExtendedRobot robot = new ExtendedRobot();128robot.waitForIdle(300);129}catch(Exception ex) {130ex.printStackTrace();131throw new Error("Unexpected Failure");132}133}134}135136137