Path: blob/master/test/jdk/javax/accessibility/JList/AccessibleJListChildNPETest.java
41152 views
/*1* Copyright (c) 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.lang.reflect.InvocationTargetException;24import java.util.ArrayList;25import java.util.Arrays;26import java.util.List;2728import javax.accessibility.Accessible;29import javax.accessibility.AccessibleContext;30import javax.swing.AbstractListModel;31import javax.swing.JFrame;32import javax.swing.JList;33import javax.swing.SwingUtilities;34import javax.swing.WindowConstants;3536/* @test37@key headful38@bug 807624939@summary NPE in AccessBridge while editing JList model40@author Mikhail Cherkasov41@run main AccessibleJListChildNPETest42*/43public class AccessibleJListChildNPETest {4445private static String[] model = { "1", "2", "3", "4", "5", "6" };46private static JList<String> list;4748public static void main(String[] args) throws InvocationTargetException, InterruptedException {49SwingUtilities.invokeAndWait(new Runnable() {50@Override51public void run() {52JFrame frame = new JFrame();53frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);54final MyModel dataModel = new MyModel(Arrays.asList(model));55list = new JList<>(dataModel);56frame.getContentPane().add(list);57frame.pack();58frame.setVisible(true);5960}61});6263SwingUtilities.invokeAndWait(new Runnable() {64@Override65public void run() {66AccessibleContext ac = list.getAccessibleContext();67MyModel model = (MyModel)list.getModel();68Accessible accessibleChild = ac.getAccessibleChild(model.getSize()-1);69model.removeFirst();70accessibleChild.getAccessibleContext().getAccessibleSelection();71accessibleChild.getAccessibleContext().getAccessibleText();72accessibleChild.getAccessibleContext().getAccessibleValue();73}74});75}7677protected static class MyModel extends AbstractListModel<String> {78private List<String> items = new ArrayList<>();7980MyModel(final List<String> newItems) {81super();82items.addAll(newItems);83fireIntervalAdded(this, 0, getSize() - 1);84}8586void removeFirst() {87if(getSize() > 0) {88items.remove(0);89fireIntervalRemoved(this, 0, 0);90}91}9293@Override94public int getSize() {95return items.size();96}9798@Override99public String getElementAt(int index) {100return items.get(index);101}102}103}104105106