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 
  24 /*
  25  * @test
  26  * @key headful
  27  * @bug 6463710
  28  * @summary Tests JListSelectionModel setSelection functionality
  29  */
  30 
  31 import javax.swing.JList;
  32 import javax.swing.DefaultListModel;
  33 import javax.swing.ListSelectionModel;
  34 import javax.swing.SwingUtilities;
  35 import javax.swing.UIManager.LookAndFeelInfo;
  36 
  37 import static javax.swing.UIManager.getInstalledLookAndFeels;
  38 import static javax.swing.UIManager.setLookAndFeel;
  39 
  40 import java.util.Arrays;
  41 
  42 public class ListSelectionModelTest {
  43 
  44     public static void main(String[] args) throws Exception {
  45 
  46         final LookAndFeelInfo[] lookAndFeelInfoArray =
  47                 getInstalledLookAndFeels();
  48 
  49         for (LookAndFeelInfo lookAndFeelInfo : lookAndFeelInfoArray) {
  50             SwingUtilities.invokeAndWait(new Runnable() {
  51                 @Override
  52                 public void run() {
  53                     try {
  54                         CreateGUIAndTest(lookAndFeelInfo.getClassName());
  55                     } catch (Exception e) {
  56                         e.printStackTrace();
  57                         throw new RuntimeException("Exception while running test");
  58                     }
  59                 }
  60             });
  61         }
  62     }
  63 
  64     static void CreateGUIAndTest(String lookAndFeel) throws Exception{
  65         setLookAndFeel(lookAndFeel);
  66         DefaultListModel listModel = new DefaultListModel();
  67         for (int j = 0; j < 10; j++) {
  68             listModel.add(j, "Item: " + j);
  69         }
  70 
  71         JList list = new JList(listModel);
  72 
  73         ListSelectionModel selectionModel = list.getSelectionModel();
  74         selectionModel.setSelectionMode(
  75                 ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  76         selectionModel.setSelectionInterval(0, 2);
  77         checkSelection(list, new int[]{0, 1, 2}, lookAndFeel);
  78 
  79         selectionModel.addSelectionInterval(5, 7);
  80         checkSelection(list, new int[]{0, 1, 2, 5, 6, 7}, lookAndFeel);
  81 
  82         selectionModel
  83                 .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
  84         checkSelection(list, new int[]{0, 1, 2}, lookAndFeel);
  85 
  86         selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  87         checkSelection(list, new int[]{0}, lookAndFeel);
  88 
  89         selectionModel
  90                 .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
  91         selectionModel.addSelectionInterval(4, 5);
  92         checkSelection(list, new int[]{4, 5}, lookAndFeel);
  93 
  94         selectionModel.addSelectionInterval(0, 2);
  95         checkSelection(list, new int[]{0, 1, 2}, lookAndFeel);
  96 
  97         selectionModel.setSelectionMode(
  98                 ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  99         selectionModel.addSelectionInterval(6, 7);
 100         checkSelection(list, new int[]{0, 1, 2, 6, 7}, lookAndFeel);
 101 
 102         System.out.println("Test passed for " + lookAndFeel);
 103     }
 104 
 105     static void checkSelection(JList list, int[] selectionArray,
 106                                String lookAndFeel) throws RuntimeException {
 107         int[] listSelection = list.getSelectedIndices();
 108         if (listSelection.length != selectionArray.length) {
 109             System.out.println("Expected: " + Arrays.toString(selectionArray));
 110             System.out.println("Actual: " + Arrays.toString(listSelection));
 111             throw new RuntimeException("Wrong selection for " + lookAndFeel);
 112         }
 113 
 114         Arrays.sort(listSelection);
 115         Arrays.sort(selectionArray);
 116 
 117         if (!Arrays.equals(listSelection, selectionArray)) {
 118             System.out.println("Expected: " + Arrays.toString(selectionArray));
 119             System.out.println("Actual: " + Arrays.toString(listSelection));
 120             throw new RuntimeException("Wrong selection for " + lookAndFeel);
 121         }
 122     }
 123 }