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