Path: blob/master/test/jdk/javax/swing/JTabbedPane/8134116/Bug8134116.java
41153 views
1import java.awt.*;2import java.awt.event.KeyEvent;3import java.util.ArrayList;4import java.util.List;5import javax.accessibility.Accessible;6import javax.accessibility.AccessibleContext;7import javax.accessibility.AccessibleState;8import javax.accessibility.AccessibleStateSet;9import javax.swing.*;10import javax.swing.plaf.nimbus.NimbusLookAndFeel;1112/*13* @test14* @key headful15* @bug 813411616* @summary JTabbedPane$Page.getBounds throws IndexOutOfBoundsException17* @run main Bug813411618*/19public class Bug8134116 {2021private static volatile Exception exception = null;22private static JFrame frame;2324public static void main(String args[]) throws Exception {2526try {27UIManager.setLookAndFeel(new NimbusLookAndFeel());28} catch (Exception e) {29throw new RuntimeException(e);30}3132try {33SwingUtilities.invokeAndWait(() -> {34JPanel panel0 = new JPanel();35JPanel panel2 = new JPanel();36BadPane badPane = new BadPane();37badPane.add("zero", panel0);38badPane.add("one", null); // no component39badPane.add("", panel2); // no title40badPane.add("", null); // no component, no title41// but give it that via a tabComponent42JPanel tabComponent = new JPanel();43JLabel tabComponentLabel = new JLabel("three");44tabComponent.add(tabComponentLabel);45badPane.setTabComponentAt(3, tabComponent);46frame = new JFrame();47frame.add(badPane);48frame.setSize(300, 300);49frame.setVisible(true);5051try {52AccessibleContext ac = badPane.getAccessibleContext();53Accessible page0 = ac.getAccessibleChild(0);54if (page0 == null) {55// Not something being tested, but checking anyway56throw new RuntimeException("getAccessibleChild(0) is null");57}58Accessible page1 = ac.getAccessibleChild(1);59if (page1 == null) {60// Not something being tested, but checking anyway61throw new RuntimeException("getAccessibleChild(1) is null");62}63Accessible page2 = ac.getAccessibleChild(2);64Accessible page3 = ac.getAccessibleChild(3);65// page0 - page3 are JTabbedPane.Page, a private inner class66// and is an AccessibleContext67// and implements Accessible and AccessibleComponent68AccessibleContext pac0 = page0.getAccessibleContext();69AccessibleContext pac1 = page1.getAccessibleContext();70AccessibleContext pac2 = page2.getAccessibleContext();71AccessibleContext pac3 = page3.getAccessibleContext();7273// test Page.getBounds74// ensure no IndexOutOfBoundsException75Rectangle r0 = pac0.getAccessibleComponent().getBounds();76// make sure second Bounds is different than first77Rectangle r1 = pac1.getAccessibleComponent().getBounds();78if (r1.equals(r0)) {79String msg = "Second tab should not have same bounds as first tab";80throw new RuntimeException(msg);81}8283// test Page.getAccessibleStateSet84// At this point page 0 is selected85AccessibleStateSet accSS0 = pac0.getAccessibleStateSet();86if (!accSS0.contains(AccessibleState.SELECTED)) {87String msg = "Empty title -> AccessibleState.SELECTED not set";88throw new RuntimeException(msg);89}90// select second tab91badPane.setSelectedIndex(1);92AccessibleStateSet accSS1 = pac1.getAccessibleStateSet();93if (!accSS1.contains(AccessibleState.SELECTED)) {94String msg = "Second tab selected but AccessibleState.SELECTED not set";95throw new RuntimeException(msg);96}97// select third tab98badPane.setSelectedIndex(2);99AccessibleStateSet accSS2 = pac2.getAccessibleStateSet();100if (!accSS1.contains(AccessibleState.SELECTED)) {101String msg = "Third tab selected but AccessibleState.SELECTED not set";102throw new RuntimeException(msg);103}104// select fourth tab105badPane.setSelectedIndex(3);106AccessibleStateSet accSS3 = pac3.getAccessibleStateSet();107if (!accSS1.contains(AccessibleState.SELECTED)) {108String msg = "Fourth tab selected but AccessibleState.SELECTED not set";109throw new RuntimeException(msg);110}111112// test Page.getAccessibleIndexInParent113if (pac0.getAccessibleIndexInParent() == -1) {114String msg = "Empty title -> negative AccessibleIndexInParent";115throw new RuntimeException(msg);116}117if (pac0.getAccessibleIndexInParent() != 0) {118String msg = "first tab is not at index 0 in parent";119throw new RuntimeException(msg);120}121if (pac1.getAccessibleIndexInParent() != 1) {122String msg = "second tab (null component) is not at index 1 in parent";123throw new RuntimeException(msg);124}125if (pac2.getAccessibleIndexInParent() != 2) {126String msg = "third tab (empty title) string is not at index 2 in parent";127throw new RuntimeException(msg);128}129if (pac3.getAccessibleIndexInParent() != 3) {130String msg = "fourth tab (empty title, null component, has tabComponent) string is not at index 3 in parent";131throw new RuntimeException(msg);132}133134// test Page.getAccessibleName135String accName = pac0.getAccessibleName();136if (!accName.equals("zero")) {137String msg = "Empty title -> empty AccessibleName";138throw new RuntimeException(msg);139}140// test Page.getAccessibleName when component is null141accName = pac1.getAccessibleName();142if (!accName.equals("one")) {143String msg = "AccessibleName of null panel not 'one'";144throw new RuntimeException(msg);145}146147// test Page.setDisplayedMnemonicIndex148// Empty title -> IllegalArgumnetException149badPane.setDisplayedMnemonicIndexAt(0, 1);150151// test Page.updateDisplayedMnemonicIndex152badPane.setMnemonicAt(0, KeyEvent.VK_Z);153if (badPane.getDisplayedMnemonicIndexAt(0) == -1) {154String msg="Empty title -> getDisplayedMnemonicIndexAt failure";155throw new RuntimeException(msg);156}157} catch (Exception e) {158exception = e;159}160});161if (exception != null) {162System.out.println("Test failed: " + exception.getMessage());163throw exception;164} else {165System.out.println("Test passed.");166}167} finally {168if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());169}170}171172// The following is likely what is being done in Burp Suite173// https://portswigger.net/burp/ which fails in the same way, i.e. the174// pages List in JTabbedPane is not being managed properly and thus175// Page.title is "" for each page. The overridden insertTab manages titles176// in the subclass passing a "" title to the superclass JTabbedPane through177// its insertTab. Later an overridden getTitleAt returns the titles as178// managed by the subclass.179static class BadPane extends JTabbedPane {180private List<String> titles;181182BadPane() {183titles = new ArrayList<String>(1);184}185186@Override187public void insertTab( String title, Icon icon, Component component,188String tip, int index ) {189titles.add(index, title);190super.insertTab("", icon, component, tip, index);191}192193@Override194public String getTitleAt(int i) {195return titles.get(i);196}197}198199}200201202