Path: blob/master/test/jdk/javax/swing/JList/ListSelectionModelTest.java
41149 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 819143627* @summary Tests JListSelectionModel setSelection functionality28*/2930import javax.swing.JList;31import javax.swing.DefaultListModel;32import javax.swing.ListSelectionModel;33import javax.swing.SwingUtilities;34import javax.swing.UIManager.LookAndFeelInfo;3536import static javax.swing.UIManager.getInstalledLookAndFeels;37import static javax.swing.UIManager.setLookAndFeel;3839import java.util.Arrays;4041public class ListSelectionModelTest {4243public static void main(String[] args) throws Exception {4445final LookAndFeelInfo[] lookAndFeelInfoArray =46getInstalledLookAndFeels();4748for (LookAndFeelInfo lookAndFeelInfo : lookAndFeelInfoArray) {49SwingUtilities.invokeAndWait(new Runnable() {50@Override51public void run() {52try {53CreateGUIAndTest(lookAndFeelInfo.getClassName());54} catch (Exception e) {55e.printStackTrace();56throw new RuntimeException("Exception while running test");57}58}59});60}61}6263static void CreateGUIAndTest(String lookAndFeel) throws Exception{64setLookAndFeel(lookAndFeel);65DefaultListModel listModel = new DefaultListModel();66for (int j = 0; j < 10; j++) {67listModel.add(j, "Item: " + j);68}6970JList list = new JList(listModel);7172ListSelectionModel selectionModel = list.getSelectionModel();73selectionModel.setSelectionMode(74ListSelectionModel.SINGLE_INTERVAL_SELECTION);75selectionModel.setSelectionInterval(0, 1);76checkSelection(list, new int[]{0, 1}, lookAndFeel);7778selectionModel.setSelectionMode(79ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);80selectionModel.setSelectionInterval(0, 2);81checkSelection(list, new int[]{0, 1, 2}, lookAndFeel);8283selectionModel.addSelectionInterval(5, 7);84checkSelection(list, new int[]{0, 1, 2, 5, 6, 7}, lookAndFeel);8586selectionModel87.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);88checkSelection(list, new int[]{0, 1, 2}, lookAndFeel);8990selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);91checkSelection(list, new int[]{0}, lookAndFeel);9293selectionModel94.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);95selectionModel.addSelectionInterval(4, 5);96checkSelection(list, new int[]{4, 5}, lookAndFeel);9798selectionModel.addSelectionInterval(0, 2);99checkSelection(list, new int[]{0, 1, 2}, lookAndFeel);100101selectionModel.setSelectionMode(102ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);103selectionModel.addSelectionInterval(6, 7);104checkSelection(list, new int[]{0, 1, 2, 6, 7}, lookAndFeel);105106System.out.println("Test passed for " + lookAndFeel);107}108109static void checkSelection(JList list, int[] selectionArray,110String lookAndFeel) throws RuntimeException {111int[] listSelection = list.getSelectedIndices();112if (listSelection.length != selectionArray.length) {113System.out.println("Expected: " + Arrays.toString(selectionArray));114System.out.println("Actual: " + Arrays.toString(listSelection));115throw new RuntimeException("Wrong selection for " + lookAndFeel);116}117118Arrays.sort(listSelection);119Arrays.sort(selectionArray);120121if (!Arrays.equals(listSelection, selectionArray)) {122System.out.println("Expected: " + Arrays.toString(selectionArray));123System.out.println("Actual: " + Arrays.toString(listSelection));124throw new RuntimeException("Wrong selection for " + lookAndFeel);125}126}127}128129130