1/*
2* Copyright (c) 2017, 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
24import java.lang.reflect.InvocationTargetException;
25import java.util.ArrayList;
26import java.util.Arrays;
27import java.util.List;
28
29import javax.accessibility.Accessible;
30import javax.accessibility.AccessibleContext;
31import javax.swing.AbstractListModel;
32import javax.swing.JFrame;
33import javax.swing.JList;
34import javax.swing.SwingUtilities;
35import javax.swing.WindowConstants;
36
37/* @test
38   @bug 8076249
39   @summary  NPE in AccessBridge while editing JList model
40   @author Mikhail Cherkasov
41   @run main AccessibleJListChildNPETest
42*/
43public class AccessibleJListChildNPETest {
44
45    private static String[] model = { "1", "2", "3", "4", "5", "6" };
46    private static JList<String> list;
47
48    public static void main(String[] args) throws InvocationTargetException, InterruptedException {
49        SwingUtilities.invokeAndWait(new Runnable() {
50            @Override
51            public void run() {
52                JFrame frame = new JFrame();
53                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
54                final MyModel dataModel = new MyModel(Arrays.asList(model));
55                list = new JList<>(dataModel);
56                frame.getContentPane().add(list);
57                frame.pack();
58                frame.setVisible(true);
59
60            }
61        });
62
63        SwingUtilities.invokeAndWait(new Runnable() {
64            @Override
65            public void run() {
66                AccessibleContext ac = list.getAccessibleContext();
67                MyModel model = (MyModel)list.getModel();
68                Accessible accessibleChild = ac.getAccessibleChild(model.getSize()-1);
69                model.removeFirst();
70                accessibleChild.getAccessibleContext().getAccessibleSelection();
71                accessibleChild.getAccessibleContext().getAccessibleText();
72                accessibleChild.getAccessibleContext().getAccessibleValue();
73            }
74        });
75    }
76
77    protected static class MyModel extends AbstractListModel<String> {
78        private List<String> items = new ArrayList<>();
79
80        MyModel(final List<String> newItems) {
81            super();
82            items.addAll(newItems);
83            fireIntervalAdded(this, 0, getSize() - 1);
84        }
85
86        void removeFirst() {
87            if(getSize() > 0) {
88                items.remove(0);
89                fireIntervalRemoved(this, 0, 0);
90            }
91        }
92
93        @Override
94        public int getSize() {
95            return items.size();
96        }
97
98        @Override
99        public String getElementAt(int index) {
100            return items.get(index);
101        }
102    }
103}
104