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