Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTabbedPane/7170310/bug7170310.java
38855 views
/*1* Copyright (c) 2014, 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* @bug 717031040* @author Alexey Ivanov41* @summary Selected tab should be scrolled into view.42* @library ../../../../lib/testlibrary/43* @build ExtendedRobot44* @run main bug717031045*/46public class bug7170310 {47private static final int TABS_NUMBER = 3;4849private static volatile JTabbedPane tabbedPane;50private static volatile int count = 1;5152private static volatile JFrame frame;5354private static volatile Exception exception = null;5556public static void main(String[] args) throws Exception {57try {58UIManager.setLookAndFeel(new MetalLookAndFeel());59SwingUtilities.invokeAndWait(bug7170310::createAndShowUI);6061sync();6263for (int i = 0; i < TABS_NUMBER; i++) {64SwingUtilities.invokeAndWait(bug7170310::addTab);65sync();66}6768SwingUtilities.invokeAndWait(bug7170310::check);6970if (exception != null) {71System.out.println("Test failed: " + exception.getMessage());72throw exception;73} else {74System.out.printf("Test passed");75}76} finally {77frame.dispose();78}79}8081private static void createAndShowUI() {82frame = new JFrame("bug7170310");83frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);84frame.setSize(200, 100);8586tabbedPane = new JTabbedPane();87tabbedPane.addTab("Main Tab", new JPanel());8889tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);9091frame.getContentPane().add(tabbedPane);92frame.setVisible(true);93}9495private static void addTab() {96tabbedPane.addTab("Added Tab " + count++, new JPanel());97tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1);98}99100private static void check() {101try {102JViewport vp = null;103for (Component c : tabbedPane.getComponents()) {104if (c instanceof JViewport) {105vp = (JViewport) c;106break;107}108}109110JComponent v = (JComponent) vp.getView();111Rectangle vr = vp.getViewRect();112Dimension vs = v.getSize();113114// The tab view must be scrolled to the end so that the last tab is visible115if (vs.width != (vr.x + vr.width)) {116throw new RuntimeException("tabScroller.tabPanel view is positioned incorrectly: "117+ vs.width + " vs " + (vr.x + vr.width));118}119} catch (Exception e) {120exception = e;121}122}123private static void sync() {124try {125ExtendedRobot robot = new ExtendedRobot();126robot.waitForIdle(300);127}catch(Exception ex) {128ex.printStackTrace();129throw new Error("Unexpected Failure");130}131}132}133134135