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 8154069
  28  * @summary Jaws reads wrong values from comboboxes when no element is selected
  29  * @run main Bug8154069
  30  */
  31 
  32 import javax.accessibility.Accessible;
  33 import javax.accessibility.AccessibleContext;
  34 import javax.accessibility.AccessibleSelection;
  35 import javax.swing.JComboBox;
  36 import javax.swing.JFrame;
  37 import javax.swing.SwingUtilities;
  38 import javax.swing.UIManager;
  39 import javax.swing.plaf.nimbus.NimbusLookAndFeel;
  40 
  41 public class Bug8154069 {
  42 
  43     private static JFrame frame;
  44     private static volatile Exception exception = null;
  45 
  46     public static void main(String args[]) throws Exception {
  47         try {
  48             try {
  49                 UIManager.setLookAndFeel(new NimbusLookAndFeel());
  50             } catch (Exception e) {
  51                 throw new RuntimeException(e);
  52             }
  53 
  54             SwingUtilities.invokeAndWait(() -> {
  55                 frame = new JFrame();
  56                 String[] petStrings = { "Bird", "Cat" };
  57                 JComboBox<String> cb = new JComboBox<>(petStrings);
  58                 cb.setSelectedIndex(1);  // select Cat
  59                 frame.add(cb);
  60                 frame.pack();
  61                 try {
  62                     cb.setSelectedIndex(-1);
  63                     int i = cb.getSelectedIndex();
  64                     if (i != -1) {
  65                         throw new RuntimeException("getSelectedIndex is not -1");
  66                     }
  67                     Object o = cb.getSelectedItem();
  68                     if (o != null) {
  69                         throw new RuntimeException("getSelectedItem is not null");
  70                     }
  71                     AccessibleContext ac = cb.getAccessibleContext();
  72                     AccessibleSelection as = ac.getAccessibleSelection();
  73                     int count = as.getAccessibleSelectionCount();
  74                     if (count != 0) {
  75                         throw new RuntimeException("getAccessibleSelection count is not 0");
  76                     }
  77                     Accessible a = as.getAccessibleSelection(0);
  78                     if (a != null) {
  79                         throw new RuntimeException("getAccessibleSelection(0) is not null");
  80                     }
  81                 } catch (Exception e) {
  82                     exception = e;
  83                 }
  84             });
  85             if (exception != null) {
  86                 System.out.println("Test failed: " + exception.getMessage());
  87                 throw exception;
  88             } else {
  89                 System.out.println("Test passed.");
  90             }
  91         } finally {
  92             SwingUtilities.invokeAndWait(() -> {
  93                 if (frame != null) { frame.dispose(); }
  94             });
  95         }
  96     }
  97 
  98 }