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