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 8191436
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 
main(String[] args)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 
CreateGUIAndTest(String lookAndFeel)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.SINGLE_INTERVAL_SELECTION);
76         selectionModel.setSelectionInterval(0, 1);
77         checkSelection(list, new int[]{0, 1}, lookAndFeel);
78 
79         selectionModel.setSelectionMode(
80                 ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
81         selectionModel.setSelectionInterval(0, 2);
82         checkSelection(list, new int[]{0, 1, 2}, lookAndFeel);
83 
84         selectionModel.addSelectionInterval(5, 7);
85         checkSelection(list, new int[]{0, 1, 2, 5, 6, 7}, lookAndFeel);
86 
87         selectionModel
88                 .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
89         checkSelection(list, new int[]{0, 1, 2}, lookAndFeel);
90 
91         selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
92         checkSelection(list, new int[]{0}, lookAndFeel);
93 
94         selectionModel
95                 .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
96         selectionModel.addSelectionInterval(4, 5);
97         checkSelection(list, new int[]{4, 5}, lookAndFeel);
98 
99         selectionModel.addSelectionInterval(0, 2);
100         checkSelection(list, new int[]{0, 1, 2}, lookAndFeel);
101 
102         selectionModel.setSelectionMode(
103                 ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
104         selectionModel.addSelectionInterval(6, 7);
105         checkSelection(list, new int[]{0, 1, 2, 6, 7}, lookAndFeel);
106 
107         System.out.println("Test passed for " + lookAndFeel);
108     }
109 
checkSelection(JList list, int[] selectionArray, String lookAndFeel)110     static void checkSelection(JList list, int[] selectionArray,
111                                String lookAndFeel) throws RuntimeException {
112         int[] listSelection = list.getSelectedIndices();
113         if (listSelection.length != selectionArray.length) {
114             System.out.println("Expected: " + Arrays.toString(selectionArray));
115             System.out.println("Actual: " + Arrays.toString(listSelection));
116             throw new RuntimeException("Wrong selection for " + lookAndFeel);
117         }
118 
119         Arrays.sort(listSelection);
120         Arrays.sort(selectionArray);
121 
122         if (!Arrays.equals(listSelection, selectionArray)) {
123             System.out.println("Expected: " + Arrays.toString(selectionArray));
124             System.out.println("Actual: " + Arrays.toString(listSelection));
125             throw new RuntimeException("Wrong selection for " + lookAndFeel);
126         }
127     }
128 }
129