1 /*
   2  * Copyright (c) 2016, 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 /*
  25  * @test
  26  * @bug 8161483
  27  * @summary Implement AccessibleAction in JList.AccessibleJList.AccessibleJListChild
  28  * @run main Bug8161483
  29  */
  30 
  31 import javax.accessibility.Accessible;
  32 import javax.accessibility.AccessibleAction;
  33 import javax.accessibility.AccessibleContext;
  34 import javax.accessibility.AccessibleSelection;
  35 import javax.swing.DefaultListModel;
  36 import javax.swing.JFrame;
  37 import javax.swing.JList;
  38 import javax.swing.SwingUtilities;
  39 
  40 public class Bug8161483 extends JFrame {
  41 
  42     private static JFrame frame;
  43     private static volatile Exception exception = null;
  44     private JList<String> countryList;
  45 
  46     public static void main(String args[]) throws Exception {
  47         try {
  48             SwingUtilities.invokeAndWait(() -> {
  49                 DefaultListModel<String> listModel = new DefaultListModel<>();
  50                 listModel.addElement("one");
  51                 listModel.addElement("two");
  52                 listModel.addElement("three");
  53                 JList<String> list = new JList<>(listModel);
  54                 frame = new JFrame();
  55                 frame.add(list);
  56                 frame.pack();
  57                 try {
  58                     AccessibleContext acList = list.getAccessibleContext();
  59                     Accessible accChild = acList.getAccessibleChild(1);
  60                     AccessibleContext acChild = accChild.getAccessibleContext();
  61                     AccessibleAction aa = acChild.getAccessibleAction();
  62                     int c = aa.getAccessibleActionCount();
  63                     if (c != 1) {
  64                         throw new RuntimeException("getAccessibleActionCount is not 1");
  65                     }
  66                     String s = aa.getAccessibleActionDescription(0);
  67                     if (!s.equals("click")) {
  68                         throw new RuntimeException("getAccessibleActionDescription is not click");
  69                     }
  70                     boolean b = aa.doAccessibleAction(0);
  71                     if (!b) {
  72                         throw new RuntimeException("doAccessibleAction did not return true");
  73                     }
  74                     AccessibleSelection as = acList.getAccessibleSelection();
  75                     int asc = as.getAccessibleSelectionCount();
  76                     if (asc != 1) {
  77                         throw new RuntimeException("getAccessibleSelectionCount is not 1");
  78                     }
  79                     boolean isSelected = as.isAccessibleChildSelected(0);
  80                     if (isSelected) {
  81                         throw new RuntimeException("isAccessibleChildSelected(0) did not return false");
  82                     }
  83                     isSelected = as.isAccessibleChildSelected(1);
  84                     if (!isSelected) {
  85                         throw new RuntimeException("isAccessibleChildSelected(1) did not return true");
  86                     }
  87                 } catch (Exception e) {
  88                     exception = e;
  89                 }
  90             });
  91             if (exception != null) {
  92                 System.out.println("Test failed: " + exception.getMessage());
  93                 throw exception;
  94             } else {
  95                 System.out.println("Test passed.");
  96             }
  97         } finally {
  98             SwingUtilities.invokeAndWait(() -> {
  99                 frame.dispose();
 100             });
 101         }
 102     }
 103 
 104 }